| OLD | NEW |
| 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> |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 private: | 91 private: |
| 92 SkColor color_; | 92 SkColor color_; |
| 93 }; | 93 }; |
| 94 | 94 |
| 95 // Layer delegate for painting text with effects on canvas. | 95 // Layer delegate for painting text with effects on canvas. |
| 96 class DrawStringLayerDelegate : public LayerDelegate { | 96 class DrawStringLayerDelegate : public LayerDelegate { |
| 97 public: | 97 public: |
| 98 enum DrawFunction { | 98 enum DrawFunction { |
| 99 STRING_WITH_HALO, | 99 STRING_WITH_HALO, |
| 100 STRING_FADED, | 100 STRING_FADED, |
| 101 STRING_WITH_SHADOWS | |
| 102 }; | 101 }; |
| 103 | 102 |
| 104 DrawStringLayerDelegate( | 103 DrawStringLayerDelegate( |
| 105 SkColor back_color, SkColor halo_color, | 104 SkColor back_color, SkColor halo_color, |
| 106 DrawFunction func, const gfx::Size& layer_size) | 105 DrawFunction func, const gfx::Size& layer_size) |
| 107 : background_color_(back_color), | 106 : background_color_(back_color), |
| 108 halo_color_(halo_color), | 107 halo_color_(halo_color), |
| 109 func_(func), | 108 func_(func), |
| 110 layer_size_(layer_size) { | 109 layer_size_(layer_size) { |
| 111 } | 110 } |
| 112 | 111 |
| 113 ~DrawStringLayerDelegate() override {} | 112 ~DrawStringLayerDelegate() override {} |
| 114 | 113 |
| 115 // Overridden from LayerDelegate: | 114 // Overridden from LayerDelegate: |
| 116 void OnPaintLayer(const ui::PaintContext& context) override { | 115 void OnPaintLayer(const ui::PaintContext& context) override { |
| 117 ui::PaintRecorder recorder(context, layer_size_); | 116 ui::PaintRecorder recorder(context, layer_size_); |
| 118 gfx::Rect bounds(layer_size_); | 117 gfx::Rect bounds(layer_size_); |
| 119 recorder.canvas()->DrawColor(background_color_); | 118 recorder.canvas()->DrawColor(background_color_); |
| 120 const base::string16 text = base::ASCIIToUTF16("Tests!"); | 119 const base::string16 text = base::ASCIIToUTF16("Tests!"); |
| 121 switch (func_) { | 120 switch (func_) { |
| 122 case STRING_WITH_HALO: | 121 case STRING_WITH_HALO: |
| 123 recorder.canvas()->DrawStringRectWithHalo( | 122 recorder.canvas()->DrawStringRectWithHalo( |
| 124 text, font_list_, SK_ColorRED, halo_color_, bounds, 0); | 123 text, font_list_, SK_ColorRED, halo_color_, bounds, 0); |
| 125 break; | 124 break; |
| 126 case STRING_FADED: | 125 case STRING_FADED: |
| 127 recorder.canvas()->DrawFadedString( | 126 recorder.canvas()->DrawFadedString( |
| 128 text, font_list_, SK_ColorRED, bounds, 0); | 127 text, font_list_, SK_ColorRED, bounds, 0); |
| 129 break; | 128 break; |
| 130 case STRING_WITH_SHADOWS: { | |
| 131 gfx::ShadowValues shadows; | |
| 132 shadows.push_back( | |
| 133 gfx::ShadowValue(gfx::Vector2d(2, 2), 2, SK_ColorRED)); | |
| 134 recorder.canvas()->DrawStringRectWithShadows( | |
| 135 text, font_list_, SK_ColorRED, bounds, 0, 0, shadows); | |
| 136 break; | |
| 137 } | |
| 138 default: | 129 default: |
| 139 NOTREACHED(); | 130 NOTREACHED(); |
| 140 } | 131 } |
| 141 } | 132 } |
| 142 | 133 |
| 143 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {} | 134 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {} |
| 144 | 135 |
| 145 void OnDeviceScaleFactorChanged(float device_scale_factor) override {} | 136 void OnDeviceScaleFactorChanged(float device_scale_factor) override {} |
| 146 | 137 |
| 147 private: | 138 private: |
| (...skipping 1371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1519 | 1510 |
| 1520 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img, | 1511 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img, |
| 1521 cc::FuzzyPixelComparator( | 1512 cc::FuzzyPixelComparator( |
| 1522 true, | 1513 true, |
| 1523 percentage_pixels_large_error, | 1514 percentage_pixels_large_error, |
| 1524 percentage_pixels_small_error, | 1515 percentage_pixels_small_error, |
| 1525 average_error_allowed_in_bad_pixels, | 1516 average_error_allowed_in_bad_pixels, |
| 1526 large_error_allowed, | 1517 large_error_allowed, |
| 1527 small_error_allowed))); | 1518 small_error_allowed))); |
| 1528 } | 1519 } |
| 1529 | |
| 1530 TEST_F(LayerWithRealCompositorTest, CanvasDrawStringRectWithShadows) { | |
| 1531 gfx::Size size(50, 50); | |
| 1532 GetCompositor()->SetScaleAndSize(1.0f, size); | |
| 1533 DrawStringLayerDelegate delegate( | |
| 1534 SK_ColorBLUE, SK_ColorWHITE, | |
| 1535 DrawStringLayerDelegate::STRING_WITH_SHADOWS, | |
| 1536 size); | |
| 1537 std::unique_ptr<Layer> layer( | |
| 1538 CreateDrawStringLayer(gfx::Rect(size), &delegate)); | |
| 1539 DrawTree(layer.get()); | |
| 1540 | |
| 1541 SkBitmap bitmap; | |
| 1542 ReadPixels(&bitmap); | |
| 1543 ASSERT_FALSE(bitmap.empty()); | |
| 1544 | |
| 1545 base::FilePath ref_img = | |
| 1546 test_data_directory().AppendASCII("string_with_shadows.png"); | |
| 1547 // WritePNGFile(bitmap, ref_img, true); | |
| 1548 | |
| 1549 float percentage_pixels_large_error = 7.4f; // 185px / (50*50) | |
| 1550 float percentage_pixels_small_error = 0.0f; | |
| 1551 float average_error_allowed_in_bad_pixels = 60.f; | |
| 1552 int large_error_allowed = 246; | |
| 1553 int small_error_allowed = 0; | |
| 1554 | |
| 1555 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img, | |
| 1556 cc::FuzzyPixelComparator( | |
| 1557 true, | |
| 1558 percentage_pixels_large_error, | |
| 1559 percentage_pixels_small_error, | |
| 1560 average_error_allowed_in_bad_pixels, | |
| 1561 large_error_allowed, | |
| 1562 small_error_allowed))); | |
| 1563 } | |
| 1564 #endif // defined(OS_WIN) | 1520 #endif // defined(OS_WIN) |
| 1565 | 1521 |
| 1566 // Opacity is rendered correctly. | 1522 // Opacity is rendered correctly. |
| 1567 // Checks that modifying the hierarchy correctly affects final composite. | 1523 // Checks that modifying the hierarchy correctly affects final composite. |
| 1568 TEST_F(LayerWithRealCompositorTest, Opacity) { | 1524 TEST_F(LayerWithRealCompositorTest, Opacity) { |
| 1569 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); | 1525 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); |
| 1570 | 1526 |
| 1571 // l0 | 1527 // l0 |
| 1572 // +-l11 | 1528 // +-l11 |
| 1573 std::unique_ptr<Layer> l0( | 1529 std::unique_ptr<Layer> l0( |
| (...skipping 703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2277 layer.set_name("foo"); | 2233 layer.set_name("foo"); |
| 2278 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> debug_info = | 2234 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> debug_info = |
| 2279 layer.TakeDebugInfo(nullptr); | 2235 layer.TakeDebugInfo(nullptr); |
| 2280 std::string trace_format("bar,"); | 2236 std::string trace_format("bar,"); |
| 2281 debug_info->AppendAsTraceFormat(&trace_format); | 2237 debug_info->AppendAsTraceFormat(&trace_format); |
| 2282 std::string expected("bar,{\"layer_name\":\"foo\"}"); | 2238 std::string expected("bar,{\"layer_name\":\"foo\"}"); |
| 2283 EXPECT_EQ(expected, trace_format); | 2239 EXPECT_EQ(expected, trace_format); |
| 2284 } | 2240 } |
| 2285 | 2241 |
| 2286 } // namespace ui | 2242 } // namespace ui |
| OLD | NEW |