Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
impeller::Playground Class Referenceabstract

#include <playground.h>

Inheritance diagram for impeller::Playground:
impeller::ComputePlaygroundTest impeller::PlaygroundTest impeller::AiksPlayground impeller::DlPlayground impeller::EntityPlayground impeller::RuntimeStagePlayground impeller::testing::RendererDartTest impeller::testing::GaussianBlurFilterContentsTest

Public Types

using SinglePassCallback = std::function< bool(RenderPass &pass)>
 

Public Member Functions

 Playground (PlaygroundSwitches switches)
 
virtual ~Playground ()
 
void SetupContext (PlaygroundBackend backend, const PlaygroundSwitches &switches)
 
void SetupWindow ()
 
void TeardownWindow ()
 
bool IsPlaygroundEnabled () const
 
Point GetCursorPosition () const
 
ISize GetWindowSize () const
 
Point GetContentScale () const
 
Scalar GetSecondsElapsed () const
 Get the amount of time elapsed from the start of the playground's execution.
 
std::shared_ptr< ContextGetContext () const
 
std::shared_ptr< ContextMakeContext () const
 
bool OpenPlaygroundHere (const Renderer::RenderCallback &render_callback)
 
bool OpenPlaygroundHere (SinglePassCallback pass_callback)
 
std::shared_ptr< TextureCreateTextureForFixture (const char *fixture_name, bool enable_mipmapping=false) const
 
std::shared_ptr< TextureCreateTextureCubeForFixture (std::array< const char *, 6 > fixture_names) const
 
virtual std::unique_ptr< fml::MappingOpenAssetAsMapping (std::string asset_name) const =0
 
virtual std::string GetWindowTitle () const =0
 
fml::Status SetCapabilities (const std::shared_ptr< Capabilities > &capabilities)
 
bool WillRenderSomething () const
 

Static Public Member Functions

static bool ShouldOpenNewPlaygrounds ()
 
static std::shared_ptr< CompressedImageLoadFixtureImageCompressed (std::shared_ptr< fml::Mapping > mapping)
 
static std::optional< DecompressedImageDecodeImageRGBA (const std::shared_ptr< CompressedImage > &compressed)
 
static std::shared_ptr< TextureCreateTextureForMapping (const std::shared_ptr< Context > &context, std::shared_ptr< fml::Mapping > mapping, bool enable_mipmapping=false)
 
static bool SupportsBackend (PlaygroundBackend backend)
 

Protected Member Functions

virtual bool ShouldKeepRendering () const
 
void SetWindowSize (ISize size)
 

Protected Attributes

const PlaygroundSwitches switches_
 

Detailed Description

Definition at line 50 of file playground.h.

Member Typedef Documentation

◆ SinglePassCallback

using impeller::Playground::SinglePassCallback = std::function<bool(RenderPass& pass)>

Definition at line 52 of file playground.h.

Constructor & Destructor Documentation

◆ Playground()

impeller::Playground::Playground ( PlaygroundSwitches  switches)
explicit

Definition at line 82 of file playground.cc.

82 : switches_(switches) {
85}
const PlaygroundSwitches switches_
Definition playground.h:120
static void InitializeGLFWOnce()
Definition playground.cc:56
void SetupSwiftshaderOnce(bool use_swiftshader)
Find and setup the installable client driver for a locally built SwiftShader at known paths....

◆ ~Playground()

impeller::Playground::~Playground ( )
virtualdefault

Member Function Documentation

◆ CreateTextureCubeForFixture()

std::shared_ptr< Texture > impeller::Playground::CreateTextureCubeForFixture ( std::array< const char *, 6 >  fixture_names) const

Definition at line 467 of file playground.cc.

