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 "webkit/plugins/ppapi/ppb_graphics_2d_impl.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 "skia/ext/platform_canvas.h" | 12 #include "skia/ext/platform_canvas.h" |
13 #include "ppapi/c/pp_errors.h" | 13 #include "ppapi/c/pp_errors.h" |
14 #include "ppapi/c/pp_rect.h" | 14 #include "ppapi/c/pp_rect.h" |
15 #include "ppapi/c/pp_resource.h" | 15 #include "ppapi/c/pp_resource.h" |
16 #include "ppapi/c/ppb_graphics_2d.h" | 16 #include "ppapi/c/ppb_graphics_2d.h" |
17 #include "ppapi/cpp/common.h" | 17 #include "ppapi/cpp/common.h" |
| 18 #include "ppapi/thunk/enter.h" |
18 #include "ppapi/thunk/thunk.h" | 19 #include "ppapi/thunk/thunk.h" |
19 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
20 #include "ui/gfx/blit.h" | 21 #include "ui/gfx/blit.h" |
21 #include "ui/gfx/point.h" | 22 #include "ui/gfx/point.h" |
22 #include "ui/gfx/rect.h" | 23 #include "ui/gfx/rect.h" |
23 #include "webkit/plugins/ppapi/common.h" | 24 #include "webkit/plugins/ppapi/common.h" |
24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 25 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
25 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" | 26 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" |
26 | 27 |
27 #if defined(OS_MACOSX) | 28 #if defined(OS_MACOSX) |
28 #include "base/mac/mac_util.h" | 29 #include "base/mac/mac_util.h" |
29 #include "base/mac/scoped_cftyperef.h" | 30 #include "base/mac/scoped_cftyperef.h" |
30 #endif | 31 #endif |
31 | 32 |
| 33 using ppapi::thunk::EnterResourceNoLock; |
| 34 using ppapi::thunk::PPB_ImageData_API; |
| 35 |
32 namespace webkit { | 36 namespace webkit { |
33 namespace ppapi { | 37 namespace ppapi { |
34 | 38 |
35 namespace { | 39 namespace { |
36 | 40 |
37 // Converts a rect inside an image of the given dimensions. The rect may be | 41 // Converts a rect inside an image of the given dimensions. The rect may be |
38 // NULL to indicate it should be the entire image. If the rect is outside of | 42 // NULL to indicate it should be the entire image. If the rect is outside of |
39 // the image, this will do nothing and return false. | 43 // the image, this will do nothing and return false. |
40 bool ValidateAndConvertRect(const PP_Rect* rect, | 44 bool ValidateAndConvertRect(const PP_Rect* rect, |
41 int image_width, int image_height, | 45 int image_width, int image_height, |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 *is_always_opaque = pp::BoolToPPBool(is_always_opaque_); | 196 *is_always_opaque = pp::BoolToPPBool(is_always_opaque_); |
193 return PP_TRUE; | 197 return PP_TRUE; |
194 } | 198 } |
195 | 199 |
196 void PPB_Graphics2D_Impl::PaintImageData(PP_Resource image_data, | 200 void PPB_Graphics2D_Impl::PaintImageData(PP_Resource image_data, |
197 const PP_Point* top_left, | 201 const PP_Point* top_left, |
198 const PP_Rect* src_rect) { | 202 const PP_Rect* src_rect) { |
199 if (!top_left) | 203 if (!top_left) |
200 return; | 204 return; |
201 | 205 |
202 scoped_refptr<PPB_ImageData_Impl> image_resource( | 206 EnterResourceNoLock<PPB_ImageData_API> enter(image_data, true); |
203 Resource::GetAs<PPB_ImageData_Impl>(image_data)); | 207 if (enter.failed()) |
204 if (!image_resource) | |
205 return; | 208 return; |
| 209 PPB_ImageData_Impl* image_resource = |
| 210 static_cast<PPB_ImageData_Impl*>(enter.object()); |
206 | 211 |
207 QueuedOperation operation(QueuedOperation::PAINT); | 212 QueuedOperation operation(QueuedOperation::PAINT); |
208 operation.paint_image = image_resource; | 213 operation.paint_image = image_resource; |
209 if (!ValidateAndConvertRect(src_rect, image_resource->width(), | 214 if (!ValidateAndConvertRect(src_rect, image_resource->width(), |
210 image_resource->height(), | 215 image_resource->height(), |
211 &operation.paint_src_rect)) | 216 &operation.paint_src_rect)) |
212 return; | 217 return; |
213 | 218 |
214 // Validate the bitmap position using the previously-validated rect, there | 219 // Validate the bitmap position using the previously-validated rect, there |
215 // should be no painted area outside of the image. | 220 // should be no painted area outside of the image. |
(...skipping 30 matching lines...) Expand all Loading... |
246 dy <= -image_data_->height() || dy >= image_data_->height()) | 251 dy <= -image_data_->height() || dy >= image_data_->height()) |
247 return; | 252 return; |
248 | 253 |
249 operation.scroll_dx = dx; | 254 operation.scroll_dx = dx; |
250 operation.scroll_dy = dy; | 255 operation.scroll_dy = dy; |
251 | 256 |
252 queued_operations_.push_back(operation); | 257 queued_operations_.push_back(operation); |
253 } | 258 } |
254 | 259 |
255 void PPB_Graphics2D_Impl::ReplaceContents(PP_Resource image_data) { | 260 void PPB_Graphics2D_Impl::ReplaceContents(PP_Resource image_data) { |
256 scoped_refptr<PPB_ImageData_Impl> image_resource( | 261 EnterResourceNoLock<PPB_ImageData_API> enter(image_data, true); |
257 Resource::GetAs<PPB_ImageData_Impl>(image_data)); | 262 if (enter.failed()) |
258 if (!image_resource) | |
259 return; | 263 return; |
| 264 PPB_ImageData_Impl* image_resource = |
| 265 static_cast<PPB_ImageData_Impl*>(enter.object()); |
| 266 |
260 if (!PPB_ImageData_Impl::IsImageDataFormatSupported( | 267 if (!PPB_ImageData_Impl::IsImageDataFormatSupported( |
261 image_resource->format())) | 268 image_resource->format())) |
262 return; | 269 return; |
263 | 270 |
264 if (image_resource->width() != image_data_->width() || | 271 if (image_resource->width() != image_data_->width() || |
265 image_resource->height() != image_data_->height()) | 272 image_resource->height() != image_data_->height()) |
266 return; | 273 return; |
267 | 274 |
268 QueuedOperation operation(QueuedOperation::REPLACE); | 275 QueuedOperation operation(QueuedOperation::REPLACE); |
269 operation.replace_image = image_resource; | 276 operation.replace_image = image_resource; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 ScheduleOffscreenCallback(FlushCallbackData(callback)); | 335 ScheduleOffscreenCallback(FlushCallbackData(callback)); |
329 } else { | 336 } else { |
330 unpainted_flush_callback_.Set(callback); | 337 unpainted_flush_callback_.Set(callback); |
331 } | 338 } |
332 return PP_OK_COMPLETIONPENDING; | 339 return PP_OK_COMPLETIONPENDING; |
333 } | 340 } |
334 | 341 |
335 bool PPB_Graphics2D_Impl::ReadImageData(PP_Resource image, | 342 bool PPB_Graphics2D_Impl::ReadImageData(PP_Resource image, |
336 const PP_Point* top_left) { | 343 const PP_Point* top_left) { |
337 // Get and validate the image object to paint into. | 344 // Get and validate the image object to paint into. |
338 scoped_refptr<PPB_ImageData_Impl> image_resource( | 345 EnterResourceNoLock<PPB_ImageData_API> enter(image, true); |
339 Resource::GetAs<PPB_ImageData_Impl>(image)); | 346 if (enter.failed()) |
340 if (!image_resource) | |
341 return false; | 347 return false; |
| 348 PPB_ImageData_Impl* image_resource = |
| 349 static_cast<PPB_ImageData_Impl*>(enter.object()); |
342 if (!PPB_ImageData_Impl::IsImageDataFormatSupported( | 350 if (!PPB_ImageData_Impl::IsImageDataFormatSupported( |
343 image_resource->format())) | 351 image_resource->format())) |
344 return false; // Must be in the right format. | 352 return false; // Must be in the right format. |
345 | 353 |
346 // Validate the bitmap position. | 354 // Validate the bitmap position. |
347 int x = top_left->x; | 355 int x = top_left->x; |
348 if (x < 0 || | 356 if (x < 0 || |
349 static_cast<int64>(x) + static_cast<int64>(image_resource->width()) > | 357 static_cast<int64>(x) + static_cast<int64>(image_resource->width()) > |
350 image_data_->width()) | 358 image_data_->width()) |
351 return false; | 359 return false; |
(...skipping 10 matching lines...) Expand all Loading... |
362 SkIRect src_irect = { x, y, | 370 SkIRect src_irect = { x, y, |
363 x + image_resource->width(), | 371 x + image_resource->width(), |
364 y + image_resource->height() }; | 372 y + image_resource->height() }; |
365 SkRect dest_rect = { SkIntToScalar(0), | 373 SkRect dest_rect = { SkIntToScalar(0), |
366 SkIntToScalar(0), | 374 SkIntToScalar(0), |
367 SkIntToScalar(image_resource->width()), | 375 SkIntToScalar(image_resource->width()), |
368 SkIntToScalar(image_resource->height()) }; | 376 SkIntToScalar(image_resource->height()) }; |
369 | 377 |
370 if (image_resource->format() != image_data_->format()) { | 378 if (image_resource->format() != image_data_->format()) { |
371 // Convert the image data if the format does not match. | 379 // Convert the image data if the format does not match. |
372 ConvertImageData(image_data_, src_irect, image_resource.get(), dest_rect); | 380 ConvertImageData(image_data_, src_irect, image_resource, dest_rect); |
373 } else { | 381 } else { |
374 skia::PlatformCanvas* dest_canvas = image_resource->mapped_canvas(); | 382 skia::PlatformCanvas* dest_canvas = image_resource->mapped_canvas(); |
375 | 383 |
376 // We want to replace the contents of the bitmap rather than blend. | 384 // We want to replace the contents of the bitmap rather than blend. |
377 SkPaint paint; | 385 SkPaint paint; |
378 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 386 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
379 dest_canvas->drawBitmapRect(*image_data_->GetMappedBitmap(), | 387 dest_canvas->drawBitmapRect(*image_data_->GetMappedBitmap(), |
380 &src_irect, dest_rect, &paint); | 388 &src_irect, dest_rect, &paint); |
381 } | 389 } |
382 return true; | 390 return true; |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 } | 615 } |
608 | 616 |
609 bool PPB_Graphics2D_Impl::HasPendingFlush() const { | 617 bool PPB_Graphics2D_Impl::HasPendingFlush() const { |
610 return !unpainted_flush_callback_.is_null() || | 618 return !unpainted_flush_callback_.is_null() || |
611 !painted_flush_callback_.is_null() || | 619 !painted_flush_callback_.is_null() || |
612 offscreen_flush_pending_; | 620 offscreen_flush_pending_; |
613 } | 621 } |
614 | 622 |
615 } // namespace ppapi | 623 } // namespace ppapi |
616 } // namespace webkit | 624 } // namespace webkit |
OLD | NEW |