Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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/playground/imgui/mtl/imgui_shaders.h"
26#include "impeller/renderer/mtl/compute_shaders.h"
27#include "impeller/scene/shaders/mtl/scene_shaders.h"
28
29namespace impeller {
30
32 CAMetalLayer* metal_layer = nil;
33};
34
35static 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>(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
56void PlaygroundImplMTL::DestroyWindowHandle(WindowHandle handle) {
57 if (!handle) {
58 return;
59 }
60 ::glfwDestroyWindow(reinterpret_cast<GLFWwindow*>(handle));
61}
62
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
77 auto context = ContextMTL::Create(
78 ShaderLibraryMappingsForPlayground(), is_gpu_disabled_sync_switch_,
79 "Playground Library",
80 switches.enable_wide_gamut
81 ? std::optional<PixelFormat>(PixelFormat::kB10G10R10A10XR)
82 : std::nullopt);
83 if (!context) {
84 return;
85 }
86 NSWindow* cocoa_window = ::glfwGetCocoaWindow(window);
87 if (cocoa_window == nil) {
88 return;
89 }
90 data_->metal_layer = [CAMetalLayer layer];
91 data_->metal_layer.device = ContextMTL::Cast(*context).GetMTLDevice();
92 data_->metal_layer.pixelFormat =
93 ToMTLPixelFormat(context->GetCapabilities()->GetDefaultColorFormat());
94 data_->metal_layer.framebufferOnly = NO;
95 cocoa_window.contentView.layer = data_->metal_layer;
96 cocoa_window.contentView.wantsLayer = YES;
97
98 handle_.reset(window);
99 context_ = std::move(context);
100}
101
103
104std::shared_ptr<Context> PlaygroundImplMTL::GetContext() const {
105 return context_;
106}
107
108// |PlaygroundImpl|
112
113// |PlaygroundImpl|
115 std::shared_ptr<Context> context) {
116 if (!data_->metal_layer) {
117 return nullptr;
118 }
119
120 const auto layer_size = data_->metal_layer.bounds.size;
121 const auto scale = GetContentScale();
122 data_->metal_layer.drawableSize =
123 CGSizeMake(layer_size.width * scale.x, layer_size.height * scale.y);
124
125 auto drawable =
126 SurfaceMTL::GetMetalDrawableAndValidate(context, data_->metal_layer);
127 return SurfaceMTL::MakeFromMetalLayerDrawable(context, drawable);
128}
129
131 const std::shared_ptr<Capabilities>& capabilities) {
132 context_->SetCapabilities(capabilities);
133 return fml::Status();
134}
135
136} // namespace impeller
static sk_sp< Effect > Create()
static ContextMTL & Cast(Context &base)
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)
id< MTLDevice > GetMTLDevice() const
WindowHandle GetWindowHandle() const override
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities) override
std::shared_ptr< Context > GetContext() const override
PlaygroundImplMTL(PlaygroundSwitches switches)
std::unique_ptr< Surface > AcquireSurfaceFrame(std::shared_ptr< Context > context) override
Vector2 GetContentScale() const
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....
static std::unique_ptr< SurfaceMTL > MakeFromMetalLayerDrawable(const std::shared_ptr< Context > &context, id< CAMetalDrawable > drawable, std::optional< IRect > clip_rect=std::nullopt)
GLFWwindow * window
Definition main.cc:45
#define GLFW_FALSE
static std::vector< std::shared_ptr< fml::Mapping > > ShaderLibraryMappingsForPlayground()
constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format)
Definition formats_mtl.h:77
Definition ref_ptr.h:256
fuchsia::ui::composition::ParentViewportWatcherHandle handle_
const Scalar scale