OLD | NEW |
1 // Copyright (c) 2010 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 | 18 |
19 namespace pp { | 19 namespace pp { |
20 namespace proxy { | 20 namespace proxy { |
21 | 21 |
22 class Graphics2D : public PluginResource { | 22 class Graphics2D : public PluginResource { |
23 public: | 23 public: |
24 Graphics2D(PP_Instance instance, | 24 Graphics2D(const HostResource& host_resource, |
25 const PP_Size& size, | 25 const PP_Size& size, |
26 PP_Bool is_always_opaque) | 26 PP_Bool is_always_opaque) |
27 : PluginResource(instance), | 27 : PluginResource(host_resource), |
28 size_(size), | 28 size_(size), |
29 is_always_opaque_(is_always_opaque), | 29 is_always_opaque_(is_always_opaque), |
30 current_flush_callback_(PP_BlockUntilComplete()) { | 30 current_flush_callback_(PP_BlockUntilComplete()) { |
31 } | 31 } |
| 32 virtual ~Graphics2D() { |
| 33 } |
32 | 34 |
33 // Resource overrides. | 35 // Resource overrides. |
34 virtual Graphics2D* AsGraphics2D() { return this; } | 36 virtual Graphics2D* AsGraphics2D() { return this; } |
35 | 37 |
36 const PP_Size& size() const { return size_; } | 38 const PP_Size& size() const { return size_; } |
37 PP_Bool is_always_opaque() const { return is_always_opaque_; } | 39 PP_Bool is_always_opaque() const { return is_always_opaque_; } |
38 | 40 |
39 bool is_flush_pending() const { return !!current_flush_callback_.func; } | 41 bool is_flush_pending() const { return !!current_flush_callback_.func; } |
40 | 42 |
41 PP_CompletionCallback current_flush_callback() const { | 43 PP_CompletionCallback current_flush_callback() const { |
(...skipping 16 matching lines...) Expand all Loading... |
58 | 60 |
59 namespace { | 61 namespace { |
60 | 62 |
61 PP_Resource Create(PP_Instance instance, | 63 PP_Resource Create(PP_Instance instance, |
62 const PP_Size* size, | 64 const PP_Size* size, |
63 PP_Bool is_always_opaque) { | 65 PP_Bool is_always_opaque) { |
64 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | 66 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
65 if (!dispatcher) | 67 if (!dispatcher) |
66 return PP_ERROR_BADARGUMENT; | 68 return PP_ERROR_BADARGUMENT; |
67 | 69 |
68 PP_Resource result = 0; | 70 HostResource result; |
69 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Create( | 71 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Create( |
70 INTERFACE_ID_PPB_GRAPHICS_2D, instance, *size, is_always_opaque, | 72 INTERFACE_ID_PPB_GRAPHICS_2D, instance, *size, is_always_opaque, |
71 &result)); | 73 &result)); |
72 if (result) { | 74 if (result.is_null()) |
73 linked_ptr<Graphics2D> graphics_2d(new Graphics2D(instance, *size, | 75 return 0; |
74 is_always_opaque)); | 76 linked_ptr<Graphics2D> graphics_2d(new Graphics2D(result, *size, |
75 PluginResourceTracker::GetInstance()->AddResource(result, graphics_2d); | 77 is_always_opaque)); |
76 } | 78 return PluginResourceTracker::GetInstance()->AddResource(graphics_2d); |
77 return result; | |
78 } | 79 } |
79 | 80 |
80 PP_Bool IsGraphics2D(PP_Resource resource) { | 81 PP_Bool IsGraphics2D(PP_Resource resource) { |
81 Graphics2D* object = PluginResource::GetAs<Graphics2D>(resource); | 82 Graphics2D* object = PluginResource::GetAs<Graphics2D>(resource); |
82 return BoolToPPBool(!!object); | 83 return BoolToPPBool(!!object); |
83 } | 84 } |
84 | 85 |
85 PP_Bool Describe(PP_Resource graphics_2d, | 86 PP_Bool Describe(PP_Resource graphics_2d, |
86 PP_Size* size, | 87 PP_Size* size, |
87 PP_Bool* is_always_opaque) { | 88 PP_Bool* is_always_opaque) { |
88 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); | 89 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); |
89 if (!object) { | 90 if (!object) { |
90 size->width = 0; | 91 size->width = 0; |
91 size->height = 0; | 92 size->height = 0; |
92 *is_always_opaque = PP_FALSE; | 93 *is_always_opaque = PP_FALSE; |
93 return PP_FALSE; | 94 return PP_FALSE; |
94 } | 95 } |
95 | 96 |
96 *size = object->size(); | 97 *size = object->size(); |
97 *is_always_opaque = object->is_always_opaque(); | 98 *is_always_opaque = object->is_always_opaque(); |
98 return PP_TRUE; | 99 return PP_TRUE; |
99 } | 100 } |
100 | 101 |
101 void PaintImageData(PP_Resource graphics_2d, | 102 void PaintImageData(PP_Resource graphics_2d, |
102 PP_Resource image_data, | 103 PP_Resource image_data, |
103 const PP_Point* top_left, | 104 const PP_Point* top_left, |
104 const PP_Rect* src_rect) { | 105 const PP_Rect* src_rect) { |
105 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); | 106 Graphics2D* graphics_object = PluginResource::GetAs<Graphics2D>(graphics_2d); |
106 if (!object) | 107 if (!graphics_object) |
107 return; | 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 |
108 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( | 116 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( |
109 object->instance()); | 117 graphics_object->instance()); |
110 if (!dispatcher) | 118 if (!dispatcher) |
111 return; | 119 return; |
112 | 120 |
113 PP_Rect dummy; | 121 PP_Rect dummy; |
114 memset(&dummy, 0, sizeof(PP_Rect)); | 122 memset(&dummy, 0, sizeof(PP_Rect)); |
115 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_PaintImageData( | 123 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_PaintImageData( |
116 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, image_data, *top_left, | 124 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_object->host_resource(), |
117 !!src_rect, src_rect ? *src_rect : dummy)); | 125 image_object->host_resource(), *top_left, !!src_rect, |
| 126 src_rect ? *src_rect : dummy)); |
118 } | 127 } |
119 | 128 |
120 void Scroll(PP_Resource graphics_2d, | 129 void Scroll(PP_Resource graphics_2d, |
121 const PP_Rect* clip_rect, | 130 const PP_Rect* clip_rect, |
122 const PP_Point* amount) { | 131 const PP_Point* amount) { |
123 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); | 132 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); |
124 if (!object) | 133 if (!object) |
125 return; | 134 return; |
126 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( | 135 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( |
127 object->instance()); | 136 object->instance()); |
128 if (!dispatcher) | 137 if (!dispatcher) |
129 return; | 138 return; |
130 | 139 |
131 PP_Rect dummy; | 140 PP_Rect dummy; |
132 memset(&dummy, 0, sizeof(PP_Rect)); | 141 memset(&dummy, 0, sizeof(PP_Rect)); |
133 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Scroll( | 142 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Scroll( |
134 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, !!clip_rect, | 143 INTERFACE_ID_PPB_GRAPHICS_2D, object->host_resource(), |
135 clip_rect ? *clip_rect : dummy, *amount)); | 144 !!clip_rect, clip_rect ? *clip_rect : dummy, *amount)); |
136 } | 145 } |
137 | 146 |
138 void ReplaceContents(PP_Resource graphics_2d, PP_Resource image_data) { | 147 void ReplaceContents(PP_Resource graphics_2d, PP_Resource image_data) { |
139 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); | 148 Graphics2D* graphics_object = PluginResource::GetAs<Graphics2D>(graphics_2d); |
140 if (!object) | 149 if (!graphics_object) |
141 return; | 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 |
142 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( | 158 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( |
143 object->instance()); | 159 graphics_object->instance()); |
144 if (!dispatcher) | 160 if (!dispatcher) |
145 return; | 161 return; |
146 | 162 |
147 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_ReplaceContents( | 163 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_ReplaceContents( |
148 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, image_data)); | 164 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_object->host_resource(), |
| 165 image_object->host_resource())); |
149 } | 166 } |
150 | 167 |
151 int32_t Flush(PP_Resource graphics_2d, | 168 int32_t Flush(PP_Resource graphics_2d, |
152 PP_CompletionCallback callback) { | 169 PP_CompletionCallback callback) { |
153 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); | 170 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); |
154 if (!object) | 171 if (!object) |
155 return PP_ERROR_BADRESOURCE; | 172 return PP_ERROR_BADRESOURCE; |
156 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( | 173 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( |
157 object->instance()); | 174 object->instance()); |
158 if (!dispatcher) | 175 if (!dispatcher) |
159 return PP_ERROR_FAILED; | 176 return PP_ERROR_FAILED; |
160 | 177 |
161 // For now, disallow blocking calls. We'll need to add support for other | 178 // For now, disallow blocking calls. We'll need to add support for other |
162 // threads to this later. | 179 // threads to this later. |
163 if (!callback.func) | 180 if (!callback.func) |
164 return PP_ERROR_BADARGUMENT; | 181 return PP_ERROR_BADARGUMENT; |
165 | 182 |
166 if (object->is_flush_pending()) | 183 if (object->is_flush_pending()) |
167 return PP_ERROR_INPROGRESS; // Can't have >1 flush pending. | 184 return PP_ERROR_INPROGRESS; // Can't have >1 flush pending. |
168 object->set_current_flush_callback(callback); | 185 object->set_current_flush_callback(callback); |
169 | 186 |
170 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Flush( | 187 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Flush( |
171 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d)); | 188 INTERFACE_ID_PPB_GRAPHICS_2D, object->host_resource())); |
172 return PP_ERROR_WOULDBLOCK; | 189 return PP_ERROR_WOULDBLOCK; |
173 } | 190 } |
174 | 191 |
175 const PPB_Graphics2D ppb_graphics_2d = { | 192 const PPB_Graphics2D ppb_graphics_2d = { |
176 &Create, | 193 &Create, |
177 &IsGraphics2D, | 194 &IsGraphics2D, |
178 &Describe, | 195 &Describe, |
179 &PaintImageData, | 196 &PaintImageData, |
180 &Scroll, | 197 &Scroll, |
181 &ReplaceContents, | 198 &ReplaceContents, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 OnMsgFlushACK) | 236 OnMsgFlushACK) |
220 IPC_MESSAGE_UNHANDLED(handled = false) | 237 IPC_MESSAGE_UNHANDLED(handled = false) |
221 IPC_END_MESSAGE_MAP() | 238 IPC_END_MESSAGE_MAP() |
222 // FIXME(brettw) handle bad messages! | 239 // FIXME(brettw) handle bad messages! |
223 return handled; | 240 return handled; |
224 } | 241 } |
225 | 242 |
226 void PPB_Graphics2D_Proxy::OnMsgCreate(PP_Instance instance, | 243 void PPB_Graphics2D_Proxy::OnMsgCreate(PP_Instance instance, |
227 const PP_Size& size, | 244 const PP_Size& size, |
228 PP_Bool is_always_opaque, | 245 PP_Bool is_always_opaque, |
229 PP_Resource* result) { | 246 HostResource* result) { |
230 *result = ppb_graphics_2d_target()->Create( | 247 result->SetHostResource(instance, ppb_graphics_2d_target()->Create( |
231 instance, &size, is_always_opaque); | 248 instance, &size, is_always_opaque)); |
232 } | 249 } |
233 | 250 |
234 void PPB_Graphics2D_Proxy::OnMsgPaintImageData(PP_Resource graphics_2d, | 251 void PPB_Graphics2D_Proxy::OnMsgPaintImageData( |
235 PP_Resource image_data, | 252 const HostResource& graphics_2d, |
236 const PP_Point& top_left, | 253 const HostResource& image_data, |
237 bool src_rect_specified, | 254 const PP_Point& top_left, |
238 const PP_Rect& src_rect) { | 255 bool src_rect_specified, |
| 256 const PP_Rect& src_rect) { |
239 ppb_graphics_2d_target()->PaintImageData( | 257 ppb_graphics_2d_target()->PaintImageData( |
240 graphics_2d, image_data, &top_left, | 258 graphics_2d.host_resource(), image_data.host_resource(), &top_left, |
241 src_rect_specified ? &src_rect : NULL); | 259 src_rect_specified ? &src_rect : NULL); |
242 } | 260 } |
243 | 261 |
244 void PPB_Graphics2D_Proxy::OnMsgScroll(PP_Resource graphics_2d, | 262 void PPB_Graphics2D_Proxy::OnMsgScroll(const HostResource& graphics_2d, |
245 bool clip_specified, | 263 bool clip_specified, |
246 const PP_Rect& clip, | 264 const PP_Rect& clip, |
247 const PP_Point& amount) { | 265 const PP_Point& amount) { |
248 ppb_graphics_2d_target()->Scroll( | 266 ppb_graphics_2d_target()->Scroll(graphics_2d.host_resource(), |
249 graphics_2d, | 267 clip_specified ? &clip : NULL, &amount); |
250 clip_specified ? &clip : NULL, &amount); | |
251 } | 268 } |
252 | 269 |
253 void PPB_Graphics2D_Proxy::OnMsgReplaceContents(PP_Resource graphics_2d, | 270 void PPB_Graphics2D_Proxy::OnMsgReplaceContents( |
254 PP_Resource image_data) { | 271 const HostResource& graphics_2d, |
255 ppb_graphics_2d_target()->ReplaceContents(graphics_2d, image_data); | 272 const HostResource& image_data) { |
| 273 ppb_graphics_2d_target()->ReplaceContents(graphics_2d.host_resource(), |
| 274 image_data.host_resource()); |
256 } | 275 } |
257 | 276 |
258 void PPB_Graphics2D_Proxy::OnMsgFlush(PP_Resource graphics_2d) { | 277 void PPB_Graphics2D_Proxy::OnMsgFlush(const HostResource& graphics_2d) { |
259 CompletionCallback callback = callback_factory_.NewCallback( | 278 CompletionCallback callback = callback_factory_.NewCallback( |
260 &PPB_Graphics2D_Proxy::SendFlushACKToPlugin, graphics_2d); | 279 &PPB_Graphics2D_Proxy::SendFlushACKToPlugin, graphics_2d); |
261 int32_t result = ppb_graphics_2d_target()->Flush( | 280 int32_t result = ppb_graphics_2d_target()->Flush( |
262 graphics_2d, callback.pp_completion_callback()); | 281 graphics_2d.host_resource(), callback.pp_completion_callback()); |
263 if (result != PP_ERROR_WOULDBLOCK) { | 282 if (result != PP_ERROR_WOULDBLOCK) { |
264 // There was some error, so we won't get a flush callback. We need to now | 283 // There was some error, so we won't get a flush callback. We need to now |
265 // issue the ACK to the plugin hears about the error. This will also clean | 284 // issue the ACK to the plugin hears about the error. This will also clean |
266 // up the data associated with the callback. | 285 // up the data associated with the callback. |
267 callback.Run(result); | 286 callback.Run(result); |
268 } | 287 } |
269 } | 288 } |
270 | 289 |
271 void PPB_Graphics2D_Proxy::OnMsgFlushACK(PP_Resource resource, | 290 void PPB_Graphics2D_Proxy::OnMsgFlushACK(const HostResource& host_resource, |
272 int32_t pp_error) { | 291 int32_t pp_error) { |
273 Graphics2D* object = PluginResource::GetAs<Graphics2D>(resource); | 292 PP_Resource plugin_resource = |
| 293 PluginResourceTracker::GetInstance()->PluginResourceForHostResource( |
| 294 host_resource); |
| 295 if (!plugin_resource) |
| 296 return; |
| 297 |
| 298 Graphics2D* object = PluginResource::GetAs<Graphics2D>(plugin_resource); |
274 if (!object) { | 299 if (!object) { |
275 // The plugin has released the graphics 2D object so don't issue the | 300 // The plugin has released the graphics 2D object so don't issue the |
276 // callback. | 301 // callback. |
277 return; | 302 return; |
278 } | 303 } |
279 | 304 |
280 // Be careful to make the callback NULL again before issuing the callback | 305 // Be careful to make the callback NULL again before issuing the callback |
281 // since the plugin might want to flush from within the callback. | 306 // since the plugin might want to flush from within the callback. |
282 PP_CompletionCallback callback = object->current_flush_callback(); | 307 PP_CompletionCallback callback = object->current_flush_callback(); |
283 object->set_current_flush_callback(PP_BlockUntilComplete()); | 308 object->set_current_flush_callback(PP_BlockUntilComplete()); |
284 PP_RunCompletionCallback(&callback, pp_error); | 309 PP_RunCompletionCallback(&callback, pp_error); |
285 } | 310 } |
286 | 311 |
287 void PPB_Graphics2D_Proxy::SendFlushACKToPlugin(int32_t result, | 312 void PPB_Graphics2D_Proxy::SendFlushACKToPlugin( |
288 PP_Resource graphics_2d) { | 313 int32_t result, |
| 314 const HostResource& graphics_2d) { |
289 dispatcher()->Send(new PpapiMsg_PPBGraphics2D_FlushACK( | 315 dispatcher()->Send(new PpapiMsg_PPBGraphics2D_FlushACK( |
290 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, result)); | 316 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, result)); |
291 } | 317 } |
292 | 318 |
293 } // namespace proxy | 319 } // namespace proxy |
294 } // namespace pp | 320 } // namespace pp |
OLD | NEW |