Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm
Original file line number Diff line number Diff line change
Expand Up @@ -437,14 +437,14 @@ static bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,
}

void FlutterPlatformViewsController::ClipViewSetMaskView(UIView* clipView) {
if (clipView.maskView) {
if (clipView.layer.mask) {
return;
}
UIView* flutterView = flutter_view_.get();
CGRect frame =
CGRectMake(-clipView.frame.origin.x, -clipView.frame.origin.y,
CGRectGetWidth(flutterView.bounds), CGRectGetHeight(flutterView.bounds));
clipView.maskView = [mask_view_pool_.get() getMaskViewWithFrame:frame];
clipView.layer.mask = [mask_view_pool_.get() getMaskViewWithFrame:frame];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is obviously still a draft since I haven't renamed mask_view_pool to mask_layer_pool etc.

}

// This method is only called when the `embedded_view` needs to be re-composited at the current
Expand All @@ -461,11 +461,12 @@ static bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,

SkMatrix transformMatrix;
NSMutableArray* blurFilters = [[[NSMutableArray alloc] init] autorelease];
FML_DCHECK(!clipView.maskView ||
[clipView.maskView isKindOfClass:[FlutterClippingMaskView class]]);
if (clipView.maskView) {
[mask_view_pool_.get() insertViewToPoolIfNeeded:(FlutterClippingMaskView*)(clipView.maskView)];
clipView.maskView = nil;
FML_DCHECK(!clipView.layer.mask ||
[clipView.layer.mask isKindOfClass:[FlutterClippingMaskLayer class]]);
if (clipView.layer.mask) {
[mask_view_pool_.get()
insertViewToPoolIfNeeded:(FlutterClippingMaskLayer*)(clipView.layer.mask)];
clipView.layer.mask = nil;
}
CGFloat screenScale = [UIScreen mainScreen].scale;
auto iter = mutators_stack.Begin();
Expand All @@ -481,8 +482,8 @@ static bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,
break;
}
ClipViewSetMaskView(clipView);
[(FlutterClippingMaskView*)clipView.maskView clipRect:(*iter)->GetRect()
matrix:transformMatrix];
[(FlutterClippingMaskLayer*)clipView.layer.mask clipRect:(*iter)->GetRect()
matrix:transformMatrix];
break;
}
case kClipRRect: {
Expand All @@ -491,17 +492,17 @@ static bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,
break;
}
ClipViewSetMaskView(clipView);
[(FlutterClippingMaskView*)clipView.maskView clipRRect:(*iter)->GetRRect()
matrix:transformMatrix];
[(FlutterClippingMaskLayer*)clipView.layer.mask clipRRect:(*iter)->GetRRect()
matrix:transformMatrix];
break;
}
case kClipPath: {
// TODO(cyanglaz): Find a way to pre-determine if path contains the PlatformView boudning
// rect. See `ClipRRectContainsPlatformViewBoundingRect`.
// https://github.com/flutter/flutter/issues/118650
ClipViewSetMaskView(clipView);
[(FlutterClippingMaskView*)clipView.maskView clipPath:(*iter)->GetPath()
matrix:transformMatrix];
[(FlutterClippingMaskLayer*)clipView.layer.mask clipPath:(*iter)->GetPath()
matrix:transformMatrix];
break;
}
case kOpacity:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2761,42 +2761,42 @@ - (void)testHasFirstResponderInViewHierarchySubtree_descendantViewBecomesFirstRe
XCTAssertFalse(view.flt_hasFirstResponderInViewHierarchySubtree);
}

- (void)testFlutterClippingMaskViewPoolReuseViewsAfterRecycle {
FlutterClippingMaskViewPool* pool = [[FlutterClippingMaskViewPool alloc] initWithCapacity:2];
FlutterClippingMaskView* view1 = [pool getMaskViewWithFrame:CGRectZero];
FlutterClippingMaskView* view2 = [pool getMaskViewWithFrame:CGRectZero];
[pool insertViewToPoolIfNeeded:view1];
[pool insertViewToPoolIfNeeded:view2];
CGRect newRect = CGRectMake(0, 0, 10, 10);
FlutterClippingMaskView* view3 = [pool getMaskViewWithFrame:newRect];
FlutterClippingMaskView* view4 = [pool getMaskViewWithFrame:newRect];
// view3 and view4 should randomly get either of view1 and view2.
NSSet* set1 = [NSSet setWithObjects:view1, view2, nil];
NSSet* set2 = [NSSet setWithObjects:view3, view4, nil];
XCTAssertEqualObjects(set1, set2);
XCTAssertTrue(CGRectEqualToRect(view3.frame, newRect));
XCTAssertTrue(CGRectEqualToRect(view4.frame, newRect));
}

