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_aura.h" | 5 #include "content/browser/renderer_host/backing_store_aura.h" |
6 | 6 |
7 #include "content/browser/renderer_host/dip_util.h" | 7 #include "content/browser/renderer_host/dip_util.h" |
8 #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" | 9 #include "content/public/browser/render_widget_host.h" |
10 #include "skia/ext/platform_canvas.h" | 10 #include "skia/ext/platform_canvas.h" |
11 #include "third_party/skia/include/core/SkCanvas.h" | 11 #include "third_party/skia/include/core/SkCanvas.h" |
12 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
13 #include "ui/gfx/image/image_skia.h" | 13 #include "ui/gfx/image/image_skia.h" |
14 #include "ui/gfx/rect.h" | 14 #include "ui/gfx/rect.h" |
15 #include "ui/gfx/rect_conversions.h" | 15 #include "ui/gfx/rect_conversions.h" |
16 #include "ui/gfx/size_conversions.h" | 16 #include "ui/gfx/size_conversions.h" |
17 #include "ui/gfx/vector2d_conversions.h" | 17 #include "ui/gfx/vector2d_conversions.h" |
18 | 18 |
19 namespace { | |
20 | |
21 gfx::Size toPixelSize(gfx::Size dipSize, float scale) { | |
22 return gfx::ToCeiledSize(gfx::ScaleSize(dipSize, scale)); | |
23 } | |
24 | |
25 } // namespace | |
26 | |
27 | |
19 namespace content { | 28 namespace content { |
20 | 29 |
21 // Assume that somewhere along the line, someone will do width * height * 4 | 30 // Assume that somewhere along the line, someone will do width * height * 4 |
22 // with signed numbers. If the maximum value is 2**31, then 2**31 / 4 = | 31 // with signed numbers. If the maximum value is 2**31, then 2**31 / 4 = |
23 // 2**29 and floor(sqrt(2**29)) = 23170. | 32 // 2**29 and floor(sqrt(2**29)) = 23170. |
24 | 33 |
25 // Max height and width for layers | 34 // Max height and width for layers |
26 static const int kMaxVideoLayerSize = 23170; | 35 static const int kMaxVideoLayerSize = 23170; |
27 | 36 |
28 BackingStoreAura::BackingStoreAura(RenderWidgetHost* widget, | 37 BackingStoreAura::BackingStoreAura(RenderWidgetHost* widget, |
29 const gfx::Size& size) | 38 const gfx::Size& size) |
30 : BackingStore(widget, size) { | 39 : BackingStore(widget, size) { |
31 device_scale_factor_ = | 40 device_scale_factor_ = |
32 ui::GetScaleFactorScale(GetScaleFactorForView(widget->GetView())); | 41 ui::GetScaleFactorScale(GetScaleFactorForView(widget->GetView())); |
33 gfx::Size pixel_size = gfx::ToFlooredSize( | 42 |
pkotwicz
2013/03/08 04:27:36
Nit: Remove the extra blank lines.
kevers
2013/03/08 15:20:09
Done.
| |
34 gfx::ScaleSize(size, device_scale_factor_)); | 43 gfx::Size pixel_size = toPixelSize(size, device_scale_factor_); |
44 | |
35 bitmap_.setConfig(SkBitmap::kARGB_8888_Config, | 45 bitmap_.setConfig(SkBitmap::kARGB_8888_Config, |
36 pixel_size.width(), pixel_size.height()); | 46 pixel_size.width(), pixel_size.height()); |
37 bitmap_.allocPixels(); | 47 bitmap_.allocPixels(); |
38 canvas_.reset(new SkCanvas(bitmap_)); | 48 canvas_.reset(new SkCanvas(bitmap_)); |
39 } | 49 } |
40 | 50 |
41 BackingStoreAura::~BackingStoreAura() { | 51 BackingStoreAura::~BackingStoreAura() { |
42 } | 52 } |
43 | 53 |
44 void BackingStoreAura::SkiaShowRect(const gfx::Point& point, | 54 void BackingStoreAura::SkiaShowRect(const gfx::Point& point, |
45 gfx::Canvas* canvas) { | 55 gfx::Canvas* canvas) { |
46 gfx::ImageSkia image = gfx::ImageSkia(gfx::ImageSkiaRep(bitmap_, | 56 gfx::ImageSkia image = gfx::ImageSkia(gfx::ImageSkiaRep(bitmap_, |
47 ui::GetScaleFactorFromScale(device_scale_factor_))); | 57 ui::GetScaleFactorFromScale(device_scale_factor_))); |
48 canvas->DrawImageInt(image, point.x(), point.y()); | 58 canvas->DrawImageInt(image, point.x(), point.y()); |
49 } | 59 } |
50 | 60 |
51 void BackingStoreAura::ScaleFactorChanged(float device_scale_factor) { | 61 void BackingStoreAura::ScaleFactorChanged(float device_scale_factor) { |
52 if (device_scale_factor == device_scale_factor_) | 62 if (device_scale_factor == device_scale_factor_) |
53 return; | 63 return; |
54 | 64 |
55 gfx::Size old_pixel_size = gfx::ToFlooredSize( | 65 gfx::Size old_pixel_size = toPixelSize(size(), device_scale_factor_); |
56 gfx::ScaleSize(size(), device_scale_factor_)); | |
57 device_scale_factor_ = device_scale_factor; | 66 device_scale_factor_ = device_scale_factor; |
58 | 67 |
59 gfx::Size pixel_size = gfx::ToFlooredSize( | 68 gfx::Size pixel_size = toPixelSize(size(), device_scale_factor_); |
60 gfx::ScaleSize(size(), device_scale_factor_)); | |
61 SkBitmap new_bitmap; | 69 SkBitmap new_bitmap; |
62 new_bitmap.setConfig(SkBitmap::kARGB_8888_Config, | 70 new_bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
63 pixel_size.width(), pixel_size.height()); | 71 pixel_size.width(), pixel_size.height()); |
64 new_bitmap.allocPixels(); | 72 new_bitmap.allocPixels(); |
65 scoped_ptr<SkCanvas> new_canvas(new SkCanvas(new_bitmap)); | 73 scoped_ptr<SkCanvas> new_canvas(new SkCanvas(new_bitmap)); |
66 | 74 |
67 // Copy old contents; a low-res flash is better than a black flash. | 75 // Copy old contents; a low-res flash is better than a black flash. |
68 SkPaint copy_paint; | 76 SkPaint copy_paint; |
69 copy_paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 77 copy_paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
70 SkIRect src_rect = SkIRect::MakeWH(old_pixel_size.width(), | 78 SkIRect src_rect = SkIRect::MakeWH(old_pixel_size.width(), |
71 old_pixel_size.height()); | 79 old_pixel_size.height()); |
72 SkRect dst_rect = SkRect::MakeWH(pixel_size.width(), pixel_size.height()); | 80 SkRect dst_rect = SkRect::MakeWH(pixel_size.width(), pixel_size.height()); |
73 new_canvas.get()->drawBitmapRect(bitmap_, &src_rect, dst_rect, ©_paint); | 81 new_canvas.get()->drawBitmapRect(bitmap_, &src_rect, dst_rect, ©_paint); |
74 | 82 |
75 canvas_.swap(new_canvas); | 83 canvas_.swap(new_canvas); |
76 bitmap_ = new_bitmap; | 84 bitmap_ = new_bitmap; |
77 } | 85 } |
78 | 86 |
79 size_t BackingStoreAura::MemorySize() { | 87 size_t BackingStoreAura::MemorySize() { |
80 // NOTE: The computation may be different when the canvas is a subrectangle of | 88 // NOTE: The computation may be different when the canvas is a subrectangle of |
81 // a larger bitmap. | 89 // a larger bitmap. |
82 return gfx::ToFlooredSize( | 90 return toPixelSize(size(), device_scale_factor_).GetArea() * 4; |
pkotwicz
2013/03/08 04:27:36
Nit: ToPixelSize()
kevers
2013/03/08 15:20:09
Done.
| |
83 gfx::ScaleSize(size(), device_scale_factor_)).GetArea() * 4; | |
84 } | 91 } |
85 | 92 |
86 void BackingStoreAura::PaintToBackingStore( | 93 void BackingStoreAura::PaintToBackingStore( |
87 RenderProcessHost* process, | 94 RenderProcessHost* process, |
88 TransportDIB::Id bitmap, | 95 TransportDIB::Id bitmap, |
89 const gfx::Rect& bitmap_rect, | 96 const gfx::Rect& bitmap_rect, |
90 const std::vector<gfx::Rect>& copy_rects, | 97 const std::vector<gfx::Rect>& copy_rects, |
91 float scale_factor, | 98 float scale_factor, |
92 const base::Closure& completion_callback, | 99 const base::Closure& completion_callback, |
93 bool* scheduled_completion_callback) { | 100 bool* scheduled_completion_callback) { |
94 *scheduled_completion_callback = false; | 101 *scheduled_completion_callback = false; |
95 if (bitmap_rect.IsEmpty()) | 102 if (bitmap_rect.IsEmpty()) |
96 return; | 103 return; |
97 | 104 |
98 gfx::Rect pixel_bitmap_rect = gfx::ToEnclosedRect( | 105 gfx::Rect pixel_bitmap_rect = gfx::ToEnclosingRect( |
99 gfx::ScaleRect(bitmap_rect, scale_factor)); | 106 gfx::ScaleRect(bitmap_rect, scale_factor)); |
100 | 107 |
101 const int width = pixel_bitmap_rect.width(); | 108 const int width = pixel_bitmap_rect.width(); |
102 const int height = pixel_bitmap_rect.height(); | 109 const int height = pixel_bitmap_rect.height(); |
103 | 110 |
104 if (width <= 0 || width > kMaxVideoLayerSize || | 111 if (width <= 0 || width > kMaxVideoLayerSize || |
105 height <= 0 || height > kMaxVideoLayerSize) | 112 height <= 0 || height > kMaxVideoLayerSize) |
106 return; | 113 return; |
107 | 114 |
108 TransportDIB* dib = process->GetTransportDIB(bitmap); | 115 TransportDIB* dib = process->GetTransportDIB(bitmap); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 | 169 |
163 SkIRect skrect = SkIRect::MakeXYWH(rect.x(), rect.y(), width, height); | 170 SkIRect skrect = SkIRect::MakeXYWH(rect.x(), rect.y(), width, height); |
164 SkBitmap b; | 171 SkBitmap b; |
165 if (!canvas_->readPixels(skrect, &b)) | 172 if (!canvas_->readPixels(skrect, &b)) |
166 return false; | 173 return false; |
167 SkCanvas(output->GetBitmap()).writePixels(b, rect.x(), rect.y()); | 174 SkCanvas(output->GetBitmap()).writePixels(b, rect.x(), rect.y()); |
168 return true; | 175 return true; |
169 } | 176 } |
170 | 177 |
171 } // namespace content | 178 } // namespace content |
OLD | NEW |