468 {
469 std::array<DecompressedImage, 6> images;
470 for (size_t i = 0; i < fixture_names.size(); i++) {
471 auto image = DecodeImageRGBA(
473 if (!image.has_value()) {
474 return nullptr;
475 }
476 images[i] = image.value();
477 }
478
479 auto texture_descriptor = TextureDescriptor{};
480 texture_descriptor.storage_mode = StorageMode::kHostVisible;
481 texture_descriptor.type = TextureType::kTextureCube;
482 texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
483 texture_descriptor.size = images[0].GetSize();
484 texture_descriptor.mip_count = 1u;
485
486 auto texture = renderer_->GetContext()->GetResourceAllocator()->CreateTexture(
487 texture_descriptor);
488 if (!texture) {
489 VALIDATION_LOG << "Could not allocate texture cube.";
490 return nullptr;
491 }
492 texture->SetLabel("Texture cube");
493
494 for (size_t i = 0; i < fixture_names.size(); i++) {
495 auto uploaded =
496 texture->SetContents(images[i].GetAllocation()->GetMapping(),
497 images[i].GetAllocation()->GetSize(), i);
498 if (!uploaded) {
499 VALIDATION_LOG << "Could not upload texture to device memory.";
500 return nullptr;
501 }
502 }
503
504 return texture;
505}
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed(std::shared_ptr< fml::Mapping > mapping)
virtual std::unique_ptr< fml::Mapping > OpenAssetAsMapping(std::string asset_name) const =0
static std::optional< DecompressedImage > DecodeImageRGBA(const std::shared_ptr< CompressedImage > &compressed)
sk_sp< SkImage > image
Definition examples.cpp:29
FlTexture * texture
std::array< MockImage, 3 > images
#define VALIDATION_LOG
Definition validation.h:73

◆ CreateTextureForFixture()

std::shared_ptr< Texture > impeller::Playground::CreateTextureForFixture ( const char *  fixture_name,
bool  enable_mipmapping = false 
) const

Definition at line 454 of file playground.cc.

456 {
457 auto texture = CreateTextureForMapping(renderer_->GetContext(),
458 OpenAssetAsMapping(fixture_name),
459 enable_mipmapping);
460 if (texture == nullptr) {
461 return nullptr;
462 }
463 texture->SetLabel(fixture_name);
464 return texture;
465}
static std::shared_ptr< Texture > CreateTextureForMapping(const std::shared_ptr< Context > &context, std::shared_ptr< fml::Mapping > mapping, bool enable_mipmapping=false)

◆ CreateTextureForMapping()

std::shared_ptr< Texture > impeller::Playground::CreateTextureForMapping ( const std::shared_ptr< Context > &  context,
std::shared_ptr< fml::Mapping mapping,
bool  enable_mipmapping = false 
)
static

Definition at line 441 of file playground.cc.

444 {
446 Playground::LoadFixtureImageCompressed(std::move(mapping)));
447 if (!image.has_value()) {
448 return nullptr;
449 }
450 return CreateTextureForDecompressedImage(context, image.value(),
451 enable_mipmapping);
452}
static std::shared_ptr< Texture > CreateTextureForDecompressedImage(const std::shared_ptr< Context > &context, DecompressedImage &decompressed_image, bool enable_mipmapping)

◆ DecodeImageRGBA()

std::optional< DecompressedImage > impeller::Playground::DecodeImageRGBA ( const std::shared_ptr< CompressedImage > &  compressed)
static

Definition at line 379 of file playground.cc.

380 {
381 if (compressed == nullptr) {
382 return std::nullopt;
383 }
384 // The decoded image is immediately converted into RGBA as that format is
385 // known to be supported everywhere. For image sources that don't need 32
386 // bit pixel strides, this is overkill. Since this is a test fixture we
387 // aren't necessarily trying to eke out memory savings here and instead
388 // favor simplicity.
389 auto image = compressed->Decode().ConvertToRGBA();
390 if (!image.IsValid()) {
391 VALIDATION_LOG << "Could not decode image.";
392 return std::nullopt;
393 }
394
395 return image;
396}

◆ GetContentScale()

Point impeller::Playground::GetContentScale ( ) const

Definition at line 191 of file playground.cc.

191 {
192 return impl_->GetContentScale();
193}

◆ GetContext()

std::shared_ptr< Context > impeller::Playground::GetContext ( ) const

Definition at line 89 of file playground.cc.

89 {
90 return context_;
91}

◆ GetCursorPosition()

Point impeller::Playground::GetCursorPosition ( ) const

Definition at line 183 of file playground.cc.

183 {
184 return cursor_position_;
185}

◆ GetSecondsElapsed()

Scalar impeller::Playground::GetSecondsElapsed ( ) const

Get the amount of time elapsed from the start of the playground's execution.

