From 827ebd5877bff27d615e6a27a147ede3ae6a5291 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Mon, 11 Nov 2024 16:40:14 -0800 Subject: [PATCH] macOS: ARC bridge casts for FlutterMetalTexture.user_data In `[FlutterSurface asFlutterMetalTexture]` we return a `FlutterMetalTexture` whose `user_data` field holds a void* reference to the associated `FlutterSurface`. For conistency with other parts of the codebase, this now uses ARC bridge casts to perform the additional retain and release of the associated surface rather than CoreFoundataion functions `CFBridgingRetain` and `CFBridigingRelease`. Also migrates the code to initialise and immediately return the struct using field designators, and reorders the assignments to be in the declaration order specified in embedder.h as required by the C++ spec. No changes to tests since this introduces no semantic change. Issue: https://github.com/flutter/flutter/issues/137801 --- .../macos/framework/Source/FlutterSurface.mm | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm b/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm index d2726560d74bd..109740942621d 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm @@ -52,21 +52,21 @@ - (instancetype)initWithSize:(CGSize)size device:(id)device { return self; } -static void ReleaseSurface(void* surface) { - if (surface != nullptr) { - CFBridgingRelease(surface); - } -} - - (FlutterMetalTexture)asFlutterMetalTexture { - FlutterMetalTexture res; - memset(&res, 0, sizeof(FlutterMetalTexture)); - res.struct_size = sizeof(FlutterMetalTexture); - res.texture = (__bridge void*)_texture; - res.texture_id = self.textureId; - res.user_data = (void*)CFBridgingRetain(self); - res.destruction_callback = ReleaseSurface; - return res; + return FlutterMetalTexture{ + .struct_size = sizeof(FlutterMetalTexture), + .texture_id = self.textureId, + .texture = (__bridge void*)_texture, + // Retain for use in [FlutterSurface fromFlutterMetalTexture]. Released in + // destruction_callback. + .user_data = (__bridge_retained void*)self, + .destruction_callback = + [](void* user_data) { + // Balancing release for the retain when setting user_data above. + FlutterSurface* surface = (__bridge_transfer FlutterSurface*)user_data; + surface = nil; + }, + }; } + (FlutterSurface*)fromFlutterMetalTexture:(const FlutterMetalTexture*)texture {