7 #include "flutter/display_list/effects/dl_image_filter.h"
8 #include "flutter/fml/platform/darwin/cf_utils.h"
19 const fml::scoped_nsobject<UIView>& overlay_view,
20 const fml::scoped_nsobject<UIView>& overlay_view_wrapper,
21 std::unique_ptr<IOSSurface> ios_surface,
22 std::unique_ptr<Surface> surface)
23 : overlay_view(overlay_view),
24 overlay_view_wrapper(overlay_view_wrapper),
25 ios_surface(std::move(ios_surface)),
26 surface(std::move(surface)){};
33 mask_view_pool_.reset(
40 return weak_factory_->GetWeakPtr();
45 CATransform3D transform = CATransform3DIdentity;
46 transform.m11 = matrix.getScaleX();
47 transform.m21 = matrix.getSkewX();
48 transform.m41 = matrix.getTranslateX();
49 transform.m14 = matrix.getPerspX();
51 transform.m12 = matrix.getSkewY();
52 transform.m22 = matrix.getScaleY();
53 transform.m42 = matrix.getTranslateY();
54 transform.m24 = matrix.getPerspY();
60 layer.anchorPoint = CGPointZero;
61 layer.position = CGPointZero;
65 return CGRectMake(clipSkRect.fLeft, clipSkRect.fTop, clipSkRect.fRight - clipSkRect.fLeft,
66 clipSkRect.fBottom - clipSkRect.fTop);
70 const CGFloat epsilon = 0.01;
71 return radius1 - radius2 < epsilon;
79 @property(nonatomic) BOOL backdropFilterViewConfigured;
84 - (void)updateVisualEffectView:(UIVisualEffectView*)visualEffectView;
98 blurRadius:(CGFloat)blurRadius
99 visualEffectView:(UIVisualEffectView*)visualEffectView {
100 if (
self = [super init]) {
105 FML_DLOG(ERROR) <<
"Apple's API for UIVisualEffectView changed. Update the implementation to "
106 "access the gaussianBlur CAFilter.";
109 _backdropFilterView = visualEffectView;
110 _backdropFilterViewConfigured = NO;
122 + (void)prepareOnce:(UIVisualEffectView*)visualEffectView {
126 for (NSUInteger i = 0; i < visualEffectView.subviews.count; i++) {
127 UIView* view = visualEffectView.subviews[i];
128 if ([NSStringFromClass([view
class]) hasSuffix:
@"BackdropView"]) {
130 for (NSObject* filter in view.layer.filters) {
131 if ([[filter valueForKey:
@"name"] isEqual:
@"gaussianBlur"] &&
132 [[filter valueForKey:
@"inputRadius"] isKindOfClass:[NSNumber class]]) {
133 _gaussianBlurFilter = filter;
137 }
else if ([NSStringFromClass([view
class]) hasSuffix:
@"VisualEffectSubview"]) {
144 + (BOOL)isUIVisualEffectViewImplementationValid {
149 FML_DCHECK(_backdropFilterView);
150 if (!
self.backdropFilterViewConfigured) {
151 [
self updateVisualEffectView:_backdropFilterView];
152 self.backdropFilterViewConfigured = YES;
154 return _backdropFilterView;
157 - (void)updateVisualEffectView:(UIVisualEffectView*)visualEffectView {
158 NSObject* gaussianBlurFilter = [_gaussianBlurFilter copy];
159 FML_DCHECK(gaussianBlurFilter);
160 UIView* backdropView = visualEffectView.subviews[_indexOfBackdropView];
161 [gaussianBlurFilter setValue:@(_blurRadius) forKey:@"inputRadius"];
162 backdropView.layer.filters = @[ gaussianBlurFilter ];
164 UIView* visualEffectSubview = visualEffectView.subviews[_indexOfVisualEffectSubview];
165 visualEffectSubview.layer.backgroundColor = UIColor.clearColor.CGColor;
166 visualEffectView.frame = _frame;
168 self.backdropFilterView = visualEffectView;
175 @property(nonatomic, copy) NSArray<PlatformViewFilter*>* filters;
185 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event {
186 for (UIView* view in
self.subviews) {
187 if ([view pointInside:[
self convertPoint:point toView:view] withEvent:event]) {
195 FML_DCHECK(
self.filters.count ==
self.backdropFilterSubviews.count);
196 if (
self.filters.count == 0 && filters.count == 0) {
199 self.filters = filters;
200 NSUInteger index = 0;
201 for (index = 0; index <
self.filters.count; index++) {
202 UIVisualEffectView* backdropFilterView;
206 [
self addSubview:backdropFilterView];
207 [
self.backdropFilterSubviews addObject:backdropFilterView];
209 [filter updateVisualEffectView:self.backdropFilterSubviews[index]];
213 [
self.backdropFilterSubviews[i - 1] removeFromSuperview];
214 [
self.backdropFilterSubviews removeLastObject];
219 if (!_backdropFilterSubviews) {
220 _backdropFilterSubviews = [[NSMutableArray alloc] init];
222 return _backdropFilterSubviews;
237 @property(nonatomic) CATransform3D reverseScreenScale;
239 - (void)addTransformedPath:(CGPathRef)path matrix:(CATransform3D)matrix;
244 CGMutablePathRef pathSoFar_;
248 return [
self initWithFrame:frame screenScale:[UIScreen mainScreen].scale];
251 - (instancetype)
initWithFrame:(CGRect)frame screenScale:(CGFloat)screenScale {
253 self.backgroundColor = UIColor.clearColor;
254 _reverseScreenScale = CATransform3DMakeScale(1 / screenScale, 1 / screenScale, 1);
255 pathSoFar_ = CGPathCreateMutable();
260 + (Class)layerClass {
261 return [CAShapeLayer class];
264 - (CAShapeLayer*)shapeLayer {
265 return (CAShapeLayer*)
self.layer;
269 CGPathRelease(pathSoFar_);
270 pathSoFar_ = CGPathCreateMutable();
271 [
self shapeLayer].path = nil;
272 [
self setNeedsDisplay];
276 CGPathRelease(pathSoFar_);
284 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event {
288 - (void)clipRect:(const SkRect&)clipSkRect matrix:(const SkMatrix&)matrix {
290 CGPathRef path = CGPathCreateWithRect(clipRect, nil);
292 CATransform3D matrixInPoints =
294 [
self addTransformedPath:path matrix:matrixInPoints];
297 - (void)clipRRect:(const SkRRect&)clipSkRRect matrix:(const SkMatrix&)matrix {
298 CGPathRef pathRef =
nullptr;
299 switch (clipSkRRect.getType()) {
300 case SkRRect::kEmpty_Type: {
303 case SkRRect::kRect_Type: {
307 case SkRRect::kOval_Type:
308 case SkRRect::kSimple_Type: {
310 pathRef = CGPathCreateWithRoundedRect(clipRect, clipSkRRect.getSimpleRadii().x(),
311 clipSkRRect.getSimpleRadii().y(), nil);
314 case SkRRect::kNinePatch_Type:
315 case SkRRect::kComplex_Type: {
316 CGMutablePathRef mutablePathRef = CGPathCreateMutable();
318 SkRect clipSkRect = clipSkRRect.rect();
319 SkVector topLeftRadii = clipSkRRect.radii(SkRRect::kUpperLeft_Corner);
320 SkVector topRightRadii = clipSkRRect.radii(SkRRect::kUpperRight_Corner);
321 SkVector bottomRightRadii = clipSkRRect.radii(SkRRect::kLowerRight_Corner);
322 SkVector bottomLeftRadii = clipSkRRect.radii(SkRRect::kLowerLeft_Corner);
326 CGPathMoveToPoint(mutablePathRef, nil, clipSkRect.fLeft + topLeftRadii.x(), clipSkRect.fTop);
328 CGPathAddLineToPoint(mutablePathRef, nil, clipSkRect.fRight - topRightRadii.x(),
330 CGPathAddCurveToPoint(mutablePathRef, nil, clipSkRect.fRight, clipSkRect.fTop,
331 clipSkRect.fRight, clipSkRect.fTop + topRightRadii.y(),
332 clipSkRect.fRight, clipSkRect.fTop + topRightRadii.y());
334 CGPathAddLineToPoint(mutablePathRef, nil, clipSkRect.fRight,
335 clipSkRect.fBottom - bottomRightRadii.y());
336 CGPathAddCurveToPoint(mutablePathRef, nil, clipSkRect.fRight, clipSkRect.fBottom,
337 clipSkRect.fRight - bottomRightRadii.x(), clipSkRect.fBottom,
338 clipSkRect.fRight - bottomRightRadii.x(), clipSkRect.fBottom);
340 CGPathAddLineToPoint(mutablePathRef, nil, clipSkRect.fLeft + bottomLeftRadii.x(),
342 CGPathAddCurveToPoint(mutablePathRef, nil, clipSkRect.fLeft, clipSkRect.fBottom,
343 clipSkRect.fLeft, clipSkRect.fBottom - bottomLeftRadii.y(),
344 clipSkRect.fLeft, clipSkRect.fBottom - bottomLeftRadii.y());
346 CGPathAddLineToPoint(mutablePathRef, nil, clipSkRect.fLeft,
347 clipSkRect.fTop + topLeftRadii.y());
348 CGPathAddCurveToPoint(mutablePathRef, nil, clipSkRect.fLeft, clipSkRect.fTop,
349 clipSkRect.fLeft + topLeftRadii.x(), clipSkRect.fTop,
350 clipSkRect.fLeft + topLeftRadii.x(), clipSkRect.fTop);
351 CGPathCloseSubpath(mutablePathRef);
353 pathRef = mutablePathRef;
358 CATransform3D matrixInPoints =
363 [
self addTransformedPath:pathRef matrix:matrixInPoints];
366 - (void)clipPath:(const SkPath&)path matrix:(const SkMatrix&)matrix {
367 if (!path.isValid()) {
370 if (path.isEmpty()) {
373 CGMutablePathRef pathRef = CGPathCreateMutable();
376 SkPath::Iter iter(path,
true);
377 SkPoint pts[kMaxPointsInVerb];
378 SkPath::Verb verb = iter.next(pts);
379 SkPoint last_pt_from_last_verb = SkPoint::Make(0, 0);
380 while (verb != SkPath::kDone_Verb) {
381 if (verb == SkPath::kLine_Verb || verb == SkPath::kQuad_Verb || verb == SkPath::kConic_Verb ||
382 verb == SkPath::kCubic_Verb) {
383 FML_DCHECK(last_pt_from_last_verb == pts[0]);
386 case SkPath::kMove_Verb: {
387 CGPathMoveToPoint(pathRef, nil, pts[0].x(), pts[0].y());
388 last_pt_from_last_verb = pts[0];
391 case SkPath::kLine_Verb: {
392 CGPathAddLineToPoint(pathRef, nil, pts[1].x(), pts[1].y());
393 last_pt_from_last_verb = pts[1];
396 case SkPath::kQuad_Verb: {
397 CGPathAddQuadCurveToPoint(pathRef, nil, pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
398 last_pt_from_last_verb = pts[2];
401 case SkPath::kConic_Verb: {
405 CGPathAddQuadCurveToPoint(pathRef, nil, pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y());
406 last_pt_from_last_verb = pts[2];
409 case SkPath::kCubic_Verb: {
410 CGPathAddCurveToPoint(pathRef, nil, pts[1].x(), pts[1].y(), pts[2].x(), pts[2].y(),
411 pts[3].x(), pts[3].y());
412 last_pt_from_last_verb = pts[3];
415 case SkPath::kClose_Verb: {
416 CGPathCloseSubpath(pathRef);
419 case SkPath::kDone_Verb: {
423 verb = iter.next(pts);
426 CATransform3D matrixInPoints =
428 [
self addTransformedPath:pathRef matrix:matrixInPoints];
431 - (void)addTransformedPath:(CGPathRef)path matrix:(CATransform3D)matrix {
432 CGAffineTransform affine =
433 CGAffineTransformMake(matrix.m11, matrix.m12, matrix.m21, matrix.m22, matrix.m41, matrix.m42);
434 CGPathAddPath(pathSoFar_, &affine, path);
435 [
self shapeLayer].path = pathSoFar_;
445 @property(nonatomic) NSUInteger capacity;
449 @property(nonatomic) NSMutableSet<FlutterClippingMaskView*>* pool;
455 - (instancetype)initWithCapacity:(NSInteger)capacity {
456 if (
self = [super init]) {
459 _pool = [[NSMutableSet alloc] initWithCapacity:1];
460 _capacity = capacity;
466 FML_DCHECK(
self.pool.count <=
self.capacity);
467 if (
self.pool.count == 0) {
470 screenScale:UIScreen.mainScreen.scale];
473 maskView.frame = frame;
475 [
self.pool removeObject:maskView];
480 FML_DCHECK(![
self.pool containsObject:maskView]);
481 FML_DCHECK(
self.pool.count <=
self.capacity);
482 if (
self.pool.count ==
self.capacity) {
485 [
self.pool addObject:maskView];