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

Migrate ios_surface files to ARC #52139

Merged
merged 2 commits into from
Apr 24, 2024
Merged

Conversation

jmagman
Copy link
Member

@jmagman jmagman commented Apr 15, 2024

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.

Part of flutter/flutter#137801.

@jmagman jmagman marked this pull request as ready for review April 16, 2024 06:57
Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we remove the scoped_nsobject from layer_?

But it's also fine to defer that to a second-pass cleanup when you eliminate scoped_nsobject completely if you'd rather.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do a cleanup sweep at the end. I'll keep removing them where it's easy though.

@jmagman jmagman added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 18, 2024
Copy link
Contributor

auto-submit bot commented Apr 18, 2024

auto label is removed for flutter/engine/52139, due to - The status or check suite Mac mac_host_engine has failed. Please fix the issues identified (or deflake) before re-applying this label.

@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Apr 18, 2024
@jmagman jmagman requested a review from gaaclarke April 19, 2024 17:22
Copy link
Member

@gaaclarke gaaclarke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😨

@stuartmorgan-g
Copy link
Contributor

@gaaclarke I'm not following your comments here; why would we need special annotations when a function that doesn't use magic words (e.g., Create) isn't returning a retained object?

@gaaclarke
Copy link
Member

@gaaclarke I'm not following your comments here; why would we need special annotations when a function that doesn't use magic words (e.g., Create) isn't returning a retained object?

  1. We have unmatched retains when creating the scoped_nsobject, I've outlined it here in this pr: Migrate PlatformMessageHandlerIos to ARC #52226 (comment)
  2. We are changing the semantics of the get methods. Previously they were returning unretained references, now they are returning retained references. When they are called from MRC that could lead to unbalanced retain/releases. If we can assert it is only called from ARC that shouldn't be a problem.
  3. A minor quibble, not using ns_returns_not_retained will change the performance profile.

@stuartmorgan-g
Copy link
Contributor

2. We are changing the semantics of the get methods. Previously they were returning unretained references, now they are returning retained references.

What get methods are you referring to here? scoped_* gets are explicitly annotated as returning unretained. None of the semantics of scoped_* change between ARC and non-ARC. If that were not true, then everything would have undefined behavior, as it did in the original version you had tried to enable. See extensive previous discussion on that.

@gaaclarke
Copy link
Member

What get methods are you referring to here?

I'm talking about IOSSurfaceMetalSkia::GetCAMetalLayer and IOSSurfaceMetalImpeller::GetCAMetalLayer.

@stuartmorgan-g
Copy link
Contributor

Okay, so that's the same basic question as #52226 (comment)

@gaaclarke
Copy link
Member

@gaaclarke I'm not following your comments here; why would we need special annotations when a function that doesn't use magic words (e.g., Create) isn't returning a retained object?

  1. We have unmatched retains when creating the scoped_nsobject, I've outlined it here in this pr: Migrate PlatformMessageHandlerIos to ARC #52226 (comment)

This was resolved to be okay. There is conditional compilation that switches the behavior based on arc or mrc, at the scoped_nsprotocol level.

  1. We are changing the semantics of the get methods. Previously they were returning unretained references, now they are returning retained references. When they are called from MRC that could lead to unbalanced retain/releases. If we can assert it is only called from ARC that shouldn't be a problem.

This was shown that the default behavior is returning autoreleased objects so should be good. I thought they were returning retained objects and the call site would release them. I think that may be an optimization that arc can do, but if it doesn't know the call site it can't.

  1. A minor quibble, not using ns_returns_not_retained will change the performance profile.

This is still a thing since we are now adding extra autorelease pool entries where there were none. I'm not sure how heavily this code is used but my professional opinon would be to not add the entries to the autorelease pool and to maintain the old semantics.

This can be observed compiling

@interface Foo : NSObject
@end

@implementation Foo
@end

class Bar {
public:
  Foo *GetFoo() { return foo_; }

private:
  Foo *foo_;
};

generates

__ZN3Bar6GetFooEv:                      ; @_ZN3Bar6GetFooEv
	.cfi_startproc
; %bb.0:
	sub	sp, sp, #16
	.cfi_def_cfa_offset 16
	str	x0, [sp, #8]
	ldr	x8, [sp, #8]
	ldr	x0, [x8]
	add	sp, sp, #16
	b	_objc_retainAutoreleaseReturnValue
	.cfi_endproc
               

@stuartmorgan-g
Copy link
Contributor

I'm not sure how heavily this code is used but my professional opinon would be to not add the entries to the autorelease pool and to maintain the old semantics.

Returning an unretained object without autoreleasing it is generally an anti-pattern, as it can lead to subtle crash bugs. I would strongly recommend that we follow standard advice relating to optimization: follow standard behaviors until/unless there's clear evidence that a particular piece of code is critical to overall performance, rather than trying to manual pre-optimize just in case.

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact "@test-exemption-reviewer" in the #hackers channel in Chat (don't just cc them here, they won't see it! Use Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@stuartmorgan-g
Copy link
Contributor

test-exempt: code refactor with no semantic change

@jmagman jmagman added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 24, 2024
@auto-submit auto-submit bot merged commit 77605fa into flutter:main Apr 24, 2024
29 checks passed
@jmagman jmagman deleted the surface-arc branch April 24, 2024 17:57
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 24, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 24, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 24, 2024
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Apr 24, 2024
…147336)

flutter/engine@b5d5832...b30c0a7

2024-04-24 [email protected] Remove UIAccessibilityTraitKeyboardKey to fix touch typing (flutter/engine#52333)
2024-04-24 [email protected] [Impeller] Remove libtess2 from libflutter. (flutter/engine#52357)
2024-04-24 [email protected] Roll Skia from 510b6766d907 to afcc1db27593 (2 revisions) (flutter/engine#52367)
2024-04-24 [email protected] [web:tests] switch to new HTML DOM matcher (flutter/engine#52354)
2024-04-24 [email protected] [Impeller] use spec constant for gaussian shader, rename, and reuse vertex sources. (flutter/engine#52361)
2024-04-24 [email protected] [Impeller] delete points compute shader. (flutter/engine#52346)
2024-04-24 [email protected] [darwin] Update pixel format handling in FlutterTexture (flutter/engine#52326)
2024-04-24 [email protected] [Impeller] make drawAtlas always use porterduff or vertices_uber shader (flutter/engine#52348)
2024-04-24 [email protected] Migrate ios_surface files to ARC (flutter/engine#52139)
2024-04-24 [email protected] Roll Dart SDK from f470eaaf6e6d to 38c43a01a51e (1 revision) (flutter/engine#52365)
2024-04-24 [email protected] Roll Skia from b5dd23bd29df to 510b6766d907 (16 revisions) (flutter/engine#52364)
2024-04-24 [email protected] Fix some warnings reported by recent versions of clang-tidy (flutter/engine#52349)
2024-04-24 [email protected] Roll Skia from e15464e6e982 to b5dd23bd29df (1 revision) (flutter/engine#52353)
2024-04-24 [email protected] Roll Dart SDK from 5227dc5103f6 to f470eaaf6e6d (1 revision) (flutter/engine#52359)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
autosubmit Merge PR when tree becomes green via auto submit App platform-ios
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants