Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
playground.cc
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#include <array>
6#include <memory>
7#include <optional>
8#include <sstream>
9
10#include "fml/closure.h"
11#include "fml/time/time_point.h"
18
19#define GLFW_INCLUDE_NONE
20#include "third_party/glfw/include/GLFW/glfw3.h"
21
22#include "flutter/fml/paths.h"
34#include "third_party/imgui/backends/imgui_impl_glfw.h"
35#include "third_party/imgui/imgui.h"
36
37#if FML_OS_MACOSX
39#endif // FML_OS_MACOSX
40
41#if IMPELLER_ENABLE_VULKAN
43#endif // IMPELLER_ENABLE_VULKAN
44
45namespace impeller {
46
48 switch (backend) {
50 return "Metal";
52 return "MetalSDF";
54 return "OpenGLES";
56 return "Vulkan";
57 }
59}
60
61static void InitializeGLFWOnce() {
62 // This guard is a hack to work around a problem where glfwCreateWindow
63 // hangs when opening a second window after GLFW has been reinitialized (for
64 // example, when flipping through multiple playground tests).
65 //
66 // Explanation:
67 // * glfwCreateWindow calls [NSApp run], which begins running the event
68 // loop on the current thread.
69 // * GLFW then immediately stops the loop when
70 // applicationDidFinishLaunching is fired.
71 // * applicationDidFinishLaunching is only ever fired once during the
72 // application's lifetime, so subsequent calls to [NSApp run] will always
73 // hang with this setup.
74 // * glfwInit resets the flag that guards against [NSApp run] being
75 // called a second time, which causes the subsequent `glfwCreateWindow`
76 // to hang indefinitely in the event loop, because
77 // applicationDidFinishLaunching is never fired.
78 static std::once_flag sOnceInitializer;
79 std::call_once(sOnceInitializer, []() {
80 ::glfwSetErrorCallback([](int code, const char* description) {
81 FML_LOG(ERROR) << "GLFW Error '" << description << "' (" << code << ").";
82 });
83 FML_CHECK(::glfwInit() == GLFW_TRUE);
84 });
85}
86
91
92Playground::~Playground() = default;
93
94std::shared_ptr<Context> Playground::GetContext() const {
95 return context_;
96}
97
98std::shared_ptr<Context> Playground::MakeContext() const {
99 // Playgrounds are already making a context for each test, so we can just
100 // return the `context_`.
101 return context_;
102}
103
105 switch (backend) {
108#if IMPELLER_ENABLE_METAL
109 return true;
110#else // IMPELLER_ENABLE_METAL
111 return false;
112#endif // IMPELLER_ENABLE_METAL
114#if IMPELLER_ENABLE_OPENGLES
115 return true;
116#else // IMPELLER_ENABLE_OPENGLES
117 return false;
118#endif // IMPELLER_ENABLE_OPENGLES
120#if IMPELLER_ENABLE_VULKAN
122#else // IMPELLER_ENABLE_VULKAN
123 return false;
124#endif // IMPELLER_ENABLE_VULKAN
125 }
127}
128
130 const PlaygroundSwitches& switches) {
131 FML_CHECK(SupportsBackend(backend));
132
133 impl_ = PlaygroundImpl::Create(backend, switches);
134 if (!impl_) {
135 FML_LOG(WARNING) << "PlaygroundImpl::Create failed.";
136 return;
137 }
138
139 context_ = impl_->GetContext();
140}
141
143 if (!context_) {
144 FML_LOG(WARNING) << "Asked to set up a window with no context (call "
145 "SetupContext first).";
146 return;
147 }
148 start_time_ = fml::TimePoint::Now().ToEpochDelta();
149}
150
154
156 if (host_buffer_) {
157 host_buffer_.reset();
158 }
159 if (context_) {
160 context_->Shutdown();
161 }
162 context_.reset();
163 impl_.reset();
164}
165
166static std::atomic_bool gShouldOpenNewPlaygrounds = true;
167
171
172static void PlaygroundKeyCallback(GLFWwindow* window,
173 int key,
174 int scancode,
175 int action,
176 int mods) {
177 if ((key == GLFW_KEY_ESCAPE) && action == GLFW_RELEASE) {
178 if (mods & (GLFW_MOD_CONTROL | GLFW_MOD_SUPER | GLFW_MOD_SHIFT)) {
180 }
181 ::glfwSetWindowShouldClose(window, GLFW_TRUE);
182 }
183}
184
186 return cursor_position_;
187}
188
190 return window_size_;
191}
192
194 return IRect::MakeSize(window_size_);
195}
196
198 return impl_->GetContentScale();
199}
200
202 return (fml::TimePoint::Now().ToEpochDelta() - start_time_).ToSecondsF();
203}
204
205void Playground::SetCursorPosition(Point pos) {
206 cursor_position_ = pos;
207}
208
210 const Playground::RenderCallback& render_callback) {
212 return true;
213 }
214
215 if (!render_callback) {
216 return true;
217 }
218
219 IMGUI_CHECKVERSION();
220 ImGui::CreateContext();
221 fml::ScopedCleanupClosure destroy_imgui_context(
222 []() { ImGui::DestroyContext(); });
223 ImGui::StyleColorsDark();
224
225 auto& io = ImGui::GetIO();
226 io.IniFilename = nullptr;
227 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
228 io.ConfigWindowsResizeFromEdges = true;
229
230 auto window = reinterpret_cast<GLFWwindow*>(impl_->GetWindowHandle());
231 if (!window) {
232 return false;
233 }
234 ::glfwSetWindowTitle(window, GetWindowTitle().c_str());
235 ::glfwSetWindowUserPointer(window, this);
236 ::glfwSetWindowSizeCallback(
237 window, [](GLFWwindow* window, int width, int height) -> void {
238 auto playground =
239 reinterpret_cast<Playground*>(::glfwGetWindowUserPointer(window));
240 if (!playground) {
241 return;
242 }
243 playground->SetWindowSize(ISize{width, height}.Max({}));
244 });
245 ::glfwSetKeyCallback(window, &PlaygroundKeyCallback);
246 ::glfwSetCursorPosCallback(window, [](GLFWwindow* window, double x,
247 double y) {
248 reinterpret_cast<Playground*>(::glfwGetWindowUserPointer(window))
249 ->SetCursorPosition({static_cast<Scalar>(x), static_cast<Scalar>(y)});
250 });
251
252 ImGui_ImplGlfw_InitForOther(window, true);
253 fml::ScopedCleanupClosure shutdown_imgui([]() { ImGui_ImplGlfw_Shutdown(); });
254
255 ImGui_ImplImpeller_Init(context_);
256 fml::ScopedCleanupClosure shutdown_imgui_impeller(
257 []() { ImGui_ImplImpeller_Shutdown(); });
258
259 ImGui::SetNextWindowPos({10, 10});
260
261 ::glfwSetWindowSize(window, GetWindowSize().width, GetWindowSize().height);
262 ::glfwSetWindowPos(window, 200, 100);
263 ::glfwShowWindow(window);
264
265 while (true) {
266#if FML_OS_MACOSX
268#endif
269 ::glfwPollEvents();
270
271 if (::glfwWindowShouldClose(window)) {
272 return true;
273 }
274
275 ImGui_ImplGlfw_NewFrame();
276
277 auto surface = impl_->AcquireSurfaceFrame(context_);
278 RenderTarget render_target = surface->GetRenderTarget();
279
280 ImGui::NewFrame();
281 ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport(),
282 ImGuiDockNodeFlags_PassthruCentralNode);
283 bool result = render_callback(render_target);
284 ImGui::Render();
285
286 // Render ImGui overlay.
287 {
288 auto buffer = context_->CreateCommandBuffer();
289 if (!buffer) {
290 VALIDATION_LOG << "Could not create command buffer.";
291 return false;
292 }
293 buffer->SetLabel("ImGui Command Buffer");
294
295 auto color0 = render_target.GetColorAttachment(0);
297 if (color0.resolve_texture) {
298 color0.texture = color0.resolve_texture;
299 color0.resolve_texture = nullptr;
300 color0.store_action = StoreAction::kStore;
301 }
302 render_target.SetColorAttachment(color0, 0);
303 render_target.SetStencilAttachment(std::nullopt);
304 render_target.SetDepthAttachment(std::nullopt);
305
306 auto pass = buffer->CreateRenderPass(render_target);
307 if (!pass) {
308 VALIDATION_LOG << "Could not create render pass.";
309 return false;
310 }
311 pass->SetLabel("ImGui Render Pass");
312 if (!host_buffer_) {
313 host_buffer_ = HostBuffer::Create(
314 context_->GetResourceAllocator(), context_->GetIdleWaiter(),
315 context_->GetCapabilities()->GetMinimumUniformAlignment());
316 }
317
318 ImGui_ImplImpeller_RenderDrawData(ImGui::GetDrawData(), *pass,
319 *host_buffer_);
320
321 pass->EncodeCommands();
322
323 if (!context_->GetCommandQueue()->Submit({buffer}).ok()) {
324 return false;
325 }
326 }
327
328 if (!result || !surface->Present()) {
329 return false;
330 }
331
332 if (!ShouldKeepRendering()) {
333 break;
334 }
335 }
336
337 ::glfwHideWindow(window);
338
339 return true;
340}
341
343 return OpenPlaygroundHere(
344 [context = GetContext(), &pass_callback](RenderTarget& render_target) {
345 auto buffer = context->CreateCommandBuffer();
346 if (!buffer) {
347 return false;
348 }
349 buffer->SetLabel("Playground Command Buffer");
350
351 auto pass = buffer->CreateRenderPass(render_target);
352 if (!pass) {
353 return false;
354 }
355 pass->SetLabel("Playground Render Pass");
356
357 if (!pass_callback(*pass)) {
358 return false;
359 }
360
361 pass->EncodeCommands();
362 if (!context->GetCommandQueue()->Submit({buffer}).ok()) {
363 return false;
364 }
365 return true;
366 });
367}
368
369std::shared_ptr<CompressedImage> Playground::LoadFixtureImageCompressed(
370 std::shared_ptr<fml::Mapping> mapping) {
371 auto compressed_image = CompressedImageSkia::Create(std::move(mapping));
372 if (!compressed_image) {
373 VALIDATION_LOG << "Could not create compressed image.";
374 return nullptr;
375 }
376
377 return compressed_image;
378}
379
380std::optional<DecompressedImage> Playground::DecodeImageRGBA(
381 const std::shared_ptr<CompressedImage>& compressed) {
382 if (compressed == nullptr) {
383 return std::nullopt;
384 }
385 // The decoded image is immediately converted into RGBA as that format is
386 // known to be supported everywhere. For image sources that don't need 32
387 // bit pixel strides, this is overkill. Since this is a test fixture we
388 // aren't necessarily trying to eke out memory savings here and instead
389 // favor simplicity.
390 auto image = compressed->Decode().ConvertToRGBA();
391 if (!image.IsValid()) {
392 VALIDATION_LOG << "Could not decode image.";
393 return std::nullopt;
394 }
395
396 return image;
397}
398
399static std::shared_ptr<Texture> CreateTextureForDecompressedImage(
400 const std::shared_ptr<Context>& context,
401 DecompressedImage& decompressed_image,
402 bool enable_mipmapping) {
403 TextureDescriptor texture_descriptor;
404 texture_descriptor.storage_mode = StorageMode::kDevicePrivate;
405 texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
406 texture_descriptor.size = decompressed_image.GetSize();
407 texture_descriptor.mip_count =
408 enable_mipmapping ? decompressed_image.GetSize().MipCount() : 1u;
409
410 auto texture =
411 context->GetResourceAllocator()->CreateTexture(texture_descriptor);
412 if (!texture) {
413 VALIDATION_LOG << "Could not allocate texture for fixture.";
414 return nullptr;
415 }
416
417 auto command_buffer = context->CreateCommandBuffer();
418 if (!command_buffer) {
419 FML_DLOG(ERROR) << "Could not create command buffer for mipmap generation.";
420 return nullptr;
421 }
422 command_buffer->SetLabel("Mipmap Command Buffer");
423
424 auto blit_pass = command_buffer->CreateBlitPass();
425 auto buffer_view = DeviceBuffer::AsBufferView(
426 context->GetResourceAllocator()->CreateBufferWithCopy(
427 *decompressed_image.GetAllocation()));
428 blit_pass->AddCopy(buffer_view, texture);
429 if (enable_mipmapping) {
430 blit_pass->SetLabel("Mipmap Blit Pass");
431 blit_pass->GenerateMipmap(texture);
432 }
433 blit_pass->EncodeCommands();
434 if (!context->GetCommandQueue()->Submit({command_buffer}).ok()) {
435 FML_DLOG(ERROR) << "Failed to submit blit pass command buffer.";
436 return nullptr;
437 }
438 return texture;
439}
440
441std::shared_ptr<Texture> Playground::CreateTextureForMapping(
442 const std::shared_ptr<Context>& context,
443 std::shared_ptr<fml::Mapping> mapping,
444 bool enable_mipmapping) {
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}
453
454std::shared_ptr<Texture> Playground::CreateTextureForFixture(
455 const char* fixture_name,
456 bool enable_mipmapping) const {
458 context_, OpenAssetAsMapping(fixture_name), enable_mipmapping);
459 if (texture == nullptr) {
460 return nullptr;
461 }
462 texture->SetLabel(fixture_name);
463 return texture;
464}
465
467 std::array<const char*, 6> fixture_names) const {
468 std::array<DecompressedImage, 6> images;
469 for (size_t i = 0; i < fixture_names.size(); i++) {
470 auto image = DecodeImageRGBA(
472 if (!image.has_value()) {
473 return nullptr;
474 }
475 images[i] = image.value();
476 }
477
478 TextureDescriptor texture_descriptor;
479 texture_descriptor.storage_mode = StorageMode::kDevicePrivate;
480 texture_descriptor.type = TextureType::kTextureCube;
481 texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
482 texture_descriptor.size = images[0].GetSize();
483 texture_descriptor.mip_count = 1u;
484
485 auto texture =
486 context_->GetResourceAllocator()->CreateTexture(texture_descriptor);
487 if (!texture) {
488 VALIDATION_LOG << "Could not allocate texture cube.";
489 return nullptr;
490 }
491 texture->SetLabel("Texture cube");
492
493 auto cmd_buffer = context_->CreateCommandBuffer();
494 auto blit_pass = cmd_buffer->CreateBlitPass();
495 for (size_t i = 0; i < fixture_names.size(); i++) {
496 auto device_buffer = context_->GetResourceAllocator()->CreateBufferWithCopy(
497 *images[i].GetAllocation());
498 blit_pass->AddCopy(DeviceBuffer::AsBufferView(device_buffer), texture, {},
499 "", /*mip_level=*/0, /*slice=*/i);
500 }
501
502 if (!blit_pass->EncodeCommands() ||
503 !context_->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok()) {
504 VALIDATION_LOG << "Could not upload texture to device memory.";
505 return nullptr;
506 }
507
508 return texture;
509}
510
512 window_size_ = size;
513}
514
516 return true;
517}
518
520 const std::shared_ptr<Capabilities>& capabilities) {
521 return impl_->SetCapabilities(capabilities);
522}
523
527
529 const {
530 return impl_->CreateGLProcAddressResolver();
531}
532
534 const {
535 return impl_->CreateVKProcAddressResolver();
536}
537
538void Playground::SetGPUDisabled(bool value) const {
539 impl_->SetGPUDisabled(value);
540}
541
543 return impl_->GetRuntimeStageBackend();
544}
545
546} // namespace impeller
Wraps a closure that is invoked in the destructor unless released by the caller.
Definition closure.h:32
constexpr TimeDelta ToEpochDelta() const
Definition time_point.h:52
static TimePoint Now()
Definition time_point.cc:49
static std::shared_ptr< CompressedImage > Create(std::shared_ptr< const fml::Mapping > allocation)
const std::shared_ptr< const fml::Mapping > & GetAllocation() const
const ISize & GetSize() const
static BufferView AsBufferView(std::shared_ptr< DeviceBuffer > buffer)
Create a buffer view of this entire buffer.
static std::shared_ptr< HostBuffer > Create(const std::shared_ptr< Allocator > &allocator, const std::shared_ptr< const IdleWaiter > &idle_waiter, size_t minimum_uniform_alignment)
Playground(PlaygroundSwitches switches)
Definition playground.cc:87
bool OpenPlaygroundHere(const RenderCallback &render_callback)
std::shared_ptr< Context > MakeContext() const
Definition playground.cc:98
bool IsPlaygroundEnabled() const
virtual bool ShouldKeepRendering() const
static bool ShouldOpenNewPlaygrounds()
Point GetCursorPosition() const
void SetWindowSize(ISize size)
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed(std::shared_ptr< fml::Mapping > mapping)
ISize GetWindowSize() const
std::function< bool(RenderPass &pass)> SinglePassCallback
Definition playground.h:37
void SetupContext(PlaygroundBackend backend, const PlaygroundSwitches &switches)
GLProcAddressResolver CreateGLProcAddressResolver() const
bool WillRenderSomething() const
Returns true if OpenPlaygroundHere will actually render anything.
RuntimeStageBackend GetRuntimeStageBackend() const
virtual std::string GetWindowTitle() const =0
std::function< bool(RenderTarget &render_target)> RenderCallback
Definition playground.h:70
void SetGPUDisabled(bool disabled) const
Mark the GPU as unavilable.
const PlaygroundSwitches switches_
Definition playground.h:122
std::shared_ptr< Context > GetContext() const
Definition playground.cc:94
static bool SupportsBackend(PlaygroundBackend backend)
static std::shared_ptr< Texture > CreateTextureForMapping(const std::shared_ptr< Context > &context, std::shared_ptr< fml::Mapping > mapping, bool enable_mipmapping=false)
IRect GetWindowBounds() const
virtual std::unique_ptr< fml::Mapping > OpenAssetAsMapping(std::string asset_name) const =0
Point GetContentScale() const
std::shared_ptr< Texture > CreateTextureForFixture(const char *fixture_name, bool enable_mipmapping=false) const
Scalar GetSecondsElapsed() const
Get the amount of time elapsed from the start of the playground's execution.
std::function< void *(void *instance, const char *proc_name)> VKProcAddressResolver
Definition playground.h:111
std::function< void *(const char *proc_name)> GLProcAddressResolver
Definition playground.h:107
static std::optional< DecompressedImage > DecodeImageRGBA(const std::shared_ptr< CompressedImage > &compressed)
std::shared_ptr< Texture > CreateTextureCubeForFixture(std::array< const char *, 6 > fixture_names) const
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities)
VKProcAddressResolver CreateVKProcAddressResolver() const
static std::unique_ptr< PlaygroundImpl > Create(PlaygroundBackend backend, PlaygroundSwitches switches)
ColorAttachment GetColorAttachment(size_t index) const
Get the color attachment at [index].
RenderTarget & SetColorAttachment(const ColorAttachment &attachment, size_t index)
RenderTarget & SetDepthAttachment(std::optional< DepthAttachment > attachment)
RenderTarget & SetStencilAttachment(std::optional< StencilAttachment > attachment)
int32_t value
int32_t x
FlutterVulkanImage * image
GLFWwindow * window
Definition main.cc:60
VkSurfaceKHR surface
Definition main.cc:65
#define GLFW_TRUE
#define FML_DLOG(severity)
Definition logging.h:121
#define FML_LOG(severity)
Definition logging.h:101
#define FML_CHECK(condition)
Definition logging.h:104
#define FML_UNREACHABLE()
Definition logging.h:128
void ImGui_ImplImpeller_RenderDrawData(ImDrawData *draw_data, impeller::RenderPass &render_pass, impeller::HostBuffer &host_buffer)
bool ImGui_ImplImpeller_Init(const std::shared_ptr< impeller::Context > &context)
void ImGui_ImplImpeller_Shutdown()
FlTexture * texture
std::array< MockImage, 3 > images
double y
void SetupSwiftshaderOnce(bool use_swiftshader)
Find and setup the installable client driver for a locally built SwiftShader at known paths....
static std::shared_ptr< Texture > CreateTextureForDecompressedImage(const std::shared_ptr< Context > &context, DecompressedImage &decompressed_image, bool enable_mipmapping)
std::string PlaygroundBackendToString(PlaygroundBackend backend)
Definition playground.cc:47
float Scalar
Definition scalar.h:19
static void PlaygroundKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
static void InitializeGLFWOnce()
Definition playground.cc:61
static std::atomic_bool gShouldOpenNewPlaygrounds
PlaygroundBackend
Definition playground.h:26
int32_t height
int32_t width
LoadAction load_action
Definition formats.h:663
static constexpr TRect MakeSize(const TSize< U > &size)
Definition rect.h:150
constexpr TSize Max(const TSize &o) const
Definition size.h:97
constexpr size_t MipCount() const
Return the mip count of the texture.
Definition size.h:137
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
#define VALIDATION_LOG
Definition validation.h:91