Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ef7b773

Browse files
authored
[Impeller] Reland 2: Add Impeller Metal support in the embedder API (#42411) (#42597)
This reverts commit 350a12b + fixes bugs. Reworked the surface wrapping to use a valid descriptor when wrapping the drawable.
1 parent 9d3ba40 commit ef7b773

File tree

51 files changed

+1159
-228
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1159
-228
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,8 @@ ORIGIN: ../../../flutter/impeller/renderer/backend/metal/surface_mtl.h + ../../.
14851485
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/surface_mtl.mm + ../../../flutter/LICENSE
14861486
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/texture_mtl.h + ../../../flutter/LICENSE
14871487
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/texture_mtl.mm + ../../../flutter/LICENSE
1488+
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/texture_wrapper_mtl.h + ../../../flutter/LICENSE
1489+
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/texture_wrapper_mtl.mm + ../../../flutter/LICENSE
14881490
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/vertex_descriptor_mtl.h + ../../../flutter/LICENSE
14891491
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/vertex_descriptor_mtl.mm + ../../../flutter/LICENSE
14901492
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/allocator_vk.cc + ../../../flutter/LICENSE
@@ -2804,13 +2806,19 @@ ORIGIN: ../../../flutter/shell/platform/embedder/embedder_render_target.cc + ../
28042806
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_render_target.h + ../../../flutter/LICENSE
28052807
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_render_target_cache.cc + ../../../flutter/LICENSE
28062808
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_render_target_cache.h + ../../../flutter/LICENSE
2809+
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_render_target_impeller.cc + ../../../flutter/LICENSE
2810+
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_render_target_impeller.h + ../../../flutter/LICENSE
2811+
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_render_target_skia.cc + ../../../flutter/LICENSE
2812+
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_render_target_skia.h + ../../../flutter/LICENSE
28072813
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_struct_macros.h + ../../../flutter/LICENSE
28082814
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_surface.cc + ../../../flutter/LICENSE
28092815
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_surface.h + ../../../flutter/LICENSE
28102816
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_surface_gl.cc + ../../../flutter/LICENSE
28112817
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_surface_gl.h + ../../../flutter/LICENSE
28122818
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_surface_metal.h + ../../../flutter/LICENSE
28132819
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_surface_metal.mm + ../../../flutter/LICENSE
2820+
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_surface_metal_impeller.h + ../../../flutter/LICENSE
2821+
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_surface_metal_impeller.mm + ../../../flutter/LICENSE
28142822
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_surface_software.cc + ../../../flutter/LICENSE
28152823
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_surface_software.h + ../../../flutter/LICENSE
28162824
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_surface_vulkan.cc + ../../../flutter/LICENSE
@@ -4154,6 +4162,8 @@ FILE: ../../../flutter/impeller/renderer/backend/metal/surface_mtl.h
41544162
FILE: ../../../flutter/impeller/renderer/backend/metal/surface_mtl.mm
41554163
FILE: ../../../flutter/impeller/renderer/backend/metal/texture_mtl.h
41564164
FILE: ../../../flutter/impeller/renderer/backend/metal/texture_mtl.mm
4165+
FILE: ../../../flutter/impeller/renderer/backend/metal/texture_wrapper_mtl.h
4166+
FILE: ../../../flutter/impeller/renderer/backend/metal/texture_wrapper_mtl.mm
41574167
FILE: ../../../flutter/impeller/renderer/backend/metal/vertex_descriptor_mtl.h
41584168
FILE: ../../../flutter/impeller/renderer/backend/metal/vertex_descriptor_mtl.mm
41594169
FILE: ../../../flutter/impeller/renderer/backend/vulkan/allocator_vk.cc
@@ -5489,13 +5499,19 @@ FILE: ../../../flutter/shell/platform/embedder/embedder_render_target.cc
54895499
FILE: ../../../flutter/shell/platform/embedder/embedder_render_target.h
54905500
FILE: ../../../flutter/shell/platform/embedder/embedder_render_target_cache.cc
54915501
FILE: ../../../flutter/shell/platform/embedder/embedder_render_target_cache.h
5502+
FILE: ../../../flutter/shell/platform/embedder/embedder_render_target_impeller.cc
5503+
FILE: ../../../flutter/shell/platform/embedder/embedder_render_target_impeller.h
5504+
FILE: ../../../flutter/shell/platform/embedder/embedder_render_target_skia.cc
5505+
FILE: ../../../flutter/shell/platform/embedder/embedder_render_target_skia.h
54925506
FILE: ../../../flutter/shell/platform/embedder/embedder_struct_macros.h
54935507
FILE: ../../../flutter/shell/platform/embedder/embedder_surface.cc
54945508
FILE: ../../../flutter/shell/platform/embedder/embedder_surface.h
54955509
FILE: ../../../flutter/shell/platform/embedder/embedder_surface_gl.cc
54965510
FILE: ../../../flutter/shell/platform/embedder/embedder_surface_gl.h
54975511
FILE: ../../../flutter/shell/platform/embedder/embedder_surface_metal.h
54985512
FILE: ../../../flutter/shell/platform/embedder/embedder_surface_metal.mm
5513+
FILE: ../../../flutter/shell/platform/embedder/embedder_surface_metal_impeller.h
5514+
FILE: ../../../flutter/shell/platform/embedder/embedder_surface_metal_impeller.mm
54995515
FILE: ../../../flutter/shell/platform/embedder/embedder_surface_software.cc
55005516
FILE: ../../../flutter/shell/platform/embedder/embedder_surface_software.h
55015517
FILE: ../../../flutter/shell/platform/embedder/embedder_surface_vulkan.cc

flow/BUILD.gn

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import("//build/fuchsia/sdk.gni")
66
import("//flutter/common/config.gni")
7+
import("//flutter/impeller/tools/impeller.gni")
78
import("//flutter/shell/config.gni")
89
import("//flutter/testing/testing.gni")
910

@@ -96,6 +97,10 @@ source_set("flow") {
9697
"//third_party/skia",
9798
]
9899

100+
if (impeller_supports_rendering) {
101+
deps += [ "//flutter/impeller" ]
102+
}
103+
99104
public_deps = [ "//flutter/display_list" ]
100105
}
101106

