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"
21 
22 namespace impeller {
23 
24 class PlaygroundImpl;
25 
26 enum class PlaygroundBackend {
27  kMetal,
28  kOpenGLES,
29  kVulkan,
30 };
31 
33  PlaygroundBackend backend) {
34  switch (backend) {
41  }
42  FML_UNREACHABLE();
43 }
44 
45 std::string PlaygroundBackendToString(PlaygroundBackend backend);
46 
47 class Playground {
48  public:
49  using SinglePassCallback = std::function<bool(RenderPass& pass)>;
50 
51  explicit Playground(PlaygroundSwitches switches);
52 
53  virtual ~Playground();
54 
55  static bool ShouldOpenNewPlaygrounds();
56 
57  void SetupContext(PlaygroundBackend backend,
58  const PlaygroundSwitches& switches);
59 
60  void SetupWindow();
61 
62  void TeardownWindow();
63 
64  bool IsPlaygroundEnabled() const;
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  std::shared_ptr<Context> MakeContext() const;
79 
80  using RenderCallback = std::function<bool(RenderTarget& render_target)>;
81 
82  bool OpenPlaygroundHere(const RenderCallback& render_callback);
83 
84  bool OpenPlaygroundHere(SinglePassCallback pass_callback);
85 
86  static std::shared_ptr<CompressedImage> LoadFixtureImageCompressed(
87  std::shared_ptr<fml::Mapping> mapping);
88 
89  static std::optional<DecompressedImage> DecodeImageRGBA(
90  const std::shared_ptr<CompressedImage>& compressed);
91 
92  static std::shared_ptr<Texture> CreateTextureForMapping(
93  const std::shared_ptr<Context>& context,
94  std::shared_ptr<fml::Mapping> mapping,
95  bool enable_mipmapping = false);
96 
97  std::shared_ptr<Texture> CreateTextureForFixture(
98  const char* fixture_name,
99  bool enable_mipmapping = false) const;
100 
101  std::shared_ptr<Texture> CreateTextureCubeForFixture(
102  std::array<const char*, 6> fixture_names) const;
103 
104  static bool SupportsBackend(PlaygroundBackend backend);
105 
106  virtual std::unique_ptr<fml::Mapping> OpenAssetAsMapping(
107  std::string asset_name) const = 0;
108 
109  virtual std::string GetWindowTitle() const = 0;
110 
111  [[nodiscard]] fml::Status SetCapabilities(
112  const std::shared_ptr<Capabilities>& capabilities);
113 
114  /// Returns true if `OpenPlaygroundHere` will actually render anything.
115  bool WillRenderSomething() const;
116 
117  using GLProcAddressResolver = std::function<void*(const char* proc_name)>;
119 
120  protected:
122 
123  virtual bool ShouldKeepRendering() const;
124 
125  void SetWindowSize(ISize size);
126 
127  private:
128  fml::TimeDelta start_time_;
129  std::unique_ptr<PlaygroundImpl> impl_;
130  std::shared_ptr<Context> context_;
131  Point cursor_position_;
132  ISize window_size_ = ISize{1024, 768};
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_
impeller::Playground::ShouldOpenNewPlaygrounds
static bool ShouldOpenNewPlaygrounds()
Definition: playground.cc:160
impeller::Playground::CreateTextureCubeForFixture
std::shared_ptr< Texture > CreateTextureCubeForFixture(std::array< const char *, 6 > fixture_names) const
Definition: playground.cc:454
impeller::PlaygroundBackend::kVulkan
@ kVulkan
point.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Playground::WillRenderSomething
bool WillRenderSomething() const
Returns true if OpenPlaygroundHere will actually render anything.
Definition: playground.cc:512
impeller::RuntimeStageBackend::kVulkan
@ kVulkan
impeller::Playground::ShouldKeepRendering
virtual bool ShouldKeepRendering() const
Definition: playground.cc:503
impeller::Playground::DecodeImageRGBA
static std::optional< DecompressedImage > DecodeImageRGBA(const std::shared_ptr< CompressedImage > &compressed)
Definition: playground.cc:368
impeller::Playground::GetSecondsElapsed
Scalar GetSecondsElapsed() const
Get the amount of time elapsed from the start of the playground's execution.
Definition: playground.cc:189
impeller::Playground::GetWindowSize
ISize GetWindowSize() const
Definition: playground.cc:181
impeller::PlaygroundBackend::kMetal
@ kMetal
impeller::PlaygroundBackendToString
std::string PlaygroundBackendToString(PlaygroundBackend backend)
Definition: playground.cc:45
impeller::PlaygroundBackend
PlaygroundBackend
Definition: playground.h:26
impeller::Playground::MakeContext
std::shared_ptr< Context > MakeContext() const
Definition: playground.cc:94
impeller::PlaygroundBackendToRuntimeStageBackend
constexpr RuntimeStageBackend PlaygroundBackendToRuntimeStageBackend(PlaygroundBackend backend)
Definition: playground.h:32
runtime_types.h
impeller::RuntimeStageBackend::kOpenGLES
@ kOpenGLES
impeller::TSize
Definition: size.h:19
render_pass.h
runtime_stage.h
impeller::Playground::RenderCallback
std::function< bool(RenderTarget &render_target)> RenderCallback
Definition: playground.h:80
impeller::Playground
Definition: playground.h:47
impeller::Playground::OpenPlaygroundHere
bool OpenPlaygroundHere(const RenderCallback &render_callback)
Definition: playground.cc:197
impeller::RuntimeStageBackend
RuntimeStageBackend
Definition: runtime_types.h:15
impeller::Playground::SetupWindow
void SetupWindow()
Definition: playground.cc:137
impeller::Playground::CreateGLProcAddressResolver
GLProcAddressResolver CreateGLProcAddressResolver() const
Definition: playground.cc:516
impeller::Playground::switches_
const PlaygroundSwitches switches_
Definition: playground.h:121
impeller::RenderTarget
Definition: render_target.h:38
impeller::Playground::IsPlaygroundEnabled
bool IsPlaygroundEnabled() const
Definition: playground.cc:146
impeller::Playground::SinglePassCallback
std::function< bool(RenderPass &pass)> SinglePassCallback
Definition: playground.h:49
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:429
impeller::Playground::GetContentScale
Point GetContentScale() const
Definition: playground.cc:185
impeller::PlaygroundSwitches
Definition: switches.h:15
impeller::Playground::LoadFixtureImageCompressed
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed(std::shared_ptr< fml::Mapping > mapping)
Definition: playground.cc:357
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:507
impeller::Playground::GetCursorPosition
Point GetCursorPosition() const
Definition: playground.cc:177
texture.h
impeller::Playground::SupportsBackend
static bool SupportsBackend(PlaygroundBackend backend)
Definition: playground.cc:100
impeller::Playground::TeardownWindow
void TeardownWindow()
Definition: playground.cc:150
impeller::Playground::Playground
Playground(PlaygroundSwitches switches)
Definition: playground.cc:83
impeller::Playground::GetContext
std::shared_ptr< Context > GetContext() const
Definition: playground.cc:90
impeller::RuntimeStageBackend::kMetal
@ kMetal
impeller::Playground::GLProcAddressResolver
std::function< void *(const char *proc_name)> GLProcAddressResolver
Definition: playground.h:117
impeller
Definition: allocation.cc:12
impeller::Playground::SetWindowSize
void SetWindowSize(ISize size)
Definition: playground.cc:499
compressed_image.h
impeller::Playground::CreateTextureForFixture
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
Definition: playground.cc:442