Add ICU message format support
[chromium-blink-merge.git] / content / renderer / pepper / ppb_graphics_3d_impl.cc
blobe9b88641dc365dfbd5588a1090f70b697b17ad1e
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 weak_ptr_factory_(this) {}
57 PPB_Graphics3D_Impl::~PPB_Graphics3D_Impl() {}
59 // static
60 PP_Resource PPB_Graphics3D_Impl::Create(PP_Instance instance,
61 PP_Resource share_context,
62 const int32_t* attrib_list) {
63 PPB_Graphics3D_API* share_api = NULL;
64 if (share_context) {
65 EnterResourceNoLock<PPB_Graphics3D_API> enter(share_context, true);
66 if (enter.failed())
67 return 0;
68 share_api = enter.object();
70 scoped_refptr<PPB_Graphics3D_Impl> graphics_3d(
71 new PPB_Graphics3D_Impl(instance));
72 if (!graphics_3d->Init(share_api, attrib_list))
73 return 0;
74 return graphics_3d->GetReference();
77 // static
78 PP_Resource PPB_Graphics3D_Impl::CreateRaw(
79 PP_Instance instance,
80 PP_Resource share_context,
81 const int32_t* attrib_list,
82 gpu::Capabilities* capabilities,
83 base::SharedMemoryHandle* shared_state_handle) {
84 PPB_Graphics3D_API* share_api = NULL;
85 if (share_context) {
86 EnterResourceNoLock<PPB_Graphics3D_API> enter(share_context, true);
87 if (enter.failed())
88 return 0;
89 share_api = enter.object();
91 scoped_refptr<PPB_Graphics3D_Impl> graphics_3d(
92 new PPB_Graphics3D_Impl(instance));
93 if (!graphics_3d->InitRaw(share_api, attrib_list, capabilities,
94 shared_state_handle))
95 return 0;
96 return graphics_3d->GetReference();
99 PP_Bool PPB_Graphics3D_Impl::SetGetBuffer(int32_t transfer_buffer_id) {
100 GetCommandBuffer()->SetGetBuffer(transfer_buffer_id);
101 return PP_TRUE;
104 scoped_refptr<gpu::Buffer> PPB_Graphics3D_Impl::CreateTransferBuffer(
105 uint32_t size,
106 int32_t* id) {
107 return GetCommandBuffer()->CreateTransferBuffer(size, id);
110 PP_Bool PPB_Graphics3D_Impl::DestroyTransferBuffer(int32_t id) {
111 GetCommandBuffer()->DestroyTransferBuffer(id);
112 return PP_TRUE;
115 PP_Bool PPB_Graphics3D_Impl::Flush(int32_t put_offset) {
116 GetCommandBuffer()->Flush(put_offset);
117 return PP_TRUE;
120 gpu::CommandBuffer::State PPB_Graphics3D_Impl::WaitForTokenInRange(
121 int32_t start,
122 int32_t end) {
123 GetCommandBuffer()->WaitForTokenInRange(start, end);
124 return GetCommandBuffer()->GetLastState();
127 gpu::CommandBuffer::State PPB_Graphics3D_Impl::WaitForGetOffsetInRange(
128 int32_t start,
129 int32_t end) {
130 GetCommandBuffer()->WaitForGetOffsetInRange(start, end);
131 return GetCommandBuffer()->GetLastState();
134 uint32_t PPB_Graphics3D_Impl::InsertSyncPoint() {
135 return command_buffer_->InsertSyncPoint();
138 uint32_t PPB_Graphics3D_Impl::InsertFutureSyncPoint() {
139 return command_buffer_->InsertFutureSyncPoint();
142 void PPB_Graphics3D_Impl::RetireSyncPoint(uint32_t sync_point) {
143 return command_buffer_->RetireSyncPoint(sync_point);
146 bool PPB_Graphics3D_Impl::BindToInstance(bool bind) {
147 bound_to_instance_ = bind;
148 return true;
151 bool PPB_Graphics3D_Impl::IsOpaque() { return !has_alpha_; }
153 void PPB_Graphics3D_Impl::ViewInitiatedPaint() {
154 commit_pending_ = false;
156 if (HasPendingSwap())
157 SwapBuffersACK(PP_OK);
160 CommandBufferProxyImpl* PPB_Graphics3D_Impl::GetCommandBufferProxy() {
161 DCHECK(command_buffer_);
162 return command_buffer_.get();
165 gpu::CommandBuffer* PPB_Graphics3D_Impl::GetCommandBuffer() {
166 return command_buffer_.get();
169 gpu::GpuControl* PPB_Graphics3D_Impl::GetGpuControl() {
170 return command_buffer_.get();
173 int32 PPB_Graphics3D_Impl::DoSwapBuffers() {
174 DCHECK(command_buffer_);
175 // We do not have a GLES2 implementation when using an OOP proxy.
176 // The plugin-side proxy is responsible for adding the SwapBuffers command
177 // to the command buffer in that case.
178 if (gles2_impl())
179 gles2_impl()->SwapBuffers();
181 // Since the backing texture has been updated, a new sync point should be
182 // inserted.
183 sync_point_ = command_buffer_->InsertSyncPoint();
185 if (bound_to_instance_) {
186 // If we are bound to the instance, we need to ask the compositor
187 // to commit our backing texture so that the graphics appears on the page.
188 // When the backing texture will be committed we get notified via
189 // ViewFlushedPaint().
191 // Don't need to check for NULL from GetPluginInstance since when we're
192 // bound, we know our instance is valid.
193 HostGlobals::Get()->GetInstance(pp_instance())->CommitBackingTexture();
194 commit_pending_ = true;
195 } else {
196 // Wait for the command to complete on the GPU to allow for throttling.
197 command_buffer_->SignalSyncPoint(
198 sync_point_,
199 base::Bind(&PPB_Graphics3D_Impl::OnSwapBuffers,
200 weak_ptr_factory_.GetWeakPtr()));
203 return PP_OK_COMPLETIONPENDING;
206 bool PPB_Graphics3D_Impl::Init(PPB_Graphics3D_API* share_context,
207 const int32_t* attrib_list) {
208 if (!InitRaw(share_context, attrib_list, NULL, NULL))
209 return false;
211 gpu::gles2::GLES2Implementation* share_gles2 = NULL;
212 if (share_context) {
213 share_gles2 =
214 static_cast<PPB_Graphics3D_Shared*>(share_context)->gles2_impl();
217 return CreateGLES2Impl(kCommandBufferSize, kTransferBufferSize, share_gles2);
220 bool PPB_Graphics3D_Impl::InitRaw(
221 PPB_Graphics3D_API* share_context,
222 const int32_t* attrib_list,
223 gpu::Capabilities* capabilities,
224 base::SharedMemoryHandle* shared_state_handle) {
225 PepperPluginInstanceImpl* plugin_instance =
226 HostGlobals::Get()->GetInstance(pp_instance());
227 if (!plugin_instance)
228 return false;
230 const WebPreferences& prefs =
231 static_cast<RenderViewImpl*>(plugin_instance->GetRenderView())
232 ->webkit_preferences();
233 // 3D access might be disabled or blacklisted.
234 if (!prefs.pepper_3d_enabled)
235 return false;
237 // Force SW rendering for keyframe extraction to avoid pixel reads from VRAM.
238 PluginInstanceThrottlerImpl* throttler = plugin_instance->throttler();
239 if (throttler && throttler->needs_representative_keyframe())
240 return false;
242 RenderThreadImpl* render_thread = RenderThreadImpl::current();
243 if (!render_thread)
244 return false;
246 channel_ = render_thread->EstablishGpuChannelSync(
247 CAUSE_FOR_GPU_LAUNCH_PEPPERPLATFORMCONTEXT3DIMPL_INITIALIZE);
248 if (!channel_.get())
249 return false;
251 gfx::Size surface_size;
252 std::vector<int32> attribs;
253 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
254 // TODO(alokp): Change GpuChannelHost::CreateOffscreenCommandBuffer()
255 // interface to accept width and height in the attrib_list so that
256 // we do not need to filter for width and height here.
257 if (attrib_list) {
258 for (const int32_t* attr = attrib_list; attr[0] != PP_GRAPHICS3DATTRIB_NONE;
259 attr += 2) {
260 switch (attr[0]) {
261 case PP_GRAPHICS3DATTRIB_WIDTH:
262 surface_size.set_width(attr[1]);
263 break;
264 case PP_GRAPHICS3DATTRIB_HEIGHT:
265 surface_size.set_height(attr[1]);
266 break;
267 case PP_GRAPHICS3DATTRIB_GPU_PREFERENCE:
268 gpu_preference =
269 (attr[1] == PP_GRAPHICS3DATTRIB_GPU_PREFERENCE_LOW_POWER)
270 ? gfx::PreferIntegratedGpu
271 : gfx::PreferDiscreteGpu;
272 break;
273 case PP_GRAPHICS3DATTRIB_ALPHA_SIZE:
274 has_alpha_ = attr[1] > 0;
275 // fall-through
276 default:
277 attribs.push_back(attr[0]);
278 attribs.push_back(attr[1]);
279 break;
282 attribs.push_back(PP_GRAPHICS3DATTRIB_NONE);
285 CommandBufferProxyImpl* share_buffer = NULL;
286 if (share_context) {
287 PPB_Graphics3D_Impl* share_graphics =
288 static_cast<PPB_Graphics3D_Impl*>(share_context);
289 share_buffer = share_graphics->GetCommandBufferProxy();
292 command_buffer_ = channel_->CreateOffscreenCommandBuffer(
293 surface_size, share_buffer, attribs, GURL::EmptyGURL(), gpu_preference);
294 if (!command_buffer_)
295 return false;
296 if (!command_buffer_->Initialize())
297 return false;
298 if (shared_state_handle)
299 *shared_state_handle = command_buffer_->GetSharedStateHandle();
300 if (capabilities)
301 *capabilities = command_buffer_->GetCapabilities();
302 mailbox_ = gpu::Mailbox::Generate();
303 if (!command_buffer_->ProduceFrontBuffer(mailbox_))
304 return false;
305 sync_point_ = command_buffer_->InsertSyncPoint();
307 command_buffer_->SetContextLostCallback(base::Bind(
308 &PPB_Graphics3D_Impl::OnContextLost, weak_ptr_factory_.GetWeakPtr()));
310 command_buffer_->SetOnConsoleMessageCallback(base::Bind(
311 &PPB_Graphics3D_Impl::OnConsoleMessage, weak_ptr_factory_.GetWeakPtr()));
312 return true;
315 void PPB_Graphics3D_Impl::OnConsoleMessage(const std::string& message, int id) {
316 if (!bound_to_instance_)
317 return;
318 WebPluginContainer* container =
319 HostGlobals::Get()->GetInstance(pp_instance())->container();
320 if (!container)
321 return;
322 WebLocalFrame* frame = container->element().document().frame();
323 if (!frame)
324 return;
325 WebConsoleMessage console_message = WebConsoleMessage(
326 WebConsoleMessage::LevelError, WebString(base::UTF8ToUTF16(message)));
327 frame->addMessageToConsole(console_message);
330 void PPB_Graphics3D_Impl::OnSwapBuffers() {
331 if (HasPendingSwap()) {
332 // If we're off-screen, no need to trigger and wait for compositing.
333 // Just send the swap-buffers ACK to the plugin immediately.
334 commit_pending_ = false;
335 SwapBuffersACK(PP_OK);
339 void PPB_Graphics3D_Impl::OnContextLost() {
340 // Don't need to check for NULL from GetPluginInstance since when we're
341 // bound, we know our instance is valid.
342 if (bound_to_instance_) {
343 HostGlobals::Get()->GetInstance(pp_instance())->BindGraphics(pp_instance(),
347 // Send context lost to plugin. This may have been caused by a PPAPI call, so
348 // avoid re-entering.
349 base::ThreadTaskRunnerHandle::Get()->PostTask(
350 FROM_HERE, base::Bind(&PPB_Graphics3D_Impl::SendContextLost,
351 weak_ptr_factory_.GetWeakPtr()));
354 void PPB_Graphics3D_Impl::SendContextLost() {
355 // By the time we run this, the instance may have been deleted, or in the
356 // process of being deleted. Even in the latter case, we don't want to send a
357 // callback after DidDestroy.
358 PepperPluginInstanceImpl* instance =
359 HostGlobals::Get()->GetInstance(pp_instance());
360 if (!instance || !instance->container())
361 return;
363 // This PPB_Graphics3D_Impl could be deleted during the call to
364 // GetPluginInterface (which sends a sync message in some cases). We still
365 // send the Graphics3DContextLost to the plugin; the instance may care about
366 // that event even though this context has been destroyed.
367 PP_Instance this_pp_instance = pp_instance();
368 const PPP_Graphics3D* ppp_graphics_3d = static_cast<const PPP_Graphics3D*>(
369 instance->module()->GetPluginInterface(PPP_GRAPHICS_3D_INTERFACE));
370 // We have to check *again* that the instance exists, because it could have
371 // been deleted during GetPluginInterface(). Even the PluginModule could be
372 // deleted, but in that case, the instance should also be gone, so the
373 // GetInstance check covers both cases.
374 if (ppp_graphics_3d && HostGlobals::Get()->GetInstance(this_pp_instance))
375 ppp_graphics_3d->Graphics3DContextLost(this_pp_instance);
378 } // namespace content