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

Commit 2bee1ad

Browse files
committed
[embedder] Ensure FlutterMetalTexture cleanup call
This ensures FlutterMetalTexture.destruction_callback gets called. FlutterRendererConfig.get_next_drawable_callback` holds a callback used by the embedder API to request a drawable; in the case of Metal, this drawable is a FlutterMetalTexture. FlutterMetalTexture.destruction_callback should be called when it's safe to release resources associated with the FlutterMetalTexture. This callback is not currently invoked for textures returned via FlutterRendererConfig.get_next_drawable_callback; instead we unpack the returned struct and pass it on. In the compositor codepath, we do create an SkSurface that triggers the destruction callback, here: https://github.com/flutter/engine/blob/303e26e96561d9b76f2344e97a5fc32eb6dfdb9a/shell/platform/embedder/embedder.cc#L868-L881 Issue: flutter/flutter#116381
1 parent fa24f94 commit 2bee1ad

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

shell/gpu/gpu_surface_metal_delegate.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ typedef void* GPUCAMetalLayerHandle;
2626
// expected to be id<MTLTexture>
2727
typedef const void* GPUMTLTextureHandle;
2828

29+
typedef void (*GPUMTLDestructionCallback)(void* /* destruction_context */);
30+
2931
struct GPUMTLTextureInfo {
3032
int64_t texture_id;
3133
GPUMTLTextureHandle texture;
34+
GPUMTLDestructionCallback destruction_callback;
35+
void* destruction_context;
3236
};
3337

3438
enum class MTLRenderTargetType { kMTLTexture, kCAMetalLayer };

shell/gpu/gpu_surface_metal_skia.mm

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@
3838
MsaaSampleCount sample_cnt,
3939
SkColorType color_type,
4040
sk_sp<SkColorSpace> color_space,
41-
const SkSurfaceProps* props) {
41+
const SkSurfaceProps* props,
42+
SkSurface::TextureReleaseProc release_proc,
43+
SkSurface::ReleaseContext release_context) {
4244
GrMtlTextureInfo info;
4345
info.fTexture.reset([texture retain]);
4446
GrBackendTexture backend_texture(texture.width, texture.height, GrMipmapped::kNo, info);
45-
return SkSurface::MakeFromBackendTexture(context, backend_texture, origin,
46-
static_cast<int>(sample_cnt), color_type,
47-
std::move(color_space), props);
47+
return SkSurface::MakeFromBackendTexture(
48+
context, backend_texture, origin, static_cast<int>(sample_cnt), color_type,
49+
std::move(color_space), props, release_proc, release_context);
4850
}
4951
} // namespace
5052

@@ -137,7 +139,9 @@
137139
msaa_samples_, // sample count
138140
kBGRA_8888_SkColorType, // color type
139141
nullptr, // colorspace
140-
nullptr // surface properties
142+
nullptr, // surface properties
143+
nullptr, // release proc
144+
nullptr // release context
141145
);
142146

143147
if (!surface) {
@@ -203,9 +207,10 @@
203207
return nullptr;
204208
}
205209

206-
sk_sp<SkSurface> surface =
207-
CreateSurfaceFromMetalTexture(context_.get(), mtl_texture, kTopLeft_GrSurfaceOrigin,
208-
msaa_samples_, kBGRA_8888_SkColorType, nullptr, nullptr);
210+
sk_sp<SkSurface> surface = CreateSurfaceFromMetalTexture(
211+
context_.get(), mtl_texture, kTopLeft_GrSurfaceOrigin, msaa_samples_, kBGRA_8888_SkColorType,
212+
nullptr, nullptr, static_cast<SkSurface::TextureReleaseProc>(texture.destruction_callback),
213+
texture.destruction_context);
209214

210215
if (!surface) {
211216
FML_LOG(ERROR) << "Could not create the SkSurface from the metal texture.";

shell/platform/embedder/embedder.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ InferMetalPlatformViewCreationCallback(
477477
FlutterMetalTexture metal_texture = ptr(user_data, &frame_info);
478478
texture_info.texture_id = metal_texture.texture_id;
479479
texture_info.texture = metal_texture.texture;
480+
texture_info.destruction_callback = metal_texture.destruction_callback;
481+
texture_info.destruction_context = metal_texture.user_data;
480482
return texture_info;
481483
};
482484

0 commit comments

Comments
 (0)