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
80 static std::once_flag sOnceInitializer;
81 std::call_once(sOnceInitializer, []() {
82 ::glfwSetErrorCallback([](
int code,
const char* description) {
83 FML_LOG(ERROR) <<
"GLFW Error '" << description <<
"' (" << code <<
").";
110#if IMPELLER_ENABLE_METAL
117#if IMPELLER_ENABLE_OPENGLES
123#if IMPELLER_ENABLE_VULKAN
138 FML_LOG(WARNING) <<
"PlaygroundImpl::Create failed.";
142 context_ = impl_->GetContext();
147 FML_LOG(WARNING) <<
"Asked to set up a window with no context (call "
148 "SetupContext first).";
160 host_buffer_.reset();
163 context_->Shutdown();
180 if ((
key == GLFW_KEY_ESCAPE) &&
action == GLFW_RELEASE) {
181 if (mods & (GLFW_MOD_CONTROL | GLFW_MOD_SUPER | GLFW_MOD_SHIFT)) {
189 return cursor_position_;
201 return impl_->GetContentScale();
208void Playground::SetCursorPosition(
Point pos) {
209 cursor_position_ = pos;
218 if (!render_callback) {
222 IMGUI_CHECKVERSION();
223 ImGui::CreateContext();
225 []() { ImGui::DestroyContext(); });
226 ImGui::StyleColorsDark();
228 auto& io = ImGui::GetIO();
229 io.IniFilename =
nullptr;
230 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
231 io.ConfigWindowsResizeFromEdges =
true;
233 auto window =
reinterpret_cast<GLFWwindow*
>(impl_->GetWindowHandle());
238 ::glfwSetWindowUserPointer(
window,
this);
239 ::glfwSetWindowSizeCallback(
249 ::glfwSetCursorPosCallback(
window, [](GLFWwindow*
window,
double x,
252 ->SetCursorPosition({
static_cast<Scalar>(
x),
static_cast<Scalar>(
y)});
255 ImGui_ImplGlfw_InitForOther(
window,
true);
262 ImGui::SetNextWindowPos({10, 10});
265 ::glfwSetWindowPos(
window, 200, 100);
274 if (::glfwWindowShouldClose(
window)) {
278 ImGui_ImplGlfw_NewFrame();
280 auto surface = impl_->AcquireSurfaceFrame(context_);
284 ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport(),
285 ImGuiDockNodeFlags_PassthruCentralNode);
286 bool result = render_callback(render_target);
291 auto buffer = context_->CreateCommandBuffer();
296 buffer->SetLabel(
"ImGui Command Buffer");
300 if (color0.resolve_texture) {
301 color0.texture = color0.resolve_texture;
302 color0.resolve_texture =
nullptr;
309 auto pass = buffer->CreateRenderPass(render_target);
314 pass->SetLabel(
"ImGui Render Pass");
317 context_->GetResourceAllocator(), context_->GetIdleWaiter(),
318 context_->GetCapabilities()->GetMinimumUniformAlignment());
324 pass->EncodeCommands();
326 if (!context_->GetCommandQueue()->Submit({buffer}).ok()) {
331 if (!result || !
surface->Present()) {
348 auto buffer =
context->CreateCommandBuffer();
352 buffer->SetLabel(
"Playground Command Buffer");
354 auto pass = buffer->CreateRenderPass(render_target);
358 pass->SetLabel(
"Playground Render Pass");
360 if (!pass_callback(*pass)) {
364 pass->EncodeCommands();
365 if (!
context->GetCommandQueue()->Submit({buffer}).ok()) {
373 std::shared_ptr<fml::Mapping> mapping) {
375 if (!compressed_image) {
380 return compressed_image;
384 const std::shared_ptr<CompressedImage>& compressed) {
385 if (compressed ==
nullptr) {
393 auto image = compressed->Decode().ConvertToRGBA();
394 if (!
image.IsValid()) {
403 const std::shared_ptr<Context>&
context,
405 bool enable_mipmapping) {
409 texture_descriptor.
size = decompressed_image.
GetSize();
414 context->GetResourceAllocator()->CreateTexture(texture_descriptor);
422 FML_DLOG(ERROR) <<
"Could not create command buffer for mipmap generation.";
429 context->GetResourceAllocator()->CreateBufferWithCopy(
431 blit_pass->AddCopy(buffer_view,
texture);
432 if (enable_mipmapping) {
433 blit_pass->SetLabel(
"Mipmap Blit Pass");
434 blit_pass->GenerateMipmap(
texture);
436 blit_pass->EncodeCommands();
437 if (!
context->GetCommandQueue()->Submit({command_buffer}).ok()) {
438 FML_DLOG(ERROR) <<
"Failed to submit blit pass command buffer.";
445 const std::shared_ptr<Context>&
context,
446 std::shared_ptr<fml::Mapping> mapping,
447 bool enable_mipmapping) {
450 if (!
image.has_value()) {
458 const char* fixture_name,
459 bool enable_mipmapping)
const {
465 texture->SetLabel(fixture_name);
470 std::array<const char*, 6> fixture_names)
const {
471 std::array<DecompressedImage, 6>
images;
472 for (
size_t i = 0;
i < fixture_names.size();
i++) {
475 if (!
image.has_value()) {
485 texture_descriptor.
size =
images[0].GetSize();
489 context_->GetResourceAllocator()->CreateTexture(texture_descriptor);
494 texture->SetLabel(
"Texture cube");
496 auto cmd_buffer = context_->CreateCommandBuffer();
497 auto blit_pass = cmd_buffer->CreateBlitPass();
498 for (
size_t i = 0;
i < fixture_names.size();
i++) {
499 auto device_buffer = context_->GetResourceAllocator()->CreateBufferWithCopy(
505 if (!blit_pass->EncodeCommands() ||
506 !context_->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok()) {
523 const std::shared_ptr<Capabilities>& capabilities) {
524 return impl_->SetCapabilities(capabilities);
533 return impl_->CreateGLProcAddressResolver();
538 return impl_->CreateVKProcAddressResolver();
542 impl_->SetGPUDisabled(
value);
546 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
std::shared_ptr< ContextGLES > context
std::shared_ptr< CommandBuffer > command_buffer
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...