| 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"
15 #include "impeller/core/runtime_types.h"
16 #include "impeller/core/texture.h"
17 #include "impeller/geometry/point.h"
18 #include "impeller/playground/image/compressed_image.h"
19 #include "impeller/playground/image/decompressed_image.h"
20 #include "impeller/playground/switches.h"
21 #include "impeller/renderer/render_pass.h"
22 #include "impeller/renderer/renderer.h"
23 #include "impeller/runtime_stage/runtime_stage.h"
24
25 namespace impeller {
26
27 class PlaygroundImpl;
28
29 enum class PlaygroundBackend {
30 kMetal,
31 kOpenGLES,
32 kVulkan,
33 };
34
35 constexpr inline RuntimeStageBackend PlaygroundBackendToRuntimeStageBackend(
36 PlaygroundBackend backend) {
37 switch (backend) {
38 case PlaygroundBackend::kMetal:
39 return RuntimeStageBackend::kMetal;
40 case PlaygroundBackend::kOpenGLES:
41 return RuntimeStageBackend::kOpenGLES;
42 case PlaygroundBackend::kVulkan:
43 return RuntimeStageBackend::kVulkan;
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 std::shared_ptr<Context> MakeContext() const;
79
80 bool OpenPlaygroundHere(const Renderer::RenderCallback& render_callback);
81
82 bool OpenPlaygroundHere(SinglePassCallback pass_callback);
83
84 static std::shared_ptr<CompressedImage> LoadFixtureImageCompressed(
85 std::shared_ptr<fml::Mapping> mapping);
86
87 static std::optional<DecompressedImage> DecodeImageRGBA(
88 const std::shared_ptr<CompressedImage>& compressed);
89
90 static std::shared_ptr<Texture> CreateTextureForMapping(
91 const std::shared_ptr<Context>& context,
92 std::shared_ptr<fml::Mapping> mapping,
93 bool enable_mipmapping = false);
94
95 std::shared_ptr<Texture> CreateTextureForFixture(
96 const char* fixture_name,
97 bool enable_mipmapping = false) const;
98
99 std::shared_ptr<Texture> CreateTextureCubeForFixture(
100 std::array<const char*, 6> fixture_names) const;
101
102 static bool SupportsBackend(PlaygroundBackend backend);
103
104 virtual std::unique_ptr<fml::Mapping> OpenAssetAsMapping(
105 std::string asset_name) const = 0;
106
107 virtual std::string GetWindowTitle() const = 0;
108
109 [[nodiscard]] fml::Status SetCapabilities(
110 const std::shared_ptr<Capabilities>& capabilities);
111
112 /// TODO(https://github.com/flutter/flutter/issues/139950): Remove this.
113 /// Returns true if `OpenPlaygroundHere` will actually render anything.
114 bool WillRenderSomething() const;
115
116 protected:
117 const PlaygroundSwitches switches_;
118
119 virtual bool ShouldKeepRendering() const;
120
121 void SetWindowSize(ISize size);
122
123 private:
124 fml::TimeDelta start_time_;
125 std::unique_ptr<PlaygroundImpl> impl_;
126 std::shared_ptr<Context> context_;
127 std::unique_ptr<Renderer> renderer_;
128 Point cursor_position_;
129 ISize window_size_ = ISize{1024, 768};
130
131 void SetCursorPosition(Point pos);
132
133 Playground(const Playground&) = delete;
134
135 Playground& operator=(const Playground&) = delete;
136 };
137
138 } // namespace impeller
139
140 #endif // FLUTTER_IMPELLER_PLAYGROUND_PLAYGROUND_H_
impeller::Playground::ShouldOpenNewPlaygrounds
static bool ShouldOpenNewPlaygrounds()
Definition: playground.cc:161
impeller::Playground::CreateTextureCubeForFixture
std::shared_ptr< Texture > CreateTextureCubeForFixture(std::array< const char *, 6 > fixture_names) const
Definition: playground.cc:462
impeller::PlaygroundBackend::kVulkan
@ kVulkan
point.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Playground::WillRenderSomething
bool WillRenderSomething() const
Definition: playground.cc:515
impeller::RuntimeStageBackend::kVulkan
@ kVulkan
impeller::Playground::ShouldKeepRendering
virtual bool ShouldKeepRendering() const
Definition: playground.cc:506
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:374
impeller::Playground::GetSecondsElapsed
Scalar GetSecondsElapsed() const
Get the amount of time elapsed from the start of the playground's execution.
Definition: playground.cc:190
impeller::Playground::GetWindowSize
ISize GetWindowSize() const
Definition: playground.cc:182
impeller::PlaygroundBackend::kMetal
@ kMetal
impeller::PlaygroundBackendToString
std::string PlaygroundBackendToString(PlaygroundBackend backend)
Definition: playground.cc:44
impeller::PlaygroundBackend
PlaygroundBackend
Definition: playground.h:29
impeller::Playground::MakeContext
std::shared_ptr< Context > MakeContext() const
Definition: playground.cc:93
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:15
impeller::Playground::SetupWindow
void SetupWindow()
Definition: playground.cc:135
impeller::Playground::switches_
const PlaygroundSwitches switches_
Definition: playground.h:117
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: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:436
impeller::Playground::GetContentScale
Point GetContentScale() const
Definition: playground.cc:186
impeller::PlaygroundSwitches
Definition: switches.h:16
impeller::Playground::LoadFixtureImageCompressed
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed(std::shared_ptr< fml::Mapping > mapping)
Definition: playground.cc:363
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:510
impeller::Playground::GetCursorPosition
Point GetCursorPosition() const
Definition: playground.cc:178
texture.h
impeller::Playground::SetupContext
void SetupContext(PlaygroundBackend backend)
Definition: playground.cc:123
impeller::Playground::SupportsBackend
static bool SupportsBackend(PlaygroundBackend backend)
Definition: playground.cc:99
impeller::Playground::OpenPlaygroundHere
bool OpenPlaygroundHere(const Renderer::RenderCallback &render_callback)
Definition: playground.cc:198
impeller::Playground::TeardownWindow
void TeardownWindow()
Definition: playground.cc:150
impeller::Playground::Playground
Playground(PlaygroundSwitches switches)
Definition: playground.cc:82
impeller::Playground::GetContext
std::shared_ptr< Context > GetContext() const
Definition: playground.cc:89
renderer.h
impeller::RuntimeStageBackend::kMetal
@ kMetal
impeller
Definition: aiks_blur_unittests.cc:20
impeller::Playground::SetWindowSize
void SetWindowSize(ISize size)
Definition: playground.cc:502
compressed_image.h
impeller::Playground::CreateTextureForFixture
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
Definition: playground.cc:449