Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: ui/accelerated_widget_mac/io_surface_layer.mm

Issue 1420533005: Mac: Kill lots of AcceleratedWidget code (with fire) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Feedback 2 Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/accelerated_widget_mac/io_surface_layer.h"
6
7 #include <CoreFoundation/CoreFoundation.h>
8 #include <OpenGL/CGLIOSurface.h>
9 #include <OpenGL/CGLRenderers.h>
10 #include <OpenGL/gl.h>
11 #include <OpenGL/OpenGL.h>
12
13 #include "base/mac/sdk_forward_declarations.h"
14 #include "base/trace_event/trace_event.h"
15 #include "ui/accelerated_widget_mac/io_surface_context.h"
16 #include "ui/accelerated_widget_mac/io_surface_texture.h"
17 #include "ui/base/cocoa/animation_utils.h"
18 #include "ui/gfx/geometry/size_conversions.h"
19 #include "ui/gl/gpu_switching_manager.h"
20
21 ////////////////////////////////////////////////////////////////////////////////
22 // IOSurfaceLayerHelper
23
24 namespace ui {
25
26 IOSurfaceLayerHelper::IOSurfaceLayerHelper(
27 IOSurfaceLayerClient* client,
28 IOSurfaceLayer* layer)
29 : client_(client),
30 layer_(layer),
31 needs_display_(false),
32 has_pending_frame_(false),
33 did_not_draw_counter_(0),
34 is_pumping_frames_(false),
35 timer_(
36 FROM_HERE,
37 base::TimeDelta::FromSeconds(1) / 6,
38 this,
39 &IOSurfaceLayerHelper::TimerFired) {}
40
41 IOSurfaceLayerHelper::~IOSurfaceLayerHelper() {
42 // Any acks that were waiting on this layer to draw will not occur, so ack
43 // them now to prevent blocking the renderer.
44 AckPendingFrame(true);
45 }
46
47 void IOSurfaceLayerHelper::GotNewFrame() {
48 // A trace value of 2 indicates that there is a pending swap ack. See
49 // canDrawInCGLContext for other value meanings.
50 TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, 2);
51
52 has_pending_frame_ = true;
53 needs_display_ = true;
54 timer_.Reset();
55
56 // If reqested, draw immediately and don't bother trying to use the
57 // isAsynchronous property to ensure smooth animation. If this is while
58 // frames are being pumped then ack and display immediately to get a
59 // correct-sized frame displayed as soon as possible.
60 if (is_pumping_frames_ || client_->IOSurfaceLayerShouldAckImmediately()) {
61 SetNeedsDisplayAndDisplayAndAck();
62 } else {
63 if (![layer_ isAsynchronous])
64 [layer_ setAsynchronous:YES];
65 }
66 }
67
68 void IOSurfaceLayerHelper::SetNeedsDisplay() {
69 needs_display_ = true;
70 }
71
72 bool IOSurfaceLayerHelper::CanDraw() {
73 // If we return NO 30 times in a row, switch to being synchronous to avoid
74 // burning CPU cycles on this callback.
75 if (needs_display_) {
76 did_not_draw_counter_ = 0;
77 } else {
78 did_not_draw_counter_ += 1;
79 if (did_not_draw_counter_ == 30)
80 [layer_ setAsynchronous:NO];
81 }
82
83 if (needs_display_) {
84 // If there is a draw pending then increase the signal from 2 to 3, to
85 // indicate that we are in the state where there is a swap pending and
86 // CoreAnimation has been committed to draw it.
87 TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, 3);
88 } else {
89 // If there is not a draw pending, then give an instantaneous blip up from
90 // 0 to 1, indicating that CoreAnimation was ready to draw a frame but we
91 // were not (or didn't have new content to draw).
92 TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, 1);
93 TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, 0);
94 }
95
96 return needs_display_;
97 }
98
99 void IOSurfaceLayerHelper::DidDraw(bool success) {
100 needs_display_ = false;
101 AckPendingFrame(success);
102 }
103
104 void IOSurfaceLayerHelper::AckPendingFrame(bool success) {
105 if (!has_pending_frame_)
106 return;
107 has_pending_frame_ = false;
108 if (success)
109 client_->IOSurfaceLayerDidDrawFrame();
110 else
111 client_->IOSurfaceLayerHitError();
112 // A trace value of 0 indicates that there is no longer a pending swap ack.
113 TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, 0);
114 }
115
116 void IOSurfaceLayerHelper::SetNeedsDisplayAndDisplayAndAck() {
117 // Drawing using setNeedsDisplay and displayIfNeeded will result in
118 // subsequent canDrawInCGLContext callbacks getting dropped, and jerky
119 // animation. Disable asynchronous drawing before issuing these calls as a
120 // workaround.
121 // http://crbug.com/395827
122 if ([layer_ isAsynchronous])
123 [layer_ setAsynchronous:NO];
124
125 [layer_ setNeedsDisplay];
126 DisplayIfNeededAndAck();
127 }
128
129 void IOSurfaceLayerHelper::DisplayIfNeededAndAck() {
130 if (!needs_display_)
131 return;
132
133 // As in SetNeedsDisplayAndDisplayAndAck, disable asynchronous drawing before
134 // issuing displayIfNeeded.
135 // http://crbug.com/395827
136 if ([layer_ isAsynchronous])
137 [layer_ setAsynchronous:NO];
138
139 // Do not bother drawing while pumping new frames -- wait until the waiting
140 // block ends to draw any of the new frames.
141 if (!is_pumping_frames_)
142 [layer_ displayIfNeeded];
143
144 // Calls to setNeedsDisplay can sometimes be ignored, especially if issued
145 // rapidly (e.g, with vsync off). This is unacceptable because the failure
146 // to ack a single frame will hang the renderer. Ensure that the renderer
147 // not be blocked by lying and claiming that we drew the frame.
148 AckPendingFrame(true);
149 }
150
151 void IOSurfaceLayerHelper::TimerFired() {
152 DisplayIfNeededAndAck();
153 }
154
155 void IOSurfaceLayerHelper::BeginPumpingFrames() {
156 is_pumping_frames_ = true;
157 }
158
159 void IOSurfaceLayerHelper::EndPumpingFrames() {
160 is_pumping_frames_ = false;
161 DisplayIfNeededAndAck();
162 }
163
164 } // namespace ui
165
166 ////////////////////////////////////////////////////////////////////////////////
167 // IOSurfaceLayer
168
169 @implementation IOSurfaceLayer
170
171 - (id)initWithClient:(ui::IOSurfaceLayerClient*)client
172 withScaleFactor:(float)scale_factor
173 needsGLFinishWorkaround:(bool)needs_gl_finish_workaround {
174 if (self = [super init]) {
175 helper_.reset(new ui::IOSurfaceLayerHelper(client, self));
176
177 iosurface_ = ui::IOSurfaceTexture::Create(
178 needs_gl_finish_workaround, false);
179 context_ = ui::IOSurfaceContext::Get(
180 ui::IOSurfaceContext::kCALayerContext);
181 if (!iosurface_.get() || !context_.get()) {
182 LOG(ERROR) << "Failed create CompositingIOSurface or context";
183 [self resetClient];
184 [self release];
185 return nil;
186 }
187
188 [self setAnchorPoint:CGPointMake(0, 0)];
189 // Setting contents gravity is necessary to prevent the layer from being
190 // scaled during dyanmic resizes (especially with devtools open).
191 [self setContentsGravity:kCAGravityTopLeft];
192 if ([self respondsToSelector:(@selector(setContentsScale:))]) {
193 [self setContentsScale:scale_factor];
194 }
195 }
196 return self;
197 }
198
199 - (void)dealloc {
200 DCHECK(!helper_);
201 [super dealloc];
202 }
203
204 - (bool)gotFrameWithIOSurface:(IOSurfaceID)io_surface_id
205 withPixelSize:(gfx::Size)pixel_size
206 withScaleFactor:(float)scale_factor {
207 return iosurface_->SetIOSurface(io_surface_id, pixel_size);
208 }
209
210 - (void)poisonContextAndSharegroup {
211 context_->PoisonContextAndSharegroup();
212 }
213
214 - (bool)hasBeenPoisoned {
215 return context_->HasBeenPoisoned();
216 }
217
218 - (float)scaleFactor {
219 if ([self respondsToSelector:(@selector(contentsScale))])
220 return [self contentsScale];
221 return 1;
222 }
223
224 - (int)rendererID {
225 GLint current_renderer_id = -1;
226 if (CGLGetParameter(context_->cgl_context(),
227 kCGLCPCurrentRendererID,
228 &current_renderer_id) == kCGLNoError) {
229 return current_renderer_id & kCGLRendererIDMatchingMask;
230 }
231 return -1;
232 }
233
234 - (void)resetClient {
235 helper_.reset();
236 }
237
238 - (void)gotNewFrame {
239 helper_->GotNewFrame();
240 }
241
242 - (void)setNeedsDisplayAndDisplayAndAck {
243 helper_->SetNeedsDisplayAndDisplayAndAck();
244 }
245
246 - (void)displayIfNeededAndAck {
247 helper_->DisplayIfNeededAndAck();
248 }
249
250 - (void)beginPumpingFrames {
251 helper_->BeginPumpingFrames();
252 }
253
254 - (void)endPumpingFrames {
255 helper_->EndPumpingFrames();
256 }
257
258 // The remaining methods implement the CAOpenGLLayer interface.
259
260 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
261 if (!context_.get())
262 return [super copyCGLPixelFormatForDisplayMask:mask];
263 return CGLRetainPixelFormat(CGLGetPixelFormat(context_->cgl_context()));
264 }
265
266 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
267 if (!context_.get())
268 return [super copyCGLContextForPixelFormat:pixelFormat];
269 return CGLRetainContext(context_->cgl_context());
270 }
271
272 - (void)setNeedsDisplay {
273 if (helper_)
274 helper_->SetNeedsDisplay();
275 [super setNeedsDisplay];
276 }
277
278 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext
279 pixelFormat:(CGLPixelFormatObj)pixelFormat
280 forLayerTime:(CFTimeInterval)timeInterval
281 displayTime:(const CVTimeStamp*)timeStamp {
282 if (helper_)
283 return helper_->CanDraw();
284 return NO;
285 }
286
287 - (void)drawInCGLContext:(CGLContextObj)glContext
288 pixelFormat:(CGLPixelFormatObj)pixelFormat
289 forLayerTime:(CFTimeInterval)timeInterval
290 displayTime:(const CVTimeStamp*)timeStamp {
291 TRACE_EVENT0("browser", "IOSurfaceLayer::drawInCGLContext");
292
293 bool draw_succeeded = iosurface_->DrawIOSurface();
294 if (helper_)
295 helper_->DidDraw(draw_succeeded);
296
297 [super drawInCGLContext:glContext
298 pixelFormat:pixelFormat
299 forLayerTime:timeInterval
300 displayTime:timeStamp];
301 }
302
303 @end
OLDNEW
« no previous file with comments | « ui/accelerated_widget_mac/io_surface_layer.h ('k') | ui/accelerated_widget_mac/io_surface_texture.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698