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/sdk_forward_declarations.h"
11 #include "content/browser/renderer_host/render_widget_host_view_mac.h"
12 #include "content/browser/renderer_host/compositing_iosurface_context_mac.h"
13 #include "content/browser/renderer_host/compositing_iosurface_mac.h"
14 #include "ui/base/cocoa/animation_utils.h"
15 #include "ui/gfx/size_conversions.h"
17 @implementation CompositingIOSurfaceLayer
19 @synthesize context = context_;
21 - (id)initWithRenderWidgetHostViewMac:(content::RenderWidgetHostViewMac*)r {
22 if (self = [super init]) {
23 renderWidgetHostView_ = r;
25 ScopedCAActionDisabler disabler;
26 [self setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)];
27 [self setAutoresizingMask:kCALayerWidthSizable | kCALayerHeightSizable];
28 [self setContentsGravity:kCAGravityTopLeft];
29 [self setFrame:NSRectToCGRect(
30 [renderWidgetHostView_->cocoa_view() bounds])];
31 [self setNeedsDisplay];
32 [self updateScaleFactor];
37 - (BOOL)ensureContext {
41 if (!renderWidgetHostView_)
44 if (renderWidgetHostView_->compositing_iosurface_) {
45 context_ = renderWidgetHostView_->compositing_iosurface_->context();
46 [context_->nsgl_context() clearDrawable];
50 context_ = content::CompositingIOSurfaceContext::Get(
51 renderWidgetHostView_->window_number());
54 return context_ ? YES : NO;
57 - (void)updateScaleFactor {
58 if (!renderWidgetHostView_ ||
59 ![self respondsToSelector:(@selector(contentsScale))] ||
60 ![self respondsToSelector:(@selector(setContentsScale:))])
63 float current_scale_factor = [self contentsScale];
64 float new_scale_factor = current_scale_factor;
65 if (renderWidgetHostView_->compositing_iosurface_) {
67 renderWidgetHostView_->compositing_iosurface_->scale_factor();
70 if (new_scale_factor == current_scale_factor)
73 ScopedCAActionDisabler disabler;
74 [self setContentsScale:new_scale_factor];
77 - (void)disableCompositing{
78 ScopedCAActionDisabler disabler;
79 [self removeFromSuperlayer];
80 renderWidgetHostView_ = nil;
83 // The remaining methods implement the CAOpenGLLayer interface.
85 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
86 if ([self ensureContext])
87 return context_->cgl_context();
91 - (void)releaseCGLContext:(CGLContextObj)glContext {
95 DCHECK(glContext == context_->cgl_context());
99 - (void)drawInCGLContext:(CGLContextObj)glContext
100 pixelFormat:(CGLPixelFormatObj)pixelFormat
101 forLayerTime:(CFTimeInterval)timeInterval
102 displayTime:(const CVTimeStamp*)timeStamp {
103 if (!context_ || !renderWidgetHostView_ ||
104 !renderWidgetHostView_->compositing_iosurface_) {
105 glClearColor(1, 1, 1, 1);
106 glClear(GL_COLOR_BUFFER_BIT);
110 DCHECK(glContext == context_->cgl_context());
112 gfx::Size window_size([self frame].size);
113 float window_scale_factor = 1.f;
114 if ([self respondsToSelector:(@selector(contentsScale))])
115 window_scale_factor = [self contentsScale];
117 if (!renderWidgetHostView_->compositing_iosurface_->DrawIOSurface(
120 renderWidgetHostView_->frame_subscriber(),
122 renderWidgetHostView_->GotAcceleratedCompositingError();