| 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 <sstream> |
| 8 |
| 7 #include <CoreFoundation/CoreFoundation.h> | 9 #include <CoreFoundation/CoreFoundation.h> |
| 8 #include <OpenGL/CGLIOSurface.h> | 10 #include <OpenGL/CGLIOSurface.h> |
| 9 #include <OpenGL/CGLRenderers.h> | 11 #include <OpenGL/CGLRenderers.h> |
| 10 #include <OpenGL/OpenGL.h> | 12 #include <OpenGL/OpenGL.h> |
| 11 | 13 |
| 12 #include "base/mac/mac_util.h" | 14 #include "base/mac/mac_util.h" |
| 13 #include "base/mac/sdk_forward_declarations.h" | 15 #include "base/mac/sdk_forward_declarations.h" |
| 16 #include "content/browser/gpu/gpu_data_manager_impl.h" |
| 14 #include "content/browser/renderer_host/render_widget_host_impl.h" | 17 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 15 #include "content/browser/renderer_host/render_widget_host_view_mac.h" | 18 #include "content/browser/renderer_host/render_widget_host_view_mac.h" |
| 16 #include "ui/base/cocoa/animation_utils.h" | 19 #include "ui/base/cocoa/animation_utils.h" |
| 17 #include "ui/gfx/size_conversions.h" | 20 #include "ui/gfx/size_conversions.h" |
| 18 #include "ui/gl/scoped_cgl.h" | 21 #include "ui/gl/scoped_cgl.h" |
| 19 #include "ui/gl/gpu_switching_manager.h" | 22 #include "ui/gl/gpu_switching_manager.h" |
| 20 | 23 |
| 21 // Convenience macro for checking errors in the below code. | 24 // Convenience macro for checking errors in the below code. |
| 22 #define CHECK_GL_ERROR() do { \ | 25 #define CHECK_GL_ERROR() do { \ |
| 23 GLenum gl_error = glGetError(); \ | 26 GLenum gl_error = glGetError(); \ |
| 24 LOG_IF(ERROR, gl_error != GL_NO_ERROR) << "GL Error: " << gl_error; \ | 27 LOG_IF(ERROR, gl_error != GL_NO_ERROR) << "GL Error: " << gl_error; \ |
| 25 } while (0) | 28 } while (0) |
| 26 | 29 |
| 30 // Helper function for logging error codes. |
| 31 namespace { |
| 32 template<typename T> |
| 33 std::string to_string(T value) { |
| 34 std::ostringstream stream; |
| 35 stream << value; |
| 36 return stream.str(); |
| 37 } |
| 38 } |
| 39 |
| 27 //////////////////////////////////////////////////////////////////////////////// | 40 //////////////////////////////////////////////////////////////////////////////// |
| 28 // IOSurfaceLayer(Private) | 41 // IOSurfaceLayer(Private) |
| 29 | 42 |
| 30 @interface IOSurfaceLayer(Private) | 43 @interface IOSurfaceLayer(Private) |
| 31 // Force a draw immediately, but only if one was requested. | 44 // Force a draw immediately, but only if one was requested. |
| 32 - (void)displayIfNeededAndAck; | 45 - (void)displayIfNeededAndAck; |
| 33 | 46 |
| 34 // Called when it has been a fixed interval of time and a frame has yet to be | 47 // Called when it has been a fixed interval of time and a frame has yet to be |
| 35 // drawn. | 48 // drawn. |
| 36 - (void)timerFired; | 49 - (void)timerFired; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 helper_->ResetTimer(); | 165 helper_->ResetTimer(); |
| 153 | 166 |
| 154 frame_pixel_size_ = pixel_size; | 167 frame_pixel_size_ = pixel_size; |
| 155 | 168 |
| 156 // If this is a new IOSurface, open the IOSurface and mark that the | 169 // If this is a new IOSurface, open the IOSurface and mark that the |
| 157 // GL texture needs to bind to the new surface. | 170 // GL texture needs to bind to the new surface. |
| 158 if (!io_surface_ || io_surface_id != IOSurfaceGetID(io_surface_)) { | 171 if (!io_surface_ || io_surface_id != IOSurfaceGetID(io_surface_)) { |
| 159 io_surface_.reset(IOSurfaceLookup(io_surface_id)); | 172 io_surface_.reset(IOSurfaceLookup(io_surface_id)); |
| 160 io_surface_texture_dirty_ = true; | 173 io_surface_texture_dirty_ = true; |
| 161 if (!io_surface_) { | 174 if (!io_surface_) { |
| 162 LOG(ERROR) << "Failed to open IOSurface for frame"; | 175 content::GpuDataManagerImpl::GetInstance()->AddLogMessage( |
| 176 logging::LOG_ERROR, |
| 177 "IOSurfaceLayer", |
| 178 "Failed to open IOSurface in gotFrameWithIOSurface"); |
| 163 if (client_) | 179 if (client_) |
| 164 client_->IOSurfaceLayerHitError(); | 180 client_->IOSurfaceLayerHitError(); |
| 165 } | 181 } |
| 166 } | 182 } |
| 167 | 183 |
| 168 // If reqested, draw immediately and don't bother trying to use the | 184 // If reqested, draw immediately and don't bother trying to use the |
| 169 // isAsynchronous property to ensure smooth animation. If this is while | 185 // isAsynchronous property to ensure smooth animation. If this is while |
| 170 // frames are being pumped then ack and display immediately to get a | 186 // frames are being pumped then ack and display immediately to get a |
| 171 // correct-sized frame displayed as soon as possible. | 187 // correct-sized frame displayed as soon as possible. |
| 172 if (is_pumping_frames_ || | 188 if (is_pumping_frames_ || |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 std::vector<CGLPixelFormatAttribute> attribs; | 283 std::vector<CGLPixelFormatAttribute> attribs; |
| 268 attribs.push_back(kCGLPFADepthSize); | 284 attribs.push_back(kCGLPFADepthSize); |
| 269 attribs.push_back(static_cast<CGLPixelFormatAttribute>(0)); | 285 attribs.push_back(static_cast<CGLPixelFormatAttribute>(0)); |
| 270 if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus()) { | 286 if (ui::GpuSwitchingManager::GetInstance()->SupportsDualGpus()) { |
| 271 attribs.push_back(kCGLPFAAllowOfflineRenderers); | 287 attribs.push_back(kCGLPFAAllowOfflineRenderers); |
| 272 attribs.push_back(static_cast<CGLPixelFormatAttribute>(1)); | 288 attribs.push_back(static_cast<CGLPixelFormatAttribute>(1)); |
| 273 } | 289 } |
| 274 attribs.push_back(static_cast<CGLPixelFormatAttribute>(0)); | 290 attribs.push_back(static_cast<CGLPixelFormatAttribute>(0)); |
| 275 GLint number_virtual_screens = 0; | 291 GLint number_virtual_screens = 0; |
| 276 base::ScopedTypeRef<CGLPixelFormatObj> pixel_format; | 292 base::ScopedTypeRef<CGLPixelFormatObj> pixel_format; |
| 277 CGLError error = CGLChoosePixelFormat( | 293 CGLError cgl_error = CGLChoosePixelFormat( |
| 278 &attribs.front(), pixel_format.InitializeInto(), &number_virtual_screens); | 294 &attribs.front(), pixel_format.InitializeInto(), &number_virtual_screens); |
| 279 if (error != kCGLNoError) { | 295 if (cgl_error != kCGLNoError) { |
| 280 LOG(ERROR) << "Failed to create pixel format object."; | 296 content::GpuDataManagerImpl::GetInstance()->AddLogMessage( |
| 297 logging::LOG_ERROR, |
| 298 "IOSurfaceLayer", |
| 299 std::string("Failed to create pixel format object with CGL error ") + |
| 300 to_string(static_cast<int>(cgl_error))); |
| 281 return NULL; | 301 return NULL; |
| 282 } | 302 } |
| 283 return CGLRetainPixelFormat(pixel_format); | 303 return CGLRetainPixelFormat(pixel_format); |
| 284 } | 304 } |
| 285 | 305 |
| 286 - (void)releaseCGLContext:(CGLContextObj)glContext { | 306 - (void)releaseCGLContext:(CGLContextObj)glContext { |
| 287 // The GL context is being destroyed, so mark the resources as needing to be | 307 // The GL context is being destroyed, so mark the resources as needing to be |
| 288 // recreated. | 308 // recreated. |
| 289 io_surface_texture_ = 0; | 309 io_surface_texture_ = 0; |
| 290 io_surface_texture_dirty_ = true; | 310 io_surface_texture_dirty_ = true; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 GL_TEXTURE_RECTANGLE_ARB, | 344 GL_TEXTURE_RECTANGLE_ARB, |
| 325 GL_RGBA, | 345 GL_RGBA, |
| 326 IOSurfaceGetWidth(io_surface_), | 346 IOSurfaceGetWidth(io_surface_), |
| 327 IOSurfaceGetHeight(io_surface_), | 347 IOSurfaceGetHeight(io_surface_), |
| 328 GL_BGRA, | 348 GL_BGRA, |
| 329 GL_UNSIGNED_INT_8_8_8_8_REV, | 349 GL_UNSIGNED_INT_8_8_8_8_REV, |
| 330 io_surface_.get(), | 350 io_surface_.get(), |
| 331 0 /* plane */); | 351 0 /* plane */); |
| 332 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); | 352 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); |
| 333 if (cgl_error != kCGLNoError) { | 353 if (cgl_error != kCGLNoError) { |
| 334 LOG(ERROR) << "CGLTexImageIOSurface2D failed with " << cgl_error; | 354 content::GpuDataManagerImpl::GetInstance()->AddLogMessage( |
| 355 logging::LOG_ERROR, |
| 356 "IOSurfaceLayer", |
| 357 std::string("CGLTexImageIOSurface2D failed with CGL error ") + |
| 358 to_string(cgl_error)); |
| 335 glDeleteTextures(1, &io_surface_texture_); | 359 glDeleteTextures(1, &io_surface_texture_); |
| 336 io_surface_texture_ = 0; | 360 io_surface_texture_ = 0; |
| 337 if (client_) | 361 if (client_) |
| 338 client_->IOSurfaceLayerHitError(); | 362 client_->IOSurfaceLayerHitError(); |
| 339 } | 363 } |
| 340 } else if (io_surface_texture_) { | 364 } else if (io_surface_texture_) { |
| 341 glDeleteTextures(1, &io_surface_texture_); | 365 glDeleteTextures(1, &io_surface_texture_); |
| 342 io_surface_texture_ = 0; | 366 io_surface_texture_ = 0; |
| 343 } | 367 } |
| 344 | 368 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 glEnd(); | 401 glEnd(); |
| 378 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); | 402 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); |
| 379 glDisable(GL_TEXTURE_RECTANGLE_ARB); | 403 glDisable(GL_TEXTURE_RECTANGLE_ARB); |
| 380 | 404 |
| 381 // Workaround for issue 158469. Issue a dummy draw call with | 405 // Workaround for issue 158469. Issue a dummy draw call with |
| 382 // io_surface_texture_ not bound to a texture, in order to shake all | 406 // io_surface_texture_ not bound to a texture, in order to shake all |
| 383 // references to the IOSurface out of the driver. | 407 // references to the IOSurface out of the driver. |
| 384 glBegin(GL_TRIANGLES); | 408 glBegin(GL_TRIANGLES); |
| 385 glEnd(); | 409 glEnd(); |
| 386 } else { | 410 } else { |
| 411 content::GpuDataManagerImpl::GetInstance()->AddLogMessage( |
| 412 logging::LOG_ERROR, |
| 413 "IOSurfaceLayer", |
| 414 std::string("No texture to draw, clearing to white")); |
| 387 glClearColor(1, 1, 1, 1); | 415 glClearColor(1, 1, 1, 1); |
| 388 glClear(GL_COLOR_BUFFER_BIT); | 416 glClear(GL_COLOR_BUFFER_BIT); |
| 389 } | 417 } |
| 390 | 418 |
| 391 // Query the current GL renderer to send back to the GPU process. | 419 // Query the current GL renderer to send back to the GPU process. |
| 392 { | 420 { |
| 393 CGLError cgl_error = CGLGetParameter( | 421 CGLError cgl_error = CGLGetParameter( |
| 394 glContext, kCGLCPCurrentRendererID, &cgl_renderer_id_); | 422 glContext, kCGLCPCurrentRendererID, &cgl_renderer_id_); |
| 395 if (cgl_error == kCGLNoError) { | 423 if (cgl_error == kCGLNoError) { |
| 396 cgl_renderer_id_ &= kCGLRendererIDMatchingMask; | 424 cgl_renderer_id_ &= kCGLRendererIDMatchingMask; |
| 397 } else { | 425 } else { |
| 398 LOG(ERROR) << "CGLGetParameter for kCGLCPCurrentRendererID failed with " | 426 content::GpuDataManagerImpl::GetInstance()->AddLogMessage( |
| 399 << cgl_error; | 427 logging::LOG_ERROR, |
| 428 "IOSurfaceLayer", |
| 429 std::string("CGLGetParameter for kCGLCPCurrentRendererID failed ") + |
| 430 std::string("with CGL error ") + to_string(cgl_error)); |
| 400 cgl_renderer_id_ = 0; | 431 cgl_renderer_id_ = 0; |
| 401 } | 432 } |
| 402 } | 433 } |
| 403 | 434 |
| 404 // If we hit any errors, tell the client. | 435 // If we hit any errors, tell the client. |
| 405 while (GLenum gl_error = glGetError()) { | 436 while (GLenum gl_error = glGetError()) { |
| 406 LOG(ERROR) << "Hit GL error " << gl_error; | 437 content::GpuDataManagerImpl::GetInstance()->AddLogMessage( |
| 438 logging::LOG_ERROR, |
| 439 "IOSurfaceLayer", |
| 440 std::string("Hit GL error ") + to_string(gl_error) + |
| 441 std::string(" in drawInCGLContext")); |
| 407 if (client_) | 442 if (client_) |
| 408 client_->IOSurfaceLayerHitError(); | 443 client_->IOSurfaceLayerHitError(); |
| 409 } | 444 } |
| 410 | 445 |
| 411 needs_display_ = false; | 446 needs_display_ = false; |
| 412 [super drawInCGLContext:glContext | 447 [super drawInCGLContext:glContext |
| 413 pixelFormat:pixelFormat | 448 pixelFormat:pixelFormat |
| 414 forLayerTime:timeInterval | 449 forLayerTime:timeInterval |
| 415 displayTime:timeStamp]; | 450 displayTime:timeStamp]; |
| 416 | 451 |
| 417 [self ackPendingFrame]; | 452 [self ackPendingFrame]; |
| 418 } | 453 } |
| 419 | 454 |
| 420 @end | 455 @end |
| OLD | NEW |