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  kOpenGLES,
30  kVulkan,
31 };
32 
34  PlaygroundBackend backend) {
35  switch (backend) {
42  }
43  FML_UNREACHABLE();
44 }
45 
46 std::string PlaygroundBackendToString(PlaygroundBackend backend);
47 
48 class Playground {
49  public:
50  using SinglePassCallback = std::function<bool(RenderPass& pass)>;
51 
52  explicit Playground(PlaygroundSwitches switches);
53 
54  virtual ~Playground();
55 
56  static bool ShouldOpenNewPlaygrounds();
57 
58  void SetupContext(PlaygroundBackend backend,
59  const PlaygroundSwitches& switches);
60 
61  void SetupWindow();
62 
63  void TeardownWindow();
64 
65  bool IsPlaygroundEnabled() const;
66 
67  Point GetCursorPosition() const;
68 
69  ISize GetWindowSize() const;
70 
71  Point GetContentScale() const;
72 
73  /// @brief Get the amount of time elapsed from the start of the playground's
74  /// execution.
75  Scalar GetSecondsElapsed() const;
76 
77  std::shared_ptr<Context> GetContext() const;
78 
79  std::shared_ptr<Context> MakeContext() const;
80 
81  using RenderCallback = std::function<bool(RenderTarget& render_target)>;
82 
83  bool OpenPlaygroundHere(const RenderCallback& render_callback);
84 
85  bool OpenPlaygroundHere(SinglePassCallback pass_callback);
86 
87  static std::shared_ptr<CompressedImage> LoadFixtureImageCompressed(
88  std::shared_ptr<fml::Mapping> mapping);
89 
90  static std::optional<DecompressedImage> DecodeImageRGBA(
91  const std::shared_ptr<CompressedImage>& compressed);
92 
93  static std::shared_ptr<Texture> CreateTextureForMapping(
94  const std::shared_ptr<Context>& context,
95  std::shared_ptr<fml::Mapping> mapping,
96  bool enable_mipmapping = false);
97 
98  std::shared_ptr<Texture> CreateTextureForFixture(
99  const char* fixture_name,
100  bool enable_mipmapping = false) const;
101 
102  std::shared_ptr<Texture> CreateTextureCubeForFixture(
103  std::array<const char*, 6> fixture_names) const;
104 
105  static bool SupportsBackend(PlaygroundBackend backend);
106 
107  virtual std::unique_ptr<fml::Mapping> OpenAssetAsMapping(
108  std::string asset_name) const = 0;
109 
110  virtual std::string GetWindowTitle() const = 0;
111 
112  [[nodiscard]] fml::Status SetCapabilities(
113  const std::shared_ptr<Capabilities>& capabilities);
114 
115  /// Returns true if `OpenPlaygroundHere` will actually render anything.
116  bool WillRenderSomething() const;
117 
118  using GLProcAddressResolver = std::function<void*(const char* proc_name)>;
120 
122  std::function<void*(void* instance, const char* proc_name)>;
124 
125  /// @brief Mark the GPU as unavilable.
126  ///
127  /// Only supported on the Metal backend.
128  void SetGPUDisabled(bool disabled) const;
129 
130  protected:
132 
133  virtual bool ShouldKeepRendering() const;
134 
135  void SetWindowSize(ISize size);
136 
137  private:
138  fml::TimeDelta start_time_;
139  std::unique_ptr<PlaygroundImpl> impl_;
140  std::shared_ptr<Context> context_;
141  Point cursor_position_;
142  ISize window_size_ = ISize{1024, 768};
143  std::shared_ptr<HostBuffer> host_buffer_;
144 
145  void SetCursorPosition(Point pos);
146 
147  Playground(const Playground&) = delete;
148 
149  Playground& operator=(const Playground&) = delete;
150 };
151 
152 } // namespace impeller
153 
154 #endif // FLUTTER_IMPELLER_PLAYGROUND_PLAYGROUND_H_
Playground(PlaygroundSwitches switches)
Definition: playground.cc:84
bool OpenPlaygroundHere(const RenderCallback &render_callback)
Definition: playground.cc:201
std::shared_ptr< Context > MakeContext() const
Definition: playground.cc:95
bool IsPlaygroundEnabled() const
Definition: playground.cc:147
virtual bool ShouldKeepRendering() const
Definition: playground.cc:507
static bool ShouldOpenNewPlaygrounds()
Definition: playground.cc:164
Point GetCursorPosition() const
Definition: playground.cc:181
void SetWindowSize(ISize size)
Definition: playground.cc:503
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed(std::shared_ptr< fml::Mapping > mapping)
Definition: playground.cc:361
ISize GetWindowSize() const
Definition: playground.cc:185
std::function< bool(RenderPass &pass)> SinglePassCallback
Definition: playground.h:50
void SetupContext(PlaygroundBackend backend, const PlaygroundSwitches &switches)
Definition: playground.cc:125
GLProcAddressResolver CreateGLProcAddressResolver() const
Definition: playground.cc:520
bool WillRenderSomething() const
Returns true if OpenPlaygroundHere will actually render anything.
Definition: playground.cc:516
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:81
void SetGPUDisabled(bool disabled) const
Mark the GPU as unavilable.
Definition: playground.cc:530
const PlaygroundSwitches switches_
Definition: playground.h:131
std::shared_ptr< Context > GetContext() const
Definition: playground.cc:91
static bool SupportsBackend(PlaygroundBackend backend)
Definition: playground.cc:101
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:433
Point GetContentScale() const
Definition: playground.cc:189
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
Definition: playground.cc:446
Scalar GetSecondsElapsed() const
Get the amount of time elapsed from the start of the playground's execution.
Definition: playground.cc:193
std::function< void *(void *instance, const char *proc_name)> VKProcAddressResolver
Definition: playground.h:122
std::function< void *(const char *proc_name)> GLProcAddressResolver
Definition: playground.h:118
static std::optional< DecompressedImage > DecodeImageRGBA(const std::shared_ptr< CompressedImage > &compressed)
Definition: playground.cc:372
std::shared_ptr< Texture > CreateTextureCubeForFixture(std::array< const char *, 6 > fixture_names) const
Definition: playground.cc:458
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities)
Definition: playground.cc:511
VKProcAddressResolver CreateVKProcAddressResolver() const
Definition: playground.cc:525
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:46
float Scalar
Definition: scalar.h:19
constexpr RuntimeStageBackend PlaygroundBackendToRuntimeStageBackend(PlaygroundBackend backend)
Definition: playground.h:33
PlaygroundBackend
Definition: playground.h:27