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

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

Issue 2877483003: Implements core logic for Pixel Canvas (Closed)
Patch Set: nits 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
(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/compositor/paint_context.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/gfx/geometry/rect.h"
14 #include "ui/gfx/geometry/size.h"
15
16 namespace ui {
17 namespace {
18
19 typedef testing::Test PaintContextTest;
20
21 // Device scale factors
22 constexpr float DSF100 = 1.f;
23 constexpr float DSF125 = 1.25f;
24 constexpr float DSF150 = 1.5f;
25 constexpr float DSF160 = 1.6f;
26 constexpr float DSF166 = 1.66f;
27
28 const std::vector<float> kDsfList = {DSF100, DSF125, DSF150, DSF160, DSF166};
29
30 const gfx::Size kLayerSize(123, 456);
31
32 // ___________
33 // | 1 |
34 // |___________|
35 // | 3 | 4 | 5 | <-- 2
36 // |___|___|___|
37 // | 7 | 8 | <-- 6
38 // |_______|___|
39 const gfx::Rect r_0(kLayerSize);
40
41 const gfx::Rect r_0_1(0, 0, 123, 152);
42
43 const gfx::Rect r_0_2(0, 152, 123, 152);
44 const gfx::Rect r_2_3(0, 0, 41, 152);
45 const gfx::Rect r_2_4(41, 0, 41, 152);
46 const gfx::Rect r_2_5(82, 0, 41, 152);
47
48 const gfx::Rect r_0_6(0, 304, 123, 152);
49 const gfx::Rect r_6_7(0, 0, 82, 152);
50 const gfx::Rect r_6_8(82, 0, 41, 152);
51
52 // Returns as DisplayItemList with the pixel canvas enabled or disabled based on
53 // |pixel_canvas_enabled|.
54 cc::DisplayItemList* GetDisplayItemList(bool pixel_canvas_enabled) {
55 cc::DisplayItemList* display_item_list = new cc::DisplayItemList;
56 display_item_list->set_pixel_canvas_enabled(pixel_canvas_enabled);
57 return std::move(display_item_list);
58 }
59
60 // ___________
61 // | 1 |
62 // |___________|
63 // | 3 | 4 | 5 | <-- 2
64 // |___|___|___|
65 // | 7 | 8 | <-- 6
66 // |_______|___|
67 // Returns the following arrangement of contexts for the given |dsf|
68 std::vector<PaintContext*> GetPaintContextSetup(bool pixel_canvas_enabled,
69 float dsf,
70 const gfx::Rect& invd_rect) {
71 std::vector<PaintContext*> contexts(9);
72 contexts[0] = new PaintContext(GetDisplayItemList(pixel_canvas_enabled), dsf,
73 invd_rect, kLayerSize);
74
75 contexts[1] =
76 new PaintContext(*contexts[0], r_0_1, PaintContext::SCALE_TO_FIT);
77
78 contexts[2] =
79 new PaintContext(*contexts[0], r_0_2, PaintContext::SCALE_TO_FIT);
80 contexts[3] =
81 new PaintContext(*contexts[2], r_2_3, PaintContext::SCALE_TO_FIT);
82 contexts[4] =
83 new PaintContext(*contexts[2], r_2_4, PaintContext::SCALE_TO_FIT);
84 contexts[5] =
85 new PaintContext(*contexts[2], r_2_5, PaintContext::SCALE_TO_FIT);
86
87 contexts[6] =
88 new PaintContext(*contexts[0], r_0_6, PaintContext::SCALE_TO_FIT);
89 contexts[7] =
90 new PaintContext(*contexts[6], r_6_7, PaintContext::SCALE_TO_FIT);
91 contexts[8] =
92 new PaintContext(*contexts[6], r_6_8, PaintContext::SCALE_TO_FIT);
93
94 return contexts;
95 }
96
97 // Verifies that the child contexts completely cover the parent context bounds.
98 void VerifyContextCoversParentContext(PaintContext* parent_context,
99 std::vector<PaintContext*> contexts) {
100 cc::Region remaining(gfx::Rect(parent_context->pixel_size()));
101 int times_empty = 0;
102 for (auto* context : contexts) {
103 const gfx::Rect& child_context_bounds = context->pixel_bounds();
104 EXPECT_TRUE(remaining.Contains(child_context_bounds))
105 << "Remaining: " << remaining.ToString()
106 << " context pixel bounds: " << child_context_bounds.ToString();
107 remaining.Subtract(child_context_bounds);
108 times_empty += remaining.IsEmpty();
109 }
110 EXPECT_EQ(times_empty, 1);
111 }
112
113 void VerifyPixelCanvasCornerScaling(std::vector<PaintContext*> contexts) {
114 // context 1, context 2 and context 6 should completely cover context 0.
115 std::vector<PaintContext*> child_contexts;
116 child_contexts.push_back(contexts[1]);
117 child_contexts.push_back(contexts[2]);
118 child_contexts.push_back(contexts[6]);
119 VerifyContextCoversParentContext(contexts[0], child_contexts);
120 child_contexts.clear();
121
122 // Context 3,4 and 5 should completely cover context 2.
123 child_contexts.push_back(contexts[3]);
124 child_contexts.push_back(contexts[4]);
125 child_contexts.push_back(contexts[5]);
126 VerifyContextCoversParentContext(contexts[2], child_contexts);
127 child_contexts.clear();
128
129 // Context 7 and 8 should completely cover context 6.
130 child_contexts.push_back(contexts[7]);
131 child_contexts.push_back(contexts[8]);
132 VerifyContextCoversParentContext(contexts[6], child_contexts);
133 child_contexts.clear();
134 }
135
136 void ClearAllContexts(std::vector<PaintContext*> contexts) {
137 for (auto* context : contexts)
138 delete context;
139 }
140
141 void VerifyPixelSizesAreSameAsDIPSize(std::vector<PaintContext*> contexts) {
142 EXPECT_EQ(contexts[0]->pixel_size(), r_0.size());
143
144 EXPECT_EQ(contexts[1]->pixel_size(), r_0_1.size());
145
146 EXPECT_EQ(contexts[2]->pixel_size(), r_0_2.size());
147 EXPECT_EQ(contexts[3]->pixel_size(), r_2_3.size());
148 EXPECT_EQ(contexts[4]->pixel_size(), r_2_4.size());
149 EXPECT_EQ(contexts[5]->pixel_size(), r_2_5.size());
150
151 EXPECT_EQ(contexts[6]->pixel_size(), r_0_6.size());
152 EXPECT_EQ(contexts[7]->pixel_size(), r_6_7.size());
153 EXPECT_EQ(contexts[8]->pixel_size(), r_6_8.size());
154 }
155
156 void VerifyInvalidationRects(float dsf, bool pixel_canvas_enabled) {
157 std::vector<gfx::Rect> invalidation_rects = {
158 gfx::Rect(0, 0, 123, 41), // Intersects with 0 & 1.
159 gfx::Rect(0, 76, 60, 152), // Intersects 0, 1, 2, 3 & 4.
160 gfx::Rect(41, 152, 41, 152), // Intersects with 0, 2 & 4.
161 gfx::Rect(80, 320, 4, 4), // Intersects with 0, 6, 7 & 8.
162 gfx::Rect(40, 151, 43, 154), // Intersects all
163 gfx::Rect(82, 304, 1, 1), // Intersects with 0, 6 & 8.
164 gfx::Rect(81, 303, 2, 2) // Intersects with 0, 2, 4, 5, 6, 7, 8
165 };
166
167 std::vector<std::vector<int>> repaint_context_indices = {
168 std::vector<int>{0, 1},
169 std::vector<int>{0, 1, 2, 3, 4},
170 std::vector<int>{0, 2, 4},
171 std::vector<int>{0, 6, 7, 8},
172 std::vector<int>{0, 1, 2, 3, 4, 5, 6, 7, 8},
173 std::vector<int>{0, 6, 8},
174 std::vector<int>{0, 2, 4, 5, 6, 7, 8}};
175
176 std::vector<PaintContext*> contexts;
177
178 EXPECT_EQ(repaint_context_indices.size(), invalidation_rects.size());
179 for (size_t i = 0; i < invalidation_rects.size(); i++) {
180 contexts =
181 GetPaintContextSetup(pixel_canvas_enabled, dsf, invalidation_rects[i]);
182 for (size_t j = 0; j < repaint_context_indices[i].size(); j++)
183 EXPECT_TRUE(contexts[repaint_context_indices[i][j]]->ShouldRepaint());
184 ClearAllContexts(contexts);
185 }
186 }
187
188 } // namespace
189
190 TEST_F(PaintContextTest, CornerScalingPixelCanvasEnabled) {
191 std::vector<PaintContext*> contexts;
192 for (float dsf : kDsfList) {
193 contexts = GetPaintContextSetup(true, dsf, gfx::Rect());
194 VerifyPixelCanvasCornerScaling(contexts);
195 ClearAllContexts(contexts);
196 }
197
198 // More accurate testing for 1.25 dsf
199 contexts = GetPaintContextSetup(true, DSF125, gfx::Rect());
200 VerifyPixelCanvasCornerScaling(contexts);
201 EXPECT_EQ(contexts[0]->pixel_size(), gfx::Size(154, 570));
202
203 EXPECT_EQ(contexts[1]->pixel_size(), gfx::Size(154, 190));
204
205 EXPECT_EQ(contexts[2]->pixel_bounds(), gfx::Rect(0, 190, 154, 190));
206 EXPECT_EQ(contexts[3]->pixel_size(), gfx::Size(51, 190));
207 EXPECT_EQ(contexts[4]->pixel_bounds(), gfx::Rect(51, 0, 52, 190));
208 EXPECT_EQ(contexts[5]->pixel_bounds(), gfx::Rect(103, 0, 51, 190));
209
210 EXPECT_EQ(contexts[6]->pixel_bounds(), gfx::Rect(0, 380, 154, 190));
211 EXPECT_EQ(contexts[7]->pixel_size(), gfx::Size(103, 190));
212 EXPECT_EQ(contexts[8]->pixel_bounds(), gfx::Rect(103, 0, 51, 190));
213 ClearAllContexts(contexts);
214 }
215
216 TEST_F(PaintContextTest, ScalingWithPixelCanvasDisabled) {
217 // With dsf as 1, there should be no scaling and size should be same.
218 std::vector<PaintContext*> contexts =
219 GetPaintContextSetup(false, DSF100, gfx::Rect());
220 for (float dsf : kDsfList) {
221 contexts = GetPaintContextSetup(false, dsf, gfx::Rect());
222 VerifyPixelCanvasCornerScaling(contexts);
223 VerifyPixelSizesAreSameAsDIPSize(contexts);
224 ClearAllContexts(contexts);
225 }
226 }
227
228 TEST_F(PaintContextTest, Invalidation) {
229 for (float dsf : kDsfList) {
230 VerifyInvalidationRects(dsf, false);
231 VerifyInvalidationRects(dsf, true);
232 }
233 }
234
235 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698