8 #define GLFW_INCLUDE_NONE
9 #import "third_party/glfw/include/GLFW/glfw3.h"
11 #define GLFW_EXPOSE_NATIVE_COCOA
12 #import "third_party/glfw/include/GLFW/glfw3native.h"
14 #include <Metal/Metal.h>
15 #include <QuartzCore/QuartzCore.h>
17 #include "flutter/fml/mapping.h"
18 #include "impeller/entity/mtl/entity_shaders.h"
19 #include "impeller/entity/mtl/framebuffer_blend_shaders.h"
20 #include "impeller/entity/mtl/modern_shaders.h"
21 #include "impeller/fixtures/mtl/fixtures_shaders.h"
22 #include "impeller/fixtures/mtl/modern_fixtures_shaders.h"
23 #include "impeller/playground/imgui/mtl/imgui_shaders.h"
28 #include "impeller/renderer/mtl/compute_shaders.h"
36 static std::vector<std::shared_ptr<fml::Mapping>>
38 return {std::make_shared<fml::NonOwnedMapping>(
39 impeller_entity_shaders_data, impeller_entity_shaders_length),
40 std::make_shared<fml::NonOwnedMapping>(
41 impeller_modern_shaders_data, impeller_modern_shaders_length),
42 std::make_shared<fml::NonOwnedMapping>(
43 impeller_framebuffer_blend_shaders_data,
44 impeller_framebuffer_blend_shaders_length),
45 std::make_shared<fml::NonOwnedMapping>(
46 impeller_fixtures_shaders_data, impeller_fixtures_shaders_length),
47 std::make_shared<fml::NonOwnedMapping>(
48 impeller_modern_fixtures_shaders_data,
49 impeller_modern_fixtures_shaders_length),
50 std::make_shared<fml::NonOwnedMapping>(impeller_imgui_shaders_data,
51 impeller_imgui_shaders_length),
52 std::make_shared<fml::NonOwnedMapping>(
53 impeller_compute_shaders_data, impeller_compute_shaders_length)
58 void PlaygroundImplMTL::DestroyWindowHandle(WindowHandle handle) {
62 ::glfwDestroyWindow(
reinterpret_cast<GLFWwindow*
>(handle));
67 handle_(nullptr, &DestroyWindowHandle),
68 data_(
std::make_unique<
Data>()),
69 concurrent_loop_(
fml::ConcurrentMessageLoop::
Create()),
70 is_gpu_disabled_sync_switch_(new
fml::SyncSwitch(false)) {
71 ::glfwDefaultWindowHints();
72 ::glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
73 ::glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
74 auto window = ::glfwCreateWindow(1, 1,
"Test",
nullptr,
nullptr);
81 is_gpu_disabled_sync_switch_,
"Playground Library",
88 NSWindow* cocoa_window = ::glfwGetCocoaWindow(window);
89 if (cocoa_window == nil) {
92 data_->metal_layer = [CAMetalLayer layer];
94 data_->metal_layer.pixelFormat =
96 data_->metal_layer.framebufferOnly = NO;
97 cocoa_window.contentView.layer = data_->metal_layer;
98 cocoa_window.contentView.wantsLayer = YES;
100 handle_.reset(window);
101 context_ = std::move(context);
102 swapchain_transients_ = std::make_shared<SwapchainTransientsMTL>(
103 context_->GetResourceAllocator());
108 std::shared_ptr<Context> PlaygroundImplMTL::GetContext()
const {
114 return handle_.get();
118 std::unique_ptr<Surface> PlaygroundImplMTL::AcquireSurfaceFrame(
119 std::shared_ptr<Context> context) {
120 if (!data_->metal_layer) {
124 const auto layer_size = data_->metal_layer.bounds.size;
126 data_->metal_layer.drawableSize =
127 CGSizeMake(layer_size.width * scale.x, layer_size.height * scale.y);
132 swapchain_transients_);
136 const std::shared_ptr<Capabilities>& capabilities) {
137 context_->SetCapabilities(capabilities);
138 return fml::Status();
141 void PlaygroundImplMTL::SetGPUDisabled(
bool disabled)
const {
142 is_gpu_disabled_sync_switch_->SetSwitch(disabled);
static ContextMTL & Cast(Context &base)
static std::shared_ptr< ContextMTL > Create(const Flags &flags, const std::vector< std::string > &shader_library_paths, std::shared_ptr< const fml::SyncSwitch > is_gpu_disabled_sync_switch)
id< MTLDevice > GetMTLDevice() const
Vector2 GetContentScale() const
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities) override
PlaygroundImplMTL(PlaygroundSwitches switches)
static std::unique_ptr< SurfaceMTL > MakeFromMetalLayerDrawable(const std::shared_ptr< Context > &context, id< CAMetalDrawable > drawable, const std::shared_ptr< SwapchainTransientsMTL > &transients, std::optional< IRect > clip_rect=std::nullopt)
static id< CAMetalDrawable > GetMetalDrawableAndValidate(const std::shared_ptr< Context > &context, CAMetalLayer *layer)
Wraps the current drawable of the given Metal layer to create a surface Impeller can render to....
ScopedObject< Object > Create(CtorArgs &&... args)
static std::vector< std::shared_ptr< fml::Mapping > > ShaderLibraryMappingsForPlayground()
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format)
CAMetalLayer * metal_layer