7 #include <QuartzCore/QuartzCore.h>
10 #include "flutter/fml/logging.h"
11 #include "flutter/shell/platform/embedder/embedder.h"
17 FML_CHECK(layer->type == kFlutterLayerContentTypePlatformView);
18 const auto* platform_view = layer->platform_view;
19 identifier_ = platform_view->identifier;
20 for (
size_t i = 0; i < platform_view->mutations_count; i++) {
21 mutations_.push_back(*platform_view->mutations[i]);
23 offset_ = layer->offset;
27 const std::vector<FlutterPlatformViewMutation>& mutations,
30 : identifier_(identifier), mutations_(mutations), offset_(offset), size_(size) {}
72 - (instancetype)initWithFrame:(NSRect)frameRect {
73 if (
self = [super initWithFrame:frameRect]) {
74 self.wantsLayer = YES;
88 - (void)maskToPath:(CGPathRef)path withOrigin:(CGPoint)origin {
89 CAShapeLayer* maskLayer =
self.layer.mask;
90 if (maskLayer == nil) {
91 maskLayer = [CAShapeLayer layer];
92 self.layer.mask = maskLayer;
94 maskLayer.path = path;
95 maskLayer.transform = CATransform3DMakeTranslation(-origin.x, -origin.y, 0);
101 CATransform3D ToCATransform3D(
const FlutterTransformation& t) {
102 CATransform3D transform = CATransform3DIdentity;
103 transform.m11 = t.scaleX;
104 transform.m21 = t.skewX;
105 transform.m41 = t.transX;
106 transform.m14 = t.pers0;
107 transform.m12 = t.skewY;
108 transform.m22 = t.scaleY;
109 transform.m42 = t.transY;
110 transform.m24 = t.pers1;
114 bool AffineTransformIsOnlyScaleOrTranslate(
const CGAffineTransform& transform) {
115 return transform.b == 0 && transform.c == 0;
118 bool IsZeroSize(
const FlutterSize size) {
119 return size.width == 0 && size.height == 0;
122 CGRect FromFlutterRect(
const FlutterRect& rect) {
123 return CGRectMake(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
126 FlutterRect ToFlutterRect(
const CGRect& rect) {
128 .left = rect.origin.x,
129 .top = rect.origin.y,
130 .right = rect.origin.x + rect.size.width,
131 .bottom = rect.origin.y + rect.size.height,
137 bool PointInsideEllipse(
const CGPoint& point,
const FlutterSize& radius) {
138 return (point.x * point.x) / (radius.width * radius.width) +
139 (point.y * point.y) / (radius.height * radius.height) <
143 bool RoundRectCornerIntersects(
const FlutterRoundedRect& roundRect,
const FlutterRect& rect) {
145 CGPoint inner_top_left =
146 CGPointMake(roundRect.rect.left + roundRect.upper_left_corner_radius.width,
147 roundRect.rect.top + roundRect.upper_left_corner_radius.height);
150 CGPoint relative_top_left =
151 CGPointMake(rect.left - inner_top_left.x, rect.top - inner_top_left.y);
154 if (relative_top_left.x < 0 && relative_top_left.y < 0) {
155 if (!PointInsideEllipse(relative_top_left, roundRect.upper_left_corner_radius)) {
161 CGPoint inner_top_right =
162 CGPointMake(roundRect.rect.right - roundRect.upper_right_corner_radius.width,
163 roundRect.rect.top + roundRect.upper_right_corner_radius.height);
166 CGPoint relative_top_right =
167 CGPointMake(rect.right - inner_top_right.x, rect.top - inner_top_right.y);
170 if (relative_top_right.x > 0 && relative_top_right.y < 0) {
171 if (!PointInsideEllipse(relative_top_right, roundRect.upper_right_corner_radius)) {
177 CGPoint inner_bottom_left =
178 CGPointMake(roundRect.rect.left + roundRect.lower_left_corner_radius.width,
179 roundRect.rect.bottom - roundRect.lower_left_corner_radius.height);
182 CGPoint relative_bottom_left =
183 CGPointMake(rect.left - inner_bottom_left.x, rect.bottom - inner_bottom_left.y);
186 if (relative_bottom_left.x < 0 && relative_bottom_left.y > 0) {
187 if (!PointInsideEllipse(relative_bottom_left, roundRect.lower_left_corner_radius)) {
193 CGPoint inner_bottom_right =
194 CGPointMake(roundRect.rect.right - roundRect.lower_right_corner_radius.width,
195 roundRect.rect.bottom - roundRect.lower_right_corner_radius.height);
198 CGPoint relative_bottom_right =
199 CGPointMake(rect.right - inner_bottom_right.x, rect.bottom - inner_bottom_right.y);
202 if (relative_bottom_right.x > 0 && relative_bottom_right.y > 0) {
203 if (!PointInsideEllipse(relative_bottom_right, roundRect.lower_right_corner_radius)) {
211 CGPathRef PathFromRoundedRect(
const FlutterRoundedRect& roundedRect) {
212 if (IsZeroSize(roundedRect.lower_left_corner_radius) &&
213 IsZeroSize(roundedRect.lower_right_corner_radius) &&
214 IsZeroSize(roundedRect.upper_left_corner_radius) &&
215 IsZeroSize(roundedRect.upper_right_corner_radius)) {
216 return CGPathCreateWithRect(FromFlutterRect(roundedRect.rect),
nullptr);
219 CGMutablePathRef path = CGPathCreateMutable();
221 const auto& rect = roundedRect.rect;
222 const auto& topLeft = roundedRect.upper_left_corner_radius;
223 const auto& topRight = roundedRect.upper_right_corner_radius;
224 const auto& bottomLeft = roundedRect.lower_left_corner_radius;
225 const auto& bottomRight = roundedRect.lower_right_corner_radius;
227 CGPathMoveToPoint(path,
nullptr, rect.left + topLeft.width, rect.top);
228 CGPathAddLineToPoint(path,
nullptr, rect.right - topRight.width, rect.top);
229 CGPathAddCurveToPoint(path,
nullptr, rect.right, rect.top, rect.right, rect.top + topRight.height,
230 rect.right, rect.top + topRight.height);
231 CGPathAddLineToPoint(path,
nullptr, rect.right, rect.bottom - bottomRight.height);
232 CGPathAddCurveToPoint(path,
nullptr, rect.right, rect.bottom, rect.right - bottomRight.width,
233 rect.bottom, rect.right - bottomRight.width, rect.bottom);
234 CGPathAddLineToPoint(path,
nullptr, rect.left + bottomLeft.width, rect.bottom);
235 CGPathAddCurveToPoint(path,
nullptr, rect.left, rect.bottom, rect.left,
236 rect.bottom - bottomLeft.height, rect.left,
237 rect.bottom - bottomLeft.height);
238 CGPathAddLineToPoint(path,
nullptr, rect.left, rect.top + topLeft.height);
239 CGPathAddCurveToPoint(path,
nullptr, rect.left, rect.top, rect.left + topLeft.width, rect.top,
240 rect.left + topLeft.width, rect.top);
241 CGPathCloseSubpath(path);
245 using MutationVector = std::vector<FlutterPlatformViewMutation>;
251 MutationVector MutationsForPlatformView(
const MutationVector& mutationsIn,
float scale) {
252 MutationVector mutations(mutationsIn);
254 mutations.insert(mutations.begin(), {
255 .type = kFlutterPlatformViewMutationTypeTransformation,
257 .scaleX = 1.0 / scale,
258 .scaleY = 1.0 / scale,
265 CATransform3D CATransformFromMutations(
const MutationVector& mutations) {
266 CATransform3D transform = CATransform3DIdentity;
267 for (
auto mutation : mutations) {
268 switch (mutation.type) {
269 case kFlutterPlatformViewMutationTypeTransformation: {
270 CATransform3D mutationTransform = ToCATransform3D(mutation.transformation);
271 transform = CATransform3DConcat(mutationTransform, transform);
274 case kFlutterPlatformViewMutationTypeClipRect:
275 case kFlutterPlatformViewMutationTypeClipRoundedRect:
276 case kFlutterPlatformViewMutationTypeOpacity:
284 float OpacityFromMutations(
const MutationVector& mutations) {
286 for (
auto mutation : mutations) {
287 switch (mutation.type) {
288 case kFlutterPlatformViewMutationTypeOpacity:
289 opacity *= mutation.opacity;
291 case kFlutterPlatformViewMutationTypeClipRect:
292 case kFlutterPlatformViewMutationTypeClipRoundedRect:
293 case kFlutterPlatformViewMutationTypeTransformation:
301 CGRect MasterClipFromMutations(CGRect bounds,
const MutationVector& mutations) {
304 CGRect master_clip = bounds;
307 CATransform3D transform = CATransform3DIdentity;
308 for (
auto mutation : mutations) {
309 switch (mutation.type) {
310 case kFlutterPlatformViewMutationTypeClipRect: {
311 CGRect rect = CGRectApplyAffineTransform(FromFlutterRect(mutation.clip_rect),
312 CATransform3DGetAffineTransform(transform));
313 master_clip = CGRectIntersection(rect, master_clip);
316 case kFlutterPlatformViewMutationTypeClipRoundedRect: {
317 CGAffineTransform affineTransform = CATransform3DGetAffineTransform(transform);
318 CGRect rect = CGRectApplyAffineTransform(FromFlutterRect(mutation.clip_rounded_rect.rect),
320 master_clip = CGRectIntersection(rect, master_clip);
323 case kFlutterPlatformViewMutationTypeTransformation:
324 transform = CATransform3DConcat(ToCATransform3D(mutation.transformation), transform);
326 case kFlutterPlatformViewMutationTypeOpacity:
335 FlutterRoundedRect rrect;
336 CGAffineTransform transform;
340 NSMutableArray* ClipPathFromMutations(CGRect master_clip,
const MutationVector& mutations) {
341 std::vector<ClipRoundedRect> rounded_rects;
343 CATransform3D transform = CATransform3DIdentity;
344 for (
auto mutation : mutations) {
345 switch (mutation.type) {
346 case kFlutterPlatformViewMutationTypeClipRoundedRect: {
347 CGAffineTransform affineTransform = CATransform3DGetAffineTransform(transform);
348 rounded_rects.push_back({mutation.clip_rounded_rect, affineTransform});
351 case kFlutterPlatformViewMutationTypeTransformation:
352 transform = CATransform3DConcat(ToCATransform3D(mutation.transformation), transform);
354 case kFlutterPlatformViewMutationTypeClipRect: {
355 CGAffineTransform affineTransform = CATransform3DGetAffineTransform(transform);
357 if (!AffineTransformIsOnlyScaleOrTranslate(affineTransform)) {
358 rounded_rects.push_back(
359 {FlutterRoundedRect{mutation.clip_rect, FlutterSize{0, 0}, FlutterSize{0, 0},
360 FlutterSize{0, 0}, FlutterSize{0, 0}},
365 case kFlutterPlatformViewMutationTypeOpacity:
370 NSMutableArray* paths = [NSMutableArray array];
371 for (
const auto& r : rounded_rects) {
372 bool requiresPath = !AffineTransformIsOnlyScaleOrTranslate(r.transform);
374 CGAffineTransform inverse = CGAffineTransformInvert(r.transform);
377 CGRect localMasterClip = CGRectApplyAffineTransform(master_clip, inverse);
378 requiresPath = RoundRectCornerIntersects(r.rrect, ToFlutterRect(localMasterClip));
384 CGPathRef path = PathFromRoundedRect(r.rrect);
385 CGPathRef transformedPath = CGPathCreateCopyByTransformingPath(path, &r.transform);
386 [paths addObject:(__bridge id)transformedPath];
387 CGPathRelease(transformedPath);
398 return _platformView;
401 - (NSMutableArray*)pathClipViews {
402 return _pathClipViews;
405 - (NSView*)platformViewContainer {
406 return _platformViewContainer;
409 - (instancetype)initWithPlatformView:(NSView*)platformView {
410 if (
self = [super initWithFrame:NSZeroRect]) {
412 _pathClipViews = [NSMutableArray array];
413 self.wantsLayer = YES;
414 self.clipsToBounds = YES;
419 - (NSView*)hitTest:(NSPoint)point {
428 - (CGFloat)contentsScale {
429 return self.superview != nil ?
self.superview.layer.contentsScale : 1.0;
433 - (void)updatePathClipViewsWithPaths:(NSArray*)paths {
435 while (_pathClipViews.count > paths.count) {
436 NSView* view = _pathClipViews.lastObject;
437 [view removeFromSuperview];
438 [_pathClipViews removeLastObject];
441 for (
size_t i = _pathClipViews.count; i < paths.count; ++i) {
442 NSView* superView = _pathClipViews.count == 0 ? self : _pathClipViews.lastObject;
444 [_pathClipViews addObject:pathClipView];
445 [superView addSubview:pathClipView];
448 for (
size_t i = 0; i < _pathClipViews.count; ++i) {
450 pathClipView.frame =
self.bounds;
451 [pathClipView maskToPath:(__bridge CGPathRef)[paths objectAtIndex:i]
452 withOrigin:self.frame.origin];
461 - (void)updatePlatformViewWithBounds:(CGRect)untransformedBounds
462 transformedBounds:(CGRect)transformedBounds
463 transform:(CATransform3D)transform
464 clipRect:(CGRect)clipRect {
466 if (_platformViewContainer == nil) {
468 _platformViewContainer.wantsLayer = YES;
472 NSView* containerSuperview = _pathClipViews.count == 0 ? self : _pathClipViews.lastObject;
473 [containerSuperview addSubview:_platformViewContainer];
474 _platformViewContainer.frame =
self.bounds;
477 [_platformViewContainer addSubview:_platformView];
478 _platformView.frame = untransformedBounds;
481 CATransform3D translation =
482 CATransform3DMakeTranslation(-transformedBounds.origin.x, -transformedBounds.origin.y, 0);
483 transform = CATransform3DConcat(transform, translation);
484 _platformViewContainer.layer.sublayerTransform = transform;
488 if (!CGRectEqualToRect(clipRect, transformedBounds)) {
489 FML_DCHECK(
self.subviews.count == 1);
490 auto subview =
self.subviews.firstObject;
491 FML_DCHECK(subview.frame.origin.x == 0 && subview.frame.origin.y == 0);
492 subview.frame = CGRectMake(transformedBounds.origin.x - clipRect.origin.x,
493 transformedBounds.origin.y - clipRect.origin.y,
494 subview.frame.size.width, subview.frame.size.height);
495 self.frame = clipRect;
502 - (void)applyFlutterLayer:(const
flutter::PlatformViewLayer*)layer {
505 CGFloat scale = [
self contentsScale];
506 MutationVector mutations = MutationsForPlatformView(layer->mutations(), scale);
508 CATransform3D finalTransform = CATransformFromMutations(mutations);
512 CGRect untransformedBoundingRect =
513 CGRectMake(0, 0, layer->size().width / scale, layer->size().height / scale);
514 CGRect finalBoundingRect = CGRectApplyAffineTransform(
515 untransformedBoundingRect, CATransform3DGetAffineTransform(finalTransform));
516 self.frame = finalBoundingRect;
519 self.layer.opacity = OpacityFromMutations(mutations);
522 CGRect masterClip = MasterClipFromMutations(finalBoundingRect, mutations);
523 if (CGRectIsNull(masterClip)) {
530 NSMutableArray* paths = ClipPathFromMutations(masterClip, mutations);
531 [
self updatePathClipViewsWithPaths:paths];
534 [
self updatePlatformViewWithBounds:untransformedBoundingRect
535 transformedBounds:finalBoundingRect
536 transform:finalTransform
537 clipRect:masterClip];