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 return needsDisplay_;
110 - (void)drawInCGLContext:(CGLContextObj)glContext
111 pixelFormat:(CGLPixelFormatObj)pixelFormat
112 forLayerTime:(CFTimeInterval)timeInterval
113 displayTime:(const CVTimeStamp*)timeStamp {
114 TRACE_EVENT0("browser", "CompositingIOSurfaceLayer::drawInCGLContext");
117 (context_ && context_->cgl_context() != glContext) ||
118 !renderWidgetHostView_ ||
119 !renderWidgetHostView_->compositing_iosurface_ ||
120 !renderWidgetHostView_->compositing_iosurface_->HasIOSurface()) {
121 glClearColor(1, 1, 1, 1);
122 glClear(GL_COLOR_BUFFER_BIT);
126 // The correct viewport to cover the layer will be set up by the caller.
127 // Transform this into a window size for DrawIOSurface, where it will be
128 // transformed back into this viewport.
130 glGetIntegerv(GL_VIEWPORT, viewport);
131 gfx::Rect window_rect(viewport[0], viewport[1], viewport[2], viewport[3]);
132 float window_scale_factor = 1.f;
133 if ([self respondsToSelector:(@selector(contentsScale))])
134 window_scale_factor = [self contentsScale];
135 window_rect = ToNearestRect(
136 gfx::ScaleRect(window_rect, 1.f/window_scale_factor));
138 if (!renderWidgetHostView_->compositing_iosurface_->DrawIOSurface(
143 renderWidgetHostView_->GotAcceleratedCompositingError();
148 renderWidgetHostView_->SendPendingLatencyInfoToHost();
149 renderWidgetHostView_->SendPendingSwapAck();
151 [super drawInCGLContext:glContext
152 pixelFormat:pixelFormat
153 forLayerTime:timeInterval
154 displayTime:timeStamp];