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/compositor/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 ui { |
| 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); |
| 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 |
| 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(bool pixel_canvas_enabled, |
| 64 const PaintContext& context) { |
| 65 ui::SetPixelCanvasForTesting(pixel_canvas_enabled); |
| 66 std::vector<PaintInfo*> info_list(9); |
| 67 |
| 68 info_list[0] = new PaintInfo(context, kLayerSize); |
| 69 |
| 70 info_list[1] = new PaintInfo(*info_list[0], r_0_1, r_0, |
| 71 PaintInfo::ScaleType::kScaleToFit); |
| 72 |
| 73 info_list[2] = new PaintInfo(*info_list[0], r_0_2, r_0, |
| 74 PaintInfo::ScaleType::kScaleToFit); |
| 75 info_list[3] = new PaintInfo(*info_list[2], r_2_3, r_0_2, |
| 76 PaintInfo::ScaleType::kScaleToFit); |
| 77 info_list[4] = new PaintInfo(*info_list[2], r_2_4, r_0_2, |
| 78 PaintInfo::ScaleType::kScaleToFit); |
| 79 info_list[5] = new PaintInfo(*info_list[2], r_2_5, r_0_2, |
| 80 PaintInfo::ScaleType::kScaleToFit); |
| 81 |
| 82 info_list[6] = new PaintInfo(*info_list[0], r_0_6, r_0, |
| 83 PaintInfo::ScaleType::kScaleToFit); |
| 84 info_list[7] = new PaintInfo(*info_list[6], r_6_7, r_0_6, |
| 85 PaintInfo::ScaleType::kScaleToFit); |
| 86 info_list[8] = new PaintInfo(*info_list[6], r_6_8, r_0_6, |
| 87 PaintInfo::ScaleType::kScaleToFit); |
| 88 |
| 89 return info_list; |
| 90 } |
| 91 |
| 92 // Verifies that the child recording bounds completely cover the parent |
| 93 // recording bounds. |
| 94 void VerifyChildBoundsCoversParent(PaintInfo* parent_paint_info, |
| 95 std::vector<PaintInfo*> info_list) { |
| 96 cc::Region remaining(gfx::Rect(parent_paint_info->paint_recording_size())); |
| 97 int times_empty = 0; |
| 98 for (auto* paint_info : info_list) { |
| 99 const gfx::Rect& child_recording_bounds = |
| 100 paint_info->paint_recording_bounds() - |
| 101 parent_paint_info->paint_recording_bounds().OffsetFromOrigin(); |
| 102 EXPECT_TRUE(remaining.Contains(child_recording_bounds)) |
| 103 << "Remaining: " << remaining.ToString() |
| 104 << " paint recording bounds: " << child_recording_bounds.ToString(); |
| 105 remaining.Subtract(child_recording_bounds); |
| 106 times_empty += remaining.IsEmpty(); |
| 107 } |
| 108 EXPECT_EQ(times_empty, 1); |
| 109 } |
| 110 |
| 111 void VerifyPixelCanvasCornerScaling(std::vector<PaintInfo*> info_list) { |
| 112 // child 1, child 2 and child 6 should completely cover child 0. |
| 113 std::vector<PaintInfo*> child_info_list; |
| 114 child_info_list.push_back(info_list[1]); |
| 115 child_info_list.push_back(info_list[2]); |
| 116 child_info_list.push_back(info_list[6]); |
| 117 VerifyChildBoundsCoversParent(info_list[0], child_info_list); |
| 118 child_info_list.clear(); |
| 119 |
| 120 // Child 3,4 and 5 should completely cover child 2. |
| 121 child_info_list.push_back(info_list[3]); |
| 122 child_info_list.push_back(info_list[4]); |
| 123 child_info_list.push_back(info_list[5]); |
| 124 VerifyChildBoundsCoversParent(info_list[2], child_info_list); |
| 125 child_info_list.clear(); |
| 126 |
| 127 // Child 7 and 8 should completely cover child 6. |
| 128 child_info_list.push_back(info_list[7]); |
| 129 child_info_list.push_back(info_list[8]); |
| 130 VerifyChildBoundsCoversParent(info_list[6], child_info_list); |
| 131 child_info_list.clear(); |
| 132 } |
| 133 |
| 134 void ClearAllPaintInfo(std::vector<PaintInfo*> info_list) { |
| 135 for (auto* paint_info : info_list) |
| 136 delete paint_info; |
| 137 } |
| 138 |
| 139 void VerifyPixelSizesAreSameAsDIPSize(std::vector<PaintInfo*> info_list) { |
| 140 EXPECT_EQ(info_list[0]->paint_recording_size(), r_0.size()); |
| 141 |
| 142 EXPECT_EQ(info_list[1]->paint_recording_size(), r_0_1.size()); |
| 143 |
| 144 EXPECT_EQ(info_list[2]->paint_recording_size(), r_0_2.size()); |
| 145 EXPECT_EQ(info_list[3]->paint_recording_size(), r_2_3.size()); |
| 146 EXPECT_EQ(info_list[4]->paint_recording_size(), r_2_4.size()); |
| 147 EXPECT_EQ(info_list[5]->paint_recording_size(), r_2_5.size()); |
| 148 |
| 149 EXPECT_EQ(info_list[6]->paint_recording_size(), r_0_6.size()); |
| 150 EXPECT_EQ(info_list[7]->paint_recording_size(), r_6_7.size()); |
| 151 EXPECT_EQ(info_list[8]->paint_recording_size(), r_6_8.size()); |
| 152 } |
| 153 |
| 154 void VerifyInvalidationRects(float dsf, bool pixel_canvas_enabled) { |
| 155 std::vector<gfx::Rect> invalidation_rects = { |
| 156 gfx::Rect(0, 0, 123, 41), // Intersects with 0 & 1. |
| 157 gfx::Rect(0, 76, 60, 152), // Intersects 0, 1, 2, 3 & 4. |
| 158 gfx::Rect(41, 152, 41, 152), // Intersects with 0, 2 & 4. |
| 159 gfx::Rect(80, 320, 4, 4), // Intersects with 0, 6, 7 & 8. |
| 160 gfx::Rect(40, 151, 43, 154), // Intersects all |
| 161 gfx::Rect(82, 304, 1, 1), // Intersects with 0, 6 & 8. |
| 162 gfx::Rect(81, 303, 2, 2) // Intersects with 0, 2, 4, 5, 6, 7, 8 |
| 163 }; |
| 164 |
| 165 std::vector<std::vector<int>> repaint_indices = { |
| 166 std::vector<int>{0, 1}, |
| 167 std::vector<int>{0, 1, 2, 3, 4}, |
| 168 std::vector<int>{0, 2, 4}, |
| 169 std::vector<int>{0, 6, 7, 8}, |
| 170 std::vector<int>{0, 1, 2, 3, 4, 5, 6, 7, 8}, |
| 171 std::vector<int>{0, 6, 8}, |
| 172 std::vector<int>{0, 2, 4, 5, 6, 7, 8}}; |
| 173 |
| 174 std::vector<PaintInfo*> info_list; |
| 175 |
| 176 EXPECT_EQ(repaint_indices.size(), invalidation_rects.size()); |
| 177 for (size_t i = 0; i < invalidation_rects.size(); i++) { |
| 178 gfx::Rect scaled_invalidation_rect = gfx::ScaleToRoundedRect( |
| 179 invalidation_rects[i], pixel_canvas_enabled ? dsf : 1.f); |
| 180 PaintContext context(nullptr, dsf, scaled_invalidation_rect); |
| 181 info_list = GetPaintInfoSetup(pixel_canvas_enabled, context); |
| 182 for (size_t j = 0; j < repaint_indices[i].size(); j++) { |
| 183 EXPECT_TRUE(info_list[repaint_indices[i][j]]->context().IsRectInvalid( |
| 184 gfx::Rect(info_list[repaint_indices[i][j]]->paint_recording_size()))); |
| 185 } |
| 186 ClearAllPaintInfo(info_list); |
| 187 } |
| 188 } |
| 189 |
| 190 } // namespace |
| 191 |
| 192 TEST_F(PaintInfoTest, CornerScalingPixelCanvasEnabled) { |
| 193 std::vector<PaintInfo*> info_list; |
| 194 for (float dsf : kDsfList) { |
| 195 PaintContext context(nullptr, dsf, gfx::Rect()); |
| 196 info_list = GetPaintInfoSetup(true, context); |
| 197 VerifyPixelCanvasCornerScaling(info_list); |
| 198 ClearAllPaintInfo(info_list); |
| 199 } |
| 200 |
| 201 // More accurate testing for 1.25 dsf |
| 202 PaintContext context(nullptr, DSF125, gfx::Rect()); |
| 203 info_list = GetPaintInfoSetup(true, context); |
| 204 VerifyPixelCanvasCornerScaling(info_list); |
| 205 EXPECT_EQ(info_list[0]->paint_recording_size(), gfx::Size(154, 570)); |
| 206 |
| 207 EXPECT_EQ(info_list[1]->paint_recording_size(), gfx::Size(154, 190)); |
| 208 |
| 209 EXPECT_EQ(info_list[2]->paint_recording_bounds(), |
| 210 gfx::Rect(0, 190, 154, 190)); |
| 211 EXPECT_EQ(info_list[3]->paint_recording_size(), gfx::Size(51, 190)); |
| 212 EXPECT_EQ(info_list[4]->paint_recording_bounds(), |
| 213 gfx::Rect(51, 190, 52, 190)); |
| 214 EXPECT_EQ(info_list[5]->paint_recording_bounds(), |
| 215 gfx::Rect(103, 190, 51, 190)); |
| 216 |
| 217 EXPECT_EQ(info_list[6]->paint_recording_bounds(), |
| 218 gfx::Rect(0, 380, 154, 190)); |
| 219 EXPECT_EQ(info_list[7]->paint_recording_size(), gfx::Size(103, 190)); |
| 220 EXPECT_EQ(info_list[8]->paint_recording_bounds(), |
| 221 gfx::Rect(103, 380, 51, 190)); |
| 222 ClearAllPaintInfo(info_list); |
| 223 } |
| 224 |
| 225 TEST_F(PaintInfoTest, ScalingWithPixelCanvasDisabled) { |
| 226 // With dsf as 1, there should be no scaling and size should be same. |
| 227 PaintContext context_1(nullptr, DSF100, gfx::Rect()); |
| 228 std::vector<PaintInfo*> info_list = GetPaintInfoSetup(false, context_1); |
| 229 for (float dsf : kDsfList) { |
| 230 PaintContext context_2(nullptr, dsf, gfx::Rect()); |
| 231 info_list = GetPaintInfoSetup(false, context_2); |
| 232 VerifyPixelCanvasCornerScaling(info_list); |
| 233 VerifyPixelSizesAreSameAsDIPSize(info_list); |
| 234 ClearAllPaintInfo(info_list); |
| 235 } |
| 236 } |
| 237 |
| 238 TEST_F(PaintInfoTest, Invalidation) { |
| 239 for (float dsf : kDsfList) { |
| 240 VerifyInvalidationRects(dsf, false); |
| 241 VerifyInvalidationRects(dsf, true); |
| 242 } |
| 243 } |
| 244 |
| 245 } // namespace ui |
OLD | NEW |