Skip to content

Commit 3ced78d

Browse files
committed
perf(macOS): Avoid calling NSView.Layer getter more than once / method
1 parent a85c49d commit 3ced78d

File tree

5 files changed

+35
-30
lines changed

5 files changed

+35
-30
lines changed

src/Uno.UI/Controls/BindableNSView.macOS.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ public BindableNSView()
5858
{
5959
Initialize();
6060
WantsLayer = true;
61-
if (Layer != null)
61+
var layer = Layer;
62+
if (layer != null)
6263
{
63-
Layer.MasksToBounds = false;
64+
layer.MasksToBounds = false;
6465
}
6566
}
6667

src/Uno.UI/Media/NativeRenderTransformAdapter.iOSmacOS.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ partial void Initialized()
3535
if (Owner is FrameworkElement element && !element.IsLoaded)
3636
{
3737
_isDeferring = true;
38-
element.Layer.Opacity = 0;
38+
var layer = element.Layer;
39+
layer.Opacity = 0;
3940
element.Loaded += DeferredInitialize;
4041

4142
_deferredInitialization = Disposable.Create(CompleteInitialization);
@@ -56,7 +57,7 @@ void CompleteInitialization()
5657
element.Loaded -= DeferredInitialize;
5758

5859
_isDeferring = false;
59-
element.Layer.Opacity = 1;
60+
layer.Opacity = 1;
6061

6162
if (!_isDisposed)
6263
{
@@ -77,13 +78,14 @@ void CompleteInitialization()
7778

7879
private void InitializeOrigin()
7980
{
80-
var oldFrame = Owner.Layer.Frame;
81+
var layer = Owner.Layer;
82+
var oldFrame = layer.Frame;
8183

82-
Owner.Layer.AnchorPoint = CurrentOrigin;
84+
layer.AnchorPoint = CurrentOrigin;
8385

8486
// Restore the old frame to correct the offset potentially introduced by changing AnchorPoint. This is safe to do since we know
8587
// that the transform is currently identity.
86-
Owner.Layer.Frame = oldFrame;
88+
layer.Frame = oldFrame;
8789
}
8890

8991
partial void Apply(bool isSizeChanged, bool isOriginChanged)
@@ -94,14 +96,15 @@ partial void Apply(bool isSizeChanged, bool isOriginChanged)
9496
return;
9597
}
9698
#endif
99+
var layer = Owner.Layer;
97100
if (Transform.IsAnimating)
98101
{
99102
// While animating the transform, we let the Animator apply the transform by itself, so do not update the Transform
100103
if (!_wasAnimating)
101104
{
102105
// At the beginning of the animation make sure we disable all properties of the transform
103-
Owner.Layer.AnchorPoint = GPUFloatValueAnimator.GetAnchorForAnimation(Transform, CurrentOrigin, CurrentSize);
104-
Owner.Layer.Transform = CoreAnimation.CATransform3D.Identity;
106+
layer.AnchorPoint = GPUFloatValueAnimator.GetAnchorForAnimation(Transform, CurrentOrigin, CurrentSize);
107+
layer.Transform = CoreAnimation.CATransform3D.Identity;
105108

106109
_wasAnimating = true;
107110
}
@@ -112,8 +115,8 @@ partial void Apply(bool isSizeChanged, bool isOriginChanged)
112115
{
113116
// Make sure to fully restore the transform at the end of an animation
114117

115-
Owner.Layer.AnchorPoint = CurrentOrigin;
116-
Owner.Layer.Transform = _transform = Transform.MatrixCore.ToTransform3D();
118+
layer.AnchorPoint = CurrentOrigin;
119+
layer.Transform = _transform = Transform.MatrixCore.ToTransform3D();
117120

118121
_wasAnimating = false;
119122

@@ -124,15 +127,15 @@ partial void Apply(bool isSizeChanged, bool isOriginChanged)
124127
{
125128
// As we use the 'AnchorPoint', the transform matrix is independent of the size of the control
126129
// But the layouter expects from us that we restore the transform on each measuring phase
127-
Owner.Layer.Transform = _transform;
130+
layer.Transform = _transform;
128131
}
129132
else if (isOriginChanged)
130133
{
131-
Owner.Layer.AnchorPoint = CurrentOrigin;
134+
layer.AnchorPoint = CurrentOrigin;
132135
}
133136
else
134137
{
135-
Owner.Layer.Transform = _transform = Transform.MatrixCore.ToTransform3D();
138+
layer.Transform = _transform = Transform.MatrixCore.ToTransform3D();
136139
}
137140
}
138141

src/Uno.UI/UI/Xaml/Controls/Layouter/Layouter.macOS.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ private IDisposable SettingFrame(View view)
111111

112112
// If NSView.Transform is not identity, then modifying the frame will give undefined behavior. (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/NSView/transform)
113113
// We have either already applied the transform to the new frame, or we will reset the transform straight after.
114+
// FIXME: view.Layer called twice (thrice with above check)
114115
var transform = view.Layer.Transform;
115116
view.Layer.Transform = CATransform3D.Identity;
116117
return Disposable.Create(reapplyTransform);

src/Uno.UI/UI/Xaml/IFrameworkElementImplementation.macOS.tt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,13 @@ namespace <#= mixin.NamespaceName #>
366366
#endif
367367

368368
CoreAnimation.CATransform3D? oldTransform = null;
369-
if (Layer != null && !Layer.Transform.IsIdentity)
369+
var layer = Layer;
370+
if (layer != null && !layer.Transform.IsIdentity)
370371
{
371372
// If NSView.Transform is not identity, then modifying the frame will give undefined behavior. (https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSView_Class/#//apple_ref/occ/instp/NSView/transform)
372373
// We reapply the transform based on the new size straight after.
373-
oldTransform = Layer.Transform;
374-
Layer.Transform = CoreAnimation.CATransform3D.Identity;
374+
oldTransform = layer.Transform;
375+
layer.Transform = CoreAnimation.CATransform3D.Identity;
375376
}
376377

377378
base.Frame = value;
@@ -389,13 +390,13 @@ namespace <#= mixin.NamespaceName #>
389390
else if (oldTransform.HasValue)
390391
{
391392
// We grudgingly support setting the native transform directly without going through RenderTransform.
392-
Layer.Transform = oldTransform.Value;
393+
layer.Transform = oldTransform.Value;
393394
}
394395
}
395396
else if (oldTransform.HasValue)
396397
{
397398
// We grudgingly support setting the native transform directly without going through RenderTransform.
398-
Layer.Transform = oldTransform.Value;
399+
layer.Transform = oldTransform.Value;
399400
}
400401
}
401402
}
@@ -882,17 +883,18 @@ namespace <#= mixin.NamespaceName #>
882883
{
883884
WantsLayer = true;
884885

886+
var layer = Layer;
885887
if(e.NewValue is SolidColorBrush scb)
886888
{
887-
Layer.BackgroundColor = scb.ColorWithOpacity;
889+
layer.BackgroundColor = scb.ColorWithOpacity;
888890

889891
_brushColorChanged.Disposable = scb.RegisterDisposablePropertyChangedCallback(
890892
SolidColorBrush.ColorProperty,
891-
(s, colorArg) => Layer.BackgroundColor = (s as SolidColorBrush).ColorWithOpacity
893+
(s, colorArg) => layer.BackgroundColor = (s as SolidColorBrush).ColorWithOpacity
892894
);
893895
_brushOpacityChanged.Disposable = scb.RegisterDisposablePropertyChangedCallback(
894896
SolidColorBrush.OpacityProperty,
895-
(s, colorArg) => Layer.BackgroundColor = (s as SolidColorBrush).ColorWithOpacity
897+
(s, colorArg) => layer.BackgroundColor = (s as SolidColorBrush).ColorWithOpacity
896898
);
897899
}
898900
else
@@ -901,15 +903,15 @@ namespace <#= mixin.NamespaceName #>
901903
_brushOpacityChanged.Disposable = null;
902904
if(e.NewValue is GradientBrush gb)
903905
{
904-
Layer.BackgroundColor = gb.FallbackColorWithOpacity;
906+
layer.BackgroundColor = gb.FallbackColorWithOpacity;
905907
}
906908
else
907909
{
908910
if (this.Log().IsEnabled(Uno.Foundation.Logging.LogLevel.Warning))
909911
{
910912
this.Log().Warn("Brush is not a SolidColorBrush, ignoring");
911913
}
912-
Layer.BackgroundColor = SolidColorBrushHelper.Transparent.Color;
914+
layer.BackgroundColor = SolidColorBrushHelper.Transparent.Color;
913915
}
914916
}
915917
}

src/Uno.UI/UI/Xaml/UIElement.macOS.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,16 @@ partial void ApplyNativeClip(Rect rect)
262262
{
263263
if (!ClippingIsSetByCornerRadius)
264264
{
265-
if (Layer != null)
266-
{
267-
this.Layer.Mask = null;
268-
}
265+
Layer.Mask = null;
269266
}
270267
return;
271268
}
272269

273270
WantsLayer = true;
274-
if (Layer != null)
271+
var layer = Layer;
272+
if (layer != null)
275273
{
276-
this.Layer.Mask = new CAShapeLayer
274+
layer.Mask = new CAShapeLayer
277275
{
278276
Path = CGPath.FromRect(rect.ToCGRect())
279277
};

0 commit comments

Comments
 (0)