| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/compositing_iosurface_layer_mac.h" | 5 #include "content/browser/renderer_host/compositing_iosurface_layer_mac.h" |
| 6 | 6 |
| 7 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
| 8 #include <OpenGL/gl.h> | 8 #include <OpenGL/gl.h> |
| 9 | 9 |
| 10 #include "base/mac/sdk_forward_declarations.h" | 10 #include "base/mac/sdk_forward_declarations.h" |
| 11 #include "content/browser/renderer_host/render_widget_host_impl.h" | 11 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 12 #include "content/browser/renderer_host/render_widget_host_view_mac.h" | 12 #include "content/browser/renderer_host/render_widget_host_view_mac.h" |
| 13 #include "content/browser/renderer_host/compositing_iosurface_context_mac.h" | 13 #include "content/browser/renderer_host/compositing_iosurface_context_mac.h" |
| 14 #include "content/browser/renderer_host/compositing_iosurface_mac.h" | 14 #include "content/browser/renderer_host/compositing_iosurface_mac.h" |
| 15 #include "ui/base/cocoa/animation_utils.h" | 15 #include "ui/base/cocoa/animation_utils.h" |
| 16 #include "ui/gfx/size_conversions.h" | 16 #include "ui/gfx/size_conversions.h" |
| 17 #include "ui/gl/gpu_switching_manager.h" | 17 #include "ui/gl/gpu_switching_manager.h" |
| 18 | 18 |
| 19 @interface CompositingIOSurfaceLayer() | |
| 20 | |
| 21 // Private method to wait for a frame of the right size if we're in an active | |
| 22 // resize. This may potentially dispatch select messages from the run loop. | |
| 23 - (void)waitForResizedFrameInContext:(CGLContextObj)glContext; | |
| 24 | |
| 25 @end | |
| 26 | |
| 27 @implementation CompositingIOSurfaceLayer | 19 @implementation CompositingIOSurfaceLayer |
| 28 | 20 |
| 29 @synthesize context = context_; | 21 @synthesize context = context_; |
| 30 | 22 |
| 31 - (id)initWithRenderWidgetHostViewMac:(content::RenderWidgetHostViewMac*)r { | 23 - (id)initWithRenderWidgetHostViewMac:(content::RenderWidgetHostViewMac*)r { |
| 32 if (self = [super init]) { | 24 if (self = [super init]) { |
| 33 renderWidgetHostView_ = r; | 25 renderWidgetHostView_ = r; |
| 34 context_ = content::CompositingIOSurfaceContext::Get( | 26 context_ = content::CompositingIOSurfaceContext::Get( |
| 35 content::CompositingIOSurfaceContext::kOffscreenContextWindowNumber); | 27 content::CompositingIOSurfaceContext::kOffscreenContextWindowNumber); |
| 36 DCHECK(context_); | 28 DCHECK(context_); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 67 | 59 |
| 68 - (void)timerSinceGotNewFrameFired { | 60 - (void)timerSinceGotNewFrameFired { |
| 69 if (![self isAsynchronous]) | 61 if (![self isAsynchronous]) |
| 70 return; | 62 return; |
| 71 | 63 |
| 72 [self setAsynchronous:NO]; | 64 [self setAsynchronous:NO]; |
| 73 if (needsDisplay_) | 65 if (needsDisplay_) |
| 74 [self setNeedsDisplay]; | 66 [self setNeedsDisplay]; |
| 75 } | 67 } |
| 76 | 68 |
| 77 - (void)waitForResizedFrameInContext:(CGLContextObj)glContext { | |
| 78 // Cache a copy of renderWidgetHostView_ because it may be reset if | |
| 79 // a software frame is received in GetBackingStore. | |
| 80 content::RenderWidgetHostViewMac* cached_view = renderWidgetHostView_; | |
| 81 if (!cached_view->render_widget_host_ || | |
| 82 cached_view->render_widget_host_->is_hidden()) { | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 // Note that GetBackingStore can potentially spawn a nested run loop, which | |
| 87 // may change the current GL context, or, because the GL contexts are | |
| 88 // shared, may change the currently-bound FBO. Ensure that, when the run | |
| 89 // loop returns, the original GL context remain current, and the original | |
| 90 // FBO remain bound. | |
| 91 // TODO(ccameron): This is far too fragile a mechanism to rely on. Find | |
| 92 // a way to avoid doing this. | |
| 93 GLuint previous_framebuffer = 0; | |
| 94 glGetIntegerv(GL_FRAMEBUFFER_BINDING, | |
| 95 reinterpret_cast<GLint*>(&previous_framebuffer)); | |
| 96 { | |
| 97 // If a resize is in progress then GetBackingStore request a frame of the | |
| 98 // current window size and block until a frame of the right size comes in. | |
| 99 // This makes the window content not lag behind the resize (at the cost of | |
| 100 // blocking on the browser's main thread). | |
| 101 gfx::ScopedCGLSetCurrentContext scoped_set_current_context(NULL); | |
| 102 cached_view->about_to_validate_and_paint_ = true; | |
| 103 (void)cached_view->render_widget_host_->GetBackingStore(true); | |
| 104 cached_view->about_to_validate_and_paint_ = false; | |
| 105 } | |
| 106 CHECK_EQ(CGLGetCurrentContext(), glContext) | |
| 107 << "original GL context failed to re-bind after nested run loop, " | |
| 108 << "browser crash is imminent."; | |
| 109 glBindFramebuffer(GL_FRAMEBUFFER, previous_framebuffer); | |
| 110 } | |
| 111 | |
| 112 // The remaining methods implement the CAOpenGLLayer interface. | 69 // The remaining methods implement the CAOpenGLLayer interface. |
| 113 | 70 |
| 114 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask { | 71 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask { |
| 115 if (!context_) | 72 if (!context_) |
| 116 return [super copyCGLPixelFormatForDisplayMask:mask]; | 73 return [super copyCGLPixelFormatForDisplayMask:mask]; |
| 117 return CGLRetainPixelFormat(CGLGetPixelFormat(context_->cgl_context())); | 74 return CGLRetainPixelFormat(CGLGetPixelFormat(context_->cgl_context())); |
| 118 } | 75 } |
| 119 | 76 |
| 120 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat { | 77 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat { |
| 121 if (!context_) | 78 if (!context_) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 143 | 100 |
| 144 if (!context_ || | 101 if (!context_ || |
| 145 (context_ && context_->cgl_context() != glContext) || | 102 (context_ && context_->cgl_context() != glContext) || |
| 146 !renderWidgetHostView_ || | 103 !renderWidgetHostView_ || |
| 147 !renderWidgetHostView_->compositing_iosurface_) { | 104 !renderWidgetHostView_->compositing_iosurface_) { |
| 148 glClearColor(1, 1, 1, 1); | 105 glClearColor(1, 1, 1, 1); |
| 149 glClear(GL_COLOR_BUFFER_BIT); | 106 glClear(GL_COLOR_BUFFER_BIT); |
| 150 return; | 107 return; |
| 151 } | 108 } |
| 152 | 109 |
| 153 // Acknowledge the frame before we potentially wait for a frame of the right | 110 // Cache a copy of renderWidgetHostView_ because it may be reset if |
| 154 // size. | 111 // a software frame is received in GetBackingStore. |
| 155 renderWidgetHostView_->SendPendingSwapAck(); | 112 content::RenderWidgetHostViewMac* cached_view = renderWidgetHostView_; |
| 156 | 113 |
| 157 // Wait for a frame of the right size to come in, if needed. | 114 // If a resize is in progress then GetBackingStore request a frame of the |
| 158 [self waitForResizedFrameInContext:glContext]; | 115 // current window size and block until a frame of the right size comes in. |
| 116 // This makes the window content not lag behind the resize (at the cost of |
| 117 // blocking on the browser's main thread). |
| 118 if (cached_view->render_widget_host_) { |
| 119 // Note that GetBackingStore can potentially spawn a nested run loop, which |
| 120 // may change the current GL context, or, because the GL contexts are |
| 121 // shared, may change the currently-bound FBO. Ensure that, when the run |
| 122 // loop returns, the original GL context remain current, and the original |
| 123 // FBO remain bound. |
| 124 // TODO(ccameron): This is far too fragile a mechanism to rely on. Find |
| 125 // a way to avoid doing this. |
| 126 GLuint previous_framebuffer = 0; |
| 127 glGetIntegerv(GL_FRAMEBUFFER_BINDING, |
| 128 reinterpret_cast<GLint*>(&previous_framebuffer)); |
| 129 { |
| 130 gfx::ScopedCGLSetCurrentContext scoped_set_current_context(NULL); |
| 131 cached_view->about_to_validate_and_paint_ = true; |
| 132 (void)cached_view->render_widget_host_->GetBackingStore(true); |
| 133 cached_view->about_to_validate_and_paint_ = false; |
| 134 } |
| 135 CHECK_EQ(CGLGetCurrentContext(), glContext) |
| 136 << "original GL context failed to re-bind after nested run loop, " |
| 137 << "browser crash is imminent."; |
| 138 glBindFramebuffer(GL_FRAMEBUFFER, previous_framebuffer); |
| 139 } |
| 159 | 140 |
| 160 // If a transition to software mode has occurred, this layer should be | 141 // If a transition to software mode has occurred, this layer should be |
| 161 // removed from the heirarchy now, so don't draw anything. | 142 // removed from the heirarchy now, so don't draw anything. |
| 162 if (!renderWidgetHostView_) | 143 if (!renderWidgetHostView_) |
| 163 return; | 144 return; |
| 164 | 145 |
| 165 gfx::Rect window_rect([self frame]); | 146 gfx::Rect window_rect([self frame]); |
| 166 float window_scale_factor = 1.f; | 147 float window_scale_factor = 1.f; |
| 167 if ([self respondsToSelector:(@selector(contentsScale))]) | 148 if ([self respondsToSelector:(@selector(contentsScale))]) |
| 168 window_scale_factor = [self contentsScale]; | 149 window_scale_factor = [self contentsScale]; |
| 169 | 150 |
| 170 if (!renderWidgetHostView_->compositing_iosurface_->DrawIOSurface( | 151 if (!renderWidgetHostView_->compositing_iosurface_->DrawIOSurface( |
| 171 context_, | 152 context_, |
| 172 window_rect, | 153 window_rect, |
| 173 window_scale_factor, | 154 window_scale_factor, |
| 174 false)) { | 155 false)) { |
| 175 renderWidgetHostView_->GotAcceleratedCompositingError(); | 156 renderWidgetHostView_->GotAcceleratedCompositingError(); |
| 176 return; | 157 return; |
| 177 } | 158 } |
| 178 | 159 |
| 179 needsDisplay_ = NO; | 160 needsDisplay_ = NO; |
| 180 renderWidgetHostView_->SendPendingLatencyInfoToHost(); | 161 renderWidgetHostView_->SendPendingLatencyInfoToHost(); |
| 181 } | 162 } |
| 182 | 163 |
| 183 @end | 164 @end |
| OLD | NEW |