Flutter Impeller
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 <chrono>
9 #include <memory>
10 
11 #include "flutter/fml/status.h"
12 #include "flutter/fml/time/time_delta.h"
15 #include "impeller/core/texture.h"
22 
23 namespace impeller {
24 
25 class PlaygroundImpl;
26 
27 enum class PlaygroundBackend {
28  kMetal,
29  kMetalSDF,
30  kOpenGLES,
32  kVulkan,
33 };
34 
35 std::string PlaygroundBackendToString(PlaygroundBackend backend);
36 
37 class Playground {
38  public:
39  using SinglePassCallback = std::function<bool(RenderPass& pass)>;
40 
41  explicit Playground(PlaygroundBackend backend,
42  const PlaygroundSwitches& switches);
43 
44  virtual ~Playground();
45 
46  static bool ShouldOpenNewPlaygrounds();
47 
48  bool IsPlaygroundEnabled() const;
49 
50  Point GetCursorPosition() const;
51 
52  ISize GetWindowSize() const;
53 
54  IRect GetWindowBounds() const;
55 
56  Point GetContentScale() const;
57 
58  /// @brief Get the amount of time elapsed from the start of the playground's
59  /// execution.
60  Scalar GetSecondsElapsed() const;
61 
62  std::shared_ptr<Context> GetContext() const;
63 
64  std::shared_ptr<Context> MakeContext() const;
65 
67 
68  std::shared_ptr<TypographerContext> GetTypographerContext() const;
69 
70  using RenderCallback = std::function<bool(RenderTarget& render_target)>;
71 
72  bool OpenPlaygroundHere(const RenderCallback& render_callback);
73 
74  bool OpenPlaygroundHere(SinglePassCallback pass_callback);
75 
76  static std::shared_ptr<CompressedImage> LoadFixtureImageCompressed(
77  std::shared_ptr<fml::Mapping> mapping);
78 
79  static std::optional<DecompressedImage> DecodeImageRGBA(
80  const std::shared_ptr<CompressedImage>& compressed);
81 
82  static std::shared_ptr<Texture> CreateTextureForMapping(
83  const std::shared_ptr<Context>& context,
84  std::shared_ptr<fml::Mapping> mapping,
85  bool enable_mipmapping = false);
86 
87  std::shared_ptr<Texture> CreateTextureForFixture(
88  const char* fixture_name,
89  bool enable_mipmapping = false) const;
90 
91  std::shared_ptr<Texture> CreateTextureCubeForFixture(
92  std::array<const char*, 6> fixture_names) const;
93 
94  static bool SupportsBackend(PlaygroundBackend backend);
95 
96  virtual std::unique_ptr<fml::Mapping> OpenAssetAsMapping(
97  std::string asset_name) const = 0;
98 
99  virtual std::string GetWindowTitle() const = 0;
100 
101  [[nodiscard]] fml::Status SetCapabilities(
102  const std::shared_ptr<Capabilities>& capabilities);
103 
104  /// Returns true if `OpenPlaygroundHere` will actually render anything.
105  bool WillRenderSomething() const;
106 
107  using GLProcAddressResolver = std::function<void*(const char* proc_name)>;
109 
111  std::function<void*(void* instance, const char* proc_name)>;
113 
114  /// @brief Mark the GPU as unavilable.
115  ///
116  /// Only supported on the Metal backend.
117  void SetGPUDisabled(bool disabled) const;
118 
120 
121  protected:
122  // This method could override testing::Test::TearDown() directly, but
123  // since we don't inherit from that Test class the override would not
124  // be recognized. Instead we make this method available to subclasses
125  // that do inherit from testing::Test so that they can redirect to
126  // it during test teardown.
127  void TearDownContextData();
128 
129  virtual bool ShouldKeepRendering() const;
130 
131  /// @brief Make sure that when the context is later created that it
132  /// will not be shared with any other playgrounds.
133  ///
134  /// Must be called before any other method except for the Ensure family
135  /// of methods.
136  virtual void EnsureContextIsUnique();
137 
138  /// @brief Returns true if the platform can support wide gamuts.
139  bool PlatformSupportsWideGamutTests() const;
140 
141  /// @brief Make sure that when the context is later created that it
142  /// will support wide gamuts if the platform supports it.
143  /// Returns whether the platform supports wide gamut.
144  ///
145  /// Must be called before any other method except for the Ensure family
146  /// of methods.
147  ///
148  /// Callers should abort (such as via GTEST_SKIP) if the method returns
149  /// false if their behavior depends on the wide gamut support.
150  ///
151  /// @see PlatformSupportsWideGamut()
152  [[nodiscard]] virtual bool EnsureContextSupportsWideGamut();
153 
154  /// @brief Make sure that when the context is later created that it
155  /// will support the experimental AA lines flag.
156  ///
157  /// Must be called before any other method except for the Ensure family
158  /// of methods.
160 
161  /// @brief Return an unmodifiable reference to the current switches.
162  /// The switches might change at the start of a test as it
163  /// has a brief opportunity to call any of the Ensure* methods
164  /// that define the environment it expects, but should be
165  /// stable by the time any subsequent methods that might perform
166  /// work are called.
167  const PlaygroundSwitches& GetSwitches() const { return switches_; }
168 
170  std::shared_ptr<TypographerContext> typographer_context);
171 
172  void SetWindowSize(ISize size);
173 
174  private:
175  const PlaygroundBackend backend_;
176  PlaygroundSwitches switches_;
177 
178  fml::TimeDelta start_time_;
179 
180  // The following state variables are created lazily because not every
181  // playground instance uses them. Most, if not all, do use the |impl_|
182  // and |context_| implicitly, especially when running with the playground
183  // window enabled, but the content and typographer contexts are only used
184  // by a small portion of the unit tests.
185  //
186  // Since they are created lazily upon first reference, they are triggered
187  // by const getter methods and so need to be mutable for the first call
188  // when they get initialized.
189  mutable std::unique_ptr<PlaygroundImpl> impl_;
190  mutable std::shared_ptr<Context> context_;
191  mutable std::unique_ptr<ContentContext> content_context_;
192  mutable std::shared_ptr<TypographerContext> typographer_context_;
193 
194  Point cursor_position_;
195  ISize window_size_ = ISize{1024, 768};
196  std::shared_ptr<HostBuffer> host_buffer_;
197 
198  std::unique_ptr<PlaygroundImpl>& GetImpl() const;
199 
200  void SetupContext() const;
201 
202  void SetupWindow();
203 
204  void SetCursorPosition(Point pos);
205 
206  Playground(const Playground&) = delete;
207 
208  Playground& operator=(const Playground&) = delete;
209 };
210 
211 } // namespace impeller
212 
213 #endif // FLUTTER_IMPELLER_PLAYGROUND_PLAYGROUND_H_
bool OpenPlaygroundHere(const RenderCallback &render_callback)
Definition: playground.cc:287
std::shared_ptr< Context > MakeContext() const
Definition: playground.cc:133
bool IsPlaygroundEnabled() const
Definition: playground.cc:224
virtual bool EnsureContextSupportsWideGamut()
Make sure that when the context is later created that it will support wide gamuts if the platform sup...
Definition: playground.cc:112
virtual bool ShouldKeepRendering() const
Definition: playground.cc:597
static bool ShouldOpenNewPlaygrounds()
Definition: playground.cc:246
bool PlatformSupportsWideGamutTests() const
Returns true if the platform can support wide gamuts.
Definition: playground.cc:104
Point GetCursorPosition() const
Definition: playground.cc:263
void SetWindowSize(ISize size)
Definition: playground.cc:593
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed(std::shared_ptr< fml::Mapping > mapping)
Definition: playground.cc:450
ISize GetWindowSize() const
Definition: playground.cc:267
std::function< bool(RenderPass &pass)> SinglePassCallback
Definition: playground.h:39
const PlaygroundSwitches & GetSwitches() const
Return an unmodifiable reference to the current switches. The switches might change at the start of a...
Definition: playground.h:167
GLProcAddressResolver CreateGLProcAddressResolver() const
Definition: playground.cc:610
bool WillRenderSomething() const
Returns true if OpenPlaygroundHere will actually render anything.
Definition: playground.cc:606
RuntimeStageBackend GetRuntimeStageBackend() const
Definition: playground.cc:624
virtual std::string GetWindowTitle() const =0
ContentContext & GetContentContext() const
Definition: playground.cc:146
virtual std::unique_ptr< fml::Mapping > OpenAssetAsMapping(std::string asset_name) const =0
std::function< bool(RenderTarget &render_target)> RenderCallback
Definition: playground.h:70
void SetGPUDisabled(bool disabled) const
Mark the GPU as unavilable.
Definition: playground.cc:620
std::shared_ptr< TypographerContext > GetTypographerContext() const
Definition: playground.cc:155
std::shared_ptr< Context > GetContext() const
Definition: playground.cc:126
static bool SupportsBackend(PlaygroundBackend backend)
Definition: playground.cc:169
static std::shared_ptr< Texture > CreateTextureForMapping(const std::shared_ptr< Context > &context, std::shared_ptr< fml::Mapping > mapping, bool enable_mipmapping=false)
Definition: playground.cc:522
virtual void EnsureContextIsUnique()
Make sure that when the context is later created that it will not be shared with any other playground...
Definition: playground.cc:99
IRect GetWindowBounds() const
Definition: playground.cc:271
Point GetContentScale() const
Definition: playground.cc:275
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
Definition: playground.cc:535
Scalar GetSecondsElapsed() const
Get the amount of time elapsed from the start of the playground's execution.
Definition: playground.cc:279
Playground(PlaygroundBackend backend, const PlaygroundSwitches &switches)
Definition: playground.cc:90
virtual void EnsureContextSupportsAntialiasLines()
Make sure that when the context is later created that it will support the experimental AA lines flag.
Definition: playground.cc:121
std::function< void *(void *instance, const char *proc_name)> VKProcAddressResolver
Definition: playground.h:111
std::function< void *(const char *proc_name)> GLProcAddressResolver
Definition: playground.h:107
void SetTypographerContext(std::shared_ptr< TypographerContext > typographer_context)
Definition: playground.cc:162
static std::optional< DecompressedImage > DecodeImageRGBA(const std::shared_ptr< CompressedImage > &compressed)
Definition: playground.cc:461
std::shared_ptr< Texture > CreateTextureCubeForFixture(std::array< const char *, 6 > fixture_names) const
Definition: playground.cc:547
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities)
Definition: playground.cc:601
VKProcAddressResolver CreateVKProcAddressResolver() const
Definition: playground.cc:615
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
std::string PlaygroundBackendToString(PlaygroundBackend backend)
Definition: playground.cc:48
float Scalar
Definition: scalar.h:19
TPoint< Scalar > Point
Definition: point.h:426
PlaygroundBackend
Definition: playground.h:27
std::shared_ptr< ContextGLES > context