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/image_data.h" |
15 #include "ppapi/proxy/plugin_dispatcher.h" | 16 #include "ppapi/proxy/plugin_dispatcher.h" |
16 #include "ppapi/proxy/plugin_resource.h" | 17 #include "ppapi/proxy/plugin_resource.h" |
17 #include "ppapi/proxy/ppapi_messages.h" | 18 #include "ppapi/proxy/ppapi_messages.h" |
18 | 19 |
19 namespace pp { | 20 namespace pp { |
20 namespace proxy { | 21 namespace proxy { |
21 | 22 |
22 class Graphics2D : public PluginResource { | 23 class Graphics2D : public PluginResource { |
23 public: | 24 public: |
24 Graphics2D(PP_Instance instance, | 25 Graphics2D(PP_Instance instance, |
| 26 SerializedResource serialized_resource, |
25 const PP_Size& size, | 27 const PP_Size& size, |
26 PP_Bool is_always_opaque) | 28 PP_Bool is_always_opaque) |
27 : PluginResource(instance), | 29 : PluginResource(instance, serialized_resource), |
28 size_(size), | 30 size_(size), |
29 is_always_opaque_(is_always_opaque), | 31 is_always_opaque_(is_always_opaque), |
30 current_flush_callback_(PP_BlockUntilComplete()) { | 32 current_flush_callback_(PP_BlockUntilComplete()) { |
31 } | 33 } |
| 34 virtual ~Graphics2D() { |
| 35 } |
32 | 36 |
33 // Resource overrides. | 37 // Resource overrides. |
34 virtual Graphics2D* AsGraphics2D() { return this; } | 38 virtual Graphics2D* AsGraphics2D() { return this; } |
35 | 39 |
36 const PP_Size& size() const { return size_; } | 40 const PP_Size& size() const { return size_; } |
37 PP_Bool is_always_opaque() const { return is_always_opaque_; } | 41 PP_Bool is_always_opaque() const { return is_always_opaque_; } |
38 | 42 |
39 bool is_flush_pending() const { return !!current_flush_callback_.func; } | 43 bool is_flush_pending() const { return !!current_flush_callback_.func; } |
40 | 44 |
41 PP_CompletionCallback current_flush_callback() const { | 45 PP_CompletionCallback current_flush_callback() const { |
(...skipping 16 matching lines...) Expand all Loading... |
58 | 62 |
59 namespace { | 63 namespace { |
60 | 64 |
61 PP_Resource Create(PP_Instance instance, | 65 PP_Resource Create(PP_Instance instance, |
62 const PP_Size* size, | 66 const PP_Size* size, |
63 PP_Bool is_always_opaque) { | 67 PP_Bool is_always_opaque) { |
64 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | 68 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
65 if (!dispatcher) | 69 if (!dispatcher) |
66 return PP_ERROR_BADARGUMENT; | 70 return PP_ERROR_BADARGUMENT; |
67 | 71 |
68 PP_Resource result = 0; | 72 SerializedResource result; |
69 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Create( | 73 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Create( |
70 INTERFACE_ID_PPB_GRAPHICS_2D, instance, *size, is_always_opaque, | 74 INTERFACE_ID_PPB_GRAPHICS_2D, instance, *size, is_always_opaque, |
71 &result)); | 75 &result)); |
72 if (result) { | 76 if (result.is_null()) |
73 linked_ptr<Graphics2D> graphics_2d(new Graphics2D(instance, *size, | 77 return 0; |
74 is_always_opaque)); | 78 linked_ptr<Graphics2D> graphics_2d(new Graphics2D(instance, result, *size, |
75 PluginResourceTracker::GetInstance()->AddResource(result, graphics_2d); | 79 is_always_opaque)); |
76 } | 80 return PluginResourceTracker::GetInstance()->AddResource(graphics_2d); |
77 return result; | |
78 } | 81 } |
79 | 82 |
80 PP_Bool IsGraphics2D(PP_Resource resource) { | 83 PP_Bool IsGraphics2D(PP_Resource resource) { |
81 Graphics2D* object = PluginResource::GetAs<Graphics2D>(resource); | 84 Graphics2D* object = PluginResource::GetAs<Graphics2D>(resource); |
82 return BoolToPPBool(!!object); | 85 return BoolToPPBool(!!object); |
83 } | 86 } |
84 | 87 |
85 PP_Bool Describe(PP_Resource graphics_2d, | 88 PP_Bool Describe(PP_Resource graphics_2d, |
86 PP_Size* size, | 89 PP_Size* size, |
87 PP_Bool* is_always_opaque) { | 90 PP_Bool* is_always_opaque) { |
88 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); | 91 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); |
89 if (!object) { | 92 if (!object) { |
90 size->width = 0; | 93 size->width = 0; |
91 size->height = 0; | 94 size->height = 0; |
92 *is_always_opaque = PP_FALSE; | 95 *is_always_opaque = PP_FALSE; |
93 return PP_FALSE; | 96 return PP_FALSE; |
94 } | 97 } |
95 | 98 |
96 *size = object->size(); | 99 *size = object->size(); |
97 *is_always_opaque = object->is_always_opaque(); | 100 *is_always_opaque = object->is_always_opaque(); |
98 return PP_TRUE; | 101 return PP_TRUE; |
99 } | 102 } |
100 | 103 |
101 void PaintImageData(PP_Resource graphics_2d, | 104 void PaintImageData(PP_Resource graphics_2d, |
102 PP_Resource image_data, | 105 PP_Resource image_data, |
103 const PP_Point* top_left, | 106 const PP_Point* top_left, |
104 const PP_Rect* src_rect) { | 107 const PP_Rect* src_rect) { |
105 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); | 108 Graphics2D* graphics_object = PluginResource::GetAs<Graphics2D>(graphics_2d); |
106 if (!object) | 109 if (!graphics_object) |
107 return; | 110 return; |
| 111 ImageData* image_object = PluginResource::GetAs<ImageData>(image_data); |
| 112 if (!image_object) |
| 113 return; |
| 114 if (graphics_object->instance() != image_object->instance()) |
| 115 return; |
| 116 |
108 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( | 117 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( |
109 object->instance()); | 118 graphics_object->instance()); |
110 if (!dispatcher) | 119 if (!dispatcher) |
111 return; | 120 return; |
112 | 121 |
113 PP_Rect dummy; | 122 PP_Rect dummy; |
114 memset(&dummy, 0, sizeof(PP_Rect)); | 123 memset(&dummy, 0, sizeof(PP_Rect)); |
115 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_PaintImageData( | 124 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_PaintImageData( |
116 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, image_data, *top_left, | 125 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_object->host_resource(), |
117 !!src_rect, src_rect ? *src_rect : dummy)); | 126 image_object->host_resource(), *top_left, !!src_rect, |
| 127 src_rect ? *src_rect : dummy)); |
118 } | 128 } |
119 | 129 |
120 void Scroll(PP_Resource graphics_2d, | 130 void Scroll(PP_Resource graphics_2d, |
121 const PP_Rect* clip_rect, | 131 const PP_Rect* clip_rect, |
122 const PP_Point* amount) { | 132 const PP_Point* amount) { |
123 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); | 133 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); |
124 if (!object) | 134 if (!object) |
125 return; | 135 return; |
126 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( | 136 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance( |
127 object->instance()); | 137 object->instance()); |
128 if (!dispatcher) | 138 if (!dispatcher) |
129 return; | 139 return; |
130 | 140 |
131 PP_Rect dummy; | 141 PP_Rect dummy; |
132 memset(&dummy, 0, sizeof(PP_Rect)); | 142 memset(&dummy, 0, sizeof(PP_Rect)); |
133 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Scroll( | 143 dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Scroll( |
134 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, !!clip_rect, | 144 INTERFACE_ID_PPB_GRAPHICS_2D, object->host_resource(), |
135 clip_rect ? *clip_rect : dummy, *amount)); | 145 !!clip_rect, clip_rect ? *clip_rect : dummy, *amount)); |
136 } | 146 } |
137 | 147 |
138 void ReplaceContents(PP_Resource graphics_2d, PP_Resource image_data) { | 148 void ReplaceContents(PP_Resource graphics_2d, PP_Resource image_data) { |
139 Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); | 149 Graphics2D* graphics_object = PluginResource::GetAs<Graphics2D>(graphics_2d); |
140 if (!object) | 150 if (!graphics_object) |
141 return; | 151 return; |
| 152 ImageData* image_object = PluginResource::GetAs<ImageData>(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, |
| 189 object->instance(), object->host_resource())); |
172 return PP_ERROR_WOULDBLOCK; | 190 return PP_ERROR_WOULDBLOCK; |
173 } | 191 } |
174 | 192 |
175 const PPB_Graphics2D ppb_graphics_2d = { | 193 const PPB_Graphics2D ppb_graphics_2d = { |
176 &Create, | 194 &Create, |
177 &IsGraphics2D, | 195 &IsGraphics2D, |
178 &Describe, | 196 &Describe, |
179 &PaintImageData, | 197 &PaintImageData, |
180 &Scroll, | 198 &Scroll, |
181 &ReplaceContents, | 199 &ReplaceContents, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 OnMsgFlushACK) | 237 OnMsgFlushACK) |
220 IPC_MESSAGE_UNHANDLED(handled = false) | 238 IPC_MESSAGE_UNHANDLED(handled = false) |
221 IPC_END_MESSAGE_MAP() | 239 IPC_END_MESSAGE_MAP() |
222 // FIXME(brettw) handle bad messages! | 240 // FIXME(brettw) handle bad messages! |
223 return handled; | 241 return handled; |
224 } | 242 } |
225 | 243 |
226 void PPB_Graphics2D_Proxy::OnMsgCreate(PP_Instance instance, | 244 void PPB_Graphics2D_Proxy::OnMsgCreate(PP_Instance instance, |
227 const PP_Size& size, | 245 const PP_Size& size, |
228 PP_Bool is_always_opaque, | 246 PP_Bool is_always_opaque, |
229 PP_Resource* result) { | 247 SerializedResource* result) { |
230 *result = ppb_graphics_2d_target()->Create( | 248 result->set_host_resource(ppb_graphics_2d_target()->Create( |
231 instance, &size, is_always_opaque); | 249 instance, &size, is_always_opaque)); |
232 } | 250 } |
233 | 251 |
234 void PPB_Graphics2D_Proxy::OnMsgPaintImageData(PP_Resource graphics_2d, | 252 void PPB_Graphics2D_Proxy::OnMsgPaintImageData(SerializedResource graphics_2d, |
235 PP_Resource image_data, | 253 SerializedResource image_data, |
236 const PP_Point& top_left, | 254 const PP_Point& top_left, |
237 bool src_rect_specified, | 255 bool src_rect_specified, |
238 const PP_Rect& src_rect) { | 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(SerializedResource 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(SerializedResource graphics_2d, |
254 PP_Resource image_data) { | 271 SerializedResource image_data) { |
255 ppb_graphics_2d_target()->ReplaceContents(graphics_2d, image_data); | 272 ppb_graphics_2d_target()->ReplaceContents(graphics_2d.host_resource(), |
| 273 image_data.host_resource()); |
256 } | 274 } |
257 | 275 |
258 void PPB_Graphics2D_Proxy::OnMsgFlush(PP_Resource graphics_2d) { | 276 void PPB_Graphics2D_Proxy::OnMsgFlush(PP_Instance instance, |
| 277 SerializedResource 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, instance, 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(PP_Instance instance, |
| 291 SerializedResource host_resource, |
272 int32_t pp_error) { | 292 int32_t pp_error) { |
273 Graphics2D* object = PluginResource::GetAs<Graphics2D>(resource); | 293 PP_Resource plugin_resource = |
| 294 PluginResourceTracker::GetInstance()->PluginResourceForHostResource( |
| 295 instance, host_resource); |
| 296 if (!plugin_resource) |
| 297 return; |
| 298 |
| 299 Graphics2D* object = PluginResource::GetAs<Graphics2D>(plugin_resource); |
274 if (!object) { | 300 if (!object) { |
275 // The plugin has released the graphics 2D object so don't issue the | 301 // The plugin has released the graphics 2D object so don't issue the |
276 // callback. | 302 // callback. |
277 return; | 303 return; |
278 } | 304 } |
279 | 305 |
280 // Be careful to make the callback NULL again before issuing the callback | 306 // Be careful to make the callback NULL again before issuing the callback |
281 // since the plugin might want to flush from within the callback. | 307 // since the plugin might want to flush from within the callback. |
282 PP_CompletionCallback callback = object->current_flush_callback(); | 308 PP_CompletionCallback callback = object->current_flush_callback(); |
283 object->set_current_flush_callback(PP_BlockUntilComplete()); | 309 object->set_current_flush_callback(PP_BlockUntilComplete()); |
284 PP_RunCompletionCallback(&callback, pp_error); | 310 PP_RunCompletionCallback(&callback, pp_error); |
285 } | 311 } |
286 | 312 |
287 void PPB_Graphics2D_Proxy::SendFlushACKToPlugin(int32_t result, | 313 void PPB_Graphics2D_Proxy::SendFlushACKToPlugin( |
288 PP_Resource graphics_2d) { | 314 int32_t result, |
| 315 PP_Instance instance, |
| 316 SerializedResource graphics_2d) { |
289 dispatcher()->Send(new PpapiMsg_PPBGraphics2D_FlushACK( | 317 dispatcher()->Send(new PpapiMsg_PPBGraphics2D_FlushACK( |
290 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, result)); | 318 INTERFACE_ID_PPB_GRAPHICS_2D, instance, graphics_2d, result)); |
291 } | 319 } |
292 | 320 |
293 } // namespace proxy | 321 } // namespace proxy |
294 } // namespace pp | 322 } // namespace pp |
OLD | NEW |