| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #include "base/ref_counted.h" | 7 #include "base/ref_counted.h" |
| 8 #include "base/singleton.h" | 8 #include "base/singleton.h" |
| 9 #include "base/thread_local.h" | |
| 10 #include "base/weak_ptr.h" | 9 #include "base/weak_ptr.h" |
| 11 #include "chrome/renderer/command_buffer_proxy.h" | 10 #include "chrome/renderer/command_buffer_proxy.h" |
| 12 #include "chrome/renderer/ggl/ggl.h" | 11 #include "chrome/renderer/ggl/ggl.h" |
| 13 #include "chrome/renderer/gpu_channel_host.h" | 12 #include "chrome/renderer/gpu_channel_host.h" |
| 14 #include "chrome/renderer/gpu_video_service_host.h" | 13 #include "chrome/renderer/gpu_video_service_host.h" |
| 15 #include "chrome/renderer/media/gles2_video_decode_context.h" | 14 #include "chrome/renderer/media/gles2_video_decode_context.h" |
| 16 #include "chrome/renderer/render_widget.h" | 15 #include "chrome/renderer/render_widget.h" |
| 17 #include "ipc/ipc_channel_handle.h" | 16 #include "ipc/ipc_channel_handle.h" |
| 18 | 17 |
| 19 #if defined(ENABLE_GPU) | 18 #if defined(ENABLE_GPU) |
| 20 #include "gpu/command_buffer/client/gles2_cmd_helper.h" | 19 #include "gpu/command_buffer/client/gles2_cmd_helper.h" |
| 21 #include "gpu/command_buffer/client/gles2_implementation.h" | 20 #include "gpu/command_buffer/client/gles2_implementation.h" |
| 22 #include "gpu/command_buffer/client/gles2_lib.h" | 21 #include "gpu/command_buffer/client/gles2_lib.h" |
| 23 #include "gpu/command_buffer/common/constants.h" | 22 #include "gpu/command_buffer/common/constants.h" |
| 24 #include "gpu/GLES2/gles2_command_buffer.h" | 23 #include "gpu/GLES2/gles2_command_buffer.h" |
| 25 #endif // ENABLE_GPU | 24 #endif // ENABLE_GPU |
| 26 | 25 |
| 27 namespace ggl { | 26 namespace ggl { |
| 28 | 27 |
| 29 #if defined(ENABLE_GPU) | 28 #if defined(ENABLE_GPU) |
| 30 | 29 |
| 31 namespace { | 30 namespace { |
| 32 | 31 |
| 33 const int32 kCommandBufferSize = 1024 * 1024; | 32 const int32 kCommandBufferSize = 1024 * 1024; |
| 34 // TODO(kbr): make the transfer buffer size configurable via context | 33 // TODO(kbr): make the transfer buffer size configurable via context |
| 35 // creation attributes. | 34 // creation attributes. |
| 36 const int32 kTransferBufferSize = 1024 * 1024; | 35 const int32 kTransferBufferSize = 1024 * 1024; |
| 37 | 36 |
| 38 base::ThreadLocalPointer<Context> g_current_context; | |
| 39 | |
| 40 // Singleton used to initialize and terminate the gles2 library. | 37 // Singleton used to initialize and terminate the gles2 library. |
| 41 class GLES2Initializer { | 38 class GLES2Initializer { |
| 42 public: | 39 public: |
| 43 GLES2Initializer() { | 40 GLES2Initializer() { |
| 44 gles2::Initialize(); | 41 gles2::Initialize(); |
| 45 } | 42 } |
| 46 | 43 |
| 47 ~GLES2Initializer() { | 44 ~GLES2Initializer() { |
| 48 gles2::Terminate(); | 45 gles2::Terminate(); |
| 49 } | 46 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 70 #if defined(OS_MACOSX) | 67 #if defined(OS_MACOSX) |
| 71 // Asynchronously resizes an onscreen frame buffer. | 68 // Asynchronously resizes an onscreen frame buffer. |
| 72 void ResizeOnscreen(const gfx::Size& size); | 69 void ResizeOnscreen(const gfx::Size& size); |
| 73 #endif | 70 #endif |
| 74 | 71 |
| 75 // Asynchronously resizes an offscreen frame buffer. | 72 // Asynchronously resizes an offscreen frame buffer. |
| 76 void ResizeOffscreen(const gfx::Size& size); | 73 void ResizeOffscreen(const gfx::Size& size); |
| 77 | 74 |
| 78 // Provides a callback that will be invoked when SwapBuffers has completed | 75 // Provides a callback that will be invoked when SwapBuffers has completed |
| 79 // service side. | 76 // service side. |
| 80 void SetSwapBuffersCallback(Callback1<Context*>::Type* callback) { | 77 void SetSwapBuffersCallback(Callback0::Type* callback) { |
| 81 swap_buffers_callback_.reset(callback); | 78 swap_buffers_callback_.reset(callback); |
| 82 } | 79 } |
| 83 | 80 |
| 84 // For an offscreen frame buffer context, return the frame buffer ID with | 81 // For an offscreen frame buffer context, return the frame buffer ID with |
| 85 // respect to the parent. | 82 // respect to the parent. |
| 86 uint32 parent_texture_id() const { | 83 uint32 parent_texture_id() const { |
| 87 return parent_texture_id_; | 84 return parent_texture_id_; |
| 88 } | 85 } |
| 89 | 86 |
| 90 uint32 CreateParentTexture(const gfx::Size& size) const; | 87 uint32 CreateParentTexture(const gfx::Size& size) const; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 110 | 107 |
| 111 // Get the current error code. Clears context's error code afterwards. | 108 // Get the current error code. Clears context's error code afterwards. |
| 112 Error GetError(); | 109 Error GetError(); |
| 113 | 110 |
| 114 // Replace the current error code with this. | 111 // Replace the current error code with this. |
| 115 void SetError(Error error); | 112 void SetError(Error error); |
| 116 | 113 |
| 117 // TODO(gman): Remove this. | 114 // TODO(gman): Remove this. |
| 118 void DisableShaderTranslation(); | 115 void DisableShaderTranslation(); |
| 119 | 116 |
| 117 gpu::gles2::GLES2Implementation* gles2_implementation() const { |
| 118 return gles2_implementation_; |
| 119 } |
| 120 private: | 120 private: |
| 121 void OnSwapBuffers(); | 121 void OnSwapBuffers(); |
| 122 | 122 |
| 123 scoped_refptr<GpuChannelHost> channel_; | 123 scoped_refptr<GpuChannelHost> channel_; |
| 124 base::WeakPtr<Context> parent_; | 124 base::WeakPtr<Context> parent_; |
| 125 scoped_ptr<Callback1<Context*>::Type> swap_buffers_callback_; | 125 scoped_ptr<Callback0::Type> swap_buffers_callback_; |
| 126 uint32 parent_texture_id_; | 126 uint32 parent_texture_id_; |
| 127 CommandBufferProxy* command_buffer_; | 127 CommandBufferProxy* command_buffer_; |
| 128 gpu::gles2::GLES2CmdHelper* gles2_helper_; | 128 gpu::gles2::GLES2CmdHelper* gles2_helper_; |
| 129 int32 transfer_buffer_id_; | 129 int32 transfer_buffer_id_; |
| 130 gpu::gles2::GLES2Implementation* gles2_implementation_; | 130 gpu::gles2::GLES2Implementation* gles2_implementation_; |
| 131 gfx::Size size_; |
| 132 |
| 131 Error last_error_; | 133 Error last_error_; |
| 132 | 134 |
| 133 DISALLOW_COPY_AND_ASSIGN(Context); | 135 DISALLOW_COPY_AND_ASSIGN(Context); |
| 134 }; | 136 }; |
| 135 | 137 |
| 136 Context::Context(GpuChannelHost* channel, Context* parent) | 138 Context::Context(GpuChannelHost* channel, Context* parent) |
| 137 : channel_(channel), | 139 : channel_(channel), |
| 138 parent_(parent ? parent->AsWeakPtr() : base::WeakPtr<Context>()), | 140 parent_(parent ? parent->AsWeakPtr() : base::WeakPtr<Context>()), |
| 139 parent_texture_id_(0), | 141 parent_texture_id_(0), |
| 140 command_buffer_(NULL), | 142 command_buffer_(NULL), |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 } | 257 } |
| 256 | 258 |
| 257 // Create the object exposing the OpenGL API. | 259 // Create the object exposing the OpenGL API. |
| 258 gles2_implementation_ = new gpu::gles2::GLES2Implementation( | 260 gles2_implementation_ = new gpu::gles2::GLES2Implementation( |
| 259 gles2_helper_, | 261 gles2_helper_, |
| 260 transfer_buffer.size, | 262 transfer_buffer.size, |
| 261 transfer_buffer.ptr, | 263 transfer_buffer.ptr, |
| 262 transfer_buffer_id_, | 264 transfer_buffer_id_, |
| 263 false); | 265 false); |
| 264 | 266 |
| 267 size_ = size; |
| 268 |
| 265 return true; | 269 return true; |
| 266 } | 270 } |
| 267 | 271 |
| 268 #if defined(OS_MACOSX) | 272 #if defined(OS_MACOSX) |
| 269 void Context::ResizeOnscreen(const gfx::Size& size) { | 273 void Context::ResizeOnscreen(const gfx::Size& size) { |
| 270 DCHECK(size.width() > 0 && size.height() > 0); | 274 DCHECK(size.width() > 0 && size.height() > 0); |
| 275 size_ = size; |
| 271 command_buffer_->SetWindowSize(size); | 276 command_buffer_->SetWindowSize(size); |
| 272 } | 277 } |
| 273 #endif | 278 #endif |
| 274 | 279 |
| 275 void Context::ResizeOffscreen(const gfx::Size& size) { | 280 void Context::ResizeOffscreen(const gfx::Size& size) { |
| 276 DCHECK(size.width() > 0 && size.height() > 0); | 281 DCHECK(size.width() > 0 && size.height() > 0); |
| 277 command_buffer_->ResizeOffscreenFrameBuffer(size); | 282 if (size_ != size) { |
| 283 command_buffer_->ResizeOffscreenFrameBuffer(size); |
| 284 size_ = size; |
| 285 } |
| 278 } | 286 } |
| 279 | 287 |
| 280 uint32 Context::CreateParentTexture(const gfx::Size& size) const { | 288 uint32 Context::CreateParentTexture(const gfx::Size& size) const { |
| 281 // Allocate a texture ID with respect to the parent. | 289 // Allocate a texture ID with respect to the parent. |
| 282 if (parent_.get()) { | 290 if (parent_.get()) { |
| 283 if (!MakeCurrent(parent_.get())) | 291 if (!MakeCurrent(parent_.get())) |
| 284 return 0; | 292 return 0; |
| 285 uint32 texture_id = parent_->gles2_implementation_->MakeTextureId(); | 293 uint32 texture_id = parent_->gles2_implementation_->MakeTextureId(); |
| 286 parent_->gles2_implementation_->BindTexture(GL_TEXTURE_2D, texture_id); | 294 parent_->gles2_implementation_->BindTexture(GL_TEXTURE_2D, texture_id); |
| 287 parent_->gles2_implementation_->TexParameteri( | 295 parent_->gles2_implementation_->TexParameteri( |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 | 344 |
| 337 if (channel_ && command_buffer_) { | 345 if (channel_ && command_buffer_) { |
| 338 channel_->DestroyCommandBuffer(command_buffer_); | 346 channel_->DestroyCommandBuffer(command_buffer_); |
| 339 command_buffer_ = NULL; | 347 command_buffer_ = NULL; |
| 340 } | 348 } |
| 341 | 349 |
| 342 channel_ = NULL; | 350 channel_ = NULL; |
| 343 } | 351 } |
| 344 | 352 |
| 345 bool Context::MakeCurrent(Context* context) { | 353 bool Context::MakeCurrent(Context* context) { |
| 346 g_current_context.Set(context); | |
| 347 if (context) { | 354 if (context) { |
| 348 gles2::SetGLContext(context->gles2_implementation_); | 355 gles2::SetGLContext(context->gles2_implementation_); |
| 349 | 356 |
| 350 // Don't request latest error status from service. Just use the locally | 357 // Don't request latest error status from service. Just use the locally |
| 351 // cached information from the last flush. | 358 // cached information from the last flush. |
| 352 // TODO(apatrick): I'm not sure if this should actually change the | 359 // TODO(apatrick): I'm not sure if this should actually change the |
| 353 // current context if it fails. For now it gets changed even if it fails | 360 // current context if it fails. For now it gets changed even if it fails |
| 354 // because making GL calls with a NULL context crashes. | 361 // because making GL calls with a NULL context crashes. |
| 355 if (context->command_buffer_->GetLastState().error != gpu::error::kNoError) | 362 if (context->command_buffer_->GetLastState().error != gpu::error::kNoError) |
| 356 return false; | 363 return false; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 last_error_ = error; | 405 last_error_ = error; |
| 399 } | 406 } |
| 400 | 407 |
| 401 // TODO(gman): Remove This | 408 // TODO(gman): Remove This |
| 402 void Context::DisableShaderTranslation() { | 409 void Context::DisableShaderTranslation() { |
| 403 gles2_implementation_->CommandBufferEnable(PEPPER3D_SKIP_GLSL_TRANSLATION); | 410 gles2_implementation_->CommandBufferEnable(PEPPER3D_SKIP_GLSL_TRANSLATION); |
| 404 } | 411 } |
| 405 | 412 |
| 406 void Context::OnSwapBuffers() { | 413 void Context::OnSwapBuffers() { |
| 407 if (swap_buffers_callback_.get()) | 414 if (swap_buffers_callback_.get()) |
| 408 swap_buffers_callback_->Run(this); | 415 swap_buffers_callback_->Run(); |
| 409 } | 416 } |
| 410 | 417 |
| 411 #endif // ENABLE_GPU | 418 #endif // ENABLE_GPU |
| 412 | 419 |
| 413 Context* CreateViewContext(GpuChannelHost* channel, | 420 Context* CreateViewContext(GpuChannelHost* channel, |
| 414 gfx::NativeViewId view, | 421 gfx::NativeViewId view, |
| 415 int render_view_id, | 422 int render_view_id, |
| 416 const char* allowed_extensions, | 423 const char* allowed_extensions, |
| 417 const int32* attrib_list) { | 424 const int32* attrib_list) { |
| 418 #if defined(ENABLE_GPU) | 425 #if defined(ENABLE_GPU) |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 #endif | 480 #endif |
| 474 } | 481 } |
| 475 | 482 |
| 476 void DeleteParentTexture(Context* context, uint32 texture) { | 483 void DeleteParentTexture(Context* context, uint32 texture) { |
| 477 #if defined(ENABLE_GPU) | 484 #if defined(ENABLE_GPU) |
| 478 context->DeleteParentTexture(texture); | 485 context->DeleteParentTexture(texture); |
| 479 #endif | 486 #endif |
| 480 } | 487 } |
| 481 | 488 |
| 482 void SetSwapBuffersCallback(Context* context, | 489 void SetSwapBuffersCallback(Context* context, |
| 483 Callback1<Context*>::Type* callback) { | 490 Callback0::Type* callback) { |
| 484 #if defined(ENABLE_GPU) | 491 #if defined(ENABLE_GPU) |
| 485 context->SetSwapBuffersCallback(callback); | 492 context->SetSwapBuffersCallback(callback); |
| 486 #endif | 493 #endif |
| 487 } | 494 } |
| 488 | 495 |
| 489 bool MakeCurrent(Context* context) { | 496 bool MakeCurrent(Context* context) { |
| 490 #if defined(ENABLE_GPU) | 497 #if defined(ENABLE_GPU) |
| 491 return Context::MakeCurrent(context); | 498 return Context::MakeCurrent(context); |
| 492 #else | 499 #else |
| 493 return false; | 500 return false; |
| 494 #endif | 501 #endif |
| 495 } | 502 } |
| 496 | 503 |
| 497 Context* GetCurrentContext() { | |
| 498 #if defined(ENABLE_GPU) | |
| 499 return g_current_context.Get(); | |
| 500 #else | |
| 501 return NULL; | |
| 502 #endif | |
| 503 } | |
| 504 | |
| 505 bool SwapBuffers(Context* context) { | 504 bool SwapBuffers(Context* context) { |
| 506 #if defined(ENABLE_GPU) | 505 #if defined(ENABLE_GPU) |
| 507 if (!context) | 506 if (!context) |
| 508 return false; | 507 return false; |
| 509 | 508 |
| 510 return context->SwapBuffers(); | 509 return context->SwapBuffers(); |
| 511 #else | 510 #else |
| 512 return false; | 511 return false; |
| 513 #endif | 512 #endif |
| 514 } | 513 } |
| 515 | 514 |
| 516 bool DestroyContext(Context* context) { | 515 bool DestroyContext(Context* context) { |
| 517 #if defined(ENABLE_GPU) | 516 #if defined(ENABLE_GPU) |
| 518 if (!context) | 517 if (!context) |
| 519 return false; | 518 return false; |
| 520 | 519 |
| 521 if (context == GetCurrentContext()) | |
| 522 MakeCurrent(NULL); | |
| 523 | |
| 524 delete context; | 520 delete context; |
| 525 return true; | 521 return true; |
| 526 #else | 522 #else |
| 527 return false; | 523 return false; |
| 528 #endif | 524 #endif |
| 529 } | 525 } |
| 530 | 526 |
| 531 media::VideoDecodeEngine* CreateVideoDecodeEngine(Context* context) { | 527 media::VideoDecodeEngine* CreateVideoDecodeEngine(Context* context) { |
| 532 return context->CreateVideoDecodeEngine(); | 528 return context->CreateVideoDecodeEngine(); |
| 533 } | 529 } |
| 534 | 530 |
| 535 media::VideoDecodeContext* CreateVideoDecodeContext( | 531 media::VideoDecodeContext* CreateVideoDecodeContext( |
| 536 Context* context, MessageLoop* message_loop, bool hardware_decoder) { | 532 Context* context, MessageLoop* message_loop, bool hardware_decoder) { |
| 537 return context->CreateVideoDecodeContext(message_loop, hardware_decoder); | 533 return context->CreateVideoDecodeContext(message_loop, hardware_decoder); |
| 538 } | 534 } |
| 539 | 535 |
| 540 Error GetError() { | 536 Error GetError(Context* context) { |
| 541 #if defined(ENABLE_GPU) | 537 #if defined(ENABLE_GPU) |
| 542 Context* context = GetCurrentContext(); | |
| 543 if (!context) | |
| 544 return BAD_CONTEXT; | |
| 545 | |
| 546 return context->GetError(); | 538 return context->GetError(); |
| 547 #else | 539 #else |
| 548 return NOT_INITIALIZED; | 540 return NOT_INITIALIZED; |
| 549 #endif | 541 #endif |
| 550 } | 542 } |
| 551 | 543 |
| 552 // TODO(gman): Remove This | 544 // TODO(gman): Remove This |
| 553 void DisableShaderTranslation(Context* context) { | 545 void DisableShaderTranslation(Context* context) { |
| 554 #if defined(ENABLE_GPU) | 546 #if defined(ENABLE_GPU) |
| 555 if (context) { | 547 if (context) { |
| 556 context->DisableShaderTranslation(); | 548 context->DisableShaderTranslation(); |
| 557 } | 549 } |
| 558 #endif | 550 #endif |
| 559 } | 551 } |
| 552 |
| 553 gpu::gles2::GLES2Implementation* GetImplementation(Context* context) { |
| 554 if (!context) |
| 555 return NULL; |
| 556 |
| 557 return context->gles2_implementation(); |
| 558 } |
| 559 |
| 560 } // namespace ggl | 560 } // namespace ggl |
| OLD | NEW |