BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / ui / accelerated_widget_mac / io_surface_layer.mm
blobd880316d9168285d05aeed0b8e657f92ba0e1efa
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.
5 #include "ui/accelerated_widget_mac/io_surface_layer.h"
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>
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"
21 ////////////////////////////////////////////////////////////////////////////////
22 // IOSurfaceLayerHelper
24 namespace ui {
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) {}
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);
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);
52   has_pending_frame_ = true;
53   needs_display_ = true;
54   timer_.Reset();
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   }
68 void IOSurfaceLayerHelper::SetNeedsDisplay() {
69   needs_display_ = true;
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   }
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   }
96   return needs_display_;
99 void IOSurfaceLayerHelper::DidDraw(bool success) {
100   needs_display_ = false;
101   AckPendingFrame(success);
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);
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];
125   [layer_ setNeedsDisplay];
126   DisplayIfNeededAndAck();
129 void IOSurfaceLayerHelper::DisplayIfNeededAndAck() {
130   if (!needs_display_)
131     return;
133   // As in SetNeedsDisplayAndDisplayAndAck, disable asynchronous drawing before
134   // issuing displayIfNeeded.
135   // http://crbug.com/395827
136   if ([layer_ isAsynchronous])
137     [layer_ setAsynchronous:NO];
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];
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);
151 void IOSurfaceLayerHelper::TimerFired() {
152   DisplayIfNeededAndAck();
155 void IOSurfaceLayerHelper::BeginPumpingFrames() {
156   is_pumping_frames_ = true;
159 void IOSurfaceLayerHelper::EndPumpingFrames() {
160   is_pumping_frames_ = false;
161   DisplayIfNeededAndAck();
164 }  // namespace ui
166 ////////////////////////////////////////////////////////////////////////////////
167 // IOSurfaceLayer
169 @implementation IOSurfaceLayer
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));
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     }
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;
199 - (void)dealloc {
200   DCHECK(!helper_);
201   [super dealloc];
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);
210 - (void)poisonContextAndSharegroup {
211   context_->PoisonContextAndSharegroup();
214 - (bool)hasBeenPoisoned {
215   return context_->HasBeenPoisoned();
218 - (float)scaleFactor {
219   if ([self respondsToSelector:(@selector(contentsScale))])
220     return [self contentsScale];
221   return 1;
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;
234 - (void)resetClient {
235   helper_.reset();
238 - (void)gotNewFrame {
239   helper_->GotNewFrame();
242 - (void)setNeedsDisplayAndDisplayAndAck {
243   helper_->SetNeedsDisplayAndDisplayAndAck();
246 - (void)displayIfNeededAndAck {
247   helper_->DisplayIfNeededAndAck();
250 - (void)beginPumpingFrames {
251   helper_->BeginPumpingFrames();
254 - (void)endPumpingFrames {
255   helper_->EndPumpingFrames();
258 // The remaining methods implement the CAOpenGLLayer interface.
260 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
261   if (!context_.get())
262     return [super copyCGLPixelFormatForDisplayMask:mask];
263   return CGLRetainPixelFormat(CGLGetPixelFormat(context_->cgl_context()));
266 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
267   if (!context_.get())
268     return [super copyCGLContextForPixelFormat:pixelFormat];
269   return CGLRetainContext(context_->cgl_context());
272 - (void)setNeedsDisplay {
273   if (helper_)
274     helper_->SetNeedsDisplay();
275   [super setNeedsDisplay];
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;
287 - (void)drawInCGLContext:(CGLContextObj)glContext
288              pixelFormat:(CGLPixelFormatObj)pixelFormat
289             forLayerTime:(CFTimeInterval)timeInterval
290              displayTime:(const CVTimeStamp*)timeStamp {
291   TRACE_EVENT0("browser", "IOSurfaceLayer::drawInCGLContext");
293   bool draw_succeeded = iosurface_->DrawIOSurface();
294   if (helper_)
295     helper_->DidDraw(draw_succeeded);
297   [super drawInCGLContext:glContext
298               pixelFormat:pixelFormat
299              forLayerTime:timeInterval
300               displayTime:timeStamp];
303 @end