Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / pepper / pepper_graphics_2d_host.cc
blob39cf57f0f5a38b9674640448ad96b78e0a5d5c5e
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/pepper/pepper_graphics_2d_host.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/trace_event/trace_event.h"
13 #include "cc/resources/shared_bitmap.h"
14 #include "cc/resources/texture_mailbox.h"
15 #include "content/child/child_shared_bitmap_manager.h"
16 #include "content/public/renderer/render_thread.h"
17 #include "content/public/renderer/renderer_ppapi_host.h"
18 #include "content/renderer/pepper/gfx_conversion.h"
19 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
20 #include "content/renderer/pepper/plugin_instance_throttler_impl.h"
21 #include "content/renderer/pepper/ppb_image_data_impl.h"
22 #include "content/renderer/render_thread_impl.h"
23 #include "ppapi/c/pp_bool.h"
24 #include "ppapi/c/pp_errors.h"
25 #include "ppapi/c/pp_rect.h"
26 #include "ppapi/c/pp_resource.h"
27 #include "ppapi/host/dispatch_host_message.h"
28 #include "ppapi/host/host_message_context.h"
29 #include "ppapi/host/ppapi_host.h"
30 #include "ppapi/proxy/ppapi_messages.h"
31 #include "ppapi/shared_impl/ppb_view_shared.h"
32 #include "ppapi/thunk/enter.h"
33 #include "skia/ext/platform_canvas.h"
34 #include "third_party/skia/include/core/SkBitmap.h"
35 #include "ui/gfx/blit.h"
36 #include "ui/gfx/geometry/point_conversions.h"
37 #include "ui/gfx/geometry/rect.h"
38 #include "ui/gfx/geometry/rect_conversions.h"
39 #include "ui/gfx/geometry/size_conversions.h"
40 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
41 #include "ui/gfx/skia_util.h"
43 #if defined(OS_MACOSX)
44 #include "base/mac/scoped_cftyperef.h"
45 #endif
47 using ppapi::thunk::EnterResourceNoLock;
48 using ppapi::thunk::PPB_ImageData_API;
50 namespace content {
52 namespace {
54 const int64 kOffscreenCallbackDelayMs = 1000 / 30; // 30 fps
56 // Converts a rect inside an image of the given dimensions. The rect may be
57 // NULL to indicate it should be the entire image. If the rect is outside of
58 // the image, this will do nothing and return false.
59 bool ValidateAndConvertRect(const PP_Rect* rect,
60 int image_width,
61 int image_height,
62 gfx::Rect* dest) {
63 if (!rect) {
64 // Use the entire image area.
65 *dest = gfx::Rect(0, 0, image_width, image_height);
66 } else {
67 // Validate the passed-in area.
68 if (rect->point.x < 0 || rect->point.y < 0 || rect->size.width <= 0 ||
69 rect->size.height <= 0)
70 return false;
72 // Check the max bounds, being careful of overflow.
73 if (static_cast<int64>(rect->point.x) +
74 static_cast<int64>(rect->size.width) >
75 static_cast<int64>(image_width))
76 return false;
77 if (static_cast<int64>(rect->point.y) +
78 static_cast<int64>(rect->size.height) >
79 static_cast<int64>(image_height))
80 return false;
82 *dest = gfx::Rect(
83 rect->point.x, rect->point.y, rect->size.width, rect->size.height);
85 return true;
88 // Converts BGRA <-> RGBA.
89 void ConvertBetweenBGRAandRGBA(const uint32_t* input,
90 int pixel_length,
91 uint32_t* output) {
92 for (int i = 0; i < pixel_length; i++) {
93 const unsigned char* pixel_in =
94 reinterpret_cast<const unsigned char*>(&input[i]);
95 unsigned char* pixel_out = reinterpret_cast<unsigned char*>(&output[i]);
96 pixel_out[0] = pixel_in[2];
97 pixel_out[1] = pixel_in[1];
98 pixel_out[2] = pixel_in[0];
99 pixel_out[3] = pixel_in[3];
103 // Converts ImageData from PP_IMAGEDATAFORMAT_BGRA_PREMUL to
104 // PP_IMAGEDATAFORMAT_RGBA_PREMUL, or reverse. It's assumed that the
105 // destination image is always mapped (so will have non-NULL data).
106 void ConvertImageData(PPB_ImageData_Impl* src_image,
107 const SkIRect& src_rect,
108 PPB_ImageData_Impl* dest_image,
109 const SkRect& dest_rect) {
110 ImageDataAutoMapper auto_mapper(src_image);
112 DCHECK(src_image->format() != dest_image->format());
113 DCHECK(PPB_ImageData_Impl::IsImageDataFormatSupported(src_image->format()));
114 DCHECK(PPB_ImageData_Impl::IsImageDataFormatSupported(dest_image->format()));
116 const SkBitmap* src_bitmap = src_image->GetMappedBitmap();
117 const SkBitmap* dest_bitmap = dest_image->GetMappedBitmap();
118 if (src_rect.width() == src_image->width() &&
119 dest_rect.width() == dest_image->width()) {
120 // Fast path if the full line needs to be converted.
121 ConvertBetweenBGRAandRGBA(
122 src_bitmap->getAddr32(static_cast<int>(src_rect.fLeft),
123 static_cast<int>(src_rect.fTop)),
124 src_rect.width() * src_rect.height(),
125 dest_bitmap->getAddr32(static_cast<int>(dest_rect.fLeft),
126 static_cast<int>(dest_rect.fTop)));
127 } else {
128 // Slow path where we convert line by line.
129 for (int y = 0; y < src_rect.height(); y++) {
130 ConvertBetweenBGRAandRGBA(
131 src_bitmap->getAddr32(static_cast<int>(src_rect.fLeft),
132 static_cast<int>(src_rect.fTop + y)),
133 src_rect.width(),
134 dest_bitmap->getAddr32(static_cast<int>(dest_rect.fLeft),
135 static_cast<int>(dest_rect.fTop + y)));
140 } // namespace
142 struct PepperGraphics2DHost::QueuedOperation {
143 enum Type { PAINT, SCROLL, REPLACE, };
145 QueuedOperation(Type t)
146 : type(t), paint_x(0), paint_y(0), scroll_dx(0), scroll_dy(0) {}
148 Type type;
150 // Valid when type == PAINT.
151 scoped_refptr<PPB_ImageData_Impl> paint_image;
152 int paint_x, paint_y;
153 gfx::Rect paint_src_rect;
155 // Valid when type == SCROLL.
156 gfx::Rect scroll_clip_rect;
157 int scroll_dx, scroll_dy;
159 // Valid when type == REPLACE.
160 scoped_refptr<PPB_ImageData_Impl> replace_image;
163 // static
164 PepperGraphics2DHost* PepperGraphics2DHost::Create(
165 RendererPpapiHost* host,
166 PP_Instance instance,
167 PP_Resource resource,
168 const PP_Size& size,
169 PP_Bool is_always_opaque,
170 scoped_refptr<PPB_ImageData_Impl> backing_store) {
171 PepperGraphics2DHost* resource_host =
172 new PepperGraphics2DHost(host, instance, resource);
173 if (!resource_host->Init(size.width,
174 size.height,
175 PP_ToBool(is_always_opaque),
176 backing_store)) {
177 delete resource_host;
178 return NULL;
180 return resource_host;
183 PepperGraphics2DHost::PepperGraphics2DHost(RendererPpapiHost* host,
184 PP_Instance instance,
185 PP_Resource resource)
186 : ResourceHost(host->GetPpapiHost(), instance, resource),
187 renderer_ppapi_host_(host),
188 bound_instance_(NULL),
189 need_flush_ack_(false),
190 offscreen_flush_pending_(false),
191 is_always_opaque_(false),
192 scale_(1.0f),
193 is_running_in_process_(host->IsRunningInProcess()),
194 texture_mailbox_modified_(true) {}
196 PepperGraphics2DHost::~PepperGraphics2DHost() {
197 // Unbind from the instance when destroyed if we're still bound.
198 if (bound_instance_)
199 bound_instance_->BindGraphics(bound_instance_->pp_instance(), 0);
202 bool PepperGraphics2DHost::Init(
203 int width,
204 int height,
205 bool is_always_opaque,
206 scoped_refptr<PPB_ImageData_Impl> backing_store) {
207 // The underlying PPB_ImageData_Impl will validate the dimensions.
208 image_data_ = backing_store;
209 if (!image_data_->Init(PPB_ImageData_Impl::GetNativeImageDataFormat(),
210 width,
211 height,
212 true) ||
213 !image_data_->Map()) {
214 image_data_ = NULL;
215 return false;
217 is_always_opaque_ = is_always_opaque;
218 scale_ = 1.0f;
219 return true;
222 int32_t PepperGraphics2DHost::OnResourceMessageReceived(
223 const IPC::Message& msg,
224 ppapi::host::HostMessageContext* context) {
225 PPAPI_BEGIN_MESSAGE_MAP(PepperGraphics2DHost, msg)
226 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Graphics2D_PaintImageData,
227 OnHostMsgPaintImageData)
228 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Graphics2D_Scroll,
229 OnHostMsgScroll)
230 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Graphics2D_ReplaceContents,
231 OnHostMsgReplaceContents)
232 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_Graphics2D_Flush,
233 OnHostMsgFlush)
234 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Graphics2D_SetScale,
235 OnHostMsgSetScale)
236 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Graphics2D_ReadImageData,
237 OnHostMsgReadImageData)
238 PPAPI_END_MESSAGE_MAP()
239 return PP_ERROR_FAILED;
242 bool PepperGraphics2DHost::IsGraphics2DHost() { return true; }
244 bool PepperGraphics2DHost::ReadImageData(PP_Resource image,
245 const PP_Point* top_left) {
246 // Get and validate the image object to paint into.
247 EnterResourceNoLock<PPB_ImageData_API> enter(image, true);
248 if (enter.failed())
249 return false;
250 PPB_ImageData_Impl* image_resource =
251 static_cast<PPB_ImageData_Impl*>(enter.object());
252 if (!PPB_ImageData_Impl::IsImageDataFormatSupported(image_resource->format()))
253 return false; // Must be in the right format.
255 // Validate the bitmap position.
256 int x = top_left->x;
257 if (x < 0 ||
258 static_cast<int64>(x) + static_cast<int64>(image_resource->width()) >
259 image_data_->width())
260 return false;
261 int y = top_left->y;
262 if (y < 0 ||
263 static_cast<int64>(y) + static_cast<int64>(image_resource->height()) >
264 image_data_->height())
265 return false;
267 ImageDataAutoMapper auto_mapper(image_resource);
268 if (!auto_mapper.is_valid())
269 return false;
271 SkIRect src_irect = {x, y, x + image_resource->width(),
272 y + image_resource->height()};
273 SkRect dest_rect = {SkIntToScalar(0), SkIntToScalar(0),
274 SkIntToScalar(image_resource->width()),
275 SkIntToScalar(image_resource->height())};
277 if (image_resource->format() != image_data_->format()) {
278 // Convert the image data if the format does not match.
279 ConvertImageData(image_data_.get(), src_irect, image_resource, dest_rect);
280 } else {
281 SkCanvas* dest_canvas = image_resource->GetCanvas();
283 // We want to replace the contents of the bitmap rather than blend.
284 SkPaint paint;
285 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
286 dest_canvas->drawBitmapRect(
287 *image_data_->GetMappedBitmap(), src_irect, dest_rect, &paint);
289 return true;
292 bool PepperGraphics2DHost::BindToInstance(
293 PepperPluginInstanceImpl* new_instance) {
294 if (new_instance && new_instance->pp_instance() != pp_instance())
295 return false; // Can't bind other instance's contexts.
296 if (bound_instance_ == new_instance)
297 return true; // Rebinding the same device, nothing to do.
298 if (bound_instance_ && new_instance)
299 return false; // Can't change a bound device.
301 if (!new_instance) {
302 // When the device is detached, we'll not get any more paint callbacks so
303 // we need to clear the list, but we still want to issue any pending
304 // callbacks to the plugin.
305 if (need_flush_ack_)
306 ScheduleOffscreenFlushAck();
307 } else {
308 // Devices being replaced, redraw the plugin.
309 new_instance->InvalidateRect(gfx::Rect());
312 cached_bitmap_.reset();
313 texture_mailbox_modified_ = true;
315 bound_instance_ = new_instance;
316 return true;
319 // The |backing_bitmap| must be clipped to the |plugin_rect| to avoid painting
320 // outside the plugin area. This can happen if the plugin has been resized since
321 // PaintImageData verified the image is within the plugin size.
322 void PepperGraphics2DHost::Paint(blink::WebCanvas* canvas,
323 const gfx::Rect& plugin_rect,
324 const gfx::Rect& paint_rect) {
325 TRACE_EVENT0("pepper", "PepperGraphics2DHost::Paint");
326 ImageDataAutoMapper auto_mapper(image_data_.get());
327 const SkBitmap& backing_bitmap = *image_data_->GetMappedBitmap();
329 gfx::Rect invalidate_rect = plugin_rect;
330 invalidate_rect.Intersect(paint_rect);
331 SkRect sk_invalidate_rect = gfx::RectToSkRect(invalidate_rect);
332 SkAutoCanvasRestore auto_restore(canvas, true);
333 canvas->clipRect(sk_invalidate_rect);
334 gfx::Size pixel_image_size(image_data_->width(), image_data_->height());
335 gfx::Size image_size =
336 gfx::ToFlooredSize(gfx::ScaleSize(pixel_image_size, scale_));
338 PepperPluginInstance* plugin_instance =
339 renderer_ppapi_host_->GetPluginInstance(pp_instance());
340 if (!plugin_instance)
341 return;
342 if (plugin_instance->IsFullPagePlugin()) {
343 // When we're resizing a window with a full-frame plugin, the plugin may
344 // not yet have bound a new device, which will leave parts of the
345 // background exposed if the window is getting larger. We want this to
346 // show white (typically less jarring) rather than black or uninitialized.
347 // We don't do this for non-full-frame plugins since we specifically want
348 // the page background to show through.
349 SkAutoCanvasRestore auto_restore(canvas, true);
350 SkRect image_data_rect =
351 gfx::RectToSkRect(gfx::Rect(plugin_rect.origin(), image_size));
352 canvas->clipRect(image_data_rect, SkRegion::kDifference_Op);
354 SkPaint paint;
355 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
356 paint.setColor(SK_ColorWHITE);
357 canvas->drawRect(sk_invalidate_rect, paint);
360 SkBitmap image;
361 // Copy to device independent bitmap when target canvas doesn't support
362 // platform paint.
363 if (!skia::SupportsPlatformPaint(canvas))
364 backing_bitmap.copyTo(&image, kN32_SkColorType);
365 else
366 image = backing_bitmap;
368 SkPaint paint;
369 if (is_always_opaque_) {
370 // When we know the device is opaque, we can disable blending for slightly
371 // more optimized painting.
372 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
375 SkPoint origin;
376 origin.set(SkIntToScalar(plugin_rect.x()), SkIntToScalar(plugin_rect.y()));
378 SkPoint pixel_origin = origin;
380 if (scale_ != 1.0f && scale_ > 0.0f) {
381 canvas->scale(scale_, scale_);
382 pixel_origin.set(pixel_origin.x() * (1.0f / scale_),
383 pixel_origin.y() * (1.0f / scale_));
385 canvas->drawBitmap(image, pixel_origin.x(), pixel_origin.y(), &paint);
388 void PepperGraphics2DHost::ViewInitiatedPaint() {
389 TRACE_EVENT0("pepper", "PepperGraphics2DHost::ViewInitiatedPaint");
390 if (need_flush_ack_) {
391 SendFlushAck();
392 need_flush_ack_ = false;
396 void PepperGraphics2DHost::SetScale(float scale) { scale_ = scale; }
398 float PepperGraphics2DHost::GetScale() const { return scale_; }
400 bool PepperGraphics2DHost::IsAlwaysOpaque() const { return is_always_opaque_; }
402 PPB_ImageData_Impl* PepperGraphics2DHost::ImageData() {
403 return image_data_.get();
406 gfx::Size PepperGraphics2DHost::Size() const {
407 if (!image_data_.get())
408 return gfx::Size();
409 return gfx::Size(image_data_->width(), image_data_->height());
412 void PepperGraphics2DHost::ClearCache() {
413 cached_bitmap_.reset();
416 int32_t PepperGraphics2DHost::OnHostMsgPaintImageData(
417 ppapi::host::HostMessageContext* context,
418 const ppapi::HostResource& image_data,
419 const PP_Point& top_left,
420 bool src_rect_specified,
421 const PP_Rect& src_rect) {
422 EnterResourceNoLock<PPB_ImageData_API> enter(image_data.host_resource(),
423 true);
424 if (enter.failed())
425 return PP_ERROR_BADRESOURCE;
426 PPB_ImageData_Impl* image_resource =
427 static_cast<PPB_ImageData_Impl*>(enter.object());
429 QueuedOperation operation(QueuedOperation::PAINT);
430 operation.paint_image = image_resource;
431 if (!ValidateAndConvertRect(src_rect_specified ? &src_rect : NULL,
432 image_resource->width(),
433 image_resource->height(),
434 &operation.paint_src_rect))
435 return PP_ERROR_BADARGUMENT;
437 // Validate the bitmap position using the previously-validated rect, there
438 // should be no painted area outside of the image.
439 int64 x64 = static_cast<int64>(top_left.x);
440 int64 y64 = static_cast<int64>(top_left.y);
441 if (x64 + static_cast<int64>(operation.paint_src_rect.x()) < 0 ||
442 x64 + static_cast<int64>(operation.paint_src_rect.right()) >
443 image_data_->width())
444 return PP_ERROR_BADARGUMENT;
445 if (y64 + static_cast<int64>(operation.paint_src_rect.y()) < 0 ||
446 y64 + static_cast<int64>(operation.paint_src_rect.bottom()) >
447 image_data_->height())
448 return PP_ERROR_BADARGUMENT;
449 operation.paint_x = top_left.x;
450 operation.paint_y = top_left.y;
452 queued_operations_.push_back(operation);
453 return PP_OK;
456 int32_t PepperGraphics2DHost::OnHostMsgScroll(
457 ppapi::host::HostMessageContext* context,
458 bool clip_specified,
459 const PP_Rect& clip,
460 const PP_Point& amount) {
461 QueuedOperation operation(QueuedOperation::SCROLL);
462 if (!ValidateAndConvertRect(clip_specified ? &clip : NULL,
463 image_data_->width(),
464 image_data_->height(),
465 &operation.scroll_clip_rect))
466 return PP_ERROR_BADARGUMENT;
468 // If we're being asked to scroll by more than the clip rect size, just
469 // ignore this scroll command and say it worked.
470 int32 dx = amount.x;
471 int32 dy = amount.y;
472 if (dx <= -image_data_->width() || dx >= image_data_->width() ||
473 dy <= -image_data_->height() || dy >= image_data_->height())
474 return PP_ERROR_BADARGUMENT;
476 operation.scroll_dx = dx;
477 operation.scroll_dy = dy;
479 queued_operations_.push_back(operation);
480 return PP_OK;
483 int32_t PepperGraphics2DHost::OnHostMsgReplaceContents(
484 ppapi::host::HostMessageContext* context,
485 const ppapi::HostResource& image_data) {
486 EnterResourceNoLock<PPB_ImageData_API> enter(image_data.host_resource(),
487 true);
488 if (enter.failed())
489 return PP_ERROR_BADRESOURCE;
490 PPB_ImageData_Impl* image_resource =
491 static_cast<PPB_ImageData_Impl*>(enter.object());
493 if (!PPB_ImageData_Impl::IsImageDataFormatSupported(image_resource->format()))
494 return PP_ERROR_BADARGUMENT;
496 if (image_resource->width() != image_data_->width() ||
497 image_resource->height() != image_data_->height())
498 return PP_ERROR_BADARGUMENT;
500 QueuedOperation operation(QueuedOperation::REPLACE);
501 operation.replace_image = image_resource;
502 queued_operations_.push_back(operation);
503 return PP_OK;
506 int32_t PepperGraphics2DHost::OnHostMsgFlush(
507 ppapi::host::HostMessageContext* context) {
508 // Don't allow more than one pending flush at a time.
509 if (HasPendingFlush())
510 return PP_ERROR_INPROGRESS;
512 PP_Resource old_image_data = 0;
513 flush_reply_context_ = context->MakeReplyMessageContext();
514 if (is_running_in_process_)
515 return Flush(NULL);
517 // Reuse image data when running out of process.
518 int32_t result = Flush(&old_image_data);
520 if (old_image_data) {
521 // If the Graphics2D has an old image data it's not using any more, send
522 // it back to the plugin for possible re-use. See ppb_image_data_proxy.cc
523 // for a description how this process works.
524 ppapi::HostResource old_image_data_host_resource;
525 old_image_data_host_resource.SetHostResource(pp_instance(), old_image_data);
526 host()->Send(new PpapiMsg_PPBImageData_NotifyUnusedImageData(
527 ppapi::API_ID_PPB_IMAGE_DATA, old_image_data_host_resource));
530 return result;
533 int32_t PepperGraphics2DHost::OnHostMsgSetScale(
534 ppapi::host::HostMessageContext* context,
535 float scale) {
536 if (scale > 0.0f) {
537 scale_ = scale;
538 return PP_OK;
540 return PP_ERROR_BADARGUMENT;
543 int32_t PepperGraphics2DHost::OnHostMsgReadImageData(
544 ppapi::host::HostMessageContext* context,
545 PP_Resource image,
546 const PP_Point& top_left) {
547 context->reply_msg = PpapiPluginMsg_Graphics2D_ReadImageDataAck();
548 return ReadImageData(image, &top_left) ? PP_OK : PP_ERROR_FAILED;
551 void PepperGraphics2DHost::ReleaseCallback(scoped_ptr<cc::SharedBitmap> bitmap,
552 const gfx::Size& bitmap_size,
553 uint32 sync_point,
554 bool lost_resource) {
555 cached_bitmap_.reset();
556 // Only keep around a cached bitmap if the plugin is currently drawing (has
557 // need_flush_ack_ set).
558 if (need_flush_ack_ && bound_instance_)
559 cached_bitmap_ = bitmap.Pass();
560 cached_bitmap_size_ = bitmap_size;
563 bool PepperGraphics2DHost::PrepareTextureMailbox(
564 cc::TextureMailbox* mailbox,
565 scoped_ptr<cc::SingleReleaseCallback>* release_callback) {
566 if (!texture_mailbox_modified_)
567 return false;
568 // TODO(jbauman): Send image_data_ through mailbox to avoid copy.
569 gfx::Size pixel_image_size(image_data_->width(), image_data_->height());
570 scoped_ptr<cc::SharedBitmap> shared_bitmap;
571 if (cached_bitmap_) {
572 if (cached_bitmap_size_ == pixel_image_size)
573 shared_bitmap = cached_bitmap_.Pass();
574 else
575 cached_bitmap_.reset();
577 if (!shared_bitmap) {
578 shared_bitmap = RenderThreadImpl::current()
579 ->shared_bitmap_manager()
580 ->AllocateSharedBitmap(pixel_image_size);
582 if (!shared_bitmap)
583 return false;
584 void* src = image_data_->Map();
585 memcpy(shared_bitmap->pixels(),
586 src,
587 cc::SharedBitmap::CheckedSizeInBytes(pixel_image_size));
588 image_data_->Unmap();
590 *mailbox = cc::TextureMailbox(shared_bitmap.get(), pixel_image_size);
591 *release_callback = cc::SingleReleaseCallback::Create(
592 base::Bind(&PepperGraphics2DHost::ReleaseCallback,
593 this->AsWeakPtr(),
594 base::Passed(&shared_bitmap),
595 pixel_image_size));
596 texture_mailbox_modified_ = false;
597 return true;
600 void PepperGraphics2DHost::AttachedToNewLayer() {
601 texture_mailbox_modified_ = true;
604 int32_t PepperGraphics2DHost::Flush(PP_Resource* old_image_data) {
605 bool done_replace_contents = false;
606 bool no_update_visible = true;
607 bool is_plugin_visible = true;
608 for (size_t i = 0; i < queued_operations_.size(); i++) {
609 QueuedOperation& operation = queued_operations_[i];
610 gfx::Rect op_rect;
611 switch (operation.type) {
612 case QueuedOperation::PAINT:
613 ExecutePaintImageData(operation.paint_image.get(),
614 operation.paint_x,
615 operation.paint_y,
616 operation.paint_src_rect,
617 &op_rect);
618 break;
619 case QueuedOperation::SCROLL:
620 ExecuteScroll(operation.scroll_clip_rect,
621 operation.scroll_dx,
622 operation.scroll_dy,
623 &op_rect);
624 break;
625 case QueuedOperation::REPLACE:
626 // Since the out parameter |old_image_data| takes ownership of the
627 // reference, if there are more than one ReplaceContents calls queued
628 // the first |old_image_data| will get overwritten and leaked. So we
629 // only supply this for the first call.
630 ExecuteReplaceContents(operation.replace_image.get(),
631 &op_rect,
632 done_replace_contents ? NULL : old_image_data);
633 done_replace_contents = true;
634 break;
637 // For correctness with accelerated compositing, we must issue an invalidate
638 // on the full op_rect even if it is partially or completely off-screen.
639 // However, if we issue an invalidate for a clipped-out region, WebKit will
640 // do nothing and we won't get any ViewFlushedPaint calls, leaving our
641 // callback stranded. So we still need to check whether the repainted area
642 // is visible to determine how to deal with the callback.
643 if (bound_instance_ && !op_rect.IsEmpty()) {
644 gfx::Point scroll_delta(operation.scroll_dx, operation.scroll_dy);
645 if (!ConvertToLogicalPixels(scale_,
646 &op_rect,
647 operation.type == QueuedOperation::SCROLL
648 ? &scroll_delta
649 : NULL)) {
650 // Conversion requires falling back to InvalidateRect.
651 operation.type = QueuedOperation::PAINT;
654 gfx::Rect clip = PP_ToGfxRect(bound_instance_->view_data().clip_rect);
655 is_plugin_visible = !clip.IsEmpty();
657 // Set |no_update_visible| to false if the change overlaps the visible
658 // area.
659 if (!gfx::IntersectRects(clip, op_rect).IsEmpty()) {
660 no_update_visible = false;
663 // Notify the plugin of the entire change (op_rect), even if it is
664 // partially or completely off-screen.
665 if (operation.type == QueuedOperation::SCROLL) {
666 bound_instance_->ScrollRect(
667 scroll_delta.x(), scroll_delta.y(), op_rect);
668 } else {
669 if (!op_rect.IsEmpty())
670 bound_instance_->InvalidateRect(op_rect);
672 texture_mailbox_modified_ = true;
675 queued_operations_.clear();
677 if (!bound_instance_) {
678 // As promised in the API, we always schedule callback when unbound.
679 ScheduleOffscreenFlushAck();
680 } else if (no_update_visible && is_plugin_visible &&
681 bound_instance_->view_data().is_page_visible) {
682 // There's nothing visible to invalidate so just schedule the callback to
683 // execute in the next round of the message loop.
684 ScheduleOffscreenFlushAck();
685 } else {
686 need_flush_ack_ = true;
689 if (bound_instance_ && bound_instance_->throttler() &&
690 bound_instance_->throttler()->needs_representative_keyframe()) {
691 bound_instance_->throttler()->OnImageFlush(image_data_->GetMappedBitmap());
694 return PP_OK_COMPLETIONPENDING;
697 void PepperGraphics2DHost::ExecutePaintImageData(PPB_ImageData_Impl* image,
698 int x,
699 int y,
700 const gfx::Rect& src_rect,
701 gfx::Rect* invalidated_rect) {
702 // Ensure the source image is mapped to read from it.
703 ImageDataAutoMapper auto_mapper(image);
704 if (!auto_mapper.is_valid())
705 return;
707 // Portion within the source image to cut out.
708 SkIRect src_irect = {src_rect.x(), src_rect.y(), src_rect.right(),
709 src_rect.bottom()};
711 // Location within the backing store to copy to.
712 *invalidated_rect = src_rect;
713 invalidated_rect->Offset(x, y);
714 SkRect dest_rect = {SkIntToScalar(invalidated_rect->x()),
715 SkIntToScalar(invalidated_rect->y()),
716 SkIntToScalar(invalidated_rect->right()),
717 SkIntToScalar(invalidated_rect->bottom())};
719 if (image->format() != image_data_->format()) {
720 // Convert the image data if the format does not match.
721 ConvertImageData(image, src_irect, image_data_.get(), dest_rect);
722 } else {
723 // We're guaranteed to have a mapped canvas since we mapped it in Init().
724 SkCanvas* backing_canvas = image_data_->GetCanvas();
726 // We want to replace the contents of the bitmap rather than blend.
727 SkPaint paint;
728 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
729 backing_canvas->drawBitmapRect(
730 *image->GetMappedBitmap(), src_irect, dest_rect, &paint);
734 void PepperGraphics2DHost::ExecuteScroll(const gfx::Rect& clip,
735 int dx,
736 int dy,
737 gfx::Rect* invalidated_rect) {
738 gfx::ScrollCanvas(image_data_->GetCanvas(), clip, gfx::Vector2d(dx, dy));
739 *invalidated_rect = clip;
742 void PepperGraphics2DHost::ExecuteReplaceContents(PPB_ImageData_Impl* image,
743 gfx::Rect* invalidated_rect,
744 PP_Resource* old_image_data) {
745 if (image->format() != image_data_->format()) {
746 DCHECK(image->width() == image_data_->width() &&
747 image->height() == image_data_->height());
748 // Convert the image data if the format does not match.
749 SkIRect src_irect = {0, 0, image->width(), image->height()};
750 SkRect dest_rect = {SkIntToScalar(0), SkIntToScalar(0),
751 SkIntToScalar(image_data_->width()),
752 SkIntToScalar(image_data_->height())};
753 ConvertImageData(image, src_irect, image_data_.get(), dest_rect);
754 } else {
755 // The passed-in image may not be mapped in our process, and we need to
756 // guarantee that the current backing store is always mapped.
757 if (!image->Map())
758 return;
760 if (old_image_data)
761 *old_image_data = image_data_->GetReference();
762 image_data_ = image;
764 *invalidated_rect =
765 gfx::Rect(0, 0, image_data_->width(), image_data_->height());
768 void PepperGraphics2DHost::SendFlushAck() {
769 host()->SendReply(flush_reply_context_, PpapiPluginMsg_Graphics2D_FlushAck());
772 void PepperGraphics2DHost::SendOffscreenFlushAck() {
773 DCHECK(offscreen_flush_pending_);
775 // We must clear this flag before issuing the callback. It will be
776 // common for the plugin to issue another invalidate in response to a flush
777 // callback, and we don't want to think that a callback is already pending.
778 offscreen_flush_pending_ = false;
779 SendFlushAck();
782 void PepperGraphics2DHost::ScheduleOffscreenFlushAck() {
783 offscreen_flush_pending_ = true;
784 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
785 FROM_HERE,
786 base::Bind(&PepperGraphics2DHost::SendOffscreenFlushAck, AsWeakPtr()),
787 base::TimeDelta::FromMilliseconds(kOffscreenCallbackDelayMs));
790 bool PepperGraphics2DHost::HasPendingFlush() const {
791 return need_flush_ack_ || offscreen_flush_pending_;
794 // static
795 bool PepperGraphics2DHost::ConvertToLogicalPixels(float scale,
796 gfx::Rect* op_rect,
797 gfx::Point* delta) {
798 if (scale == 1.0f || scale <= 0.0f)
799 return true;
801 gfx::Rect original_rect = *op_rect;
802 // Take the enclosing rectangle after scaling so a rectangle scaled down then
803 // scaled back up by the inverse scale would fully contain the entire area
804 // affected by the original rectangle.
805 *op_rect = gfx::ScaleToEnclosingRect(*op_rect, scale);
806 if (delta) {
807 gfx::Point original_delta = *delta;
808 float inverse_scale = 1.0f / scale;
809 *delta = gfx::ToFlooredPoint(gfx::ScalePoint(*delta, scale));
811 gfx::Rect inverse_scaled_rect =
812 gfx::ScaleToEnclosingRect(*op_rect, inverse_scale);
813 if (original_rect != inverse_scaled_rect)
814 return false;
815 gfx::Point inverse_scaled_point =
816 gfx::ToFlooredPoint(gfx::ScalePoint(*delta, inverse_scale));
817 if (original_delta != inverse_scaled_point)
818 return false;
821 return true;
824 } // namespace content