Chromium Code Reviews| 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/compositor/io_surface_layer_mac.h" | 5 #include "content/browser/compositor/io_surface_layer_mac.h" | 
| 6 | 6 | 
| 7 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> | 
| 8 #include <OpenGL/gl.h> | 8 #include <OpenGL/CGLIOSurface.h> | 
| 9 #include <OpenGL/CGLRenderers.h> | |
| 10 #include <OpenGL/OpenGL.h> | |
| 9 | 11 | 
| 10 #include "base/mac/mac_util.h" | 12 #include "base/mac/mac_util.h" | 
| 11 #include "base/mac/sdk_forward_declarations.h" | 13 #include "base/mac/sdk_forward_declarations.h" | 
| 12 #include "content/browser/renderer_host/render_widget_host_impl.h" | 14 #include "content/browser/renderer_host/render_widget_host_impl.h" | 
| 13 #include "content/browser/renderer_host/render_widget_host_view_mac.h" | 15 #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" | 16 #include "ui/base/cocoa/animation_utils.h" | 
| 17 #include "ui/gfx/size_conversions.h" | 17 #include "ui/gfx/size_conversions.h" | 
| 18 #include "ui/gl/scoped_cgl.h" | |
| 18 #include "ui/gl/gpu_switching_manager.h" | 19 #include "ui/gl/gpu_switching_manager.h" | 
| 19 | 20 | 
| 21 // Convenience macro for checking errors in the below code. | |
| 22 #define CHECK_GL_ERROR() do { \ | |
| 23 GLenum gl_error = glGetError(); \ | |
| 24 LOG_IF(ERROR, gl_error != GL_NO_ERROR) << "GL Error: " << gl_error; \ | |
| 25 } while (0) | |
| 26 | |
| 27 //////////////////////////////////////////////////////////////////////////////// | |
| 28 // IOSurfaceLayer(Private) | |
| 29 | |
| 30 @interface IOSurfaceLayer(Private) | |
| 31 // Force a draw immediately, but only if one was requested. | |
| 32 - (void)displayIfNeededAndAck; | |
| 33 | |
| 34 // Called when it has been a fixed interval of time and a frame has yet to be | |
| 35 // drawn. | |
| 36 - (void)timerFired; | |
| 37 @end | |
| 38 | |
| 20 //////////////////////////////////////////////////////////////////////////////// | 39 //////////////////////////////////////////////////////////////////////////////// | 
| 21 // IOSurfaceLayerHelper | 40 // IOSurfaceLayerHelper | 
| 22 | 41 | 
| 23 namespace content { | 42 namespace content { | 
| 24 | 43 | 
| 44 // IOSurfaceLayerHelper provides C++ functionality needed for the | |
| 45 // IOSurfaceLayer class (interfacing with base::DelayTimer). | |
| 46 class IOSurfaceLayerHelper { | |
| 47 public: | |
| 48 IOSurfaceLayerHelper(IOSurfaceLayer* layer); | |
| 49 ~IOSurfaceLayerHelper(); | |
| 50 void ResetTimer(); | |
| 51 | |
| 52 private: | |
| 53 void TimerFired(); | |
| 54 | |
| 55 // The layer that owns this helper. | |
| 56 IOSurfaceLayer* const layer_; | |
| 57 | |
| 58 // The browser places back-pressure on the GPU by not acknowledging swap | |
| 59 // calls until they appear on the screen. This can lead to hangs if the | |
| 60 // view is moved offscreen (among other things). Prevent hangs by always | |
| 61 // acknowledging the frame after timeout of 1/6th of a second has passed. | |
| 62 base::DelayTimer<IOSurfaceLayerHelper> timer_; | |
| 63 }; | |
| 64 | |
| 65 | |
| 25 IOSurfaceLayerHelper::IOSurfaceLayerHelper( | 66 IOSurfaceLayerHelper::IOSurfaceLayerHelper( | 
| 26 IOSurfaceLayerClient* client, | |
| 27 IOSurfaceLayer* layer) | 67 IOSurfaceLayer* layer) | 
| 28 : client_(client), | 68 : layer_(layer), | 
| 29 layer_(layer), | |
| 30 needs_display_(false), | |
| 31 has_pending_frame_(false), | |
| 32 did_not_draw_counter_(0), | |
| 33 is_pumping_frames_(false), | |
| 34 timer_( | 69 timer_( | 
| 35 FROM_HERE, | 70 FROM_HERE, | 
| 36 base::TimeDelta::FromSeconds(1) / 6, | 71 base::TimeDelta::FromSeconds(1) / 6, | 
| 37 this, | 72 this, | 
| 38 &IOSurfaceLayerHelper::TimerFired) {} | 73 &IOSurfaceLayerHelper::TimerFired) {} | 
| 39 | 74 | 
| 40 IOSurfaceLayerHelper::~IOSurfaceLayerHelper() { | 75 IOSurfaceLayerHelper::~IOSurfaceLayerHelper() { | 
| 76 } | |
| 77 | |
| 78 void IOSurfaceLayerHelper::ResetTimer() { | |
| 79 timer_.Reset(); | |
| 80 } | |
| 81 | |
| 82 void IOSurfaceLayerHelper::TimerFired() { | |
| 83 [layer_ timerFired]; | |
| 84 } | |
| 85 | |
| 86 } // namespace content | |
| 87 | |
| 88 //////////////////////////////////////////////////////////////////////////////// | |
| 89 // IOSurfaceLayer | |
| 90 | |
| 91 @implementation IOSurfaceLayer | |
| 92 | |
| 93 - (id)initWithClient:(content::IOSurfaceLayerClient*)client | |
| 94 withScaleFactor:(float)scale_factor { | |
| 95 if (self = [super init]) { | |
| 96 client_ = client; | |
| 97 helper_.reset(new content::IOSurfaceLayerHelper(self)); | |
| 98 needs_display_ = false; | |
| 99 has_pending_frame_ = false; | |
| 100 did_not_draw_counter_ = 0; | |
| 101 is_pumping_frames_ = false; | |
| 102 io_surface_texture_ = 0; | |
| 103 io_surface_texture_dirty_ = false; | |
| 104 cgl_renderer_id_ = 0; | |
| 105 | |
| 106 [self setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)]; | |
| 107 [self setAnchorPoint:CGPointMake(0, 0)]; | |
| 108 // Setting contents gravity is necessary to prevent the layer from being | |
| 109 // scaled during dyanmic resizes (especially with devtools open). | |
| 110 [self setContentsGravity:kCAGravityTopLeft]; | |
| 111 if ([self respondsToSelector:(@selector(setContentsScale:))]) | |
| 112 [self setContentsScale:scale_factor]; | |
| 113 } | |
| 114 return self; | |
| 115 } | |
| 116 | |
| 117 - (void)dealloc { | |
| 118 DCHECK(!helper_ && !client_); | |
| 119 [super dealloc]; | |
| 120 } | |
| 121 | |
| 122 - (float)scaleFactor { | |
| 123 if ([self respondsToSelector:(@selector(contentsScale))]) | |
| 124 return [self contentsScale]; | |
| 125 return 1; | |
| 126 } | |
| 127 | |
| 128 - (int)rendererID { | |
| 129 return cgl_renderer_id_; | |
| 130 } | |
| 131 | |
| 132 - (void)timerFired { | |
| 133 [self displayIfNeededAndAck]; | |
| 134 } | |
| 135 | |
| 136 - (void)resetClient { | |
| 41 // Any acks that were waiting on this layer to draw will not occur, so ack | 137 // Any acks that were waiting on this layer to draw will not occur, so ack | 
| 42 // them now to prevent blocking the renderer. | 138 // them now to prevent blocking the renderer. | 
| 43 AckPendingFrame(true); | 139 [self ackPendingFrame]; | 
| 140 helper_.reset(); | |
| 141 client_ = NULL; | |
| 44 } | 142 } | 
| 45 | 143 | 
| 46 void IOSurfaceLayerHelper::GotNewFrame() { | 144 - (void)gotFrameWithIOSurface:(IOSurfaceID)io_surface_id | 
| 145 withPixelSize:(gfx::Size)pixel_size | |
| 146 withScaleFactor:(float)scale_factor { | |
| 47 // A trace value of 2 indicates that there is a pending swap ack. See | 147 // A trace value of 2 indicates that there is a pending swap ack. See | 
| 48 // canDrawInCGLContext for other value meanings. | 148 // canDrawInCGLContext for other value meanings. | 
| 49 TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, 2); | 149 TRACE_COUNTER_ID1("browser", "PendingSwapAck", self, 2); | 
| 50 | |
| 51 has_pending_frame_ = true; | 150 has_pending_frame_ = true; | 
| 52 needs_display_ = true; | 151 needs_display_ = true; | 
| 53 timer_.Reset(); | 152 helper_->ResetTimer(); | 
| 153 | |
| 154 frame_pixel_size_ = pixel_size; | |
| 155 | |
| 156 // If this is a new IOSurface, open the IOSurface and mark that the | |
| 157 // GL texture needs to bind to the new surface. | |
| 158 if (!io_surface_ || io_surface_id != IOSurfaceGetID(io_surface_)) { | |
| 159 io_surface_.reset(IOSurfaceLookup(io_surface_id)); | |
| 160 io_surface_texture_dirty_ = true; | |
| 161 if (!io_surface_) { | |
| 162 LOG(ERROR) << "Failed to open IOSurface for frame"; | |
| 163 if (client_) | |
| 164 client_->IOSurfaceLayerHitError(); | |
| 165 } | |
| 166 } | |
| 54 | 167 | 
| 55 // If reqested, draw immediately and don't bother trying to use the | 168 // If reqested, draw immediately and don't bother trying to use the | 
| 56 // isAsynchronous property to ensure smooth animation. If this is while | 169 // isAsynchronous property to ensure smooth animation. If this is while | 
| 57 // frames are being pumped then ack and display immediately to get a | 170 // frames are being pumped then ack and display immediately to get a | 
| 58 // correct-sized frame displayed as soon as possible. | 171 // correct-sized frame displayed as soon as possible. | 
| 59 if (is_pumping_frames_ || client_->IOSurfaceLayerShouldAckImmediately()) { | 172 if (is_pumping_frames_ || | 
| 60 SetNeedsDisplayAndDisplayAndAck(); | 173 (client_ && client_->IOSurfaceLayerShouldAckImmediately())) { | 
| 174 [self setNeedsDisplayAndDisplayAndAck]; | |
| 61 } else { | 175 } else { | 
| 62 if (![layer_ isAsynchronous]) | 176 if (![self isAsynchronous]) | 
| 63 [layer_ setAsynchronous:YES]; | 177 [self setAsynchronous:YES]; | 
| 64 } | 178 } | 
| 65 } | 179 } | 
| 66 | 180 | 
| 67 void IOSurfaceLayerHelper::SetNeedsDisplay() { | 181 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext | 
| 68 needs_display_ = true; | 182 pixelFormat:(CGLPixelFormatObj)pixelFormat | 
| 69 } | 183 forLayerTime:(CFTimeInterval)timeInterval | 
| 70 | 184 displayTime:(const CVTimeStamp*)timeStamp { | 
| 71 bool IOSurfaceLayerHelper::CanDraw() { | |
| 72 // If we return NO 30 times in a row, switch to being synchronous to avoid | 185 // If we return NO 30 times in a row, switch to being synchronous to avoid | 
| 73 // burning CPU cycles on this callback. | 186 // burning CPU cycles on this callback. | 
| 74 if (needs_display_) { | 187 if (needs_display_) { | 
| 75 did_not_draw_counter_ = 0; | 188 did_not_draw_counter_ = 0; | 
| 76 } else { | 189 } else { | 
| 77 did_not_draw_counter_ += 1; | 190 did_not_draw_counter_ += 1; | 
| 78 if (did_not_draw_counter_ == 30) | 191 if (did_not_draw_counter_ == 30) | 
| 79 [layer_ setAsynchronous:NO]; | 192 [self setAsynchronous:NO]; | 
| 80 } | 193 } | 
| 81 | 194 | 
| 82 // Add an instantaneous blip to the PendingSwapAck state to indicate | 195 // Add an instantaneous blip to the PendingSwapAck state to indicate | 
| 83 // that CoreAnimation asked if a frame is ready. A blip up to to 3 (usually | 196 // that CoreAnimation asked if a frame is ready. A blip up to to 3 (usually | 
| 84 // from 2, indicating that a swap ack is pending) indicates that we | 197 // from 2, indicating that a swap ack is pending) indicates that we | 
| 85 // requested a draw. A blip up to 1 (usually from 0, indicating there is no | 198 // requested a draw. A blip up to 1 (usually from 0, indicating there is no | 
| 86 // pending swap ack) indicates that we did not request a draw. This would | 199 // pending swap ack) indicates that we did not request a draw. This would | 
| 87 // be more natural to do with a tracing pseudo-thread | 200 // be more natural to do with a tracing pseudo-thread | 
| 88 // http://crbug.com/366300 | 201 // http://crbug.com/366300 | 
| 89 TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, needs_display_ ? 3 : 1); | 202 TRACE_COUNTER_ID1("browser", "PendingSwapAck", self, needs_display_ ? 3 : 1); | 
| 90 TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, | 203 TRACE_COUNTER_ID1("browser", "PendingSwapAck", self, | 
| 91 has_pending_frame_ ? 2 : 0); | 204 has_pending_frame_ ? 2 : 0); | 
| 92 | 205 | 
| 93 return needs_display_; | 206 return needs_display_; | 
| 94 } | 207 } | 
| 95 | 208 | 
| 96 void IOSurfaceLayerHelper::DidDraw(bool success) { | 209 - (void)ackPendingFrame { | 
| 97 needs_display_ = false; | |
| 98 AckPendingFrame(success); | |
| 99 } | |
| 100 | |
| 101 void IOSurfaceLayerHelper::AckPendingFrame(bool success) { | |
| 102 if (!has_pending_frame_) | 210 if (!has_pending_frame_) | 
| 103 return; | 211 return; | 
| 104 has_pending_frame_ = false; | 212 has_pending_frame_ = false; | 
| 105 if (success) | 213 if (client_) | 
| 106 client_->IOSurfaceLayerDidDrawFrame(); | 214 client_->IOSurfaceLayerDidDrawFrame(); | 
| 107 else | |
| 108 client_->IOSurfaceLayerHitError(); | |
| 109 // A trace value of 0 indicates that there is no longer a pending swap ack. | 215 // A trace value of 0 indicates that there is no longer a pending swap ack. | 
| 110 TRACE_COUNTER_ID1("browser", "PendingSwapAck", this, 0); | 216 TRACE_COUNTER_ID1("browser", "PendingSwapAck", self, 0); | 
| 111 } | 217 } | 
| 112 | 218 | 
| 113 void IOSurfaceLayerHelper::SetNeedsDisplayAndDisplayAndAck() { | 219 - (void)setNeedsDisplayAndDisplayAndAck { | 
| 114 // Drawing using setNeedsDisplay and displayIfNeeded will result in | 220 // Drawing using setNeedsDisplay and displayIfNeeded will result in | 
| 115 // subsequent canDrawInCGLContext callbacks getting dropped, and jerky | 221 // subsequent canDrawInCGLContext callbacks getting dropped, and jerky | 
| 116 // animation. Disable asynchronous drawing before issuing these calls as a | 222 // animation. Disable asynchronous drawing before issuing these calls as a | 
| 117 // workaround. | 223 // workaround. | 
| 118 // http://crbug.com/395827 | 224 // http://crbug.com/395827 | 
| 119 if ([layer_ isAsynchronous]) | 225 if ([self isAsynchronous]) | 
| 120 [layer_ setAsynchronous:NO]; | 226 [self setAsynchronous:NO]; | 
| 121 | 227 | 
| 122 [layer_ setNeedsDisplay]; | 228 [self setNeedsDisplay]; | 
| 123 DisplayIfNeededAndAck(); | 229 [self displayIfNeededAndAck]; | 
| 124 } | 230 } | 
| 125 | 231 | 
| 126 void IOSurfaceLayerHelper::DisplayIfNeededAndAck() { | 232 - (void)displayIfNeededAndAck { | 
| 127 if (!needs_display_) | 233 if (!needs_display_) | 
| 128 return; | 234 return; | 
| 129 | 235 | 
| 130 // As in SetNeedsDisplayAndDisplayAndAck, disable asynchronous drawing before | 236 // As in SetNeedsDisplayAndDisplayAndAck, disable asynchronous drawing before | 
| 131 // issuing displayIfNeeded. | 237 // issuing displayIfNeeded. | 
| 132 // http://crbug.com/395827 | 238 // http://crbug.com/395827 | 
| 133 if ([layer_ isAsynchronous]) | 239 if ([self isAsynchronous]) | 
| 134 [layer_ setAsynchronous:NO]; | 240 [self setAsynchronous:NO]; | 
| 135 | 241 | 
| 136 // Do not bother drawing while pumping new frames -- wait until the waiting | 242 // Do not bother drawing while pumping new frames -- wait until the waiting | 
| 137 // block ends to draw any of the new frames. | 243 // block ends to draw any of the new frames. | 
| 138 if (!is_pumping_frames_) | 244 if (!is_pumping_frames_) | 
| 139 [layer_ displayIfNeeded]; | 245 [self displayIfNeeded]; | 
| 140 | 246 | 
| 141 // Calls to setNeedsDisplay can sometimes be ignored, especially if issued | 247 // Calls to setNeedsDisplay can sometimes be ignored, especially if issued | 
| 142 // rapidly (e.g, with vsync off). This is unacceptable because the failure | 248 // rapidly (e.g, with vsync off). This is unacceptable because the failure | 
| 143 // to ack a single frame will hang the renderer. Ensure that the renderer | 249 // to ack a single frame will hang the renderer. Ensure that the renderer | 
| 144 // not be blocked by lying and claiming that we drew the frame. | 250 // not be blocked by lying and claiming that we drew the frame. | 
| 145 AckPendingFrame(true); | 251 [self ackPendingFrame]; | 
| 146 } | 252 } | 
| 147 | 253 | 
| 148 void IOSurfaceLayerHelper::TimerFired() { | 254 - (void)beginPumpingFrames { | 
| 149 SetNeedsDisplayAndDisplayAndAck(); | |
| 
 
ccameron
2014/09/21 09:16:59
*** this is wrong -- it needs to be DisplayIfNeede
 
 | |
| 150 } | |
| 151 | |
| 152 void IOSurfaceLayerHelper::BeginPumpingFrames() { | |
| 153 is_pumping_frames_ = true; | 255 is_pumping_frames_ = true; | 
| 154 } | 256 } | 
| 155 | 257 | 
| 156 void IOSurfaceLayerHelper::EndPumpingFrames() { | 258 - (void)endPumpingFrames { | 
| 157 is_pumping_frames_ = false; | 259 is_pumping_frames_ = false; | 
| 158 DisplayIfNeededAndAck(); | 260 [self displayIfNeededAndAck]; | 
| 159 } | |
| 160 | |
| 161 } // namespace content | |
| 162 | |
| 163 //////////////////////////////////////////////////////////////////////////////// | |
| 164 // IOSurfaceLayer | |
| 165 | |
| 166 @implementation IOSurfaceLayer | |
| 167 | |
| 168 - (content::CompositingIOSurfaceMac*)iosurface { | |
| 169 return iosurface_.get(); | |
| 170 } | |
| 171 | |
| 172 - (content::CompositingIOSurfaceContext*)context { | |
| 173 return context_.get(); | |
| 174 } | |
| 175 | |
| 176 - (id)initWithClient:(content::IOSurfaceLayerClient*)client | |
| 177 withScaleFactor:(float)scale_factor { | |
| 178 if (self = [super init]) { | |
| 179 helper_.reset(new content::IOSurfaceLayerHelper(client, self)); | |
| 180 | |
| 181 iosurface_ = content::CompositingIOSurfaceMac::Create(); | |
| 182 context_ = content::CompositingIOSurfaceContext::Get( | |
| 183 content::CompositingIOSurfaceContext::kCALayerContextWindowNumber); | |
| 184 if (!iosurface_ || !context_) { | |
| 185 LOG(ERROR) << "Failed create CompositingIOSurface or context"; | |
| 186 [self resetClient]; | |
| 187 [self release]; | |
| 188 return nil; | |
| 189 } | |
| 190 | |
| 191 [self setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)]; | |
| 192 [self setAnchorPoint:CGPointMake(0, 0)]; | |
| 193 // Setting contents gravity is necessary to prevent the layer from being | |
| 194 // scaled during dyanmic resizes (especially with devtools open). | |
| 195 [self setContentsGravity:kCAGravityTopLeft]; | |
| 196 if ([self respondsToSelector:(@selector(setContentsScale:))]) { | |
| 197 [self setContentsScale:scale_factor]; | |
| 198 } | |
| 199 } | |
| 200 return self; | |
| 201 } | |
| 202 | |
| 203 - (void)dealloc { | |
| 204 DCHECK(!helper_); | |
| 205 [super dealloc]; | |
| 206 } | |
| 207 | |
| 208 - (bool)gotFrameWithIOSurface:(IOSurfaceID)io_surface_id | |
| 209 withPixelSize:(gfx::Size)pixel_size | |
| 210 withScaleFactor:(float)scale_factor { | |
| 211 bool result = true; | |
| 212 gfx::ScopedCGLSetCurrentContext scoped_set_current_context( | |
| 213 context_->cgl_context()); | |
| 214 result = iosurface_->SetIOSurfaceWithContextCurrent( | |
| 215 context_, io_surface_id, pixel_size, scale_factor); | |
| 216 return result; | |
| 217 } | |
| 218 | |
| 219 - (void)poisonContextAndSharegroup { | |
| 220 context_->PoisonContextAndSharegroup(); | |
| 221 } | |
| 222 | |
| 223 - (bool)hasBeenPoisoned { | |
| 224 return context_->HasBeenPoisoned(); | |
| 225 } | |
| 226 | |
| 227 - (float)scaleFactor { | |
| 228 return iosurface_->scale_factor(); | |
| 229 } | |
| 230 | |
| 231 - (int)rendererID { | |
| 232 return iosurface_->GetRendererID(); | |
| 233 } | |
| 234 | |
| 235 - (void)resetClient { | |
| 236 helper_.reset(); | |
| 237 } | |
| 238 | |
| 239 - (void)gotNewFrame { | |
| 240 helper_->GotNewFrame(); | |
| 241 } | |
| 242 | |
| 243 - (void)setNeedsDisplayAndDisplayAndAck { | |
| 244 helper_->SetNeedsDisplayAndDisplayAndAck(); | |
| 245 } | |
| 246 | |
| 247 - (void)displayIfNeededAndAck { | |
| 248 helper_->DisplayIfNeededAndAck(); | |
| 249 } | |
| 250 | |
| 251 - (void)beginPumpingFrames { | |
| 252 helper_->BeginPumpingFrames(); | |
| 253 } | |
| 254 | |
| 255 - (void)endPumpingFrames { | |
| 256 helper_->EndPumpingFrames(); | |
| 257 } | 261 } | 
| 258 | 262 | 
| 259 // The remaining methods implement the CAOpenGLLayer interface. | 263 // The remaining methods implement the CAOpenGLLayer interface. | 
| 260 | 264 | 
| 261 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask { | 265 - (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask { | 
| 262 if (!context_) | 266 // Create the pixel format object for the context. | 
| 263 return [super copyCGLPixelFormatForDisplayMask:mask]; | 267 std::vector<CGLPixelFormatAttribute> attribs; | 
| 264 return CGLRetainPixelFormat(CGLGetPixelFormat(context_->cgl_context())); | 268 attribs.push_back(kCGLPFADepthSize); | 
| 265 } | 269 attribs.push_back(static_cast<CGLPixelFormatAttribute>(0)); | 
| 266 | 270 if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus()) { | 
| 267 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat { | 271 attribs.push_back(kCGLPFAAllowOfflineRenderers); | 
| 268 if (!context_) | 272 attribs.push_back(static_cast<CGLPixelFormatAttribute>(1)); | 
| 269 return [super copyCGLContextForPixelFormat:pixelFormat]; | 273 } | 
| 270 return CGLRetainContext(context_->cgl_context()); | 274 attribs.push_back(static_cast<CGLPixelFormatAttribute>(0)); | 
| 275 GLint number_virtual_screens = 0; | |
| 276 base::ScopedTypeRef<CGLPixelFormatObj> pixel_format; | |
| 277 CGLError error = CGLChoosePixelFormat( | |
| 278 &attribs.front(), pixel_format.InitializeInto(), &number_virtual_screens); | |
| 279 if (error != kCGLNoError) { | |
| 280 LOG(ERROR) << "Failed to create pixel format object."; | |
| 281 return NULL; | |
| 282 } | |
| 283 return CGLRetainPixelFormat(pixel_format); | |
| 284 } | |
| 285 | |
| 286 - (void)releaseCGLContext:(CGLContextObj)glContext { | |
| 287 // The GL context is being destroyed, so mark the resources as needing to be | |
| 288 // recreated. | |
| 289 io_surface_texture_ = 0; | |
| 290 io_surface_texture_dirty_ = true; | |
| 291 cgl_renderer_id_ = 0; | |
| 292 [super releaseCGLContext:glContext]; | |
| 271 } | 293 } | 
| 272 | 294 | 
| 273 - (void)setNeedsDisplay { | 295 - (void)setNeedsDisplay { | 
| 274 if (helper_) | 296 needs_display_ = true; | 
| 275 helper_->SetNeedsDisplay(); | |
| 276 [super setNeedsDisplay]; | 297 [super setNeedsDisplay]; | 
| 277 } | 298 } | 
| 278 | 299 | 
| 279 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext | |
| 280 pixelFormat:(CGLPixelFormatObj)pixelFormat | |
| 281 forLayerTime:(CFTimeInterval)timeInterval | |
| 282 displayTime:(const CVTimeStamp*)timeStamp { | |
| 283 if (helper_) | |
| 284 return helper_->CanDraw(); | |
| 285 return NO; | |
| 286 } | |
| 287 | |
| 288 - (void)drawInCGLContext:(CGLContextObj)glContext | 300 - (void)drawInCGLContext:(CGLContextObj)glContext | 
| 289 pixelFormat:(CGLPixelFormatObj)pixelFormat | 301 pixelFormat:(CGLPixelFormatObj)pixelFormat | 
| 290 forLayerTime:(CFTimeInterval)timeInterval | 302 forLayerTime:(CFTimeInterval)timeInterval | 
| 291 displayTime:(const CVTimeStamp*)timeStamp { | 303 displayTime:(const CVTimeStamp*)timeStamp { | 
| 292 TRACE_EVENT0("browser", "IOSurfaceLayer::drawInCGLContext"); | 304 TRACE_EVENT0("browser", "IOSurfaceLayer::drawInCGLContext"); | 
| 293 | 305 | 
| 294 if (!iosurface_->HasIOSurface() || context_->cgl_context() != glContext) { | 306 // Create the texture if it has not been created in this context yet. | 
| 307 if (!io_surface_texture_) { | |
| 308 glGenTextures(1, &io_surface_texture_); | |
| 309 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, io_surface_texture_); | |
| 310 glTexParameteri( | |
| 311 GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 312 glTexParameteri( | |
| 313 GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 314 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); | |
| 315 io_surface_texture_dirty_ = true; | |
| 316 } | |
| 317 | |
| 318 // Associate the IOSurface with this texture, if the underlying IOSurface has | |
| 319 // been changed. | |
| 320 if (io_surface_texture_dirty_ && io_surface_) { | |
| 321 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, io_surface_texture_); | |
| 322 CGLError cgl_error = CGLTexImageIOSurface2D( | |
| 323 glContext, | |
| 324 GL_TEXTURE_RECTANGLE_ARB, | |
| 325 GL_RGBA, | |
| 326 IOSurfaceGetWidth(io_surface_), | |
| 327 IOSurfaceGetHeight(io_surface_), | |
| 328 GL_BGRA, | |
| 329 GL_UNSIGNED_INT_8_8_8_8_REV, | |
| 330 io_surface_.get(), | |
| 331 0 /* plane */); | |
| 332 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); | |
| 333 if (cgl_error != kCGLNoError) { | |
| 334 LOG(ERROR) << "CGLTexImageIOSurface2D failed with " << cgl_error; | |
| 335 glDeleteTextures(1, &io_surface_texture_); | |
| 336 io_surface_texture_ = 0; | |
| 337 if (client_) | |
| 338 client_->IOSurfaceLayerHitError(); | |
| 339 } | |
| 340 } else if (io_surface_texture_) { | |
| 341 glDeleteTextures(1, &io_surface_texture_); | |
| 342 io_surface_texture_ = 0; | |
| 343 } | |
| 344 | |
| 345 // Fill the viewport with the texture. The viewport must be smaller or equal | |
| 346 // than the texture, because it is resized as frames arrive. | |
| 347 if (io_surface_texture_) { | |
| 348 GLint viewport[4]; | |
| 349 glGetIntegerv(GL_VIEWPORT, viewport); | |
| 350 gfx::Size viewport_pixel_size(viewport[2], viewport[3]); | |
| 351 DCHECK_LE( | |
| 352 viewport_pixel_size.width(), | |
| 353 frame_pixel_size_.width()); | |
| 354 DCHECK_LE( | |
| 355 viewport_pixel_size.height(), | |
| 356 frame_pixel_size_.height()); | |
| 357 | |
| 358 glMatrixMode(GL_PROJECTION); | |
| 359 glLoadIdentity(); | |
| 360 glOrtho(0, viewport_pixel_size.width(), | |
| 361 0, viewport_pixel_size.height(), -1, 1); | |
| 362 glMatrixMode(GL_MODELVIEW); | |
| 363 glLoadIdentity(); | |
| 364 | |
| 365 glColor4f(1, 1, 1, 1); | |
| 366 glEnable(GL_TEXTURE_RECTANGLE_ARB); | |
| 367 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, io_surface_texture_); | |
| 368 glBegin(GL_QUADS); | |
| 369 glTexCoord2f(0, 0); | |
| 370 glVertex2f(0, 0); | |
| 371 glTexCoord2f(viewport[2], 0); | |
| 372 glVertex2f(frame_pixel_size_.width(), 0); | |
| 373 glTexCoord2f(viewport[2], viewport[3]); | |
| 374 glVertex2f(frame_pixel_size_.width(), frame_pixel_size_.height()); | |
| 375 glTexCoord2f(0, viewport[3]); | |
| 376 glVertex2f(0, frame_pixel_size_.height()); | |
| 377 glEnd(); | |
| 378 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); | |
| 379 glDisable(GL_TEXTURE_RECTANGLE_ARB); | |
| 380 | |
| 381 // Workaround for issue 158469. Issue a dummy draw call with | |
| 382 // io_surface_texture_ not bound to a texture, in order to shake all | |
| 383 // references to the IOSurface out of the driver. | |
| 384 glBegin(GL_TRIANGLES); | |
| 385 glEnd(); | |
| 386 } else { | |
| 295 glClearColor(1, 1, 1, 1); | 387 glClearColor(1, 1, 1, 1); | 
| 296 glClear(GL_COLOR_BUFFER_BIT); | 388 glClear(GL_COLOR_BUFFER_BIT); | 
| 297 return; | 389 } | 
| 298 } | 390 | 
| 299 | 391 // Query the current GL renderer to send back to the GPU process. | 
| 300 // The correct viewport to cover the layer will be set up by the caller. | 392 { | 
| 301 // Transform this into a window size for DrawIOSurface, where it will be | 393 CGLError cgl_error = CGLGetParameter( | 
| 302 // transformed back into this viewport. | 394 glContext, kCGLCPCurrentRendererID, &cgl_renderer_id_); | 
| 303 GLint viewport[4]; | 395 if (cgl_error == kCGLNoError) { | 
| 304 glGetIntegerv(GL_VIEWPORT, viewport); | 396 cgl_renderer_id_ &= kCGLRendererIDMatchingMask; | 
| 305 gfx::Rect window_rect(viewport[0], viewport[1], viewport[2], viewport[3]); | 397 } else { | 
| 306 float window_scale_factor = 1.f; | 398 LOG(ERROR) << "CGLGetParameter for kCGLCPCurrentRendererID failed with " | 
| 307 if ([self respondsToSelector:(@selector(contentsScale))]) | 399 << cgl_error; | 
| 308 window_scale_factor = [self contentsScale]; | 400 cgl_renderer_id_ = 0; | 
| 309 window_rect = ToNearestRect( | 401 } | 
| 310 gfx::ScaleRect(window_rect, 1.f/window_scale_factor)); | 402 } | 
| 311 | 403 | 
| 312 bool draw_succeeded = iosurface_->DrawIOSurface( | 404 // If we hit any errors, tell the client. | 
| 313 context_, window_rect, window_scale_factor); | 405 while (GLenum gl_error = glGetError()) { | 
| 314 | 406 LOG(ERROR) << "Hit GL error " << gl_error; | 
| 315 if (helper_) | 407 if (client_) | 
| 316 helper_->DidDraw(draw_succeeded); | 408 client_->IOSurfaceLayerHitError(); | 
| 317 | 409 } | 
| 410 | |
| 411 needs_display_ = false; | |
| 318 [super drawInCGLContext:glContext | 412 [super drawInCGLContext:glContext | 
| 319 pixelFormat:pixelFormat | 413 pixelFormat:pixelFormat | 
| 320 forLayerTime:timeInterval | 414 forLayerTime:timeInterval | 
| 321 displayTime:timeStamp]; | 415 displayTime:timeStamp]; | 
| 416 | |
| 417 [self ackPendingFrame]; | |
| 322 } | 418 } | 
| 323 | 419 | 
| 324 @end | 420 @end | 
| OLD | NEW |