19#define GLFW_INCLUDE_NONE
20#include "third_party/glfw/include/GLFW/glfw3.h"
34#include "third_party/imgui/backends/imgui_impl_glfw.h"
35#include "third_party/imgui/imgui.h"
41#if IMPELLER_ENABLE_VULKAN
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 <<
").";
108#if IMPELLER_ENABLE_METAL
114#if IMPELLER_ENABLE_OPENGLES
120#if IMPELLER_ENABLE_VULKAN
135 FML_LOG(WARNING) <<
"PlaygroundImpl::Create failed.";
139 context_ = impl_->GetContext();
144 FML_LOG(WARNING) <<
"Asked to set up a window with no context (call "
145 "SetupContext first).";
157 host_buffer_.reset();
160 context_->Shutdown();
177 if ((
key == GLFW_KEY_ESCAPE) &&
action == GLFW_RELEASE) {
178 if (mods & (GLFW_MOD_CONTROL | GLFW_MOD_SUPER | GLFW_MOD_SHIFT)) {
186 return cursor_position_;
198 return impl_->GetContentScale();
205void Playground::SetCursorPosition(
Point pos) {
206 cursor_position_ = pos;
215 if (!render_callback) {
219 IMGUI_CHECKVERSION();
220 ImGui::CreateContext();
222 []() { ImGui::DestroyContext(); });
223 ImGui::StyleColorsDark();
225 auto& io = ImGui::GetIO();
226 io.IniFilename =
nullptr;
227 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
228 io.ConfigWindowsResizeFromEdges =
true;
230 auto window =
reinterpret_cast<GLFWwindow*
>(impl_->GetWindowHandle());
235 ::glfwSetWindowUserPointer(
window,
this);
236 ::glfwSetWindowSizeCallback(
246 ::glfwSetCursorPosCallback(
window, [](GLFWwindow*
window,
double x,
249 ->SetCursorPosition({
static_cast<Scalar>(
x),
static_cast<Scalar>(
y)});
252 ImGui_ImplGlfw_InitForOther(
window,
true);
259 ImGui::SetNextWindowPos({10, 10});
262 ::glfwSetWindowPos(
window, 200, 100);
271 if (::glfwWindowShouldClose(
window)) {
275 ImGui_ImplGlfw_NewFrame();
277 auto surface = impl_->AcquireSurfaceFrame(context_);
281 ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport(),
282 ImGuiDockNodeFlags_PassthruCentralNode);
283 bool result = render_callback(render_target);
288 auto buffer = context_->CreateCommandBuffer();
293 buffer->SetLabel(
"ImGui Command Buffer");
297 if (color0.resolve_texture) {
298 color0.texture = color0.resolve_texture;
299 color0.resolve_texture =
nullptr;
306 auto pass = buffer->CreateRenderPass(render_target);
311 pass->SetLabel(
"ImGui Render Pass");
314 context_->GetResourceAllocator(), context_->GetIdleWaiter(),
315 context_->GetCapabilities()->GetMinimumUniformAlignment());
321 pass->EncodeCommands();
323 if (!context_->GetCommandQueue()->Submit({buffer}).ok()) {
328 if (!result || !
surface->Present()) {
345 auto buffer = context->CreateCommandBuffer();
349 buffer->SetLabel(
"Playground Command Buffer");
351 auto pass = buffer->CreateRenderPass(render_target);
355 pass->SetLabel(
"Playground Render Pass");
357 if (!pass_callback(*pass)) {
361 pass->EncodeCommands();
362 if (!context->GetCommandQueue()->Submit({buffer}).ok()) {
370 std::shared_ptr<fml::Mapping> mapping) {
372 if (!compressed_image) {
377 return compressed_image;
381 const std::shared_ptr<CompressedImage>& compressed) {
382 if (compressed ==
nullptr) {
390 auto image = compressed->Decode().ConvertToRGBA();
391 if (!
image.IsValid()) {
400 const std::shared_ptr<Context>& context,
402 bool enable_mipmapping) {
406 texture_descriptor.
size = decompressed_image.
GetSize();
411 context->GetResourceAllocator()->CreateTexture(texture_descriptor);
417 auto command_buffer = context->CreateCommandBuffer();
418 if (!command_buffer) {
419 FML_DLOG(ERROR) <<
"Could not create command buffer for mipmap generation.";
422 command_buffer->SetLabel(
"Mipmap Command Buffer");
424 auto blit_pass = command_buffer->CreateBlitPass();
426 context->GetResourceAllocator()->CreateBufferWithCopy(
428 blit_pass->AddCopy(buffer_view,
texture);
429 if (enable_mipmapping) {
430 blit_pass->SetLabel(
"Mipmap Blit Pass");
431 blit_pass->GenerateMipmap(
texture);
433 blit_pass->EncodeCommands();
434 if (!context->GetCommandQueue()->Submit({command_buffer}).ok()) {
435 FML_DLOG(ERROR) <<
"Failed to submit blit pass command buffer.";
442 const std::shared_ptr<Context>& context,
443 std::shared_ptr<fml::Mapping> mapping,
444 bool enable_mipmapping) {
447 if (!
image.has_value()) {
455 const char* fixture_name,
456 bool enable_mipmapping)
const {
462 texture->SetLabel(fixture_name);
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++) {
472 if (!
image.has_value()) {
482 texture_descriptor.
size =
images[0].GetSize();
486 context_->GetResourceAllocator()->CreateTexture(texture_descriptor);
491 texture->SetLabel(
"Texture cube");
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(
502 if (!blit_pass->EncodeCommands() ||
503 !context_->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok()) {
520 const std::shared_ptr<Capabilities>& capabilities) {
521 return impl_->SetCapabilities(capabilities);
530 return impl_->CreateGLProcAddressResolver();
535 return impl_->CreateVKProcAddressResolver();
539 impl_->SetGPUDisabled(
value);
543 return impl_->GetRuntimeStageBackend();
Wraps a closure that is invoked in the destructor unless released by the caller.
constexpr TimeDelta ToEpochDelta() const
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)
bool OpenPlaygroundHere(const RenderCallback &render_callback)
std::shared_ptr< Context > MakeContext() const
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
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
void SetGPUDisabled(bool disabled) const
Mark the GPU as unavilable.
const PlaygroundSwitches switches_
std::shared_ptr< Context > GetContext() const
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
std::function< void *(const char *proc_name)> GLProcAddressResolver
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)
static bool IsVulkanDriverPresent()
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)
FlutterVulkanImage * image
#define FML_DLOG(severity)
#define FML_LOG(severity)
#define FML_CHECK(condition)
#define FML_UNREACHABLE()
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()
std::array< MockImage, 3 > images
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)
static void PlaygroundKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
static void InitializeGLFWOnce()
static std::atomic_bool gShouldOpenNewPlaygrounds
static constexpr TRect MakeSize(const TSize< U > &size)
constexpr TSize Max(const TSize &o) const
constexpr size_t MipCount() const
Return the mip count of the texture.
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...