OLD | NEW |
---|---|
(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 #include <vector> | |
8 | |
9 #include "base/macros.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "cc/base/region.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 #include "ui/compositor/compositor_switches.h" | |
14 #include "ui/compositor/paint_context.h" | |
15 #include "ui/gfx/geometry/rect.h" | |
16 #include "ui/gfx/geometry/size.h" | |
17 | |
18 namespace views { | |
19 namespace { | |
20 | |
21 typedef testing::Test PaintInfoTest; | |
22 | |
23 // Device scale factors | |
24 constexpr float DSF100 = 1.f; | |
25 constexpr float DSF125 = 1.25f; | |
26 constexpr float DSF150 = 1.5f; | |
27 constexpr float DSF160 = 1.6f; | |
28 constexpr float DSF166 = 1.66f; | |
29 | |
30 const std::vector<float> kDsfList = {DSF100, DSF125, DSF150, DSF160, DSF166}; | |
31 | |
32 const gfx::Size kLayerSize(123, 456); | |
33 | |
34 // ___________ | |
35 // | 1 | | |
36 // |___________| | |
37 // | 3 | 4 | 5 | <-- 2 | |
38 // |___|___|___| | |
39 // | 7 | 8 | <-- 6 | |
40 // |_______|___| | |
41 const gfx::Rect r_0(kLayerSize); | |
sky
2017/07/26 22:12:12
constexpr on these.
malaykeshav
2017/07/28 01:24:38
Done
| |
42 | |
43 const gfx::Rect r_0_1(0, 0, 123, 152); | |
44 | |
45 const gfx::Rect r_0_2(0, 152, 123, 152); | |
46 const gfx::Rect r_2_3(0, 0, 41, 152); | |
47 const gfx::Rect r_2_4(41, 0, 41, 152); | |
48 const gfx::Rect r_2_5(82, 0, 41, 152); | |
49 | |
50 const gfx::Rect r_0_6(0, 304, 123, 152); | |
51 const gfx::Rect r_6_7(0, 0, 82, 152); | |
52 const gfx::Rect r_6_8(82, 0, 41, 152); | |
53 | |
54 // ___________ | |
55 // | 1 | | |
56 // |___________| | |
57 // | 3 | 4 | 5 | <-- 2 | |
sky
2017/07/26 22:12:12
The <-- 2 isn't clear at all. Maybe you mean there
malaykeshav
2017/07/28 01:24:38
Done. Adding further comments
| |
58 // |___|___|___| | |
59 // | 7 | 8 | <-- 6 | |
60 // |_______|___| | |
61 // Returns the following arrangement of paint recording bounds for the given | |
62 // |dsf| | |
63 std::vector<PaintInfo*> GetPaintInfoSetup(const ui::PaintContext& context) { | |
sky
2017/07/26 22:12:12
Passing around raw pointers like this is a recipe
malaykeshav
2017/07/28 01:24:38
Done
| |
64 std::vector<PaintInfo*> info_list(9); | |
65 | |
66 info_list[0] = new PaintInfo(context, kLayerSize); | |
67 | |
68 info_list[1] = new PaintInfo(*info_list[0], r_0_1, r_0, | |
69 PaintInfo::ScaleType::kScaleToFit); | |
70 | |
71 info_list[2] = new PaintInfo(*info_list[0], r_0_2, r_0, | |
72 PaintInfo::ScaleType::kScaleToFit); | |
73 info_list[3] = new PaintInfo(*info_list[2], r_2_3, r_0_2, | |
74 PaintInfo::ScaleType::kScaleToFit); | |
75 info_list[4] = new PaintInfo(*info_list[2], r_2_4, r_0_2, | |
76 PaintInfo::ScaleType::kScaleToFit); | |
77 info_list[5] = new PaintInfo(*info_list[2], r_2_5, r_0_2, | |
78 PaintInfo::ScaleType::kScaleToFit); | |
79 | |
80 info_list[6] = new PaintInfo(*info_list[0], r_0_6, r_0, | |
81 PaintInfo::ScaleType::kScaleToFit); | |
82 info_list[7] = new PaintInfo(*info_list[6], r_6_7, r_0_6, | |
83 PaintInfo::ScaleType::kScaleToFit); | |
84 info_list[8] = new PaintInfo(*info_list[6], r_6_8, r_0_6, | |
85 PaintInfo::ScaleType::kScaleToFit); | |
86 | |
87 return info_list; | |
88 } | |
89 | |
90 // Verifies that the child recording bounds completely cover the parent | |
91 // recording bounds. | |
92 void VerifyChildBoundsCoversParent(PaintInfo* parent_paint_info, | |
sky
2017/07/26 22:12:12
Can both of these parameters be const refs? That m
malaykeshav
2017/07/28 01:24:38
Done
| |
93 std::vector<PaintInfo*> info_list) { | |
94 cc::Region remaining(gfx::Rect(parent_paint_info->paint_recording_size())); | |
95 int times_empty = 0; | |
96 for (auto* paint_info : info_list) { | |
97 const gfx::Rect& child_recording_bounds = | |
98 paint_info->paint_recording_bounds() - | |
99 parent_paint_info->paint_recording_bounds().OffsetFromOrigin(); | |
100 EXPECT_TRUE(remaining.Contains(child_recording_bounds)) | |
101 << "Remaining: " << remaining.ToString() | |
102 << " paint recording bounds: " << child_recording_bounds.ToString(); | |
103 remaining.Subtract(child_recording_bounds); | |
104 times_empty += remaining.IsEmpty(); | |
105 } | |
106 EXPECT_EQ(times_empty, 1); | |
107 } | |
108 | |
109 void VerifyPixelCanvasCornerScaling(std::vector<PaintInfo*> info_list) { | |
110 // child 1, child 2 and child 6 should completely cover child 0. | |
111 std::vector<PaintInfo*> child_info_list; | |
112 child_info_list.push_back(info_list[1]); | |
113 child_info_list.push_back(info_list[2]); | |
114 child_info_list.push_back(info_list[6]); | |
115 VerifyChildBoundsCoversParent(info_list[0], child_info_list); | |
116 child_info_list.clear(); | |
117 | |
118 // Child 3,4 and 5 should completely cover child 2. | |
119 child_info_list.push_back(info_list[3]); | |
120 child_info_list.push_back(info_list[4]); | |
121 child_info_list.push_back(info_list[5]); | |
122 VerifyChildBoundsCoversParent(info_list[2], child_info_list); | |
123 child_info_list.clear(); | |
124 | |
125 // Child 7 and 8 should completely cover child 6. | |
126 child_info_list.push_back(info_list[7]); | |
127 child_info_list.push_back(info_list[8]); | |
128 VerifyChildBoundsCoversParent(info_list[6], child_info_list); | |
129 child_info_list.clear(); | |
130 } | |
131 | |
132 void ClearAllPaintInfo(std::vector<PaintInfo*> info_list) { | |
133 for (auto* paint_info : info_list) | |
134 delete paint_info; | |
135 } | |
136 | |
137 void VerifyPixelSizesAreSameAsDIPSize(std::vector<PaintInfo*> info_list) { | |
138 EXPECT_EQ(info_list[0]->paint_recording_size(), r_0.size()); | |
139 | |
140 EXPECT_EQ(info_list[1]->paint_recording_size(), r_0_1.size()); | |
141 | |
142 EXPECT_EQ(info_list[2]->paint_recording_size(), r_0_2.size()); | |
143 EXPECT_EQ(info_list[3]->paint_recording_size(), r_2_3.size()); | |
144 EXPECT_EQ(info_list[4]->paint_recording_size(), r_2_4.size()); | |
145 EXPECT_EQ(info_list[5]->paint_recording_size(), r_2_5.size()); | |
146 | |
147 EXPECT_EQ(info_list[6]->paint_recording_size(), r_0_6.size()); | |
148 EXPECT_EQ(info_list[7]->paint_recording_size(), r_6_7.size()); | |
149 EXPECT_EQ(info_list[8]->paint_recording_size(), r_6_8.size()); | |
150 } | |
151 | |
152 void VerifyInvalidationRects(float dsf, bool pixel_canvas_enabled) { | |
153 std::vector<gfx::Rect> invalidation_rects = { | |
154 gfx::Rect(0, 0, 123, 41), // Intersects with 0 & 1. | |
155 gfx::Rect(0, 76, 60, 152), // Intersects 0, 1, 2, 3 & 4. | |
156 gfx::Rect(41, 152, 41, 152), // Intersects with 0, 2 & 4. | |
157 gfx::Rect(80, 320, 4, 4), // Intersects with 0, 6, 7 & 8. | |
158 gfx::Rect(40, 151, 43, 154), // Intersects all | |
159 gfx::Rect(82, 304, 1, 1), // Intersects with 0, 6 & 8. | |
160 gfx::Rect(81, 303, 2, 2) // Intersects with 0, 2, 4, 5, 6, 7, 8 | |
161 }; | |
162 | |
163 std::vector<std::vector<int>> repaint_indices = { | |
164 std::vector<int>{0, 1}, | |
165 std::vector<int>{0, 1, 2, 3, 4}, | |
166 std::vector<int>{0, 2, 4}, | |
167 std::vector<int>{0, 6, 7, 8}, | |
168 std::vector<int>{0, 1, 2, 3, 4, 5, 6, 7, 8}, | |
169 std::vector<int>{0, 6, 8}, | |
170 std::vector<int>{0, 2, 4, 5, 6, 7, 8}}; | |
171 | |
172 std::vector<PaintInfo*> info_list; | |
173 | |
174 EXPECT_EQ(repaint_indices.size(), invalidation_rects.size()); | |
175 for (size_t i = 0; i < invalidation_rects.size(); i++) { | |
176 ui::PaintContext context(nullptr, dsf, invalidation_rects[i], | |
177 pixel_canvas_enabled); | |
178 info_list = GetPaintInfoSetup(context); | |
179 for (size_t j = 0; j < repaint_indices[i].size(); j++) { | |
180 EXPECT_TRUE(info_list[repaint_indices[i][j]]->context().IsRectInvalid( | |
181 gfx::Rect(info_list[repaint_indices[i][j]]->paint_recording_size()))); | |
182 } | |
183 ClearAllPaintInfo(info_list); | |
184 } | |
185 } | |
186 | |
187 } // namespace | |
188 | |
189 TEST_F(PaintInfoTest, CornerScalingPixelCanvasEnabled) { | |
190 std::vector<PaintInfo*> info_list; | |
191 for (float dsf : kDsfList) { | |
192 ui::PaintContext context(nullptr, dsf, gfx::Rect(), true); | |
193 info_list = GetPaintInfoSetup(context); | |
194 VerifyPixelCanvasCornerScaling(info_list); | |
195 ClearAllPaintInfo(info_list); | |
196 } | |
197 | |
198 // More accurate testing for 1.25 dsf | |
199 ui::PaintContext context(nullptr, DSF125, gfx::Rect(), true); | |
200 info_list = GetPaintInfoSetup(context); | |
201 VerifyPixelCanvasCornerScaling(info_list); | |
202 EXPECT_EQ(info_list[0]->paint_recording_size(), gfx::Size(154, 570)); | |
203 | |
204 EXPECT_EQ(info_list[1]->paint_recording_size(), gfx::Size(154, 190)); | |
205 | |
206 EXPECT_EQ(info_list[2]->paint_recording_bounds(), | |
207 gfx::Rect(0, 190, 154, 190)); | |
208 EXPECT_EQ(info_list[3]->paint_recording_size(), gfx::Size(51, 190)); | |
209 EXPECT_EQ(info_list[4]->paint_recording_bounds(), | |
210 gfx::Rect(51, 190, 52, 190)); | |
211 EXPECT_EQ(info_list[5]->paint_recording_bounds(), | |
212 gfx::Rect(103, 190, 51, 190)); | |
213 | |
214 EXPECT_EQ(info_list[6]->paint_recording_bounds(), | |
215 gfx::Rect(0, 380, 154, 190)); | |
216 EXPECT_EQ(info_list[7]->paint_recording_size(), gfx::Size(103, 190)); | |
217 EXPECT_EQ(info_list[8]->paint_recording_bounds(), | |
218 gfx::Rect(103, 380, 51, 190)); | |
219 ClearAllPaintInfo(info_list); | |
220 } | |
221 | |
222 TEST_F(PaintInfoTest, ScalingWithPixelCanvasDisabled) { | |
223 // With dsf as 1, there should be no scaling and size should be same. | |
224 ui::PaintContext context_1(nullptr, DSF100, gfx::Rect(), false); | |
225 std::vector<PaintInfo*> info_list = GetPaintInfoSetup(context_1); | |
226 for (float dsf : kDsfList) { | |
227 ui::PaintContext context_2(nullptr, dsf, gfx::Rect(), false); | |
228 info_list = GetPaintInfoSetup(context_2); | |
229 VerifyPixelCanvasCornerScaling(info_list); | |
230 VerifyPixelSizesAreSameAsDIPSize(info_list); | |
231 ClearAllPaintInfo(info_list); | |
232 } | |
233 } | |
234 | |
235 TEST_F(PaintInfoTest, Invalidation) { | |
236 for (float dsf : kDsfList) { | |
237 VerifyInvalidationRects(dsf, false); | |
238 VerifyInvalidationRects(dsf, true); | |
239 } | |
240 } | |
241 | |
242 } // namespace views | |
OLD | NEW |