Definition at line 195 of file playground.cc.

195 {
196 return (fml::TimePoint::Now().ToEpochDelta() - start_time_).ToSecondsF();
197}
static TimePoint Now()
Definition time_point.cc:49

◆ GetWindowSize()

ISize impeller::Playground::GetWindowSize ( ) const

Definition at line 187 of file playground.cc.

187 {
188 return window_size_;
189}

◆ GetWindowTitle()

virtual std::string impeller::Playground::GetWindowTitle ( ) const
pure virtual

◆ IsPlaygroundEnabled()

bool impeller::Playground::IsPlaygroundEnabled ( ) const

Definition at line 151 of file playground.cc.

151 {
153}

◆ LoadFixtureImageCompressed()

std::shared_ptr< CompressedImage > impeller::Playground::LoadFixtureImageCompressed ( std::shared_ptr< fml::Mapping mapping)
static

Definition at line 368 of file playground.cc.

369 {
370 auto compressed_image = CompressedImageSkia::Create(std::move(mapping));
371 if (!compressed_image) {
372 VALIDATION_LOG << "Could not create compressed image.";
373 return nullptr;
374 }
375
376 return compressed_image;
377}
static std::shared_ptr< CompressedImage > Create(std::shared_ptr< const fml::Mapping > allocation)

◆ MakeContext()

std::shared_ptr< Context > impeller::Playground::MakeContext ( ) const

Definition at line 93 of file playground.cc.

93 {
94 // Playgrounds are already making a context for each test, so we can just
95 // return the `context_`.
96 return context_;
97}

◆ OpenAssetAsMapping()

virtual std::unique_ptr< fml::Mapping > impeller::Playground::OpenAssetAsMapping ( std::string  asset_name) const
pure virtual

◆ OpenPlaygroundHere() [1/2]

bool impeller::Playground::OpenPlaygroundHere ( const Renderer::RenderCallback render_callback)

Definition at line 203 of file playground.cc.