- (void)testFlutterClippingMaskViewPoolAllocsNewMaskViewsAfterReachingCapacity {
FlutterClippingMaskViewPool* pool = [[FlutterClippingMaskViewPool alloc] initWithCapacity:2];
FlutterClippingMaskView* view1 = [pool getMaskViewWithFrame:CGRectZero];
FlutterClippingMaskView* view2 = [pool getMaskViewWithFrame:CGRectZero];
FlutterClippingMaskView* view3 = [pool getMaskViewWithFrame:CGRectZero];
XCTAssertNotEqual(view1, view3);
XCTAssertNotEqual(view2, view3);
}

- (void)testMaskViewsReleasedWhenPoolIsReleased {
__weak UIView* weakView;
@autoreleasepool {
FlutterClippingMaskViewPool* pool = [[FlutterClippingMaskViewPool alloc] initWithCapacity:2];
FlutterClippingMaskView* view = [pool getMaskViewWithFrame:CGRectZero];
weakView = view;
XCTAssertNotNil(weakView);
}
XCTAssertNil(weakView);
}
// - (void)testFlutterClippingMaskViewPoolReuseViewsAfterRecycle {
// FlutterClippingMaskViewPool* pool = [[FlutterClippingMaskViewPool alloc] initWithCapacity:2];
// FlutterClippingMaskView* view1 = [pool getMaskViewWithFrame:CGRectZero];
// FlutterClippingMaskView* view2 = [pool getMaskViewWithFrame:CGRectZero];
// [pool insertViewToPoolIfNeeded:view1];
// [pool insertViewToPoolIfNeeded:view2];
// CGRect newRect = CGRectMake(0, 0, 10, 10);
// FlutterClippingMaskView* view3 = [pool getMaskViewWithFrame:newRect];
// FlutterClippingMaskView* view4 = [pool getMaskViewWithFrame:newRect];
// // view3 and view4 should randomly get either of view1 and view2.
// NSSet* set1 = [NSSet setWithObjects:view1, view2, nil];
// NSSet* set2 = [NSSet setWithObjects:view3, view4, nil];
// XCTAssertEqualObjects(set1, set2);
// XCTAssertTrue(CGRectEqualToRect(view3.frame, newRect));
// XCTAssertTrue(CGRectEqualToRect(view4.frame, newRect));
// }

// - (void)testFlutterClippingMaskViewPoolAllocsNewMaskViewsAfterReachingCapacity {
// FlutterClippingMaskViewPool* pool = [[FlutterClippingMaskViewPool alloc] initWithCapacity:2];
// FlutterClippingMaskView* view1 = [pool getMaskViewWithFrame:CGRectZero];
// FlutterClippingMaskView* view2 = [pool getMaskViewWithFrame:CGRectZero];
// FlutterClippingMaskView* view3 = [pool getMaskViewWithFrame:CGRectZero];
// XCTAssertNotEqual(view1, view3);
// XCTAssertNotEqual(view2, view3);
// }

// - (void)testMaskViewsReleasedWhenPoolIsReleased {
// __weak UIView* weakView;
// @autoreleasepool {
// FlutterClippingMaskViewPool* pool = [[FlutterClippingMaskViewPool alloc] initWithCapacity:2];
// FlutterClippingMaskView* view = [pool getMaskViewWithFrame:CGRectZero];
// weakView = view;
// XCTAssertNotNil(weakView);
// }
// XCTAssertNil(weakView);
// }

