Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(727)

Side by Side Diff: content/renderer/pepper/pepper_graphics_2d_host.cc

Issue 225903006: PPAPI: Run clang_format.py on content/renderer/pepper (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "content/renderer/pepper/pepper_graphics_2d_host.h" 5 #include "content/renderer/pepper/pepper_graphics_2d_host.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/debug/trace_event.h" 8 #include "base/debug/trace_event.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 namespace content { 46 namespace content {
47 47
48 namespace { 48 namespace {
49 49
50 const int64 kOffscreenCallbackDelayMs = 1000 / 30; // 30 fps 50 const int64 kOffscreenCallbackDelayMs = 1000 / 30; // 30 fps
51 51
52 // Converts a rect inside an image of the given dimensions. The rect may be 52 // Converts a rect inside an image of the given dimensions. The rect may be
53 // NULL to indicate it should be the entire image. If the rect is outside of 53 // NULL to indicate it should be the entire image. If the rect is outside of
54 // the image, this will do nothing and return false. 54 // the image, this will do nothing and return false.
55 bool ValidateAndConvertRect(const PP_Rect* rect, 55 bool ValidateAndConvertRect(const PP_Rect* rect,
56 int image_width, int image_height, 56 int image_width,
57 int image_height,
57 gfx::Rect* dest) { 58 gfx::Rect* dest) {
58 if (!rect) { 59 if (!rect) {
59 // Use the entire image area. 60 // Use the entire image area.
60 *dest = gfx::Rect(0, 0, image_width, image_height); 61 *dest = gfx::Rect(0, 0, image_width, image_height);
61 } else { 62 } else {
62 // Validate the passed-in area. 63 // Validate the passed-in area.
63 if (rect->point.x < 0 || rect->point.y < 0 || 64 if (rect->point.x < 0 || rect->point.y < 0 || rect->size.width <= 0 ||
64 rect->size.width <= 0 || rect->size.height <= 0) 65 rect->size.height <= 0)
65 return false; 66 return false;
66 67
67 // Check the max bounds, being careful of overflow. 68 // Check the max bounds, being careful of overflow.
68 if (static_cast<int64>(rect->point.x) + 69 if (static_cast<int64>(rect->point.x) +
69 static_cast<int64>(rect->size.width) > 70 static_cast<int64>(rect->size.width) >
70 static_cast<int64>(image_width)) 71 static_cast<int64>(image_width))
71 return false; 72 return false;
72 if (static_cast<int64>(rect->point.y) + 73 if (static_cast<int64>(rect->point.y) +
73 static_cast<int64>(rect->size.height) > 74 static_cast<int64>(rect->size.height) >
74 static_cast<int64>(image_height)) 75 static_cast<int64>(image_height))
75 return false; 76 return false;
76 77
77 *dest = gfx::Rect(rect->point.x, rect->point.y, 78 *dest = gfx::Rect(
78 rect->size.width, rect->size.height); 79 rect->point.x, rect->point.y, rect->size.width, rect->size.height);
79 } 80 }
80 return true; 81 return true;
81 } 82 }
82 83
83 // Converts BGRA <-> RGBA. 84 // Converts BGRA <-> RGBA.
84 void ConvertBetweenBGRAandRGBA(const uint32_t* input, 85 void ConvertBetweenBGRAandRGBA(const uint32_t* input,
85 int pixel_length, 86 int pixel_length,
86 uint32_t* output) { 87 uint32_t* output) {
87 for (int i = 0; i < pixel_length; i++) { 88 for (int i = 0; i < pixel_length; i++) {
88 const unsigned char* pixel_in = 89 const unsigned char* pixel_in =
89 reinterpret_cast<const unsigned char*>(&input[i]); 90 reinterpret_cast<const unsigned char*>(&input[i]);
90 unsigned char* pixel_out = reinterpret_cast<unsigned char*>(&output[i]); 91 unsigned char* pixel_out = reinterpret_cast<unsigned char*>(&output[i]);
91 pixel_out[0] = pixel_in[2]; 92 pixel_out[0] = pixel_in[2];
92 pixel_out[1] = pixel_in[1]; 93 pixel_out[1] = pixel_in[1];
93 pixel_out[2] = pixel_in[0]; 94 pixel_out[2] = pixel_in[0];
94 pixel_out[3] = pixel_in[3]; 95 pixel_out[3] = pixel_in[3];
95 } 96 }
96 } 97 }
97 98
98 // Converts ImageData from PP_IMAGEDATAFORMAT_BGRA_PREMUL to 99 // Converts ImageData from PP_IMAGEDATAFORMAT_BGRA_PREMUL to
99 // PP_IMAGEDATAFORMAT_RGBA_PREMUL, or reverse. It's assumed that the 100 // PP_IMAGEDATAFORMAT_RGBA_PREMUL, or reverse. It's assumed that the
100 // destination image is always mapped (so will have non-NULL data). 101 // destination image is always mapped (so will have non-NULL data).
101 void ConvertImageData(PPB_ImageData_Impl* src_image, const SkIRect& src_rect, 102 void ConvertImageData(PPB_ImageData_Impl* src_image,
102 PPB_ImageData_Impl* dest_image, const SkRect& dest_rect) { 103 const SkIRect& src_rect,
104 PPB_ImageData_Impl* dest_image,
105 const SkRect& dest_rect) {
103 ImageDataAutoMapper auto_mapper(src_image); 106 ImageDataAutoMapper auto_mapper(src_image);
104 107
105 DCHECK(src_image->format() != dest_image->format()); 108 DCHECK(src_image->format() != dest_image->format());
106 DCHECK(PPB_ImageData_Impl::IsImageDataFormatSupported(src_image->format())); 109 DCHECK(PPB_ImageData_Impl::IsImageDataFormatSupported(src_image->format()));
107 DCHECK(PPB_ImageData_Impl::IsImageDataFormatSupported(dest_image->format())); 110 DCHECK(PPB_ImageData_Impl::IsImageDataFormatSupported(dest_image->format()));
108 111
109 const SkBitmap* src_bitmap = src_image->GetMappedBitmap(); 112 const SkBitmap* src_bitmap = src_image->GetMappedBitmap();
110 const SkBitmap* dest_bitmap = dest_image->GetMappedBitmap(); 113 const SkBitmap* dest_bitmap = dest_image->GetMappedBitmap();
111 if (src_rect.width() == src_image->width() && 114 if (src_rect.width() == src_image->width() &&
112 dest_rect.width() == dest_image->width()) { 115 dest_rect.width() == dest_image->width()) {
(...skipping 13 matching lines...) Expand all
126 src_rect.width(), 129 src_rect.width(),
127 dest_bitmap->getAddr32(static_cast<int>(dest_rect.fLeft), 130 dest_bitmap->getAddr32(static_cast<int>(dest_rect.fLeft),
128 static_cast<int>(dest_rect.fTop + y))); 131 static_cast<int>(dest_rect.fTop + y)));
129 } 132 }
130 } 133 }
131 } 134 }
132 135
133 } // namespace 136 } // namespace
134 137
135 struct PepperGraphics2DHost::QueuedOperation { 138 struct PepperGraphics2DHost::QueuedOperation {
136 enum Type { 139 enum Type { PAINT, SCROLL, REPLACE, };
137 PAINT,
138 SCROLL,
139 REPLACE,
140 };
141 140
142 QueuedOperation(Type t) 141 QueuedOperation(Type t)
143 : type(t), 142 : type(t), paint_x(0), paint_y(0), scroll_dx(0), scroll_dy(0) {}
144 paint_x(0),
145 paint_y(0),
146 scroll_dx(0),
147 scroll_dy(0) {
148 }
149 143
150 Type type; 144 Type type;
151 145
152 // Valid when type == PAINT. 146 // Valid when type == PAINT.
153 scoped_refptr<PPB_ImageData_Impl> paint_image; 147 scoped_refptr<PPB_ImageData_Impl> paint_image;
154 int paint_x, paint_y; 148 int paint_x, paint_y;
155 gfx::Rect paint_src_rect; 149 gfx::Rect paint_src_rect;
156 150
157 // Valid when type == SCROLL. 151 // Valid when type == SCROLL.
158 gfx::Rect scroll_clip_rect; 152 gfx::Rect scroll_clip_rect;
159 int scroll_dx, scroll_dy; 153 int scroll_dx, scroll_dy;
160 154
161 // Valid when type == REPLACE. 155 // Valid when type == REPLACE.
162 scoped_refptr<PPB_ImageData_Impl> replace_image; 156 scoped_refptr<PPB_ImageData_Impl> replace_image;
163 }; 157 };
164 158
165 // static 159 // static
166 PepperGraphics2DHost* PepperGraphics2DHost::Create( 160 PepperGraphics2DHost* PepperGraphics2DHost::Create(
167 RendererPpapiHost* host, 161 RendererPpapiHost* host,
168 PP_Instance instance, 162 PP_Instance instance,
169 PP_Resource resource, 163 PP_Resource resource,
170 const PP_Size& size, 164 const PP_Size& size,
171 PP_Bool is_always_opaque, 165 PP_Bool is_always_opaque,
172 scoped_refptr<PPB_ImageData_Impl> backing_store) { 166 scoped_refptr<PPB_ImageData_Impl> backing_store) {
173 PepperGraphics2DHost* resource_host = 167 PepperGraphics2DHost* resource_host =
174 new PepperGraphics2DHost(host, instance, resource); 168 new PepperGraphics2DHost(host, instance, resource);
175 if (!resource_host->Init(size.width, size.height, 169 if (!resource_host->Init(size.width,
170 size.height,
176 PP_ToBool(is_always_opaque), 171 PP_ToBool(is_always_opaque),
177 backing_store)) { 172 backing_store)) {
178 delete resource_host; 173 delete resource_host;
179 return NULL; 174 return NULL;
180 } 175 }
181 return resource_host; 176 return resource_host;
182 } 177 }
183 178
184 PepperGraphics2DHost::PepperGraphics2DHost(RendererPpapiHost* host, 179 PepperGraphics2DHost::PepperGraphics2DHost(RendererPpapiHost* host,
185 PP_Instance instance, 180 PP_Instance instance,
(...skipping 15 matching lines...) Expand all
201 } 196 }
202 197
203 bool PepperGraphics2DHost::Init( 198 bool PepperGraphics2DHost::Init(
204 int width, 199 int width,
205 int height, 200 int height,
206 bool is_always_opaque, 201 bool is_always_opaque,
207 scoped_refptr<PPB_ImageData_Impl> backing_store) { 202 scoped_refptr<PPB_ImageData_Impl> backing_store) {
208 // The underlying PPB_ImageData_Impl will validate the dimensions. 203 // The underlying PPB_ImageData_Impl will validate the dimensions.
209 image_data_ = backing_store; 204 image_data_ = backing_store;
210 if (!image_data_->Init(PPB_ImageData_Impl::GetNativeImageDataFormat(), 205 if (!image_data_->Init(PPB_ImageData_Impl::GetNativeImageDataFormat(),
211 width, height, true) || 206 width,
207 height,
208 true) ||
212 !image_data_->Map()) { 209 !image_data_->Map()) {
213 image_data_ = NULL; 210 image_data_ = NULL;
214 return false; 211 return false;
215 } 212 }
216 is_always_opaque_ = is_always_opaque; 213 is_always_opaque_ = is_always_opaque;
217 scale_ = 1.0f; 214 scale_ = 1.0f;
218 return true; 215 return true;
219 } 216 }
220 217
221 int32_t PepperGraphics2DHost::OnResourceMessageReceived( 218 int32_t PepperGraphics2DHost::OnResourceMessageReceived(
222 const IPC::Message& msg, 219 const IPC::Message& msg,
223 ppapi::host::HostMessageContext* context) { 220 ppapi::host::HostMessageContext* context) {
224 IPC_BEGIN_MESSAGE_MAP(PepperGraphics2DHost, msg) 221 IPC_BEGIN_MESSAGE_MAP(PepperGraphics2DHost, msg)
225 PPAPI_DISPATCH_HOST_RESOURCE_CALL( 222 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Graphics2D_PaintImageData,
226 PpapiHostMsg_Graphics2D_PaintImageData, 223 OnHostMsgPaintImageData)
227 OnHostMsgPaintImageData) 224 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Graphics2D_Scroll,
228 PPAPI_DISPATCH_HOST_RESOURCE_CALL( 225 OnHostMsgScroll)
229 PpapiHostMsg_Graphics2D_Scroll, 226 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Graphics2D_ReplaceContents,
230 OnHostMsgScroll) 227 OnHostMsgReplaceContents)
231 PPAPI_DISPATCH_HOST_RESOURCE_CALL( 228 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_Graphics2D_Flush,
232 PpapiHostMsg_Graphics2D_ReplaceContents, 229 OnHostMsgFlush)
233 OnHostMsgReplaceContents) 230 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Graphics2D_SetScale,
234 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( 231 OnHostMsgSetScale)
235 PpapiHostMsg_Graphics2D_Flush, 232 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Graphics2D_ReadImageData,
236 OnHostMsgFlush) 233 OnHostMsgReadImageData)
237 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
238 PpapiHostMsg_Graphics2D_SetScale,
239 OnHostMsgSetScale)
240 PPAPI_DISPATCH_HOST_RESOURCE_CALL(
241 PpapiHostMsg_Graphics2D_ReadImageData,
242 OnHostMsgReadImageData)
243 IPC_END_MESSAGE_MAP() 234 IPC_END_MESSAGE_MAP()
244 return PP_ERROR_FAILED; 235 return PP_ERROR_FAILED;
245 } 236 }
246 237
247 bool PepperGraphics2DHost::IsGraphics2DHost() { 238 bool PepperGraphics2DHost::IsGraphics2DHost() { return true; }
248 return true;
249 }
250 239
251 bool PepperGraphics2DHost::ReadImageData(PP_Resource image, 240 bool PepperGraphics2DHost::ReadImageData(PP_Resource image,
252 const PP_Point* top_left) { 241 const PP_Point* top_left) {
253 // Get and validate the image object to paint into. 242 // Get and validate the image object to paint into.
254 EnterResourceNoLock<PPB_ImageData_API> enter(image, true); 243 EnterResourceNoLock<PPB_ImageData_API> enter(image, true);
255 if (enter.failed()) 244 if (enter.failed())
256 return false; 245 return false;
257 PPB_ImageData_Impl* image_resource = 246 PPB_ImageData_Impl* image_resource =
258 static_cast<PPB_ImageData_Impl*>(enter.object()); 247 static_cast<PPB_ImageData_Impl*>(enter.object());
259 if (!PPB_ImageData_Impl::IsImageDataFormatSupported( 248 if (!PPB_ImageData_Impl::IsImageDataFormatSupported(image_resource->format()))
260 image_resource->format()))
261 return false; // Must be in the right format. 249 return false; // Must be in the right format.
262 250
263 // Validate the bitmap position. 251 // Validate the bitmap position.
264 int x = top_left->x; 252 int x = top_left->x;
265 if (x < 0 || 253 if (x < 0 ||
266 static_cast<int64>(x) + static_cast<int64>(image_resource->width()) > 254 static_cast<int64>(x) + static_cast<int64>(image_resource->width()) >
267 image_data_->width()) 255 image_data_->width())
268 return false; 256 return false;
269 int y = top_left->y; 257 int y = top_left->y;
270 if (y < 0 || 258 if (y < 0 ||
271 static_cast<int64>(y) + static_cast<int64>(image_resource->height()) > 259 static_cast<int64>(y) + static_cast<int64>(image_resource->height()) >
272 image_data_->height()) 260 image_data_->height())
273 return false; 261 return false;
274 262
275 ImageDataAutoMapper auto_mapper(image_resource); 263 ImageDataAutoMapper auto_mapper(image_resource);
276 if (!auto_mapper.is_valid()) 264 if (!auto_mapper.is_valid())
277 return false; 265 return false;
278 266
279 SkIRect src_irect = { x, y, 267 SkIRect src_irect = {x, y, x + image_resource->width(),
280 x + image_resource->width(), 268 y + image_resource->height()};
281 y + image_resource->height() }; 269 SkRect dest_rect = {SkIntToScalar(0), SkIntToScalar(0),
282 SkRect dest_rect = { SkIntToScalar(0), 270 SkIntToScalar(image_resource->width()),
283 SkIntToScalar(0), 271 SkIntToScalar(image_resource->height())};
284 SkIntToScalar(image_resource->width()),
285 SkIntToScalar(image_resource->height()) };
286 272
287 if (image_resource->format() != image_data_->format()) { 273 if (image_resource->format() != image_data_->format()) {
288 // Convert the image data if the format does not match. 274 // Convert the image data if the format does not match.
289 ConvertImageData(image_data_.get(), src_irect, image_resource, dest_rect); 275 ConvertImageData(image_data_.get(), src_irect, image_resource, dest_rect);
290 } else { 276 } else {
291 SkCanvas* dest_canvas = image_resource->GetCanvas(); 277 SkCanvas* dest_canvas = image_resource->GetCanvas();
292 278
293 // We want to replace the contents of the bitmap rather than blend. 279 // We want to replace the contents of the bitmap rather than blend.
294 SkPaint paint; 280 SkPaint paint;
295 paint.setXfermodeMode(SkXfermode::kSrc_Mode); 281 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
296 dest_canvas->drawBitmapRect(*image_data_->GetMappedBitmap(), 282 dest_canvas->drawBitmapRect(
297 &src_irect, dest_rect, &paint); 283 *image_data_->GetMappedBitmap(), &src_irect, dest_rect, &paint);
298 } 284 }
299 return true; 285 return true;
300 } 286 }
301 287
302 bool PepperGraphics2DHost::BindToInstance( 288 bool PepperGraphics2DHost::BindToInstance(
303 PepperPluginInstanceImpl* new_instance) { 289 PepperPluginInstanceImpl* new_instance) {
304 if (new_instance && new_instance->pp_instance() != pp_instance()) 290 if (new_instance && new_instance->pp_instance() != pp_instance())
305 return false; // Can't bind other instance's contexts. 291 return false; // Can't bind other instance's contexts.
306 if (bound_instance_ == new_instance) 292 if (bound_instance_ == new_instance)
307 return true; // Rebinding the same device, nothing to do. 293 return true; // Rebinding the same device, nothing to do.
(...skipping 26 matching lines...) Expand all
334 TRACE_EVENT0("pepper", "PepperGraphics2DHost::Paint"); 320 TRACE_EVENT0("pepper", "PepperGraphics2DHost::Paint");
335 ImageDataAutoMapper auto_mapper(image_data_.get()); 321 ImageDataAutoMapper auto_mapper(image_data_.get());
336 const SkBitmap& backing_bitmap = *image_data_->GetMappedBitmap(); 322 const SkBitmap& backing_bitmap = *image_data_->GetMappedBitmap();
337 323
338 gfx::Rect invalidate_rect = plugin_rect; 324 gfx::Rect invalidate_rect = plugin_rect;
339 invalidate_rect.Intersect(paint_rect); 325 invalidate_rect.Intersect(paint_rect);
340 SkRect sk_invalidate_rect = gfx::RectToSkRect(invalidate_rect); 326 SkRect sk_invalidate_rect = gfx::RectToSkRect(invalidate_rect);
341 SkAutoCanvasRestore auto_restore(canvas, true); 327 SkAutoCanvasRestore auto_restore(canvas, true);
342 canvas->clipRect(sk_invalidate_rect); 328 canvas->clipRect(sk_invalidate_rect);
343 gfx::Size pixel_image_size(image_data_->width(), image_data_->height()); 329 gfx::Size pixel_image_size(image_data_->width(), image_data_->height());
344 gfx::Size image_size = gfx::ToFlooredSize( 330 gfx::Size image_size =
345 gfx::ScaleSize(pixel_image_size, scale_)); 331 gfx::ToFlooredSize(gfx::ScaleSize(pixel_image_size, scale_));
346 332
347 PepperPluginInstance* plugin_instance = 333 PepperPluginInstance* plugin_instance =
348 renderer_ppapi_host_->GetPluginInstance(pp_instance()); 334 renderer_ppapi_host_->GetPluginInstance(pp_instance());
349 if (!plugin_instance) 335 if (!plugin_instance)
350 return; 336 return;
351 if (plugin_instance->IsFullPagePlugin()) { 337 if (plugin_instance->IsFullPagePlugin()) {
352 // When we're resizing a window with a full-frame plugin, the plugin may 338 // When we're resizing a window with a full-frame plugin, the plugin may
353 // not yet have bound a new device, which will leave parts of the 339 // not yet have bound a new device, which will leave parts of the
354 // background exposed if the window is getting larger. We want this to 340 // background exposed if the window is getting larger. We want this to
355 // show white (typically less jarring) rather than black or uninitialized. 341 // show white (typically less jarring) rather than black or uninitialized.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 SkPoint pixel_origin = origin; 373 SkPoint pixel_origin = origin;
388 374
389 if (scale_ != 1.0f && scale_ > 0.0f) { 375 if (scale_ != 1.0f && scale_ > 0.0f) {
390 canvas->scale(scale_, scale_); 376 canvas->scale(scale_, scale_);
391 pixel_origin.set(pixel_origin.x() * (1.0f / scale_), 377 pixel_origin.set(pixel_origin.x() * (1.0f / scale_),
392 pixel_origin.y() * (1.0f / scale_)); 378 pixel_origin.y() * (1.0f / scale_));
393 } 379 }
394 canvas->drawBitmap(image, pixel_origin.x(), pixel_origin.y(), &paint); 380 canvas->drawBitmap(image, pixel_origin.x(), pixel_origin.y(), &paint);
395 } 381 }
396 382
397 void PepperGraphics2DHost::ViewInitiatedPaint() { 383 void PepperGraphics2DHost::ViewInitiatedPaint() {}
398 }
399 384
400 void PepperGraphics2DHost::ViewFlushedPaint() { 385 void PepperGraphics2DHost::ViewFlushedPaint() {
401 TRACE_EVENT0("pepper", "PepperGraphics2DHost::ViewFlushedPaint"); 386 TRACE_EVENT0("pepper", "PepperGraphics2DHost::ViewFlushedPaint");
402 if (need_flush_ack_) { 387 if (need_flush_ack_) {
403 SendFlushAck(); 388 SendFlushAck();
404 need_flush_ack_ = false; 389 need_flush_ack_ = false;
405 } 390 }
406 } 391 }
407 392
408 void PepperGraphics2DHost::SetScale(float scale) { 393 void PepperGraphics2DHost::SetScale(float scale) { scale_ = scale; }
409 scale_ = scale;
410 }
411 394
412 float PepperGraphics2DHost::GetScale() const { 395 float PepperGraphics2DHost::GetScale() const { return scale_; }
413 return scale_;
414 }
415 396
416 bool PepperGraphics2DHost::IsAlwaysOpaque() const { 397 bool PepperGraphics2DHost::IsAlwaysOpaque() const { return is_always_opaque_; }
417 return is_always_opaque_;
418 }
419 398
420 PPB_ImageData_Impl* PepperGraphics2DHost::ImageData() { 399 PPB_ImageData_Impl* PepperGraphics2DHost::ImageData() {
421 return image_data_.get(); 400 return image_data_.get();
422 } 401 }
423 402
424 gfx::Size PepperGraphics2DHost::Size() const { 403 gfx::Size PepperGraphics2DHost::Size() const {
425 if (!image_data_) 404 if (!image_data_)
426 return gfx::Size(); 405 return gfx::Size();
427 return gfx::Size(image_data_->width(), image_data_->height()); 406 return gfx::Size(image_data_->width(), image_data_->height());
428 } 407 }
(...skipping 18 matching lines...) Expand all
447 image_resource->height(), 426 image_resource->height(),
448 &operation.paint_src_rect)) 427 &operation.paint_src_rect))
449 return PP_ERROR_BADARGUMENT; 428 return PP_ERROR_BADARGUMENT;
450 429
451 // Validate the bitmap position using the previously-validated rect, there 430 // Validate the bitmap position using the previously-validated rect, there
452 // should be no painted area outside of the image. 431 // should be no painted area outside of the image.
453 int64 x64 = static_cast<int64>(top_left.x); 432 int64 x64 = static_cast<int64>(top_left.x);
454 int64 y64 = static_cast<int64>(top_left.y); 433 int64 y64 = static_cast<int64>(top_left.y);
455 if (x64 + static_cast<int64>(operation.paint_src_rect.x()) < 0 || 434 if (x64 + static_cast<int64>(operation.paint_src_rect.x()) < 0 ||
456 x64 + static_cast<int64>(operation.paint_src_rect.right()) > 435 x64 + static_cast<int64>(operation.paint_src_rect.right()) >
457 image_data_->width()) 436 image_data_->width())
458 return PP_ERROR_BADARGUMENT; 437 return PP_ERROR_BADARGUMENT;
459 if (y64 + static_cast<int64>(operation.paint_src_rect.y()) < 0 || 438 if (y64 + static_cast<int64>(operation.paint_src_rect.y()) < 0 ||
460 y64 + static_cast<int64>(operation.paint_src_rect.bottom()) > 439 y64 + static_cast<int64>(operation.paint_src_rect.bottom()) >
461 image_data_->height()) 440 image_data_->height())
462 return PP_ERROR_BADARGUMENT; 441 return PP_ERROR_BADARGUMENT;
463 operation.paint_x = top_left.x; 442 operation.paint_x = top_left.x;
464 operation.paint_y = top_left.y; 443 operation.paint_y = top_left.y;
465 444
466 queued_operations_.push_back(operation); 445 queued_operations_.push_back(operation);
467 return PP_OK; 446 return PP_OK;
468 } 447 }
469 448
470 int32_t PepperGraphics2DHost::OnHostMsgScroll( 449 int32_t PepperGraphics2DHost::OnHostMsgScroll(
471 ppapi::host::HostMessageContext* context, 450 ppapi::host::HostMessageContext* context,
(...skipping 25 matching lines...) Expand all
497 int32_t PepperGraphics2DHost::OnHostMsgReplaceContents( 476 int32_t PepperGraphics2DHost::OnHostMsgReplaceContents(
498 ppapi::host::HostMessageContext* context, 477 ppapi::host::HostMessageContext* context,
499 const ppapi::HostResource& image_data) { 478 const ppapi::HostResource& image_data) {
500 EnterResourceNoLock<PPB_ImageData_API> enter(image_data.host_resource(), 479 EnterResourceNoLock<PPB_ImageData_API> enter(image_data.host_resource(),
501 true); 480 true);
502 if (enter.failed()) 481 if (enter.failed())
503 return PP_ERROR_BADRESOURCE; 482 return PP_ERROR_BADRESOURCE;
504 PPB_ImageData_Impl* image_resource = 483 PPB_ImageData_Impl* image_resource =
505 static_cast<PPB_ImageData_Impl*>(enter.object()); 484 static_cast<PPB_ImageData_Impl*>(enter.object());
506 485
507 if (!PPB_ImageData_Impl::IsImageDataFormatSupported( 486 if (!PPB_ImageData_Impl::IsImageDataFormatSupported(image_resource->format()))
508 image_resource->format()))
509 return PP_ERROR_BADARGUMENT; 487 return PP_ERROR_BADARGUMENT;
510 488
511 if (image_resource->width() != image_data_->width() || 489 if (image_resource->width() != image_data_->width() ||
512 image_resource->height() != image_data_->height()) 490 image_resource->height() != image_data_->height())
513 return PP_ERROR_BADARGUMENT; 491 return PP_ERROR_BADARGUMENT;
514 492
515 QueuedOperation operation(QueuedOperation::REPLACE); 493 QueuedOperation operation(QueuedOperation::REPLACE);
516 operation.replace_image = image_resource; 494 operation.replace_image = image_resource;
517 queued_operations_.push_back(operation); 495 queued_operations_.push_back(operation);
518 return PP_OK; 496 return PP_OK;
(...skipping 11 matching lines...) Expand all
530 return Flush(NULL); 508 return Flush(NULL);
531 509
532 // Reuse image data when running out of process. 510 // Reuse image data when running out of process.
533 int32_t result = Flush(&old_image_data); 511 int32_t result = Flush(&old_image_data);
534 512
535 if (old_image_data) { 513 if (old_image_data) {
536 // If the Graphics2D has an old image data it's not using any more, send 514 // If the Graphics2D has an old image data it's not using any more, send
537 // it back to the plugin for possible re-use. See ppb_image_data_proxy.cc 515 // it back to the plugin for possible re-use. See ppb_image_data_proxy.cc
538 // for a description how this process works. 516 // for a description how this process works.
539 ppapi::HostResource old_image_data_host_resource; 517 ppapi::HostResource old_image_data_host_resource;
540 old_image_data_host_resource.SetHostResource(pp_instance(), 518 old_image_data_host_resource.SetHostResource(pp_instance(), old_image_data);
541 old_image_data);
542 host()->Send(new PpapiMsg_PPBImageData_NotifyUnusedImageData( 519 host()->Send(new PpapiMsg_PPBImageData_NotifyUnusedImageData(
543 ppapi::API_ID_PPB_IMAGE_DATA, old_image_data_host_resource)); 520 ppapi::API_ID_PPB_IMAGE_DATA, old_image_data_host_resource));
544 } 521 }
545 522
546 return result; 523 return result;
547 } 524 }
548 525
549 int32_t PepperGraphics2DHost::OnHostMsgSetScale( 526 int32_t PepperGraphics2DHost::OnHostMsgSetScale(
550 ppapi::host::HostMessageContext* context, 527 ppapi::host::HostMessageContext* context,
551 float scale) { 528 float scale) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
605 switch (operation.type) { 582 switch (operation.type) {
606 case QueuedOperation::PAINT: 583 case QueuedOperation::PAINT:
607 ExecutePaintImageData(operation.paint_image.get(), 584 ExecutePaintImageData(operation.paint_image.get(),
608 operation.paint_x, 585 operation.paint_x,
609 operation.paint_y, 586 operation.paint_y,
610 operation.paint_src_rect, 587 operation.paint_src_rect,
611 &op_rect); 588 &op_rect);
612 break; 589 break;
613 case QueuedOperation::SCROLL: 590 case QueuedOperation::SCROLL:
614 ExecuteScroll(operation.scroll_clip_rect, 591 ExecuteScroll(operation.scroll_clip_rect,
615 operation.scroll_dx, operation.scroll_dy, 592 operation.scroll_dx,
593 operation.scroll_dy,
616 &op_rect); 594 &op_rect);
617 break; 595 break;
618 case QueuedOperation::REPLACE: 596 case QueuedOperation::REPLACE:
619 // Since the out parameter |old_image_data| takes ownership of the 597 // Since the out parameter |old_image_data| takes ownership of the
620 // reference, if there are more than one ReplaceContents calls queued 598 // reference, if there are more than one ReplaceContents calls queued
621 // the first |old_image_data| will get overwritten and leaked. So we 599 // the first |old_image_data| will get overwritten and leaked. So we
622 // only supply this for the first call. 600 // only supply this for the first call.
623 ExecuteReplaceContents(operation.replace_image.get(), 601 ExecuteReplaceContents(operation.replace_image.get(),
624 &op_rect, 602 &op_rect,
625 done_replace_contents ? NULL : old_image_data); 603 done_replace_contents ? NULL : old_image_data);
626 done_replace_contents = true; 604 done_replace_contents = true;
627 break; 605 break;
628 } 606 }
629 607
630 // For correctness with accelerated compositing, we must issue an invalidate 608 // For correctness with accelerated compositing, we must issue an invalidate
631 // on the full op_rect even if it is partially or completely off-screen. 609 // on the full op_rect even if it is partially or completely off-screen.
632 // However, if we issue an invalidate for a clipped-out region, WebKit will 610 // However, if we issue an invalidate for a clipped-out region, WebKit will
633 // do nothing and we won't get any ViewFlushedPaint calls, leaving our 611 // do nothing and we won't get any ViewFlushedPaint calls, leaving our
634 // callback stranded. So we still need to check whether the repainted area 612 // callback stranded. So we still need to check whether the repainted area
635 // is visible to determine how to deal with the callback. 613 // is visible to determine how to deal with the callback.
636 if (bound_instance_ && !op_rect.IsEmpty()) { 614 if (bound_instance_ && !op_rect.IsEmpty()) {
637 gfx::Point scroll_delta(operation.scroll_dx, operation.scroll_dy); 615 gfx::Point scroll_delta(operation.scroll_dx, operation.scroll_dy);
638 if (!ConvertToLogicalPixels(scale_, 616 if (!ConvertToLogicalPixels(scale_,
639 &op_rect, 617 &op_rect,
640 operation.type == QueuedOperation::SCROLL ? 618 operation.type == QueuedOperation::SCROLL
641 &scroll_delta : NULL)) { 619 ? &scroll_delta
620 : NULL)) {
642 // Conversion requires falling back to InvalidateRect. 621 // Conversion requires falling back to InvalidateRect.
643 operation.type = QueuedOperation::PAINT; 622 operation.type = QueuedOperation::PAINT;
644 } 623 }
645 624
646 gfx::Rect clip = PP_ToGfxRect(bound_instance_->view_data().clip_rect); 625 gfx::Rect clip = PP_ToGfxRect(bound_instance_->view_data().clip_rect);
647 is_plugin_visible = !clip.IsEmpty(); 626 is_plugin_visible = !clip.IsEmpty();
648 627
649 // Set |no_update_visible| to false if the change overlaps the visible 628 // Set |no_update_visible| to false if the change overlaps the visible
650 // area. 629 // area.
651 if (!gfx::IntersectRects(clip, op_rect).IsEmpty()) { 630 if (!gfx::IntersectRects(clip, op_rect).IsEmpty()) {
652 no_update_visible = false; 631 no_update_visible = false;
653 } 632 }
654 633
655 // Notify the plugin of the entire change (op_rect), even if it is 634 // Notify the plugin of the entire change (op_rect), even if it is
656 // partially or completely off-screen. 635 // partially or completely off-screen.
657 if (operation.type == QueuedOperation::SCROLL) { 636 if (operation.type == QueuedOperation::SCROLL) {
658 bound_instance_->ScrollRect(scroll_delta.x(), scroll_delta.y(), 637 bound_instance_->ScrollRect(
659 op_rect); 638 scroll_delta.x(), scroll_delta.y(), op_rect);
660 } else { 639 } else {
661 if (!op_rect.IsEmpty()) 640 if (!op_rect.IsEmpty())
662 bound_instance_->InvalidateRect(op_rect); 641 bound_instance_->InvalidateRect(op_rect);
663 } 642 }
664 texture_mailbox_modified_ = true; 643 texture_mailbox_modified_ = true;
665 } 644 }
666 } 645 }
667 queued_operations_.clear(); 646 queued_operations_.clear();
668 647
669 if (!bound_instance_) { 648 if (!bound_instance_) {
670 // As promised in the API, we always schedule callback when unbound. 649 // As promised in the API, we always schedule callback when unbound.
671 ScheduleOffscreenFlushAck(); 650 ScheduleOffscreenFlushAck();
672 } else if (no_update_visible && is_plugin_visible && 651 } else if (no_update_visible && is_plugin_visible &&
673 bound_instance_->view_data().is_page_visible) { 652 bound_instance_->view_data().is_page_visible) {
674 // There's nothing visible to invalidate so just schedule the callback to 653 // There's nothing visible to invalidate so just schedule the callback to
675 // execute in the next round of the message loop. 654 // execute in the next round of the message loop.
676 ScheduleOffscreenFlushAck(); 655 ScheduleOffscreenFlushAck();
677 } else { 656 } else {
678 need_flush_ack_ = true; 657 need_flush_ack_ = true;
679 } 658 }
680 659
681 return PP_OK_COMPLETIONPENDING; 660 return PP_OK_COMPLETIONPENDING;
682 } 661 }
683 662
684 void PepperGraphics2DHost::ExecutePaintImageData(PPB_ImageData_Impl* image, 663 void PepperGraphics2DHost::ExecutePaintImageData(PPB_ImageData_Impl* image,
685 int x, int y, 664 int x,
686 const gfx::Rect& src_rect, 665 int y,
687 gfx::Rect* invalidated_rect) { 666 const gfx::Rect& src_rect,
667 gfx::Rect* invalidated_rect) {
688 // Ensure the source image is mapped to read from it. 668 // Ensure the source image is mapped to read from it.
689 ImageDataAutoMapper auto_mapper(image); 669 ImageDataAutoMapper auto_mapper(image);
690 if (!auto_mapper.is_valid()) 670 if (!auto_mapper.is_valid())
691 return; 671 return;
692 672
693 // Portion within the source image to cut out. 673 // Portion within the source image to cut out.
694 SkIRect src_irect = { src_rect.x(), src_rect.y(), 674 SkIRect src_irect = {src_rect.x(), src_rect.y(), src_rect.right(),
695 src_rect.right(), src_rect.bottom() }; 675 src_rect.bottom()};
696 676
697 // Location within the backing store to copy to. 677 // Location within the backing store to copy to.
698 *invalidated_rect = src_rect; 678 *invalidated_rect = src_rect;
699 invalidated_rect->Offset(x, y); 679 invalidated_rect->Offset(x, y);
700 SkRect dest_rect = { SkIntToScalar(invalidated_rect->x()), 680 SkRect dest_rect = {SkIntToScalar(invalidated_rect->x()),
701 SkIntToScalar(invalidated_rect->y()), 681 SkIntToScalar(invalidated_rect->y()),
702 SkIntToScalar(invalidated_rect->right()), 682 SkIntToScalar(invalidated_rect->right()),
703 SkIntToScalar(invalidated_rect->bottom()) }; 683 SkIntToScalar(invalidated_rect->bottom())};
704 684
705 if (image->format() != image_data_->format()) { 685 if (image->format() != image_data_->format()) {
706 // Convert the image data if the format does not match. 686 // Convert the image data if the format does not match.
707 ConvertImageData(image, src_irect, image_data_.get(), dest_rect); 687 ConvertImageData(image, src_irect, image_data_.get(), dest_rect);
708 } else { 688 } else {
709 // We're guaranteed to have a mapped canvas since we mapped it in Init(). 689 // We're guaranteed to have a mapped canvas since we mapped it in Init().
710 SkCanvas* backing_canvas = image_data_->GetCanvas(); 690 SkCanvas* backing_canvas = image_data_->GetCanvas();
711 691
712 // We want to replace the contents of the bitmap rather than blend. 692 // We want to replace the contents of the bitmap rather than blend.
713 SkPaint paint; 693 SkPaint paint;
714 paint.setXfermodeMode(SkXfermode::kSrc_Mode); 694 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
715 backing_canvas->drawBitmapRect(*image->GetMappedBitmap(), 695 backing_canvas->drawBitmapRect(
716 &src_irect, dest_rect, &paint); 696 *image->GetMappedBitmap(), &src_irect, dest_rect, &paint);
717 } 697 }
718 } 698 }
719 699
720 void PepperGraphics2DHost::ExecuteScroll(const gfx::Rect& clip, 700 void PepperGraphics2DHost::ExecuteScroll(const gfx::Rect& clip,
721 int dx, int dy, 701 int dx,
722 gfx::Rect* invalidated_rect) { 702 int dy,
723 gfx::ScrollCanvas(image_data_->GetCanvas(), 703 gfx::Rect* invalidated_rect) {
724 clip, gfx::Vector2d(dx, dy)); 704 gfx::ScrollCanvas(image_data_->GetCanvas(), clip, gfx::Vector2d(dx, dy));
725 *invalidated_rect = clip; 705 *invalidated_rect = clip;
726 } 706 }
727 707
728 void PepperGraphics2DHost::ExecuteReplaceContents(PPB_ImageData_Impl* image, 708 void PepperGraphics2DHost::ExecuteReplaceContents(PPB_ImageData_Impl* image,
729 gfx::Rect* invalidated_rect, 709 gfx::Rect* invalidated_rect,
730 PP_Resource* old_image_data) { 710 PP_Resource* old_image_data) {
731 if (image->format() != image_data_->format()) { 711 if (image->format() != image_data_->format()) {
732 DCHECK(image->width() == image_data_->width() && 712 DCHECK(image->width() == image_data_->width() &&
733 image->height() == image_data_->height()); 713 image->height() == image_data_->height());
734 // Convert the image data if the format does not match. 714 // Convert the image data if the format does not match.
735 SkIRect src_irect = { 0, 0, image->width(), image->height() }; 715 SkIRect src_irect = {0, 0, image->width(), image->height()};
736 SkRect dest_rect = { SkIntToScalar(0), 716 SkRect dest_rect = {SkIntToScalar(0), SkIntToScalar(0),
737 SkIntToScalar(0), 717 SkIntToScalar(image_data_->width()),
738 SkIntToScalar(image_data_->width()), 718 SkIntToScalar(image_data_->height())};
739 SkIntToScalar(image_data_->height()) };
740 ConvertImageData(image, src_irect, image_data_.get(), dest_rect); 719 ConvertImageData(image, src_irect, image_data_.get(), dest_rect);
741 } else { 720 } else {
742 // The passed-in image may not be mapped in our process, and we need to 721 // The passed-in image may not be mapped in our process, and we need to
743 // guarantee that the current backing store is always mapped. 722 // guarantee that the current backing store is always mapped.
744 if (!image->Map()) 723 if (!image->Map())
745 return; 724 return;
746 725
747 if (old_image_data) 726 if (old_image_data)
748 *old_image_data = image_data_->GetReference(); 727 *old_image_data = image_data_->GetReference();
749 image_data_ = image; 728 image_data_ = image;
750 } 729 }
751 *invalidated_rect = gfx::Rect(0, 0, 730 *invalidated_rect =
752 image_data_->width(), image_data_->height()); 731 gfx::Rect(0, 0, image_data_->width(), image_data_->height());
753 } 732 }
754 733
755 void PepperGraphics2DHost::SendFlushAck() { 734 void PepperGraphics2DHost::SendFlushAck() {
756 host()->SendReply(flush_reply_context_, 735 host()->SendReply(flush_reply_context_, PpapiPluginMsg_Graphics2D_FlushAck());
757 PpapiPluginMsg_Graphics2D_FlushAck());
758 } 736 }
759 737
760 void PepperGraphics2DHost::SendOffscreenFlushAck() { 738 void PepperGraphics2DHost::SendOffscreenFlushAck() {
761 DCHECK(offscreen_flush_pending_); 739 DCHECK(offscreen_flush_pending_);
762 740
763 // We must clear this flag before issuing the callback. It will be 741 // We must clear this flag before issuing the callback. It will be
764 // common for the plugin to issue another invalidate in response to a flush 742 // common for the plugin to issue another invalidate in response to a flush
765 // callback, and we don't want to think that a callback is already pending. 743 // callback, and we don't want to think that a callback is already pending.
766 offscreen_flush_pending_ = false; 744 offscreen_flush_pending_ = false;
767 SendFlushAck(); 745 SendFlushAck();
768 } 746 }
769 747
770 void PepperGraphics2DHost::ScheduleOffscreenFlushAck() { 748 void PepperGraphics2DHost::ScheduleOffscreenFlushAck() {
771 offscreen_flush_pending_ = true; 749 offscreen_flush_pending_ = true;
772 base::MessageLoop::current()->PostDelayedTask( 750 base::MessageLoop::current()->PostDelayedTask(
773 FROM_HERE, 751 FROM_HERE,
774 base::Bind(&PepperGraphics2DHost::SendOffscreenFlushAck, 752 base::Bind(&PepperGraphics2DHost::SendOffscreenFlushAck, AsWeakPtr()),
775 AsWeakPtr()),
776 base::TimeDelta::FromMilliseconds(kOffscreenCallbackDelayMs)); 753 base::TimeDelta::FromMilliseconds(kOffscreenCallbackDelayMs));
777 } 754 }
778 755
779 bool PepperGraphics2DHost::HasPendingFlush() const { 756 bool PepperGraphics2DHost::HasPendingFlush() const {
780 return need_flush_ack_ || offscreen_flush_pending_; 757 return need_flush_ack_ || offscreen_flush_pending_;
781 } 758 }
782 759
783 // static 760 // static
784 bool PepperGraphics2DHost::ConvertToLogicalPixels(float scale, 761 bool PepperGraphics2DHost::ConvertToLogicalPixels(float scale,
785 gfx::Rect* op_rect, 762 gfx::Rect* op_rect,
786 gfx::Point* delta) { 763 gfx::Point* delta) {
787 if (scale == 1.0f || scale <= 0.0f) 764 if (scale == 1.0f || scale <= 0.0f)
788 return true; 765 return true;
789 766
790 gfx::Rect original_rect = *op_rect; 767 gfx::Rect original_rect = *op_rect;
791 // Take the enclosing rectangle after scaling so a rectangle scaled down then 768 // Take the enclosing rectangle after scaling so a rectangle scaled down then
792 // scaled back up by the inverse scale would fully contain the entire area 769 // scaled back up by the inverse scale would fully contain the entire area
793 // affected by the original rectangle. 770 // affected by the original rectangle.
794 *op_rect = gfx::ToEnclosingRect(gfx::ScaleRect(*op_rect, scale)); 771 *op_rect = gfx::ToEnclosingRect(gfx::ScaleRect(*op_rect, scale));
795 if (delta) { 772 if (delta) {
796 gfx::Point original_delta = *delta; 773 gfx::Point original_delta = *delta;
797 float inverse_scale = 1.0f / scale; 774 float inverse_scale = 1.0f / scale;
798 *delta = gfx::ToFlooredPoint(gfx::ScalePoint(*delta, scale)); 775 *delta = gfx::ToFlooredPoint(gfx::ScalePoint(*delta, scale));
799 776
800 gfx::Rect inverse_scaled_rect = 777 gfx::Rect inverse_scaled_rect =
801 gfx::ToEnclosingRect(gfx::ScaleRect(*op_rect, inverse_scale)); 778 gfx::ToEnclosingRect(gfx::ScaleRect(*op_rect, inverse_scale));
802 if (original_rect != inverse_scaled_rect) 779 if (original_rect != inverse_scaled_rect)
803 return false; 780 return false;
804 gfx::Point inverse_scaled_point = 781 gfx::Point inverse_scaled_point =
805 gfx::ToFlooredPoint(gfx::ScalePoint(*delta, inverse_scale)); 782 gfx::ToFlooredPoint(gfx::ScalePoint(*delta, inverse_scale));
806 if (original_delta != inverse_scaled_point) 783 if (original_delta != inverse_scaled_point)
807 return false; 784 return false;
808 } 785 }
809 786
810 return true; 787 return true;
811 } 788 }
812 789
813 } // namespace content 790 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698