| 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 #include "third_party/skia/include/core/SkPaint.h"
22
23 namespace impeller {
24 namespace testing {
25
26 namespace {
27 std::string GetTestName() {
28 std::string suite_name =
29 ::testing::UnitTest::GetInstance()->current_test_suite()->name();
30 std::string test_name =
31 ::testing::UnitTest::GetInstance()->current_test_info()->name();
32 std::stringstream ss;
33 ss << "impeller_" << suite_name << "_" << test_name;
34 return ss.str();
35 }
36
37 std::string GetGoldenFilename() {
38 return GetTestName() + ".png";
39 }
40
41 bool SaveScreenshot(std::unique_ptr<Screenshot> screenshot) {
42 if (!screenshot || !screenshot->GetBytes()) {
43 return false;
44 }
45 std::string test_name = GetTestName();
46 std::string filename = GetGoldenFilename();
47 GoldenDigest::Instance()->AddImage(
48 test_name, filename, screenshot->GetWidth(), screenshot->GetHeight());
49 return screenshot->WriteToPNG(
50 WorkingDirectory::Instance()->GetFilenamePath(filename));
51 }
52
53 } // namespace
54
55 class GoldenTests : public ::testing::Test {
56 public:
57 GoldenTests()
58 : screenshotter_(new MetalScreenshotter(/*enable_wide_gamut=*/false)) {}
59
60 MetalScreenshotter& Screenshotter() { return *screenshotter_; }
61
62 void SetUp() override {
63 testing::GoldenDigest::Instance()->AddDimension(
64 "gpu_string",
65 Screenshotter().GetPlayground().GetContext()->DescribeGpuModel());
66 }
67
68 private:
69 // This must be placed before any other members that may use the
70 // autorelease pool.
71 fml::ScopedNSAutoreleasePool autorelease_pool_;
72
73 std::unique_ptr<MetalScreenshotter> screenshotter_;
74 };
75
76 TEST_F(GoldenTests, ConicalGradient) {
77 flutter::DisplayListBuilder builder;
78 flutter::DlPaint paint;
79 paint.setDrawStyle(flutter::DlDrawStyle::kFill);
80
81 flutter::DlColor colors[2] = {flutter::DlColor::RGBA(1, 0, 0, 1),
82 flutter::DlColor::RGBA(0, 0, 1, 1)};
83 Scalar stops[2] = {0, 1};
84
85 paint.setColorSource(flutter::DlConicalGradientColorSource::MakeConical(
86 /*start_center=*/{125, 125}, //
87 /*start_radius=*/125, {180, 180}, //
88 /*end_radius=*/0, //
89 /*stop_count=*/2, //
90 /*colors=*/colors, //
91 /*stops=*/stops, //
92 /*tile_mode=*/flutter::DlTileMode::kClamp //
93 ));
94
95 builder.DrawRect(SkRect::MakeXYWH(10, 10, 250, 250), paint);
96
97 auto aiks_context =
98 AiksContext(Screenshotter().GetPlayground().GetContext(), nullptr);
99 auto screenshot = Screenshotter().MakeScreenshot(
100 aiks_context,
101 DisplayListToTexture(builder.Build(), {240, 240}, aiks_context));
102 ASSERT_TRUE(SaveScreenshot(std::move(screenshot)));
103 }
104 } // namespace testing
105 } // namespace impeller
impeller::testing::GoldenTests
Definition: golden_tests.cc:55
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::AiksContext
Definition: aiks_context.h:19
impeller::testing::GoldenTests::Screenshotter
MetalScreenshotter & Screenshotter()
Definition: golden_tests.cc:60
impeller::testing::Screenshotter
Definition: screenshotter.h:17
golden_digest.h
aiks_context.h
dl_dispatcher.h
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
metal_screenshot.h
impeller::testing::TEST_F
TEST_F(GoldenTests, ConicalGradient)
Definition: golden_tests.cc:76
impeller::testing::Screenshotter::MakeScreenshot
virtual std::unique_ptr< Screenshot > MakeScreenshot(AiksContext &aiks_context, const std::shared_ptr< Texture > texture)=0
working_directory.h
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::WorkingDirectory::Instance
static WorkingDirectory * Instance()
Definition: working_directory.cc:16
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:1195
impeller::testing::GoldenTests::GoldenTests
GoldenTests()
Definition: golden_tests.cc:57
impeller::testing::MetalScreenshotter
Definition: metal_screenshotter.h:18
impeller::testing::GoldenTests::SetUp
void SetUp() override
Definition: golden_tests.cc:62
metal_screenshotter.h
impeller
Definition: allocation.cc:12