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"
21 
22 namespace impeller {
23 
24 class PlaygroundImpl;
25 
26 enum class PlaygroundBackend {
27  kMetal,
28  kMetalSDF,
29  kOpenGLES,
30  kVulkan,
31 };
32 
33 std::string PlaygroundBackendToString(PlaygroundBackend backend);
34 
35 class Playground {
36  public:
37  using SinglePassCallback = std::function<bool(RenderPass& pass)>;
38 
39  explicit Playground(PlaygroundSwitches switches);
40 
41  virtual ~Playground();
42 
43  static bool ShouldOpenNewPlaygrounds();
44 
45  void SetupContext(PlaygroundBackend backend,
46  const PlaygroundSwitches& switches);
47 
48  void SetupWindow();
49 
50  void TeardownWindow();
51 
52  bool IsPlaygroundEnabled() const;
53 
54  Point GetCursorPosition() const;
55 
56  ISize GetWindowSize() const;
57 
58  Point GetContentScale() const;
59 
60  /// @brief Get the amount of time elapsed from the start of the playground's
61  /// execution.
62  Scalar GetSecondsElapsed() const;
63 
64  std::shared_ptr<Context> GetContext() const;
65 
66  std::shared_ptr<Context> MakeContext() const;
67 
68  using RenderCallback = std::function<bool(RenderTarget& render_target)>;
69 
70  bool OpenPlaygroundHere(const RenderCallback& render_callback);
71 
72  bool OpenPlaygroundHere(SinglePassCallback pass_callback);
73 
74  static std::shared_ptr<CompressedImage> LoadFixtureImageCompressed(
75  std::shared_ptr<fml::Mapping> mapping);
76 
77  static std::optional<DecompressedImage> DecodeImageRGBA(
78  const std::shared_ptr<CompressedImage>& compressed);
79 
80  static std::shared_ptr<Texture> CreateTextureForMapping(
81  const std::shared_ptr<Context>& context,
82  std::shared_ptr<fml::Mapping> mapping,
83  bool enable_mipmapping = false);
84 
85  std::shared_ptr<Texture> CreateTextureForFixture(
86  const char* fixture_name,
87  bool enable_mipmapping = false) const;
88 
89  std::shared_ptr<Texture> CreateTextureCubeForFixture(
90  std::array<const char*, 6> fixture_names) const;
91 
92  static bool SupportsBackend(PlaygroundBackend backend);
93 
94  virtual std::unique_ptr<fml::Mapping> OpenAssetAsMapping(
95  std::string asset_name) const = 0;
96 
97  virtual std::string GetWindowTitle() const = 0;
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;
104 
105  using GLProcAddressResolver = std::function<void*(const char* proc_name)>;
107 
109  std::function<void*(void* instance, const char* proc_name)>;
111 
112  /// @brief Mark the GPU as unavilable.
113  ///
114  /// Only supported on the Metal backend.
115  void SetGPUDisabled(bool disabled) const;
116 
118 
119  protected:
121 
122  virtual bool ShouldKeepRendering() const;
123 
124  void SetWindowSize(ISize size);
125 
126  private:
127  fml::TimeDelta start_time_;
128  std::unique_ptr<PlaygroundImpl> impl_;
129  std::shared_ptr<Context> context_;
130  Point cursor_position_;
131  ISize window_size_ = ISize{1024, 768};
132  std::shared_ptr<HostBuffer> host_buffer_;
133 
134  void SetCursorPosition(Point pos);
135 
136  Playground(const Playground&) = delete;
137 
138  Playground& operator=(const Playground&) = delete;
139 };
140 
141 } // namespace impeller
142 
143 #endif // FLUTTER_IMPELLER_PLAYGROUND_PLAYGROUND_H_
Playground(PlaygroundSwitches switches)
Definition: playground.cc:87
bool OpenPlaygroundHere(const RenderCallback &render_callback)
Definition: playground.cc:205
std::shared_ptr< Context > MakeContext() const
Definition: playground.cc:98
bool IsPlaygroundEnabled() const
Definition: playground.cc:151
virtual bool ShouldKeepRendering() const
Definition: playground.cc:511
static bool ShouldOpenNewPlaygrounds()
Definition: playground.cc:168
Point GetCursorPosition() const
Definition: playground.cc:185
void SetWindowSize(ISize size)
Definition: playground.cc:507
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed(std::shared_ptr< fml::Mapping > mapping)
Definition: playground.cc:365
ISize GetWindowSize() const
Definition: playground.cc:189
std::function< bool(RenderPass &pass)> SinglePassCallback
Definition: playground.h:37
void SetupContext(PlaygroundBackend backend, const PlaygroundSwitches &switches)
Definition: playground.cc:129
GLProcAddressResolver CreateGLProcAddressResolver() const
Definition: playground.cc:524
bool WillRenderSomething() const
Returns true if OpenPlaygroundHere will actually render anything.
Definition: playground.cc:520
RuntimeStageBackend GetRuntimeStageBackend() const
Definition: playground.cc:538
virtual std::string GetWindowTitle() const =0
virtual std::unique_ptr< fml::Mapping > OpenAssetAsMapping(std::string asset_name) const =0
std::function< bool(RenderTarget &render_target)> RenderCallback
Definition: playground.h:68
void SetGPUDisabled(bool disabled) const
Mark the GPU as unavilable.
Definition: playground.cc:534
const PlaygroundSwitches switches_
Definition: playground.h:120
std::shared_ptr< Context > GetContext() const
Definition: playground.cc:94
static bool SupportsBackend(PlaygroundBackend backend)
Definition: playground.cc:104
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:437
Point GetContentScale() const
Definition: playground.cc:193
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
Definition: playground.cc:450
Scalar GetSecondsElapsed() const
Get the amount of time elapsed from the start of the playground's execution.
Definition: playground.cc:197
std::function< void *(void *instance, const char *proc_name)> VKProcAddressResolver
Definition: playground.h:109
std::function< void *(const char *proc_name)> GLProcAddressResolver
Definition: playground.h:105
static std::optional< DecompressedImage > DecodeImageRGBA(const std::shared_ptr< CompressedImage > &compressed)
Definition: playground.cc:376
std::shared_ptr< Texture > CreateTextureCubeForFixture(std::array< const char *, 6 > fixture_names) const
Definition: playground.cc:462
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities)
Definition: playground.cc:515
VKProcAddressResolver CreateVKProcAddressResolver() const
Definition: playground.cc:529
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:47
float Scalar
Definition: scalar.h:19
PlaygroundBackend
Definition: playground.h:26