From 3c27f944d9e33750020daa9ff343382c349c5764 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Tue, 12 Nov 2024 13:48:41 -0800 Subject: [PATCH] macOS: ARC bridge casts for FlutterMetalTexture.user_data (flutter/engine#56518) 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 CoreFoundation functions `CFBridgingRetain` and `CFBridigingRelease`. In FlutterEngine, for example: https://github.com/flutter/engine/blob/67eb4ae000de830545670ef5de4cd08930a8ef2a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm#L648-L664 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 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../macos/framework/Source/FlutterSurface.mm | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm index d2726560d7..1097409426 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm @@ -52,21 +52,21 @@ 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 {