OLD | NEW |
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/browser/renderer_host/backing_store_skia.h" | 5 #include "content/browser/renderer_host/backing_store_skia.h" |
6 | 6 |
| 7 #include "content/browser/renderer_host/dip_util.h" |
7 #include "content/browser/renderer_host/render_process_host_impl.h" | 8 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 9 #include "content/public/browser/render_widget_host.h" |
8 #include "skia/ext/platform_canvas.h" | 10 #include "skia/ext/platform_canvas.h" |
9 #include "third_party/skia/include/core/SkCanvas.h" | 11 #include "third_party/skia/include/core/SkCanvas.h" |
10 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
11 #include "ui/gfx/rect.h" | 13 #include "ui/gfx/rect.h" |
12 | 14 |
13 // Assume that somewhere along the line, someone will do width * height * 4 | 15 // Assume that somewhere along the line, someone will do width * height * 4 |
14 // with signed numbers. If the maximum value is 2**31, then 2**31 / 4 = | 16 // with signed numbers. If the maximum value is 2**31, then 2**31 / 4 = |
15 // 2**29 and floor(sqrt(2**29)) = 23170. | 17 // 2**29 and floor(sqrt(2**29)) = 23170. |
16 | 18 |
17 // Max height and width for layers | 19 // Max height and width for layers |
18 static const int kMaxVideoLayerSize = 23170; | 20 static const int kMaxVideoLayerSize = 23170; |
19 | 21 |
20 BackingStoreSkia::BackingStoreSkia(content::RenderWidgetHost* widget, | 22 BackingStoreSkia::BackingStoreSkia(content::RenderWidgetHost* widget, |
21 const gfx::Size& size) | 23 const gfx::Size& size) |
22 : BackingStore(widget, size) { | 24 : BackingStore(widget, size), |
23 bitmap_.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height()); | 25 device_scale_factor_(content::GetDIPScaleFactor(widget->GetView())) { |
| 26 gfx::Size pixel_size = size.Scale(device_scale_factor_); |
| 27 bitmap_.setConfig(SkBitmap::kARGB_8888_Config, |
| 28 pixel_size.width(), pixel_size.height()); |
24 bitmap_.allocPixels(); | 29 bitmap_.allocPixels(); |
25 canvas_.reset(new SkCanvas(bitmap_)); | 30 canvas_.reset(new SkCanvas(bitmap_)); |
26 } | 31 } |
27 | 32 |
28 BackingStoreSkia::~BackingStoreSkia() { | 33 BackingStoreSkia::~BackingStoreSkia() { |
29 } | 34 } |
30 | 35 |
31 void BackingStoreSkia::SkiaShowRect(const gfx::Point& point, | 36 void BackingStoreSkia::SkiaShowRect(const gfx::Point& point, |
32 gfx::Canvas* canvas) { | 37 gfx::Canvas* canvas) { |
| 38 const gfx::Point scaled_point = point.Scale(device_scale_factor_); |
33 canvas->sk_canvas()->drawBitmap(bitmap_, | 39 canvas->sk_canvas()->drawBitmap(bitmap_, |
34 SkIntToScalar(point.x()), SkIntToScalar(point.y())); | 40 SkIntToScalar(scaled_point.x()), SkIntToScalar(scaled_point.y())); |
35 } | 41 } |
36 | 42 |
37 size_t BackingStoreSkia::MemorySize() { | 43 size_t BackingStoreSkia::MemorySize() { |
38 // NOTE: The computation may be different when the canvas is a subrectangle of | 44 // NOTE: The computation may be different when the canvas is a subrectangle of |
39 // a larger bitmap. | 45 // a larger bitmap. |
40 return size().GetArea() * 4; | 46 return size().Scale(device_scale_factor_).GetArea() * 4; |
41 } | 47 } |
42 | 48 |
43 void BackingStoreSkia::PaintToBackingStore( | 49 void BackingStoreSkia::PaintToBackingStore( |
44 content::RenderProcessHost* process, | 50 content::RenderProcessHost* process, |
45 TransportDIB::Id bitmap, | 51 TransportDIB::Id bitmap, |
46 const gfx::Rect& bitmap_rect, | 52 const gfx::Rect& bitmap_rect, |
47 const std::vector<gfx::Rect>& copy_rects, | 53 const std::vector<gfx::Rect>& copy_rects, |
48 const base::Closure& completion_callback, | 54 const base::Closure& completion_callback, |
49 bool* scheduled_completion_callback) { | 55 bool* scheduled_completion_callback) { |
50 *scheduled_completion_callback = false; | 56 *scheduled_completion_callback = false; |
51 if (bitmap_rect.IsEmpty()) | 57 if (bitmap_rect.IsEmpty()) |
52 return; | 58 return; |
53 | 59 |
54 const int width = bitmap_rect.width(); | 60 gfx::Rect pixel_rect = bitmap_rect.Scale(device_scale_factor_); |
55 const int height = bitmap_rect.height(); | 61 |
| 62 const int width = pixel_rect.width(); |
| 63 const int height = pixel_rect.height(); |
56 | 64 |
57 if (width <= 0 || width > kMaxVideoLayerSize || | 65 if (width <= 0 || width > kMaxVideoLayerSize || |
58 height <= 0 || height > kMaxVideoLayerSize) | 66 height <= 0 || height > kMaxVideoLayerSize) |
59 return; | 67 return; |
60 | 68 |
61 TransportDIB* dib = process->GetTransportDIB(bitmap); | 69 TransportDIB* dib = process->GetTransportDIB(bitmap); |
62 if (!dib) | 70 if (!dib) |
63 return; | 71 return; |
64 | 72 |
65 SkPaint copy_paint; | 73 SkPaint copy_paint; |
66 copy_paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 74 copy_paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
67 | 75 |
68 SkBitmap sk_bitmap; | 76 SkBitmap sk_bitmap; |
69 sk_bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 77 sk_bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
70 sk_bitmap.setPixels(dib->memory()); | 78 sk_bitmap.setPixels(dib->memory()); |
71 for (size_t i = 0; i < copy_rects.size(); i++) { | 79 for (size_t i = 0; i < copy_rects.size(); i++) { |
72 const gfx::Rect& copy_rect = copy_rects[i]; | 80 const gfx::Rect copy_rect = copy_rects[i].Scale(device_scale_factor_); |
73 int x = copy_rect.x() - bitmap_rect.x(); | 81 int x = copy_rect.x() - pixel_rect.x(); |
74 int y = copy_rect.y() - bitmap_rect.y(); | 82 int y = copy_rect.y() - pixel_rect.y(); |
75 int w = copy_rect.width(); | 83 int w = copy_rect.width(); |
76 int h = copy_rect.height(); | 84 int h = copy_rect.height(); |
77 SkIRect srcrect = SkIRect::MakeXYWH(x, y, w, h); | 85 SkIRect srcrect = SkIRect::MakeXYWH(x, y, w, h); |
78 SkRect dstrect = SkRect::MakeXYWH( | 86 SkRect dstrect = SkRect::MakeXYWH( |
79 SkIntToScalar(copy_rect.x()), SkIntToScalar(copy_rect.y()), | 87 SkIntToScalar(copy_rect.x()), SkIntToScalar(copy_rect.y()), |
80 SkIntToScalar(w), SkIntToScalar(h)); | 88 SkIntToScalar(w), SkIntToScalar(h)); |
81 canvas_.get()->drawBitmapRect(sk_bitmap, &srcrect, dstrect, ©_paint); | 89 canvas_.get()->drawBitmapRect(sk_bitmap, &srcrect, dstrect, ©_paint); |
82 } | 90 } |
83 } | 91 } |
84 | 92 |
85 void BackingStoreSkia::ScrollBackingStore(int dx, int dy, | 93 void BackingStoreSkia::ScrollBackingStore(int dx, int dy, |
86 const gfx::Rect& clip_rect, | 94 const gfx::Rect& clip_rect, |
87 const gfx::Size& view_size) { | 95 const gfx::Size& view_size) { |
88 int x = std::min(clip_rect.x(), clip_rect.x() - dx); | 96 gfx::Rect pixel_rect = clip_rect.Scale(device_scale_factor_); |
89 int y = std::min(clip_rect.y(), clip_rect.y() - dy); | 97 int x = std::min(pixel_rect.x(), pixel_rect.x() - dx); |
90 int w = clip_rect.width() + abs(dx); | 98 int y = std::min(pixel_rect.y(), pixel_rect.y() - dy); |
91 int h = clip_rect.height() + abs(dy); | 99 int w = pixel_rect.width() + abs(dx); |
| 100 int h = pixel_rect.height() + abs(dy); |
92 SkIRect rect = SkIRect::MakeXYWH(x, y, w, h); | 101 SkIRect rect = SkIRect::MakeXYWH(x, y, w, h); |
93 bitmap_.scrollRect(&rect, dx, dy); | 102 bitmap_.scrollRect(&rect, dx, dy); |
94 } | 103 } |
95 | 104 |
96 bool BackingStoreSkia::CopyFromBackingStore(const gfx::Rect& rect, | 105 bool BackingStoreSkia::CopyFromBackingStore(const gfx::Rect& rect, |
97 skia::PlatformCanvas* output) { | 106 skia::PlatformCanvas* output) { |
98 const int width = std::min(size().width(), rect.width()); | 107 const int width = |
99 const int height = std::min(size().height(), rect.height()); | 108 std::min(size().width(), rect.width()) * device_scale_factor_; |
| 109 const int height = |
| 110 std::min(size().height(), rect.height()) * device_scale_factor_; |
100 if (!output->initialize(width, height, true)) | 111 if (!output->initialize(width, height, true)) |
101 return false; | 112 return false; |
102 | 113 |
103 SkBitmap bitmap = skia::GetTopDevice(*output)->accessBitmap(true); | 114 SkBitmap bitmap = skia::GetTopDevice(*output)->accessBitmap(true); |
104 SkIRect skrect = SkIRect::MakeXYWH(rect.x(), rect.y(), width, height); | 115 SkIRect skrect = SkIRect::MakeXYWH(rect.x(), rect.y(), width, height); |
105 SkBitmap b; | 116 SkBitmap b; |
106 if (!canvas_->readPixels(skrect, &b)) | 117 if (!canvas_->readPixels(skrect, &b)) |
107 return false; | 118 return false; |
108 output->writePixels(b, rect.x(), rect.y()); | 119 output->writePixels(b, rect.x(), rect.y()); |
109 return true; | 120 return true; |
110 } | 121 } |
OLD | NEW |