Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / compositing_iosurface_layer_mac.mm
bloba787de08fa69102ac47c32f59adc52b80276436b
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>
8 #include <OpenGL/gl.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];
33   }
34   return self;
37 - (BOOL)ensureContext {
38   if (context_)
39     return YES;
41   if (!renderWidgetHostView_)
42     return NO;
44   if (renderWidgetHostView_->compositing_iosurface_) {
45     context_ = renderWidgetHostView_->compositing_iosurface_->context();
46     [context_->nsgl_context() clearDrawable];
47   }
49   if (!context_) {
50     context_ = content::CompositingIOSurfaceContext::Get(
51         renderWidgetHostView_->window_number());
52   }
54   return context_ ? YES : NO;
57 - (void)updateScaleFactor {
58   if (!renderWidgetHostView_ ||
59       ![self respondsToSelector:(@selector(contentsScale))] ||
60       ![self respondsToSelector:(@selector(setContentsScale:))])
61     return;
63   float current_scale_factor = [self contentsScale];
64   float new_scale_factor = current_scale_factor;
65   if (renderWidgetHostView_->compositing_iosurface_) {
66     new_scale_factor =
67         renderWidgetHostView_->compositing_iosurface_->scale_factor();
68   }
70   if (new_scale_factor == current_scale_factor)
71     return;
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();
88   return nil;
91 - (void)releaseCGLContext:(CGLContextObj)glContext {
92   if (!context_)
93     return;
95   DCHECK(glContext == context_->cgl_context());
96   context_ = nil;
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);
107     return;
108   }
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(
118         window_size,
119         window_scale_factor,
120         renderWidgetHostView_->frame_subscriber(),
121         true)) {
122     renderWidgetHostView_->GotAcceleratedCompositingError();
123   }
126 @end