7 #include <QuartzCore/QuartzCore.h>
10 #include "flutter/fml/logging.h"
11 #include "flutter/shell/platform/embedder/embedder.h"
54 - (instancetype)initWithFrame:(NSRect)frameRect {
55 if (
self = [super initWithFrame:frameRect]) {
56 self.wantsLayer = YES;
70 - (void)maskToPath:(CGPathRef)path withOrigin:(CGPoint)origin {
71 CAShapeLayer* maskLayer =
self.layer.mask;
72 if (maskLayer == nil) {
73 maskLayer = [CAShapeLayer layer];
74 self.layer.mask = maskLayer;
76 maskLayer.path = path;
77 maskLayer.transform = CATransform3DMakeTranslation(-origin.x, -origin.y, 0);
83 CATransform3D ToCATransform3D(
const FlutterTransformation& t) {
84 CATransform3D transform = CATransform3DIdentity;
85 transform.m11 = t.scaleX;
86 transform.m21 = t.skewX;
87 transform.m41 = t.transX;
88 transform.m14 = t.pers0;
89 transform.m12 = t.skewY;
90 transform.m22 = t.scaleY;
91 transform.m42 = t.transY;
92 transform.m24 = t.pers1;
96 bool AffineTransformIsOnlyScaleOrTranslate(
const CGAffineTransform& transform) {
97 return transform.b == 0 && transform.c == 0;
100 bool IsZeroSize(
const FlutterSize size) {
101 return size.width == 0 && size.height == 0;
104 CGRect FromFlutterRect(
const FlutterRect& rect) {
105 return CGRectMake(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
108 FlutterRect ToFlutterRect(
const CGRect& rect) {
110 .left = rect.origin.x,
111 .top = rect.origin.y,
112 .right = rect.origin.x + rect.size.width,
113 .bottom = rect.origin.y + rect.size.height,
119 bool PointInsideEllipse(
const CGPoint& point,
const FlutterSize& radius) {
120 return (point.x * point.x) / (radius.width * radius.width) +
121 (point.y * point.y) / (radius.height * radius.height) <
125 bool RoundRectCornerIntersects(
const FlutterRoundedRect& roundRect,
const FlutterRect& rect) {
127 CGPoint inner_top_left =
128 CGPointMake(roundRect.rect.left + roundRect.upper_left_corner_radius.width,
129 roundRect.rect.top + roundRect.upper_left_corner_radius.height);
132 CGPoint relative_top_left =
133 CGPointMake(rect.left - inner_top_left.x, rect.top - inner_top_left.y);
136 if (relative_top_left.x < 0 && relative_top_left.y < 0) {
137 if (!PointInsideEllipse(relative_top_left, roundRect.upper_left_corner_radius)) {
143 CGPoint inner_top_right =
144 CGPointMake(roundRect.rect.right - roundRect.upper_right_corner_radius.width,
145 roundRect.rect.top + roundRect.upper_right_corner_radius.height);
148 CGPoint relative_top_right =
149 CGPointMake(rect.right - inner_top_right.x, rect.top - inner_top_right.y);
152 if (relative_top_right.x > 0 && relative_top_right.y < 0) {
153 if (!PointInsideEllipse(relative_top_right, roundRect.upper_right_corner_radius)) {
159 CGPoint inner_bottom_left =
160 CGPointMake(roundRect.rect.left + roundRect.lower_left_corner_radius.width,
161 roundRect.rect.bottom - roundRect.lower_left_corner_radius.height);
164 CGPoint relative_bottom_left =
165 CGPointMake(rect.left - inner_bottom_left.x, rect.bottom - inner_bottom_left.y);
168 if (relative_bottom_left.x < 0 && relative_bottom_left.y > 0) {
169 if (!PointInsideEllipse(relative_bottom_left, roundRect.lower_left_corner_radius)) {
175 CGPoint inner_bottom_right =
176 CGPointMake(roundRect.rect.right - roundRect.lower_right_corner_radius.width,
177 roundRect.rect.bottom - roundRect.lower_right_corner_radius.height);
180 CGPoint relative_bottom_right =
181 CGPointMake(rect.right - inner_bottom_right.x, rect.bottom - inner_bottom_right.y);
184 if (relative_bottom_right.x > 0 && relative_bottom_right.y > 0) {
185 if (!PointInsideEllipse(relative_bottom_right, roundRect.lower_right_corner_radius)) {
193 CGPathRef PathFromRoundedRect(
const FlutterRoundedRect& roundedRect) {
194 if (IsZeroSize(roundedRect.lower_left_corner_radius) &&
195 IsZeroSize(roundedRect.lower_right_corner_radius) &&
196 IsZeroSize(roundedRect.upper_left_corner_radius) &&
197 IsZeroSize(roundedRect.upper_right_corner_radius)) {
198 return CGPathCreateWithRect(FromFlutterRect(roundedRect.rect),
nullptr);
201 CGMutablePathRef path = CGPathCreateMutable();
203 const auto& rect = roundedRect.rect;
204 const auto& topLeft = roundedRect.upper_left_corner_radius;
205 const auto& topRight = roundedRect.upper_right_corner_radius;
206 const auto& bottomLeft = roundedRect.lower_left_corner_radius;
207 const auto& bottomRight = roundedRect.lower_right_corner_radius;
209 CGPathMoveToPoint(path,
nullptr, rect.left + topLeft.width, rect.top);
210 CGPathAddLineToPoint(path,
nullptr, rect.right - topRight.width, rect.top);
211 CGPathAddCurveToPoint(path,
nullptr, rect.right, rect.top, rect.right, rect.top + topRight.height,
212 rect.right, rect.top + topRight.height);
213 CGPathAddLineToPoint(path,
nullptr, rect.right, rect.bottom - bottomRight.height);
214 CGPathAddCurveToPoint(path,
nullptr, rect.right, rect.bottom, rect.right - bottomRight.width,
215 rect.bottom, rect.right - bottomRight.width, rect.bottom);
216 CGPathAddLineToPoint(path,
nullptr, rect.left + bottomLeft.width, rect.bottom);
217 CGPathAddCurveToPoint(path,
nullptr, rect.left, rect.bottom, rect.left,
218 rect.bottom - bottomLeft.height, rect.left,
219 rect.bottom - bottomLeft.height);
220 CGPathAddLineToPoint(path,
nullptr, rect.left, rect.top + topLeft.height);
221 CGPathAddCurveToPoint(path,
nullptr, rect.left, rect.top, rect.left + topLeft.width, rect.top,
222 rect.left + topLeft.width, rect.top);
223 CGPathCloseSubpath(path);
227 using MutationVector = std::vector<FlutterPlatformViewMutation>;
233 MutationVector MutationsForPlatformView(
const FlutterPlatformView* view,
float scale) {
234 MutationVector mutations;
235 mutations.reserve(view->mutations_count + 1);
236 mutations.push_back({
237 .type = kFlutterPlatformViewMutationTypeTransformation,
239 .scaleX = 1.0 / scale,
240 .scaleY = 1.0 / scale,
243 for (
size_t i = 0; i < view->mutations_count; ++i) {
244 mutations.push_back(*view->mutations[i]);
250 CATransform3D CATransformFromMutations(
const MutationVector& mutations) {
251 CATransform3D transform = CATransform3DIdentity;
252 for (
auto mutation : mutations) {
253 switch (mutation.type) {
254 case kFlutterPlatformViewMutationTypeTransformation: {
255 CATransform3D mutationTransform = ToCATransform3D(mutation.transformation);
256 transform = CATransform3DConcat(mutationTransform, transform);
259 case kFlutterPlatformViewMutationTypeClipRect:
260 case kFlutterPlatformViewMutationTypeClipRoundedRect:
261 case kFlutterPlatformViewMutationTypeOpacity:
269 float OpacityFromMutations(
const MutationVector& mutations) {
271 for (
auto mutation : mutations) {
272 switch (mutation.type) {
273 case kFlutterPlatformViewMutationTypeOpacity:
274 opacity *= mutation.opacity;
276 case kFlutterPlatformViewMutationTypeClipRect:
277 case kFlutterPlatformViewMutationTypeClipRoundedRect:
278 case kFlutterPlatformViewMutationTypeTransformation:
286 CGRect MasterClipFromMutations(CGRect bounds,
const MutationVector& mutations) {
289 CGRect master_clip = bounds;
292 CATransform3D transform = CATransform3DIdentity;
293 for (
auto mutation : mutations) {
294 switch (mutation.type) {
295 case kFlutterPlatformViewMutationTypeClipRect: {
296 CGRect rect = CGRectApplyAffineTransform(FromFlutterRect(mutation.clip_rect),
297 CATransform3DGetAffineTransform(transform));
298 master_clip = CGRectIntersection(rect, master_clip);
301 case kFlutterPlatformViewMutationTypeClipRoundedRect: {
302 CGAffineTransform affineTransform = CATransform3DGetAffineTransform(transform);
303 CGRect rect = CGRectApplyAffineTransform(FromFlutterRect(mutation.clip_rounded_rect.rect),
305 master_clip = CGRectIntersection(rect, master_clip);
308 case kFlutterPlatformViewMutationTypeTransformation:
309 transform = CATransform3DConcat(ToCATransform3D(mutation.transformation), transform);
311 case kFlutterPlatformViewMutationTypeOpacity:
320 FlutterRoundedRect rrect;
321 CGAffineTransform transform;
325 NSMutableArray* ClipPathFromMutations(CGRect master_clip,
const MutationVector& mutations) {
326 std::vector<ClipRoundedRect> rounded_rects;
328 CATransform3D transform = CATransform3DIdentity;
329 for (
auto mutation : mutations) {
330 switch (mutation.type) {
331 case kFlutterPlatformViewMutationTypeClipRoundedRect: {
332 CGAffineTransform affineTransform = CATransform3DGetAffineTransform(transform);
333 rounded_rects.push_back({mutation.clip_rounded_rect, affineTransform});
336 case kFlutterPlatformViewMutationTypeTransformation:
337 transform = CATransform3DConcat(ToCATransform3D(mutation.transformation), transform);
339 case kFlutterPlatformViewMutationTypeClipRect: {
340 CGAffineTransform affineTransform = CATransform3DGetAffineTransform(transform);
342 if (!AffineTransformIsOnlyScaleOrTranslate(affineTransform)) {
343 rounded_rects.push_back(
344 {FlutterRoundedRect{mutation.clip_rect, FlutterSize{0, 0}, FlutterSize{0, 0},
345 FlutterSize{0, 0}, FlutterSize{0, 0}},
350 case kFlutterPlatformViewMutationTypeOpacity:
355 NSMutableArray* paths = [NSMutableArray array];
356 for (
const auto& r : rounded_rects) {
357 bool requiresPath = !AffineTransformIsOnlyScaleOrTranslate(r.transform);
359 CGAffineTransform inverse = CGAffineTransformInvert(r.transform);
362 CGRect localMasterClip = CGRectApplyAffineTransform(master_clip, inverse);
363 requiresPath = RoundRectCornerIntersects(r.rrect, ToFlutterRect(localMasterClip));
369 CGPathRef path = PathFromRoundedRect(r.rrect);
370 CGPathRef transformedPath = CGPathCreateCopyByTransformingPath(path, &r.transform);
371 [paths addObject:(__bridge id)transformedPath];
372 CGPathRelease(transformedPath);
383 return _platformView;
386 - (NSMutableArray*)pathClipViews {
387 return _pathClipViews;
390 - (NSView*)platformViewContainer {
391 return _platformViewContainer;
394 - (instancetype)initWithPlatformView:(NSView*)
platformView {
395 if (
self = [super initWithFrame:NSZeroRect]) {
397 _pathClipViews = [NSMutableArray array];
398 self.wantsLayer = YES;
399 self.clipsToBounds = YES;
404 - (NSView*)hitTest:(NSPoint)point {
413 - (CGFloat)contentsScale {
414 return self.superview != nil ?
self.superview.layer.contentsScale : 1.0;
418 - (void)updatePathClipViewsWithPaths:(NSArray*)paths {
420 while (_pathClipViews.count > paths.count) {
421 NSView* view = _pathClipViews.lastObject;
422 [view removeFromSuperview];
423 [_pathClipViews removeLastObject];
426 for (
size_t i = _pathClipViews.count; i < paths.count; ++i) {
427 NSView* superView = _pathClipViews.count == 0 ? self : _pathClipViews.lastObject;
429 [_pathClipViews addObject:pathClipView];
430 [superView addSubview:pathClipView];
433 for (
size_t i = 0; i < _pathClipViews.count; ++i) {
435 pathClipView.frame =
self.bounds;
436 [pathClipView maskToPath:(__bridge CGPathRef)[paths objectAtIndex:i]
437 withOrigin:self.frame.origin];
446 - (void)updatePlatformViewWithBounds:(CGRect)untransformedBounds
447 transformedBounds:(CGRect)transformedBounds
448 transform:(CATransform3D)transform
449 clipRect:(CGRect)clipRect {
451 if (_platformViewContainer == nil) {
453 _platformViewContainer.wantsLayer = YES;
457 NSView* containerSuperview = _pathClipViews.count == 0 ? self : _pathClipViews.lastObject;
458 [containerSuperview addSubview:_platformViewContainer];
459 _platformViewContainer.frame =
self.bounds;
462 [_platformViewContainer addSubview:_platformView];
463 _platformView.frame = untransformedBounds;
466 CATransform3D translation =
467 CATransform3DMakeTranslation(-transformedBounds.origin.x, -transformedBounds.origin.y, 0);
468 transform = CATransform3DConcat(transform, translation);
469 _platformViewContainer.layer.sublayerTransform = transform;
473 if (!CGRectEqualToRect(clipRect, transformedBounds)) {
474 FML_DCHECK(
self.subviews.count == 1);
475 auto subview =
self.subviews.firstObject;
476 FML_DCHECK(subview.frame.origin.x == 0 && subview.frame.origin.y == 0);
477 subview.frame = CGRectMake(transformedBounds.origin.x - clipRect.origin.x,
478 transformedBounds.origin.y - clipRect.origin.y,
479 subview.frame.size.width, subview.frame.size.height);
480 self.frame = clipRect;
487 - (void)applyFlutterLayer:(
const FlutterLayer*)layer {
490 CGFloat scale = [
self contentsScale];
491 MutationVector mutations = MutationsForPlatformView(layer->platform_view, scale);
493 CATransform3D finalTransform = CATransformFromMutations(mutations);
497 CGRect untransformedBoundingRect =
498 CGRectMake(0, 0, layer->size.width / scale, layer->size.height / scale);
499 CGRect finalBoundingRect = CGRectApplyAffineTransform(
500 untransformedBoundingRect, CATransform3DGetAffineTransform(finalTransform));
501 self.frame = finalBoundingRect;
504 self.layer.opacity = OpacityFromMutations(mutations);
507 CGRect masterClip = MasterClipFromMutations(finalBoundingRect, mutations);
508 if (CGRectIsNull(masterClip)) {
515 NSMutableArray* paths = ClipPathFromMutations(masterClip, mutations);
516 [
self updatePathClipViewsWithPaths:paths];
519 [
self updatePlatformViewWithBounds:untransformedBoundingRect
520 transformedBounds:finalBoundingRect
521 transform:finalTransform
522 clipRect:masterClip];