| 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
5 #include "impeller/playground/backend/metal/playground_impl_mtl.h"
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/playground/imgui/mtl/imgui_shaders.h"
22 #include "impeller/renderer/backend/metal/context_mtl.h"
23 #include "impeller/renderer/backend/metal/formats_mtl.h"
24 #include "impeller/renderer/backend/metal/surface_mtl.h"
25 #include "impeller/renderer/backend/metal/texture_mtl.h"
26 #include "impeller/renderer/mtl/compute_shaders.h"
27 #include "impeller/scene/shaders/mtl/scene_shaders.h"
28
29 namespace impeller {
30
31 struct PlaygroundImplMTL::Data {
32 CAMetalLayer* metal_layer = nil;
33 };
34
35 static std::vector<std::shared_ptr<fml::Mapping>>
36 ShaderLibraryMappingsForPlayground() {
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>(impeller_imgui_shaders_data,
47 impeller_imgui_shaders_length),
48 std::make_shared<fml::NonOwnedMapping>(impeller_scene_shaders_data,
49 impeller_scene_shaders_length),
50 std::make_shared<fml::NonOwnedMapping>(
51 impeller_compute_shaders_data, impeller_compute_shaders_length)
52
53 };
54 }
55
56 void PlaygroundImplMTL::DestroyWindowHandle(WindowHandle handle) {
57 if (!handle) {
58 return;
59 }
60 ::glfwDestroyWindow(reinterpret_cast<GLFWwindow*>(handle));
61 }
62
63 PlaygroundImplMTL::PlaygroundImplMTL(PlaygroundSwitches switches)
64 : PlaygroundImpl(switches),
65 handle_(nullptr, &DestroyWindowHandle),
66 data_(std::make_unique<Data>()),
67 concurrent_loop_(fml::ConcurrentMessageLoop::Create()),
68 is_gpu_disabled_sync_switch_(new fml::SyncSwitch(false)) {
69 ::glfwDefaultWindowHints();
70 ::glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
71 ::glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
72 auto window = ::glfwCreateWindow(1, 1, "Test", nullptr, nullptr);
73 if (!window) {
74 return;
75 }
76 auto context =
77 ContextMTL::Create(ShaderLibraryMappingsForPlayground(),
78 is_gpu_disabled_sync_switch_, "Playground Library");
79 if (!context) {
80 return;
81 }
82 NSWindow* cocoa_window = ::glfwGetCocoaWindow(window);
83 if (cocoa_window == nil) {
84 return;
85 }
86 data_->metal_layer = [CAMetalLayer layer];
87 data_->metal_layer.device = ContextMTL::Cast(*context).GetMTLDevice();
88 data_->metal_layer.pixelFormat =
89 ToMTLPixelFormat(context->GetCapabilities()->GetDefaultColorFormat());
90 data_->metal_layer.framebufferOnly = NO;
91 cocoa_window.contentView.layer = data_->metal_layer;
92 cocoa_window.contentView.wantsLayer = YES;
93
94 handle_.reset(window);
95 context_ = std::move(context);
96 }
97
98 PlaygroundImplMTL::~PlaygroundImplMTL() = default;
99
100 std::shared_ptr<Context> PlaygroundImplMTL::GetContext() const {
101 return context_;
102 }
103
104 // |PlaygroundImpl|
105 PlaygroundImpl::WindowHandle PlaygroundImplMTL::GetWindowHandle() const {
106 return handle_.get();
107 }
108
109 // |PlaygroundImpl|
110 std::unique_ptr<Surface> PlaygroundImplMTL::AcquireSurfaceFrame(
111 std::shared_ptr<Context> context) {
112 if (!data_->metal_layer) {
113 return nullptr;
114 }
115
116 const auto layer_size = data_->metal_layer.bounds.size;
117 const auto scale = GetContentScale();
118 data_->metal_layer.drawableSize =
119 CGSizeMake(layer_size.width * scale.x, layer_size.height * scale.y);
120
121 auto drawable =
122 SurfaceMTL::GetMetalDrawableAndValidate(context, data_->metal_layer);
123 return SurfaceMTL::MakeFromMetalLayerDrawable(context, drawable);
124 }
125
126 } // namespace impeller
context_mtl.h
formats_mtl.h
impeller::PlaygroundImplMTL::~PlaygroundImplMTL
~PlaygroundImplMTL()
impeller::PlaygroundImplMTL::Data
Definition: playground_impl_mtl.mm:31
impeller::PlaygroundImpl
Definition: playground_impl.h:17
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:229
impeller::PlaygroundImpl::GetContentScale
Vector2 GetContentScale() const
Definition: playground_impl.cc:53
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:119
impeller::ShaderLibraryMappingsForPlayground
static std::vector< std::shared_ptr< fml::Mapping > > ShaderLibraryMappingsForPlayground()
Definition: playground_impl_gles.cc:89
playground_impl_mtl.h
surface_mtl.h
impeller::ToMTLPixelFormat
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format)
Definition: formats_mtl.h:75
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:356
impeller::PlaygroundSwitches
Definition: switches.h:15
std
Definition: comparable.h:98
impeller::PlaygroundImplMTL::PlaygroundImplMTL
PlaygroundImplMTL(PlaygroundSwitches switches)
Definition: playground_impl_mtl.mm:63
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:21
texture_mtl.h
impeller::BackendCast< ContextMTL, Context >::Cast
static ContextMTL & Cast(Context &base)
Definition: backend_cast.h:14
impeller
Definition: aiks_context.cc:10
impeller::PlaygroundImpl::WindowHandle
void * WindowHandle
Definition: playground_impl.h:24