This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[ios_platform_view] only recycle maskView when the view is applying mutators #41573
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
#import "flutter/shell/platform/darwin/ios/ios_surface.h" | ||
|
||
static int kMaxPointsInVerb = 4; | ||
static const NSUInteger kFlutterClippingMaskViewPoolCapacity = 5; | ||
|
||
namespace flutter { | ||
|
||
|
@@ -26,7 +27,10 @@ | |
|
||
FlutterPlatformViewsController::FlutterPlatformViewsController() | ||
: layer_pool_(std::make_unique<FlutterPlatformViewLayerPool>()), | ||
weak_factory_(std::make_unique<fml::WeakPtrFactory<FlutterPlatformViewsController>>(this)){}; | ||
weak_factory_(std::make_unique<fml::WeakPtrFactory<FlutterPlatformViewsController>>(this)) { | ||
mask_view_pool_.reset( | ||
[[FlutterClippingMaskViewPool alloc] initWithCapacity:kFlutterClippingMaskViewPoolCapacity]); | ||
}; | ||
|
||
FlutterPlatformViewsController::~FlutterPlatformViewsController() = default; | ||
|
||
|
@@ -458,58 +462,53 @@ @interface FlutterClippingMaskViewPool () | |
// The maximum number of `FlutterClippingMaskView` the pool can contain. | ||
// This prevents the pool to grow infinately and limits the maximum memory a pool can use. | ||
@property(assign, nonatomic) NSUInteger capacity; | ||
@property(retain, nonatomic) NSMutableArray<FlutterClippingMaskView*>* pool; | ||
// The index points to the first available FlutterClippingMaskView in the `pool`. | ||
@property(assign, nonatomic) NSUInteger availableIndex; | ||
|
||
// The pool contains the views that are available to use. | ||
// The number of items in the pool must not excceds `capacity`. | ||
@property(retain, nonatomic) NSMutableSet<FlutterClippingMaskView*>* pool; | ||
|
||
@end | ||
|
||
@implementation FlutterClippingMaskViewPool : NSObject | ||
|
||
- (instancetype)initWithCapacity:(NSInteger)capacity { | ||
if (self = [super init]) { | ||
_pool = [[NSMutableArray alloc] initWithCapacity:capacity]; | ||
// Most of cases, there are only one PlatformView in the scene. | ||
// Thus init with the capacity of 1. | ||
_pool = [[NSMutableSet alloc] initWithCapacity:1]; | ||
_capacity = capacity; | ||
_availableIndex = 0; | ||
} | ||
return self; | ||
} | ||
|
||
- (FlutterClippingMaskView*)getMaskViewWithFrame:(CGRect)frame { | ||
FML_DCHECK(self.availableIndex <= self.capacity); | ||
FML_DCHECK(self.pool.count <= self.capacity); | ||
FlutterClippingMaskView* maskView; | ||
if (self.availableIndex == self.capacity) { | ||
// The pool is full, alloc a new one. | ||
if (self.pool.count == 0) { | ||
// The pool is empty, alloc a new one. | ||
maskView = | ||
[[[FlutterClippingMaskView alloc] initWithFrame:frame | ||
screenScale:[UIScreen mainScreen].scale] autorelease]; | ||
return maskView; | ||
} | ||
|
||
if (self.availableIndex >= self.pool.count) { | ||
// The pool doesn't have enough maskViews, alloc a new one and add to the pool. | ||
maskView = | ||
[[[FlutterClippingMaskView alloc] initWithFrame:frame | ||
screenScale:[UIScreen mainScreen].scale] autorelease]; | ||
[self.pool addObject:maskView]; | ||
FML_DCHECK(self.pool.count <= self.capacity); | ||
} else { | ||
// Reuse a maskView from the pool. | ||
maskView = [self.pool objectAtIndex:self.availableIndex]; | ||
maskView.frame = frame; | ||
[maskView reset]; | ||
} | ||
self.availableIndex++; | ||
maskView = [self.pool anyObject]; | ||
maskView.frame = frame; | ||
[maskView reset]; | ||
[self.pool removeObject:maskView]; | ||
return maskView; | ||
} | ||
|
||
- (void)recycleMaskViews { | ||
self.availableIndex = 0; | ||
- (void)insertViewToPoolIfNeeded:(FlutterClippingMaskView*)maskView { | ||
FML_DCHECK(![self.pool containsObject:maskView]); | ||
FML_DCHECK(self.pool.count <= self.capacity); | ||
if (self.pool.count == self.capacity) { | ||
return; | ||
} | ||
[self.pool addObject:maskView]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The user of the maskView decides to insert the view back to the pool when they are done with the maskView There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: need to add capacity check here. |
||
} | ||
|
||
- (void)dealloc { | ||
[_pool release]; | ||
_pool = nil; | ||
|
||
[super dealloc]; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you know if this view is available for reuse?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the view is in use, it is removed from the pool:
See line 493 below.