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