OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "webkit/glue/plugins/pepper_graphics_2d.h" | 5 #include "webkit/plugins/ppapi/ppb_graphics_2d_impl.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/task.h" | 11 #include "base/task.h" |
12 #include "gfx/blit.h" | 12 #include "gfx/blit.h" |
13 #include "gfx/point.h" | 13 #include "gfx/point.h" |
14 #include "gfx/rect.h" | 14 #include "gfx/rect.h" |
15 #include "skia/ext/platform_canvas.h" | 15 #include "skia/ext/platform_canvas.h" |
16 #include "ppapi/c/pp_errors.h" | 16 #include "ppapi/c/pp_errors.h" |
17 #include "ppapi/c/pp_module.h" | 17 #include "ppapi/c/pp_module.h" |
18 #include "ppapi/c/pp_rect.h" | 18 #include "ppapi/c/pp_rect.h" |
19 #include "ppapi/c/pp_resource.h" | 19 #include "ppapi/c/pp_resource.h" |
20 #include "ppapi/c/ppb_graphics_2d.h" | 20 #include "ppapi/c/ppb_graphics_2d.h" |
21 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
22 #include "webkit/glue/plugins/pepper_common.h" | 22 #include "webkit/plugins/ppapi/common.h" |
23 #include "webkit/glue/plugins/pepper_image_data.h" | 23 #include "webkit/plugins/ppapi/plugin_instance.h" |
24 #include "webkit/glue/plugins/pepper_plugin_instance.h" | 24 #include "webkit/plugins/ppapi/plugin_module.h" |
25 #include "webkit/glue/plugins/pepper_plugin_module.h" | 25 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" |
26 | 26 |
27 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
28 #include "base/mac_util.h" | 28 #include "base/mac_util.h" |
29 #include "base/mac/scoped_cftyperef.h" | 29 #include "base/mac/scoped_cftyperef.h" |
30 #endif | 30 #endif |
31 | 31 |
32 namespace pepper { | 32 namespace webkit { |
| 33 namespace ppapi { |
33 | 34 |
34 namespace { | 35 namespace { |
35 | 36 |
36 // Converts a rect inside an image of the given dimensions. The rect may be | 37 // Converts a rect inside an image of the given dimensions. The rect may be |
37 // NULL to indicate it should be the entire image. If the rect is outside of | 38 // NULL to indicate it should be the entire image. If the rect is outside of |
38 // the image, this will do nothing and return false. | 39 // the image, this will do nothing and return false. |
39 bool ValidateAndConvertRect(const PP_Rect* rect, | 40 bool ValidateAndConvertRect(const PP_Rect* rect, |
40 int image_width, int image_height, | 41 int image_width, int image_height, |
41 gfx::Rect* dest) { | 42 gfx::Rect* dest) { |
42 if (!rect) { | 43 if (!rect) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 unsigned char* pixel_out = reinterpret_cast<unsigned char*>(&output[i]); | 75 unsigned char* pixel_out = reinterpret_cast<unsigned char*>(&output[i]); |
75 pixel_out[0] = pixel_in[2]; | 76 pixel_out[0] = pixel_in[2]; |
76 pixel_out[1] = pixel_in[1]; | 77 pixel_out[1] = pixel_in[1]; |
77 pixel_out[2] = pixel_in[0]; | 78 pixel_out[2] = pixel_in[0]; |
78 pixel_out[3] = pixel_in[3]; | 79 pixel_out[3] = pixel_in[3]; |
79 } | 80 } |
80 } | 81 } |
81 | 82 |
82 // Converts ImageData from PP_IMAGEDATAFORMAT_BGRA_PREMUL to | 83 // Converts ImageData from PP_IMAGEDATAFORMAT_BGRA_PREMUL to |
83 // PP_IMAGEDATAFORMAT_RGBA_PREMUL, or reverse. | 84 // PP_IMAGEDATAFORMAT_RGBA_PREMUL, or reverse. |
84 void ConvertImageData(ImageData* src_image, const SkIRect& src_rect, | 85 void ConvertImageData(PPB_ImageData_Impl* src_image, const SkIRect& src_rect, |
85 ImageData* dest_image, const SkRect& dest_rect) { | 86 PPB_ImageData_Impl* dest_image, const SkRect& dest_rect) { |
86 DCHECK(src_image->format() != dest_image->format()); | 87 DCHECK(src_image->format() != dest_image->format()); |
87 DCHECK(ImageData::IsImageDataFormatSupported(src_image->format())); | 88 DCHECK(PPB_ImageData_Impl::IsImageDataFormatSupported(src_image->format())); |
88 DCHECK(ImageData::IsImageDataFormatSupported(dest_image->format())); | 89 DCHECK(PPB_ImageData_Impl::IsImageDataFormatSupported(dest_image->format())); |
89 | 90 |
90 const SkBitmap* src_bitmap = src_image->GetMappedBitmap(); | 91 const SkBitmap* src_bitmap = src_image->GetMappedBitmap(); |
91 const SkBitmap* dest_bitmap = dest_image->GetMappedBitmap(); | 92 const SkBitmap* dest_bitmap = dest_image->GetMappedBitmap(); |
92 if (src_rect.width() == src_image->width() && | 93 if (src_rect.width() == src_image->width() && |
93 dest_rect.width() == dest_image->width()) { | 94 dest_rect.width() == dest_image->width()) { |
94 // Fast path if the full line needs to be converted. | 95 // Fast path if the full line needs to be converted. |
95 ConvertBetweenBGRAandRGBA( | 96 ConvertBetweenBGRAandRGBA( |
96 src_bitmap->getAddr32(static_cast<int>(src_rect.fLeft), | 97 src_bitmap->getAddr32(static_cast<int>(src_rect.fLeft), |
97 static_cast<int>(src_rect.fTop)), | 98 static_cast<int>(src_rect.fTop)), |
98 src_rect.width() * src_rect.height(), | 99 src_rect.width() * src_rect.height(), |
(...skipping 12 matching lines...) Expand all Loading... |
111 } | 112 } |
112 } | 113 } |
113 | 114 |
114 PP_Resource Create(PP_Module module_id, | 115 PP_Resource Create(PP_Module module_id, |
115 const PP_Size* size, | 116 const PP_Size* size, |
116 PP_Bool is_always_opaque) { | 117 PP_Bool is_always_opaque) { |
117 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); | 118 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); |
118 if (!module) | 119 if (!module) |
119 return 0; | 120 return 0; |
120 | 121 |
121 scoped_refptr<Graphics2D> context(new Graphics2D(module)); | 122 scoped_refptr<PPB_Graphics2D_Impl> context(new PPB_Graphics2D_Impl(module)); |
122 if (!context->Init(size->width, size->height, PPBoolToBool(is_always_opaque))) | 123 if (!context->Init(size->width, size->height, PPBoolToBool(is_always_opaque))) |
123 return 0; | 124 return 0; |
124 return context->GetReference(); | 125 return context->GetReference(); |
125 } | 126 } |
126 | 127 |
127 PP_Bool IsGraphics2D(PP_Resource resource) { | 128 PP_Bool IsGraphics2D(PP_Resource resource) { |
128 return BoolToPPBool(!!Resource::GetAs<Graphics2D>(resource)); | 129 return BoolToPPBool(!!Resource::GetAs<PPB_Graphics2D_Impl>(resource)); |
129 } | 130 } |
130 | 131 |
131 PP_Bool Describe(PP_Resource graphics_2d, | 132 PP_Bool Describe(PP_Resource graphics_2d, |
132 PP_Size* size, | 133 PP_Size* size, |
133 PP_Bool* is_always_opaque) { | 134 PP_Bool* is_always_opaque) { |
134 scoped_refptr<Graphics2D> context( | 135 scoped_refptr<PPB_Graphics2D_Impl> context( |
135 Resource::GetAs<Graphics2D>(graphics_2d)); | 136 Resource::GetAs<PPB_Graphics2D_Impl>(graphics_2d)); |
136 if (!context) | 137 if (!context) |
137 return PP_FALSE; | 138 return PP_FALSE; |
138 return context->Describe(size, is_always_opaque); | 139 return context->Describe(size, is_always_opaque); |
139 } | 140 } |
140 | 141 |
141 void PaintImageData(PP_Resource graphics_2d, | 142 void PaintImageData(PP_Resource graphics_2d, |
142 PP_Resource image_data, | 143 PP_Resource image_data, |
143 const PP_Point* top_left, | 144 const PP_Point* top_left, |
144 const PP_Rect* src_rect) { | 145 const PP_Rect* src_rect) { |
145 scoped_refptr<Graphics2D> context( | 146 scoped_refptr<PPB_Graphics2D_Impl> context( |
146 Resource::GetAs<Graphics2D>(graphics_2d)); | 147 Resource::GetAs<PPB_Graphics2D_Impl>(graphics_2d)); |
147 if (context) | 148 if (context) |
148 context->PaintImageData(image_data, top_left, src_rect); | 149 context->PaintImageData(image_data, top_left, src_rect); |
149 } | 150 } |
150 | 151 |
151 void Scroll(PP_Resource graphics_2d, | 152 void Scroll(PP_Resource graphics_2d, |
152 const PP_Rect* clip_rect, | 153 const PP_Rect* clip_rect, |
153 const PP_Point* amount) { | 154 const PP_Point* amount) { |
154 scoped_refptr<Graphics2D> context( | 155 scoped_refptr<PPB_Graphics2D_Impl> context( |
155 Resource::GetAs<Graphics2D>(graphics_2d)); | 156 Resource::GetAs<PPB_Graphics2D_Impl>(graphics_2d)); |
156 if (context) | 157 if (context) |
157 context->Scroll(clip_rect, amount); | 158 context->Scroll(clip_rect, amount); |
158 } | 159 } |
159 | 160 |
160 void ReplaceContents(PP_Resource graphics_2d, PP_Resource image_data) { | 161 void ReplaceContents(PP_Resource graphics_2d, PP_Resource image_data) { |
161 scoped_refptr<Graphics2D> context( | 162 scoped_refptr<PPB_Graphics2D_Impl> context( |
162 Resource::GetAs<Graphics2D>(graphics_2d)); | 163 Resource::GetAs<PPB_Graphics2D_Impl>(graphics_2d)); |
163 if (context) | 164 if (context) |
164 context->ReplaceContents(image_data); | 165 context->ReplaceContents(image_data); |
165 } | 166 } |
166 | 167 |
167 int32_t Flush(PP_Resource graphics_2d, | 168 int32_t Flush(PP_Resource graphics_2d, |
168 PP_CompletionCallback callback) { | 169 PP_CompletionCallback callback) { |
169 scoped_refptr<Graphics2D> context( | 170 scoped_refptr<PPB_Graphics2D_Impl> context( |
170 Resource::GetAs<Graphics2D>(graphics_2d)); | 171 Resource::GetAs<PPB_Graphics2D_Impl>(graphics_2d)); |
171 if (!context) | 172 if (!context) |
172 return PP_ERROR_BADRESOURCE; | 173 return PP_ERROR_BADRESOURCE; |
173 return context->Flush(callback); | 174 return context->Flush(callback); |
174 } | 175 } |
175 | 176 |
176 const PPB_Graphics2D ppb_graphics_2d = { | 177 const PPB_Graphics2D ppb_graphics_2d = { |
177 &Create, | 178 &Create, |
178 &IsGraphics2D, | 179 &IsGraphics2D, |
179 &Describe, | 180 &Describe, |
180 &PaintImageData, | 181 &PaintImageData, |
181 &Scroll, | 182 &Scroll, |
182 &ReplaceContents, | 183 &ReplaceContents, |
183 &Flush | 184 &Flush |
184 }; | 185 }; |
185 | 186 |
186 } // namespace | 187 } // namespace |
187 | 188 |
188 struct Graphics2D::QueuedOperation { | 189 struct PPB_Graphics2D_Impl::QueuedOperation { |
189 enum Type { | 190 enum Type { |
190 PAINT, | 191 PAINT, |
191 SCROLL, | 192 SCROLL, |
192 REPLACE | 193 REPLACE |
193 }; | 194 }; |
194 | 195 |
195 QueuedOperation(Type t) | 196 QueuedOperation(Type t) |
196 : type(t), | 197 : type(t), |
197 paint_x(0), | 198 paint_x(0), |
198 paint_y(0), | 199 paint_y(0), |
199 scroll_dx(0), | 200 scroll_dx(0), |
200 scroll_dy(0) { | 201 scroll_dy(0) { |
201 } | 202 } |
202 | 203 |
203 Type type; | 204 Type type; |
204 | 205 |
205 // Valid when type == PAINT. | 206 // Valid when type == PAINT. |
206 scoped_refptr<ImageData> paint_image; | 207 scoped_refptr<PPB_ImageData_Impl> paint_image; |
207 int paint_x, paint_y; | 208 int paint_x, paint_y; |
208 gfx::Rect paint_src_rect; | 209 gfx::Rect paint_src_rect; |
209 | 210 |
210 // Valid when type == SCROLL. | 211 // Valid when type == SCROLL. |
211 gfx::Rect scroll_clip_rect; | 212 gfx::Rect scroll_clip_rect; |
212 int scroll_dx, scroll_dy; | 213 int scroll_dx, scroll_dy; |
213 | 214 |
214 // Valid when type == REPLACE. | 215 // Valid when type == REPLACE. |
215 scoped_refptr<ImageData> replace_image; | 216 scoped_refptr<PPB_ImageData_Impl> replace_image; |
216 }; | 217 }; |
217 | 218 |
218 Graphics2D::Graphics2D(PluginModule* module) | 219 PPB_Graphics2D_Impl::PPB_Graphics2D_Impl(PluginModule* module) |
219 : Resource(module), | 220 : Resource(module), |
220 bound_instance_(NULL), | 221 bound_instance_(NULL), |
221 flushed_any_data_(false), | 222 flushed_any_data_(false), |
222 offscreen_flush_pending_(false), | 223 offscreen_flush_pending_(false), |
223 is_always_opaque_(false) { | 224 is_always_opaque_(false) { |
224 } | 225 } |
225 | 226 |
226 Graphics2D::~Graphics2D() { | 227 PPB_Graphics2D_Impl::~PPB_Graphics2D_Impl() { |
227 } | 228 } |
228 | 229 |
229 // static | 230 // static |
230 const PPB_Graphics2D* Graphics2D::GetInterface() { | 231 const PPB_Graphics2D* PPB_Graphics2D_Impl::GetInterface() { |
231 return &ppb_graphics_2d; | 232 return &ppb_graphics_2d; |
232 } | 233 } |
233 | 234 |
234 bool Graphics2D::Init(int width, int height, bool is_always_opaque) { | 235 bool PPB_Graphics2D_Impl::Init(int width, int height, bool is_always_opaque) { |
235 // The underlying ImageData will validate the dimensions. | 236 // The underlying PPB_ImageData_Impl will validate the dimensions. |
236 image_data_ = new ImageData(module()); | 237 image_data_ = new PPB_ImageData_Impl(module()); |
237 if (!image_data_->Init(ImageData::GetNativeImageDataFormat(), width, height, | 238 if (!image_data_->Init(PPB_ImageData_Impl::GetNativeImageDataFormat(), |
238 true) || !image_data_->Map()) { | 239 width, height, true) || |
| 240 !image_data_->Map()) { |
239 image_data_ = NULL; | 241 image_data_ = NULL; |
240 return false; | 242 return false; |
241 } | 243 } |
242 is_always_opaque_ = is_always_opaque; | 244 is_always_opaque_ = is_always_opaque; |
243 return true; | 245 return true; |
244 } | 246 } |
245 | 247 |
246 PP_Bool Graphics2D::Describe(PP_Size* size, PP_Bool* is_always_opaque) { | 248 PPB_Graphics2D_Impl* PPB_Graphics2D_Impl::AsPPB_Graphics2D_Impl() { |
| 249 return this; |
| 250 } |
| 251 |
| 252 PP_Bool PPB_Graphics2D_Impl::Describe(PP_Size* size, PP_Bool* is_always_opaque)
{ |
247 size->width = image_data_->width(); | 253 size->width = image_data_->width(); |
248 size->height = image_data_->height(); | 254 size->height = image_data_->height(); |
249 *is_always_opaque = PP_FALSE; // TODO(brettw) implement this. | 255 *is_always_opaque = PP_FALSE; // TODO(brettw) implement this. |
250 return PP_TRUE; | 256 return PP_TRUE; |
251 } | 257 } |
252 | 258 |
253 void Graphics2D::PaintImageData(PP_Resource image_data, | 259 void PPB_Graphics2D_Impl::PaintImageData(PP_Resource image_data, |
254 const PP_Point* top_left, | 260 const PP_Point* top_left, |
255 const PP_Rect* src_rect) { | 261 const PP_Rect* src_rect) { |
256 if (!top_left) | 262 if (!top_left) |
257 return; | 263 return; |
258 | 264 |
259 scoped_refptr<ImageData> image_resource( | 265 scoped_refptr<PPB_ImageData_Impl> image_resource( |
260 Resource::GetAs<ImageData>(image_data)); | 266 Resource::GetAs<PPB_ImageData_Impl>(image_data)); |
261 if (!image_resource) | 267 if (!image_resource) |
262 return; | 268 return; |
263 | 269 |
264 QueuedOperation operation(QueuedOperation::PAINT); | 270 QueuedOperation operation(QueuedOperation::PAINT); |
265 operation.paint_image = image_resource; | 271 operation.paint_image = image_resource; |
266 if (!ValidateAndConvertRect(src_rect, image_resource->width(), | 272 if (!ValidateAndConvertRect(src_rect, image_resource->width(), |
267 image_resource->height(), | 273 image_resource->height(), |
268 &operation.paint_src_rect)) | 274 &operation.paint_src_rect)) |
269 return; | 275 return; |
270 | 276 |
271 // Validate the bitmap position using the previously-validated rect, there | 277 // Validate the bitmap position using the previously-validated rect, there |
272 // should be no painted area outside of the image. | 278 // should be no painted area outside of the image. |
273 int64 x64 = static_cast<int64>(top_left->x); | 279 int64 x64 = static_cast<int64>(top_left->x); |
274 int64 y64 = static_cast<int64>(top_left->y); | 280 int64 y64 = static_cast<int64>(top_left->y); |
275 if (x64 + static_cast<int64>(operation.paint_src_rect.x()) < 0 || | 281 if (x64 + static_cast<int64>(operation.paint_src_rect.x()) < 0 || |
276 x64 + static_cast<int64>(operation.paint_src_rect.right()) > | 282 x64 + static_cast<int64>(operation.paint_src_rect.right()) > |
277 image_data_->width()) | 283 image_data_->width()) |
278 return; | 284 return; |
279 if (y64 + static_cast<int64>(operation.paint_src_rect.y()) < 0 || | 285 if (y64 + static_cast<int64>(operation.paint_src_rect.y()) < 0 || |
280 y64 + static_cast<int64>(operation.paint_src_rect.bottom()) > | 286 y64 + static_cast<int64>(operation.paint_src_rect.bottom()) > |
281 image_data_->height()) | 287 image_data_->height()) |
282 return; | 288 return; |
283 operation.paint_x = top_left->x; | 289 operation.paint_x = top_left->x; |
284 operation.paint_y = top_left->y; | 290 operation.paint_y = top_left->y; |
285 | 291 |
286 queued_operations_.push_back(operation); | 292 queued_operations_.push_back(operation); |
287 } | 293 } |
288 | 294 |
289 void Graphics2D::Scroll(const PP_Rect* clip_rect, const PP_Point* amount) { | 295 void PPB_Graphics2D_Impl::Scroll(const PP_Rect* clip_rect, |
| 296 const PP_Point* amount) { |
290 QueuedOperation operation(QueuedOperation::SCROLL); | 297 QueuedOperation operation(QueuedOperation::SCROLL); |
291 if (!ValidateAndConvertRect(clip_rect, | 298 if (!ValidateAndConvertRect(clip_rect, |
292 image_data_->width(), | 299 image_data_->width(), |
293 image_data_->height(), | 300 image_data_->height(), |
294 &operation.scroll_clip_rect)) | 301 &operation.scroll_clip_rect)) |
295 return; | 302 return; |
296 | 303 |
297 // If we're being asked to scroll by more than the clip rect size, just | 304 // If we're being asked to scroll by more than the clip rect size, just |
298 // ignore this scroll command and say it worked. | 305 // ignore this scroll command and say it worked. |
299 int32 dx = amount->x; | 306 int32 dx = amount->x; |
300 int32 dy = amount->y; | 307 int32 dy = amount->y; |
301 if (dx <= -image_data_->width() || dx >= image_data_->width() || | 308 if (dx <= -image_data_->width() || dx >= image_data_->width() || |
302 dy <= -image_data_->height() || dy >= image_data_->height()) | 309 dy <= -image_data_->height() || dy >= image_data_->height()) |
303 return; | 310 return; |
304 | 311 |
305 operation.scroll_dx = dx; | 312 operation.scroll_dx = dx; |
306 operation.scroll_dy = dy; | 313 operation.scroll_dy = dy; |
307 | 314 |
308 queued_operations_.push_back(operation); | 315 queued_operations_.push_back(operation); |
309 } | 316 } |
310 | 317 |
311 void Graphics2D::ReplaceContents(PP_Resource image_data) { | 318 void PPB_Graphics2D_Impl::ReplaceContents(PP_Resource image_data) { |
312 scoped_refptr<ImageData> image_resource( | 319 scoped_refptr<PPB_ImageData_Impl> image_resource( |
313 Resource::GetAs<ImageData>(image_data)); | 320 Resource::GetAs<PPB_ImageData_Impl>(image_data)); |
314 if (!image_resource) | 321 if (!image_resource) |
315 return; | 322 return; |
316 if (!ImageData::IsImageDataFormatSupported(image_resource->format())) | 323 if (!PPB_ImageData_Impl::IsImageDataFormatSupported( |
| 324 image_resource->format())) |
317 return; | 325 return; |
318 | 326 |
319 if (image_resource->width() != image_data_->width() || | 327 if (image_resource->width() != image_data_->width() || |
320 image_resource->height() != image_data_->height()) | 328 image_resource->height() != image_data_->height()) |
321 return; | 329 return; |
322 | 330 |
323 QueuedOperation operation(QueuedOperation::REPLACE); | 331 QueuedOperation operation(QueuedOperation::REPLACE); |
324 operation.replace_image = image_resource; | 332 operation.replace_image = image_resource; |
325 queued_operations_.push_back(operation); | 333 queued_operations_.push_back(operation); |
326 } | 334 } |
327 | 335 |
328 int32_t Graphics2D::Flush(const PP_CompletionCallback& callback) { | 336 int32_t PPB_Graphics2D_Impl::Flush(const PP_CompletionCallback& callback) { |
329 // Don't allow more than one pending flush at a time. | 337 // Don't allow more than one pending flush at a time. |
330 if (HasPendingFlush()) | 338 if (HasPendingFlush()) |
331 return PP_ERROR_INPROGRESS; | 339 return PP_ERROR_INPROGRESS; |
332 | 340 |
333 // TODO(brettw) check that the current thread is not the main one and | 341 // TODO(brettw) check that the current thread is not the main one and |
334 // implement blocking flushes in this case. | 342 // implement blocking flushes in this case. |
335 if (!callback.func) | 343 if (!callback.func) |
336 return PP_ERROR_BADARGUMENT; | 344 return PP_ERROR_BADARGUMENT; |
337 | 345 |
338 bool nothing_visible = true; | 346 bool nothing_visible = true; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 if (nothing_visible) { | 388 if (nothing_visible) { |
381 // There's nothing visible to invalidate so just schedule the callback to | 389 // There's nothing visible to invalidate so just schedule the callback to |
382 // execute in the next round of the message loop. | 390 // execute in the next round of the message loop. |
383 ScheduleOffscreenCallback(FlushCallbackData(callback)); | 391 ScheduleOffscreenCallback(FlushCallbackData(callback)); |
384 } else { | 392 } else { |
385 unpainted_flush_callback_.Set(callback); | 393 unpainted_flush_callback_.Set(callback); |
386 } | 394 } |
387 return PP_ERROR_WOULDBLOCK; | 395 return PP_ERROR_WOULDBLOCK; |
388 } | 396 } |
389 | 397 |
390 bool Graphics2D::ReadImageData(PP_Resource image, | 398 bool PPB_Graphics2D_Impl::ReadImageData(PP_Resource image, |
391 const PP_Point* top_left) { | 399 const PP_Point* top_left) { |
392 // Get and validate the image object to paint into. | 400 // Get and validate the image object to paint into. |
393 scoped_refptr<ImageData> image_resource(Resource::GetAs<ImageData>(image)); | 401 scoped_refptr<PPB_ImageData_Impl> image_resource( |
| 402 Resource::GetAs<PPB_ImageData_Impl>(image)); |
394 if (!image_resource) | 403 if (!image_resource) |
395 return false; | 404 return false; |
396 if (!ImageData::IsImageDataFormatSupported(image_resource->format())) | 405 if (!PPB_ImageData_Impl::IsImageDataFormatSupported( |
| 406 image_resource->format())) |
397 return false; // Must be in the right format. | 407 return false; // Must be in the right format. |
398 | 408 |
399 // Validate the bitmap position. | 409 // Validate the bitmap position. |
400 int x = top_left->x; | 410 int x = top_left->x; |
401 if (x < 0 || | 411 if (x < 0 || |
402 static_cast<int64>(x) + static_cast<int64>(image_resource->width()) > | 412 static_cast<int64>(x) + static_cast<int64>(image_resource->width()) > |
403 image_data_->width()) | 413 image_data_->width()) |
404 return false; | 414 return false; |
405 int y = top_left->y; | 415 int y = top_left->y; |
406 if (y < 0 || | 416 if (y < 0 || |
(...skipping 22 matching lines...) Expand all Loading... |
429 | 439 |
430 // We want to replace the contents of the bitmap rather than blend. | 440 // We want to replace the contents of the bitmap rather than blend. |
431 SkPaint paint; | 441 SkPaint paint; |
432 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 442 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
433 dest_canvas->drawBitmapRect(*image_data_->GetMappedBitmap(), | 443 dest_canvas->drawBitmapRect(*image_data_->GetMappedBitmap(), |
434 &src_irect, dest_rect, &paint); | 444 &src_irect, dest_rect, &paint); |
435 } | 445 } |
436 return true; | 446 return true; |
437 } | 447 } |
438 | 448 |
439 bool Graphics2D::BindToInstance(PluginInstance* new_instance) { | 449 bool PPB_Graphics2D_Impl::BindToInstance(PluginInstance* new_instance) { |
440 if (bound_instance_ == new_instance) | 450 if (bound_instance_ == new_instance) |
441 return true; // Rebinding the same device, nothing to do. | 451 return true; // Rebinding the same device, nothing to do. |
442 if (bound_instance_ && new_instance) | 452 if (bound_instance_ && new_instance) |
443 return false; // Can't change a bound device. | 453 return false; // Can't change a bound device. |
444 | 454 |
445 if (!new_instance) { | 455 if (!new_instance) { |
446 // When the device is detached, we'll not get any more paint callbacks so | 456 // When the device is detached, we'll not get any more paint callbacks so |
447 // we need to clear the list, but we still want to issue any pending | 457 // we need to clear the list, but we still want to issue any pending |
448 // callbacks to the plugin. | 458 // callbacks to the plugin. |
449 if (!unpainted_flush_callback_.is_null()) { | 459 if (!unpainted_flush_callback_.is_null()) { |
(...skipping 13 matching lines...) Expand all Loading... |
463 // actually painted something. By not bothering to schedule an invalidate | 473 // actually painted something. By not bothering to schedule an invalidate |
464 // when an empty device is initially bound, we can save an extra paint for | 474 // when an empty device is initially bound, we can save an extra paint for |
465 // many plugins during the critical page initialization phase. | 475 // many plugins during the critical page initialization phase. |
466 new_instance->InvalidateRect(gfx::Rect()); | 476 new_instance->InvalidateRect(gfx::Rect()); |
467 } | 477 } |
468 | 478 |
469 bound_instance_ = new_instance; | 479 bound_instance_ = new_instance; |
470 return true; | 480 return true; |
471 } | 481 } |
472 | 482 |
473 void Graphics2D::Paint(WebKit::WebCanvas* canvas, | 483 void PPB_Graphics2D_Impl::Paint(WebKit::WebCanvas* canvas, |
474 const gfx::Rect& plugin_rect, | 484 const gfx::Rect& plugin_rect, |
475 const gfx::Rect& paint_rect) { | 485 const gfx::Rect& paint_rect) { |
476 ImageDataAutoMapper auto_mapper(image_data_); | 486 ImageDataAutoMapper auto_mapper(image_data_); |
477 const SkBitmap& backing_bitmap = *image_data_->GetMappedBitmap(); | 487 const SkBitmap& backing_bitmap = *image_data_->GetMappedBitmap(); |
478 | 488 |
479 #if defined(OS_MACOSX) | 489 #if defined(OS_MACOSX) |
480 SkAutoLockPixels lock(backing_bitmap); | 490 SkAutoLockPixels lock(backing_bitmap); |
481 | 491 |
482 base::mac::ScopedCFTypeRef<CGDataProviderRef> data_provider( | 492 base::mac::ScopedCFTypeRef<CGDataProviderRef> data_provider( |
483 CGDataProviderCreateWithData( | 493 CGDataProviderCreateWithData( |
484 NULL, backing_bitmap.getAddr32(0, 0), | 494 NULL, backing_bitmap.getAddr32(0, 0), |
485 backing_bitmap.rowBytes() * backing_bitmap.height(), NULL)); | 495 backing_bitmap.rowBytes() * backing_bitmap.height(), NULL)); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 } | 528 } |
519 | 529 |
520 gfx::Point origin(plugin_rect.origin().x(), plugin_rect.origin().y()); | 530 gfx::Point origin(plugin_rect.origin().x(), plugin_rect.origin().y()); |
521 canvas->drawBitmap(backing_bitmap, | 531 canvas->drawBitmap(backing_bitmap, |
522 SkIntToScalar(plugin_rect.origin().x()), | 532 SkIntToScalar(plugin_rect.origin().x()), |
523 SkIntToScalar(plugin_rect.origin().y()), | 533 SkIntToScalar(plugin_rect.origin().y()), |
524 &paint); | 534 &paint); |
525 #endif | 535 #endif |
526 } | 536 } |
527 | 537 |
528 void Graphics2D::ViewInitiatedPaint() { | 538 void PPB_Graphics2D_Impl::ViewInitiatedPaint() { |
529 // Move any "unpainted" callback to the painted state. See | 539 // Move any "unpainted" callback to the painted state. See |
530 // |unpainted_flush_callback_| in the header for more. | 540 // |unpainted_flush_callback_| in the header for more. |
531 if (!unpainted_flush_callback_.is_null()) { | 541 if (!unpainted_flush_callback_.is_null()) { |
532 DCHECK(painted_flush_callback_.is_null()); | 542 DCHECK(painted_flush_callback_.is_null()); |
533 std::swap(painted_flush_callback_, unpainted_flush_callback_); | 543 std::swap(painted_flush_callback_, unpainted_flush_callback_); |
534 } | 544 } |
535 } | 545 } |
536 | 546 |
537 void Graphics2D::ViewFlushedPaint() { | 547 void PPB_Graphics2D_Impl::ViewFlushedPaint() { |
538 // Notify any "painted" callback. See |unpainted_flush_callback_| in the | 548 // Notify any "painted" callback. See |unpainted_flush_callback_| in the |
539 // header for more. | 549 // header for more. |
540 if (!painted_flush_callback_.is_null()) { | 550 if (!painted_flush_callback_.is_null()) { |
541 // We must clear this variable before issuing the callback. It will be | 551 // We must clear this variable before issuing the callback. It will be |
542 // common for the plugin to issue another invalidate in response to a flush | 552 // common for the plugin to issue another invalidate in response to a flush |
543 // callback, and we don't want to think that a callback is already pending. | 553 // callback, and we don't want to think that a callback is already pending. |
544 FlushCallbackData callback; | 554 FlushCallbackData callback; |
545 std::swap(callback, painted_flush_callback_); | 555 std::swap(callback, painted_flush_callback_); |
546 callback.Execute(PP_OK); | 556 callback.Execute(PP_OK); |
547 } | 557 } |
548 } | 558 } |
549 | 559 |
550 void Graphics2D::ExecutePaintImageData(ImageData* image, | 560 void PPB_Graphics2D_Impl::ExecutePaintImageData(PPB_ImageData_Impl* image, |
551 int x, int y, | 561 int x, int y, |
552 const gfx::Rect& src_rect, | 562 const gfx::Rect& src_rect, |
553 gfx::Rect* invalidated_rect) { | 563 gfx::Rect* invalidated_rect) { |
554 // Ensure the source image is mapped to read from it. | 564 // Ensure the source image is mapped to read from it. |
555 ImageDataAutoMapper auto_mapper(image); | 565 ImageDataAutoMapper auto_mapper(image); |
556 if (!auto_mapper.is_valid()) | 566 if (!auto_mapper.is_valid()) |
557 return; | 567 return; |
558 | 568 |
559 // Portion within the source image to cut out. | 569 // Portion within the source image to cut out. |
560 SkIRect src_irect = { src_rect.x(), src_rect.y(), | 570 SkIRect src_irect = { src_rect.x(), src_rect.y(), |
561 src_rect.right(), src_rect.bottom() }; | 571 src_rect.right(), src_rect.bottom() }; |
562 | 572 |
563 // Location within the backing store to copy to. | 573 // Location within the backing store to copy to. |
(...skipping 12 matching lines...) Expand all Loading... |
576 skia::PlatformCanvas* backing_canvas = image_data_->mapped_canvas(); | 586 skia::PlatformCanvas* backing_canvas = image_data_->mapped_canvas(); |
577 | 587 |
578 // We want to replace the contents of the bitmap rather than blend. | 588 // We want to replace the contents of the bitmap rather than blend. |
579 SkPaint paint; | 589 SkPaint paint; |
580 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 590 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
581 backing_canvas->drawBitmapRect(*image->GetMappedBitmap(), | 591 backing_canvas->drawBitmapRect(*image->GetMappedBitmap(), |
582 &src_irect, dest_rect, &paint); | 592 &src_irect, dest_rect, &paint); |
583 } | 593 } |
584 } | 594 } |
585 | 595 |
586 void Graphics2D::ExecuteScroll(const gfx::Rect& clip, int dx, int dy, | 596 void PPB_Graphics2D_Impl::ExecuteScroll(const gfx::Rect& clip, |
587 gfx::Rect* invalidated_rect) { | 597 int dx, int dy, |
| 598 gfx::Rect* invalidated_rect) { |
588 gfx::ScrollCanvas(image_data_->mapped_canvas(), | 599 gfx::ScrollCanvas(image_data_->mapped_canvas(), |
589 clip, gfx::Point(dx, dy)); | 600 clip, gfx::Point(dx, dy)); |
590 *invalidated_rect = clip; | 601 *invalidated_rect = clip; |
591 } | 602 } |
592 | 603 |
593 void Graphics2D::ExecuteReplaceContents(ImageData* image, | 604 void PPB_Graphics2D_Impl::ExecuteReplaceContents(PPB_ImageData_Impl* image, |
594 gfx::Rect* invalidated_rect) { | 605 gfx::Rect* invalidated_rect) { |
595 if (image->format() != image_data_->format()) { | 606 if (image->format() != image_data_->format()) { |
596 DCHECK(image->width() == image_data_->width() && | 607 DCHECK(image->width() == image_data_->width() && |
597 image->height() == image_data_->height()); | 608 image->height() == image_data_->height()); |
598 // Convert the image data if the format does not match. | 609 // Convert the image data if the format does not match. |
599 SkIRect src_irect = { 0, 0, image->width(), image->height() }; | 610 SkIRect src_irect = { 0, 0, image->width(), image->height() }; |
600 SkRect dest_rect = { SkIntToScalar(0), | 611 SkRect dest_rect = { SkIntToScalar(0), |
601 SkIntToScalar(0), | 612 SkIntToScalar(0), |
602 SkIntToScalar(image_data_->width()), | 613 SkIntToScalar(image_data_->width()), |
603 SkIntToScalar(image_data_->height()) }; | 614 SkIntToScalar(image_data_->height()) }; |
604 ConvertImageData(image, src_irect, image_data_, dest_rect); | 615 ConvertImageData(image, src_irect, image_data_, dest_rect); |
605 } else { | 616 } else { |
606 image_data_->Swap(image); | 617 image_data_->Swap(image); |
607 } | 618 } |
608 *invalidated_rect = gfx::Rect(0, 0, | 619 *invalidated_rect = gfx::Rect(0, 0, |
609 image_data_->width(), image_data_->height()); | 620 image_data_->width(), image_data_->height()); |
610 } | 621 } |
611 | 622 |
612 void Graphics2D::ScheduleOffscreenCallback(const FlushCallbackData& callback) { | 623 void PPB_Graphics2D_Impl::ScheduleOffscreenCallback( |
| 624 const FlushCallbackData& callback) { |
613 DCHECK(!HasPendingFlush()); | 625 DCHECK(!HasPendingFlush()); |
614 offscreen_flush_pending_ = true; | 626 offscreen_flush_pending_ = true; |
615 MessageLoop::current()->PostTask( | 627 MessageLoop::current()->PostTask( |
616 FROM_HERE, | 628 FROM_HERE, |
617 NewRunnableMethod(this, | 629 NewRunnableMethod(this, |
618 &Graphics2D::ExecuteOffscreenCallback, | 630 &PPB_Graphics2D_Impl::ExecuteOffscreenCallback, |
619 callback)); | 631 callback)); |
620 } | 632 } |
621 | 633 |
622 void Graphics2D::ExecuteOffscreenCallback(FlushCallbackData data) { | 634 void PPB_Graphics2D_Impl::ExecuteOffscreenCallback(FlushCallbackData data) { |
623 DCHECK(offscreen_flush_pending_); | 635 DCHECK(offscreen_flush_pending_); |
624 | 636 |
625 // We must clear this flag before issuing the callback. It will be | 637 // We must clear this flag before issuing the callback. It will be |
626 // common for the plugin to issue another invalidate in response to a flush | 638 // common for the plugin to issue another invalidate in response to a flush |
627 // callback, and we don't want to think that a callback is already pending. | 639 // callback, and we don't want to think that a callback is already pending. |
628 offscreen_flush_pending_ = false; | 640 offscreen_flush_pending_ = false; |
629 data.Execute(PP_OK); | 641 data.Execute(PP_OK); |
630 } | 642 } |
631 | 643 |
632 bool Graphics2D::HasPendingFlush() const { | 644 bool PPB_Graphics2D_Impl::HasPendingFlush() const { |
633 return !unpainted_flush_callback_.is_null() || | 645 return !unpainted_flush_callback_.is_null() || |
634 !painted_flush_callback_.is_null() || | 646 !painted_flush_callback_.is_null() || |
635 offscreen_flush_pending_; | 647 offscreen_flush_pending_; |
636 } | 648 } |
637 | 649 |
638 } // namespace pepper | 650 } // namespace ppapi |
| 651 } // namespace webkit |
| 652 |
OLD | NEW |