Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
tester_context_mtl_factory.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#define FML_USED_ON_EMBEDDER
6
8
9#include <Foundation/Foundation.h>
10#include <QuartzCore/QuartzCore.h>
11#include <deque>
12#include <optional>
13#include <type_traits>
14#include <vector>
15
16#include "flutter/fml/mapping.h"
21#include "impeller/entity/mtl/entity_shaders.h"
22#include "impeller/entity/mtl/framebuffer_blend_shaders.h"
23#include "impeller/entity/mtl/modern_shaders.h"
27
28namespace flutter {
29
30namespace {
31
32class TesterGPUSurfaceMetalDelegate : public GPUSurfaceMetalDelegate {
33 public:
34 explicit TesterGPUSurfaceMetalDelegate(id<MTLDevice> device)
35 : GPUSurfaceMetalDelegate(MTLRenderTargetType::kCAMetalLayer) {
36 layer_ = [[CAMetalLayer alloc] init];
37 layer_.device = device;
38 layer_.pixelFormat = MTLPixelFormatBGRA8Unorm;
39 }
40
41 ~TesterGPUSurfaceMetalDelegate() = default;
42
43 GPUCAMetalLayerHandle GetCAMetalLayer(const DlISize& frame_info) const override {
44 layer_.drawableSize = CGSizeMake(frame_info.width, frame_info.height);
45 return (__bridge GPUCAMetalLayerHandle)(layer_);
46 }
47
48 bool PresentDrawable(GrMTLHandle drawable) const override { return true; }
49
50 GPUMTLTextureInfo GetMTLTexture(const DlISize& frame_info) const override { return {}; }
51
52 bool PresentTexture(GPUMTLTextureInfo texture) const override { return true; }
53
54 bool AllowsDrawingWhenGpuDisabled() const override { return true; }
55
56 private:
57 CAMetalLayer* layer_ = nil;
58};
59
60std::vector<std::shared_ptr<fml::Mapping>> ShaderLibraryMappings() {
61 return {
62 std::make_shared<fml::NonOwnedMapping>(impeller_entity_shaders_data,
63 impeller_entity_shaders_length),
64 std::make_shared<fml::NonOwnedMapping>(impeller_modern_shaders_data,
65 impeller_modern_shaders_length),
66 std::make_shared<fml::NonOwnedMapping>(impeller_framebuffer_blend_shaders_data,
67 impeller_framebuffer_blend_shaders_length),
68 };
69}
70
71} // namespace
72
74 public:
75 TesterContextMTL() = default;
76
77 ~TesterContextMTL() override {
78 if (context_) {
79 context_->Shutdown();
80 }
81 }
82
83 bool Initialize() {
84 std::vector<std::shared_ptr<fml::Mapping>> shader_mappings = ShaderLibraryMappings();
85 auto sync_switch = std::make_shared<fml::SyncSwitch>(false);
86 context_ = impeller::ContextMTL::Create(impeller::Flags{}, shader_mappings, sync_switch,
87 "Impeller Library");
88
89 if (!context_ || !context_->IsValid()) {
90 VALIDATION_LOG << "Could not create Metal context.";
91 return false;
92 }
93
94 auto device = context_->GetMTLDevice();
95 if (!device) {
96 VALIDATION_LOG << "Could not get Metal device.";
97 return false;
98 }
99
100 delegate_ = std::make_unique<TesterGPUSurfaceMetalDelegate>(device);
101 aiks_context_ = std::make_shared<impeller::AiksContext>(
102 context_, /*typographer_context=*/impeller::TypographerContextSkia::Make());
103
104 return true;
105 }
106
107 // |TesterContext|
108 std::shared_ptr<impeller::Context> GetImpellerContext() const override { return context_; }
109
110 // |TesterContext|
111 std::unique_ptr<Surface> CreateRenderingSurface() override {
112 auto surface = std::make_unique<GPUSurfaceMetalImpeller>(delegate_.get(), aiks_context_);
113 if (!surface->IsValid()) {
114 return nullptr;
115 }
116 return surface;
117 }
118
119 private:
120 std::shared_ptr<impeller::ContextMTL> context_;
121 std::unique_ptr<TesterGPUSurfaceMetalDelegate> delegate_;
122 std::shared_ptr<impeller::AiksContext> aiks_context_;
123};
124
125std::unique_ptr<TesterContext> TesterContextMTLFactory::Create() {
126 auto context = std::make_unique<TesterContextMTL>();
127 if (!context->Initialize()) {
128 return nullptr;
129 }
130 return context;
131}
132
133} // namespace flutter
static std::unique_ptr< TesterContext > Create()
std::shared_ptr< impeller::Context > GetImpellerContext() const override
std::unique_ptr< Surface > CreateRenderingSurface() override
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)
static std::shared_ptr< TypographerContext > Make()
VkDevice device
Definition main.cc:69
VkSurfaceKHR surface
Definition main.cc:65
FlTexture * texture
impeller::ISize32 DlISize
#define VALIDATION_LOG
Definition validation.h:91