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 "content/browser/renderer_host/compositing_iosurface_layer_mac.h"
7 #include <CoreFoundation/CoreFoundation.h>
10 #include "base/mac/mac_util.h"
11 #include "base/mac/sdk_forward_declarations.h"
12 #include "content/browser/renderer_host/render_widget_host_impl.h"
13 #include "content/browser/renderer_host/render_widget_host_view_mac.h"
14 #include "content/browser/renderer_host/compositing_iosurface_context_mac.h"
15 #include "content/browser/renderer_host/compositing_iosurface_mac.h"
16 #include "ui/base/cocoa/animation_utils.h"
17 #include "ui/gfx/size_conversions.h"
18 #include "ui/gl/gpu_switching_manager.h"
20 @implementation CompositingIOSurfaceLayer
22 - (id)initWithRenderWidgetHostViewMac:(content::RenderWidgetHostViewMac*)r {
23 if (self = [super init]) {
24 renderWidgetHostView_ = r;
25 context_ = content::CompositingIOSurfaceContext::Get(
26 content::CompositingIOSurfaceContext::kCALayerContextWindowNumber);
30 [self setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)];
31 [self setAnchorPoint:CGPointMake(0, 0)];
32 // Setting contents gravity is necessary to prevent the layer from being
33 // scaled during dyanmic resizes (especially with devtools open).
34 [self setContentsGravity:kCAGravityTopLeft];
35 if (renderWidgetHostView_->compositing_iosurface_ &&
36 [self respondsToSelector:(@selector(setContentsScale:))]) {
37 [self setContentsScale:
38 renderWidgetHostView_->compositing_iosurface_->scale_factor()];
44 - (void)disableCompositing{
45 renderWidgetHostView_ = nil;
49 if (context_ && context_->is_vsync_disabled()) {
50 // If vsync is disabled, draw immediately and don't bother trying to use
51 // the isAsynchronous property to ensure smooth animation.
52 [self setNeedsDisplay];
53 [self displayIfNeeded];
55 // Calls to setNeedsDisplay can sometimes be ignored, especially if issued
56 // rapidly (e.g, with vsync off). This is unacceptable because the failure
57 // to ack a single frame will hang the renderer. Ensure that the renderer
60 renderWidgetHostView_->SendPendingSwapAck();
63 if (![self isAsynchronous])
64 [self setAsynchronous:YES];
68 - (void)timerSinceGotNewFrameFired {
69 if (![self isAsynchronous])
72 [self setAsynchronous:NO];
74 // If there was a pending frame, ensure that it goes through.
76 [self setNeedsDisplay];
77 [self displayIfNeeded];
79 // If that fails then ensure that, at a minimum, the renderer is not blocked.
81 renderWidgetHostView_->SendPendingSwapAck();
84 // The remaining methods implement the CAOpenGLLayer interface.
86 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
88 return [super copyCGLPixelFormatForDisplayMask:mask];
89 return CGLRetainPixelFormat(CGLGetPixelFormat(context_->cgl_context()));
92 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
94 return [super copyCGLContextForPixelFormat:pixelFormat];
95 return CGLRetainContext(context_->cgl_context());
98 - (void)setNeedsDisplay {
100 [super setNeedsDisplay];
103 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext
104 pixelFormat:(CGLPixelFormatObj)pixelFormat
105 forLayerTime:(CFTimeInterval)timeInterval
106 displayTime:(const CVTimeStamp*)timeStamp {
107 if (!renderWidgetHostView_)
110 // Add an instantaneous blip to the PendingSwapAck state to indicate
111 // that CoreAnimation asked if a frame is ready. A blip up to to 3 (usually
112 // from 2, indicating that a swap ack is pending) indicates that we requested
113 // a draw. A blip up to 1 (usually from 0, indicating there is no pending swap
114 // ack) indicates that we did not request a draw. This would be more natural
115 // to do with a tracing pseudo-thread
116 // http://crbug.com/366300
117 TRACE_COUNTER_ID1("browser", "PendingSwapAck", renderWidgetHostView_,
118 needsDisplay_ ? 3 : 1);
119 TRACE_COUNTER_ID1("browser", "PendingSwapAck", renderWidgetHostView_,
120 renderWidgetHostView_->HasPendingSwapAck() ? 2 : 0);
122 return needsDisplay_;
125 - (void)drawInCGLContext:(CGLContextObj)glContext
126 pixelFormat:(CGLPixelFormatObj)pixelFormat
127 forLayerTime:(CFTimeInterval)timeInterval
128 displayTime:(const CVTimeStamp*)timeStamp {
129 TRACE_EVENT0("browser", "CompositingIOSurfaceLayer::drawInCGLContext");
132 (context_ && context_->cgl_context() != glContext) ||
133 !renderWidgetHostView_ ||
134 !renderWidgetHostView_->compositing_iosurface_ ||
135 !renderWidgetHostView_->compositing_iosurface_->HasIOSurface()) {
136 glClearColor(1, 1, 1, 1);
137 glClear(GL_COLOR_BUFFER_BIT);
141 // The correct viewport to cover the layer will be set up by the caller.
142 // Transform this into a window size for DrawIOSurface, where it will be
143 // transformed back into this viewport.
145 glGetIntegerv(GL_VIEWPORT, viewport);
146 gfx::Rect window_rect(viewport[0], viewport[1], viewport[2], viewport[3]);
147 float window_scale_factor = 1.f;
148 if ([self respondsToSelector:(@selector(contentsScale))])
149 window_scale_factor = [self contentsScale];
150 window_rect = ToNearestRect(
151 gfx::ScaleRect(window_rect, 1.f/window_scale_factor));
153 if (!renderWidgetHostView_->compositing_iosurface_->DrawIOSurface(
158 renderWidgetHostView_->GotAcceleratedCompositingError();
163 renderWidgetHostView_->SendPendingLatencyInfoToHost();
164 renderWidgetHostView_->SendPendingSwapAck();
166 [super drawInCGLContext:glContext
167 pixelFormat:pixelFormat
168 forLayerTime:timeInterval
169 displayTime:timeStamp];