10 #include "fml/closure.h"
11 #include "fml/time/time_point.h"
19 #define GLFW_INCLUDE_NONE
20 #include "third_party/glfw/include/GLFW/glfw3.h"
22 #include "flutter/fml/paths.h"
33 #include "third_party/imgui/backends/imgui_impl_glfw.h"
34 #include "third_party/imgui/imgui.h"
37 #include "fml/platform/darwin/scoped_nsautorelease_pool.h"
40 #if IMPELLER_ENABLE_VULKAN
75 static std::once_flag sOnceInitializer;
76 std::call_once(sOnceInitializer, []() {
77 ::glfwSetErrorCallback([](
int code,
const char* description) {
78 FML_LOG(ERROR) <<
"GLFW Error '" << description <<
"' (" << code <<
").";
80 FML_CHECK(::glfwInit() == GLFW_TRUE);
104 #if IMPELLER_ENABLE_METAL
110 #if IMPELLER_ENABLE_OPENGLES
116 #if IMPELLER_ENABLE_VULKAN
131 FML_LOG(WARNING) <<
"PlaygroundImpl::Create failed.";
135 context_ = impl_->GetContext();
140 FML_LOG(WARNING) <<
"Asked to set up a window with no context (call "
141 "SetupContext first).";
144 start_time_ = fml::TimePoint::Now().ToEpochDelta();
153 host_buffer_.reset();
156 context_->Shutdown();
173 if ((key == GLFW_KEY_ESCAPE) && action == GLFW_RELEASE) {
174 if (mods & (GLFW_MOD_CONTROL | GLFW_MOD_SUPER | GLFW_MOD_SHIFT)) {
177 ::glfwSetWindowShouldClose(window, GLFW_TRUE);
182 return cursor_position_;
190 return impl_->GetContentScale();
194 return (fml::TimePoint::Now().ToEpochDelta() - start_time_).ToSecondsF();
197 void Playground::SetCursorPosition(
Point pos) {
198 cursor_position_ = pos;
207 if (!render_callback) {
211 IMGUI_CHECKVERSION();
213 fml::ScopedCleanupClosure destroy_imgui_context(
214 []() { ImGui::DestroyContext(); });
215 ImGui::StyleColorsDark();
217 auto& io = ImGui::GetIO();
218 io.IniFilename =
nullptr;
219 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
220 io.ConfigWindowsResizeFromEdges =
true;
222 auto window =
reinterpret_cast<GLFWwindow*
>(impl_->GetWindowHandle());
227 ::glfwSetWindowUserPointer(window,
this);
228 ::glfwSetWindowSizeCallback(
229 window, [](GLFWwindow* window,
int width,
int height) ->
void {
231 reinterpret_cast<Playground*
>(::glfwGetWindowUserPointer(window));
238 ::glfwSetCursorPosCallback(window, [](GLFWwindow* window,
double x,
240 reinterpret_cast<Playground*
>(::glfwGetWindowUserPointer(window))
241 ->SetCursorPosition({
static_cast<Scalar>(
x),
static_cast<Scalar>(y)});
244 ImGui_ImplGlfw_InitForOther(window,
true);
245 fml::ScopedCleanupClosure shutdown_imgui([]() { ImGui_ImplGlfw_Shutdown(); });
248 fml::ScopedCleanupClosure shutdown_imgui_impeller(
251 ImGui::SetNextWindowPos({10, 10});
254 ::glfwSetWindowPos(window, 200, 100);
255 ::glfwShowWindow(window);
259 fml::ScopedNSAutoreleasePool pool;
263 if (::glfwWindowShouldClose(window)) {
267 ImGui_ImplGlfw_NewFrame();
269 auto surface = impl_->AcquireSurfaceFrame(context_);
270 RenderTarget render_target = surface->GetRenderTarget();
273 ImGui::DockSpaceOverViewport(ImGui::GetMainViewport(),
274 ImGuiDockNodeFlags_PassthruCentralNode);
275 bool result = render_callback(render_target);
280 auto buffer = context_->CreateCommandBuffer();
285 buffer->SetLabel(
"ImGui Command Buffer");
289 if (color0.resolve_texture) {
290 color0.texture = color0.resolve_texture;
291 color0.resolve_texture =
nullptr;
298 auto pass = buffer->CreateRenderPass(render_target);
303 pass->SetLabel(
"ImGui Render Pass");
306 context_->GetIdleWaiter());
312 pass->EncodeCommands();
314 if (!context_->GetCommandQueue()->Submit({buffer}).ok()) {
319 if (!result || !surface->Present()) {
328 ::glfwHideWindow(window);
336 auto buffer = context->CreateCommandBuffer();
340 buffer->SetLabel(
"Playground Command Buffer");
342 auto pass = buffer->CreateRenderPass(render_target);
346 pass->SetLabel(
"Playground Render Pass");
348 if (!pass_callback(*pass)) {
352 pass->EncodeCommands();
353 if (!context->GetCommandQueue()->Submit({buffer}).ok()) {
361 std::shared_ptr<fml::Mapping> mapping) {
363 if (!compressed_image) {
368 return compressed_image;
372 const std::shared_ptr<CompressedImage>& compressed) {
373 if (compressed ==
nullptr) {
381 auto image = compressed->Decode().ConvertToRGBA();
382 if (!image.IsValid()) {
391 const std::shared_ptr<Context>& context,
393 bool enable_mipmapping) {
397 texture_descriptor.
size = decompressed_image.
GetSize();
402 context->GetResourceAllocator()->CreateTexture(texture_descriptor);
408 auto command_buffer = context->CreateCommandBuffer();
409 if (!command_buffer) {
410 FML_DLOG(ERROR) <<
"Could not create command buffer for mipmap generation.";
413 command_buffer->SetLabel(
"Mipmap Command Buffer");
415 auto blit_pass = command_buffer->CreateBlitPass();
417 context->GetResourceAllocator()->CreateBufferWithCopy(
420 if (enable_mipmapping) {
421 blit_pass->SetLabel(
"Mipmap Blit Pass");
422 blit_pass->GenerateMipmap(texture);
424 blit_pass->EncodeCommands();
425 if (!context->GetCommandQueue()->Submit({command_buffer}).ok()) {
426 FML_DLOG(ERROR) <<
"Failed to submit blit pass command buffer.";
433 const std::shared_ptr<Context>& context,
434 std::shared_ptr<fml::Mapping> mapping,
435 bool enable_mipmapping) {
438 if (!image.has_value()) {
446 const char* fixture_name,
447 bool enable_mipmapping)
const {
450 if (texture ==
nullptr) {
453 texture->SetLabel(fixture_name);
458 std::array<const char*, 6> fixture_names)
const {
459 std::array<DecompressedImage, 6> images;
460 for (
size_t i = 0; i < fixture_names.size(); i++) {
463 if (!image.has_value()) {
466 images[i] = image.value();
473 texture_descriptor.
size = images[0].GetSize();
477 context_->GetResourceAllocator()->CreateTexture(texture_descriptor);
482 texture->SetLabel(
"Texture cube");
484 auto cmd_buffer = context_->CreateCommandBuffer();
485 auto blit_pass = cmd_buffer->CreateBlitPass();
486 for (
size_t i = 0; i < fixture_names.size(); i++) {
487 auto device_buffer = context_->GetResourceAllocator()->CreateBufferWithCopy(
488 *images[i].GetAllocation());
493 if (!blit_pass->EncodeCommands() ||
494 !context_->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok()) {
511 const std::shared_ptr<Capabilities>& capabilities) {
512 return impl_->SetCapabilities(capabilities);
521 return impl_->CreateGLProcAddressResolver();
526 return impl_->CreateVKProcAddressResolver();
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)
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.
virtual std::string GetWindowTitle() const =0
virtual std::unique_ptr< fml::Mapping > OpenAssetAsMapping(std::string asset_name) const =0
std::function< bool(RenderTarget &render_target)> RenderCallback
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)
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)
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::shared_ptr< Context > CreateContext()
std::string PlaygroundBackendToString(PlaygroundBackend backend)
static void PlaygroundKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
static void InitializeGLFWOnce()
static std::shared_ptr< Texture > CreateTextureForDecompressedImage(const std::shared_ptr< Context > &context, DecompressedImage &decompressed_image, bool enable_mipmapping)
static std::atomic_bool gShouldOpenNewPlaygrounds
void SetupSwiftshaderOnce(bool use_swiftshader)
Find and setup the installable client driver for a locally built SwiftShader at known paths....
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...