204 {
206 return true;
207 }
208
209 if (!render_callback) {
210 return true;
211 }
212
213 if (!renderer_ || !renderer_->IsValid()) {
214 return false;
215 }
216
217 IMGUI_CHECKVERSION();
218 ImGui::CreateContext();
219 fml::ScopedCleanupClosure destroy_imgui_context(
220 []() { ImGui::DestroyContext(); });
221 ImGui::StyleColorsDark();
222
223 auto& io = ImGui::GetIO();
224 io.IniFilename = nullptr;
225 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
226 io.ConfigWindowsResizeFromEdges = true;
227
228 auto window = reinterpret_cast<GLFWwindow*>(impl_->GetWindowHandle());
229 if (!window) {
230 return false;
231 }
232 ::glfwSetWindowTitle(window, GetWindowTitle().c_str());
233 ::glfwSetWindowUserPointer(window, this);
234 ::glfwSetWindowSizeCallback(
235 window, [](GLFWwindow* window, int width, int height) -> void {
236 auto playground =
237 reinterpret_cast<Playground*>(::glfwGetWindowUserPointer(window));
238 if (!playground) {
239 return;
240 }
241 playground->SetWindowSize(ISize{width, height}.Max({}));
242 });
243 ::glfwSetKeyCallback(window, &PlaygroundKeyCallback);
244 ::glfwSetCursorPosCallback(window, [](GLFWwindow* window, double x,
245 double y) {
246 reinterpret_cast<Playground*>(::glfwGetWindowUserPointer(window))
247 ->SetCursorPosition({static_cast<Scalar>(x), static_cast<Scalar>(y)});
248 });
249
250 ImGui_ImplGlfw_InitForOther(window, true);
251 fml::ScopedCleanupClosure shutdown_imgui([]() { ImGui_ImplGlfw_Shutdown(); });
252
253 ImGui_ImplImpeller_Init(renderer_->GetContext());
254 fml::ScopedCleanupClosure shutdown_imgui_impeller(
255 []() { ImGui_ImplImpeller_Shutdown(); });
256
257 ImGui::SetNextWindowPos({10, 10});
258
259 ::glfwSetWindowSize(window, GetWindowSize().width, GetWindowSize().height);
260 ::glfwSetWindowPos(window, 200, 100);
261 ::glfwShowWindow(window);
262
263 while (true) {
264#if FML_OS_MACOSX
266#endif
267 ::glfwPollEvents();
268
269 if (::glfwWindowShouldClose(window)) {
270 return true;
271 }
272
273 ImGui_ImplGlfw_NewFrame();
274
275 Renderer::RenderCallback wrapped_callback =
276 [render_callback,
277 &renderer = renderer_](RenderTarget& render_target) -> bool {
278 ImGui::NewFrame();
279 ImGui::DockSpaceOverViewport(ImGui::GetMainViewport(),
280 ImGuiDockNodeFlags_PassthruCentralNode);
281 bool result = render_callback(render_target);
282 ImGui::Render();
283
284 // Render ImGui overlay.
285 {
286 auto buffer = renderer->GetContext()->CreateCommandBuffer();
287 if (!buffer) {
288 return false;
289 }
290 buffer->SetLabel("ImGui Command Buffer");
291
292 if (render_target.GetColorAttachments().empty()) {
293 return false;
294 }
295
296 auto color0 = render_target.GetColorAttachments().find(0)->second;
297 color0.load_action = LoadAction::kLoad;
298 if (color0.resolve_texture) {
299 color0.texture = color0.resolve_texture;
300 color0.resolve_texture = nullptr;
301 color0.store_action = StoreAction::kStore;
302 }
303 render_target.SetColorAttachment(color0, 0);
304
305 render_target.SetStencilAttachment(std::nullopt);
306 render_target.SetDepthAttachment(std::nullopt);
307
308 auto pass = buffer->CreateRenderPass(render_target);
309 if (!pass) {
310 return false;
311 }
312 pass->SetLabel("ImGui Render Pass");
313
314 ImGui_ImplImpeller_RenderDrawData(ImGui::GetDrawData(), *pass);
315
316 pass->EncodeCommands();
317 if (!renderer->GetContext()->GetCommandQueue()->Submit({buffer}).ok()) {
318 return false;
319 }
320 }
321
322 return result;
323 };
324
325 if (!renderer_->Render(impl_->AcquireSurfaceFrame(renderer_->GetContext()),
326 wrapped_callback)) {
327 VALIDATION_LOG << "Could not render into the surface.";
328 return false;
329 }
330
331 if (!ShouldKeepRendering()) {
332 break;
333 }
334 }
335
336 ::glfwHideWindow(window);
337
338 return true;
339}
AutoreleasePool pool
Wraps a closure that is invoked in the destructor unless released by the caller.
Definition closure.h:32
Playground(PlaygroundSwitches switches)
Definition playground.cc:82
virtual bool ShouldKeepRendering() const
ISize GetWindowSize() const
virtual std::string GetWindowTitle() const =0
std::function< bool(RenderTarget &render_target)> RenderCallback
Definition renderer.h:23
GLFWwindow * window
Definition main.cc:45
static const uint8_t buffer[]
GAsyncResult * result
bool ImGui_ImplImpeller_Init(const std::shared_ptr< impeller::Context > &context)
void ImGui_ImplImpeller_RenderDrawData(ImDrawData *draw_data, impeller::RenderPass &render_pass)
void ImGui_ImplImpeller_Shutdown()
double y
double x
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
float Scalar
Definition scalar.h:18
static void PlaygroundKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
TSize< int64_t > ISize
Definition size.h:138
int32_t height
int32_t width
constexpr TSize Max(const TSize &o) const
Definition size.h:81

◆ OpenPlaygroundHere() [2/2]

bool impeller::Playground::OpenPlaygroundHere ( SinglePassCallback  pass_callback)

Definition at line 341 of file playground.cc.

341 {
342 return OpenPlaygroundHere(
343 [context = GetContext(), &pass_callback](RenderTarget& render_target) {
344 auto buffer = context->CreateCommandBuffer();
345 if (!buffer) {
346 return false;
347 }
348 buffer->SetLabel("Playground Command Buffer");
349
350 auto pass = buffer->CreateRenderPass(render_target);
351 if (!pass) {
352 return false;
353 }
354 pass->SetLabel("Playground Render Pass");
355
356 if (!pass_callback(*pass)) {
357 return false;
358 }
359
360 pass->EncodeCommands();
361 if (!context->GetCommandQueue()->Submit({buffer}).ok()) {
362 return false;
363 }
364 return true;
365 });
366}
std::shared_ptr< Context > GetContext() const
Definition playground.cc:89
bool OpenPlaygroundHere(const Renderer::RenderCallback &render_callback)

