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/enter_proxy.h" | |
16 #include "ppapi/proxy/plugin_dispatcher.h" | 15 #include "ppapi/proxy/plugin_dispatcher.h" |
17 #include "ppapi/proxy/plugin_resource.h" | 16 #include "ppapi/proxy/plugin_resource.h" |
18 #include "ppapi/proxy/ppapi_messages.h" | 17 #include "ppapi/proxy/ppapi_messages.h" |
19 #include "ppapi/thunk/enter.h" | 18 #include "ppapi/thunk/enter.h" |
20 #include "ppapi/thunk/ppb_graphics_2d_api.h" | |
21 #include "ppapi/thunk/thunk.h" | 19 #include "ppapi/thunk/thunk.h" |
22 | 20 |
23 using ::ppapi::thunk::PPB_Graphics2D_API; | 21 using ::ppapi::thunk::PPB_Graphics2D_API; |
| 22 using ::ppapi::thunk::EnterResource; |
24 | 23 |
25 namespace pp { | 24 namespace pp { |
26 namespace proxy { | 25 namespace proxy { |
27 | 26 |
28 namespace { | 27 namespace { |
29 | 28 |
30 InterfaceProxy* CreateGraphics2DProxy(Dispatcher* dispatcher, | 29 InterfaceProxy* CreateGraphics2DProxy(Dispatcher* dispatcher, |
31 const void* target_interface) { | 30 const void* target_interface) { |
32 return new PPB_Graphics2D_Proxy(dispatcher, target_interface); | 31 return new PPB_Graphics2D_Proxy(dispatcher, target_interface); |
33 } | 32 } |
34 | 33 |
35 } // namespace | 34 } // namespace |
36 | 35 |
37 class Graphics2D : public PluginResource, | 36 PPB_Graphics2D_API* Graphics2D::AsGraphics2D_API() { |
38 public ::ppapi::thunk::PPB_Graphics2D_API { | |
39 public: | |
40 Graphics2D(const HostResource& host_resource, | |
41 const PP_Size& size, | |
42 PP_Bool is_always_opaque); | |
43 virtual ~Graphics2D(); | |
44 | |
45 // ResourceObjectBase. | |
46 virtual PPB_Graphics2D_API* AsPPB_Graphics2D_API(); | |
47 | |
48 // PPB_Graphics_2D_API. | |
49 PP_Bool Describe(PP_Size* size, PP_Bool* is_always_opaque); | |
50 void PaintImageData(PP_Resource image_data, | |
51 const PP_Point* top_left, | |
52 const PP_Rect* src_rect); | |
53 void Scroll(const PP_Rect* clip_rect, | |
54 const PP_Point* amount); | |
55 void ReplaceContents(PP_Resource image_data); | |
56 int32_t Flush(PP_CompletionCallback callback); | |
57 | |
58 // Notification that the host has sent an ACK for a pending Flush. | |
59 void FlushACK(int32_t result_code); | |
60 | |
61 private: | |
62 PP_Size size_; | |
63 PP_Bool is_always_opaque_; | |
64 | |
65 // In the plugin, this is the current callback set for Flushes. When the | |
66 // callback function pointer is non-NULL, we're waiting for a flush ACK. | |
67 PP_CompletionCallback current_flush_callback_; | |
68 | |
69 DISALLOW_COPY_AND_ASSIGN(Graphics2D); | |
70 }; | |
71 | |
72 Graphics2D::Graphics2D(const HostResource& host_resource, | |
73 const PP_Size& size, | |
74 PP_Bool is_always_opaque) | |
75 : PluginResource(host_resource), | |
76 size_(size), | |
77 is_always_opaque_(is_always_opaque), | |
78 current_flush_callback_(PP_BlockUntilComplete()) { | |
79 } | |
80 | |
81 Graphics2D::~Graphics2D() { | |
82 } | |
83 | |
84 PPB_Graphics2D_API* Graphics2D::AsPPB_Graphics2D_API() { | |
85 return this; | 37 return this; |
86 } | 38 } |
87 | 39 |
88 PP_Bool Graphics2D::Describe(PP_Size* size, PP_Bool* is_always_opaque) { | 40 PP_Bool Graphics2D::Describe(PP_Size* size, PP_Bool* is_always_opaque) { |
89 *size = size_; | 41 *size = size_; |
90 *is_always_opaque = is_always_opaque_; | 42 *is_always_opaque = is_always_opaque_; |
91 return PP_TRUE; | 43 return PP_TRUE; |
92 } | 44 } |
93 | 45 |
94 void Graphics2D::PaintImageData(PP_Resource image_data, | 46 void Graphics2D::PaintImageData(PP_Resource image_data, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 INTERFACE_ID_PPB_GRAPHICS_2D, host_resource(), | 78 INTERFACE_ID_PPB_GRAPHICS_2D, host_resource(), |
127 image_object->host_resource())); | 79 image_object->host_resource())); |
128 } | 80 } |
129 | 81 |
130 int32_t Graphics2D::Flush(PP_CompletionCallback callback) { | 82 int32_t Graphics2D::Flush(PP_CompletionCallback callback) { |
131 // For now, disallow blocking calls. We'll need to add support for other | 83 // For now, disallow blocking calls. We'll need to add support for other |
132 // threads to this later. | 84 // threads to this later. |
133 if (!callback.func) | 85 if (!callback.func) |
134 return PP_ERROR_BADARGUMENT; | 86 return PP_ERROR_BADARGUMENT; |
135 | 87 |
136 if (current_flush_callback_.func) | 88 if (is_flush_pending()) |
137 return PP_ERROR_INPROGRESS; // Can't have >1 flush pending. | 89 return PP_ERROR_INPROGRESS; // Can't have >1 flush pending. |
138 current_flush_callback_ = callback; | 90 set_current_flush_callback(callback); |
139 | 91 |
140 GetDispatcher()->Send(new PpapiHostMsg_PPBGraphics2D_Flush( | 92 GetDispatcher()->Send(new PpapiHostMsg_PPBGraphics2D_Flush( |
141 INTERFACE_ID_PPB_GRAPHICS_2D, host_resource())); | 93 INTERFACE_ID_PPB_GRAPHICS_2D, host_resource())); |
142 return PP_OK_COMPLETIONPENDING; | 94 return PP_OK_COMPLETIONPENDING; |
143 } | 95 } |
144 | 96 |
145 void Graphics2D::FlushACK(int32_t result_code) { | |
146 PP_RunAndClearCompletionCallback(¤t_flush_callback_, result_code); | |
147 } | |
148 | |
149 PPB_Graphics2D_Proxy::PPB_Graphics2D_Proxy(Dispatcher* dispatcher, | 97 PPB_Graphics2D_Proxy::PPB_Graphics2D_Proxy(Dispatcher* dispatcher, |
150 const void* target_interface) | 98 const void* target_interface) |
151 : InterfaceProxy(dispatcher, target_interface), | 99 : InterfaceProxy(dispatcher, target_interface), |
152 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 100 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
153 } | 101 } |
154 | 102 |
155 PPB_Graphics2D_Proxy::~PPB_Graphics2D_Proxy() { | 103 PPB_Graphics2D_Proxy::~PPB_Graphics2D_Proxy() { |
156 } | 104 } |
157 | 105 |
158 // static | 106 // static |
159 const InterfaceProxy::Info* PPB_Graphics2D_Proxy::GetInfo() { | 107 const InterfaceProxy::Info* PPB_Graphics2D_Proxy::GetInfo() { |
160 static const Info info = { | 108 static const Info info = { |
161 ::ppapi::thunk::GetPPB_Graphics2D_Thunk(), | 109 ::ppapi::thunk::GetPPB_Graphics2D_Thunk(), |
162 PPB_GRAPHICS_2D_INTERFACE, | 110 PPB_GRAPHICS_2D_INTERFACE, |
163 INTERFACE_ID_PPB_GRAPHICS_2D, | 111 INTERFACE_ID_PPB_GRAPHICS_2D, |
164 false, | 112 false, |
165 &CreateGraphics2DProxy, | 113 &CreateGraphics2DProxy, |
166 }; | 114 }; |
167 return &info; | 115 return &info; |
168 } | 116 } |
169 | 117 |
170 // static | |
171 PP_Resource PPB_Graphics2D_Proxy::CreateProxyResource( | |
172 PP_Instance instance, | |
173 const PP_Size& size, | |
174 PP_Bool is_always_opaque) { | |
175 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | |
176 if (!dispatcher) | |
177 return 0; | |
178 | |
179 HostResource result; | |
180 dispatcher->Send(new PpapiHostMsg_ResourceCreation_Graphics2D( | |
181 INTERFACE_ID_RESOURCE_CREATION, instance, size, is_always_opaque, | |
182 &result)); | |
183 if (result.is_null()) | |
184 return 0; | |
185 linked_ptr<Graphics2D> graphics_2d(new Graphics2D(result, size, | |
186 is_always_opaque)); | |
187 return PluginResourceTracker::GetInstance()->AddResource(graphics_2d); | |
188 } | |
189 | |
190 bool PPB_Graphics2D_Proxy::OnMessageReceived(const IPC::Message& msg) { | 118 bool PPB_Graphics2D_Proxy::OnMessageReceived(const IPC::Message& msg) { |
191 bool handled = true; | 119 bool handled = true; |
192 IPC_BEGIN_MESSAGE_MAP(PPB_Graphics2D_Proxy, msg) | 120 IPC_BEGIN_MESSAGE_MAP(PPB_Graphics2D_Proxy, msg) |
193 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_PaintImageData, | 121 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_PaintImageData, |
194 OnMsgPaintImageData) | 122 OnMsgPaintImageData) |
195 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_Scroll, | 123 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_Scroll, |
196 OnMsgScroll) | 124 OnMsgScroll) |
197 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_ReplaceContents, | 125 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_ReplaceContents, |
198 OnMsgReplaceContents) | 126 OnMsgReplaceContents) |
199 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_Flush, | 127 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_Flush, |
200 OnMsgFlush) | 128 OnMsgFlush) |
201 | 129 |
202 IPC_MESSAGE_HANDLER(PpapiMsg_PPBGraphics2D_FlushACK, | 130 IPC_MESSAGE_HANDLER(PpapiMsg_PPBGraphics2D_FlushACK, |
203 OnMsgFlushACK) | 131 OnMsgFlushACK) |
204 IPC_MESSAGE_UNHANDLED(handled = false) | 132 IPC_MESSAGE_UNHANDLED(handled = false) |
205 IPC_END_MESSAGE_MAP() | 133 IPC_END_MESSAGE_MAP() |
206 // FIXME(brettw) handle bad messages! | 134 // FIXME(brettw) handle bad messages! |
207 return handled; | 135 return handled; |
208 } | 136 } |
209 | 137 |
210 void PPB_Graphics2D_Proxy::OnMsgPaintImageData( | 138 void PPB_Graphics2D_Proxy::OnMsgPaintImageData( |
211 const HostResource& graphics_2d, | 139 const HostResource& graphics_2d, |
212 const HostResource& image_data, | 140 const HostResource& image_data, |
213 const PP_Point& top_left, | 141 const PP_Point& top_left, |
214 bool src_rect_specified, | 142 bool src_rect_specified, |
215 const PP_Rect& src_rect) { | 143 const PP_Rect& src_rect) { |
216 EnterHostFromHostResource<PPB_Graphics2D_API> enter(graphics_2d); | 144 EnterResource<PPB_Graphics2D_API> enter(graphics_2d.host_resource(), false); |
217 if (enter.failed()) | 145 if (enter.failed()) |
218 return; | 146 return; |
219 enter.object()->PaintImageData(image_data.host_resource(), &top_left, | 147 enter.object()->PaintImageData(image_data.host_resource(), &top_left, |
220 src_rect_specified ? &src_rect : NULL); | 148 src_rect_specified ? &src_rect : NULL); |
221 } | 149 } |
222 | 150 |
223 void PPB_Graphics2D_Proxy::OnMsgScroll(const HostResource& graphics_2d, | 151 void PPB_Graphics2D_Proxy::OnMsgScroll(const HostResource& graphics_2d, |
224 bool clip_specified, | 152 bool clip_specified, |
225 const PP_Rect& clip, | 153 const PP_Rect& clip, |
226 const PP_Point& amount) { | 154 const PP_Point& amount) { |
227 EnterHostFromHostResource<PPB_Graphics2D_API> enter(graphics_2d); | 155 EnterResource<PPB_Graphics2D_API> enter(graphics_2d.host_resource(), false); |
228 if (enter.failed()) | 156 if (enter.failed()) |
229 return; | 157 return; |
230 enter.object()->Scroll(clip_specified ? &clip : NULL, &amount); | 158 enter.object()->Scroll(clip_specified ? &clip : NULL, &amount); |
231 } | 159 } |
232 | 160 |
233 void PPB_Graphics2D_Proxy::OnMsgReplaceContents( | 161 void PPB_Graphics2D_Proxy::OnMsgReplaceContents( |
234 const HostResource& graphics_2d, | 162 const HostResource& graphics_2d, |
235 const HostResource& image_data) { | 163 const HostResource& image_data) { |
236 EnterHostFromHostResource<PPB_Graphics2D_API> enter(graphics_2d); | 164 EnterResource<PPB_Graphics2D_API> enter(graphics_2d.host_resource(), false); |
237 if (enter.failed()) | 165 if (enter.failed()) |
238 return; | 166 return; |
239 enter.object()->ReplaceContents(image_data.host_resource()); | 167 enter.object()->ReplaceContents(image_data.host_resource()); |
240 } | 168 } |
241 | 169 |
242 void PPB_Graphics2D_Proxy::OnMsgFlush(const HostResource& graphics_2d) { | 170 void PPB_Graphics2D_Proxy::OnMsgFlush(const HostResource& graphics_2d) { |
243 CompletionCallback callback = callback_factory_.NewCallback( | 171 CompletionCallback callback = callback_factory_.NewCallback( |
244 &PPB_Graphics2D_Proxy::SendFlushACKToPlugin, graphics_2d); | 172 &PPB_Graphics2D_Proxy::SendFlushACKToPlugin, graphics_2d); |
245 int32_t result = ppb_graphics_2d_target()->Flush( | 173 int32_t result = ppb_graphics_2d_target()->Flush( |
246 graphics_2d.host_resource(), callback.pp_completion_callback()); | 174 graphics_2d.host_resource(), callback.pp_completion_callback()); |
247 if (result != PP_OK_COMPLETIONPENDING) { | 175 if (result != PP_OK_COMPLETIONPENDING) { |
248 // 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 |
249 // 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 |
250 // up the data associated with the callback. | 178 // up the data associated with the callback. |
251 callback.Run(result); | 179 callback.Run(result); |
252 } | 180 } |
253 } | 181 } |
254 | 182 |
255 void PPB_Graphics2D_Proxy::OnMsgFlushACK(const HostResource& host_resource, | 183 void PPB_Graphics2D_Proxy::OnMsgFlushACK(const HostResource& host_resource, |
256 int32_t pp_error) { | 184 int32_t pp_error) { |
257 EnterPluginFromHostResource<PPB_Graphics2D_API> enter(host_resource); | 185 PP_Resource plugin_resource = |
258 if (enter.succeeded()) | 186 PluginResourceTracker::GetInstance()->PluginResourceForHostResource( |
259 static_cast<Graphics2D*>(enter.object())->FlushACK(pp_error); | 187 host_resource); |
| 188 if (!plugin_resource) |
| 189 return; |
| 190 |
| 191 Graphics2D* object = PluginResource::GetAs<Graphics2D>(plugin_resource); |
| 192 if (!object) { |
| 193 // The plugin has released the graphics 2D object so don't issue the |
| 194 // callback. |
| 195 return; |
| 196 } |
| 197 |
| 198 // Be careful to make the callback NULL again before issuing the callback |
| 199 // since the plugin might want to flush from within the callback. |
| 200 PP_CompletionCallback callback = object->current_flush_callback(); |
| 201 object->set_current_flush_callback(PP_BlockUntilComplete()); |
| 202 PP_RunCompletionCallback(&callback, pp_error); |
260 } | 203 } |
261 | 204 |
262 void PPB_Graphics2D_Proxy::SendFlushACKToPlugin( | 205 void PPB_Graphics2D_Proxy::SendFlushACKToPlugin( |
263 int32_t result, | 206 int32_t result, |
264 const HostResource& graphics_2d) { | 207 const HostResource& graphics_2d) { |
265 dispatcher()->Send(new PpapiMsg_PPBGraphics2D_FlushACK( | 208 dispatcher()->Send(new PpapiMsg_PPBGraphics2D_FlushACK( |
266 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, result)); | 209 INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, result)); |
267 } | 210 } |
268 | 211 |
269 } // namespace proxy | 212 } // namespace proxy |
270 } // namespace pp | 213 } // namespace pp |
OLD | NEW |