Skip to content

Commit cea4600

Browse files
authored
iOS: Eliminate global in platformviews controller (flutter/engine#56805)
This is a minor refactoring that moves a global bool to a boolean ivar in FlutterPlatformViewsController. The purpose of this variable is simply to avoid the overhead of trying to create backdrop filters if we've ever failed to create one in the past. Given that this class will only ever have the one instance created and held by per engine with the same duration, and that most apps only ever have one engine, the performance win will be identical for most apps. For the few add-to-app cases with multiple engines either at once or over the course of an app's lifetime, the costs associated with firing up an engine are already a far bigger hit than those being saved by this bool. Also migrates from C++ style namespace { ... } to Obj-C style static functions. These are entirely equivalent as both restrict symbols to the current translation unit. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent db142a3 commit cea4600

File tree

1 file changed

+38
-44
lines changed

1 file changed

+38
-44
lines changed

engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsController.mm

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,39 @@
1111
#include "flutter/fml/synchronization/count_down_latch.h"
1212
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h"
1313
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h"
14+
#include "flutter/shell/platform/darwin/ios/framework/Source/overlay_layer_pool.h"
1415
#import "flutter/shell/platform/darwin/ios/ios_surface.h"
1516

16-
namespace {
17-
1817
// The number of frames the rasterizer task runner will continue
1918
// to run on the platform thread after no platform view is rendered.
2019
//
2120
// Note: this is an arbitrary number.
22-
static const int kDefaultMergedLeaseDuration = 10;
21+
static constexpr int kDefaultMergedLeaseDuration = 10;
2322

2423
static constexpr NSUInteger kFlutterClippingMaskViewPoolCapacity = 5;
2524

25+
struct LayerData {
26+
SkRect rect;
27+
int64_t view_id;
28+
int64_t overlay_id;
29+
std::shared_ptr<flutter::OverlayLayer> layer;
30+
};
31+
using LayersMap = std::unordered_map<int64_t, LayerData>;
32+
33+
/// Each of the following structs stores part of the platform view hierarchy according to its
34+
/// ID.
35+
///
36+
/// This data must only be accessed on the platform thread.
37+
struct PlatformViewData {
38+
NSObject<FlutterPlatformView>* view;
39+
FlutterTouchInterceptingView* touch_interceptor;
40+
UIView* root_view;
41+
};
42+
2643
// Converts a SkMatrix to CATransform3D.
2744
//
2845
// Certain fields are ignored in CATransform3D since SkMatrix is 3x3 and CATransform3D is 4x4.
29-
CATransform3D GetCATransform3DFromSkMatrix(const SkMatrix& matrix) {
46+
static CATransform3D GetCATransform3DFromSkMatrix(const SkMatrix& matrix) {
3047
// Skia only supports 2D transform so we don't map z.
3148
CATransform3D transform = CATransform3DIdentity;
3249
transform.m11 = matrix.getScaleX();
@@ -44,13 +61,13 @@ CATransform3D GetCATransform3DFromSkMatrix(const SkMatrix& matrix) {
4461
// Reset the anchor of `layer` to match the transform operation from flow.
4562
//
4663
// The position of the `layer` should be unchanged after resetting the anchor.
47-
void ResetAnchor(CALayer* layer) {
64+
static void ResetAnchor(CALayer* layer) {
4865
// Flow uses (0, 0) to apply transform matrix so we need to match that in Quartz.
4966
layer.anchorPoint = CGPointZero;
5067
layer.position = CGPointZero;
5168
}
5269

53-
CGRect GetCGRectFromSkRect(const SkRect& clipSkRect) {
70+
static CGRect GetCGRectFromSkRect(const SkRect& clipSkRect) {
5471
return CGRectMake(clipSkRect.fLeft, clipSkRect.fTop, clipSkRect.fRight - clipSkRect.fLeft,
5572
clipSkRect.fBottom - clipSkRect.fTop);
5673
}
@@ -63,9 +80,9 @@ CGRect GetCGRectFromSkRect(const SkRect& clipSkRect) {
6380
//
6481
// `platformview_boundingrect` is the final bounding rect of the PlatformView in the coordinate
6582
// space where the PlatformView is displayed.
66-
bool ClipRectContainsPlatformViewBoundingRect(const SkRect& clip_rect,
67-
const SkRect& platformview_boundingrect,
68-
const SkMatrix& transform_matrix) {
83+
static bool ClipRectContainsPlatformViewBoundingRect(const SkRect& clip_rect,
84+
const SkRect& platformview_boundingrect,
85+
const SkMatrix& transform_matrix) {
6986
SkRect transformed_rect = transform_matrix.mapRect(clip_rect);
7087
return transformed_rect.contains(platformview_boundingrect);
7188
}
@@ -78,9 +95,9 @@ bool ClipRectContainsPlatformViewBoundingRect(const SkRect& clip_rect,
7895
//
7996
// `platformview_boundingrect` is the final bounding rect of the PlatformView in the coordinate
8097
// space where the PlatformView is displayed.
81-
bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,
82-
const SkRect& platformview_boundingrect,
83-
const SkMatrix& transform_matrix) {
98+
static bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,
99+
const SkRect& platformview_boundingrect,
100+
const SkMatrix& transform_matrix) {
84101
SkVector upper_left = clip_rrect.radii(SkRRect::Corner::kUpperLeft_Corner);
85102
SkVector upper_right = clip_rrect.radii(SkRRect::Corner::kUpperRight_Corner);
86103
SkVector lower_right = clip_rrect.radii(SkRRect::Corner::kLowerRight_Corner);
@@ -103,27 +120,6 @@ bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,
103120
return transformed_rrect.contains(platformview_boundingrect);
104121
}
105122

106-
struct LayerData {
107-
SkRect rect;
108-
int64_t view_id;
109-
int64_t overlay_id;
110-
std::shared_ptr<flutter::OverlayLayer> layer;
111-
};
112-
113-
using LayersMap = std::unordered_map<int64_t, LayerData>;
114-
115-
/// Each of the following structs stores part of the platform view hierarchy according to its
116-
/// ID.
117-
///
118-
/// This data must only be accessed on the platform thread.
119-
struct PlatformViewData {
120-
NSObject<FlutterPlatformView>* view;
121-
FlutterTouchInterceptingView* touch_interceptor;
122-
UIView* root_view;
123-
};
124-
125-
} // namespace
126-
127123
@interface FlutterPlatformViewsController ()
128124

129125
// The pool of reusable view layers. The pool allows to recycle layer in each frame.
@@ -196,6 +192,11 @@ @interface FlutterPlatformViewsController ()
196192
/// Only accessed from the raster thread.
197193
@property(nonatomic, assign) BOOL hadPlatformViews;
198194

195+
/// Whether blurred backdrop filters can be applied.
196+
///
197+
/// Defaults to YES, but becomes NO if blurred backdrop filters cannot be applied.
198+
@property(nonatomic, assign) BOOL canApplyBlurBackdrop;
199+
199200
/// Populate any missing overlay layers.
200201
///
201202
/// This requires posting a task to the platform thread and blocking on its completion.
@@ -264,14 +265,6 @@ - (void)removeUnusedLayers:(const std::vector<std::shared_ptr<flutter::OverlayLa
264265
- (void)resetFrameState;
265266
@end
266267

267-
namespace flutter {
268-
269-
// TODO(cbracken): Eliminate the use of globals.
270-
// Becomes NO if Apple's API changes and blurred backdrop filters cannot be applied.
271-
BOOL canApplyBlurBackdrop = YES;
272-
273-
} // namespace flutter
274-
275268
@implementation FlutterPlatformViewsController {
276269
// TODO(cbracken): Replace with Obj-C types and use @property declarations to automatically
277270
// synthesize the ivars.
@@ -301,6 +294,7 @@ - (id)init {
301294
_maskViewPool =
302295
[[FlutterClippingMaskViewPool alloc] initWithCapacity:kFlutterClippingMaskViewPoolCapacity];
303296
_hadPlatformViews = NO;
297+
_canApplyBlurBackdrop = YES;
304298
}
305299
return self;
306300
}
@@ -630,7 +624,7 @@ - (void)applyMutators:(const flutter::MutatorsStack&)mutatorsStack
630624
break;
631625
case flutter::kBackdropFilter: {
632626
// Only support DlBlurImageFilter for BackdropFilter.
633-
if (!flutter::canApplyBlurBackdrop || !(*iter)->GetFilterMutation().GetFilter().asBlur()) {
627+
if (!self.canApplyBlurBackdrop || !(*iter)->GetFilterMutation().GetFilter().asBlur()) {
634628
break;
635629
}
636630
CGRect filterRect = GetCGRectFromSkRect((*iter)->GetFilterMutation().GetFilterRect());
@@ -656,7 +650,7 @@ - (void)applyMutators:(const flutter::MutatorsStack&)mutatorsStack
656650
blurRadius:blurRadius
657651
visualEffectView:visualEffectView];
658652
if (!filter) {
659-
flutter::canApplyBlurBackdrop = NO;
653+
self.canApplyBlurBackdrop = NO;
660654
} else {
661655
[blurFilters addObject:filter];
662656
}
@@ -666,7 +660,7 @@ - (void)applyMutators:(const flutter::MutatorsStack&)mutatorsStack
666660
++iter;
667661
}
668662

669-
if (flutter::canApplyBlurBackdrop) {
663+
if (self.canApplyBlurBackdrop) {
670664
[clipView applyBlurBackdropFilters:blurFilters];
671665
}
672666

0 commit comments

Comments
 (0)