OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/pepper_platform_context_3d_impl.h" | 5 #include "content/renderer/pepper/pepper_platform_context_3d_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "content/common/gpu/client/context_provider_command_buffer.h" |
8 #include "content/common/gpu/client/gpu_channel_host.h" | 9 #include "content/common/gpu/client/gpu_channel_host.h" |
9 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" | 10 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" |
10 #include "content/renderer/pepper/pepper_parent_context_provider.h" | |
11 #include "content/renderer/render_thread_impl.h" | 11 #include "content/renderer/render_thread_impl.h" |
12 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
13 #include "gpu/command_buffer/client/gles2_cmd_helper.h" | 13 #include "gpu/command_buffer/client/gles2_cmd_helper.h" |
14 #include "gpu/command_buffer/client/gles2_implementation.h" | 14 #include "gpu/command_buffer/client/gles2_implementation.h" |
15 #include "gpu/ipc/command_buffer_proxy.h" | 15 #include "gpu/ipc/command_buffer_proxy.h" |
16 #include "ppapi/c/pp_graphics_3d.h" | 16 #include "ppapi/c/pp_graphics_3d.h" |
17 #include "ui/gl/gpu_preference.h" | 17 #include "ui/gl/gpu_preference.h" |
18 | 18 |
19 #ifdef ENABLE_GPU | 19 #ifdef ENABLE_GPU |
20 | 20 |
21 namespace content { | 21 namespace content { |
22 | 22 |
23 PlatformContext3DImpl::PlatformContext3DImpl( | 23 PlatformContext3DImpl::PlatformContext3DImpl() |
24 PepperParentContextProvider* parent_context_provider) | 24 : parent_texture_id_(0), |
25 : parent_context_provider_(parent_context_provider), | 25 has_alpha_(false), |
26 parent_texture_id_(0), | 26 command_buffer_(NULL), |
27 has_alpha_(false), | 27 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
28 command_buffer_(NULL), | |
29 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
30 } | 28 } |
31 | 29 |
32 PlatformContext3DImpl::~PlatformContext3DImpl() { | 30 PlatformContext3DImpl::~PlatformContext3DImpl() { |
33 if (parent_context_.get() && parent_texture_id_ != 0) { | 31 DestroyParentContextProviderAndBackingTexture(); |
34 // Flush any remaining commands in the parent context to make sure the | |
35 // texture id accounting stays consistent. | |
36 gpu::gles2::GLES2Implementation* parent_gles2 = | |
37 parent_context_->GetImplementation(); | |
38 parent_gles2->helper()->CommandBufferHelper::Finish(); | |
39 parent_gles2->FreeTextureId(parent_texture_id_); | |
40 } | |
41 | 32 |
42 if (command_buffer_) { | 33 if (command_buffer_) { |
43 DCHECK(channel_.get()); | 34 DCHECK(channel_.get()); |
44 channel_->DestroyCommandBuffer(command_buffer_); | 35 channel_->DestroyCommandBuffer(command_buffer_); |
45 command_buffer_ = NULL; | 36 command_buffer_ = NULL; |
46 } | 37 } |
47 | 38 |
48 channel_ = NULL; | 39 channel_ = NULL; |
49 } | 40 } |
50 | 41 |
51 bool PlatformContext3DImpl::Init(const int32* attrib_list, | 42 bool PlatformContext3DImpl::Init(const int32* attrib_list, |
52 PlatformContext3D* share_context) { | 43 PlatformContext3D* share_context) { |
53 // Ignore initializing more than once. | 44 // Ignore initializing more than once. |
54 if (command_buffer_) | 45 if (command_buffer_) |
55 return true; | 46 return true; |
56 | 47 |
57 if (!parent_context_provider_) | |
58 return false; | |
59 | |
60 RenderThreadImpl* render_thread = RenderThreadImpl::current(); | 48 RenderThreadImpl* render_thread = RenderThreadImpl::current(); |
61 if (!render_thread) | 49 if (!render_thread) |
62 return false; | 50 return false; |
63 | 51 |
64 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu; | 52 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu; |
65 | 53 |
66 channel_ = render_thread->EstablishGpuChannelSync( | 54 channel_ = render_thread->EstablishGpuChannelSync( |
67 CAUSE_FOR_GPU_LAUNCH_PEPPERPLATFORMCONTEXT3DIMPL_INITIALIZE); | 55 CAUSE_FOR_GPU_LAUNCH_PEPPERPLATFORMCONTEXT3DIMPL_INITIALIZE); |
68 if (!channel_.get()) | 56 if (!channel_.get()) |
69 return false; | 57 return false; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 if (!command_buffer_) | 107 if (!command_buffer_) |
120 return false; | 108 return false; |
121 | 109 |
122 command_buffer_->SetChannelErrorCallback( | 110 command_buffer_->SetChannelErrorCallback( |
123 base::Bind(&PlatformContext3DImpl::OnContextLost, | 111 base::Bind(&PlatformContext3DImpl::OnContextLost, |
124 weak_ptr_factory_.GetWeakPtr())); | 112 weak_ptr_factory_.GetWeakPtr())); |
125 command_buffer_->SetOnConsoleMessageCallback( | 113 command_buffer_->SetOnConsoleMessageCallback( |
126 base::Bind(&PlatformContext3DImpl::OnConsoleMessage, | 114 base::Bind(&PlatformContext3DImpl::OnConsoleMessage, |
127 weak_ptr_factory_.GetWeakPtr())); | 115 weak_ptr_factory_.GetWeakPtr())); |
128 | 116 |
129 // Fetch the parent context now, after any potential shutdown of the | 117 return SetParentAndCreateBackingTextureIfNeeded(); |
130 // channel due to GPU switching, and creation of the Pepper 3D | 118 } |
131 // context with the discrete GPU preference. | 119 |
132 WebGraphicsContext3DCommandBufferImpl* parent_context = | 120 bool PlatformContext3DImpl::SetParentAndCreateBackingTextureIfNeeded() { |
133 parent_context_provider_->GetParentContextForPlatformContext3D(); | 121 if (parent_context_provider_ && |
134 if (!parent_context) | 122 !parent_context_provider_->DestroyedOnMainThread() && |
| 123 parent_texture_id_) |
| 124 return true; |
| 125 |
| 126 parent_context_provider_ = |
| 127 RenderThreadImpl::current()->OffscreenContextProviderForMainThread(); |
| 128 if (!parent_context_provider_->InitializeOnMainThread() || |
| 129 !parent_context_provider_->BindToCurrentThread()) { |
| 130 DestroyParentContextProviderAndBackingTexture(); |
135 return false; | 131 return false; |
136 | 132 } |
137 parent_context_provider_ = NULL; | |
138 parent_context_ = parent_context->AsWeakPtr(); | |
139 | 133 |
140 // Flush any remaining commands in the parent context to make sure the | 134 // Flush any remaining commands in the parent context to make sure the |
141 // texture id accounting stays consistent. | 135 // texture id accounting stays consistent. |
142 gpu::gles2::GLES2Implementation* parent_gles2 = | 136 gpu::gles2::GLES2Implementation* parent_gles2 = |
143 parent_context_->GetImplementation(); | 137 parent_context_provider_->Context3d()->GetImplementation(); |
144 parent_gles2->helper()->CommandBufferHelper::Finish(); | 138 parent_gles2->helper()->CommandBufferHelper::Finish(); |
145 parent_texture_id_ = parent_gles2->MakeTextureId(); | 139 parent_texture_id_ = parent_gles2->MakeTextureId(); |
146 | 140 |
147 CommandBufferProxyImpl* parent_command_buffer = | 141 CommandBufferProxyImpl* parent_command_buffer = |
148 parent_context_->GetCommandBufferProxy(); | 142 parent_context_provider_->Context3d()->GetCommandBufferProxy(); |
149 if (!command_buffer_->SetParent(parent_command_buffer, parent_texture_id_)) | 143 if (!command_buffer_->SetParent(parent_command_buffer, parent_texture_id_)) |
150 return false; | 144 return false; |
151 | 145 |
152 return true; | 146 return true; |
153 } | 147 } |
154 | 148 |
155 void PlatformContext3DImpl::SetParentContext( | 149 void PlatformContext3DImpl::DestroyParentContextProviderAndBackingTexture() { |
156 PepperParentContextProvider* parent_context_provider) { | 150 if (!parent_context_provider_) |
157 if (parent_context_.get() && parent_texture_id_ != 0) { | 151 return; |
| 152 |
| 153 if (parent_texture_id_) { |
158 // Flush any remaining commands in the parent context to make sure the | 154 // Flush any remaining commands in the parent context to make sure the |
159 // texture id accounting stays consistent. | 155 // texture id accounting stays consistent. |
160 gpu::gles2::GLES2Implementation* parent_gles2 = | 156 gpu::gles2::GLES2Implementation* parent_gles2 = |
161 parent_context_->GetImplementation(); | 157 parent_context_provider_->Context3d()->GetImplementation(); |
162 parent_gles2->helper()->CommandBufferHelper::Flush(); | 158 parent_gles2->helper()->CommandBufferHelper::Finish(); |
163 parent_gles2->FreeTextureId(parent_texture_id_); | 159 parent_gles2->FreeTextureId(parent_texture_id_); |
164 parent_context_.reset(); | |
165 parent_texture_id_ = 0; | 160 parent_texture_id_ = 0; |
166 } | 161 } |
167 | 162 |
168 WebGraphicsContext3DCommandBufferImpl* parent_context = | 163 parent_context_provider_ = NULL; |
169 parent_context_provider->GetParentContextForPlatformContext3D(); | |
170 if (!parent_context) | |
171 return; | |
172 | |
173 parent_context_ = parent_context->AsWeakPtr(); | |
174 // Flush any remaining commands in the parent context to make sure the | |
175 // texture id accounting stays consistent. | |
176 gpu::gles2::GLES2Implementation* parent_gles2 = | |
177 parent_context_->GetImplementation(); | |
178 parent_gles2->helper()->CommandBufferHelper::Flush(); | |
179 parent_texture_id_ = parent_gles2->MakeTextureId(); | |
180 | |
181 CommandBufferProxyImpl* parent_command_buffer = | |
182 parent_context_->GetCommandBufferProxy(); | |
183 command_buffer_->SetParent(parent_command_buffer, parent_texture_id_); | |
184 } | 164 } |
185 | 165 |
186 unsigned PlatformContext3DImpl::GetBackingTextureId() { | 166 unsigned PlatformContext3DImpl::GetBackingTextureId() { |
187 DCHECK(command_buffer_); | 167 DCHECK(command_buffer_); |
188 return parent_texture_id_; | 168 return parent_texture_id_; |
189 } | 169 } |
190 | 170 |
191 WebKit::WebGraphicsContext3D* PlatformContext3DImpl::GetParentContext() { | 171 WebKit::WebGraphicsContext3D* PlatformContext3DImpl::GetParentContext() { |
192 return parent_context_.get(); | 172 if (!parent_context_provider_) |
| 173 return NULL; |
| 174 return parent_context_provider_->Context3d(); |
193 } | 175 } |
194 | 176 |
195 bool PlatformContext3DImpl::IsOpaque() { | 177 bool PlatformContext3DImpl::IsOpaque() { |
196 DCHECK(command_buffer_); | 178 DCHECK(command_buffer_); |
197 return !has_alpha_; | 179 return !has_alpha_; |
198 } | 180 } |
199 | 181 |
200 gpu::CommandBuffer* PlatformContext3DImpl::GetCommandBuffer() { | 182 gpu::CommandBuffer* PlatformContext3DImpl::GetCommandBuffer() { |
201 return command_buffer_; | 183 return command_buffer_; |
202 } | 184 } |
(...skipping 26 matching lines...) Expand all Loading... |
229 void PlatformContext3DImpl::OnConsoleMessage(const std::string& msg, int id) { | 211 void PlatformContext3DImpl::OnConsoleMessage(const std::string& msg, int id) { |
230 DCHECK(command_buffer_); | 212 DCHECK(command_buffer_); |
231 | 213 |
232 if (!console_message_callback_.is_null()) | 214 if (!console_message_callback_.is_null()) |
233 console_message_callback_.Run(msg, id); | 215 console_message_callback_.Run(msg, id); |
234 } | 216 } |
235 | 217 |
236 } // namespace content | 218 } // namespace content |
237 | 219 |
238 #endif // ENABLE_GPU | 220 #endif // ENABLE_GPU |
OLD | NEW |