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"
14 #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  bool OpenPlaygroundHere(const Renderer::RenderCallback& render_callback);
82 
83  bool OpenPlaygroundHere(SinglePassCallback pass_callback);
84 
85  static std::shared_ptr<CompressedImage> LoadFixtureImageCompressed(
86  std::shared_ptr<fml::Mapping> mapping);
87 
88  static std::optional<DecompressedImage> DecodeImageRGBA(
89  const std::shared_ptr<CompressedImage>& compressed);
90 
91  static std::shared_ptr<Texture> CreateTextureForMapping(
92  const std::shared_ptr<Context>& context,
93  std::shared_ptr<fml::Mapping> mapping,
94  bool enable_mipmapping = false);
95 
96  std::shared_ptr<Texture> CreateTextureForFixture(
97  const char* fixture_name,
98  bool enable_mipmapping = false) const;
99 
100  std::shared_ptr<Texture> CreateTextureCubeForFixture(
101  std::array<const char*, 6> fixture_names) const;
102 
103  static bool SupportsBackend(PlaygroundBackend backend);
104 
105  virtual std::unique_ptr<fml::Mapping> OpenAssetAsMapping(
106  std::string asset_name) const = 0;
107 
108  virtual std::string GetWindowTitle() const = 0;
109 
110  [[nodiscard]] fml::Status SetCapabilities(
111  const std::shared_ptr<Capabilities>& capabilities);
112 
113  /// TODO(https://github.com/flutter/flutter/issues/139950): Remove this.
114  /// Returns true if `OpenPlaygroundHere` will actually render anything.
115  bool WillRenderSomething() const;
116 
117  protected:
119 
120  virtual bool ShouldKeepRendering() const;
121 
122  void SetWindowSize(ISize size);
123 
124  private:
125  fml::TimeDelta start_time_;
126  std::unique_ptr<PlaygroundImpl> impl_;
127  std::shared_ptr<Context> context_;
128  std::unique_ptr<Renderer> renderer_;
129  Point cursor_position_;
130  ISize window_size_ = ISize{1024, 768};
131 
132  void SetCursorPosition(Point pos);
133 
134  Playground(const Playground&) = delete;
135 
136  Playground& operator=(const Playground&) = delete;
137 };
138 
139 } // namespace impeller
140 
141 #endif // FLUTTER_IMPELLER_PLAYGROUND_PLAYGROUND_H_
impeller::Playground::ShouldOpenNewPlaygrounds
static bool ShouldOpenNewPlaygrounds()
Definition: playground.cc:167
impeller::Playground::CreateTextureCubeForFixture
std::shared_ptr< Texture > CreateTextureCubeForFixture(std::array< const char *, 6 > fixture_names) const
Definition: playground.cc:467
impeller::PlaygroundBackend::kVulkan
@ kVulkan
point.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Playground::WillRenderSomething
bool WillRenderSomething() const
Definition: playground.cc:530
impeller::RuntimeStageBackend::kVulkan
@ kVulkan
impeller::Playground::ShouldKeepRendering
virtual bool ShouldKeepRendering() const
Definition: playground.cc:521
impeller::Renderer::RenderCallback
std::function< bool(RenderTarget &render_target)> RenderCallback
Definition: renderer.h:23
impeller::Playground::DecodeImageRGBA
static std::optional< DecompressedImage > DecodeImageRGBA(const std::shared_ptr< CompressedImage > &compressed)
Definition: playground.cc:380
impeller::Playground::GetSecondsElapsed
Scalar GetSecondsElapsed() const
Get the amount of time elapsed from the start of the playground's execution.
Definition: playground.cc:196
impeller::Playground::GetWindowSize
ISize GetWindowSize() const
Definition: playground.cc:188
impeller::PlaygroundBackend::kMetal
@ kMetal
impeller::PlaygroundBackendToString
std::string PlaygroundBackendToString(PlaygroundBackend backend)
Definition: playground.cc:45
impeller::PlaygroundBackend
PlaygroundBackend
Definition: playground.h:27
impeller::Playground::MakeContext
std::shared_ptr< Context > MakeContext() const
Definition: playground.cc:94
impeller::PlaygroundBackendToRuntimeStageBackend
constexpr RuntimeStageBackend PlaygroundBackendToRuntimeStageBackend(PlaygroundBackend backend)
Definition: playground.h:33
runtime_types.h
impeller::RuntimeStageBackend::kOpenGLES
@ kOpenGLES
impeller::TSize
Definition: size.h:19
render_pass.h
runtime_stage.h
impeller::Playground
Definition: playground.h:48
impeller::RuntimeStageBackend
RuntimeStageBackend
Definition: runtime_types.h:15
impeller::Playground::SetupWindow
void SetupWindow()
Definition: playground.cc:137
impeller::Playground::switches_
const PlaygroundSwitches switches_
Definition: playground.h:118
impeller::Playground::IsPlaygroundEnabled
bool IsPlaygroundEnabled() const
Definition: playground.cc:152
impeller::Playground::SinglePassCallback
std::function< bool(RenderPass &pass)> SinglePassCallback
Definition: playground.h:50
switches.h
impeller::Playground::SetupContext
void SetupContext(PlaygroundBackend backend, const PlaygroundSwitches &switches)
Definition: playground.cc:124
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
decompressed_image.h
impeller::Playground::~Playground
virtual ~Playground()
impeller::PlaygroundBackend::kOpenGLES
@ kOpenGLES
impeller::Playground::CreateTextureForMapping
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:441
impeller::Playground::GetContentScale
Point GetContentScale() const
Definition: playground.cc:192
impeller::PlaygroundSwitches
Definition: switches.h:15
impeller::Playground::LoadFixtureImageCompressed
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed(std::shared_ptr< fml::Mapping > mapping)
Definition: playground.cc:369
impeller::Playground::GetWindowTitle
virtual std::string GetWindowTitle() const =0
impeller::TPoint< Scalar >
impeller::Playground::OpenAssetAsMapping
virtual std::unique_ptr< fml::Mapping > OpenAssetAsMapping(std::string asset_name) const =0
impeller::Playground::SetCapabilities
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities)
Definition: playground.cc:525
impeller::Playground::GetCursorPosition
Point GetCursorPosition() const
Definition: playground.cc:184
texture.h
impeller::Playground::SupportsBackend
static bool SupportsBackend(PlaygroundBackend backend)
Definition: playground.cc:100
impeller::Playground::OpenPlaygroundHere
bool OpenPlaygroundHere(const Renderer::RenderCallback &render_callback)
Definition: playground.cc:204
impeller::Playground::TeardownWindow
void TeardownWindow()
Definition: playground.cc:156
impeller::Playground::Playground
Playground(PlaygroundSwitches switches)
Definition: playground.cc:83
impeller::Playground::GetContext
std::shared_ptr< Context > GetContext() const
Definition: playground.cc:90
renderer.h
impeller::RuntimeStageBackend::kMetal
@ kMetal
impeller
Definition: aiks_blend_unittests.cc:18
impeller::Playground::SetWindowSize
void SetWindowSize(ISize size)
Definition: playground.cc:517
compressed_image.h
impeller::Playground::CreateTextureForFixture
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
Definition: playground.cc:454