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"
16 #include "impeller/display_list/dl_dispatcher.h" // nogncheck
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();
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:
62  : screenshotter_(new MetalScreenshotter(GetPlaygroundSwitches())) {}
63 
64  MetalScreenshotter& Screenshotter() { return *screenshotter_; }
65 
66  void SetUp() override {
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
static GoldenDigest * Instance()
void AddDimension(const std::string &name, const std::string &value)
void AddImage(const std::string &test_name, const std::string &filename, int32_t width, int32_t height)
MetalScreenshotter & Screenshotter()
Definition: golden_tests.cc:64
virtual std::unique_ptr< Screenshot > MakeScreenshot(AiksContext &aiks_context, const std::shared_ptr< Texture > texture)=0
static WorkingDirectory * Instance()
TEST_F(GoldenTests, ConicalGradient)
Definition: golden_tests.cc:80
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.
float Scalar
Definition: scalar.h:19