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

Side by Side Diff: ui/views/paint_info.cc

Issue 2877483003: Implements core logic for Pixel Canvas (Closed)
Patch Set: Sync With ToT Created 3 years, 4 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
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/views/paint_info.h"
6
7 namespace views {
8
9 // static
10 PaintInfo PaintInfo::CreateRootPaintInfo(const ui::PaintContext& root_context,
11 const gfx::Size& size) {
12 return PaintInfo(root_context, size);
13 }
14
15 // static
16 PaintInfo PaintInfo::CreateChildPaintInfo(const PaintInfo& parent_paint_info,
17 const gfx::Rect& bounds,
18 const gfx::Size& parent_size,
19 ScaleType scale_type) {
20 return PaintInfo(parent_paint_info, bounds, parent_size, scale_type);
21 }
22
23 // static
24 PaintInfo PaintInfo::ClonePaintInfo(const PaintInfo& parent_paint_info) {
25 return PaintInfo(parent_paint_info,
26 ui::PaintContext::CLONE_WITHOUT_INVALIDATION);
27 }
28
29 PaintInfo::PaintInfo(const PaintInfo& other)
30 : paint_recording_scale_x_(other.paint_recording_scale_x_),
31 paint_recording_scale_y_(other.paint_recording_scale_y_),
32 paint_recording_bounds_(other.paint_recording_bounds_),
33 offset_from_parent_(other.offset_from_parent_),
34 context_(other.context(), gfx::Vector2d()),
35 root_context_(nullptr) {}
36
37 PaintInfo::PaintInfo(const ui::PaintContext& root_context,
38 const gfx::Size& size)
39 : paint_recording_scale_x_(root_context.is_pixel_canvas()
40 ? root_context.device_scale_factor()
41 : 1.f),
42 paint_recording_scale_y_(paint_recording_scale_x_),
43 paint_recording_bounds_(
44 gfx::ScaleToRoundedRect(gfx::Rect(size), paint_recording_scale_x_)),
45 context_(root_context, gfx::Vector2d()),
46 root_context_(&root_context) {}
47
48 PaintInfo::PaintInfo(const PaintInfo& parent_paint_info,
49 const gfx::Rect& bounds,
50 const gfx::Size& parent_size,
51 ScaleType scale_type)
52 : paint_recording_scale_x_(1.f),
53 paint_recording_scale_y_(1.f),
54 paint_recording_bounds_(
55 parent_paint_info.GetSnappedRecordingBounds(parent_size, bounds)),
56 offset_from_parent_(
57 paint_recording_bounds_.OffsetFromOrigin() -
58 parent_paint_info.paint_recording_bounds_.OffsetFromOrigin()),
59 context_(parent_paint_info.context(), offset_from_parent_),
60 root_context_(nullptr) {
61 if (IsPixelCanvas()) {
62 if (scale_type == ScaleType::kScaleToScaleFactor) {
63 paint_recording_scale_x_ = paint_recording_scale_y_ =
64 context().device_scale_factor();
65 } else if (scale_type == ScaleType::kScaleToFit) {
66 if (bounds.size().width() > 0) {
67 paint_recording_scale_x_ =
68 static_cast<float>(paint_recording_bounds_.width()) /
69 static_cast<float>(bounds.size().width());
70 }
71 if (bounds.size().height() > 0) {
72 paint_recording_scale_y_ =
73 static_cast<float>(paint_recording_bounds_.height()) /
74 static_cast<float>(bounds.size().height());
75 }
76 }
77 }
78 }
79
80 PaintInfo::PaintInfo(const PaintInfo& other,
81 ui::PaintContext::CloneWithoutInvalidation c)
82 : paint_recording_scale_x_(other.paint_recording_scale_x_),
83 paint_recording_scale_y_(other.paint_recording_scale_y_),
84 paint_recording_bounds_(other.paint_recording_bounds_),
85 offset_from_parent_(other.offset_from_parent_),
86 context_(other.context(), c),
87 root_context_(nullptr) {}
88
89 PaintInfo::~PaintInfo() {}
90
91 bool PaintInfo::IsPixelCanvas() const {
92 return context().is_pixel_canvas();
93 }
94
95 gfx::Rect PaintInfo::GetSnappedRecordingBounds(
96 const gfx::Size& parent_size,
97 const gfx::Rect& child_bounds) const {
98 if (!IsPixelCanvas())
99 return (child_bounds + paint_recording_bounds_.OffsetFromOrigin());
100
101 const gfx::Vector2d& child_origin = child_bounds.OffsetFromOrigin();
102
103 int right = child_origin.x() + child_bounds.width();
104 int bottom = child_origin.y() + child_bounds.height();
105
106 int new_x = std::round(child_origin.x() * context().device_scale_factor());
107 int new_y = std::round(child_origin.y() * context().device_scale_factor());
108
109 int new_right;
110 int new_bottom;
111
112 if (right == parent_size.width())
113 new_right = paint_recording_bounds_.width();
114 else
115 new_right = std::round(right * context().device_scale_factor());
116
117 if (bottom == parent_size.height())
118 new_bottom = paint_recording_bounds_.height();
119 else
120 new_bottom = std::round(bottom * context().device_scale_factor());
121
122 return gfx::Rect(new_x + paint_recording_bounds_.x(),
123 new_y + paint_recording_bounds_.y(), new_right - new_x,
124 new_bottom - new_y);
125 }
126
127 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/paint_info.h ('k') | ui/views/paint_info_unittest.cc » ('j') | ui/views/view.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698