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

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

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

Powered by Google App Engine
This is Rietveld 408576698