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

Side by Side Diff: ui/compositor/paint_context.cc

Issue 2877483003: Implements core logic for Pixel Canvas (Closed)
Patch Set: nit Created 3 years, 6 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
OLDNEW
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 #include "ui/gfx/transform.h"
8 9
9 namespace ui { 10 namespace ui {
11 namespace {
12
13 // Scales the given |rect| by scaling its corner points.
14 gfx::Rect ScaleToCornerScaledRect(gfx::Rect rect,
15 float x_scale,
16 float y_scale = -1) {
17 y_scale = y_scale < 0 ? x_scale : y_scale;
18 const gfx::Vector2d& origin = rect.OffsetFromOrigin();
19
20 int right = origin.x() + rect.width();
21 int bottom = origin.y() + rect.height();
22
23 int new_x = std::round(origin.x() * x_scale);
24 int new_y = std::round(origin.y() * y_scale);
25
26 return gfx::Rect(new_x, new_y, std::round(right * x_scale) - new_x,
27 std::round(bottom * y_scale) - new_y);
28 }
29
30 } // namespace
10 31
11 PaintContext::PaintContext(cc::DisplayItemList* list, 32 PaintContext::PaintContext(cc::DisplayItemList* list,
12 float device_scale_factor, 33 float device_scale_factor,
13 const gfx::Rect& invalidation) 34 const gfx::Rect& invalidation,
35 const gfx::Size& size)
14 : list_(list), 36 : list_(list),
15 device_scale_factor_(device_scale_factor), 37 device_scale_factor_(device_scale_factor),
16 invalidation_(invalidation) { 38 invalidation_(gfx::ScaleToEnclosingRect(
39 invalidation,
40 IsPixelCanvas() ? device_scale_factor : 1.f)),
41 bounds_(size),
42 pixel_bounds_(ScaleToCornerScaledRect(
danakj 2017/06/27 16:28:36 It is weird to me that this context has a size now
malaykeshav 2017/07/07 22:57:09 Agreed on the fact that the PaintContext does not
43 bounds_,
44 IsPixelCanvas() ? device_scale_factor : 1.f)) {
17 #if DCHECK_IS_ON() 45 #if DCHECK_IS_ON()
18 root_visited_ = nullptr; 46 root_visited_ = nullptr;
19 inside_paint_recorder_ = false; 47 inside_paint_recorder_ = false;
20 #endif 48 #endif
21 } 49 }
22 50
23 PaintContext::PaintContext(const PaintContext& other, 51 PaintContext::PaintContext(const PaintContext& other,
24 const gfx::Vector2d& offset) 52 const gfx::Rect& bounds,
53 int scale_type)
25 : list_(other.list_), 54 : list_(other.list_),
26 device_scale_factor_(other.device_scale_factor_), 55 device_scale_factor_(other.device_scale_factor_),
27 invalidation_(other.invalidation_), 56 invalidation_(other.invalidation_),
28 offset_(other.offset_ + offset) { 57 bounds_(bounds),
58 pixel_bounds_(other.GetSnappedPixelBounds(bounds_)),
59 offset_(other.offset_ + pixel_bounds_.OffsetFromOrigin()),
60 scale_type_(scale_type) {
29 #if DCHECK_IS_ON() 61 #if DCHECK_IS_ON()
30 root_visited_ = other.root_visited_; 62 root_visited_ = other.root_visited_;
31 inside_paint_recorder_ = other.inside_paint_recorder_; 63 inside_paint_recorder_ = other.inside_paint_recorder_;
32 #endif 64 #endif
33 } 65 }
34 66
35 PaintContext::PaintContext(const PaintContext& other, 67 PaintContext::PaintContext(const PaintContext& other,
36 CloneWithoutInvalidation c) 68 CloneWithoutInvalidation c)
37 : list_(other.list_), 69 : list_(other.list_),
38 device_scale_factor_(other.device_scale_factor_), 70 device_scale_factor_(other.device_scale_factor_),
39 invalidation_(), 71 invalidation_(),
72 bounds_(other.bounds_),
73 pixel_bounds_(other.pixel_bounds_),
40 offset_(other.offset_) { 74 offset_(other.offset_) {
41 #if DCHECK_IS_ON() 75 #if DCHECK_IS_ON()
42 root_visited_ = other.root_visited_; 76 root_visited_ = other.root_visited_;
43 inside_paint_recorder_ = other.inside_paint_recorder_; 77 inside_paint_recorder_ = other.inside_paint_recorder_;
44 #endif 78 #endif
45 } 79 }
46 80
47 PaintContext::~PaintContext() { 81 PaintContext::~PaintContext() {
48 } 82 }
49 83
50 gfx::Rect PaintContext::ToLayerSpaceBounds( 84 gfx::Rect PaintContext::ToLayerSpaceBounds(
51 const gfx::Size& size_in_context) const { 85 const gfx::Size& size_in_context) const {
52 return gfx::Rect(size_in_context) + offset_; 86 return gfx::Rect(size_in_context) + offset_;
53 } 87 }
54 88
55 gfx::Rect PaintContext::ToLayerSpaceRect(const gfx::Rect& rect) const { 89 float PaintContext::effective_scale_factor_x() const {
56 return rect + offset_; 90 if (scale_type_ == SCALE_TO_SCALE_FACTOR || pixel_bounds_.width() == 0)
91 return IsPixelCanvas() ? device_scale_factor_ : 1.f;
92 return static_cast<float>(pixel_bounds_.width()) /
93 static_cast<float>(bounds_.width());
94 }
95
96 float PaintContext::effective_scale_factor_y() const {
97 if (scale_type_ == SCALE_TO_SCALE_FACTOR || pixel_bounds_.height() == 0)
98 return IsPixelCanvas() ? device_scale_factor_ : 1.f;
99 return static_cast<float>(pixel_bounds_.height()) /
100 static_cast<float>(bounds_.height());
101 }
102
103 gfx::Size PaintContext::ScaleToCornerScaledPixelSize(
104 const gfx::Size& size) const {
105 // effective_scale_factor_x/y() would return 1.f value if pixel canvas is
106 // disabled. This early return is only an optimization.
107 if (!IsPixelCanvas())
108 return size;
109 return ScaleToCornerScaledRect(gfx::Rect(size), effective_scale_factor_x(),
110 effective_scale_factor_y())
111 .size();
112 }
113
114 gfx::Transform PaintContext::TransformToParentPixelSpace() const {
115 gfx::Transform to_parent_pixel_space;
116 if (IsPixelCanvas()) {
117 // This is just an optimization to prevent the computation of effective
118 // scale factors which would return 1.f if the Pixel Canvas is disabled.
119 to_parent_pixel_space.Scale(SkFloatToScalar(effective_scale_factor_x()),
120 SkFloatToScalar(effective_scale_factor_y()));
121 }
122 to_parent_pixel_space.Translate(pixel_bounds().OffsetFromOrigin());
123 return to_parent_pixel_space;
124 }
125
126 gfx::Rect PaintContext::GetSnappedPixelBounds(
127 const gfx::Rect& child_bounds) const {
128 if (!IsPixelCanvas())
129 return child_bounds;
130
131 const gfx::Vector2d& child_origin = child_bounds.OffsetFromOrigin();
132
133 int right = child_origin.x() + child_bounds.width();
134 int bottom = child_origin.y() + child_bounds.height();
135
136 int new_x = std::round(child_origin.x() * device_scale_factor_);
137 int new_y = std::round(child_origin.y() * device_scale_factor_);
138
139 int new_right;
140 int new_bottom;
141
142 if (right == bounds_.width()) {
143 new_right = pixel_bounds_.width();
144 } else {
145 new_right = std::round(right * device_scale_factor_);
146 }
147
148 if (bottom == bounds_.height()) {
149 new_bottom = pixel_bounds_.height();
150 } else {
151 new_bottom = std::round(bottom * device_scale_factor_);
152 }
153 return gfx::Rect(new_x, new_y, new_right - new_x, new_bottom - new_y);
57 } 154 }
58 155
59 } // namespace ui 156 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698