| Flutter Impeller |

golden_tests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 "display_list/dl_color.h"
6 #include "display_list/dl_tile_mode.h"
7 #include "gtest/gtest.h"
8
9 #include <sstream>
10
11 #include "display_list/dl_builder.h"
12 #include "display_list/dl_paint.h"
13 #include "display_list/effects/dl_color_source.h"
14 #include "flutter/fml/platform/darwin/scoped_nsautorelease_pool.h"
15 #include "impeller/display_list/aiks_context.h"
16 #include "impeller/display_list/dl_dispatcher.h" // nogncheck
17 #include "impeller/golden_tests/golden_digest.h"
18 #include "impeller/golden_tests/metal_screenshot.h"
19 #include "impeller/golden_tests/metal_screenshotter.h"
20 #include "impeller/golden_tests/working_directory.h"
21
22 namespace impeller {
23 namespace testing {
24
25 namespace {
26 std::string GetTestName() {
27 std::string suite_name =
28 ::testing::UnitTest::GetInstance()->current_test_suite()->name();
29 std::string test_name =
30 ::testing::UnitTest::GetInstance()->current_test_info()->name();
31 std::stringstream ss;
32 ss << "impeller_" << suite_name << "_" << test_name;
33 return ss.str();
34 }
35
36 std::string GetGoldenFilename() {
37 return GetTestName() + ".png";
38 }
39
40 bool SaveScreenshot(std::unique_ptr<Screenshot> screenshot) {
41 if (!screenshot || !screenshot->GetBytes()) {
42 return false;
43 }
44 std::string test_name = GetTestName();
45 std::string filename = GetGoldenFilename();
46 GoldenDigest::Instance()->AddImage(
47 test_name, filename, screenshot->GetWidth(), screenshot->GetHeight());
48 return screenshot->WriteToPNG(
49 WorkingDirectory::Instance()->GetFilenamePath(filename));
50 }
51
52 PlaygroundSwitches GetPlaygroundSwitches() {
53 PlaygroundSwitches switches;
54 switches.enable_wide_gamut = false;
55 return switches;
56 }
57 } // namespace
58
59 class GoldenTests : public ::testing::Test {
60 public:
61 GoldenTests()
62 : screenshotter_(new MetalScreenshotter(GetPlaygroundSwitches())) {}
63
64 MetalScreenshotter& Screenshotter() { return *screenshotter_; }
65
66 void SetUp() override {
67 testing::GoldenDigest::Instance()->AddDimension(
68 "gpu_string",
69 Screenshotter().GetPlayground().GetContext()->DescribeGpuModel());
70 }
71
72 private:
73 // This must be placed before any other members that may use the
74 // autorelease pool.
75 fml::ScopedNSAutoreleasePool autorelease_pool_;
76
77 std::unique_ptr<MetalScreenshotter> screenshotter_;
78 };
79
80 TEST_F(GoldenTests, ConicalGradient) {
81 flutter::DisplayListBuilder builder;
82 flutter::DlPaint paint;
83 paint.setDrawStyle(flutter::DlDrawStyle::kFill);
84
85 flutter::DlColor colors[2] = {flutter::DlColor::RGBA(1, 0, 0, 1),
86 flutter::DlColor::RGBA(0, 0, 1, 1)};
87 Scalar stops[2] = {0, 1};
88
89 paint.setColorSource(flutter::DlColorSource::MakeConical(
90 /*start_center=*/{125, 125}, //
91 /*start_radius=*/125, {180, 180}, //
92 /*end_radius=*/0, //
93 /*stop_count=*/2, //
94 /*colors=*/colors, //
95 /*stops=*/stops, //
96 /*tile_mode=*/flutter::DlTileMode::kClamp //
97 ));
98
99 builder.DrawRect(DlRect::MakeXYWH(10, 10, 250, 250), paint);
100
101 auto aiks_context =
102 AiksContext(Screenshotter().GetPlayground().GetContext(), nullptr);
103 auto screenshot = Screenshotter().MakeScreenshot(
104 aiks_context,
105 DisplayListToTexture(builder.Build(), {240, 240}, aiks_context));
106 ASSERT_TRUE(SaveScreenshot(std::move(screenshot)));
107 }
108 } // namespace testing
109 } // namespace impeller
aiks_context.h
impeller::AiksContext
Definition: aiks_context.h:19
impeller::testing::GoldenDigest::Instance
static GoldenDigest * Instance()
Definition: golden_digest.cc:18
impeller::testing::GoldenDigest::AddDimension
void AddDimension(const std::string &name, const std::string &value)
Definition: golden_digest.cc:27
impeller::testing::GoldenDigest::AddImage
void AddImage(const std::string &test_name, const std::string &filename, int32_t width, int32_t height)
Definition: golden_digest.cc:34
impeller::testing::GoldenTests
Definition: golden_tests.cc:59
impeller::testing::GoldenTests::GoldenTests
GoldenTests()
Definition: golden_tests.cc:61
impeller::testing::GoldenTests::SetUp
void SetUp() override
Definition: golden_tests.cc:66
impeller::testing::GoldenTests::Screenshotter
MetalScreenshotter & Screenshotter()
Definition: golden_tests.cc:64
impeller::testing::MetalScreenshotter
Definition: metal_screenshotter.h:18
impeller::testing::Screenshotter
Definition: screenshotter.h:17
impeller::testing::Screenshotter::MakeScreenshot
virtual std::unique_ptr< Screenshot > MakeScreenshot(AiksContext &aiks_context, const std::shared_ptr< Texture > texture)=0
impeller::testing::WorkingDirectory::Instance
static WorkingDirectory * Instance()
Definition: working_directory.cc:16
dl_dispatcher.h
golden_digest.h
metal_screenshot.h
metal_screenshotter.h
impeller::testing::TEST_F
TEST_F(GoldenTests, ConicalGradient)
Definition: golden_tests.cc:80
impeller
Definition: allocation.cc:12
impeller::DisplayListToTexture
std::shared_ptr< Texture > DisplayListToTexture(const sk_sp< flutter::DisplayList > &display_list, ISize size, AiksContext &context, bool reset_host_buffer, bool generate_mips)
Render the provided display list to a texture with the given size.
Definition: dl_dispatcher.cc:1224
impeller::Scalar
float Scalar
Definition: scalar.h:19
working_directory.h