Malay Keshav | 851d5768 | 2018-02-14 21:55:23 | [diff] [blame] | 1 | // Copyright 2018 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/canvas_painter.h" |
| 6 | |
| 7 | #include "base/macros.h" |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | #include "ui/compositor/paint_recorder.h" |
| 10 | #include "ui/gfx/canvas.h" |
| 11 | #include "ui/gfx/geometry/rect.h" |
| 12 | #include "ui/gfx/geometry/size.h" |
| 13 | |
| 14 | namespace ui { |
| 15 | namespace { |
| 16 | void CheckPaintedShape(const SkBitmap& bitmap, |
| 17 | const gfx::Rect& shape_bounds, |
| 18 | const SkColor shape_color, |
| 19 | float device_scale_factor) { |
| 20 | // Whether pixel canvas is enabled or not, the pixel location of the shape |
| 21 | // should remain the same. |
| 22 | const gfx::Point expected_top_left_location = |
| 23 | gfx::ScaleToRoundedPoint(shape_bounds.origin(), device_scale_factor); |
| 24 | const gfx::Point expected_bottom_right_location = gfx::ScaleToRoundedPoint( |
| 25 | shape_bounds.bottom_right(), device_scale_factor); |
| 26 | |
| 27 | EXPECT_EQ(bitmap.getColor(expected_top_left_location.x(), |
| 28 | expected_top_left_location.y()), |
| 29 | shape_color); |
| 30 | EXPECT_EQ(bitmap.getColor(expected_bottom_right_location.x(), |
| 31 | expected_bottom_right_location.y()), |
| 32 | shape_color); |
| 33 | } |
| 34 | } // namespace |
| 35 | |
| 36 | class CanvasPainterTest : public ::testing::TestWithParam<float> { |
| 37 | public: |
| 38 | CanvasPainterTest() : device_scale_factor_(GetParam()) {} |
| 39 | |
| 40 | float device_scale_factor() const { return device_scale_factor_; } |
| 41 | |
| 42 | const gfx::Size& pixel_output_size(const CanvasPainter& painter) const { |
| 43 | return painter.pixel_output_size_; |
| 44 | } |
| 45 | |
| 46 | float raster_scale(const CanvasPainter& painter) const { |
| 47 | return painter.raster_scale_; |
| 48 | } |
| 49 | |
| 50 | // Paints a rect with bounds |shape_bounds| and color |shape_color| on |
| 51 | // |bitmap| with the help of CanvasPainter. The output size of the bitmap in |
| 52 | // DIP is |size|. |
| 53 | void Paint(SkBitmap* bitmap, |
| 54 | const gfx::Size& size, |
| 55 | float device_scale_factor, |
| 56 | bool is_pixel_canvas, |
| 57 | const gfx::Rect& shape_bounds, |
| 58 | SkColor shape_color) { |
| 59 | CanvasPainter painter(bitmap, size, device_scale_factor, |
| 60 | SK_ColorTRANSPARENT, is_pixel_canvas); |
| 61 | |
| 62 | // The paint recording size is scaled to match the raster size if pixel |
| 63 | // canvas is enabled. |
| 64 | const gfx::Size paint_recording_size = gfx::ScaleToCeiledSize( |
| 65 | size, is_pixel_canvas ? device_scale_factor : 1.f); |
| 66 | |
| 67 | PaintRecorder recorder(painter.context(), paint_recording_size, |
| 68 | device_scale_factor, device_scale_factor, nullptr); |
| 69 | recorder.canvas()->DrawRect(gfx::RectF(shape_bounds), shape_color); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | float device_scale_factor_; |
| 74 | |
| 75 | DISALLOW_COPY_AND_ASSIGN(CanvasPainterTest); |
| 76 | }; |
| 77 | |
| 78 | TEST_P(CanvasPainterTest, Initialization) { |
| 79 | SkBitmap output; |
| 80 | const gfx::Size output_size(100, 100); |
| 81 | CanvasPainter painter(&output, output_size, device_scale_factor(), |
| 82 | SK_ColorTRANSPARENT, false /* is_pixel_canvas */); |
| 83 | EXPECT_EQ(pixel_output_size(painter), |
| 84 | gfx::ScaleToCeiledSize(output_size, device_scale_factor())); |
| 85 | EXPECT_EQ(raster_scale(painter), device_scale_factor()); |
| 86 | } |
| 87 | |
| 88 | TEST_P(CanvasPainterTest, InitializationPixelCanvasEnabled) { |
| 89 | SkBitmap output; |
| 90 | const gfx::Size output_size(100, 100); |
| 91 | CanvasPainter painter(&output, output_size, device_scale_factor(), |
| 92 | SK_ColorTRANSPARENT, true /* is_pixel_canvas */); |
| 93 | EXPECT_EQ(pixel_output_size(painter), |
| 94 | gfx::ScaleToCeiledSize(output_size, device_scale_factor())); |
| 95 | EXPECT_EQ(raster_scale(painter), 1.f); |
| 96 | } |
| 97 | |
| 98 | TEST_P(CanvasPainterTest, Paint) { |
| 99 | SkBitmap bitmap; |
| 100 | const SkColor shape_color = SK_ColorRED; |
| 101 | const gfx::Rect shape_bounds(100, 100, 100, 100); |
| 102 | |
| 103 | Paint(&bitmap, gfx::Size(1000, 1000), device_scale_factor(), |
| 104 | false /* is_pixel_canvas */, shape_bounds, shape_color); |
| 105 | CheckPaintedShape(bitmap, shape_bounds, shape_color, device_scale_factor()); |
| 106 | } |
| 107 | |
| 108 | TEST_P(CanvasPainterTest, PaintPixelCanvasEnabled) { |
| 109 | SkBitmap bitmap; |
| 110 | const SkColor shape_color = SK_ColorRED; |
| 111 | const gfx::Rect shape_bounds(100, 100, 100, 100); |
| 112 | |
| 113 | Paint(&bitmap, gfx::Size(1000, 1000), device_scale_factor(), |
| 114 | true /* is_pixel_canvas */, shape_bounds, shape_color); |
| 115 | CheckPaintedShape(bitmap, shape_bounds, shape_color, device_scale_factor()); |
| 116 | } |
| 117 | |
| 118 | INSTANTIATE_TEST_CASE_P(, |
| 119 | CanvasPainterTest, |
| 120 | ::testing::Values(1.f, 1.25f, 1.5f, 1.6f, 2.f, 2.25f)); |
| 121 | } // namespace ui |