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_->GetResourceAllocator(), context_->GetIdleWaiter(),
307 context_->GetCapabilities()->GetMinimumUniformAlignment());
313 pass->EncodeCommands();
315 if (!context_->GetCommandQueue()->Submit({buffer}).ok()) {
320 if (!result || !surface->Present()) {
329 ::glfwHideWindow(window);
337 auto buffer = context->CreateCommandBuffer();
341 buffer->SetLabel(
"Playground Command Buffer");
343 auto pass = buffer->CreateRenderPass(render_target);
347 pass->SetLabel(
"Playground Render Pass");
349 if (!pass_callback(*pass)) {
353 pass->EncodeCommands();
354 if (!context->GetCommandQueue()->Submit({buffer}).ok()) {
362 std::shared_ptr<fml::Mapping> mapping) {
364 if (!compressed_image) {
369 return compressed_image;
373 const std::shared_ptr<CompressedImage>& compressed) {
374 if (compressed ==
nullptr) {
382 auto image = compressed->Decode().ConvertToRGBA();
383 if (!image.IsValid()) {
392 const std::shared_ptr<Context>& context,
394 bool enable_mipmapping) {
398 texture_descriptor.
size = decompressed_image.
GetSize();
403 context->GetResourceAllocator()->CreateTexture(texture_descriptor);
409 auto command_buffer = context->CreateCommandBuffer();
410 if (!command_buffer) {
411 FML_DLOG(ERROR) <<
"Could not create command buffer for mipmap generation.";
414 command_buffer->SetLabel(
"Mipmap Command Buffer");
416 auto blit_pass = command_buffer->CreateBlitPass();
418 context->GetResourceAllocator()->CreateBufferWithCopy(
421 if (enable_mipmapping) {
422 blit_pass->SetLabel(
"Mipmap Blit Pass");
423 blit_pass->GenerateMipmap(texture);
425 blit_pass->EncodeCommands();
426 if (!context->GetCommandQueue()->Submit({command_buffer}).ok()) {
427 FML_DLOG(ERROR) <<
"Failed to submit blit pass command buffer.";
434 const std::shared_ptr<Context>& context,
435 std::shared_ptr<fml::Mapping> mapping,
436 bool enable_mipmapping) {
439 if (!image.has_value()) {
447 const char* fixture_name,
448 bool enable_mipmapping)
const {
451 if (texture ==
nullptr) {
454 texture->SetLabel(fixture_name);
459 std::array<const char*, 6> fixture_names)
const {
460 std::array<DecompressedImage, 6> images;
461 for (
size_t i = 0; i < fixture_names.size(); i++) {
464 if (!image.has_value()) {
467 images[i] = image.value();
474 texture_descriptor.
size = images[0].GetSize();
478 context_->GetResourceAllocator()->CreateTexture(texture_descriptor);
483 texture->SetLabel(
"Texture cube");
485 auto cmd_buffer = context_->CreateCommandBuffer();
486 auto blit_pass = cmd_buffer->CreateBlitPass();
487 for (
size_t i = 0; i < fixture_names.size(); i++) {
488 auto device_buffer = context_->GetResourceAllocator()->CreateBufferWithCopy(
489 *images[i].GetAllocation());
494 if (!blit_pass->EncodeCommands() ||
495 !context_->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok()) {
512 const std::shared_ptr<Capabilities>& capabilities) {
513 return impl_->SetCapabilities(capabilities);
522 return impl_->CreateGLProcAddressResolver();
527 return impl_->CreateVKProcAddressResolver();
531 impl_->SetGPUDisabled(
value);
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.
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
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)
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...