| 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"
13 #include "impeller/core/host_buffer.h"
14 #include "impeller/core/runtime_types.h"
15 #include "impeller/core/texture.h"
16 #include "impeller/geometry/point.h"
17 #include "impeller/playground/image/compressed_image.h"
18 #include "impeller/playground/image/decompressed_image.h"
19 #include "impeller/playground/switches.h"
20 #include "impeller/renderer/render_pass.h"
21 #include "impeller/runtime_stage/runtime_stage.h"
22
23 namespace impeller {
24
25 class PlaygroundImpl;
26
27 enum class PlaygroundBackend {
28 kMetal,
29 kOpenGLES,
30 kVulkan,
31 };
32
33 constexpr inline RuntimeStageBackend PlaygroundBackendToRuntimeStageBackend(
34 PlaygroundBackend backend) {
35 switch (backend) {
36 case PlaygroundBackend::kMetal:
37 return RuntimeStageBackend::kMetal;
38 case PlaygroundBackend::kOpenGLES:
39 return RuntimeStageBackend::kOpenGLES;
40 case PlaygroundBackend::kVulkan:
41 return RuntimeStageBackend::kVulkan;
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)>;
119 GLProcAddressResolver CreateGLProcAddressResolver() const;
120
121 using VKProcAddressResolver =
122 std::function<void*(void* instance, const char* proc_name)>;
123 VKProcAddressResolver CreateVKProcAddressResolver() const;
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:
131 const PlaygroundSwitches switches_;
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_
impeller::Playground
Definition: playground.h:48
impeller::Playground::Playground
Playground(PlaygroundSwitches switches)
Definition: playground.cc:84
impeller::Playground::OpenPlaygroundHere
bool OpenPlaygroundHere(const RenderCallback &render_callback)
Definition: playground.cc:201
impeller::Playground::MakeContext
std::shared_ptr< Context > MakeContext() const
Definition: playground.cc:95
impeller::Playground::IsPlaygroundEnabled
bool IsPlaygroundEnabled() const
Definition: playground.cc:147
impeller::Playground::ShouldKeepRendering
virtual bool ShouldKeepRendering() const
Definition: playground.cc:507
impeller::Playground::ShouldOpenNewPlaygrounds
static bool ShouldOpenNewPlaygrounds()
Definition: playground.cc:164
impeller::Playground::GetCursorPosition
Point GetCursorPosition() const
Definition: playground.cc:181
impeller::Playground::SetWindowSize
void SetWindowSize(ISize size)
Definition: playground.cc:503
impeller::Playground::LoadFixtureImageCompressed
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed(std::shared_ptr< fml::Mapping > mapping)
Definition: playground.cc:361
impeller::Playground::GetWindowSize
ISize GetWindowSize() const
Definition: playground.cc:185
impeller::Playground::SinglePassCallback
std::function< bool(RenderPass &pass)> SinglePassCallback
Definition: playground.h:50
impeller::Playground::SetupContext
void SetupContext(PlaygroundBackend backend, const PlaygroundSwitches &switches)
Definition: playground.cc:125
impeller::Playground::CreateGLProcAddressResolver
GLProcAddressResolver CreateGLProcAddressResolver() const
Definition: playground.cc:520
impeller::Playground::WillRenderSomething
bool WillRenderSomething() const
Returns true if OpenPlaygroundHere will actually render anything.
Definition: playground.cc:516
impeller::Playground::~Playground
virtual ~Playground()
impeller::Playground::GetWindowTitle
virtual std::string GetWindowTitle() const =0
impeller::Playground::OpenAssetAsMapping
virtual std::unique_ptr< fml::Mapping > OpenAssetAsMapping(std::string asset_name) const =0
impeller::Playground::RenderCallback
std::function< bool(RenderTarget &render_target)> RenderCallback
Definition: playground.h:81
impeller::Playground::SetGPUDisabled
void SetGPUDisabled(bool disabled) const
Mark the GPU as unavilable.
Definition: playground.cc:530
impeller::Playground::TeardownWindow
void TeardownWindow()
Definition: playground.cc:151
impeller::Playground::switches_
const PlaygroundSwitches switches_
Definition: playground.h:131
impeller::Playground::GetContext
std::shared_ptr< Context > GetContext() const
Definition: playground.cc:91
impeller::Playground::SupportsBackend
static bool SupportsBackend(PlaygroundBackend backend)
Definition: playground.cc:101
impeller::Playground::SetupWindow
void SetupWindow()
Definition: playground.cc:138
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:433
impeller::Playground::GetContentScale
Point GetContentScale() const
Definition: playground.cc:189
impeller::Playground::CreateTextureForFixture
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
Definition: playground.cc:446
impeller::Playground::GetSecondsElapsed
Scalar GetSecondsElapsed() const
Get the amount of time elapsed from the start of the playground's execution.
Definition: playground.cc:193
impeller::Playground::VKProcAddressResolver
std::function< void *(void *instance, const char *proc_name)> VKProcAddressResolver
Definition: playground.h:122
impeller::Playground::GLProcAddressResolver
std::function< void *(const char *proc_name)> GLProcAddressResolver
Definition: playground.h:118
impeller::Playground::DecodeImageRGBA
static std::optional< DecompressedImage > DecodeImageRGBA(const std::shared_ptr< CompressedImage > &compressed)
Definition: playground.cc:372
impeller::Playground::CreateTextureCubeForFixture
std::shared_ptr< Texture > CreateTextureCubeForFixture(std::array< const char *, 6 > fixture_names) const
Definition: playground.cc:458
impeller::Playground::SetCapabilities
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities)
Definition: playground.cc:511
impeller::Playground::CreateVKProcAddressResolver
VKProcAddressResolver CreateVKProcAddressResolver() const
Definition: playground.cc:525
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::RenderTarget
Definition: render_target.h:38
compressed_image.h
texture.h
decompressed_image.h
host_buffer.h
impeller
Definition: allocation.cc:12
impeller::PlaygroundBackendToString
std::string PlaygroundBackendToString(PlaygroundBackend backend)
Definition: playground.cc:46
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::RuntimeStageBackend
RuntimeStageBackend
Definition: runtime_types.h:16
impeller::RuntimeStageBackend::kOpenGLES
@ kOpenGLES
impeller::RuntimeStageBackend::kMetal
@ kMetal
impeller::RuntimeStageBackend::kVulkan
@ kVulkan
impeller::PlaygroundBackendToRuntimeStageBackend
constexpr RuntimeStageBackend PlaygroundBackendToRuntimeStageBackend(PlaygroundBackend backend)
Definition: playground.h:33
impeller::PlaygroundBackend
PlaygroundBackend
Definition: playground.h:27
impeller::PlaygroundBackend::kOpenGLES
@ kOpenGLES
impeller::PlaygroundBackend::kMetal
@ kMetal
impeller::PlaygroundBackend::kVulkan
@ kVulkan
switches.h
point.h
render_pass.h
runtime_stage.h
runtime_types.h
impeller::PlaygroundSwitches
Definition: switches.h:16
impeller::TPoint< Scalar >
impeller::TSize
Definition: size.h:25