Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "content/renderer/pepper/pepper_graphics_2d_host.h" | |
| 6 | |
| 7 #include "content/public/renderer/renderer_ppapi_host.h" | |
| 8 #include "ppapi/host/dispatch_host_message.h" | |
| 9 #include "ppapi/host/host_message_context.h" | |
| 10 #include "ppapi/host/ppapi_host.h" | |
| 11 #include "ppapi/shared_impl/api_id.h" | |
| 12 #include "ppapi/thunk/enter.h" | |
| 13 #include "ppapi/proxy/ppapi_messages.h" | |
| 14 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
| 15 #include "webkit/plugins/ppapi/ppb_graphics_2d_impl.h" // TODO: merge to here | |
| 16 | |
| 17 namespace content { | |
| 18 | |
| 19 // static | |
| 20 PepperGraphics2DHost* PepperGraphics2DHost::Create(RendererPpapiHost* host, | |
| 21 PP_Instance instance, | |
| 22 PP_Resource resource, | |
| 23 const PP_Size& size, | |
| 24 PP_Bool is_always_opaque) { | |
| 25 PepperGraphics2DHost* resource_host = | |
| 26 new PepperGraphics2DHost(host, instance, resource); | |
| 27 if (!resource_host->graphics_2d_->Init(size.width, size.height, | |
| 28 is_always_opaque)) { | |
| 29 delete resource_host; | |
| 30 return NULL; | |
| 31 } | |
| 32 return resource_host; | |
| 33 } | |
| 34 | |
| 35 PepperGraphics2DHost::PepperGraphics2DHost(RendererPpapiHost* host, | |
| 36 PP_Instance instance, | |
| 37 PP_Resource resource) | |
| 38 : ResourceHost(host->GetPpapiHost(), instance, resource), | |
| 39 graphics_2d_(new webkit::ppapi::PPB_Graphics2D_Impl(instance)), | |
| 40 is_running_in_process_(host->IsRunningInProcess()) { | |
| 41 } | |
| 42 | |
| 43 PepperGraphics2DHost::~PepperGraphics2DHost() { | |
| 44 // Unbound from the instance when destoryed. | |
|
brettw
2012/10/19 21:44:13
Unbound -> Unbind
victorhsieh
2012/10/22 02:18:53
Done.
| |
| 45 PP_Instance instance = graphics_2d_->pp_instance(); | |
| 46 ppapi::thunk::EnterInstanceNoLock enter(instance); | |
| 47 if (enter.succeeded()) | |
| 48 enter.functions()->BindGraphics(instance, 0); | |
| 49 } | |
| 50 | |
| 51 int32_t PepperGraphics2DHost::OnResourceMessageReceived( | |
| 52 const IPC::Message& msg, | |
| 53 ppapi::host::HostMessageContext* context) { | |
| 54 IPC_BEGIN_MESSAGE_MAP(PepperGraphics2DHost, msg) | |
| 55 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
| 56 PpapiHostMsg_Graphics2D_PaintImageData, | |
| 57 OnHostMsgPaintImageData) | |
| 58 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
| 59 PpapiHostMsg_Graphics2D_Scroll, | |
| 60 OnHostMsgScroll) | |
| 61 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
| 62 PpapiHostMsg_Graphics2D_ReplaceContents, | |
| 63 OnHostMsgReplaceContents) | |
| 64 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( | |
| 65 PpapiHostMsg_Graphics2D_Flush, | |
| 66 OnHostMsgFlush) | |
| 67 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
| 68 PpapiHostMsg_Graphics2D_Dev_SetScale, | |
| 69 OnHostMsgSetScale) | |
| 70 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
| 71 PpapiHostMsg_Graphics2D_ReadImageData, | |
| 72 OnHostMsgReadImageData) | |
| 73 IPC_END_MESSAGE_MAP() | |
| 74 return PP_ERROR_FAILED; | |
| 75 } | |
| 76 | |
| 77 bool PepperGraphics2DHost::ReadImageData(PP_Resource image, | |
| 78 const PP_Point* top_left) { | |
| 79 return graphics_2d_->ReadImageData(image, top_left); | |
| 80 } | |
| 81 | |
| 82 bool PepperGraphics2DHost::BindToInstance( | |
| 83 webkit::ppapi::PluginInstance* new_instance) { | |
| 84 if (new_instance && | |
| 85 new_instance->pp_instance() != graphics_2d_->pp_instance()) | |
| 86 return false; // Can't bind other instance's contexts. | |
| 87 return graphics_2d_->BindToInstance(new_instance); | |
| 88 } | |
| 89 | |
| 90 void PepperGraphics2DHost::Paint(WebKit::WebCanvas* canvas, | |
| 91 const gfx::Rect& plugin_rect, | |
| 92 const gfx::Rect& paint_rect) { | |
| 93 graphics_2d_->Paint(canvas, plugin_rect, paint_rect); | |
| 94 } | |
| 95 | |
| 96 void PepperGraphics2DHost::ViewWillInitiatePaint() { | |
| 97 graphics_2d_->ViewWillInitiatePaint(); | |
| 98 } | |
| 99 | |
| 100 void PepperGraphics2DHost::ViewInitiatedPaint() { | |
| 101 graphics_2d_->ViewInitiatedPaint(); | |
| 102 } | |
| 103 | |
| 104 void PepperGraphics2DHost::ViewFlushedPaint() { | |
| 105 graphics_2d_->ViewFlushedPaint(); | |
| 106 } | |
| 107 | |
| 108 int32_t PepperGraphics2DHost::OnHostMsgPaintImageData( | |
| 109 ppapi::host::HostMessageContext* context, | |
| 110 const ppapi::HostResource& image_data, | |
| 111 const PP_Point& top_left, | |
| 112 bool src_rect_specified, | |
| 113 const PP_Rect& src_rect) { | |
| 114 graphics_2d_->PaintImageData(image_data.host_resource(), &top_left, | |
| 115 src_rect_specified ? &src_rect : NULL); | |
| 116 return PP_OK; | |
| 117 } | |
| 118 | |
| 119 int32_t PepperGraphics2DHost::OnHostMsgScroll( | |
| 120 ppapi::host::HostMessageContext* context, | |
| 121 bool clip_specified, | |
| 122 const PP_Rect& clip, | |
| 123 const PP_Point& amount) { | |
| 124 graphics_2d_->Scroll(clip_specified ? &clip : NULL, &amount); | |
| 125 return PP_OK; | |
| 126 } | |
| 127 | |
| 128 int32_t PepperGraphics2DHost::OnHostMsgReplaceContents( | |
| 129 ppapi::host::HostMessageContext* context, | |
| 130 const ppapi::HostResource& image_data) { | |
| 131 graphics_2d_->ReplaceContents(image_data.host_resource()); | |
| 132 return PP_OK; | |
| 133 } | |
| 134 | |
| 135 int32_t PepperGraphics2DHost::OnHostMsgFlush( | |
| 136 ppapi::host::HostMessageContext* context) { | |
| 137 PP_Resource old_image_data = 0; | |
| 138 flush_reply_context_ = context->MakeReplyMessageContext(); | |
| 139 // TODO: when merge PP_Graphics2D_Impl, we won't need this tracked callback. | |
| 140 scoped_refptr<ppapi::TrackedCallback> callback(new ppapi::TrackedCallback( | |
| 141 graphics_2d_.get(), | |
| 142 PP_MakeCompletionCallback(&PepperGraphics2DHost::SendFlushACKToPlugin, | |
| 143 AsWeakPtr()))); | |
| 144 if (is_running_in_process_) | |
| 145 return graphics_2d_->Flush(callback, NULL); | |
| 146 | |
| 147 // Reuse image data when running out of process. | |
| 148 int32_t result = graphics_2d_->Flush(callback, &old_image_data); | |
| 149 | |
| 150 if (old_image_data) { | |
| 151 // If the Graphics2D has an old image data it's not using any more, send | |
| 152 // it back to the plugin for possible re-use. See ppb_image_data_proxy.cc | |
| 153 // for a description how this process works. | |
| 154 ppapi::HostResource old_image_data_host_resource; | |
| 155 old_image_data_host_resource.SetHostResource(pp_instance(), | |
| 156 old_image_data); | |
| 157 host()->Send(new PpapiMsg_PPBImageData_NotifyUnusedImageData( | |
| 158 ppapi::API_ID_PPB_IMAGE_DATA, old_image_data_host_resource)); | |
| 159 } | |
| 160 | |
| 161 return result; | |
| 162 } | |
| 163 | |
| 164 int32_t PepperGraphics2DHost::OnHostMsgSetScale( | |
| 165 ppapi::host::HostMessageContext* context, | |
| 166 float scale) { | |
| 167 return graphics_2d_->SetScale(scale) ? PP_OK : PP_ERROR_BADARGUMENT; | |
| 168 } | |
| 169 | |
| 170 int32_t PepperGraphics2DHost::OnHostMsgReadImageData( | |
| 171 ppapi::host::HostMessageContext* context, | |
| 172 PP_Resource image, | |
| 173 const PP_Point& top_left) { | |
| 174 context->reply_msg = PpapiPluginMsg_Graphics2D_ReadImageDataAck(); | |
| 175 return ReadImageData(image, &top_left) ? PP_OK : PP_ERROR_FAILED; | |
| 176 } | |
| 177 | |
| 178 // static | |
| 179 void PepperGraphics2DHost::SendFlushACKToPlugin(void* data, | |
| 180 int32_t pp_error) { | |
| 181 if (!data || pp_error != PP_OK) | |
| 182 return; | |
| 183 PepperGraphics2DHost* self = (PepperGraphics2DHost*) data; | |
| 184 self->host()->SendReply(self->flush_reply_context_, | |
| 185 PpapiPluginMsg_Graphics2D_FlushAck()); | |
| 186 } | |
| 187 | |
| 188 } // namespace content | |
| OLD | NEW |