Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/browser_plugin/browser_plugin_compositing_helper.h" | 5 #include "content/renderer/browser_plugin/browser_plugin_compositing_helper.h" |
| 6 | 6 |
| 7 #include "cc/texture_layer.h" | 7 #include "cc/texture_layer.h" |
| 8 #include "content/common/browser_plugin_messages.h" | 8 #include "content/common/browser_plugin_messages.h" |
| 9 #include "content/renderer/render_thread_impl.h" | 9 #include "content/renderer/render_thread_impl.h" |
| 10 #include "third_party/khronos/GLES2/gl2.h" | |
| 11 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3 D.h" | |
| 12 #include "third_party/WebKit/Source/Platform/chromium/public/WebSharedGraphicsCo ntext3D.h" | |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| 11 #include "webkit/compositor_bindings/web_layer_impl.h" | 14 #include "webkit/compositor_bindings/web_layer_impl.h" |
| 12 | 15 |
| 13 namespace content { | 16 namespace content { |
| 14 | 17 |
| 15 static void SendACK(const std::string& mailbox_name, | |
| 16 int host_route_id, | |
| 17 int gpu_route_id, | |
| 18 int gpu_host_id, | |
| 19 unsigned sync_point) { | |
| 20 RenderThread::Get()->Send( | |
| 21 new BrowserPluginHostMsg_BuffersSwappedACK( | |
| 22 host_route_id, | |
| 23 gpu_route_id, | |
| 24 gpu_host_id, | |
| 25 mailbox_name, | |
| 26 sync_point)); | |
| 27 } | |
| 28 | |
| 29 BrowserPluginCompositingHelper::BrowserPluginCompositingHelper( | 18 BrowserPluginCompositingHelper::BrowserPluginCompositingHelper( |
| 30 WebKit::WebPluginContainer* container, | 19 WebKit::WebPluginContainer* container, |
| 31 int host_routing_id) | 20 int host_routing_id) |
| 32 : host_routing_id_(host_routing_id), | 21 : host_routing_id_(host_routing_id), |
| 33 last_mailbox_valid_(false), | 22 last_mailbox_valid_(false), |
| 23 ack_pending_(true), | |
| 34 container_(container) { | 24 container_(container) { |
| 35 } | 25 } |
| 36 | 26 |
| 37 BrowserPluginCompositingHelper::~BrowserPluginCompositingHelper() { | 27 BrowserPluginCompositingHelper::~BrowserPluginCompositingHelper() { |
| 38 container_->setWebLayer(NULL); | |
| 39 } | 28 } |
| 40 | 29 |
| 41 void BrowserPluginCompositingHelper::EnableCompositing(bool enable) { | 30 void BrowserPluginCompositingHelper::EnableCompositing(bool enable) { |
| 42 if (enable && !texture_layer_) { | 31 if (enable && !texture_layer_) { |
| 43 texture_layer_ = cc::TextureLayer::createForMailbox(); | 32 texture_layer_ = cc::TextureLayer::createForMailbox(); |
| 44 web_layer_.reset(new WebKit::WebLayerImpl(texture_layer_)); | 33 web_layer_.reset(new WebKit::WebLayerImpl(texture_layer_)); |
| 45 } | 34 } |
| 46 | 35 |
| 47 container_->setWebLayer(enable ? web_layer_.get() : NULL); | 36 container_->setWebLayer(enable ? web_layer_.get() : NULL); |
| 48 } | 37 } |
| 49 | 38 |
| 39 void BrowserPluginCompositingHelper::FreeMailboxMemory( | |
| 40 const std::string& mailbox_name) { | |
| 41 WebKit::WebGraphicsContext3D *context = | |
| 42 WebKit::WebSharedGraphicsContext3D::mainThreadContext(); | |
| 43 DCHECK(context); | |
| 44 unsigned texture_id = context->createTexture(); | |
| 45 context->bindTexture(GL_TEXTURE_2D, texture_id); | |
| 46 context->consumeTextureCHROMIUM( | |
| 47 GL_TEXTURE_2D, | |
| 48 reinterpret_cast<const int8*>(mailbox_name.data())); | |
| 49 context->deleteTexture(texture_id); | |
| 50 } | |
| 51 | |
| 52 void BrowserPluginCompositingHelper::MailboxReleased( | |
| 53 const std::string& mailbox_name, | |
| 54 int gpu_route_id, | |
| 55 int gpu_host_id, | |
| 56 unsigned sync_point) { | |
| 57 if (!ack_pending_) { | |
| 58 FreeMailboxMemory(mailbox_name); | |
| 59 last_mailbox_valid_ = false; | |
| 60 return; | |
| 61 } | |
| 62 ack_pending_ = false; | |
| 63 RenderThread::Get()->Send( | |
|
Fady Samuel
2013/01/16 16:22:22
Please use BrowserPluginManager to send messages f
| |
| 64 new BrowserPluginHostMsg_BuffersSwappedACK( | |
| 65 host_routing_id_, | |
| 66 gpu_route_id, | |
| 67 gpu_host_id, | |
| 68 mailbox_name, | |
| 69 sync_point)); | |
| 70 } | |
| 71 | |
| 72 void BrowserPluginCompositingHelper::OnContainerDestroy() { | |
| 73 if (container_) | |
| 74 container_->setWebLayer(NULL); | |
| 75 container_ = NULL; | |
| 76 | |
| 77 texture_layer_ = NULL; | |
| 78 web_layer_.reset(); | |
| 79 } | |
| 80 | |
| 50 void BrowserPluginCompositingHelper::OnBuffersSwapped( | 81 void BrowserPluginCompositingHelper::OnBuffersSwapped( |
| 51 const gfx::Size& size, | 82 const gfx::Size& size, |
| 52 const std::string& mailbox_name, | 83 const std::string& mailbox_name, |
| 53 int gpu_route_id, | 84 int gpu_route_id, |
| 54 int gpu_host_id) { | 85 int gpu_host_id) { |
| 86 ack_pending_ = true; | |
| 87 // Browser plugin getting destroyed, do a fast ACK. | |
| 88 if (!texture_layer_) { | |
| 89 MailboxReleased(mailbox_name, gpu_route_id, gpu_host_id, 0); | |
| 90 return; | |
| 91 } | |
| 92 | |
| 55 if (buffer_size_ != size) { | 93 if (buffer_size_ != size) { |
| 56 buffer_size_ = size; | 94 buffer_size_ = size; |
| 57 UpdateUVRect(); | 95 UpdateUVRect(); |
| 58 } | 96 } |
| 59 if (!last_mailbox_valid_) | |
| 60 SendACK(std::string(), host_routing_id_, gpu_route_id, gpu_host_id, 0); | |
| 61 | 97 |
| 62 bool current_mailbox_valid = !mailbox_name.empty(); | 98 bool current_mailbox_valid = !mailbox_name.empty(); |
| 63 if (!current_mailbox_valid && !last_mailbox_valid_) | 99 if (!last_mailbox_valid_) { |
| 64 return; | 100 MailboxReleased(std::string(), gpu_route_id, gpu_host_id, 0); |
| 101 if (!current_mailbox_valid) | |
| 102 return; | |
| 103 } | |
| 65 | 104 |
| 66 cc::TextureLayer::MailboxCallback callback; | 105 cc::TextureLayer::MailboxCallback callback; |
| 67 if (current_mailbox_valid) { | 106 if (current_mailbox_valid) { |
| 68 callback = base::Bind(&SendACK, | 107 scoped_refptr<BrowserPluginCompositingHelper> helper(this); |
| 108 callback = base::Bind(&BrowserPluginCompositingHelper::MailboxReleased, | |
| 109 helper, | |
| 69 mailbox_name, | 110 mailbox_name, |
| 70 host_routing_id_, | |
| 71 gpu_route_id, | 111 gpu_route_id, |
| 72 gpu_host_id); | 112 gpu_host_id); |
| 73 } | 113 } |
| 114 | |
| 74 texture_layer_->setTextureMailbox(mailbox_name, callback); | 115 texture_layer_->setTextureMailbox(mailbox_name, callback); |
| 75 last_mailbox_valid_ = current_mailbox_valid; | 116 last_mailbox_valid_ = current_mailbox_valid; |
| 76 } | 117 } |
| 77 | 118 |
| 78 void BrowserPluginCompositingHelper::SetContainerSize(const gfx::Size& size) { | 119 void BrowserPluginCompositingHelper::SetContainerSize(const gfx::Size& size) { |
| 79 if (container_size_ == size) | 120 if (container_size_ == size) |
| 80 return; | 121 return; |
| 81 | 122 |
| 82 container_size_ = size; | 123 container_size_ = size; |
| 83 UpdateUVRect(); | 124 UpdateUVRect(); |
| 84 } | 125 } |
| 85 | 126 |
| 86 void BrowserPluginCompositingHelper::UpdateUVRect() { | 127 void BrowserPluginCompositingHelper::UpdateUVRect() { |
| 87 if (!texture_layer_) | 128 if (!texture_layer_) |
| 88 return; | 129 return; |
| 89 | 130 |
| 90 gfx::RectF uv_rect(0, 0, 1, 1); | 131 gfx::RectF uv_rect(0, 0, 1, 1); |
| 91 if (!buffer_size_.IsEmpty() && !container_size_.IsEmpty()) { | 132 if (!buffer_size_.IsEmpty() && !container_size_.IsEmpty()) { |
| 92 uv_rect.set_width(static_cast<float>(container_size_.width()) / | 133 uv_rect.set_width(static_cast<float>(container_size_.width()) / |
| 93 static_cast<float>(buffer_size_.width())); | 134 static_cast<float>(buffer_size_.width())); |
| 94 uv_rect.set_height(static_cast<float>(container_size_.height()) / | 135 uv_rect.set_height(static_cast<float>(container_size_.height()) / |
| 95 static_cast<float>(buffer_size_.height())); | 136 static_cast<float>(buffer_size_.height())); |
| 96 } | 137 } |
| 97 texture_layer_->setUVRect(uv_rect); | 138 texture_layer_->setUVRect(uv_rect); |
| 98 } | 139 } |
| 99 | 140 |
| 100 } // namespace content | 141 } // namespace content |
| OLD | NEW |