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

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

Issue 2877483003: Implements core logic for Pixel Canvas (Closed)
Patch Set: Clean up PaintContext and rename scale to recording scale 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 offset_(other.offset_ + paint_recording_bounds_.OffsetFromOrigin()),
43 scale_type_(scale_type) {
44 ComputePaintRecordingScales(bounds.size());
29 #if DCHECK_IS_ON() 45 #if DCHECK_IS_ON()
30 root_visited_ = other.root_visited_; 46 root_visited_ = other.root_visited_;
31 inside_paint_recorder_ = other.inside_paint_recorder_; 47 inside_paint_recorder_ = other.inside_paint_recorder_;
32 #endif 48 #endif
33 } 49 }
34 50
35 PaintContext::PaintContext(const PaintContext& other, 51 PaintContext::PaintContext(const PaintContext& other,
36 CloneWithoutInvalidation c) 52 CloneWithoutInvalidation c)
37 : list_(other.list_), 53 : list_(other.list_),
38 device_scale_factor_(other.device_scale_factor_), 54 device_scale_factor_(other.device_scale_factor_),
55 paint_recording_scale_x_(other.paint_recording_scale_x_),
56 paint_recording_scale_y_(other.paint_recording_scale_y_),
39 invalidation_(), 57 invalidation_(),
58 paint_recording_bounds_(other.paint_recording_bounds_),
40 offset_(other.offset_) { 59 offset_(other.offset_) {
41 #if DCHECK_IS_ON() 60 #if DCHECK_IS_ON()
42 root_visited_ = other.root_visited_; 61 root_visited_ = other.root_visited_;
43 inside_paint_recorder_ = other.inside_paint_recorder_; 62 inside_paint_recorder_ = other.inside_paint_recorder_;
44 #endif 63 #endif
45 } 64 }
46 65
47 PaintContext::~PaintContext() { 66 PaintContext::~PaintContext() {
48 } 67 }
49 68
50 gfx::Rect PaintContext::ToLayerSpaceBounds( 69 gfx::Rect PaintContext::ToLayerSpaceBounds(
51 const gfx::Size& size_in_context) const { 70 const gfx::Size& size_in_context) const {
52 return gfx::Rect(size_in_context) + offset_; 71 return gfx::Rect(size_in_context) + offset_;
53 } 72 }
54 73
55 gfx::Rect PaintContext::ToLayerSpaceRect(const gfx::Rect& rect) const { 74 void PaintContext::ComputePaintRecordingScales(const gfx::Size& size) {
56 return rect + offset_; 75 if (!IsPixelCanvas()) {
76 paint_recording_scale_x_ = paint_recording_scale_y_ = 1.f;
77 return;
78 }
79
80 paint_recording_scale_x_ = paint_recording_scale_y_ = device_scale_factor_;
81 if (scale_type_ == ScaleType::SCALE_TO_SCALE_FACTOR)
82 return;
83
84 if (size.width() > 0) {
85 paint_recording_scale_x_ =
86 static_cast<float>(paint_recording_bounds_.width()) /
87 static_cast<float>(size.width());
88 }
89
90 if (size.height() > 0) {
91 paint_recording_scale_y_ =
92 static_cast<float>(paint_recording_bounds_.height()) /
93 static_cast<float>(size.height());
94 }
95 }
96
97 bool PaintContext::IsPixelCanvas() const {
98 return IsPixelCanvasRecordingEnabled();
99 }
100
101 gfx::Transform PaintContext::TransformToParentRecordingSpace() const {
102 gfx::Transform to_parent_pixel_space;
103 if (IsPixelCanvas()) {
104 // This is just an optimization to prevent the computation of effective
105 // scale factors which would return 1.f if the Pixel Canvas is disabled.
106 to_parent_pixel_space.Scale(SkFloatToScalar(paint_recording_scale_x_),
107 SkFloatToScalar(paint_recording_scale_y_));
108 }
109 to_parent_pixel_space.Translate(paint_recording_bounds().OffsetFromOrigin());
110 return to_parent_pixel_space;
111 }
112
113 gfx::Rect PaintContext::GetSnappedRecordingBounds(
114 const gfx::Rect& parent_bounds,
115 const gfx::Rect& child_bounds) const {
116 if (!IsPixelCanvas())
117 return child_bounds;
118
119 const gfx::Vector2d& child_origin = child_bounds.OffsetFromOrigin();
120
121 int right = child_origin.x() + child_bounds.width();
122 int bottom = child_origin.y() + child_bounds.height();
123
124 int new_x = std::round(child_origin.x() * device_scale_factor_);
125 int new_y = std::round(child_origin.y() * device_scale_factor_);
126
127 int new_right;
128 int new_bottom;
129
130 if (right == parent_bounds.width()) {
131 new_right = paint_recording_bounds_.width();
132 } else {
133 new_right = std::round(right * device_scale_factor_);
134 }
135
136 if (bottom == parent_bounds.height()) {
137 new_bottom = paint_recording_bounds_.height();
138 } else {
139 new_bottom = std::round(bottom * device_scale_factor_);
140 }
141 return gfx::Rect(new_x, new_y, new_right - new_x, new_bottom - new_y);
57 } 142 }
58 143
59 } // namespace ui 144 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698