Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / renderer / pepper / ppb_graphics_3d_impl.cc
blob662945c19abce3f64140a8096ce9cc228bbc30b8
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
3 // found in the LICENSE file.
5 #include "content/renderer/pepper/ppb_graphics_3d_impl.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
14 #include "content/common/gpu/client/gpu_channel_host.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/common/web_preferences.h"
17 #include "content/renderer/pepper/host_globals.h"
18 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
19 #include "content/renderer/pepper/plugin_instance_throttler_impl.h"
20 #include "content/renderer/pepper/plugin_module.h"
21 #include "content/renderer/render_thread_impl.h"
22 #include "content/renderer/render_view_impl.h"
23 #include "gpu/command_buffer/client/gles2_implementation.h"
24 #include "ppapi/c/ppp_graphics_3d.h"
25 #include "ppapi/thunk/enter.h"
26 #include "third_party/WebKit/public/platform/WebString.h"
27 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
28 #include "third_party/WebKit/public/web/WebDocument.h"
29 #include "third_party/WebKit/public/web/WebElement.h"
30 #include "third_party/WebKit/public/web/WebLocalFrame.h"
31 #include "third_party/WebKit/public/web/WebPluginContainer.h"
33 using ppapi::thunk::EnterResourceNoLock;
34 using ppapi::thunk::PPB_Graphics3D_API;
35 using blink::WebConsoleMessage;
36 using blink::WebLocalFrame;
37 using blink::WebPluginContainer;
38 using blink::WebString;
40 namespace content {
42 namespace {
44 const int32 kCommandBufferSize = 1024 * 1024;
45 const int32 kTransferBufferSize = 1024 * 1024;
47 } // namespace
49 PPB_Graphics3D_Impl::PPB_Graphics3D_Impl(PP_Instance instance)
50 : PPB_Graphics3D_Shared(instance),
51 bound_to_instance_(false),
52 commit_pending_(false),
53 sync_point_(0),
54 has_alpha_(false),
55 command_buffer_(NULL),
56 weak_ptr_factory_(this) {}
58 PPB_Graphics3D_Impl::~PPB_Graphics3D_Impl() {
59 DestroyGLES2Impl();
60 if (command_buffer_) {
61 DCHECK(channel_.get());
62 channel_->DestroyCommandBuffer(command_buffer_);
63 command_buffer_ = NULL;
66 channel_ = NULL;
69 // static
70 PP_Resource PPB_Graphics3D_Impl::Create(PP_Instance instance,
71 PP_Resource share_context,
72 const int32_t* attrib_list) {
73 PPB_Graphics3D_API* share_api = NULL;
74 if (share_context) {
75 EnterResourceNoLock<PPB_Graphics3D_API> enter(share_context, true);
76 if (enter.failed())
77 return 0;
78 share_api = enter.object();
80 scoped_refptr<PPB_Graphics3D_Impl> graphics_3d(
81 new PPB_Graphics3D_Impl(instance));
82 if (!graphics_3d->Init(share_api, attrib_list))
83 return 0;
84 return graphics_3d->GetReference();
87 // static
88 PP_Resource PPB_Graphics3D_Impl::CreateRaw(
89 PP_Instance instance,
90 PP_Resource share_context,
91 const int32_t* attrib_list,
92 gpu::Capabilities* capabilities,
93 base::SharedMemoryHandle* shared_state_handle) {
94 PPB_Graphics3D_API* share_api = NULL;
95 if (share_context) {
96 EnterResourceNoLock<PPB_Graphics3D_API> enter(share_context, true);
97 if (enter.failed())
98 return 0;
99 share_api = enter.object();
101 scoped_refptr<PPB_Graphics3D_Impl> graphics_3d(
102 new PPB_Graphics3D_Impl(instance));
103 if (!graphics_3d->InitRaw(share_api, attrib_list, capabilities,
104 shared_state_handle))
105 return 0;
106 return graphics_3d->GetReference();
109 PP_Bool PPB_Graphics3D_Impl::SetGetBuffer(int32_t transfer_buffer_id) {
110 GetCommandBuffer()->SetGetBuffer(transfer_buffer_id);
111 return PP_TRUE;
114 scoped_refptr<gpu::Buffer> PPB_Graphics3D_Impl::CreateTransferBuffer(
115 uint32_t size,
116 int32_t* id) {
117 return GetCommandBuffer()->CreateTransferBuffer(size, id);
120 PP_Bool PPB_Graphics3D_Impl::DestroyTransferBuffer(int32_t id) {
121 GetCommandBuffer()->DestroyTransferBuffer(id);
122 return PP_TRUE;
125 PP_Bool PPB_Graphics3D_Impl::Flush(int32_t put_offset) {
126 GetCommandBuffer()->Flush(put_offset);
127 return PP_TRUE;
130 gpu::CommandBuffer::State PPB_Graphics3D_Impl::WaitForTokenInRange(
131 int32_t start,
132 int32_t end) {
133 GetCommandBuffer()->WaitForTokenInRange(start, end);
134 return GetCommandBuffer()->GetLastState();
137 gpu::CommandBuffer::State PPB_Graphics3D_Impl::WaitForGetOffsetInRange(
138 int32_t start,
139 int32_t end) {
140 GetCommandBuffer()->WaitForGetOffsetInRange(start, end);
141 return GetCommandBuffer()->GetLastState();
144 uint32_t PPB_Graphics3D_Impl::InsertSyncPoint() {
145 return command_buffer_->InsertSyncPoint();
148 uint32_t PPB_Graphics3D_Impl::InsertFutureSyncPoint() {
149 return command_buffer_->InsertFutureSyncPoint();
152 void PPB_Graphics3D_Impl::RetireSyncPoint(uint32_t sync_point) {
153 return command_buffer_->RetireSyncPoint(sync_point);
156 bool PPB_Graphics3D_Impl::BindToInstance(bool bind) {
157 bound_to_instance_ = bind;
158 return true;
161 bool PPB_Graphics3D_Impl::IsOpaque() { return !has_alpha_; }
163 void PPB_Graphics3D_Impl::ViewInitiatedPaint() {
164 commit_pending_ = false;
166 if (HasPendingSwap())
167 SwapBuffersACK(PP_OK);
170 int PPB_Graphics3D_Impl::GetCommandBufferRouteId() {
171 DCHECK(command_buffer_);
172 return command_buffer_->GetRouteID();
175 gpu::CommandBuffer* PPB_Graphics3D_Impl::GetCommandBuffer() {
176 return command_buffer_;
179 gpu::GpuControl* PPB_Graphics3D_Impl::GetGpuControl() {
180 return command_buffer_;
183 int32 PPB_Graphics3D_Impl::DoSwapBuffers() {
184 DCHECK(command_buffer_);
185 // We do not have a GLES2 implementation when using an OOP proxy.
186 // The plugin-side proxy is responsible for adding the SwapBuffers command
187 // to the command buffer in that case.
188 if (gles2_impl())
189 gles2_impl()->SwapBuffers();
191 // Since the backing texture has been updated, a new sync point should be
192 // inserted.
193 sync_point_ = command_buffer_->InsertSyncPoint();
195 if (bound_to_instance_) {
196 // If we are bound to the instance, we need to ask the compositor
197 // to commit our backing texture so that the graphics appears on the page.
198 // When the backing texture will be committed we get notified via
199 // ViewFlushedPaint().
201 // Don't need to check for NULL from GetPluginInstance since when we're
202 // bound, we know our instance is valid.
203 HostGlobals::Get()->GetInstance(pp_instance())->CommitBackingTexture();
204 commit_pending_ = true;
205 } else {
206 // Wait for the command to complete on the GPU to allow for throttling.
207 command_buffer_->SignalSyncPoint(
208 sync_point_,
209 base::Bind(&PPB_Graphics3D_Impl::OnSwapBuffers,
210 weak_ptr_factory_.GetWeakPtr()));
213 return PP_OK_COMPLETIONPENDING;
216 bool PPB_Graphics3D_Impl::Init(PPB_Graphics3D_API* share_context,
217 const int32_t* attrib_list) {
218 if (!InitRaw(share_context, attrib_list, NULL, NULL))
219 return false;
221 gpu::gles2::GLES2Implementation* share_gles2 = NULL;
222 if (share_context) {
223 share_gles2 =
224 static_cast<PPB_Graphics3D_Shared*>(share_context)->gles2_impl();
227 return CreateGLES2Impl(kCommandBufferSize, kTransferBufferSize, share_gles2);
230 bool PPB_Graphics3D_Impl::InitRaw(
231 PPB_Graphics3D_API* share_context,
232 const int32_t* attrib_list,
233 gpu::Capabilities* capabilities,
234 base::SharedMemoryHandle* shared_state_handle) {
235 PepperPluginInstanceImpl* plugin_instance =
236 HostGlobals::Get()->GetInstance(pp_instance());
237 if (!plugin_instance)
238 return false;
240 const WebPreferences& prefs =
241 static_cast<RenderViewImpl*>(plugin_instance->GetRenderView())
242 ->webkit_preferences();
243 // 3D access might be disabled or blacklisted.
244 if (!prefs.pepper_3d_enabled)
245 return false;
247 // Force SW rendering for keyframe extraction to avoid pixel reads from VRAM.
248 PluginInstanceThrottlerImpl* throttler = plugin_instance->throttler();
249 if (throttler && throttler->needs_representative_keyframe())
250 return false;
252 RenderThreadImpl* render_thread = RenderThreadImpl::current();
253 if (!render_thread)
254 return false;
256 channel_ = render_thread->EstablishGpuChannelSync(
257 CAUSE_FOR_GPU_LAUNCH_PEPPERPLATFORMCONTEXT3DIMPL_INITIALIZE);
258 if (!channel_.get())
259 return false;
261 gfx::Size surface_size;
262 std::vector<int32> attribs;
263 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
264 // TODO(alokp): Change GpuChannelHost::CreateOffscreenCommandBuffer()
265 // interface to accept width and height in the attrib_list so that
266 // we do not need to filter for width and height here.
267 if (attrib_list) {
268 for (const int32_t* attr = attrib_list; attr[0] != PP_GRAPHICS3DATTRIB_NONE;
269 attr += 2) {
270 switch (attr[0]) {
271 case PP_GRAPHICS3DATTRIB_WIDTH:
272 surface_size.set_width(attr[1]);
273 break;
274 case PP_GRAPHICS3DATTRIB_HEIGHT:
275 surface_size.set_height(attr[1]);
276 break;
277 case PP_GRAPHICS3DATTRIB_GPU_PREFERENCE:
278 gpu_preference =
279 (attr[1] == PP_GRAPHICS3DATTRIB_GPU_PREFERENCE_LOW_POWER)
280 ? gfx::PreferIntegratedGpu
281 : gfx::PreferDiscreteGpu;
282 break;
283 case PP_GRAPHICS3DATTRIB_ALPHA_SIZE:
284 has_alpha_ = attr[1] > 0;
285 // fall-through
286 default:
287 attribs.push_back(attr[0]);
288 attribs.push_back(attr[1]);
289 break;
292 attribs.push_back(PP_GRAPHICS3DATTRIB_NONE);
295 CommandBufferProxyImpl* share_buffer = NULL;
296 if (share_context) {
297 PPB_Graphics3D_Impl* share_graphics =
298 static_cast<PPB_Graphics3D_Impl*>(share_context);
299 share_buffer = share_graphics->command_buffer_;
302 command_buffer_ = channel_->CreateOffscreenCommandBuffer(
303 surface_size, share_buffer, attribs, GURL::EmptyGURL(), gpu_preference);
304 if (!command_buffer_)
305 return false;
306 if (!command_buffer_->Initialize())
307 return false;
308 if (shared_state_handle)
309 *shared_state_handle = command_buffer_->GetSharedStateHandle();
310 if (capabilities)
311 *capabilities = command_buffer_->GetCapabilities();
312 mailbox_ = gpu::Mailbox::Generate();
313 if (!command_buffer_->ProduceFrontBuffer(mailbox_))
314 return false;
315 sync_point_ = command_buffer_->InsertSyncPoint();
317 command_buffer_->SetContextLostCallback(base::Bind(
318 &PPB_Graphics3D_Impl::OnContextLost, weak_ptr_factory_.GetWeakPtr()));
320 command_buffer_->SetOnConsoleMessageCallback(base::Bind(
321 &PPB_Graphics3D_Impl::OnConsoleMessage, weak_ptr_factory_.GetWeakPtr()));
322 return true;
325 void PPB_Graphics3D_Impl::OnConsoleMessage(const std::string& message, int id) {
326 if (!bound_to_instance_)
327 return;
328 WebPluginContainer* container =
329 HostGlobals::Get()->GetInstance(pp_instance())->container();
330 if (!container)
331 return;
332 WebLocalFrame* frame = container->element().document().frame();
333 if (!frame)
334 return;
335 WebConsoleMessage console_message = WebConsoleMessage(
336 WebConsoleMessage::LevelError, WebString(base::UTF8ToUTF16(message)));
337 frame->addMessageToConsole(console_message);
340 void PPB_Graphics3D_Impl::OnSwapBuffers() {
341 if (HasPendingSwap()) {
342 // If we're off-screen, no need to trigger and wait for compositing.
343 // Just send the swap-buffers ACK to the plugin immediately.
344 commit_pending_ = false;
345 SwapBuffersACK(PP_OK);
349 void PPB_Graphics3D_Impl::OnContextLost() {
350 // Don't need to check for NULL from GetPluginInstance since when we're
351 // bound, we know our instance is valid.
352 if (bound_to_instance_) {
353 HostGlobals::Get()->GetInstance(pp_instance())->BindGraphics(pp_instance(),
357 // Send context lost to plugin. This may have been caused by a PPAPI call, so
358 // avoid re-entering.
359 base::ThreadTaskRunnerHandle::Get()->PostTask(
360 FROM_HERE, base::Bind(&PPB_Graphics3D_Impl::SendContextLost,
361 weak_ptr_factory_.GetWeakPtr()));
364 void PPB_Graphics3D_Impl::SendContextLost() {
365 // By the time we run this, the instance may have been deleted, or in the
366 // process of being deleted. Even in the latter case, we don't want to send a
367 // callback after DidDestroy.
368 PepperPluginInstanceImpl* instance =
369 HostGlobals::Get()->GetInstance(pp_instance());
370 if (!instance || !instance->container())
371 return;
373 // This PPB_Graphics3D_Impl could be deleted during the call to
374 // GetPluginInterface (which sends a sync message in some cases). We still
375 // send the Graphics3DContextLost to the plugin; the instance may care about
376 // that event even though this context has been destroyed.
377 PP_Instance this_pp_instance = pp_instance();
378 const PPP_Graphics3D* ppp_graphics_3d = static_cast<const PPP_Graphics3D*>(
379 instance->module()->GetPluginInterface(PPP_GRAPHICS_3D_INTERFACE));
380 // We have to check *again* that the instance exists, because it could have
381 // been deleted during GetPluginInterface(). Even the PluginModule could be
382 // deleted, but in that case, the instance should also be gone, so the
383 // GetInstance check covers both cases.
384 if (ppp_graphics_3d && HostGlobals::Get()->GetInstance(this_pp_instance))
385 ppp_graphics_3d->Graphics3DContextLost(this_pp_instance);
388 } // namespace content