◆ SetCapabilities()

fml::Status impeller::Playground::SetCapabilities ( const std::shared_ptr< Capabilities > &  capabilities)

Definition at line 515 of file playground.cc.

516 {
517 return impl_->SetCapabilities(capabilities);
518}

◆ SetupContext()

void impeller::Playground::SetupContext ( PlaygroundBackend  backend,
const PlaygroundSwitches switches 
)

Definition at line 123 of file playground.cc.

124 {
125 FML_CHECK(SupportsBackend(backend));
126
127 impl_ = PlaygroundImpl::Create(backend, switches);
128 if (!impl_) {
129 FML_LOG(WARNING) << "PlaygroundImpl::Create failed.";
130 return;
131 }
132
133 context_ = impl_->GetContext();
134}
static std::unique_ptr< PlaygroundImpl > Create(PlaygroundBackend backend, PlaygroundSwitches switches)
static bool SupportsBackend(PlaygroundBackend backend)
Definition playground.cc:99
#define FML_LOG(severity)
Definition logging.h:82
#define FML_CHECK(condition)
Definition logging.h:85

◆ SetupWindow()

void impeller::Playground::SetupWindow ( )

Definition at line 136 of file playground.cc.

136 {
137 if (!context_) {
138 FML_LOG(WARNING) << "Asked to set up a window with no context (call "
139 "SetupContext first).";
140 return;
141 }
142 auto renderer = std::make_unique<Renderer>(context_);
143 if (!renderer->IsValid()) {
144 return;
145 }
146 renderer_ = std::move(renderer);
147
148 start_time_ = fml::TimePoint::Now().ToEpochDelta();
149}
TimeDelta ToEpochDelta() const
Definition time_point.h:52

◆ SetWindowSize()

void impeller::Playground::SetWindowSize ( ISize  size)
protected

Definition at line 507 of file playground.cc.

507 {
508 window_size_ = size;
509}
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition switches.h:259

◆ ShouldKeepRendering()

bool impeller::Playground::ShouldKeepRendering ( ) const
protectedvirtual

Definition at line 511 of file playground.cc.

511 {
512 return true;
513}

◆ ShouldOpenNewPlaygrounds()

bool impeller::Playground::ShouldOpenNewPlaygrounds ( )
static

Definition at line 166 of file playground.cc.

166 {
168}
static std::atomic_bool gShouldOpenNewPlaygrounds

◆ SupportsBackend()

bool impeller::Playground::SupportsBackend ( PlaygroundBackend  backend)
static

Definition at line 99 of file playground.cc.

99 {
100 switch (backend) {
102#if IMPELLER_ENABLE_METAL
103 return true;
104#else // IMPELLER_ENABLE_METAL
105 return false;
106#endif // IMPELLER_ENABLE_METAL
108#if IMPELLER_ENABLE_OPENGLES
109 return true;
110#else // IMPELLER_ENABLE_OPENGLES
111 return false;
112#endif // IMPELLER_ENABLE_OPENGLES
114#if IMPELLER_ENABLE_VULKAN
116#else // IMPELLER_ENABLE_VULKAN
117 return false;
118#endif // IMPELLER_ENABLE_VULKAN
119 }
121}
#define FML_UNREACHABLE()
Definition logging.h:109

◆ TeardownWindow()

void impeller::Playground::TeardownWindow ( )

Definition at line 155 of file playground.cc.

155 {
156 if (context_) {
157 context_->Shutdown();
158 }
159 context_.reset();
160 renderer_.reset();
161 impl_.reset();
162}

◆ WillRenderSomething()

bool impeller::Playground::WillRenderSomething ( ) const

TODO(https://github.com/flutter/flutter/issues/139950): Remove this. Returns true if OpenPlaygroundHere will actually render anything.

Definition at line 520 of file playground.cc.

520 {
522}

Member Data Documentation

◆ switches_

const PlaygroundSwitches impeller::Playground::switches_
protected

Definition at line 120 of file playground.h.


The documentation for this class was generated from the following files: