Flutter Impeller
golden_playground_test.h
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 #ifndef FLUTTER_IMPELLER_GOLDEN_TESTS_GOLDEN_PLAYGROUND_TEST_H_
6 #define FLUTTER_IMPELLER_GOLDEN_TESTS_GOLDEN_PLAYGROUND_TEST_H_
7 
8 #include <memory>
9 
10 #include "flutter/display_list/display_list.h"
11 #include "flutter/display_list/image/dl_image.h"
15 #include "flutter/testing/testing.h"
18 #include "third_party/abseil-cpp/absl/status/statusor.h"
19 #include "third_party/imgui/imgui.h"
20 
21 #if FML_OS_MACOSX
22 #include "flutter/fml/platform/darwin/scoped_nsautorelease_pool.h"
23 #endif
24 
25 namespace impeller {
26 
28  : public ::testing::TestWithParam<PlaygroundBackend> {
29  public:
31  std::function<std::optional<Picture>(AiksContext& renderer)>;
32 
33  using AiksDlPlaygroundCallback = std::function<sk_sp<flutter::DisplayList>()>;
34 
36 
38 
39  void SetUp();
40 
41  void TearDown();
42 
44 
46  std::shared_ptr<TypographerContext> typographer_context);
47 
48  bool OpenPlaygroundHere(Picture picture);
49 
51 
52  bool OpenPlaygroundHere(const AiksDlPlaygroundCallback& callback);
53 
54  bool OpenPlaygroundHere(const sk_sp<flutter::DisplayList>& list);
55 
56  /// Renders `callback` into an offscreen render pass and saves the result as
57  /// a golden image. The render target is single-sampled, uses the context's
58  /// default color format, and has no depth or stencil attachment, so a
59  /// pipeline built from `PipelineBuilder<>::MakeDefaultPipelineDescriptor`
60  /// must be reduced to match by calling `SetSampleCount(kCount1)`,
61  /// `ClearStencilAttachments()`, and `ClearDepthAttachment()` on it. Calling
62  /// only `SetStencilAttachmentDescriptors(nullopt)` leaves the stencil pixel
63  /// format set and trips Metal's render pipeline validation.
65 
66  std::unique_ptr<testing::Screenshot> MakeScreenshot(
67  const sk_sp<flutter::DisplayList>& list);
68 
69  static bool SaveScreenshot(std::unique_ptr<testing::Screenshot> screenshot,
70  const std::string& postfix = "");
71 
72  static bool ImGuiBegin(const char* name,
73  bool* p_open,
74  ImGuiWindowFlags flags);
75 
76  std::shared_ptr<Texture> CreateTextureForFixture(
77  const char* fixture_name,
78  bool enable_mipmapping = false) const;
79 
80  sk_sp<flutter::DlImage> CreateDlImageForFixture(
81  const char* fixture_name,
82  bool enable_mipmapping = false) const;
83 
84  absl::StatusOr<RuntimeStage::Map> OpenAssetAsRuntimeStage(
85  const char* asset_name) const;
86 
87  std::shared_ptr<Context> GetContext() const;
88 
89  std::shared_ptr<Context> MakeContext() const;
90 
91  Point GetContentScale() const;
92 
93  Scalar GetSecondsElapsed() const;
94 
95  ISize GetWindowSize() const;
96 
97  IRect GetWindowBounds() const;
98 
99  [[nodiscard]] fml::Status SetCapabilities(
100  const std::shared_ptr<Capabilities>& capabilities);
101 
102  /// Returns true if `OpenPlaygroundHere` will actually render anything.
103  bool WillRenderSomething() const { return true; }
104 
106 
107  bool IsGoldenTest() { return true; }
108 
109  protected:
110  void SetWindowSize(ISize size);
111 
112  // See |Playground::PlatformSupportsWideGamutTests|
113  [[nodiscard]] bool PlatformSupportsWideGamutTests() const;
114 
115  // See |Playground::EnsureContextIsUnique|
116  // GoldenPlaygroundTest uses context replacement on the fly to support this.
118 
119  // See |Playground::EnsureContextSupportsWideGamut|
120  // GoldenPlaygroundTest uses name matching to support this.
121  [[nodiscard]] bool EnsureContextSupportsWideGamut() { return true; }
122 
123  // See |Playground::EnsureContextSupportsAntialiasLines|
124  // GoldenPlaygroundTest uses name matching to support this.
126 
127  private:
128 #if FML_OS_MACOSX
129  // This must be placed first so that the autorelease pool is not destroyed
130  // until the GoldenPlaygroundTestImpl has been destructed.
131  fml::ScopedNSAutoreleasePool autorelease_pool_;
132 #endif
133 
134  std::shared_ptr<TypographerContext> typographer_context_;
135 
136  struct GoldenPlaygroundTestImpl;
137  // This is only a shared_ptr so it can work with a forward declared type.
138  std::shared_ptr<GoldenPlaygroundTestImpl> pimpl_;
139 
141 
142  GoldenPlaygroundTest& operator=(const GoldenPlaygroundTest&) = delete;
143 };
144 
145 } // namespace impeller
146 
147 #endif // FLUTTER_IMPELLER_GOLDEN_TESTS_GOLDEN_PLAYGROUND_TEST_H_
bool OpenPlaygroundHere(AiksPlaygroundCallback callback)
RuntimeStageBackend GetRuntimeStageBackend() const
std::function< std::optional< Picture >(AiksContext &renderer)> AiksPlaygroundCallback
sk_sp< flutter::DlImage > CreateDlImageForFixture(const char *fixture_name, bool enable_mipmapping=false) const
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities)
void SetTypographerContext(std::shared_ptr< TypographerContext > typographer_context)
std::shared_ptr< Context > MakeContext() const
static bool SaveScreenshot(std::unique_ptr< testing::Screenshot > screenshot, const std::string &postfix="")
std::unique_ptr< testing::Screenshot > MakeScreenshot(const sk_sp< flutter::DisplayList > &list)
bool OpenPlaygroundHere(Picture picture)
static bool ImGuiBegin(const char *name, bool *p_open, ImGuiWindowFlags flags)
std::function< sk_sp< flutter::DisplayList >()> AiksDlPlaygroundCallback
absl::StatusOr< RuntimeStage::Map > OpenAssetAsRuntimeStage(const char *asset_name) const
std::shared_ptr< Context > GetContext() const
bool WillRenderSomething() const
Returns true if OpenPlaygroundHere will actually render anything.
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
std::function< bool(RenderPass &pass)> SinglePassCallback
Definition: playground.h:39
float Scalar
Definition: scalar.h:19
PlaygroundBackend
Definition: playground.h:27