Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
playground.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_PLAYGROUND_PLAYGROUND_H_
6#define FLUTTER_IMPELLER_PLAYGROUND_PLAYGROUND_H_
7
8#include <atomic>
9#include <chrono>
10#include <memory>
11
12#include "flutter/fml/status.h"
23
24namespace impeller {
25
26class PlaygroundImpl;
27
28namespace testing {
29class GoldenDigestManager;
30}
31
33 kMetal,
37 kVulkan,
38};
39
41
43 public:
44 using SinglePassCallback = std::function<bool(RenderPass& pass)>;
45
46 explicit Playground(PlaygroundBackend backend,
47 const PlaygroundSwitches& switches);
48
49 virtual ~Playground();
50
51 static void OnTearDownTestEnvironment();
52
53 static bool ShouldOpenNewPlaygrounds();
54
55 bool IsPlaygroundEnabled() const;
56
58
59 ISize GetWindowSize() const;
60
61 IRect GetWindowBounds() const;
62
63 Point GetContentScale() const;
64
65 /// @brief Get the amount of time elapsed from the start of the playground's
66 /// execution.
68
69 std::shared_ptr<Context> GetContext() const;
70
71 std::shared_ptr<Context> MakeContext() const;
72
74
75 std::shared_ptr<TypographerContext> GetTypographerContext() const;
76
77 using RenderCallback = std::function<bool(RenderTarget& render_target)>;
78
79 /// @brief Whether this instance will write a golden image of the output
80 /// from |OpenPlaygroundHere|.
82
83 /// @brief Sets a particular test to either write a golden or not, false
84 /// by default.
85 void SetEnableWriteGolden(bool write_golden);
86
87 bool OpenPlaygroundHere(const RenderCallback& render_callback);
88
89 bool OpenPlaygroundHere(const SinglePassCallback& pass_callback);
90
91 static std::shared_ptr<CompressedImage> LoadFixtureImageCompressed(
92 std::shared_ptr<fml::Mapping> mapping);
93
94 static std::optional<DecompressedImage> DecodeImageRGBA(
95 const std::shared_ptr<CompressedImage>& compressed);
96
97 static std::shared_ptr<Texture> CreateTextureForMapping(
98 const std::shared_ptr<Context>& context,
99 std::shared_ptr<fml::Mapping> mapping,
100 bool enable_mipmapping = false);
101
102 std::shared_ptr<Texture> CreateTextureForFixture(
103 const char* fixture_name,
104 bool enable_mipmapping = false) const;
105
106 std::shared_ptr<Texture> CreateTextureCubeForFixture(
107 std::array<const char*, 6> fixture_names) const;
108
109 static bool SupportsBackend(PlaygroundBackend backend);
110
111 virtual std::unique_ptr<fml::Mapping> OpenAssetAsMapping(
112 std::string asset_name) const = 0;
113
114 virtual std::string GetWindowTitle() const = 0;
115
116 [[nodiscard]] fml::Status SetCapabilities(
117 const std::shared_ptr<Capabilities>& capabilities);
118
119 using GLProcAddressResolver = std::function<void*(const char* proc_name)>;
121
123 std::function<void*(void* instance, const char* proc_name)>;
125
126 /// @brief Mark the GPU as unavilable.
127 ///
128 /// Only supported on the Metal backend.
129 void SetGPUDisabled(bool disabled) const;
130
132
133 /// @brief Initializes the provided |PipelineDescriptor| with appropriate
134 /// default values to match the conditions under which a pipeline
135 /// will be rendered.
137
138 protected:
139 // This method could override testing::Test::TearDown() directly, but
140 // since we don't inherit from that Test class the override would not
141 // be recognized. Instead we make this method available to subclasses
142 // that do inherit from testing::Test so that they can redirect to
143 // it during test teardown.
144 void TearDownContextData();
145
146 virtual bool ShouldKeepRendering() const;
147
148 /// @brief Make sure that when the context is later created that it
149 /// will not be shared with any other playgrounds.
150 ///
151 /// Must be called before any other method except for the Ensure family
152 /// of methods.
153 virtual void EnsureContextIsUnique();
154
155 /// @brief Returns true if the platform can support wide gamuts.
157
158 /// @brief Returns true if the rendering path supports MSAA rendering.
159 bool RenderingSupportsMSAA() const;
160
161 /// @brief Returns the default sample count of the rendering path.
163
164 /// @brief Make sure that when the context is later created that it
165 /// will support wide gamuts if the platform supports it.
166 /// Returns whether the platform supports wide gamut.
167 ///
168 /// Must be called before any other method except for the Ensure family
169 /// of methods.
170 ///
171 /// Callers should abort (such as via GTEST_SKIP) if the method returns
172 /// false if their behavior depends on the wide gamut support.
173 ///
174 /// @see PlatformSupportsWideGamut()
175 [[nodiscard]] virtual bool EnsureContextSupportsWideGamut();
176
177 /// @brief Return an unmodifiable reference to the current switches.
178 /// The switches might change at the start of a test as it
179 /// has a brief opportunity to call any of the Ensure* methods
180 /// that define the environment it expects, but should be
181 /// stable by the time any subsequent methods that might perform
182 /// work are called.
183 const PlaygroundSwitches& GetSwitches() const { return switches_; }
184
186 std::shared_ptr<TypographerContext> typographer_context);
187
188 void SetWindowSize(ISize size);
189
191
192 private:
193 static void InitializeGLFWOnce();
194 static std::atomic<bool> glfw_initialized_;
195
196 const PlaygroundBackend backend_;
197 PlaygroundSwitches switches_;
198
199 fml::TimeDelta start_time_;
200
201 // The following state variables are created lazily because not every
202 // playground instance uses them. Most, if not all, do use the |impl_|
203 // and |context_| implicitly, especially when running with the playground
204 // window enabled, but the content and typographer contexts are only used
205 // by a small portion of the unit tests.
206 //
207 // Since they are created lazily upon first reference, they are triggered
208 // by const getter methods and so need to be mutable for the first call
209 // when they get initialized.
210 mutable std::unique_ptr<PlaygroundImpl> impl_;
211 mutable std::shared_ptr<Context> context_;
212 mutable std::unique_ptr<ContentContext> content_context_;
213 mutable std::shared_ptr<TypographerContext> typographer_context_;
214
215 Point cursor_position_;
216 ISize window_size_ = ISize{1024, 768};
217 std::shared_ptr<HostBuffer> host_buffer_;
218 bool should_write_golden_ = false;
219
220 std::unique_ptr<PlaygroundImpl>& GetImpl() const;
221
222 void SetupContext() const;
223
224 void SetupWindow();
225
226 void SetCursorPosition(Point pos);
227
228 [[nodiscard]]
229 bool RenderImage(const RenderCallback& render_callback, bool write_image);
230
231 [[nodiscard]]
232 bool WriteGoldenImage(const RenderTarget& render_target,
233 const std::string& postfix = "");
234
235 Playground(const Playground&) = delete;
236
237 Playground& operator=(const Playground&) = delete;
238};
239
240} // namespace impeller
241
242#endif // FLUTTER_IMPELLER_PLAYGROUND_PLAYGROUND_H_
bool OpenPlaygroundHere(const RenderCallback &render_callback)
std::shared_ptr< Context > MakeContext() const
bool IsPlaygroundEnabled() const
static void OnTearDownTestEnvironment()
virtual bool EnsureContextSupportsWideGamut()
Make sure that when the context is later created that it will support wide gamuts if the platform sup...
virtual bool ShouldKeepRendering() const
bool ShouldWriteGoldenImage()
Whether this instance will write a golden image of the output from |OpenPlaygroundHere|.
static bool ShouldOpenNewPlaygrounds()
bool PlatformSupportsWideGamutTests() const
Returns true if the platform can support wide gamuts.
Point GetCursorPosition() const
void SetWindowSize(ISize size)
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed(std::shared_ptr< fml::Mapping > mapping)
ISize GetWindowSize() const
bool RenderingSupportsMSAA() const
Returns true if the rendering path supports MSAA rendering.
std::function< bool(RenderPass &pass)> SinglePassCallback
Definition playground.h:44
GLProcAddressResolver CreateGLProcAddressResolver() const
RuntimeStageBackend GetRuntimeStageBackend() const
virtual std::string GetWindowTitle() const =0
ContentContext & GetContentContext() const
std::function< bool(RenderTarget &render_target)> RenderCallback
Definition playground.h:77
void SetGPUDisabled(bool disabled) const
Mark the GPU as unavilable.
std::shared_ptr< TypographerContext > GetTypographerContext() const
virtual testing::GoldenDigestManager * GetGoldenDigestManager() const
bool InitializePipelineDescriptorForRendering(PipelineDescriptor &desc) const
Initializes the provided |PipelineDescriptor| with appropriate default values to match the conditions...
std::shared_ptr< Context > GetContext() const
static bool SupportsBackend(PlaygroundBackend backend)
SampleCount GetDefaultSampleCount() const
Returns the default sample count of the rendering path.
static std::shared_ptr< Texture > CreateTextureForMapping(const std::shared_ptr< Context > &context, std::shared_ptr< fml::Mapping > mapping, bool enable_mipmapping=false)
virtual void EnsureContextIsUnique()
Make sure that when the context is later created that it will not be shared with any other playground...
IRect GetWindowBounds() const
const PlaygroundSwitches & GetSwitches() const
Return an unmodifiable reference to the current switches. The switches might change at the start of a...
Definition playground.h:183
virtual std::unique_ptr< fml::Mapping > OpenAssetAsMapping(std::string asset_name) const =0
Point GetContentScale() const
void SetEnableWriteGolden(bool write_golden)
Sets a particular test to either write a golden or not, false by default.
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
Scalar GetSecondsElapsed() const
Get the amount of time elapsed from the start of the playground's execution.
std::function< void *(void *instance, const char *proc_name)> VKProcAddressResolver
Definition playground.h:123
std::function< void *(const char *proc_name)> GLProcAddressResolver
Definition playground.h:119
void SetTypographerContext(std::shared_ptr< TypographerContext > typographer_context)
static std::optional< DecompressedImage > DecodeImageRGBA(const std::shared_ptr< CompressedImage > &compressed)
std::shared_ptr< Texture > CreateTextureCubeForFixture(std::array< const char *, 6 > fixture_names) const
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities)
VKProcAddressResolver CreateVKProcAddressResolver() const
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition render_pass.h:30
VkInstance instance
Definition main.cc:64
std::string PlaygroundBackendToString(PlaygroundBackend backend)
Definition playground.cc:70
float Scalar
Definition scalar.h:19
TPoint< Scalar > Point
Definition point.h:426
PlaygroundBackend
Definition playground.h:32
std::shared_ptr< ContextGLES > context