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/closure.h"
12 #include "flutter/fml/macros.h"
13 #include "flutter/fml/status.h"
14 #include "flutter/fml/time/time_delta.h"
16 #include "impeller/core/texture.h"
24 
25 namespace impeller {
26 
27 class PlaygroundImpl;
28 
29 enum class PlaygroundBackend {
30  kMetal,
31  kOpenGLES,
32  kVulkan,
33 };
34 
36  PlaygroundBackend backend) {
37  switch (backend) {
44  }
45  FML_UNREACHABLE();
46 }
47 
48 std::string PlaygroundBackendToString(PlaygroundBackend backend);
49 
50 class Playground {
51  public:
52  using SinglePassCallback = std::function<bool(RenderPass& pass)>;
53 
54  explicit Playground(PlaygroundSwitches switches);
55 
56  virtual ~Playground();
57 
58  static bool ShouldOpenNewPlaygrounds();
59 
60  void SetupContext(PlaygroundBackend backend);
61 
62  void SetupWindow();
63 
64  void TeardownWindow();
65 
66  Point GetCursorPosition() const;
67 
68  ISize GetWindowSize() const;
69 
70  Point GetContentScale() const;
71 
72  /// @brief Get the amount of time elapsed from the start of the playground's
73  /// execution.
74  Scalar GetSecondsElapsed() const;
75 
76  std::shared_ptr<Context> GetContext() const;
77 
78  bool OpenPlaygroundHere(const Renderer::RenderCallback& render_callback);
79 
80  bool OpenPlaygroundHere(SinglePassCallback pass_callback);
81 
82  static std::shared_ptr<CompressedImage> LoadFixtureImageCompressed(
83  std::shared_ptr<fml::Mapping> mapping);
84 
85  static std::optional<DecompressedImage> DecodeImageRGBA(
86  const std::shared_ptr<CompressedImage>& compressed);
87 
88  static std::shared_ptr<Texture> CreateTextureForMapping(
89  const std::shared_ptr<Context>& context,
90  std::shared_ptr<fml::Mapping> mapping,
91  bool enable_mipmapping = false);
92 
93  std::shared_ptr<Texture> CreateTextureForFixture(
94  const char* fixture_name,
95  bool enable_mipmapping = false) const;
96 
97  std::shared_ptr<Texture> CreateTextureCubeForFixture(
98  std::array<const char*, 6> fixture_names) const;
99 
100  static bool SupportsBackend(PlaygroundBackend backend);
101 
102  virtual std::unique_ptr<fml::Mapping> OpenAssetAsMapping(
103  std::string asset_name) const = 0;
104 
105  virtual std::string GetWindowTitle() const = 0;
106 
107  [[nodiscard]] fml::Status SetCapabilities(
108  const std::shared_ptr<Capabilities>& capabilities);
109 
110  /// TODO(https://github.com/flutter/flutter/issues/139950): Remove this.
111  /// Returns true if `OpenPlaygroundHere` will actually render anything.
112  bool WillRenderSomething() const;
113 
114  protected:
116 
117  virtual bool ShouldKeepRendering() const;
118 
119  void SetWindowSize(ISize size);
120 
121  private:
122  struct GLFWInitializer;
123 
124  fml::TimeDelta start_time_;
125  std::unique_ptr<GLFWInitializer> glfw_initializer_;
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:152
impeller::Playground::CreateTextureCubeForFixture
std::shared_ptr< Texture > CreateTextureCubeForFixture(std::array< const char *, 6 > fixture_names) const
Definition: playground.cc:487
impeller::PlaygroundBackend::kVulkan
@ kVulkan
point.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Playground::WillRenderSomething
bool WillRenderSomething() const
Definition: playground.cc:540
impeller::RuntimeStageBackend::kVulkan
@ kVulkan
impeller::Playground::ShouldKeepRendering
virtual bool ShouldKeepRendering() const
Definition: playground.cc:531
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:365
impeller::Playground::GetSecondsElapsed
Scalar GetSecondsElapsed() const
Get the amount of time elapsed from the start of the playground's execution.
Definition: playground.cc:181
impeller::Playground::GetWindowSize
ISize GetWindowSize() const
Definition: playground.cc:173
impeller::PlaygroundBackend::kMetal
@ kMetal
impeller::PlaygroundBackendToString
std::string PlaygroundBackendToString(PlaygroundBackend backend)
Definition: playground.cc:39
impeller::PlaygroundBackend
PlaygroundBackend
Definition: playground.h:29
impeller::PlaygroundBackendToRuntimeStageBackend
constexpr RuntimeStageBackend PlaygroundBackendToRuntimeStageBackend(PlaygroundBackend backend)
Definition: playground.h:35
runtime_types.h
impeller::RuntimeStageBackend::kOpenGLES
@ kOpenGLES
impeller::TSize< int64_t >
render_pass.h
runtime_stage.h
impeller::Playground
Definition: playground.h:50
impeller::RuntimeStageBackend
RuntimeStageBackend
Definition: runtime_types.h:14
impeller::Playground::SetupWindow
void SetupWindow()
Definition: playground.cc:126
impeller::Playground::switches_
const PlaygroundSwitches switches_
Definition: playground.h:115
impeller::Playground::GLFWInitializer
Definition: playground.cc:51
impeller::Playground::SinglePassCallback
std::function< bool(RenderPass &pass)> SinglePassCallback
Definition: playground.h:52
switches.h
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:29
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:461
impeller::Playground::GetContentScale
Point GetContentScale() const
Definition: playground.cc:177
impeller::PlaygroundSwitches
Definition: switches.h:16
impeller::Playground::LoadFixtureImageCompressed
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed(std::shared_ptr< fml::Mapping > mapping)
Definition: playground.cc:354
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:535
impeller::Playground::GetCursorPosition
Point GetCursorPosition() const
Definition: playground.cc:169
texture.h
impeller::Playground::SetupContext
void SetupContext(PlaygroundBackend backend)
Definition: playground.cc:114
impeller::Playground::SupportsBackend
static bool SupportsBackend(PlaygroundBackend backend)
Definition: playground.cc:90
impeller::Playground::OpenPlaygroundHere
bool OpenPlaygroundHere(const Renderer::RenderCallback &render_callback)
Definition: playground.cc:189
impeller::Playground::TeardownWindow
void TeardownWindow()
Definition: playground.cc:141
impeller::Playground::Playground
Playground(PlaygroundSwitches switches)
Definition: playground.cc:80
impeller::Playground::GetContext
std::shared_ptr< Context > GetContext() const
Definition: playground.cc:86
renderer.h
impeller::RuntimeStageBackend::kMetal
@ kMetal
impeller
Definition: aiks_context.cc:10
impeller::Playground::SetWindowSize
void SetWindowSize(ISize size)
Definition: playground.cc:527
compressed_image.h
impeller::Playground::CreateTextureForFixture
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
Definition: playground.cc:474