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
[DRAFT][ios][platform_view]Use CAShapeLayer as the mask to avoid software rendering #52957
Closed
hellohuanlin
wants to merge
1
commit into
flutter:main
from
hellohuanlin:pv_mask_using_cashapelayer_directly
Closed
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -188,6 +188,20 @@ @interface ChildClippingView () | |
|
||
@implementation ChildClippingView | ||
|
||
- (id<CAAction>)actionForLayer:(CALayer*)layer forKey:(NSString*)event { | ||
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. 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. | ||
|
@@ -243,7 +257,7 @@ - (NSMutableArray*)backdropFilterSubviews { | |
|
||
@end | ||
|
||
@interface FlutterClippingMaskView () | ||
@interface FlutterClippingMaskLayer () | ||
|
||
// A `CATransform3D` matrix represnts a scale transform that revese UIScreen.scale. | ||
// | ||
|
@@ -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. | ||
|
@@ -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 { | ||
|
@@ -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 { | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
@@ -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) { | ||
|
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.
This is obviously still a draft since I haven't renamed
mask_view_pool
tomask_layer_pool
etc.