Skip to content

Commit

Permalink
Migrate ios_surface files to ARC (#52139)
Browse files Browse the repository at this point in the history
Smart pointers support ARC as of #47612, and the unit tests were migrated in #48162.

Migrate `ios_surface` classes from MRC to ARC.

Decorate C functions that take or return Objective-C objects or structs containing Objective-C objects with [`cf_audited_transfer`](https://clang.llvm.org/docs/AutomaticReferenceCounting.html#auditing-of-c-retainable-pointer-interfaces).  

Part of flutter/flutter#137801.
  • Loading branch information
jmagman authored Apr 24, 2024
1 parent c603f03 commit 77605fa
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 33 deletions.
16 changes: 8 additions & 8 deletions shell/platform/darwin/ios/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ source_set("flutter_framework_source_arc") {
"ios_context_software.mm",
"ios_external_texture_metal.h",
"ios_external_texture_metal.mm",
"ios_surface.h",
"ios_surface.mm",
"ios_surface_metal_impeller.h",
"ios_surface_metal_impeller.mm",
"ios_surface_metal_skia.h",
"ios_surface_metal_skia.mm",
"ios_surface_software.h",
"ios_surface_software.mm",
"rendering_api_selection.h",
"rendering_api_selection.mm",
]
Expand Down Expand Up @@ -172,14 +180,6 @@ source_set("flutter_framework_source") {
"framework/Source/accessibility_text_entry.mm",
"ios_external_view_embedder.h",
"ios_external_view_embedder.mm",
"ios_surface.h",
"ios_surface.mm",
"ios_surface_metal_impeller.h",
"ios_surface_metal_impeller.mm",
"ios_surface_metal_skia.h",
"ios_surface_metal_skia.mm",
"ios_surface_software.h",
"ios_surface_software.mm",
"platform_message_handler_ios.h",
"platform_message_handler_ios.mm",
"platform_view_ios.h",
Expand Down
12 changes: 6 additions & 6 deletions shell/platform/darwin/ios/ios_surface.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import "flutter/shell/platform/darwin/ios/ios_surface_software.h"
#include "flutter/shell/platform/darwin/ios/rendering_api_selection.h"

FLUTTER_ASSERT_ARC

namespace flutter {

std::unique_ptr<IOSSurface> IOSSurface::Create(std::shared_ptr<IOSContext> context,
Expand All @@ -21,16 +23,14 @@
switch (context->GetBackend()) {
case IOSRenderingBackend::kSkia:
return std::make_unique<IOSSurfaceMetalSkia>(
fml::scoped_nsobject<CAMetalLayer>(
reinterpret_cast<CAMetalLayer*>([layer.get() retain])), // Metal layer
std::move(context) // context
fml::scoped_nsobject<CAMetalLayer>((CAMetalLayer*)layer.get()), // Metal layer
std::move(context) // context
);
break;
case IOSRenderingBackend::kImpeller:
return std::make_unique<IOSSurfaceMetalImpeller>(
fml::scoped_nsobject<CAMetalLayer>(
reinterpret_cast<CAMetalLayer*>([layer.get() retain])), // Metal layer
std::move(context) // context
fml::scoped_nsobject<CAMetalLayer>((CAMetalLayer*)layer.get()), // Metal layer
std::move(context) // context
);
}
}
Expand Down
11 changes: 7 additions & 4 deletions shell/platform/darwin/ios/ios_surface_metal_impeller.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,19 @@ class SK_API_AVAILABLE_CA_METAL_LAYER IOSSurfaceMetalImpeller final
std::unique_ptr<Surface> CreateGPUSurface(GrDirectContext* gr_context) override;

// |GPUSurfaceMetalDelegate|
GPUCAMetalLayerHandle GetCAMetalLayer(const SkISize& frame_info) const override;
GPUCAMetalLayerHandle GetCAMetalLayer(const SkISize& frame_info) const override
__attribute__((cf_audited_transfer));

// |GPUSurfaceMetalDelegate|
bool PresentDrawable(GrMTLHandle drawable) const override;
bool PresentDrawable(GrMTLHandle drawable) const override __attribute__((cf_audited_transfer));

// |GPUSurfaceMetalDelegate|
GPUMTLTextureInfo GetMTLTexture(const SkISize& frame_info) const override;
GPUMTLTextureInfo GetMTLTexture(const SkISize& frame_info) const override
__attribute__((cf_audited_transfer));

// |GPUSurfaceMetalDelegate|
bool PresentTexture(GPUMTLTextureInfo texture) const override;
bool PresentTexture(GPUMTLTextureInfo texture) const override
__attribute__((cf_audited_transfer));

// |GPUSurfaceMetalDelegate|
bool AllowsDrawingWhenGpuDisabled() const override;
Expand Down
4 changes: 3 additions & 1 deletion shell/platform/darwin/ios/ios_surface_metal_impeller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "flutter/impeller/renderer/context.h"
#include "flutter/shell/gpu/gpu_surface_metal_impeller.h"

FLUTTER_ASSERT_ARC

namespace flutter {

IOSSurfaceMetalImpeller::IOSSurfaceMetalImpeller(const fml::scoped_nsobject<CAMetalLayer>& layer,
Expand Down Expand Up @@ -64,7 +66,7 @@
// exit the app.
layer.presentsWithTransaction = [[NSThread currentThread] isMainThread];

return layer;
return (__bridge GPUCAMetalLayerHandle)layer;
}

// |GPUSurfaceMetalDelegate|
Expand Down
11 changes: 7 additions & 4 deletions shell/platform/darwin/ios/ios_surface_metal_skia.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ class SK_API_AVAILABLE_CA_METAL_LAYER IOSSurfaceMetalSkia final : public IOSSurf
std::unique_ptr<Surface> CreateGPUSurface(GrDirectContext* gr_context) override;

// |GPUSurfaceMetalDelegate|
GPUCAMetalLayerHandle GetCAMetalLayer(const SkISize& frame_info) const override;
GPUCAMetalLayerHandle GetCAMetalLayer(const SkISize& frame_info) const override
__attribute__((cf_audited_transfer));

// |GPUSurfaceMetalDelegate|
bool PresentDrawable(GrMTLHandle drawable) const override;
bool PresentDrawable(GrMTLHandle drawable) const override __attribute__((cf_audited_transfer));

// |GPUSurfaceMetalDelegate|
GPUMTLTextureInfo GetMTLTexture(const SkISize& frame_info) const override;
GPUMTLTextureInfo GetMTLTexture(const SkISize& frame_info) const override
__attribute__((cf_audited_transfer));

// |GPUSurfaceMetalDelegate|
bool PresentTexture(GPUMTLTextureInfo texture) const override;
bool PresentTexture(GPUMTLTextureInfo texture) const override
__attribute__((cf_audited_transfer));

// |GPUSurfaceMetalDelegate|
bool AllowsDrawingWhenGpuDisabled() const override;
Expand Down
20 changes: 11 additions & 9 deletions shell/platform/darwin/ios/ios_surface_metal_skia.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
#include "flutter/shell/gpu/gpu_surface_metal_skia.h"
#include "flutter/shell/platform/darwin/ios/ios_context_metal_skia.h"

FLUTTER_ASSERT_ARC

@protocol FlutterMetalDrawable <MTLDrawable>
- (void)flutterPrepareForPresent:(nonnull id<MTLCommandBuffer>)commandBuffer;
@end

namespace flutter {

static IOSContextMetalSkia* CastToMetalContext(const std::shared_ptr<IOSContext>& context) {
return reinterpret_cast<IOSContextMetalSkia*>(context.get());
static IOSContextMetalSkia* CastToMetalContext(const std::shared_ptr<IOSContext>& context)
__attribute__((cf_audited_transfer)) {
return (IOSContextMetalSkia*)context.get();
}

IOSSurfaceMetalSkia::IOSSurfaceMetalSkia(const fml::scoped_nsobject<CAMetalLayer>& layer,
Expand Down Expand Up @@ -72,7 +75,7 @@ - (void)flutterPrepareForPresent:(nonnull id<MTLCommandBuffer>)commandBuffer;
// the raster thread, there is no such transaction.
layer.presentsWithTransaction = [[NSThread currentThread] isMainThread];

return layer;
return (__bridge GPUCAMetalLayerHandle)layer;
}

// |GPUSurfaceMetalDelegate|
Expand All @@ -82,16 +85,15 @@ - (void)flutterPrepareForPresent:(nonnull id<MTLCommandBuffer>)commandBuffer;
return false;
}

auto command_buffer =
fml::scoped_nsprotocol<id<MTLCommandBuffer>>([[command_queue_ commandBuffer] retain]);
id<MTLCommandBuffer> command_buffer = [command_queue_ commandBuffer];

id<CAMetalDrawable> metal_drawable = reinterpret_cast<id<CAMetalDrawable>>(drawable);
id<CAMetalDrawable> metal_drawable = (__bridge id<CAMetalDrawable>)drawable;
if ([metal_drawable conformsToProtocol:@protocol(FlutterMetalDrawable)]) {
[(id<FlutterMetalDrawable>)metal_drawable flutterPrepareForPresent:command_buffer.get()];
[(id<FlutterMetalDrawable>)metal_drawable flutterPrepareForPresent:command_buffer];
}

[command_buffer.get() commit];
[command_buffer.get() waitUntilScheduled];
[command_buffer commit];
[command_buffer waitUntilScheduled];

[metal_drawable present];
return true;
Expand Down
4 changes: 3 additions & 1 deletion shell/platform/darwin/ios/ios_surface_software.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/utils/mac/SkCGUtils.h"

FLUTTER_ASSERT_ARC

namespace flutter {

IOSSurfaceSoftware::IOSSurfaceSoftware(const fml::scoped_nsobject<CALayer>& layer,
Expand Down Expand Up @@ -118,7 +120,7 @@
return false;
}

layer_.get().contents = reinterpret_cast<id>(static_cast<CGImageRef>(pixmap_image));
layer_.get().contents = (__bridge id)(static_cast<CGImageRef>(pixmap_image));

return true;
}
Expand Down

0 comments on commit 77605fa

Please sign in to comment.