Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/renderer/pepper_platform_context_3d_impl.h" | 5 #include "content/renderer/pepper_platform_context_3d_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "content/renderer/pepper_parent_context_provider.h" | |
| 8 #include "content/renderer/render_thread_impl.h" | 9 #include "content/renderer/render_thread_impl.h" |
| 9 #include "content/renderer/gpu/renderer_gl_context.h" | 10 #include "content/renderer/gpu/renderer_gl_context.h" |
| 10 #include "content/renderer/gpu/gpu_channel_host.h" | 11 #include "content/renderer/gpu/gpu_channel_host.h" |
| 11 #include "content/renderer/gpu/command_buffer_proxy.h" | 12 #include "content/renderer/gpu/command_buffer_proxy.h" |
| 12 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 13 #include "gpu/command_buffer/client/gles2_cmd_helper.h" | 14 #include "gpu/command_buffer/client/gles2_cmd_helper.h" |
| 14 #include "gpu/command_buffer/client/gles2_implementation.h" | 15 #include "gpu/command_buffer/client/gles2_implementation.h" |
| 15 #include "ui/gfx/gl/gpu_preference.h" | 16 #include "ui/gfx/gl/gpu_preference.h" |
| 16 | 17 |
| 17 #ifdef ENABLE_GPU | 18 #ifdef ENABLE_GPU |
| 18 | 19 |
| 19 PlatformContext3DImpl::PlatformContext3DImpl(RendererGLContext* parent_context) | 20 PlatformContext3DImpl::PlatformContext3DImpl( |
| 20 : parent_context_(parent_context->AsWeakPtr()), | 21 PepperParentContextProvider* parent_context_provider) |
| 22 : parent_context_provider_(parent_context_provider), | |
| 21 parent_texture_id_(0), | 23 parent_texture_id_(0), |
| 22 command_buffer_(NULL), | 24 command_buffer_(NULL), |
| 23 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 25 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 24 } | 26 } |
| 25 | 27 |
| 26 PlatformContext3DImpl::~PlatformContext3DImpl() { | 28 PlatformContext3DImpl::~PlatformContext3DImpl() { |
| 27 if (parent_context_.get() && parent_texture_id_ != 0) { | 29 if (parent_context_.get() && parent_texture_id_ != 0) { |
| 28 // Flush any remaining commands in the parent context to make sure the | 30 // Flush any remaining commands in the parent context to make sure the |
| 29 // texture id accounting stays consistent. | 31 // texture id accounting stays consistent. |
| 30 gpu::gles2::GLES2Implementation* parent_gles2 = | 32 gpu::gles2::GLES2Implementation* parent_gles2 = |
| 31 parent_context_->GetImplementation(); | 33 parent_context_->GetImplementation(); |
| 32 parent_gles2->helper()->CommandBufferHelper::Finish(); | 34 parent_gles2->helper()->CommandBufferHelper::Finish(); |
| 33 parent_gles2->FreeTextureId(parent_texture_id_); | 35 parent_gles2->FreeTextureId(parent_texture_id_); |
| 34 } | 36 } |
| 35 | 37 |
| 36 if (command_buffer_) { | 38 if (command_buffer_) { |
| 37 DCHECK(channel_.get()); | 39 DCHECK(channel_.get()); |
| 38 channel_->DestroyCommandBuffer(command_buffer_); | 40 channel_->DestroyCommandBuffer(command_buffer_); |
| 39 command_buffer_ = NULL; | 41 command_buffer_ = NULL; |
| 42 if (channel_->WillGpuSwitchOccur(false, gfx::PreferDiscreteGpu)) { | |
| 43 channel_->ForciblyCloseChannel(); | |
| 44 } | |
| 40 } | 45 } |
| 41 | 46 |
| 42 channel_ = NULL; | 47 channel_ = NULL; |
| 43 } | 48 } |
| 44 | 49 |
| 45 bool PlatformContext3DImpl::Init(const int32* attrib_list) { | 50 bool PlatformContext3DImpl::Init(const int32* attrib_list) { |
| 46 // Ignore initializing more than once. | 51 // Ignore initializing more than once. |
| 47 if (command_buffer_) | 52 if (command_buffer_) |
| 48 return true; | 53 return true; |
| 49 | 54 |
| 50 // Parent may already have been deleted. | 55 if (!parent_context_provider_) |
| 51 if (!parent_context_.get()) | |
| 52 return false; | 56 return false; |
| 53 | 57 |
| 54 RenderThreadImpl* render_thread = RenderThreadImpl::current(); | 58 RenderThreadImpl* render_thread = RenderThreadImpl::current(); |
| 55 if (!render_thread) | 59 if (!render_thread) |
| 56 return false; | 60 return false; |
| 57 | 61 |
| 58 channel_ = render_thread->GetGpuChannel(); | 62 bool retry = false; |
| 59 if (!channel_.get()) | 63 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu; |
| 60 return false; | |
| 61 | 64 |
| 62 DCHECK(channel_->state() == GpuChannelHost::kConnected); | 65 // Note similar code in WebGraphicsContext3DCommandBufferImpl::initialize. |
| 63 | 66 do { |
| 64 // Flush any remaining commands in the parent context to make sure the | 67 channel_ = render_thread->EstablishGpuChannelSync( |
| 65 // texture id accounting stays consistent. | 68 content::CAUSE_FOR_GPU_LAUNCH_PEPPERPLATFORMCONTEXT3DIMPL_INITIALIZE); |
| 66 gpu::gles2::GLES2Implementation* parent_gles2 = | 69 if (!channel_.get()) |
| 67 parent_context_->GetImplementation(); | 70 return false; |
| 68 parent_gles2->helper()->CommandBufferHelper::Finish(); | 71 DCHECK(channel_->state() == GpuChannelHost::kConnected); |
| 69 parent_texture_id_ = parent_gles2->MakeTextureId(); | 72 if (!retry) { |
| 73 // If the creation of this context requires all contexts for this | |
| 74 // renderer to be destroyed on the GPU process side, then drop the | |
| 75 // channel and recreate it. | |
| 76 if (channel_->WillGpuSwitchOccur(true, gpu_preference)) { | |
| 77 channel_->ForciblyCloseChannel(); | |
| 78 retry = true; | |
| 79 } | |
| 80 } else { | |
| 81 retry = false; | |
| 82 } | |
| 83 } while (retry); | |
| 70 | 84 |
| 71 gfx::Size surface_size; | 85 gfx::Size surface_size; |
| 72 std::vector<int32> attribs; | 86 std::vector<int32> attribs; |
| 73 // TODO(alokp): Change GpuChannelHost::CreateOffscreenCommandBuffer() | 87 // TODO(alokp): Change GpuChannelHost::CreateOffscreenCommandBuffer() |
| 74 // interface to accept width and height in the attrib_list so that | 88 // interface to accept width and height in the attrib_list so that |
| 75 // we do not need to filter for width and height here. | 89 // we do not need to filter for width and height here. |
| 76 if (attrib_list) { | 90 if (attrib_list) { |
| 77 for (const int32_t* attr = attrib_list; | 91 for (const int32_t* attr = attrib_list; |
| 78 attr[0] != RendererGLContext::NONE; | 92 attr[0] != RendererGLContext::NONE; |
| 79 attr += 2) { | 93 attr += 2) { |
| 80 switch (attr[0]) { | 94 switch (attr[0]) { |
| 81 case RendererGLContext::WIDTH: | 95 case RendererGLContext::WIDTH: |
| 82 surface_size.set_width(attr[1]); | 96 surface_size.set_width(attr[1]); |
| 83 break; | 97 break; |
| 84 case RendererGLContext::HEIGHT: | 98 case RendererGLContext::HEIGHT: |
| 85 surface_size.set_height(attr[1]); | 99 surface_size.set_height(attr[1]); |
| 86 break; | 100 break; |
| 87 default: | 101 default: |
| 88 attribs.push_back(attr[0]); | 102 attribs.push_back(attr[0]); |
| 89 attribs.push_back(attr[1]); | 103 attribs.push_back(attr[1]); |
| 90 break; | 104 break; |
| 91 } | 105 } |
| 92 } | 106 } |
| 93 attribs.push_back(RendererGLContext::NONE); | 107 attribs.push_back(RendererGLContext::NONE); |
| 94 } | 108 } |
| 95 | 109 |
| 96 CommandBufferProxy* parent_command_buffer = | |
| 97 parent_context_->GetCommandBufferProxy(); | |
| 98 command_buffer_ = channel_->CreateOffscreenCommandBuffer( | 110 command_buffer_ = channel_->CreateOffscreenCommandBuffer( |
| 99 surface_size, | 111 surface_size, |
| 100 NULL, | 112 NULL, |
| 101 "*", | 113 "*", |
| 102 attribs, | 114 attribs, |
| 103 GURL::EmptyGURL(), | 115 GURL::EmptyGURL(), |
| 104 gfx::PreferDiscreteGpu); | 116 gpu_preference); |
| 105 if (!command_buffer_) | 117 if (!command_buffer_) |
| 106 return false; | 118 return false; |
| 107 | 119 |
| 108 if (!command_buffer_->SetParent(parent_command_buffer, parent_texture_id_)) | |
| 109 return false; | |
| 110 | |
| 111 command_buffer_->SetChannelErrorCallback( | 120 command_buffer_->SetChannelErrorCallback( |
| 112 base::Bind(&PlatformContext3DImpl::OnContextLost, | 121 base::Bind(&PlatformContext3DImpl::OnContextLost, |
| 113 weak_ptr_factory_.GetWeakPtr())); | 122 weak_ptr_factory_.GetWeakPtr())); |
| 114 | 123 |
| 124 // Fetch the parent context now, after any potential shutdown of the | |
| 125 // channel due to GPU switching, and creation of the Pepper 3D | |
| 126 // context with the discrete GPU preference. | |
| 127 RendererGLContext* parent_context = | |
| 128 parent_context_provider_->GetParentContextForPlatformContext3D(); | |
| 129 if (!parent_context) | |
| 130 return false; | |
| 131 | |
| 132 parent_context_ = parent_context->AsWeakPtr(); | |
| 133 | |
|
apatrick_chromium
2011/10/19 18:24:27
You could null parent_context_provider_ here since
Ken Russell (switch to Gerrit)
2011/10/19 22:04:45
Done.
| |
| 134 // Flush any remaining commands in the parent context to make sure the | |
| 135 // texture id accounting stays consistent. | |
| 136 gpu::gles2::GLES2Implementation* parent_gles2 = | |
| 137 parent_context_->GetImplementation(); | |
| 138 parent_gles2->helper()->CommandBufferHelper::Finish(); | |
| 139 parent_texture_id_ = parent_gles2->MakeTextureId(); | |
| 140 | |
| 141 CommandBufferProxy* parent_command_buffer = | |
| 142 parent_context_->GetCommandBufferProxy(); | |
| 143 if (!command_buffer_->SetParent(parent_command_buffer, parent_texture_id_)) | |
| 144 return false; | |
| 145 | |
| 115 return true; | 146 return true; |
| 116 } | 147 } |
| 117 | 148 |
| 118 unsigned PlatformContext3DImpl::GetBackingTextureId() { | 149 unsigned PlatformContext3DImpl::GetBackingTextureId() { |
| 119 DCHECK(command_buffer_); | 150 DCHECK(command_buffer_); |
| 120 return parent_texture_id_; | 151 return parent_texture_id_; |
| 121 } | 152 } |
| 122 | 153 |
| 123 gpu::CommandBuffer* PlatformContext3DImpl::GetCommandBuffer() { | 154 gpu::CommandBuffer* PlatformContext3DImpl::GetCommandBuffer() { |
| 124 return command_buffer_; | 155 return command_buffer_; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 138 } | 169 } |
| 139 | 170 |
| 140 void PlatformContext3DImpl::OnContextLost() { | 171 void PlatformContext3DImpl::OnContextLost() { |
| 141 DCHECK(command_buffer_); | 172 DCHECK(command_buffer_); |
| 142 | 173 |
| 143 if (!context_lost_callback_.is_null()) | 174 if (!context_lost_callback_.is_null()) |
| 144 context_lost_callback_.Run(); | 175 context_lost_callback_.Run(); |
| 145 } | 176 } |
| 146 | 177 |
| 147 #endif // ENABLE_GPU | 178 #endif // ENABLE_GPU |
| OLD | NEW |