Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / compositing_iosurface_layer_mac.mm
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.
4
5 #include "content/browser/renderer_host/compositing_iosurface_layer_mac.h"
6
7 #include <CoreFoundation/CoreFoundation.h>
8 #include <OpenGL/gl.h>
9
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"
19
20 @implementation CompositingIOSurfaceLayer
21
22 - (id)initWithRenderWidgetHostViewMac:(content::RenderWidgetHostViewMac*)r {
23   if (self = [super init]) {
24     renderWidgetHostView_ = r;
25     context_ = content::CompositingIOSurfaceContext::Get(
26         content::CompositingIOSurfaceContext::kCALayerContextWindowNumber);
27     DCHECK(context_);
28     needsDisplay_ = NO;
29
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()];
39     }
40   }
41   return self;
42 }
43
44 - (void)disableCompositing{
45   renderWidgetHostView_ = nil;
46 }
47
48 - (void)gotNewFrame {
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];
54
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
58     // not be blocked.
59     if (needsDisplay_)
60       renderWidgetHostView_->SendPendingSwapAck();
61   } else {
62     needsDisplay_ = YES;
63     if (![self isAsynchronous])
64       [self setAsynchronous:YES];
65   }
66 }
67
68 - (void)timerSinceGotNewFrameFired {
69   if (![self isAsynchronous])
70     return;
71
72   [self setAsynchronous:NO];
73
74   // If there was a pending frame, ensure that it goes through.
75   if (needsDisplay_) {
76     [self setNeedsDisplay];
77     [self displayIfNeeded];
78   }
79   // If that fails then ensure that, at a minimum, the renderer is not blocked.
80   if (needsDisplay_)
81     renderWidgetHostView_->SendPendingSwapAck();
82 }
83
84 // The remaining methods implement the CAOpenGLLayer interface.
85
86 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
87   if (!context_)
88     return [super copyCGLPixelFormatForDisplayMask:mask];
89   return CGLRetainPixelFormat(CGLGetPixelFormat(context_->cgl_context()));
90 }
91
92 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
93   if (!context_)
94     return [super copyCGLContextForPixelFormat:pixelFormat];
95   return CGLRetainContext(context_->cgl_context());
96 }
97
98 - (void)setNeedsDisplay {
99   needsDisplay_ = YES;
100   [super setNeedsDisplay];
101 }
102
103 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext
104                 pixelFormat:(CGLPixelFormatObj)pixelFormat
105                forLayerTime:(CFTimeInterval)timeInterval
106                 displayTime:(const CVTimeStamp*)timeStamp {
107   return needsDisplay_;
108 }
109
110 - (void)drawInCGLContext:(CGLContextObj)glContext
111              pixelFormat:(CGLPixelFormatObj)pixelFormat
112             forLayerTime:(CFTimeInterval)timeInterval
113              displayTime:(const CVTimeStamp*)timeStamp {
114   TRACE_EVENT0("browser", "CompositingIOSurfaceLayer::drawInCGLContext");
115
116   if (!context_ ||
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);
123     return;
124   }
125
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.
129   GLint viewport[4];
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));
137
138   if (!renderWidgetHostView_->compositing_iosurface_->DrawIOSurface(
139         context_,
140         window_rect,
141         window_scale_factor,
142         false)) {
143     renderWidgetHostView_->GotAcceleratedCompositingError();
144     return;
145   }
146
147   needsDisplay_ = NO;
148   renderWidgetHostView_->SendPendingLatencyInfoToHost();
149   renderWidgetHostView_->SendPendingSwapAck();
150
151   [super drawInCGLContext:glContext
152               pixelFormat:pixelFormat
153              forLayerTime:timeInterval
154               displayTime:timeStamp];
155 }
156
157 @end