mesa gn build: suppress -Wstring-conversion warnings
[chromium-blink-merge.git] / content / renderer / pepper / ppb_graphics_3d_impl.cc
blobe93f08e0e4c8be0d358042cddb46dcf49f0220c0
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/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
12 #include "content/common/gpu/client/gpu_channel_host.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/common/web_preferences.h"
15 #include "content/renderer/pepper/host_globals.h"
16 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
17 #include "content/renderer/pepper/pepper_plugin_instance_throttler.h"
18 #include "content/renderer/pepper/plugin_module.h"
19 #include "content/renderer/render_thread_impl.h"
20 #include "content/renderer/render_view_impl.h"
21 #include "gpu/command_buffer/client/gles2_implementation.h"
22 #include "ppapi/c/ppp_graphics_3d.h"
23 #include "ppapi/thunk/enter.h"
24 #include "third_party/WebKit/public/platform/WebString.h"
25 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
26 #include "third_party/WebKit/public/web/WebDocument.h"
27 #include "third_party/WebKit/public/web/WebElement.h"
28 #include "third_party/WebKit/public/web/WebLocalFrame.h"
29 #include "third_party/WebKit/public/web/WebPluginContainer.h"
31 using ppapi::thunk::EnterResourceNoLock;
32 using ppapi::thunk::PPB_Graphics3D_API;
33 using blink::WebConsoleMessage;
34 using blink::WebLocalFrame;
35 using blink::WebPluginContainer;
36 using blink::WebString;
38 namespace content {
40 namespace {
41 const int32 kCommandBufferSize = 1024 * 1024;
42 const int32 kTransferBufferSize = 1024 * 1024;
44 } // namespace.
46 PPB_Graphics3D_Impl::PPB_Graphics3D_Impl(PP_Instance instance)
47 : PPB_Graphics3D_Shared(instance),
48 bound_to_instance_(false),
49 commit_pending_(false),
50 sync_point_(0),
51 has_alpha_(false),
52 command_buffer_(NULL),
53 weak_ptr_factory_(this) {}
55 PPB_Graphics3D_Impl::~PPB_Graphics3D_Impl() {
56 DestroyGLES2Impl();
57 if (command_buffer_) {
58 DCHECK(channel_.get());
59 channel_->DestroyCommandBuffer(command_buffer_);
60 command_buffer_ = NULL;
63 channel_ = NULL;
66 // static
67 PP_Resource PPB_Graphics3D_Impl::Create(PP_Instance instance,
68 PP_Resource share_context,
69 const int32_t* attrib_list) {
70 PPB_Graphics3D_API* share_api = NULL;
71 if (share_context) {
72 EnterResourceNoLock<PPB_Graphics3D_API> enter(share_context, true);
73 if (enter.failed())
74 return 0;
75 share_api = enter.object();
77 scoped_refptr<PPB_Graphics3D_Impl> graphics_3d(
78 new PPB_Graphics3D_Impl(instance));
79 if (!graphics_3d->Init(share_api, attrib_list))
80 return 0;
81 return graphics_3d->GetReference();
84 // static
85 PP_Resource PPB_Graphics3D_Impl::CreateRaw(
86 PP_Instance instance,
87 PP_Resource share_context,
88 const int32_t* attrib_list,
89 gpu::Capabilities* capabilities,
90 base::SharedMemoryHandle* shared_state_handle) {
91 PPB_Graphics3D_API* share_api = NULL;
92 if (share_context) {
93 EnterResourceNoLock<PPB_Graphics3D_API> enter(share_context, true);
94 if (enter.failed())
95 return 0;
96 share_api = enter.object();
98 scoped_refptr<PPB_Graphics3D_Impl> graphics_3d(
99 new PPB_Graphics3D_Impl(instance));
100 if (!graphics_3d->InitRaw(share_api, attrib_list, capabilities,
101 shared_state_handle))
102 return 0;
103 return graphics_3d->GetReference();
106 PP_Bool PPB_Graphics3D_Impl::SetGetBuffer(int32_t transfer_buffer_id) {
107 GetCommandBuffer()->SetGetBuffer(transfer_buffer_id);
108 return PP_TRUE;
111 scoped_refptr<gpu::Buffer> PPB_Graphics3D_Impl::CreateTransferBuffer(
112 uint32_t size,
113 int32_t* id) {
114 return GetCommandBuffer()->CreateTransferBuffer(size, id);
117 PP_Bool PPB_Graphics3D_Impl::DestroyTransferBuffer(int32_t id) {
118 GetCommandBuffer()->DestroyTransferBuffer(id);
119 return PP_TRUE;
122 PP_Bool PPB_Graphics3D_Impl::Flush(int32_t put_offset) {
123 GetCommandBuffer()->Flush(put_offset);
124 return PP_TRUE;
127 gpu::CommandBuffer::State PPB_Graphics3D_Impl::WaitForTokenInRange(
128 int32_t start,
129 int32_t end) {
130 GetCommandBuffer()->WaitForTokenInRange(start, end);
131 return GetCommandBuffer()->GetLastState();
134 gpu::CommandBuffer::State PPB_Graphics3D_Impl::WaitForGetOffsetInRange(
135 int32_t start,
136 int32_t end) {
137 GetCommandBuffer()->WaitForGetOffsetInRange(start, end);
138 return GetCommandBuffer()->GetLastState();
141 uint32_t PPB_Graphics3D_Impl::InsertSyncPoint() {
142 return command_buffer_->InsertSyncPoint();
145 uint32_t PPB_Graphics3D_Impl::InsertFutureSyncPoint() {
146 return command_buffer_->InsertFutureSyncPoint();
149 void PPB_Graphics3D_Impl::RetireSyncPoint(uint32_t sync_point) {
150 return command_buffer_->RetireSyncPoint(sync_point);
153 bool PPB_Graphics3D_Impl::BindToInstance(bool bind) {
154 bound_to_instance_ = bind;
155 return true;
158 bool PPB_Graphics3D_Impl::IsOpaque() { return !has_alpha_; }
160 void PPB_Graphics3D_Impl::ViewInitiatedPaint() {
161 commit_pending_ = false;
163 if (HasPendingSwap())
164 SwapBuffersACK(PP_OK);
167 void PPB_Graphics3D_Impl::ViewFlushedPaint() {}
169 int PPB_Graphics3D_Impl::GetCommandBufferRouteId() {
170 DCHECK(command_buffer_);
171 return command_buffer_->GetRouteID();
174 gpu::CommandBuffer* PPB_Graphics3D_Impl::GetCommandBuffer() {
175 return command_buffer_;
178 gpu::GpuControl* PPB_Graphics3D_Impl::GetGpuControl() {
179 return command_buffer_;
182 int32 PPB_Graphics3D_Impl::DoSwapBuffers() {
183 DCHECK(command_buffer_);
184 // We do not have a GLES2 implementation when using an OOP proxy.
185 // The plugin-side proxy is responsible for adding the SwapBuffers command
186 // to the command buffer in that case.
187 if (gles2_impl())
188 gles2_impl()->SwapBuffers();
190 // Since the backing texture has been updated, a new sync point should be
191 // inserted.
192 sync_point_ = command_buffer_->InsertSyncPoint();
194 if (bound_to_instance_) {
195 // If we are bound to the instance, we need to ask the compositor
196 // to commit our backing texture so that the graphics appears on the page.
197 // When the backing texture will be committed we get notified via
198 // ViewFlushedPaint().
200 // Don't need to check for NULL from GetPluginInstance since when we're
201 // bound, we know our instance is valid.
202 HostGlobals::Get()->GetInstance(pp_instance())->CommitBackingTexture();
203 commit_pending_ = true;
204 } else {
205 // Wait for the command to complete on the GPU to allow for throttling.
206 command_buffer_->SignalSyncPoint(
207 sync_point_,
208 base::Bind(&PPB_Graphics3D_Impl::OnSwapBuffers,
209 weak_ptr_factory_.GetWeakPtr()));
212 return PP_OK_COMPLETIONPENDING;
215 bool PPB_Graphics3D_Impl::Init(PPB_Graphics3D_API* share_context,
216 const int32_t* attrib_list) {
217 if (!InitRaw(share_context, attrib_list, NULL, NULL))
218 return false;
220 gpu::gles2::GLES2Implementation* share_gles2 = NULL;
221 if (share_context) {
222 share_gles2 =
223 static_cast<PPB_Graphics3D_Shared*>(share_context)->gles2_impl();
226 return CreateGLES2Impl(kCommandBufferSize, kTransferBufferSize, share_gles2);
229 bool PPB_Graphics3D_Impl::InitRaw(
230 PPB_Graphics3D_API* share_context,
231 const int32_t* attrib_list,
232 gpu::Capabilities* capabilities,
233 base::SharedMemoryHandle* shared_state_handle) {
234 PepperPluginInstanceImpl* plugin_instance =
235 HostGlobals::Get()->GetInstance(pp_instance());
236 if (!plugin_instance)
237 return false;
239 const WebPreferences& prefs =
240 static_cast<RenderViewImpl*>(plugin_instance->GetRenderView())
241 ->webkit_preferences();
242 // 3D access might be disabled or blacklisted.
243 if (!prefs.pepper_3d_enabled)
244 return false;
246 // Force SW rendering for keyframe extraction to avoid pixel reads from VRAM.
247 PepperPluginInstanceThrottler* throttler = plugin_instance->throttler();
248 if (throttler && throttler->needs_representative_keyframe())
249 return false;
251 RenderThreadImpl* render_thread = RenderThreadImpl::current();
252 if (!render_thread)
253 return false;
255 channel_ = render_thread->EstablishGpuChannelSync(
256 CAUSE_FOR_GPU_LAUNCH_PEPPERPLATFORMCONTEXT3DIMPL_INITIALIZE);
257 if (!channel_.get())
258 return false;
260 gfx::Size surface_size;
261 std::vector<int32> attribs;
262 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
263 // TODO(alokp): Change GpuChannelHost::CreateOffscreenCommandBuffer()
264 // interface to accept width and height in the attrib_list so that
265 // we do not need to filter for width and height here.
266 if (attrib_list) {
267 for (const int32_t* attr = attrib_list; attr[0] != PP_GRAPHICS3DATTRIB_NONE;
268 attr += 2) {
269 switch (attr[0]) {
270 case PP_GRAPHICS3DATTRIB_WIDTH:
271 surface_size.set_width(attr[1]);
272 break;
273 case PP_GRAPHICS3DATTRIB_HEIGHT:
274 surface_size.set_height(attr[1]);
275 break;
276 case PP_GRAPHICS3DATTRIB_GPU_PREFERENCE:
277 gpu_preference =
278 (attr[1] == PP_GRAPHICS3DATTRIB_GPU_PREFERENCE_LOW_POWER)
279 ? gfx::PreferIntegratedGpu
280 : gfx::PreferDiscreteGpu;
281 break;
282 case PP_GRAPHICS3DATTRIB_ALPHA_SIZE:
283 has_alpha_ = attr[1] > 0;
284 // fall-through
285 default:
286 attribs.push_back(attr[0]);
287 attribs.push_back(attr[1]);
288 break;
291 attribs.push_back(PP_GRAPHICS3DATTRIB_NONE);
294 CommandBufferProxyImpl* share_buffer = NULL;
295 if (share_context) {
296 PPB_Graphics3D_Impl* share_graphics =
297 static_cast<PPB_Graphics3D_Impl*>(share_context);
298 share_buffer = share_graphics->command_buffer_;
301 command_buffer_ = channel_->CreateOffscreenCommandBuffer(
302 surface_size, share_buffer, attribs, GURL::EmptyGURL(), gpu_preference);
303 if (!command_buffer_)
304 return false;
305 if (!command_buffer_->Initialize())
306 return false;
307 if (shared_state_handle)
308 *shared_state_handle = command_buffer_->GetSharedStateHandle();
309 if (capabilities)
310 *capabilities = command_buffer_->GetCapabilities();
311 mailbox_ = gpu::Mailbox::Generate();
312 if (!command_buffer_->ProduceFrontBuffer(mailbox_))
313 return false;
314 sync_point_ = command_buffer_->InsertSyncPoint();
316 command_buffer_->SetChannelErrorCallback(base::Bind(
317 &PPB_Graphics3D_Impl::OnContextLost, weak_ptr_factory_.GetWeakPtr()));
319 command_buffer_->SetOnConsoleMessageCallback(base::Bind(
320 &PPB_Graphics3D_Impl::OnConsoleMessage, weak_ptr_factory_.GetWeakPtr()));
321 return true;
324 void PPB_Graphics3D_Impl::OnConsoleMessage(const std::string& message, int id) {
325 if (!bound_to_instance_)
326 return;
327 WebPluginContainer* container =
328 HostGlobals::Get()->GetInstance(pp_instance())->container();
329 if (!container)
330 return;
331 WebLocalFrame* frame = container->element().document().frame();
332 if (!frame)
333 return;
334 WebConsoleMessage console_message = WebConsoleMessage(
335 WebConsoleMessage::LevelError, WebString(base::UTF8ToUTF16(message)));
336 frame->addMessageToConsole(console_message);
339 void PPB_Graphics3D_Impl::OnSwapBuffers() {
340 if (HasPendingSwap()) {
341 // If we're off-screen, no need to trigger and wait for compositing.
342 // Just send the swap-buffers ACK to the plugin immediately.
343 commit_pending_ = false;
344 SwapBuffersACK(PP_OK);
348 void PPB_Graphics3D_Impl::OnContextLost() {
349 // Don't need to check for NULL from GetPluginInstance since when we're
350 // bound, we know our instance is valid.
351 if (bound_to_instance_) {
352 HostGlobals::Get()->GetInstance(pp_instance())->BindGraphics(pp_instance(),
356 // Send context lost to plugin. This may have been caused by a PPAPI call, so
357 // avoid re-entering.
358 base::MessageLoop::current()->PostTask(
359 FROM_HERE,
360 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