Skip to content

Commit

Permalink
perf(brush): Don't use reflection to invoke brush updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Nov 22, 2024
1 parent 40a6745 commit 7c1bf08
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 198 deletions.
142 changes: 0 additions & 142 deletions src/Uno.UI/Helpers/WeakEvents/WeakEventManager.cs

This file was deleted.

5 changes: 4 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/Border/Border.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ private void OnBorderThicknessChanged(Thickness oldValue, Thickness newValue)

#if !__SKIA__
private Action _borderBrushChanged;
private IDisposable _brushChangedSubscription;
#endif

#if __ANDROID__
Expand Down Expand Up @@ -305,7 +306,9 @@ private void OnBorderBrushChanged(Brush oldValue, Brush newValue)
#if __SKIA__
this.UpdateBorderBrush();
#else
Brush.SetupBrushChanged(oldValue, newValue, ref _borderBrushChanged, _borderBrushChanged ?? (() =>
_brushChangedSubscription?.Dispose();

_brushChangedSubscription = Brush.SetupBrushChanged(newValue, ref _borderBrushChanged, _borderBrushChanged ?? (() =>
{
UpdateBorder();
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,11 @@ private IDisposable InnerCreateLayers(
// because even though the brush instance is the same, there are additional properties
// that BorderLayerState tracks on Android. This is not ideal and we should avoid it by refactoring
// this file to handle brush changes on the same brush instance on its own instead.
Brush.SetupBrushChanged(_currentState.Background, background, ref _backgroundChanged, () => Update(true), false);
Brush.SetupBrushChanged(_currentState.BorderBrush, borderBrush, ref _borderChanged, () => Update(true), false);
_backgroundBrushChangedSubscription?.Dispose();
_backgroundBrushChangedSubscription = Brush.SetupBrushChanged(background, ref _backgroundChanged, () => Update(true), false);

_borderBrushChangedSubscription?.Dispose();
_borderBrushChangedSubscription = Brush.SetupBrushChanged(borderBrush, ref _borderChanged, () => Update(true), false);

return disposables;
}
Expand Down
18 changes: 6 additions & 12 deletions src/Uno.UI/UI/Xaml/Controls/Border/BorderLayerRenderer.iOSmacOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ void OnImageChanged(_Image _)
{
Action onInvalidateRender = () => backgroundLayer.FillColor = Brush.GetFallbackColor(background);
onInvalidateRender();
background.InvalidateRender += onInvalidateRender;
new DisposableAction(() => background.InvalidateRender -= onInvalidateRender).DisposeWith(disposables);

background.RegisterInvalidateRender(onInvalidateRender).DisposeWith(disposables);
}
else
{
Expand Down Expand Up @@ -284,8 +284,7 @@ GradientBrush gradientBorder when gradientBorder.CanApplyToBorder(cornerRadius)
else
{
onInvalidateRender();
borderBrush.InvalidateRender += onInvalidateRender;
new DisposableAction(() => borderBrush.InvalidateRender -= onInvalidateRender).DisposeWith(disposables);
borderBrush.RegisterInvalidateRender(onInvalidateRender).DisposeWith(disposables);
}
}

Expand Down Expand Up @@ -334,9 +333,7 @@ GradientBrush gradientBorder when gradientBorder.CanApplyToBorder(cornerRadius)
Action onInvalidateRender = () => backgroundLayer.FillColor = Brush.GetFallbackColor(background);

onInvalidateRender();
background.InvalidateRender += onInvalidateRender;
new DisposableAction(() => background.InvalidateRender -= onInvalidateRender)
.DisposeWith(disposables);
background.RegisterInvalidateRender(onInvalidateRender).DisposeWith(disposables);

// This is required because changing the CornerRadius changes the background drawing
// implementation and we don't want a rectangular background behind a rounded background.
Expand All @@ -362,10 +359,8 @@ imageData.NativeImage is _Image uiImage &&
else if (background is XamlCompositionBrushBase)
{
Action onInvalidateRender = () => backgroundLayer.FillColor = Brush.GetFallbackColor(background);
background.InvalidateRender += onInvalidateRender;
background.RegisterInvalidateRender(onInvalidateRender).DisposeWith(disposables);
onInvalidateRender();
new DisposableAction(() => background.InvalidateRender -= onInvalidateRender)
.DisposeWith(disposables);

// This is required because changing the CornerRadius changes the background drawing
// implementation and we don't want a rectangular background behind a rounded background.
Expand Down Expand Up @@ -417,8 +412,7 @@ GradientBrush gradientBorder when gradientBorder.CanApplyToBorder(cornerRadius)
Action onInvalidateRender = () => layer.FillColor = Brush.GetFallbackColor(borderBrush);

onInvalidateRender();
borderBrush.InvalidateRender += onInvalidateRender;
new DisposableAction(() => borderBrush.InvalidateRender -= onInvalidateRender).DisposeWith(disposables);
borderBrush.RegisterInvalidateRender(onInvalidateRender).DisposeWith(disposables);
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/Uno.UI/UI/Xaml/Controls/Border/BorderLayerRenderer.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ partial class BorderLayerRenderer
{
private Action _backgroundChanged;
private Action _borderChanged;
private IDisposable _borderBrushChangedSubscription;
private IDisposable _brushChangedSubscription;

partial void UpdatePlatform(bool forceUpdate)
{
Expand Down Expand Up @@ -56,8 +58,9 @@ partial void UpdatePlatform(bool forceUpdate)
private void SetAndObserveBorder(BorderLayerState newState)
{
SetBorder(newState);
Brush.SetupBrushChanged(
_currentState.BorderBrush,

_borderBrushChangedSubscription?.Dispose();
_borderBrushChangedSubscription = Brush.SetupBrushChanged(
newState.BorderBrush,
ref _borderChanged,
() =>
Expand Down Expand Up @@ -217,7 +220,8 @@ private void SetAndObserveBackgroundBrush(BorderLayerState newState, ref Action

if (newOnInvalidateRender is not null)
{
Brush.SetupBrushChanged(oldValue, newValue, ref brushChanged, newOnInvalidateRender);
_brushChangedSubscription?.Dispose();
_brushChangedSubscription = Brush.SetupBrushChanged(newValue, ref brushChanged, newOnInvalidateRender);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public partial class TextBlock : DependencyObject, IThemeChangeAware
{
private InlineCollection _inlines;
private string _inlinesText; // Text derived from the content of Inlines
private IDisposable _foregroundBrushChangedSubscription;

#if !__WASM__
// Used for text selection which is handled natively
Expand Down Expand Up @@ -485,7 +486,9 @@ Brush Foreground
private void Subscribe(Brush oldValue, Brush newValue)
{
var newOnInvalidateRender = _foregroundChanged ?? (() => OnForegroundChanged());
Brush.SetupBrushChanged(oldValue, newValue, ref _foregroundChanged, newOnInvalidateRender);

_foregroundBrushChangedSubscription?.Dispose();
_foregroundBrushChangedSubscription = Brush.SetupBrushChanged(newValue, ref _foregroundChanged, newOnInvalidateRender);
}

private void OnForegroundChanged()
Expand Down
5 changes: 4 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ partial class TextBlock : FrameworkElement, IBlock
private readonly TextVisual _textVisual;
private Action? _selectionHighlightColorChanged;
private MenuFlyout? _contextMenu;
private IDisposable? _selectionHighlightBrushChangedSubscription;
private readonly Dictionary<ContextMenuItem, MenuFlyoutItem> _flyoutItems = new();
private readonly VirtualKeyModifiers _platformCtrlKey = OperatingSystem.IsMacOS() ? VirtualKeyModifiers.Windows : VirtualKeyModifiers.Control;

Expand Down Expand Up @@ -357,7 +358,9 @@ private void OnSelectionHighlightColorChanged(SolidColorBrush? oldBrush, SolidCo
{
oldBrush ??= DefaultBrushes.SelectionHighlightColor;
newBrush ??= DefaultBrushes.SelectionHighlightColor;
Brush.SetupBrushChanged(oldBrush, newBrush, ref _selectionHighlightColorChanged, () => OnSelectionHighlightColorChangedPartial(newBrush));

_selectionHighlightBrushChangedSubscription?.Dispose();
_selectionHighlightBrushChangedSubscription = Brush.SetupBrushChanged(newBrush, ref _selectionHighlightColorChanged, () => OnSelectionHighlightColorChangedPartial(newBrush));
}

partial void OnSelectionHighlightColorChangedPartial(SolidColorBrush brush);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public partial class MultilineTextBoxView : UITextView, ITextBoxView, Dependency
private readonly WeakReference<TextBox> _textBox;
private WeakReference<Uno.UI.Controls.Window> _window;
private Action _foregroundChanged;
private IDisposable _foregroundBrushChangedSubscription;

public override void Paste(NSObject sender) => HandlePaste(() => base.Paste(sender));

Expand Down Expand Up @@ -234,7 +235,8 @@ public void OnForegroundChanged(Brush oldValue, Brush newValue)
{
if (newValue is SolidColorBrush scb)
{
Brush.SetupBrushChanged(oldValue, newValue, ref _foregroundChanged, () => ApplyColor());
_foregroundBrushChangedSubscription?.Dispose();
_foregroundBrushChangedSubscription = Brush.SetupBrushChanged(newValue, ref _foregroundChanged, () => ApplyColor());

void ApplyColor()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public partial class SinglelineTextBoxView : UITextField, ITextBoxView, Dependen
private SinglelineTextBoxDelegate _delegate;
private readonly WeakReference<TextBox> _textBox;
private Action _foregroundChanged;
private IDisposable _foregroundBrushChangedSubscription;

public SinglelineTextBoxView(TextBox textBox)
{
Expand Down Expand Up @@ -204,7 +205,8 @@ public void OnForegroundChanged(Brush oldValue, Brush newValue)
{
if (newValue is SolidColorBrush scb)
{
Brush.SetupBrushChanged(oldValue, newValue, ref _foregroundChanged, () => ApplyColor());
_foregroundBrushChangedSubscription?.Dispose();
_foregroundBrushChangedSubscription = Brush.SetupBrushChanged(newValue, ref _foregroundChanged, () => ApplyColor());

void ApplyColor()
{
Expand Down
9 changes: 7 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public partial class TextBox : Control, IFrameworkTemplatePoolAware

private Action _selectionHighlightColorChanged;
private Action _foregroundBrushChanged;
private IDisposable _selectionHighlightBrushChangedSubscription;
private IDisposable _foregroundBrushChangedSubscription;
#pragma warning restore CS0067, CS0649

private ContentPresenter _header;
Expand Down Expand Up @@ -523,7 +525,8 @@ protected override void OnFontWeightChanged(FontWeight oldValue, FontWeight newV

protected override void OnForegroundColorChanged(Brush oldValue, Brush newValue)
{
Brush.SetupBrushChanged(oldValue, newValue, ref _foregroundBrushChanged, () => OnForegroundColorChangedPartial(newValue));
_foregroundBrushChangedSubscription?.Dispose();
_foregroundBrushChangedSubscription = Brush.SetupBrushChanged(newValue, ref _foregroundBrushChanged, () => OnForegroundColorChangedPartial(newValue));
}

partial void OnForegroundColorChangedPartial(Brush newValue);
Expand Down Expand Up @@ -573,7 +576,9 @@ private void OnSelectionHighlightColorChanged(SolidColorBrush oldBrush, SolidCol
{
oldBrush ??= DefaultBrushes.SelectionHighlightColor;
newBrush ??= DefaultBrushes.SelectionHighlightColor;
Brush.SetupBrushChanged(oldBrush, newBrush, ref _selectionHighlightColorChanged, () => OnSelectionHighlightColorChangedPartial(newBrush));

_selectionHighlightBrushChangedSubscription?.Dispose();
_selectionHighlightBrushChangedSubscription = Brush.SetupBrushChanged(newBrush, ref _selectionHighlightColorChanged, () => OnSelectionHighlightColorChangedPartial(newBrush));
}

partial void OnSelectionHighlightColorChangedPartial(SolidColorBrush brush);
Expand Down
3 changes: 2 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ private void OnForegroundChanged(Brush oldValue, Brush newValue)
{
if (newValue is SolidColorBrush scb)
{
Brush.SetupBrushChanged(oldValue, newValue, ref _foregroundChanged, () => ApplyColor());
_foregroundBrushChangedSubscription?.Dispose();
_foregroundBrushChangedSubscription = Brush.SetupBrushChanged(newValue, ref _foregroundChanged, () => ApplyColor());

void ApplyColor()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.macOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ public NSRange SelectedRange

public void OnForegroundChanged(Brush oldValue, Brush newValue)
{
Brush.SetupBrushChanged(oldValue, newValue, ref _foregroundChanged, () => ApplyColor());
_foregroundBrushChangedSubscription?.Dispose();
_foregroundBrushChangedSubscription = Brush.SetupBrushChanged(newValue, ref _foregroundChanged, () => ApplyColor());

void ApplyColor()
{
Expand Down
Loading

0 comments on commit 7c1bf08

Please sign in to comment.