Flutter Impeller
playground_impl_mtl.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #define GLFW_INCLUDE_NONE
8 #import "third_party/glfw/include/GLFW/glfw3.h"
9 
10 #define GLFW_EXPOSE_NATIVE_COCOA
11 #import "third_party/glfw/include/GLFW/glfw3native.h"
12 
13 #include <Metal/Metal.h>
14 #include <QuartzCore/QuartzCore.h>
15 
16 #include "flutter/fml/mapping.h"
17 #include "impeller/entity/mtl/entity_shaders.h"
18 #include "impeller/entity/mtl/framebuffer_blend_shaders.h"
19 #include "impeller/entity/mtl/modern_shaders.h"
20 #include "impeller/fixtures/mtl/fixtures_shaders.h"
21 #include "impeller/fixtures/mtl/modern_fixtures_shaders.h"
22 #include "impeller/playground/imgui/mtl/imgui_shaders.h"
27 #include "impeller/renderer/mtl/compute_shaders.h"
28 
29 namespace impeller {
30 
32  CAMetalLayer* metal_layer = nil;
33 };
34 
35 static std::vector<std::shared_ptr<fml::Mapping>>
37  return {std::make_shared<fml::NonOwnedMapping>(
38  impeller_entity_shaders_data, impeller_entity_shaders_length),
39  std::make_shared<fml::NonOwnedMapping>(
40  impeller_modern_shaders_data, impeller_modern_shaders_length),
41  std::make_shared<fml::NonOwnedMapping>(
42  impeller_framebuffer_blend_shaders_data,
43  impeller_framebuffer_blend_shaders_length),
44  std::make_shared<fml::NonOwnedMapping>(
45  impeller_fixtures_shaders_data, impeller_fixtures_shaders_length),
46  std::make_shared<fml::NonOwnedMapping>(
47  impeller_modern_fixtures_shaders_data,
48  impeller_modern_fixtures_shaders_length),
49  std::make_shared<fml::NonOwnedMapping>(impeller_imgui_shaders_data,
50  impeller_imgui_shaders_length),
51  std::make_shared<fml::NonOwnedMapping>(
52  impeller_compute_shaders_data, impeller_compute_shaders_length)
53 
54  };
55 }
56 
57 void PlaygroundImplMTL::DestroyWindowHandle(WindowHandle handle) {
58  if (!handle) {
59  return;
60  }
61  ::glfwDestroyWindow(reinterpret_cast<GLFWwindow*>(handle));
62 }
63 
65  : PlaygroundImpl(switches),
66  handle_(nullptr, &DestroyWindowHandle),
67  data_(std::make_unique<Data>()),
68  concurrent_loop_(fml::ConcurrentMessageLoop::Create()),
69  is_gpu_disabled_sync_switch_(new fml::SyncSwitch(false)) {
70  ::glfwDefaultWindowHints();
71  ::glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
72  ::glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
73  auto window = ::glfwCreateWindow(1, 1, "Test", nullptr, nullptr);
74  if (!window) {
75  return;
76  }
77 
78  auto context = ContextMTL::Create(
79  ShaderLibraryMappingsForPlayground(), is_gpu_disabled_sync_switch_,
80  "Playground Library",
81  switches.enable_wide_gamut
82  ? std::optional<PixelFormat>(PixelFormat::kB10G10R10A10XR)
83  : std::nullopt);
84  if (!context) {
85  return;
86  }
87  NSWindow* cocoa_window = ::glfwGetCocoaWindow(window);
88  if (cocoa_window == nil) {
89  return;
90  }
91  data_->metal_layer = [CAMetalLayer layer];
92  data_->metal_layer.device = ContextMTL::Cast(*context).GetMTLDevice();
93  data_->metal_layer.pixelFormat =
94  ToMTLPixelFormat(context->GetCapabilities()->GetDefaultColorFormat());
95  data_->metal_layer.framebufferOnly = NO;
96  cocoa_window.contentView.layer = data_->metal_layer;
97  cocoa_window.contentView.wantsLayer = YES;
98 
99  handle_.reset(window);
100  context_ = std::move(context);
101 }
102 
104 
105 std::shared_ptr<Context> PlaygroundImplMTL::GetContext() const {
106  return context_;
107 }
108 
109 // |PlaygroundImpl|
110 PlaygroundImpl::WindowHandle PlaygroundImplMTL::GetWindowHandle() const {
111  return handle_.get();
112 }
113 
114 // |PlaygroundImpl|
115 std::unique_ptr<Surface> PlaygroundImplMTL::AcquireSurfaceFrame(
116  std::shared_ptr<Context> context) {
117  if (!data_->metal_layer) {
118  return nullptr;
119  }
120 
121  const auto layer_size = data_->metal_layer.bounds.size;
122  const auto scale = GetContentScale();
123  data_->metal_layer.drawableSize =
124  CGSizeMake(layer_size.width * scale.x, layer_size.height * scale.y);
125 
126  auto drawable =
127  SurfaceMTL::GetMetalDrawableAndValidate(context, data_->metal_layer);
128  return SurfaceMTL::MakeFromMetalLayerDrawable(context, drawable);
129 }
130 
132  const std::shared_ptr<Capabilities>& capabilities) {
133  context_->SetCapabilities(capabilities);
134  return fml::Status();
135 }
136 
137 } // namespace impeller
impeller::PixelFormat::kB10G10R10A10XR
@ kB10G10R10A10XR
context_mtl.h
impeller::PlaygroundSwitches::enable_wide_gamut
bool enable_wide_gamut
Definition: switches.h:35
formats_mtl.h
impeller::PlaygroundImplMTL::~PlaygroundImplMTL
~PlaygroundImplMTL()
impeller::PlaygroundImplMTL::Data
Definition: playground_impl_mtl.mm:31
impeller::PlaygroundImpl
Definition: playground_impl.h:18
impeller::ContextMTL::Create
static std::shared_ptr< ContextMTL > Create(const std::vector< std::string > &shader_library_paths, std::shared_ptr< const fml::SyncSwitch > is_gpu_disabled_sync_switch)
Definition: context_mtl.mm:225
impeller::PlaygroundImplMTL::SetCapabilities
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities) override
Definition: playground_impl_mtl.mm:131
impeller::PlaygroundImpl::GetContentScale
Vector2 GetContentScale() const
Definition: playground_impl.cc:60
impeller::SurfaceMTL::MakeFromMetalLayerDrawable
static std::unique_ptr< SurfaceMTL > MakeFromMetalLayerDrawable(const std::shared_ptr< Context > &context, id< CAMetalDrawable > drawable, std::optional< IRect > clip_rect=std::nullopt)
Definition: surface_mtl.mm:123
impeller::ShaderLibraryMappingsForPlayground
static std::vector< std::shared_ptr< fml::Mapping > > ShaderLibraryMappingsForPlayground()
Definition: playground_impl_gles.cc:106
playground_impl_mtl.h
surface_mtl.h
impeller::ToMTLPixelFormat
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format)
Definition: formats_mtl.h:76
impeller::PlaygroundImplMTL::Data::metal_layer
CAMetalLayer * metal_layer
Definition: playground_impl_mtl.mm:32
impeller::ContextMTL::GetMTLDevice
id< MTLDevice > GetMTLDevice() const
Definition: context_mtl.mm:354
impeller::PlaygroundSwitches
Definition: switches.h:15
std
Definition: comparable.h:95
impeller::PlaygroundImplMTL::PlaygroundImplMTL
PlaygroundImplMTL(PlaygroundSwitches switches)
Definition: playground_impl_mtl.mm:64
impeller::SurfaceMTL::GetMetalDrawableAndValidate
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....
Definition: surface_mtl.mm:25
texture_mtl.h
impeller::BackendCast< ContextMTL, Context >::Cast
static ContextMTL & Cast(Context &base)
Definition: backend_cast.h:13
scale
const Scalar scale
Definition: stroke_path_geometry.cc:301
impeller::interop::Create
ScopedObject< Object > Create(CtorArgs &&... args)
Definition: object.h:160
impeller
Definition: allocation.cc:12
impeller::PlaygroundImpl::WindowHandle
void * WindowHandle
Definition: playground_impl.h:25