- (void)testClipMaskViewIsReused {
flutter::FlutterPlatformViewsTestMockPlatformViewDelegate mock_delegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//
// When a UIView sets a |FlutterClippingMaskView| as its `maskView`, the alpha channel of the UIView
// is replaced with the alpha channel of the |FlutterClippingMaskView|.
@interface FlutterClippingMaskView : UIView
@interface FlutterClippingMaskLayer : CAShapeLayer

- (instancetype)initWithFrame:(CGRect)frame screenScale:(CGFloat)screenScale;

Expand Down Expand Up @@ -66,10 +66,10 @@
- (instancetype)initWithCapacity:(NSInteger)capacity;

// Reuse a maskView from the pool, or allocate a new one.
- (FlutterClippingMaskView*)getMaskViewWithFrame:(CGRect)frame;
- (FlutterClippingMaskLayer*)getMaskViewWithFrame:(CGRect)frame;

// Insert the `maskView` into the pool.
- (void)insertViewToPoolIfNeeded:(FlutterClippingMaskView*)maskView;
- (void)insertViewToPoolIfNeeded:(FlutterClippingMaskLayer*)maskView;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ @interface ChildClippingView ()

@implementation ChildClippingView

- (id<CAAction>)actionForLayer:(CALayer*)layer forKey:(NSString*)event {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

cross ref: #51661 (comment)

// Unlike UIView, CALayer has animation enabled by default.
// Disable all animations for ChildClippingView's layer, such as its `mask` and other animatable
// properties. Note that we should disable animation for the layer being masked, rather than
// FlutterClippingMaskLayer itself. See
// https://developer.apple.com/documentation/quartzcore/calayer/1410844-actionforkey
// We cannot simply use `setDisableActions` since the embedded platform views may have their own
// anmiations.
if (layer == self.layer) {
return [NSNull null];
}
return [super actionForLayer:layer forKey:event];
}

// The ChildClippingView's frame is the bounding rect of the platform view. we only want touches to
// be hit tested and consumed by this view if they are inside the embedded platform view which could
// be smaller the embedded platform view is rotated.
Expand Down Expand Up @@ -243,7 +257,7 @@ - (NSMutableArray*)backdropFilterSubviews {

@end

@interface FlutterClippingMaskView ()
@interface FlutterClippingMaskLayer ()

// A `CATransform3D` matrix represnts a scale transform that revese UIScreen.scale.
//
Expand All @@ -255,31 +269,39 @@ @interface FlutterClippingMaskView ()
// information about screen scale.
@property(nonatomic) CATransform3D reverseScreenScale;

- (fml::CFRef<CGPathRef>)getTransformedPath:(CGPathRef)path matrix:(CATransform3D)matrix;
- (void)addTransformedPath:(CGPathRef)path matrix:(CATransform3D)matrix;

@end

@implementation FlutterClippingMaskView {
std::vector<fml::CFRef<CGPathRef>> paths_;
@implementation FlutterClippingMaskLayer {
CGMutablePathRef pathSoFar_;
}

- (instancetype)initWithFrame:(CGRect)frame {
return [self initWithFrame:frame screenScale:[UIScreen mainScreen].scale];
}

- (instancetype)initWithFrame:(CGRect)frame screenScale:(CGFloat)screenScale {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = UIColor.clearColor;
if (self = [super init]) {
_reverseScreenScale = CATransform3DMakeScale(1 / screenScale, 1 / screenScale, 1);
pathSoFar_ = CGPathCreateMutable();
self.frame = frame;
}
return self;
}

- (void)reset {
paths_.clear();
CGPathRelease(pathSoFar_);
pathSoFar_ = CGPathCreateMutable();
self.path = nil;
[self setNeedsDisplay];
}

- (void)dealloc {
CGPathRelease(pathSoFar_);
[super dealloc];
}

// In some scenarios, when we add this view as a maskView of the ChildClippingView, iOS added
// this view as a subview of the ChildClippingView.
// This results this view blocking touch events on the ChildClippingView.
Expand All @@ -289,28 +311,13 @@ - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event {
return NO;
}

- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);

// For mask view, only the alpha channel is used.
CGContextSetAlpha(context, 1);

for (size_t i = 0; i < paths_.size(); i++) {
CGContextAddPath(context, paths_.at(i));
CGContextClip(context);
}
CGContextFillRect(context, rect);
CGContextRestoreGState(context);
}

- (void)clipRect:(const SkRect&)clipSkRect matrix:(const SkMatrix&)matrix {
CGRect clipRect = flutter::GetCGRectFromSkRect(clipSkRect);
CGPathRef path = CGPathCreateWithRect(clipRect, nil);
// The `matrix` is based on the physical pixels, convert it to UIKit points.
CATransform3D matrixInPoints =
CATransform3DConcat(flutter::GetCATransform3DFromSkMatrix(matrix), _reverseScreenScale);
paths_.push_back([self getTransformedPath:path matrix:matrixInPoints]);
[self addTransformedPath:path matrix:matrixInPoints];
}

- (void)clipRRect:(const SkRRect&)clipSkRRect matrix:(const SkMatrix&)matrix {
Expand Down Expand Up @@ -379,7 +386,7 @@ - (void)clipRRect:(const SkRRect&)clipSkRRect matrix:(const SkMatrix&)matrix {
// TODO(cyanglaz): iOS does not seem to support hard edge on CAShapeLayer. It clearly stated that
// the CAShaperLayer will be drawn antialiased. Need to figure out a way to do the hard edge
// clipping on iOS.
paths_.push_back([self getTransformedPath:pathRef matrix:matrixInPoints]);
[self addTransformedPath:pathRef matrix:matrixInPoints];
}

- (void)clipPath:(const SkPath&)path matrix:(const SkMatrix&)matrix {
Expand Down Expand Up @@ -444,15 +451,15 @@ - (void)clipPath:(const SkPath&)path matrix:(const SkMatrix&)matrix {
// The `matrix` is based on the physical pixels, convert it to UIKit points.
CATransform3D matrixInPoints =
CATransform3DConcat(flutter::GetCATransform3DFromSkMatrix(matrix), _reverseScreenScale);
paths_.push_back([self getTransformedPath:pathRef matrix:matrixInPoints]);
[self addTransformedPath:pathRef matrix:matrixInPoints];
}

- (fml::CFRef<CGPathRef>)getTransformedPath:(CGPathRef)path matrix:(CATransform3D)matrix {
- (void)addTransformedPath:(CGPathRef)path matrix:(CATransform3D)matrix {
CGAffineTransform affine =
CGAffineTransformMake(matrix.m11, matrix.m12, matrix.m21, matrix.m22, matrix.m41, matrix.m42);
CGPathRef transformedPath = CGPathCreateCopyByTransformingPath(path, &affine);
CGPathAddPath(pathSoFar_, &affine, path);
self.path = pathSoFar_;
CGPathRelease(path);
return fml::CFRef<CGPathRef>(transformedPath);
}

@end
Expand All @@ -465,7 +472,7 @@ @interface FlutterClippingMaskViewPool ()

// 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;
@property(retain, nonatomic) NSMutableSet<FlutterClippingMaskLayer*>* pool;

@end

Expand All @@ -481,22 +488,22 @@ - (instancetype)initWithCapacity:(NSInteger)capacity {
return self;
}

- (FlutterClippingMaskView*)getMaskViewWithFrame:(CGRect)frame {
- (FlutterClippingMaskLayer*)getMaskViewWithFrame:(CGRect)frame {
FML_DCHECK(self.pool.count <= self.capacity);
if (self.pool.count == 0) {
// The pool is empty, alloc a new one.
return
[[[FlutterClippingMaskView alloc] initWithFrame:frame
screenScale:[UIScreen mainScreen].scale] autorelease];
[[[FlutterClippingMaskLayer alloc] initWithFrame:frame
screenScale:[UIScreen mainScreen].scale] autorelease];
}
FlutterClippingMaskView* maskView = [[[self.pool anyObject] retain] autorelease];
FlutterClippingMaskLayer* maskView = [[[self.pool anyObject] retain] autorelease];
maskView.frame = frame;
[maskView reset];
[self.pool removeObject:maskView];
return maskView;
}

- (void)insertViewToPoolIfNeeded:(FlutterClippingMaskView*)maskView {
- (void)insertViewToPoolIfNeeded:(FlutterClippingMaskLayer*)maskView {
FML_DCHECK(![self.pool containsObject:maskView]);
FML_DCHECK(self.pool.count <= self.capacity);
if (self.pool.count == self.capacity) {
Expand Down