| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ppapi/proxy/ppb_graphics_2d_proxy.h" | 5 #include "ppapi/proxy/ppb_graphics_2d_proxy.h" |
| 6 | 6 |
| 7 #include <string.h> // For memset. | 7 #include <string.h> // For memset. |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "ppapi/c/pp_completion_callback.h" | 11 #include "ppapi/c/pp_completion_callback.h" |
| 12 #include "ppapi/c/pp_errors.h" | 12 #include "ppapi/c/pp_errors.h" |
| 13 #include "ppapi/c/pp_resource.h" | 13 #include "ppapi/c/pp_resource.h" |
| 14 #include "ppapi/c/ppb_graphics_2d.h" | 14 #include "ppapi/c/ppb_graphics_2d.h" |
| 15 #include "ppapi/proxy/plugin_dispatcher.h" | 15 #include "ppapi/proxy/plugin_dispatcher.h" |
| 16 #include "ppapi/proxy/plugin_resource.h" | 16 #include "ppapi/proxy/plugin_resource.h" |
| 17 #include "ppapi/proxy/ppapi_messages.h" | 17 #include "ppapi/proxy/ppapi_messages.h" |
| 18 #include "ppapi/thunk/enter.h" |
| 19 #include "ppapi/thunk/thunk.h" |
| 20 |
| 21 using ::ppapi::thunk::PPB_Graphics2D_API; |
| 22 using ::ppapi::thunk::EnterResource; |
| 18 | 23 |
| 19 namespace pp { | 24 namespace pp { |
| 20 namespace proxy { | 25 namespace proxy { |
| 21 | 26 |
| 22 class Graphics2D : public PluginResource { | |
| 23 public: | |
| 24 Graphics2D(const HostResource& host_resource, | |
| 25 const PP_Size& size, | |
| 26 PP_Bool is_always_opaque) | |
| 27 : PluginResource(host_resource), | |
| 28 size_(size), | |
| 29 is_always_opaque_(is_always_opaque), | |
| 30 current_flush_callback_(PP_BlockUntilComplete()) { | |
| 31 } | |
| 32 virtual ~Graphics2D() { | |
| 33 } | |
| 34 | |
| 35 // Resource overrides. | |
| 36 virtual Graphics2D* AsGraphics2D() { return this; } | |
| 37 | |
| 38 const PP_Size& size() const { return size_; } | |
| 39 PP_Bool is_always_opaque() const { return is_always_opaque_; } | |
| 40 | |
| 41 bool is_flush_pending() const { return !!current_flush_callback_.func; } | |
| 42 | |
| 43 PP_CompletionCallback current_flush_callback() const { | |
| 44 return current_flush_callback_; | |
| 45 } | |
| 46 void set_current_flush_callback(PP_CompletionCallback cb) { | |
| 47 current_flush_callback_ = cb; | |
| 48 } | |
| 49 | |
| 50 private: | |
| 51 PP_Size size_; | |
| 52 PP_Bool is_always_opaque_; | |
| 53 | |
| 54 // In the plugin, this is the current callback set for Flushes. When the | |
| 55 // callback function pointer is non-NULL, we're waiting for a flush ACK. | |
| 56 PP_CompletionCallback current_flush_callback_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(Graphics2D); | |
| 59 }; | |
| 60 | |
| 61 namespace { | 27 namespace { |
| 62 | 28 |
| 63 PP_Resource Create(PP_Instance instance, | |
| 64 const PP_Size* size, | |
| 65 PP_Bool is_always_opaque) { | |
| 66 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | |
| 67 if (!dispatcher) | |
| 68 return PP_ERROR_BADARGUMENT; | |
| 69 | |
| 70 HostResource result; | |
| 71 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Create( | |
| 72 INTERFACE_ID_PPB_GRAPHICS_2D, instance, *size, is_always_opaque, | |
| 73 &result)); | |
| 74 if (result.is_null()) | |
| 75 return 0; | |
| 76 linked_ptr<Graphics2D> graphics_2d(new Graphics2D(result, *size, | |
| 77 is_always_opaque)); | |
| 78 return PluginResourceTracker::GetInstance()->AddResource(graphics_2d); | |
| 79 } | |
| 80 | |
| 81 PP_Bool IsGraphics2D(PP_Resource resource) { | |
| 82 Graphics2D* object = PluginResource::GetAs<Graphics2D>(resource); | |
| 83 return BoolToPPBool(!!object); | |
| 84 } | |
| 85 | |
| 86 PP_Bool Describe(PP_Resource graphics_2d, | |
| 87 PP_Size* size, | |
| 88 PP_Bool* is_always_opaque) { | |
| 89 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); | |
| 90 if (!object) { | |
| 91 size->width = 0; | |
| 92 size->height = 0; | |
| 93 *is_always_opaque = PP_FALSE; | |
| 94 return PP_FALSE; | |
| 95 } | |
| 96 | |
| 97 *size = object->size(); | |
| 98 *is_always_opaque = object->is_always_opaque(); | |
| 99 return PP_TRUE; | |
| 100 } | |
| 101 | |
| 102 void PaintImageData(PP_Resource graphics_2d, | |
| 103 PP_Resource image_data, | |
| 104 const PP_Point* top_left, | |
| 105 const PP_Rect* src_rect) { | |
| 106 Graphics2D* graphics_object = PluginResource::GetAs<Graphics2D>(graphics_2d); | |
| 107 if (!graphics_object) | |
| 108 return; | |
| 109 PluginResource* image_object = PluginResourceTracker::GetInstance()-> | |
| 110 GetResourceObject(image_data); | |
| 111 if (!image_object) | |
| 112 return; | |
| 113 if (graphics_object->instance() != image_object->instance()) | |
| 114 return; | |
| 115 | |
| 116 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( | |
| 117 graphics_object->instance()); | |
| 118 if (!dispatcher) | |
| 119 return; | |
| 120 | |
| 121 PP_Rect dummy; | |
| 122 memset(&dummy, 0, sizeof(PP_Rect)); | |
| 123 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_PaintImageData( | |
| 124 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_object->host_resource(), | |
| 125 image_object->host_resource(), *top_left, !!src_rect, | |
| 126 src_rect ? *src_rect : dummy)); | |
| 127 } | |
| 128 | |
| 129 void Scroll(PP_Resource graphics_2d, | |
| 130 const PP_Rect* clip_rect, | |
| 131 const PP_Point* amount) { | |
| 132 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); | |
| 133 if (!object) | |
| 134 return; | |
| 135 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( | |
| 136 object->instance()); | |
| 137 if (!dispatcher) | |
| 138 return; | |
| 139 | |
| 140 PP_Rect dummy; | |
| 141 memset(&dummy, 0, sizeof(PP_Rect)); | |
| 142 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Scroll( | |
| 143 INTERFACE_ID_PPB_GRAPHICS_2D, object->host_resource(), | |
| 144 !!clip_rect, clip_rect ? *clip_rect : dummy, *amount)); | |
| 145 } | |
| 146 | |
| 147 void ReplaceContents(PP_Resource graphics_2d, PP_Resource image_data) { | |
| 148 Graphics2D* graphics_object = PluginResource::GetAs<Graphics2D>(graphics_2d); | |
| 149 if (!graphics_object) | |
| 150 return; | |
| 151 PluginResource* image_object = PluginResourceTracker::GetInstance()-> | |
| 152 GetResourceObject(image_data); | |
| 153 if (!image_object) | |
| 154 return; | |
| 155 if (graphics_object->instance() != image_object->instance()) | |
| 156 return; | |
| 157 | |
| 158 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( | |
| 159 graphics_object->instance()); | |
| 160 if (!dispatcher) | |
| 161 return; | |
| 162 | |
| 163 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_ReplaceContents( | |
| 164 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_object->host_resource(), | |
| 165 image_object->host_resource())); | |
| 166 } | |
| 167 | |
| 168 int32_t Flush(PP_Resource graphics_2d, | |
| 169 PP_CompletionCallback callback) { | |
| 170 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); | |
| 171 if (!object) | |
| 172 return PP_ERROR_BADRESOURCE; | |
| 173 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( | |
| 174 object->instance()); | |
| 175 if (!dispatcher) | |
| 176 return PP_ERROR_FAILED; | |
| 177 | |
| 178 // For now, disallow blocking calls. We'll need to add support for other | |
| 179 // threads to this later. | |
| 180 if (!callback.func) | |
| 181 return PP_ERROR_BADARGUMENT; | |
| 182 | |
| 183 if (object->is_flush_pending()) | |
| 184 return PP_ERROR_INPROGRESS; // Can't have >1 flush pending. | |
| 185 object->set_current_flush_callback(callback); | |
| 186 | |
| 187 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Flush( | |
| 188 INTERFACE_ID_PPB_GRAPHICS_2D, object->host_resource())); | |
| 189 return PP_OK_COMPLETIONPENDING; | |
| 190 } | |
| 191 | |
| 192 const PPB_Graphics2D graphics_2d_interface = { | |
| 193 &Create, | |
| 194 &IsGraphics2D, | |
| 195 &Describe, | |
| 196 &PaintImageData, | |
| 197 &Scroll, | |
| 198 &ReplaceContents, | |
| 199 &Flush | |
| 200 }; | |
| 201 | |
| 202 InterfaceProxy* CreateGraphics2DProxy(Dispatcher* dispatcher, | 29 InterfaceProxy* CreateGraphics2DProxy(Dispatcher* dispatcher, |
| 203 const void* target_interface) { | 30 const void* target_interface) { |
| 204 return new PPB_Graphics2D_Proxy(dispatcher, target_interface); | 31 return new PPB_Graphics2D_Proxy(dispatcher, target_interface); |
| 205 } | 32 } |
| 206 | 33 |
| 207 } // namespace | 34 } // namespace |
| 208 | 35 |
| 36 PPB_Graphics2D_API* Graphics2D::AsGraphics2D_API() { |
| 37 return this; |
| 38 } |
| 39 |
| 40 PP_Bool Graphics2D::Describe(PP_Size* size, PP_Bool* is_always_opaque) { |
| 41 *size = size_; |
| 42 *is_always_opaque = is_always_opaque_; |
| 43 return PP_TRUE; |
| 44 } |
| 45 |
| 46 void Graphics2D::PaintImageData(PP_Resource image_data, |
| 47 const PP_Point* top_left, |
| 48 const PP_Rect* src_rect) { |
| 49 PluginResource* image_object = PluginResourceTracker::GetInstance()-> |
| 50 GetResourceObject(image_data); |
| 51 //if (!image_object || instance() != image_object->instance()) |
| 52 // return; |
| 53 |
| 54 PP_Rect dummy; |
| 55 memset(&dummy, 0, sizeof(PP_Rect)); |
| 56 GetDispatcher()->Send(new PpapiHostMsg_PPBGraphics2D_PaintImageData( |
| 57 INTERFACE_ID_PPB_GRAPHICS_2D, host_resource(), |
| 58 image_object->host_resource(), *top_left, !!src_rect, |
| 59 src_rect ? *src_rect : dummy)); |
| 60 } |
| 61 |
| 62 void Graphics2D::Scroll(const PP_Rect* clip_rect, |
| 63 const PP_Point* amount) { |
| 64 PP_Rect dummy; |
| 65 memset(&dummy, 0, sizeof(PP_Rect)); |
| 66 GetDispatcher()->Send(new PpapiHostMsg_PPBGraphics2D_Scroll( |
| 67 INTERFACE_ID_PPB_GRAPHICS_2D, host_resource(), |
| 68 !!clip_rect, clip_rect ? *clip_rect : dummy, *amount)); |
| 69 } |
| 70 |
| 71 void Graphics2D::ReplaceContents(PP_Resource image_data) { |
| 72 PluginResource* image_object = PluginResourceTracker::GetInstance()-> |
| 73 GetResourceObject(image_data); |
| 74 if (!image_object || instance() != image_object->instance()) |
| 75 return; |
| 76 |
| 77 GetDispatcher()->Send(new PpapiHostMsg_PPBGraphics2D_ReplaceContents( |
| 78 INTERFACE_ID_PPB_GRAPHICS_2D, host_resource(), |
| 79 image_object->host_resource())); |
| 80 } |
| 81 |
| 82 int32_t Graphics2D::Flush(PP_CompletionCallback callback) { |
| 83 // For now, disallow blocking calls. We'll need to add support for other |
| 84 // threads to this later. |
| 85 if (!callback.func) |
| 86 return PP_ERROR_BADARGUMENT; |
| 87 |
| 88 if (is_flush_pending()) |
| 89 return PP_ERROR_INPROGRESS; // Can't have >1 flush pending. |
| 90 set_current_flush_callback(callback); |
| 91 |
| 92 GetDispatcher()->Send(new PpapiHostMsg_PPBGraphics2D_Flush( |
| 93 INTERFACE_ID_PPB_GRAPHICS_2D, host_resource())); |
| 94 return PP_OK_COMPLETIONPENDING; |
| 95 } |
| 96 |
| 209 PPB_Graphics2D_Proxy::PPB_Graphics2D_Proxy(Dispatcher* dispatcher, | 97 PPB_Graphics2D_Proxy::PPB_Graphics2D_Proxy(Dispatcher* dispatcher, |
| 210 const void* target_interface) | 98 const void* target_interface) |
| 211 : InterfaceProxy(dispatcher, target_interface), | 99 : InterfaceProxy(dispatcher, target_interface), |
| 212 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 100 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 213 } | 101 } |
| 214 | 102 |
| 215 PPB_Graphics2D_Proxy::~PPB_Graphics2D_Proxy() { | 103 PPB_Graphics2D_Proxy::~PPB_Graphics2D_Proxy() { |
| 216 } | 104 } |
| 217 | 105 |
| 218 // static | 106 // static |
| 219 const InterfaceProxy::Info* PPB_Graphics2D_Proxy::GetInfo() { | 107 const InterfaceProxy::Info* PPB_Graphics2D_Proxy::GetInfo() { |
| 220 static const Info info = { | 108 static const Info info = { |
| 221 &graphics_2d_interface, | 109 ::ppapi::thunk::GetPPB_Graphics2D_Thunk(), |
| 222 PPB_GRAPHICS_2D_INTERFACE, | 110 PPB_GRAPHICS_2D_INTERFACE, |
| 223 INTERFACE_ID_PPB_GRAPHICS_2D, | 111 INTERFACE_ID_PPB_GRAPHICS_2D, |
| 224 false, | 112 false, |
| 225 &CreateGraphics2DProxy, | 113 &CreateGraphics2DProxy, |
| 226 }; | 114 }; |
| 227 return &info; | 115 return &info; |
| 228 } | 116 } |
| 229 | 117 |
| 230 bool PPB_Graphics2D_Proxy::OnMessageReceived(const IPC::Message& msg) { | 118 bool PPB_Graphics2D_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 231 bool handled = true; | 119 bool handled = true; |
| 232 IPC_BEGIN_MESSAGE_MAP(PPB_Graphics2D_Proxy, msg) | 120 IPC_BEGIN_MESSAGE_MAP(PPB_Graphics2D_Proxy, msg) |
| 233 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_Create, | |
| 234 OnMsgCreate) | |
| 235 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_PaintImageData, | 121 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_PaintImageData, |
| 236 OnMsgPaintImageData) | 122 OnMsgPaintImageData) |
| 237 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_Scroll, | 123 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_Scroll, |
| 238 OnMsgScroll) | 124 OnMsgScroll) |
| 239 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_ReplaceContents, | 125 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_ReplaceContents, |
| 240 OnMsgReplaceContents) | 126 OnMsgReplaceContents) |
| 241 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_Flush, | 127 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_Flush, |
| 242 OnMsgFlush) | 128 OnMsgFlush) |
| 243 | 129 |
| 244 IPC_MESSAGE_HANDLER(PpapiMsg_PPBGraphics2D_FlushACK, | 130 IPC_MESSAGE_HANDLER(PpapiMsg_PPBGraphics2D_FlushACK, |
| 245 OnMsgFlushACK) | 131 OnMsgFlushACK) |
| 246 IPC_MESSAGE_UNHANDLED(handled = false) | 132 IPC_MESSAGE_UNHANDLED(handled = false) |
| 247 IPC_END_MESSAGE_MAP() | 133 IPC_END_MESSAGE_MAP() |
| 248 // FIXME(brettw) handle bad messages! | 134 // FIXME(brettw) handle bad messages! |
| 249 return handled; | 135 return handled; |
| 250 } | 136 } |
| 251 | 137 |
| 252 void PPB_Graphics2D_Proxy::OnMsgCreate(PP_Instance instance, | |
| 253 const PP_Size& size, | |
| 254 PP_Bool is_always_opaque, | |
| 255 HostResource* result) { | |
| 256 result->SetHostResource(instance, ppb_graphics_2d_target()->Create( | |
| 257 instance, &size, is_always_opaque)); | |
| 258 } | |
| 259 | |
| 260 void PPB_Graphics2D_Proxy::OnMsgPaintImageData( | 138 void PPB_Graphics2D_Proxy::OnMsgPaintImageData( |
| 261 const HostResource& graphics_2d, | 139 const HostResource& graphics_2d, |
| 262 const HostResource& image_data, | 140 const HostResource& image_data, |
| 263 const PP_Point& top_left, | 141 const PP_Point& top_left, |
| 264 bool src_rect_specified, | 142 bool src_rect_specified, |
| 265 const PP_Rect& src_rect) { | 143 const PP_Rect& src_rect) { |
| 266 ppb_graphics_2d_target()->PaintImageData( | 144 EnterResource<PPB_Graphics2D_API> enter(graphics_2d.host_resource(), false); |
| 267 graphics_2d.host_resource(), image_data.host_resource(), &top_left, | 145 if (enter.failed()) |
| 146 return; |
| 147 enter.object()->PaintImageData(image_data.host_resource(), &top_left, |
| 268 src_rect_specified ? &src_rect : NULL); | 148 src_rect_specified ? &src_rect : NULL); |
| 269 } | 149 } |
| 270 | 150 |
| 271 void PPB_Graphics2D_Proxy::OnMsgScroll(const HostResource& graphics_2d, | 151 void PPB_Graphics2D_Proxy::OnMsgScroll(const HostResource& graphics_2d, |
| 272 bool clip_specified, | 152 bool clip_specified, |
| 273 const PP_Rect& clip, | 153 const PP_Rect& clip, |
| 274 const PP_Point& amount) { | 154 const PP_Point& amount) { |
| 275 ppb_graphics_2d_target()->Scroll(graphics_2d.host_resource(), | 155 EnterResource<PPB_Graphics2D_API> enter(graphics_2d.host_resource(), false); |
| 276 clip_specified ? &clip : NULL, &amount); | 156 if (enter.failed()) |
| 157 return; |
| 158 enter.object()->Scroll(clip_specified ? &clip : NULL, &amount); |
| 277 } | 159 } |
| 278 | 160 |
| 279 void PPB_Graphics2D_Proxy::OnMsgReplaceContents( | 161 void PPB_Graphics2D_Proxy::OnMsgReplaceContents( |
| 280 const HostResource& graphics_2d, | 162 const HostResource& graphics_2d, |
| 281 const HostResource& image_data) { | 163 const HostResource& image_data) { |
| 282 ppb_graphics_2d_target()->ReplaceContents(graphics_2d.host_resource(), | 164 EnterResource<PPB_Graphics2D_API> enter(graphics_2d.host_resource(), false); |
| 283 image_data.host_resource()); | 165 if (enter.failed()) |
| 166 return; |
| 167 enter.object()->ReplaceContents(image_data.host_resource()); |
| 284 } | 168 } |
| 285 | 169 |
| 286 void PPB_Graphics2D_Proxy::OnMsgFlush(const HostResource& graphics_2d) { | 170 void PPB_Graphics2D_Proxy::OnMsgFlush(const HostResource& graphics_2d) { |
| 287 CompletionCallback callback = callback_factory_.NewCallback( | 171 CompletionCallback callback = callback_factory_.NewCallback( |
| 288 &PPB_Graphics2D_Proxy::SendFlushACKToPlugin, graphics_2d); | 172 &PPB_Graphics2D_Proxy::SendFlushACKToPlugin, graphics_2d); |
| 289 int32_t result = ppb_graphics_2d_target()->Flush( | 173 int32_t result = ppb_graphics_2d_target()->Flush( |
| 290 graphics_2d.host_resource(), callback.pp_completion_callback()); | 174 graphics_2d.host_resource(), callback.pp_completion_callback()); |
| 291 if (result != PP_OK_COMPLETIONPENDING) { | 175 if (result != PP_OK_COMPLETIONPENDING) { |
| 292 // There was some error, so we won't get a flush callback. We need to now | 176 // There was some error, so we won't get a flush callback. We need to now |
| 293 // issue the ACK to the plugin hears about the error. This will also clean | 177 // issue the ACK to the plugin hears about the error. This will also clean |
| (...skipping 26 matching lines...) Expand all Loading... |
| 320 | 204 |
| 321 void PPB_Graphics2D_Proxy::SendFlushACKToPlugin( | 205 void PPB_Graphics2D_Proxy::SendFlushACKToPlugin( |
| 322 int32_t result, | 206 int32_t result, |
| 323 const HostResource& graphics_2d) { | 207 const HostResource& graphics_2d) { |
| 324 dispatcher()->Send(new PpapiMsg_PPBGraphics2D_FlushACK( | 208 dispatcher()->Send(new PpapiMsg_PPBGraphics2D_FlushACK( |
| 325 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, result)); | 209 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, result)); |
| 326 } | 210 } |
| 327 | 211 |
| 328 } // namespace proxy | 212 } // namespace proxy |
| 329 } // namespace pp | 213 } // namespace pp |
| OLD | NEW |