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

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

Issue 2877483003: Implements core logic for Pixel Canvas (Closed)
Patch Set: Merged PaintContext Offset into paint_recording_bounds_ Created 3 years, 5 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/compositor/compositor_switches.h"
7 #include "ui/gfx/canvas.h" 8 #include "ui/gfx/canvas.h"
9 #include "ui/gfx/transform.h"
8 10
9 namespace ui { 11 namespace ui {
10 12
11 PaintContext::PaintContext(cc::DisplayItemList* list, 13 PaintContext::PaintContext(cc::DisplayItemList* list,
12 float device_scale_factor, 14 float device_scale_factor,
13 const gfx::Rect& invalidation) 15 const gfx::Rect& invalidation,
16 const gfx::Size& size)
14 : list_(list), 17 : list_(list),
15 device_scale_factor_(device_scale_factor), 18 device_scale_factor_(device_scale_factor),
16 invalidation_(invalidation) { 19 paint_recording_scale_x_(IsPixelCanvas() ? device_scale_factor_ : 1.f),
20 paint_recording_scale_y_(paint_recording_scale_x_),
21 invalidation_(
22 gfx::ScaleToEnclosingRect(invalidation, paint_recording_scale_x_)),
23 paint_recording_bounds_(
24 gfx::ScaleToCornerScaledRect(gfx::Rect(size),
25 paint_recording_scale_x_)) {
26 ComputePaintRecordingScales(size);
17 #if DCHECK_IS_ON() 27 #if DCHECK_IS_ON()
18 root_visited_ = nullptr; 28 root_visited_ = nullptr;
19 inside_paint_recorder_ = false; 29 inside_paint_recorder_ = false;
20 #endif 30 #endif
21 } 31 }
22 32
23 PaintContext::PaintContext(const PaintContext& other, 33 PaintContext::PaintContext(const PaintContext& other,
24 const gfx::Vector2d& offset) 34 const gfx::Rect& bounds,
35 const gfx::Rect& parent_bounds,
36 ScaleType scale_type)
25 : list_(other.list_), 37 : list_(other.list_),
26 device_scale_factor_(other.device_scale_factor_), 38 device_scale_factor_(other.device_scale_factor_),
27 invalidation_(other.invalidation_), 39 invalidation_(other.invalidation_),
28 offset_(other.offset_ + offset) { 40 paint_recording_bounds_(
41 other.GetSnappedRecordingBounds(parent_bounds, bounds)),
42 scale_type_(scale_type) {
43 ComputePaintRecordingScales(bounds.size());
29 #if DCHECK_IS_ON() 44 #if DCHECK_IS_ON()
30 root_visited_ = other.root_visited_; 45 root_visited_ = other.root_visited_;
31 inside_paint_recorder_ = other.inside_paint_recorder_; 46 inside_paint_recorder_ = other.inside_paint_recorder_;
32 #endif 47 #endif
33 } 48 }
34 49
35 PaintContext::PaintContext(const PaintContext& other, 50 PaintContext::PaintContext(const PaintContext& other,
36 CloneWithoutInvalidation c) 51 CloneWithoutInvalidation c)
37 : list_(other.list_), 52 : list_(other.list_),
38 device_scale_factor_(other.device_scale_factor_), 53 device_scale_factor_(other.device_scale_factor_),
54 paint_recording_scale_x_(other.paint_recording_scale_x_),
55 paint_recording_scale_y_(other.paint_recording_scale_y_),
39 invalidation_(), 56 invalidation_(),
40 offset_(other.offset_) { 57 paint_recording_bounds_(other.paint_recording_bounds_) {
41 #if DCHECK_IS_ON() 58 #if DCHECK_IS_ON()
42 root_visited_ = other.root_visited_; 59 root_visited_ = other.root_visited_;
43 inside_paint_recorder_ = other.inside_paint_recorder_; 60 inside_paint_recorder_ = other.inside_paint_recorder_;
44 #endif 61 #endif
45 } 62 }
46 63
47 PaintContext::~PaintContext() { 64 PaintContext::~PaintContext() {
48 } 65 }
49 66
50 gfx::Rect PaintContext::ToLayerSpaceBounds( 67 gfx::Rect PaintContext::ToLayerSpaceBounds(
51 const gfx::Size& size_in_context) const { 68 const gfx::Size& size_in_context) const {
52 return gfx::Rect(size_in_context) + offset_; 69 return gfx::Rect(paint_recording_bounds_.origin(), size_in_context);
53 } 70 }
54 71
55 gfx::Rect PaintContext::ToLayerSpaceRect(const gfx::Rect& rect) const { 72 void PaintContext::ComputePaintRecordingScales(const gfx::Size& size) {
56 return rect + offset_; 73 if (!IsPixelCanvas()) {
74 paint_recording_scale_x_ = paint_recording_scale_y_ = 1.f;
75 return;
76 }
77
78 paint_recording_scale_x_ = paint_recording_scale_y_ = device_scale_factor_;
79 if (scale_type_ == ScaleType::SCALE_TO_SCALE_FACTOR)
80 return;
81
82 if (size.width() > 0) {
83 paint_recording_scale_x_ =
84 static_cast<float>(paint_recording_bounds_.width()) /
85 static_cast<float>(size.width());
86 }
87
88 if (size.height() > 0) {
89 paint_recording_scale_y_ =
90 static_cast<float>(paint_recording_bounds_.height()) /
91 static_cast<float>(size.height());
92 }
93 }
94
95 bool PaintContext::IsPixelCanvas() const {
96 return IsPixelCanvasRecordingEnabled();
97 }
98
99 gfx::Transform PaintContext::TransformToParentRecordingSpace(
100 const gfx::Vector2d& offset_from_parent) const {
101 gfx::Transform to_parent_recording_space;
102 to_parent_recording_space.Scale(SkFloatToScalar(paint_recording_scale_x_),
103 SkFloatToScalar(paint_recording_scale_y_));
104 to_parent_recording_space.Translate(offset_from_parent);
105 return to_parent_recording_space;
106 }
107
108 gfx::Rect PaintContext::GetSnappedRecordingBounds(
109 const gfx::Rect& parent_bounds,
110 const gfx::Rect& child_bounds) const {
111 if (!IsPixelCanvas())
112 return (child_bounds + paint_recording_bounds().OffsetFromOrigin());
113
114 const gfx::Vector2d& child_origin = child_bounds.OffsetFromOrigin();
115
116 int right = child_origin.x() + child_bounds.width();
117 int bottom = child_origin.y() + child_bounds.height();
118
119 int new_x = std::round(child_origin.x() * device_scale_factor_);
120 int new_y = std::round(child_origin.y() * device_scale_factor_);
121
122 int new_right;
123 int new_bottom;
124
125 if (right == parent_bounds.width()) {
126 new_right = paint_recording_bounds_.width();
127 } else {
128 new_right = std::round(right * device_scale_factor_);
129 }
130
131 if (bottom == parent_bounds.height()) {
132 new_bottom = paint_recording_bounds_.height();
133 } else {
134 new_bottom = std::round(bottom * device_scale_factor_);
135 }
136 return gfx::Rect(new_x + paint_recording_bounds().x(),
137 new_y + paint_recording_bounds().y(), new_right - new_x,
138 new_bottom - new_y);
57 } 139 }
58 140
59 } // namespace ui 141 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698