OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ui/compositor/paint_context.h" | 5 #include "ui/compositor/paint_context.h" |
6 | 6 |
7 #include "ui/gfx/canvas.h" | 7 #include "ui/gfx/canvas.h" |
8 | 8 |
9 namespace ui { | 9 namespace ui { |
| 10 namespace { |
| 11 |
| 12 // Scales the given |rect| by scaling its corner points. |
| 13 gfx::Rect ScaleToCornerScaledRect(gfx::Rect rect, |
| 14 float x_scale, |
| 15 float y_scale = -1) { |
| 16 y_scale = y_scale < 0 ? x_scale : y_scale; |
| 17 const gfx::Vector2d& origin = rect.OffsetFromOrigin(); |
| 18 |
| 19 int right = origin.x() + rect.width(); |
| 20 int bottom = origin.y() + rect.height(); |
| 21 |
| 22 int new_x = std::round(origin.x() * x_scale); |
| 23 int new_y = std::round(origin.y() * y_scale); |
| 24 |
| 25 return gfx::Rect(new_x, new_y, std::round(right * x_scale) - new_x, |
| 26 std::round(bottom * y_scale) - new_y); |
| 27 } |
| 28 |
| 29 } // namespace |
10 | 30 |
11 PaintContext::PaintContext(cc::DisplayItemList* list, | 31 PaintContext::PaintContext(cc::DisplayItemList* list, |
12 float device_scale_factor, | 32 float device_scale_factor, |
13 const gfx::Rect& invalidation) | 33 const gfx::Rect& invalidation, |
| 34 const gfx::Size& size) |
14 : list_(list), | 35 : list_(list), |
15 device_scale_factor_(device_scale_factor), | 36 device_scale_factor_(device_scale_factor), |
16 invalidation_(invalidation) { | 37 invalidation_(gfx::ScaleToEnclosingRect( |
| 38 invalidation, |
| 39 IsPixelCanvas() ? device_scale_factor : 1.f)), |
| 40 bounds_(size), |
| 41 pixel_bounds_(ScaleToCornerScaledRect( |
| 42 bounds_, |
| 43 IsPixelCanvas() ? device_scale_factor : 1.f)) { |
17 #if DCHECK_IS_ON() | 44 #if DCHECK_IS_ON() |
18 root_visited_ = nullptr; | 45 root_visited_ = nullptr; |
19 inside_paint_recorder_ = false; | 46 inside_paint_recorder_ = false; |
20 #endif | 47 #endif |
21 } | 48 } |
22 | 49 |
23 PaintContext::PaintContext(const PaintContext& other, | 50 PaintContext::PaintContext(const PaintContext& other, |
24 const gfx::Vector2d& offset) | 51 const gfx::Rect& bounds, |
| 52 int scale_type) |
25 : list_(other.list_), | 53 : list_(other.list_), |
26 device_scale_factor_(other.device_scale_factor_), | 54 device_scale_factor_(other.device_scale_factor_), |
27 invalidation_(other.invalidation_), | 55 invalidation_(other.invalidation_), |
28 offset_(other.offset_ + offset) { | 56 bounds_(bounds), |
| 57 pixel_bounds_(other.GetSnappedPixelBounds(bounds_)), |
| 58 offset_(other.offset_ + pixel_bounds_.OffsetFromOrigin()), |
| 59 scale_type_(scale_type) { |
29 #if DCHECK_IS_ON() | 60 #if DCHECK_IS_ON() |
30 root_visited_ = other.root_visited_; | 61 root_visited_ = other.root_visited_; |
31 inside_paint_recorder_ = other.inside_paint_recorder_; | 62 inside_paint_recorder_ = other.inside_paint_recorder_; |
32 #endif | 63 #endif |
33 } | 64 } |
34 | 65 |
35 PaintContext::PaintContext(const PaintContext& other, | 66 PaintContext::PaintContext(const PaintContext& other, |
36 CloneWithoutInvalidation c) | 67 CloneWithoutInvalidation c) |
37 : list_(other.list_), | 68 : list_(other.list_), |
38 device_scale_factor_(other.device_scale_factor_), | 69 device_scale_factor_(other.device_scale_factor_), |
39 invalidation_(), | 70 invalidation_(), |
| 71 bounds_(other.bounds_), |
| 72 pixel_bounds_(other.pixel_bounds_), |
40 offset_(other.offset_) { | 73 offset_(other.offset_) { |
41 #if DCHECK_IS_ON() | 74 #if DCHECK_IS_ON() |
42 root_visited_ = other.root_visited_; | 75 root_visited_ = other.root_visited_; |
43 inside_paint_recorder_ = other.inside_paint_recorder_; | 76 inside_paint_recorder_ = other.inside_paint_recorder_; |
44 #endif | 77 #endif |
45 } | 78 } |
46 | 79 |
47 PaintContext::~PaintContext() { | 80 PaintContext::~PaintContext() { |
48 } | 81 } |
49 | 82 |
50 gfx::Rect PaintContext::ToLayerSpaceBounds( | 83 gfx::Rect PaintContext::ToLayerSpaceBounds( |
51 const gfx::Size& size_in_context) const { | 84 const gfx::Size& size_in_context) const { |
52 return gfx::Rect(size_in_context) + offset_; | 85 return gfx::Rect(size_in_context) + offset_; |
53 } | 86 } |
54 | 87 |
55 gfx::Rect PaintContext::ToLayerSpaceRect(const gfx::Rect& rect) const { | 88 float PaintContext::effective_scale_factor_x() const { |
56 return rect + offset_; | 89 if (scale_type_ == SCALE_TO_SCALE_FACTOR || pixel_bounds_.width() == 0) |
| 90 return IsPixelCanvas() ? device_scale_factor_ : 1.f; |
| 91 return static_cast<float>(pixel_bounds_.width()) / |
| 92 static_cast<float>(bounds_.width()); |
| 93 } |
| 94 |
| 95 float PaintContext::effective_scale_factor_y() const { |
| 96 if (scale_type_ == SCALE_TO_SCALE_FACTOR || pixel_bounds_.height() == 0) |
| 97 return IsPixelCanvas() ? device_scale_factor_ : 1.f; |
| 98 return static_cast<float>(pixel_bounds_.height()) / |
| 99 static_cast<float>(bounds_.height()); |
| 100 } |
| 101 |
| 102 gfx::Size PaintContext::ScaleToCorneredPixelSize(const gfx::Size& size) const { |
| 103 // effective_scale_factor_x/y() would return 1.f value if pixel canvas is |
| 104 // disabled. This early return is only an optimization. |
| 105 if (!IsPixelCanvas()) |
| 106 return size; |
| 107 return ScaleToCornerScaledRect(gfx::Rect(size), effective_scale_factor_x(), |
| 108 effective_scale_factor_y()) |
| 109 .size(); |
| 110 } |
| 111 |
| 112 gfx::Rect PaintContext::GetSnappedPixelBounds( |
| 113 const gfx::Rect& child_bounds) const { |
| 114 if (!IsPixelCanvas()) |
| 115 return child_bounds; |
| 116 |
| 117 const gfx::Vector2d& child_origin = child_bounds.OffsetFromOrigin(); |
| 118 |
| 119 int right = child_origin.x() + child_bounds.width(); |
| 120 int bottom = child_origin.y() + child_bounds.height(); |
| 121 |
| 122 int new_x = std::round(child_origin.x() * device_scale_factor_); |
| 123 int new_y = std::round(child_origin.y() * device_scale_factor_); |
| 124 |
| 125 int new_right; |
| 126 int new_bottom; |
| 127 |
| 128 if (right == bounds_.width()) { |
| 129 new_right = pixel_bounds_.width(); |
| 130 } else { |
| 131 new_right = std::round(right * device_scale_factor_); |
| 132 } |
| 133 |
| 134 if (bottom == bounds_.height()) { |
| 135 new_bottom = pixel_bounds_.height(); |
| 136 } else { |
| 137 new_bottom = std::round(bottom * device_scale_factor_); |
| 138 } |
| 139 return gfx::Rect(new_x, new_y, new_right - new_x, new_bottom - new_y); |
57 } | 140 } |
58 | 141 |
59 } // namespace ui | 142 } // namespace ui |
OLD | NEW |