1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/compositing_iosurface_layer_mac.h"
#include <CoreFoundation/CoreFoundation.h>
#include <OpenGL/gl.h>
#include "base/mac/mac_util.h"
#include "base/mac/sdk_forward_declarations.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_view_mac.h"
#include "content/browser/renderer_host/compositing_iosurface_context_mac.h"
#include "content/browser/renderer_host/compositing_iosurface_mac.h"
#include "ui/base/cocoa/animation_utils.h"
#include "ui/gfx/size_conversions.h"
#include "ui/gl/gpu_switching_manager.h"
////////////////////////////////////////////////////////////////////////////////
// CompositingIOSurfaceLayerHelper
namespace content {
CompositingIOSurfaceLayerHelper::CompositingIOSurfaceLayerHelper(
CompositingIOSurfaceLayerClient* client,
CompositingIOSurfaceLayer* layer)
: client_(client),
layer_(layer),
needs_display_(false),
has_pending_frame_(false),
did_not_draw_counter_(0),
is_pumping_frames_(false),
timer_(
FROM_HERE,
base::TimeDelta::FromSeconds(1) / 6,
this,
&CompositingIOSurfaceLayerHelper::TimerFired) {}
CompositingIOSurfaceLayerHelper::~CompositingIOSurfaceLayerHelper() {
// Any acks that were waiting on this layer to draw will not occur, so ack
// them now to prevent blocking the renderer.
AckPendingFrame(true);
}
void CompositingIOSurfaceLayerHelper::GotNewFrame() {
// A trace value of 2 indicates that there is a pending swap ack. See
// canDrawInCGLContext for other value meanings.
TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, 2);
has_pending_frame_ = true;
needs_display_ = true;
timer_.Reset();
// If reqested, draw immediately and don't bother trying to use the
// isAsynchronous property to ensure smooth animation. If this is while
// frames are being pumped then ack and display immediately to get a
// correct-sized frame displayed as soon as possible.
if (is_pumping_frames_ || client_->AcceleratedLayerShouldAckImmediately()) {
SetNeedsDisplayAndDisplayAndAck();
} else {
if (![layer_ isAsynchronous])
[layer_ setAsynchronous:YES];
}
}
void CompositingIOSurfaceLayerHelper::SetNeedsDisplay() {
needs_display_ = true;
}
bool CompositingIOSurfaceLayerHelper::CanDraw() {
// If we return NO 30 times in a row, switch to being synchronous to avoid
// burning CPU cycles on this callback.
if (needs_display_) {
did_not_draw_counter_ = 0;
} else {
did_not_draw_counter_ += 1;
if (did_not_draw_counter_ == 30)
[layer_ setAsynchronous:NO];
}
// Add an instantaneous blip to the PendingSwapAck state to indicate
// that CoreAnimation asked if a frame is ready. A blip up to to 3 (usually
// from 2, indicating that a swap ack is pending) indicates that we
// requested a draw. A blip up to 1 (usually from 0, indicating there is no
// pending swap ack) indicates that we did not request a draw. This would
// be more natural to do with a tracing pseudo-thread
// http://crbug.com/366300
TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, needs_display_ ? 3 : 1);
TRACE_COUNTER_ID1("browser", "PendingSwapAck", this,
has_pending_frame_ ? 2 : 0);
return needs_display_;
}
void CompositingIOSurfaceLayerHelper::DidDraw(bool success) {
needs_display_ = false;
AckPendingFrame(success);
}
void CompositingIOSurfaceLayerHelper::AckPendingFrame(bool success) {
if (!has_pending_frame_)
return;
has_pending_frame_ = false;
client_->AcceleratedLayerDidDrawFrame(success);
// A trace value of 0 indicates that there is no longer a pending swap ack.
TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, 0);
}
void CompositingIOSurfaceLayerHelper::SetNeedsDisplayAndDisplayAndAck() {
// Drawing using setNeedsDisplay and displayIfNeeded will result in
// subsequent canDrawInCGLContext callbacks getting dropped, and jerky
// animation. Disable asynchronous drawing before issuing these calls as a
// workaround.
// http://crbug.com/395827
if ([layer_ isAsynchronous])
[layer_ setAsynchronous:NO];
[layer_ setNeedsDisplay];
DisplayIfNeededAndAck();
}
void CompositingIOSurfaceLayerHelper::DisplayIfNeededAndAck() {
if (!needs_display_)
return;
// As in SetNeedsDisplayAndDisplayAndAck, disable asynchronous drawing before
// issuing displayIfNeeded.
// http://crbug.com/395827
if ([layer_ isAsynchronous])
[layer_ setAsynchronous:NO];
// Do not bother drawing while pumping new frames -- wait until the waiting
// block ends to draw any of the new frames.
if (!is_pumping_frames_)
[layer_ displayIfNeeded];
// Calls to setNeedsDisplay can sometimes be ignored, especially if issued
// rapidly (e.g, with vsync off). This is unacceptable because the failure
// to ack a single frame will hang the renderer. Ensure that the renderer
// not be blocked by lying and claiming that we drew the frame.
AckPendingFrame(true);
}
void CompositingIOSurfaceLayerHelper::TimerFired() {
SetNeedsDisplayAndDisplayAndAck();
}
void CompositingIOSurfaceLayerHelper::BeginPumpingFrames() {
is_pumping_frames_ = true;
}
void CompositingIOSurfaceLayerHelper::EndPumpingFrames() {
is_pumping_frames_ = false;
DisplayIfNeededAndAck();
}
} // namespace content
////////////////////////////////////////////////////////////////////////////////
// CompositingIOSurfaceLayer
@implementation CompositingIOSurfaceLayer
- (content::CompositingIOSurfaceMac*)iosurface {
return iosurface_.get();
}
- (content::CompositingIOSurfaceContext*)context {
return context_.get();
}
- (id)initWithIOSurface:(scoped_refptr<content::CompositingIOSurfaceMac>)
iosurface
withScaleFactor:(float)scale_factor
withClient:(content::CompositingIOSurfaceLayerClient*)client {
if (self = [super init]) {
helper_.reset(new content::CompositingIOSurfaceLayerHelper(client, self));
iosurface_ = iosurface;
context_ = content::CompositingIOSurfaceContext::Get(
content::CompositingIOSurfaceContext::kCALayerContextWindowNumber);
DCHECK(context_);
[self setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)];
[self setAnchorPoint:CGPointMake(0, 0)];
// Setting contents gravity is necessary to prevent the layer from being
// scaled during dyanmic resizes (especially with devtools open).
[self setContentsGravity:kCAGravityTopLeft];
if ([self respondsToSelector:(@selector(setContentsScale:))]) {
[self setContentsScale:scale_factor];
}
}
return self;
}
- (void)dealloc {
DCHECK(!helper_);
[super dealloc];
}
- (void)resetClient {
helper_.reset();
}
- (void)gotNewFrame {
helper_->GotNewFrame();
}
- (void)setNeedsDisplayAndDisplayAndAck {
helper_->SetNeedsDisplayAndDisplayAndAck();
}
- (void)displayIfNeededAndAck {
helper_->DisplayIfNeededAndAck();
}
- (void)beginPumpingFrames {
helper_->BeginPumpingFrames();
}
- (void)endPumpingFrames {
helper_->EndPumpingFrames();
}
// The remaining methods implement the CAOpenGLLayer interface.
- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
if (!context_)
return [super copyCGLPixelFormatForDisplayMask:mask];
return CGLRetainPixelFormat(CGLGetPixelFormat(context_->cgl_context()));
}
- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
if (!context_)
return [super copyCGLContextForPixelFormat:pixelFormat];
return CGLRetainContext(context_->cgl_context());
}
- (void)setNeedsDisplay {
if (helper_)
helper_->SetNeedsDisplay();
[super setNeedsDisplay];
}
- (BOOL)canDrawInCGLContext:(CGLContextObj)glContext
pixelFormat:(CGLPixelFormatObj)pixelFormat
forLayerTime:(CFTimeInterval)timeInterval
displayTime:(const CVTimeStamp*)timeStamp {
if (helper_)
return helper_->CanDraw();
return NO;
}
- (void)drawInCGLContext:(CGLContextObj)glContext
pixelFormat:(CGLPixelFormatObj)pixelFormat
forLayerTime:(CFTimeInterval)timeInterval
displayTime:(const CVTimeStamp*)timeStamp {
TRACE_EVENT0("browser", "CompositingIOSurfaceLayer::drawInCGLContext");
if (!iosurface_->HasIOSurface() || context_->cgl_context() != glContext) {
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
return;
}
// The correct viewport to cover the layer will be set up by the caller.
// Transform this into a window size for DrawIOSurface, where it will be
// transformed back into this viewport.
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
gfx::Rect window_rect(viewport[0], viewport[1], viewport[2], viewport[3]);
float window_scale_factor = 1.f;
if ([self respondsToSelector:(@selector(contentsScale))])
window_scale_factor = [self contentsScale];
window_rect = ToNearestRect(
gfx::ScaleRect(window_rect, 1.f/window_scale_factor));
bool draw_succeeded = iosurface_->DrawIOSurface(
context_, window_rect, window_scale_factor);
if (helper_)
helper_->DidDraw(draw_succeeded);
[super drawInCGLContext:glContext
pixelFormat:pixelFormat
forLayerTime:timeInterval
displayTime:timeStamp];
}
@end
|