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

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

Issue 1634103003: Fix gfx::Canvas::DrawStringRectWithHalo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes Created 4 years, 7 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
« no previous file with comments | « no previous file | ui/gfx/canvas.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/compositor/layer.h" 5 #include "ui/compositor/layer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h" 15 #include "base/files/file_util.h"
16 #include "base/json/json_reader.h" 16 #include "base/json/json_reader.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/ptr_util.h" 18 #include "base/memory/ptr_util.h"
19 #include "base/message_loop/message_loop.h" 19 #include "base/message_loop/message_loop.h"
20 #include "base/path_service.h" 20 #include "base/path_service.h"
21 #include "base/strings/string_util.h" 21 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h" 22 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h"
23 #include "base/trace_event/trace_event.h" 24 #include "base/trace_event/trace_event.h"
24 #include "cc/layers/layer.h" 25 #include "cc/layers/layer.h"
25 #include "cc/output/copy_output_request.h" 26 #include "cc/output/copy_output_request.h"
26 #include "cc/output/copy_output_result.h" 27 #include "cc/output/copy_output_result.h"
27 #include "cc/surfaces/surface_id.h" 28 #include "cc/surfaces/surface_id.h"
28 #include "cc/surfaces/surface_sequence.h" 29 #include "cc/surfaces/surface_sequence.h"
29 #include "cc/test/pixel_test_utils.h" 30 #include "cc/test/pixel_test_utils.h"
30 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
31 #include "ui/compositor/compositor_observer.h" 32 #include "ui/compositor/compositor_observer.h"
32 #include "ui/compositor/dip_util.h" 33 #include "ui/compositor/dip_util.h"
33 #include "ui/compositor/layer_animation_element.h" 34 #include "ui/compositor/layer_animation_element.h"
34 #include "ui/compositor/layer_animation_observer.h" 35 #include "ui/compositor/layer_animation_observer.h"
35 #include "ui/compositor/layer_animation_sequence.h" 36 #include "ui/compositor/layer_animation_sequence.h"
36 #include "ui/compositor/layer_animator.h" 37 #include "ui/compositor/layer_animator.h"
37 #include "ui/compositor/paint_context.h" 38 #include "ui/compositor/paint_context.h"
38 #include "ui/compositor/paint_recorder.h" 39 #include "ui/compositor/paint_recorder.h"
39 #include "ui/compositor/scoped_animation_duration_scale_mode.h" 40 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
40 #include "ui/compositor/scoped_layer_animation_settings.h" 41 #include "ui/compositor/scoped_layer_animation_settings.h"
41 #include "ui/compositor/test/context_factories_for_test.h" 42 #include "ui/compositor/test/context_factories_for_test.h"
42 #include "ui/compositor/test/draw_waiter_for_test.h" 43 #include "ui/compositor/test/draw_waiter_for_test.h"
43 #include "ui/compositor/test/test_compositor_host.h" 44 #include "ui/compositor/test/test_compositor_host.h"
44 #include "ui/compositor/test/test_layers.h" 45 #include "ui/compositor/test/test_layers.h"
45 #include "ui/gfx/canvas.h" 46 #include "ui/gfx/canvas.h"
46 #include "ui/gfx/codec/png_codec.h" 47 #include "ui/gfx/codec/png_codec.h"
48 #include "ui/gfx/font_list.h"
47 #include "ui/gfx/gfx_paths.h" 49 #include "ui/gfx/gfx_paths.h"
48 #include "ui/gfx/skia_util.h" 50 #include "ui/gfx/skia_util.h"
49 51
50 using cc::MatchesPNGFile; 52 using cc::MatchesPNGFile;
53 using cc::WritePNGFile;
51 54
52 namespace ui { 55 namespace ui {
53 56
54 namespace { 57 namespace {
55 58
56 // There are three test classes in here that configure the Compositor and 59 // There are three test classes in here that configure the Compositor and
57 // Layer's slightly differently: 60 // Layer's slightly differently:
58 // - LayerWithNullDelegateTest uses NullLayerDelegate as the LayerDelegate. This 61 // - LayerWithNullDelegateTest uses NullLayerDelegate as the LayerDelegate. This
59 // is typically the base class you want to use. 62 // is typically the base class you want to use.
60 // - LayerWithDelegateTest uses LayerDelegate on the delegates. 63 // - LayerWithDelegateTest uses LayerDelegate on the delegates.
(...skipping 22 matching lines...) Expand all
83 void OnDeviceScaleFactorChanged(float device_scale_factor) override {} 86 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
84 87
85 base::Closure PrepareForLayerBoundsChange() override { 88 base::Closure PrepareForLayerBoundsChange() override {
86 return base::Closure(); 89 return base::Closure();
87 } 90 }
88 91
89 private: 92 private:
90 SkColor color_; 93 SkColor color_;
91 }; 94 };
92 95
96 class DrawStringLayerDelegate: public LayerDelegate {
97 public:
98 enum DrawFunction {
99 STRING_WITH_HALO,
100 STRING_FADED,
101 STRING_WITH_SHADOWS
102 };
103
104 DrawStringLayerDelegate(
105 SkColor back_color, SkColor halo_color, DrawFunction func)
106 : background_color_(back_color),
107 halo_color_(halo_color),
108 func_(func) {
109 }
110
111 ~DrawStringLayerDelegate() override {}
112
113 void set_layer_size(const gfx::Size& layer_size) {
114 layer_size_ = layer_size;
115 }
116
117 // Overridden from LayerDelegate:
118 void OnPaintLayer(const ui::PaintContext& context) override {
119 ui::PaintRecorder recorder(context, layer_size_);
120 gfx::Rect bounds(layer_size_);
121 recorder.canvas()->DrawColor(background_color_);
122 const base::string16 text = base::ASCIIToUTF16("Tests!");
123 switch (func_) {
124 case STRING_WITH_HALO:
125 recorder.canvas()->DrawStringRectWithHalo(
126 text, font_list_, SK_ColorRED, halo_color_, bounds, 0);
127 break;
128 case STRING_FADED:
129 recorder.canvas()->DrawFadedString(
130 text, font_list_, SK_ColorRED, bounds, 0);
131 break;
132 case STRING_WITH_SHADOWS: {
133 gfx::ShadowValues shadows;
134 shadows.push_back(
135 gfx::ShadowValue(gfx::Vector2d(2, 2), 2, SK_ColorRED));
136 recorder.canvas()->DrawStringRectWithShadows(
137 text, font_list_, SK_ColorRED, bounds, 0, 0, shadows);
138 break;
139 }
140 default:
141 NOTREACHED();
142 }
143 }
144
145 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {}
146
147 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
148
149 base::Closure PrepareForLayerBoundsChange() override {
150 return base::Closure();
151 }
152
153 private:
154 SkColor background_color_;
155 SkColor halo_color_;
156 DrawFunction func_;
157 gfx::FontList font_list_;
158 gfx::Size layer_size_;
159 };
160
93 class LayerWithRealCompositorTest : public testing::Test { 161 class LayerWithRealCompositorTest : public testing::Test {
94 public: 162 public:
95 LayerWithRealCompositorTest() { 163 LayerWithRealCompositorTest() {
96 if (PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)) { 164 if (PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)) {
97 test_data_directory_ = test_data_directory_.AppendASCII("compositor"); 165 test_data_directory_ = test_data_directory_.AppendASCII("compositor");
98 } else { 166 } else {
99 LOG(ERROR) << "Could not open test data directory."; 167 LOG(ERROR) << "Could not open test data directory.";
100 } 168 }
169 gfx::FontList::SetDefaultFontDescription("Arial, Times New Roman, 15px");
101 } 170 }
102 ~LayerWithRealCompositorTest() override {} 171 ~LayerWithRealCompositorTest() override {}
103 172
104 // Overridden from testing::Test: 173 // Overridden from testing::Test:
105 void SetUp() override { 174 void SetUp() override {
106 bool enable_pixel_output = true; 175 bool enable_pixel_output = true;
107 ui::ContextFactory* context_factory = 176 ui::ContextFactory* context_factory =
108 InitializeContextFactoryForTests(enable_pixel_output); 177 InitializeContextFactoryForTests(enable_pixel_output);
109 178
110 const gfx::Rect host_bounds(10, 10, 500, 500); 179 const gfx::Rect host_bounds(10, 10, 500, 500);
(...skipping 22 matching lines...) Expand all
133 layer->SetBounds(bounds); 202 layer->SetBounds(bounds);
134 return layer; 203 return layer;
135 } 204 }
136 205
137 Layer* CreateNoTextureLayer(const gfx::Rect& bounds) { 206 Layer* CreateNoTextureLayer(const gfx::Rect& bounds) {
138 Layer* layer = CreateLayer(LAYER_NOT_DRAWN); 207 Layer* layer = CreateLayer(LAYER_NOT_DRAWN);
139 layer->SetBounds(bounds); 208 layer->SetBounds(bounds);
140 return layer; 209 return layer;
141 } 210 }
142 211
212 Layer* CreateDrawStringLayer(const gfx::Rect& bounds,
213 DrawStringLayerDelegate* delegate) {
214 Layer* layer = new Layer(LAYER_TEXTURED);
215 layer->SetBounds(bounds);
216 delegate->set_layer_size(layer->size());
217 layer->set_delegate(delegate);
218 return layer;
219 }
220
143 void DrawTree(Layer* root) { 221 void DrawTree(Layer* root) {
144 GetCompositor()->SetRootLayer(root); 222 GetCompositor()->SetRootLayer(root);
145 GetCompositor()->ScheduleDraw(); 223 GetCompositor()->ScheduleDraw();
146 WaitForSwap(); 224 WaitForSwap();
147 } 225 }
148 226
149 void ReadPixels(SkBitmap* bitmap) { 227 void ReadPixels(SkBitmap* bitmap) {
150 ReadPixels(bitmap, gfx::Rect(GetCompositor()->size())); 228 ReadPixels(bitmap, gfx::Rect(GetCompositor()->size()));
151 } 229 }
152 230
(...skipping 1068 matching lines...) Expand 10 before | Expand all | Expand 10 after
1221 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true))); 1299 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true)));
1222 1300
1223 // l11 back to front 1301 // l11 back to front
1224 l0->StackAbove(l11.get(), l12.get()); 1302 l0->StackAbove(l11.get(), l12.get());
1225 DrawTree(l0.get()); 1303 DrawTree(l0.get());
1226 ReadPixels(&bitmap); 1304 ReadPixels(&bitmap);
1227 ASSERT_FALSE(bitmap.empty()); 1305 ASSERT_FALSE(bitmap.empty());
1228 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); 1306 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1229 } 1307 }
1230 1308
1309 TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithHalo) {
1310 gfx::Size size(50, 50);
1311 GetCompositor()->SetScaleAndSize(1.0f, size);
1312 DrawStringLayerDelegate delegate(SK_ColorBLUE, SK_ColorWHITE,
1313 DrawStringLayerDelegate::STRING_WITH_HALO);
1314 std::unique_ptr<Layer> layer(
1315 CreateDrawStringLayer(gfx::Rect(size), &delegate));
1316 DrawTree(layer.get());
1317
1318 SkBitmap bitmap;
1319 ReadPixels(&bitmap);
1320 ASSERT_FALSE(bitmap.empty());
1321
1322 base::FilePath ref_img =
1323 test_data_directory().AppendASCII("string_with_halo.png");
1324 // WritePNGFile(bitmap, ref_img, true);
1325
1326 #if defined(OS_WIN)
1327 float percentage_pixels_large_error = 10.0f; // 250px / (50*50)
1328 #else
1329 float percentage_pixels_large_error = 50.0f; // Don't check.
danakj 2016/06/01 18:45:47 What do you mean don't check? Surely the platform
danakj 2016/06/22 23:14:03 OK then just #ifdef windows around the whole test,
1330 #endif // defined(OS_WIN)
1331 float percentage_pixels_small_error = 0.0f;
1332 float average_error_allowed_in_bad_pixels = 256.f;
1333 int large_error_allowed = 256;
danakj 2016/06/22 23:14:03 Can you narrow this? Surely the pixels are off by
1334 int small_error_allowed = 0;
1335
1336 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img,
1337 cc::FuzzyPixelComparator(
1338 true,
1339 percentage_pixels_large_error,
1340 percentage_pixels_small_error,
1341 average_error_allowed_in_bad_pixels,
1342 large_error_allowed,
1343 small_error_allowed)));
1344 }
1345
1346 TEST_F(LayerWithRealCompositorTest, CanvasDrawFadedString) {
1347 gfx::Size size(50, 50);
1348 GetCompositor()->SetScaleAndSize(1.0f, size);
1349 DrawStringLayerDelegate delegate(SK_ColorBLUE, SK_ColorWHITE,
1350 DrawStringLayerDelegate::STRING_FADED);
1351 std::unique_ptr<Layer> layer(
1352 CreateDrawStringLayer(gfx::Rect(size), &delegate));
1353 DrawTree(layer.get());
1354
1355 SkBitmap bitmap;
1356 ReadPixels(&bitmap);
1357 ASSERT_FALSE(bitmap.empty());
1358
1359 base::FilePath ref_img =
1360 test_data_directory().AppendASCII("string_faded.png");
1361 // WritePNGFile(bitmap, ref_img, true);
1362
1363 #if defined(OS_WIN)
1364 float percentage_pixels_large_error = 15.0f; // 375px / (50*50)
1365 #else
1366 float percentage_pixels_large_error = 50.0f; // Don't check.
1367 #endif // defined(OS_WIN)
1368 float percentage_pixels_small_error = 0.0f;
1369 float average_error_allowed_in_bad_pixels = 256.f;
1370 int large_error_allowed = 256;
1371 int small_error_allowed = 0;
1372
1373 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img,
1374 cc::FuzzyPixelComparator(
1375 true,
1376 percentage_pixels_large_error,
1377 percentage_pixels_small_error,
1378 average_error_allowed_in_bad_pixels,
1379 large_error_allowed,
1380 small_error_allowed)));
1381 }
1382
1383 TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithShadows) {
1384 gfx::Size size(50, 50);
1385 GetCompositor()->SetScaleAndSize(1.0f, size);
1386 DrawStringLayerDelegate delegate(
1387 SK_ColorBLUE, SK_ColorWHITE,
1388 DrawStringLayerDelegate::STRING_WITH_SHADOWS);
1389 std::unique_ptr<Layer> layer(
1390 CreateDrawStringLayer(gfx::Rect(size), &delegate));
1391 DrawTree(layer.get());
1392
1393 SkBitmap bitmap;
1394 ReadPixels(&bitmap);
1395 ASSERT_FALSE(bitmap.empty());
1396
1397 base::FilePath ref_img =
1398 test_data_directory().AppendASCII("string_with_shadows.png");
1399 // WritePNGFile(bitmap, ref_img, true);
1400
1401 #if defined(OS_WIN)
1402 float percentage_pixels_large_error = 20.0f; // 500px / (50*50)
1403 #else
1404 float percentage_pixels_large_error = 50.0f; // Don't check.
1405 #endif // defined(OS_WIN)
1406 float percentage_pixels_small_error = 0.0f;
1407 float average_error_allowed_in_bad_pixels = 256.f;
1408 int large_error_allowed = 256;
1409 int small_error_allowed = 0;
1410
1411 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img,
1412 cc::FuzzyPixelComparator(
1413 true,
1414 percentage_pixels_large_error,
1415 percentage_pixels_small_error,
1416 average_error_allowed_in_bad_pixels,
1417 large_error_allowed,
1418 small_error_allowed)));
1419 }
1420
1231 // Opacity is rendered correctly. 1421 // Opacity is rendered correctly.
1232 // Checks that modifying the hierarchy correctly affects final composite. 1422 // Checks that modifying the hierarchy correctly affects final composite.
1233 TEST_F(LayerWithRealCompositorTest, Opacity) { 1423 TEST_F(LayerWithRealCompositorTest, Opacity) {
1234 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); 1424 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50));
1235 1425
1236 // l0 1426 // l0
1237 // +-l11 1427 // +-l11
1238 std::unique_ptr<Layer> l0( 1428 std::unique_ptr<Layer> l0(
1239 CreateColorLayer(SK_ColorRED, gfx::Rect(0, 0, 50, 50))); 1429 CreateColorLayer(SK_ColorRED, gfx::Rect(0, 0, 50, 50)));
1240 std::unique_ptr<Layer> l11( 1430 std::unique_ptr<Layer> l11(
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after
1877 root->SetOpacity(0.5f); 2067 root->SetOpacity(0.5f);
1878 WaitForSwap(); 2068 WaitForSwap();
1879 EXPECT_EQ(1u, animation_observer.animation_step_count()); 2069 EXPECT_EQ(1u, animation_observer.animation_step_count());
1880 2070
1881 EXPECT_FALSE(animation_observer.shutdown()); 2071 EXPECT_FALSE(animation_observer.shutdown());
1882 ResetCompositor(); 2072 ResetCompositor();
1883 EXPECT_TRUE(animation_observer.shutdown()); 2073 EXPECT_TRUE(animation_observer.shutdown());
1884 } 2074 }
1885 2075
1886 } // namespace ui 2076 } // namespace ui
OLDNEW
« no previous file with comments | « no previous file | ui/gfx/canvas.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698