Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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|
108 return [](DefaultPointerDataDispatcher::Delegate& delegate) {
109 return std::make_unique<SmoothPointerDataDispatcher>(delegate);
110 };
111}
112
113// |PlatformView|
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],
121}
122
123// |PlatformView|
124std::shared_ptr<impeller::Context> ShellTestPlatformViewMetal::GetImpellerContext() const {
125 return [metal_context_->impeller_context() context];
126}
127
128// |GPUSurfaceMetalDelegate|
130 FML_CHECK(false) << "A Metal Delegate configured with MTLRenderTargetType::kMTLTexture was asked "
131 "to acquire a layer.";
132 return nullptr;
133}
134
135// |GPUSurfaceMetalDelegate|
137 FML_CHECK(false) << "A Metal Delegate configured with MTLRenderTargetType::kMTLTexture was asked "
138 "to present a layer drawable.";
139 return true;
140}
141
142// |GPUSurfaceMetalDelegate|
144 return metal_context_->offscreen_texture_info();
145}
146
147// |GPUSurfaceMetalDelegate|
149 // The texture resides offscreen. There is nothing to render to.
150 return true;
151}
152
153} // namespace testing
154} // 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...
const Settings & GetSettings() const
Get the settings for this platform view instance.
The interface for Engine to implement.
std::shared_ptr< impeller::Context > GetImpellerContext() const override
GPUMTLTextureInfo GetMTLTexture(const SkISize &frame_info) const override
Returns the handle to the MTLTexture to render to. This is only called when the specified render targ...
GPUCAMetalLayerHandle GetCAMetalLayer(const SkISize &frame_info) const override
Returns the handle to the CAMetalLayer to render to. This is only called when the specified render ta...
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)
std::unique_ptr< VsyncWaiter > CreateVSyncWaiter() override
Invoked by the shell to obtain a platform specific vsync waiter. It is optional for platforms to over...
std::shared_ptr< ExternalViewEmbedder > CreateExternalViewEmbedder() override
PointerDataDispatcherMaker GetDispatcherMaker() override
Returns a platform-specific PointerDataDispatcherMaker so the Engine can construct the PointerDataPac...
bool PresentTexture(GPUMTLTextureInfo texture) const override
Presents the texture with texture_id to the "screen". texture_id corresponds to a texture that has be...
bool PresentDrawable(GrMTLHandle drawable) const override
Presents the drawable to the "screen". The drawable is obtained from the CAMetalLayer that given by G...
std::unique_ptr< Surface > CreateRenderingSurface() override
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 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