Flutter Engine
The Flutter Engine
shell_test_platform_view_metal.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 "flutter/shell/common/shell_test_platform_view_metal.h"
6
7#import <Metal/Metal.h>
8
9#include <utility>
10
11#include "flutter/fml/platform/darwin/scoped_nsobject.h"
12#include "flutter/shell/gpu/gpu_surface_metal_impeller.h"
13#include "flutter/shell/gpu/gpu_surface_metal_skia.h"
14#include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.h"
15#include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalSkia.h"
16
17namespace flutter {
18namespace testing {
19
21 auto descriptor =
22 [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
23 width:800
24 height:600
25 mipmapped:NO];
26 descriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
27 return fml::scoped_nsprotocol<id<MTLTexture>>{[device newTextureWithDescriptor:descriptor]};
28}
29
30// This is out of the header so that shell_test_platform_view_metal.h can be included in
31// non-Objective-C TUs.
33 public:
34 explicit DarwinContextMetal(
35 bool impeller,
36 const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch)
37 : context_(impeller ? nil : [[FlutterDarwinContextMetalSkia alloc] initWithDefaultMTLDevice]),
38 impeller_context_(
39 impeller ? [[FlutterDarwinContextMetalImpeller alloc] init:is_gpu_disabled_sync_switch]
40 : nil),
41 offscreen_texture_(CreateOffscreenTexture(
42 impeller ? [impeller_context_ context]->GetMTLDevice() : [context_ device])) {}
43
44 ~DarwinContextMetal() = default;
45
47 return impeller_context_;
48 }
49
50 fml::scoped_nsobject<FlutterDarwinContextMetalSkia> context() const { return context_; }
51
52 fml::scoped_nsprotocol<id<MTLTexture>> offscreen_texture() const { return offscreen_texture_; }
53
54 GPUMTLTextureInfo offscreen_texture_info() const {
56 info.texture_id = 0;
57 info.texture = reinterpret_cast<GPUMTLTextureHandle>(offscreen_texture_.get());
58 return info;
59 }
60
61 private:
64 const fml::scoped_nsprotocol<id<MTLTexture>> offscreen_texture_;
65
67};
68
70 PlatformView::Delegate& delegate,
71 const TaskRunners& task_runners,
72 std::shared_ptr<ShellTestVsyncClock> vsync_clock,
73 CreateVsyncWaiter create_vsync_waiter,
74 std::shared_ptr<ShellTestExternalViewEmbedder> shell_test_external_view_embedder,
75 const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch)
76 : ShellTestPlatformView(delegate, task_runners),
78 metal_context_(std::make_unique<DarwinContextMetal>(GetSettings().enable_impeller,
79 is_gpu_disabled_sync_switch)),
80 create_vsync_waiter_(std::move(create_vsync_waiter)),
81 vsync_clock_(std::move(vsync_clock)),
82 shell_test_external_view_embedder_(std::move(shell_test_external_view_embedder)) {
83 if (GetSettings().enable_impeller) {
84 FML_CHECK([metal_context_->impeller_context() context] != nil);
85 } else {
86 FML_CHECK([metal_context_->context() mainContext] != nil);
87 }
88}
89
91
92std::unique_ptr<VsyncWaiter> ShellTestPlatformViewMetal::CreateVSyncWaiter() {
93 return create_vsync_waiter_();
94}
95
96// |ShellTestPlatformView|
98 vsync_clock_->SimulateVSync();
99}
100
101// |PlatformView|
102std::shared_ptr<ExternalViewEmbedder> ShellTestPlatformViewMetal::CreateExternalViewEmbedder() {
103 return shell_test_external_view_embedder_;
104}
105
106// |PlatformView|
107PointerDataDispatcherMaker ShellTestPlatformViewMetal::GetDispatcherMaker() {
108 return [](DefaultPointerDataDispatcher::Delegate& delegate) {
109 return std::make_unique<SmoothPointerDataDispatcher>(delegate);
110 };
111}
112
113// |PlatformView|
114std::unique_ptr<Surface> ShellTestPlatformViewMetal::CreateRenderingSurface() {
115 if (GetSettings().enable_impeller) {
116 return std::make_unique<GPUSurfaceMetalImpeller>(this,
117 [metal_context_->impeller_context() context]);
118 }
119 return std::make_unique<GPUSurfaceMetalSkia>(this, [metal_context_->context() mainContext]);
120}
121
122// |PlatformView|
123std::shared_ptr<impeller::Context> ShellTestPlatformViewMetal::GetImpellerContext() const {
124 return [metal_context_->impeller_context() context];
125}
126
127// |GPUSurfaceMetalDelegate|
128GPUCAMetalLayerHandle ShellTestPlatformViewMetal::GetCAMetalLayer(const SkISize& frame_info) const {
129 FML_CHECK(false) << "A Metal Delegate configured with MTLRenderTargetType::kMTLTexture was asked "
130 "to acquire a layer.";
131 return nullptr;
132}
133
134// |GPUSurfaceMetalDelegate|
135bool ShellTestPlatformViewMetal::PresentDrawable(GrMTLHandle drawable) const {
136 FML_CHECK(false) << "A Metal Delegate configured with MTLRenderTargetType::kMTLTexture was asked "
137 "to present a layer drawable.";
138 return true;
139}
140
141// |GPUSurfaceMetalDelegate|
142GPUMTLTextureInfo ShellTestPlatformViewMetal::GetMTLTexture(const SkISize& frame_info) const {
143 return metal_context_->offscreen_texture_info();
144}
145
146// |GPUSurfaceMetalDelegate|
147bool ShellTestPlatformViewMetal::PresentTexture(GPUMTLTextureInfo texture) const {
148 // The texture resides offscreen. There is nothing to render to.
149 return true;
150}
151
152} // namespace testing
153} // namespace flutter
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
const void * GrMTLHandle
Definition: GrMtlTypes.h:20
Interface implemented by all platform surfaces that can present a metal backing store to the "screen"...
Used to forward events from the platform view to interested subsystems. This forwarding is done by th...
Definition: platform_view.h:60
const Settings & GetSettings() const
Get the settings for this platform view instance.
The interface for Engine to implement.
ShellTestPlatformViewMetal(PlatformView::Delegate &delegate, const TaskRunners &task_runners, std::shared_ptr< ShellTestVsyncClock > vsync_clock, CreateVsyncWaiter create_vsync_waiter, std::shared_ptr< ShellTestExternalViewEmbedder > shell_test_external_view_embedder, const std::shared_ptr< const fml::SyncSwitch > &is_gpu_disabled_sync_switch)
VkDevice device
Definition: main.cc:53
#define FML_CHECK(condition)
Definition: logging.h:85
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
FlTexture * texture
static bool init()
static fml::scoped_nsprotocol< id< MTLTexture > > CreateOffscreenTexture(id< MTLDevice > device)
std::function< std::unique_ptr< VsyncWaiter >()> CreateVsyncWaiter
std::function< std::unique_ptr< PointerDataDispatcher >(PointerDataDispatcher::Delegate &)> PointerDataDispatcherMaker
Signature for constructing PointerDataDispatcher.
const void * GPUMTLTextureHandle
Definition: ref_ptr.h:256
int32_t height
int32_t width
Definition: SkSize.h:16