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

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: bots' baselines Created 4 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
« 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 {
sky 2016/07/18 15:02:02 Add description, also 'DrawStringLayerDelegate:' -
Lof 2016/07/19 14:34:37 Done.
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_;
sky 2016/07/18 15:02:02 nit: use const where applicable.
Lof 2016/07/19 14:34:37 Done.
155 SkColor halo_color_;
156 DrawFunction func_;
157 gfx::FontList font_list_;
158 gfx::Size layer_size_;
159 };
sky 2016/07/18 15:02:02 nit: DISALLOW...
Lof 2016/07/19 14:34:37 Done.
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,
sky 2016/07/18 15:02:02 nit: make return type std::unique_ptr
Lof 2016/07/19 14:34:38 Done.
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 1095 matching lines...) Expand 10 before | Expand all | Expand 10 after
1248 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true))); 1326 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true)));
1249 1327
1250 // l11 back to front 1328 // l11 back to front
1251 l0->StackAbove(l11.get(), l12.get()); 1329 l0->StackAbove(l11.get(), l12.get());
1252 DrawTree(l0.get()); 1330 DrawTree(l0.get());
1253 ReadPixels(&bitmap); 1331 ReadPixels(&bitmap);
1254 ASSERT_FALSE(bitmap.empty()); 1332 ASSERT_FALSE(bitmap.empty());
1255 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); 1333 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1256 } 1334 }
1257 1335
1336 #if defined(OS_WIN)
sky 2016/07/18 15:02:02 Document why windows only.
Lof 2016/07/19 14:34:37 Done.
1337 TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithHalo) {
1338 gfx::Size size(50, 50);
1339 GetCompositor()->SetScaleAndSize(1.0f, size);
1340 DrawStringLayerDelegate delegate(SK_ColorBLUE, SK_ColorWHITE,
1341 DrawStringLayerDelegate::STRING_WITH_HALO);
1342 std::unique_ptr<Layer> layer(
1343 CreateDrawStringLayer(gfx::Rect(size), &delegate));
1344 DrawTree(layer.get());
1345
1346 SkBitmap bitmap;
1347 ReadPixels(&bitmap);
1348 ASSERT_FALSE(bitmap.empty());
1349
1350 base::FilePath ref_img =
1351 test_data_directory().AppendASCII("string_with_halo.png");
1352 // WritePNGFile(bitmap, ref_img, true);
1353
1354 float percentage_pixels_large_error = 1.0f;
1355 float percentage_pixels_small_error = 0.0f;
1356 float average_error_allowed_in_bad_pixels = 1.f;
1357 int large_error_allowed = 1;
1358 int small_error_allowed = 0;
1359
1360 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img,
1361 cc::FuzzyPixelComparator(
1362 true,
1363 percentage_pixels_large_error,
1364 percentage_pixels_small_error,
1365 average_error_allowed_in_bad_pixels,
1366 large_error_allowed,
1367 small_error_allowed)));
1368 }
1369
1370 TEST_F(LayerWithRealCompositorTest, CanvasDrawFadedString) {
1371 gfx::Size size(50, 50);
1372 GetCompositor()->SetScaleAndSize(1.0f, size);
1373 DrawStringLayerDelegate delegate(SK_ColorBLUE, SK_ColorWHITE,
1374 DrawStringLayerDelegate::STRING_FADED);
1375 std::unique_ptr<Layer> layer(
1376 CreateDrawStringLayer(gfx::Rect(size), &delegate));
1377 DrawTree(layer.get());
1378
1379 SkBitmap bitmap;
1380 ReadPixels(&bitmap);
1381 ASSERT_FALSE(bitmap.empty());
1382
1383 base::FilePath ref_img =
1384 test_data_directory().AppendASCII("string_faded.png");
1385 // WritePNGFile(bitmap, ref_img, true);
1386
1387 float percentage_pixels_large_error = 8.0f; // 200px / (50*50)
1388 float percentage_pixels_small_error = 0.0f;
1389 float average_error_allowed_in_bad_pixels = 80.f;
1390 int large_error_allowed = 255;
1391 int small_error_allowed = 0;
1392
1393 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img,
1394 cc::FuzzyPixelComparator(
1395 true,
1396 percentage_pixels_large_error,
1397 percentage_pixels_small_error,
1398 average_error_allowed_in_bad_pixels,
1399 large_error_allowed,
1400 small_error_allowed)));
1401 }
1402
1403 TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithShadows) {
1404 gfx::Size size(50, 50);
1405 GetCompositor()->SetScaleAndSize(1.0f, size);
1406 DrawStringLayerDelegate delegate(
1407 SK_ColorBLUE, SK_ColorWHITE,
1408 DrawStringLayerDelegate::STRING_WITH_SHADOWS);
1409 std::unique_ptr<Layer> layer(
1410 CreateDrawStringLayer(gfx::Rect(size), &delegate));
1411 DrawTree(layer.get());
1412
1413 SkBitmap bitmap;
1414 ReadPixels(&bitmap);
1415 ASSERT_FALSE(bitmap.empty());
1416
1417 base::FilePath ref_img =
1418 test_data_directory().AppendASCII("string_with_shadows.png");
1419 // WritePNGFile(bitmap, ref_img, true);
1420
1421 float percentage_pixels_large_error = 7.4f; // 185px / (50*50)
1422 float percentage_pixels_small_error = 0.0f;
1423 float average_error_allowed_in_bad_pixels = 60.f;
1424 int large_error_allowed = 246;
1425 int small_error_allowed = 0;
1426
1427 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img,
1428 cc::FuzzyPixelComparator(
1429 true,
1430 percentage_pixels_large_error,
1431 percentage_pixels_small_error,
1432 average_error_allowed_in_bad_pixels,
1433 large_error_allowed,
1434 small_error_allowed)));
1435 }
1436 #endif // defined(OS_WIN)
1437
1258 // Opacity is rendered correctly. 1438 // Opacity is rendered correctly.
1259 // Checks that modifying the hierarchy correctly affects final composite. 1439 // Checks that modifying the hierarchy correctly affects final composite.
1260 TEST_F(LayerWithRealCompositorTest, Opacity) { 1440 TEST_F(LayerWithRealCompositorTest, Opacity) {
1261 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); 1441 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50));
1262 1442
1263 // l0 1443 // l0
1264 // +-l11 1444 // +-l11
1265 std::unique_ptr<Layer> l0( 1445 std::unique_ptr<Layer> l0(
1266 CreateColorLayer(SK_ColorRED, gfx::Rect(0, 0, 50, 50))); 1446 CreateColorLayer(SK_ColorRED, gfx::Rect(0, 0, 50, 50)));
1267 std::unique_ptr<Layer> l11( 1447 std::unique_ptr<Layer> l11(
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after
1904 root->SetOpacity(0.5f); 2084 root->SetOpacity(0.5f);
1905 WaitForSwap(); 2085 WaitForSwap();
1906 EXPECT_EQ(1u, animation_observer.animation_step_count()); 2086 EXPECT_EQ(1u, animation_observer.animation_step_count());
1907 2087
1908 EXPECT_FALSE(animation_observer.shutdown()); 2088 EXPECT_FALSE(animation_observer.shutdown());
1909 ResetCompositor(); 2089 ResetCompositor();
1910 EXPECT_TRUE(animation_observer.shutdown()); 2090 EXPECT_TRUE(animation_observer.shutdown());
1911 } 2091 }
1912 2092
1913 } // namespace ui 2093 } // 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