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/browser_plugin/browser_plugin_backing_store.h"
7 #include "ui/gfx/canvas.h"
8 #include "ui/gfx/rect.h"
9 #include "ui/gfx/rect_conversions.h"
10 #include "ui/gfx/size_conversions.h"
11 #include "ui/gfx/vector2d_conversions.h"
12 #include "ui/surface/transport_dib.h"
16 // Max height and width for layers
17 static const int kMaxSize
= 23170;
19 BrowserPluginBackingStore::BrowserPluginBackingStore(
20 const gfx::Size
& size
,
23 scale_factor_(scale_factor
) {
24 gfx::Size pixel_size
= gfx::ToFlooredSize(gfx::ScaleSize(size
, scale_factor
));
25 bitmap_
.setConfig(SkBitmap::kARGB_8888_Config
,
26 pixel_size
.width(), pixel_size
.height());
27 bitmap_
.allocPixels();
28 canvas_
.reset(new SkCanvas(bitmap_
));
31 BrowserPluginBackingStore::~BrowserPluginBackingStore() {
34 void BrowserPluginBackingStore::PaintToBackingStore(
35 const gfx::Rect
& bitmap_rect
,
36 const std::vector
<gfx::Rect
>& copy_rects
,
38 if (bitmap_rect
.IsEmpty())
41 gfx::Rect pixel_bitmap_rect
= gfx::ToFlooredRectDeprecated(
42 gfx::ScaleRect(bitmap_rect
, scale_factor_
));
44 const int width
= pixel_bitmap_rect
.width();
45 const int height
= pixel_bitmap_rect
.height();
47 if (width
<= 0 || width
> kMaxSize
||
48 height
<= 0 || height
> kMaxSize
)
55 copy_paint
.setXfermodeMode(SkXfermode::kSrc_Mode
);
58 sk_bitmap
.setConfig(SkBitmap::kARGB_8888_Config
, width
, height
);
59 sk_bitmap
.setPixels(dib
->memory());
60 for (size_t i
= 0; i
< copy_rects
.size(); i
++) {
61 const gfx::Rect
& pixel_copy_rect
= gfx::ToEnclosingRect(
62 gfx::ScaleRect(copy_rects
[i
], scale_factor_
));
63 int x
= pixel_copy_rect
.x() - pixel_bitmap_rect
.x();
64 int y
= pixel_copy_rect
.y() - pixel_bitmap_rect
.y();
65 SkIRect srcrect
= SkIRect::MakeXYWH(x
, y
,
66 pixel_copy_rect
.width(),
67 pixel_copy_rect
.height());
69 SkRect dstrect
= SkRect::MakeXYWH(
70 SkIntToScalar(pixel_copy_rect
.x()),
71 SkIntToScalar(pixel_copy_rect
.y()),
72 SkIntToScalar(pixel_copy_rect
.width()),
73 SkIntToScalar(pixel_copy_rect
.height()));
74 canvas_
.get()->drawBitmapRect(sk_bitmap
, &srcrect
, dstrect
, ©_paint
);
78 void BrowserPluginBackingStore::ScrollBackingStore(
79 const gfx::Vector2d
& delta
,
80 const gfx::Rect
& clip_rect
,
81 const gfx::Size
& view_size
) {
82 gfx::Rect pixel_rect
= gfx::ToEnclosingRect(
83 gfx::ScaleRect(clip_rect
, scale_factor_
));
84 gfx::Vector2d pixel_delta
= gfx::ToFlooredVector2d(
85 gfx::ScaleVector2d(delta
, scale_factor_
));
87 int x
= std::min(pixel_rect
.x(), pixel_rect
.x() - pixel_delta
.x());
88 int y
= std::min(pixel_rect
.y(), pixel_rect
.y() - pixel_delta
.y());
89 int w
= pixel_rect
.width() + abs(pixel_delta
.x());
90 int h
= pixel_rect
.height() + abs(pixel_delta
.y());
91 SkIRect rect
= SkIRect::MakeXYWH(x
, y
, w
, h
);
92 bitmap_
.scrollRect(&rect
, pixel_delta
.x(), pixel_delta
.y());
95 void BrowserPluginBackingStore::Clear(SkColor clear_color
) {
96 canvas_
->clear(clear_color
);
99 } // namespace content