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_graphics_2d_host.h" | 5 #include "content/renderer/pepper/pepper_graphics_2d_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "cc/resources/texture_mailbox.h" |
| 12 #include "content/public/renderer/render_thread.h" |
11 #include "content/public/renderer/renderer_ppapi_host.h" | 13 #include "content/public/renderer/renderer_ppapi_host.h" |
12 #include "content/renderer/pepper/common.h" | 14 #include "content/renderer/pepper/common.h" |
13 #include "content/renderer/pepper/gfx_conversion.h" | 15 #include "content/renderer/pepper/gfx_conversion.h" |
14 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" | 16 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
15 #include "content/renderer/pepper/ppb_image_data_impl.h" | 17 #include "content/renderer/pepper/ppb_image_data_impl.h" |
16 #include "ppapi/c/pp_bool.h" | 18 #include "ppapi/c/pp_bool.h" |
17 #include "ppapi/c/pp_errors.h" | 19 #include "ppapi/c/pp_errors.h" |
18 #include "ppapi/c/pp_rect.h" | 20 #include "ppapi/c/pp_rect.h" |
19 #include "ppapi/c/pp_resource.h" | 21 #include "ppapi/c/pp_resource.h" |
20 #include "ppapi/host/dispatch_host_message.h" | 22 #include "ppapi/host/dispatch_host_message.h" |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 PP_Instance instance, | 182 PP_Instance instance, |
181 PP_Resource resource) | 183 PP_Resource resource) |
182 : ResourceHost(host->GetPpapiHost(), instance, resource), | 184 : ResourceHost(host->GetPpapiHost(), instance, resource), |
183 renderer_ppapi_host_(host), | 185 renderer_ppapi_host_(host), |
184 bound_instance_(NULL), | 186 bound_instance_(NULL), |
185 need_flush_ack_(false), | 187 need_flush_ack_(false), |
186 offscreen_flush_pending_(false), | 188 offscreen_flush_pending_(false), |
187 is_always_opaque_(false), | 189 is_always_opaque_(false), |
188 scale_(1.0f), | 190 scale_(1.0f), |
189 weak_ptr_factory_(this), | 191 weak_ptr_factory_(this), |
190 is_running_in_process_(host->IsRunningInProcess()) { | 192 is_running_in_process_(host->IsRunningInProcess()), |
191 } | 193 texture_mailbox_modified_(true) {} |
192 | 194 |
193 PepperGraphics2DHost::~PepperGraphics2DHost() { | 195 PepperGraphics2DHost::~PepperGraphics2DHost() { |
194 // Unbind from the instance when destroyed if we're still bound. | 196 // Unbind from the instance when destroyed if we're still bound. |
195 if (bound_instance_) | 197 if (bound_instance_) |
196 bound_instance_->BindGraphics(bound_instance_->pp_instance(), 0); | 198 bound_instance_->BindGraphics(bound_instance_->pp_instance(), 0); |
197 } | 199 } |
198 | 200 |
199 bool PepperGraphics2DHost::Init(int width, int height, bool is_always_opaque) { | 201 bool PepperGraphics2DHost::Init(int width, int height, bool is_always_opaque) { |
200 // The underlying PPB_ImageData_Impl will validate the dimensions. | 202 // The underlying PPB_ImageData_Impl will validate the dimensions. |
201 image_data_ = new PPB_ImageData_Impl(pp_instance(), | 203 image_data_ = new PPB_ImageData_Impl(pp_instance(), |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 // When the device is detached, we'll not get any more paint callbacks so | 307 // When the device is detached, we'll not get any more paint callbacks so |
306 // we need to clear the list, but we still want to issue any pending | 308 // we need to clear the list, but we still want to issue any pending |
307 // callbacks to the plugin. | 309 // callbacks to the plugin. |
308 if (need_flush_ack_) | 310 if (need_flush_ack_) |
309 ScheduleOffscreenFlushAck(); | 311 ScheduleOffscreenFlushAck(); |
310 } else { | 312 } else { |
311 // Devices being replaced, redraw the plugin. | 313 // Devices being replaced, redraw the plugin. |
312 new_instance->InvalidateRect(gfx::Rect()); | 314 new_instance->InvalidateRect(gfx::Rect()); |
313 } | 315 } |
314 | 316 |
| 317 texture_mailbox_modified_ = true; |
| 318 |
315 bound_instance_ = new_instance; | 319 bound_instance_ = new_instance; |
316 return true; | 320 return true; |
317 } | 321 } |
318 | 322 |
319 // The |backing_bitmap| must be clipped to the |plugin_rect| to avoid painting | 323 // The |backing_bitmap| must be clipped to the |plugin_rect| to avoid painting |
320 // outside the plugin area. This can happen if the plugin has been resized since | 324 // outside the plugin area. This can happen if the plugin has been resized since |
321 // PaintImageData verified the image is within the plugin size. | 325 // PaintImageData verified the image is within the plugin size. |
322 void PepperGraphics2DHost::Paint(WebKit::WebCanvas* canvas, | 326 void PepperGraphics2DHost::Paint(WebKit::WebCanvas* canvas, |
323 const gfx::Rect& plugin_rect, | 327 const gfx::Rect& plugin_rect, |
324 const gfx::Rect& paint_rect) { | 328 const gfx::Rect& paint_rect) { |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 } | 548 } |
545 | 549 |
546 int32_t PepperGraphics2DHost::OnHostMsgReadImageData( | 550 int32_t PepperGraphics2DHost::OnHostMsgReadImageData( |
547 ppapi::host::HostMessageContext* context, | 551 ppapi::host::HostMessageContext* context, |
548 PP_Resource image, | 552 PP_Resource image, |
549 const PP_Point& top_left) { | 553 const PP_Point& top_left) { |
550 context->reply_msg = PpapiPluginMsg_Graphics2D_ReadImageDataAck(); | 554 context->reply_msg = PpapiPluginMsg_Graphics2D_ReadImageDataAck(); |
551 return ReadImageData(image, &top_left) ? PP_OK : PP_ERROR_FAILED; | 555 return ReadImageData(image, &top_left) ? PP_OK : PP_ERROR_FAILED; |
552 } | 556 } |
553 | 557 |
| 558 void ReleaseCallback(scoped_ptr<base::SharedMemory> memory, |
| 559 unsigned sync_point, |
| 560 bool lost_resource) {} |
| 561 |
| 562 bool PepperGraphics2DHost::PrepareTextureMailbox(cc::TextureMailbox* mailbox) { |
| 563 if (!texture_mailbox_modified_) |
| 564 return false; |
| 565 // TODO(jbauman): Send image_data_ through mailbox to avoid copy. |
| 566 gfx::Size pixel_image_size(image_data_->width(), image_data_->height()); |
| 567 int buffer_size = pixel_image_size.GetArea() * 4; |
| 568 scoped_ptr<base::SharedMemory> memory = |
| 569 RenderThread::Get()->HostAllocateSharedMemoryBuffer(buffer_size); |
| 570 if (!memory || !memory->Map(buffer_size)) |
| 571 return false; |
| 572 void* src = image_data_->Map(); |
| 573 memcpy(memory->memory(), src, buffer_size); |
| 574 image_data_->Unmap(); |
| 575 |
| 576 base::SharedMemory* mem = memory.get(); |
| 577 *mailbox = |
| 578 cc::TextureMailbox(mem, |
| 579 pixel_image_size, |
| 580 base::Bind(&ReleaseCallback, base::Passed(&memory))); |
| 581 texture_mailbox_modified_ = false; |
| 582 return true; |
| 583 } |
| 584 |
| 585 void PepperGraphics2DHost::AttachedToNewLayer() { |
| 586 texture_mailbox_modified_ = true; |
| 587 } |
| 588 |
554 int32_t PepperGraphics2DHost::Flush(PP_Resource* old_image_data) { | 589 int32_t PepperGraphics2DHost::Flush(PP_Resource* old_image_data) { |
555 bool done_replace_contents = false; | 590 bool done_replace_contents = false; |
556 bool no_update_visible = true; | 591 bool no_update_visible = true; |
557 bool is_plugin_visible = true; | 592 bool is_plugin_visible = true; |
558 for (size_t i = 0; i < queued_operations_.size(); i++) { | 593 for (size_t i = 0; i < queued_operations_.size(); i++) { |
559 QueuedOperation& operation = queued_operations_[i]; | 594 QueuedOperation& operation = queued_operations_[i]; |
560 gfx::Rect op_rect; | 595 gfx::Rect op_rect; |
561 switch (operation.type) { | 596 switch (operation.type) { |
562 case QueuedOperation::PAINT: | 597 case QueuedOperation::PAINT: |
563 ExecutePaintImageData(operation.paint_image.get(), | 598 ExecutePaintImageData(operation.paint_image.get(), |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 no_update_visible = false; | 644 no_update_visible = false; |
610 | 645 |
611 // Notify the plugin of the entire change (op_rect), even if it is | 646 // Notify the plugin of the entire change (op_rect), even if it is |
612 // partially or completely off-screen. | 647 // partially or completely off-screen. |
613 if (operation.type == QueuedOperation::SCROLL) { | 648 if (operation.type == QueuedOperation::SCROLL) { |
614 bound_instance_->ScrollRect(scroll_delta.x(), scroll_delta.y(), | 649 bound_instance_->ScrollRect(scroll_delta.x(), scroll_delta.y(), |
615 op_rect); | 650 op_rect); |
616 } else { | 651 } else { |
617 bound_instance_->InvalidateRect(op_rect); | 652 bound_instance_->InvalidateRect(op_rect); |
618 } | 653 } |
| 654 texture_mailbox_modified_ = true; |
619 } | 655 } |
620 } | 656 } |
621 queued_operations_.clear(); | 657 queued_operations_.clear(); |
622 | 658 |
623 if (!bound_instance_) { | 659 if (!bound_instance_) { |
624 // As promised in the API, we always schedule callback when unbound. | 660 // As promised in the API, we always schedule callback when unbound. |
625 ScheduleOffscreenFlushAck(); | 661 ScheduleOffscreenFlushAck(); |
626 } else if (no_update_visible && is_plugin_visible && | 662 } else if (no_update_visible && is_plugin_visible && |
627 bound_instance_->view_data().is_page_visible) { | 663 bound_instance_->view_data().is_page_visible) { |
628 // There's nothing visible to invalidate so just schedule the callback to | 664 // There's nothing visible to invalidate so just schedule the callback to |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
758 gfx::Point inverse_scaled_point = | 794 gfx::Point inverse_scaled_point = |
759 gfx::ToFlooredPoint(gfx::ScalePoint(*delta, inverse_scale)); | 795 gfx::ToFlooredPoint(gfx::ScalePoint(*delta, inverse_scale)); |
760 if (original_delta != inverse_scaled_point) | 796 if (original_delta != inverse_scaled_point) |
761 return false; | 797 return false; |
762 } | 798 } |
763 | 799 |
764 return true; | 800 return true; |
765 } | 801 } |
766 | 802 |
767 } // namespace content | 803 } // namespace content |
OLD | NEW |