flow/embedded_views.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ bool DisplayListEmbedderViewSlice::recording_ended() {
4242
return builder_ == nullptr;
4343
}
4444

45-
void ExternalViewEmbedder::SubmitFrame(GrDirectContext* context,
46-
std::unique_ptr<SurfaceFrame> frame) {
45+
void ExternalViewEmbedder::SubmitFrame(
46+
GrDirectContext* context,
47+
const std::shared_ptr<impeller::AiksContext>& aiks_context,
48+
std::unique_ptr<SurfaceFrame> frame) {
4749
frame->Submit();
4850
};
4951

flow/embedded_views.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef FLUTTER_FLOW_EMBEDDED_VIEWS_H_
66
#define FLUTTER_FLOW_EMBEDDED_VIEWS_H_
77

8+
#include <memory>
89
#include <vector>
910

1011
#include "flutter/display_list/dl_builder.h"
@@ -22,6 +23,16 @@
2223
#include "third_party/skia/include/core/SkSize.h"
2324
#include "third_party/skia/include/core/SkSurface.h"
2425

26+
#if IMPELLER_SUPPORTS_RENDERING
27+
#include "flutter/impeller/aiks/aiks_context.h" // nogncheck
28+
#include "flutter/impeller/renderer/context.h" // nogncheck
29+
#else // IMPELLER_SUPPORTS_RENDERING
30+
namespace impeller {
31+
class Context;
32+
class AiksContext;
33+
} // namespace impeller
34+
#endif // !IMPELLER_SUPPORTS_RENDERING
35+
2536
class GrDirectContext;
2637

2738
namespace flutter {
@@ -414,8 +425,10 @@ class ExternalViewEmbedder {
414425
// This method can mutate the root Skia canvas before submitting the frame.
415426
//
416427
// It can also allocate frames for overlay surfaces to compose hybrid views.
417-
virtual void SubmitFrame(GrDirectContext* context,
418-
std::unique_ptr<SurfaceFrame> frame);
428+
virtual void SubmitFrame(
429+
GrDirectContext* context,
430+
const std::shared_ptr<impeller::AiksContext>& aiks_context,
431+
std::unique_ptr<SurfaceFrame> frame);
419432

420433
// This method provides the embedder a way to do additional tasks after
421434
// |SubmitFrame|. For example, merge task runners if `should_resubmit_frame`

impeller/playground/backend/metal/playground_impl_mtl.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121

122122
auto drawable =
123123
SurfaceMTL::GetMetalDrawableAndValidate(context, data_->metal_layer);
124-
return SurfaceMTL::WrapCurrentMetalLayerDrawable(context, drawable);
124+
return SurfaceMTL::MakeFromMetalLayerDrawable(context, drawable);
125125
}
126126

127127
} // namespace impeller

impeller/renderer/backend/metal/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ impeller_component("metal") {
4242
"surface_mtl.mm",
4343
"texture_mtl.h",
4444
"texture_mtl.mm",
45+
"texture_wrapper_mtl.h",
46+
"texture_wrapper_mtl.mm",
4547
"vertex_descriptor_mtl.h",
4648
"vertex_descriptor_mtl.mm",
4749
]

impeller/renderer/backend/metal/context_mtl.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
#include "impeller/renderer/capabilities.h"
2222
#include "impeller/renderer/context.h"
2323

24+
#if TARGET_OS_SIMULATOR
25+
#define IMPELLER_CA_METAL_LAYER_AVAILABLE API_AVAILABLE(macos(10.11), ios(13.0))
26+
#else // TARGET_OS_SIMULATOR
27+
#define IMPELLER_CA_METAL_LAYER_AVAILABLE API_AVAILABLE(macos(10.11), ios(8.0))
28+
#endif // TARGET_OS_SIMULATOR
29+
2430
namespace impeller {
2531

2632
class ContextMTL final : public Context,
@@ -38,6 +44,14 @@ class ContextMTL final : public Context,
3844
std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
3945
const std::string& label);
4046

47+
static std::shared_ptr<ContextMTL> Create(
48+
id<MTLDevice> device,
49+
id<MTLCommandQueue> command_queue,
50+
const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
51+
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
52+
std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
53+
const std::string& label);
54+
4155
// |Context|
4256
~ContextMTL() override;
4357

@@ -90,6 +104,7 @@ class ContextMTL final : public Context,
90104

91105
ContextMTL(
92106
id<MTLDevice> device,
107+
id<MTLCommandQueue> command_queue,
93108
NSArray<id<MTLLibrary>>* shader_libraries,
94109
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
95110
std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch);

impeller/renderer/backend/metal/context_mtl.mm

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
#include <Foundation/Foundation.h>
88

9+
#include "flutter/fml/concurrent_message_loop.h"
910
#include "flutter/fml/file.h"
1011
#include "flutter/fml/logging.h"
1112
#include "flutter/fml/paths.h"
13+
#include "flutter/fml/synchronization/sync_switch.h"
1214
#include "impeller/core/sampler_descriptor.h"
1315
#include "impeller/renderer/backend/metal/sampler_library_mtl.h"
1416
#include "impeller/renderer/capabilities.h"
@@ -67,10 +69,12 @@ static bool DeviceSupportsComputeSubgroups(id<MTLDevice> device) {
6769

6870
ContextMTL::ContextMTL(
6971
id<MTLDevice> device,
72+
id<MTLCommandQueue> command_queue,
7073
NSArray<id<MTLLibrary>>* shader_libraries,
7174
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
7275
std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch)
7376
: device_(device),
77+
command_queue_(command_queue),
7478
worker_task_runner_(std::move(worker_task_runner)),
7579
is_gpu_disabled_sync_switch_(std::move(is_gpu_disabled_sync_switch)) {
7680
// Validate device.
@@ -96,16 +100,6 @@ static bool DeviceSupportsComputeSubgroups(id<MTLDevice> device) {
96100
shader_library_ = std::move(library);
97101
}
98102

99-
// Setup command queue.
100-
{
101-
command_queue_ = device_.newCommandQueue;
102-
if (!command_queue_) {
103-
VALIDATION_LOG << "Could not setup the command queue.";
104-
return;
105-
}
106-
command_queue_.label = @"Impeller Command Queue";
107-
}
108-
109103
// Setup the pipeline library.
110104
{
111105
pipeline_library_ =
@@ -204,13 +198,28 @@ static bool DeviceSupportsComputeSubgroups(id<MTLDevice> device) {
204198
return ::MTLCreateSystemDefaultDevice();
205199
}
206200

201+
static id<MTLCommandQueue> CreateMetalCommandQueue(id<MTLDevice> device) {
202+
auto command_queue = device.newCommandQueue;
203+
if (!command_queue) {
204+
VALIDATION_LOG << "Could not setup the command queue.";
205+
return nullptr;
206+
}
207+
command_queue.label = @"Impeller Command Queue";
208+
return command_queue;
209+
}
210+
207211
std::shared_ptr<ContextMTL> ContextMTL::Create(
208212
const std::vector<std::string>& shader_library_paths,
209213
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
210214
std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch) {
211215
auto device = CreateMetalDevice();
216+
auto command_queue = CreateMetalCommandQueue(device);
217+
if (!command_queue) {
218+
return nullptr;
219+
}
212220
auto context = std::shared_ptr<ContextMTL>(new ContextMTL(
213-
device, MTLShaderLibraryFromFilePaths(device, shader_library_paths),
221+
device, command_queue,
222+
MTLShaderLibraryFromFilePaths(device, shader_library_paths),
214223
std::move(worker_task_runner), std::move(is_gpu_disabled_sync_switch)));
215224
if (!context->IsValid()) {
216225
FML_LOG(ERROR) << "Could not create Metal context.";
@@ -223,11 +232,35 @@ static bool DeviceSupportsComputeSubgroups(id<MTLDevice> device) {
223232
const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
224233
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
225234
std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
226-
const std::string& label) {
235+
const std::string& library_label) {
227236
auto device = CreateMetalDevice();
237+
auto command_queue = CreateMetalCommandQueue(device);
238+
if (!command_queue) {
239+
return nullptr;
240+
}
241+
auto context = std::shared_ptr<ContextMTL>(new ContextMTL(
242+
device, command_queue,
243+
MTLShaderLibraryFromFileData(device, shader_libraries_data,
244+
library_label),
245+
std::move(worker_task_runner), std::move(is_gpu_disabled_sync_switch)));
246+
if (!context->IsValid()) {
247+
FML_LOG(ERROR) << "Could not create Metal context.";
248+
return nullptr;
249+
}
250+
return context;
251+
}
252+
253+
std::shared_ptr<ContextMTL> ContextMTL::Create(
254+
id<MTLDevice> device,
255+
id<MTLCommandQueue> command_queue,
256+
const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
257+
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
258+
std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
259+
const std::string& library_label) {
228260
auto context = std::shared_ptr<ContextMTL>(new ContextMTL(
229-
device,
230-
MTLShaderLibraryFromFileData(device, shader_libraries_data, label),
261+
device, command_queue,
262+
MTLShaderLibraryFromFileData(device, shader_libraries_data,
263+
library_label),
231264
std::move(worker_task_runner), std::move(is_gpu_disabled_sync_switch)));
232265
if (!context->IsValid()) {
233266
FML_LOG(ERROR) << "Could not create Metal context.";

impeller/renderer/backend/metal/surface_mtl.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include <QuartzCore/CAMetalLayer.h>
8+
#include <memory>
89

910
#include "flutter/fml/macros.h"
1011
#include "impeller/geometry/rect.h"
@@ -37,10 +38,16 @@ class SurfaceMTL final : public Surface {
3738
const std::shared_ptr<Context>& context,
3839
CAMetalLayer* layer);
3940

40-
static std::unique_ptr<SurfaceMTL> WrapCurrentMetalLayerDrawable(
41+
static std::unique_ptr<SurfaceMTL> MakeFromMetalLayerDrawable(
4142
const std::shared_ptr<Context>& context,
4243
id<CAMetalDrawable> drawable,
4344
std::optional<IRect> clip_rect = std::nullopt);
45+
46+
static std::unique_ptr<SurfaceMTL> MakeFromTexture(
47+
const std::shared_ptr<Context>& context,
48+
id<MTLTexture> texture,
49+
std::optional<IRect> clip_rect,
50+
id<CAMetalDrawable> drawable = nil);
4451
#pragma GCC diagnostic pop
4552

4653
// |Surface|
@@ -58,6 +65,8 @@ class SurfaceMTL final : public Surface {
5865
std::weak_ptr<Context> context_;
5966
std::shared_ptr<Texture> resolve_texture_;
6067
id<CAMetalDrawable> drawable_ = nil;
68+
std::shared_ptr<Texture> source_texture_;
69+
std::shared_ptr<Texture> destination_texture_;
6170
bool requires_blit_ = false;
6271
std::optional<IRect> clip_rect_;
6372

@@ -67,6 +76,8 @@ class SurfaceMTL final : public Surface {
6776
const RenderTarget& target,
6877
std::shared_ptr<Texture> resolve_texture,
6978
id<CAMetalDrawable> drawable,
79+
std::shared_ptr<Texture> source_texture,
80+
std::shared_ptr<Texture> destination_texture,
7081
bool requires_blit,
7182
std::optional<IRect> clip_rect);
7283

0 commit comments

Comments
 (0)