Skip to content

Commit

Permalink
Fixed compositor nullreference in WinUI 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Arlodotexe authored and michael-hawker committed Dec 14, 2023
1 parent bef863c commit 8e4bd7a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 12 deletions.
11 changes: 9 additions & 2 deletions components/Media/src/Brushes/BackdropGammaTransferBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,14 @@ protected override void OnConnected()
return;
}

var backdrop = Window.Current.Compositor.CreateBackdropBrush();

#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif

var backdrop = compositor.CreateBackdropBrush();

// Use a Win2D blur affect applied to a CompositionBackdropBrush.
var graphicsEffect = new GammaTransferEffect
Expand All @@ -370,7 +377,7 @@ protected override void OnConnected()
Source = new CompositionEffectSourceParameter("backdrop")
};

var effectFactory = Window.Current.Compositor.CreateEffectFactory(graphicsEffect, new[]
var effectFactory = compositor.CreateEffectFactory(graphicsEffect, new[]
{
"GammaTransfer.AlphaAmplitude",
"GammaTransfer.AlphaExponent",
Expand Down
10 changes: 8 additions & 2 deletions components/Media/src/Brushes/CanvasBrushBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ protected override void OnConnected()
_graphics.RenderingDeviceReplaced -= CanvasDevice_RenderingDeviceReplaced;
}

_graphics = CanvasComposition.CreateCompositionGraphicsDevice(Window.Current.Compositor, _device);
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif

_graphics = CanvasComposition.CreateCompositionGraphicsDevice(compositor, _device);
_graphics.RenderingDeviceReplaced += CanvasDevice_RenderingDeviceReplaced;

// Delay creating composition resources until they're required.
Expand Down Expand Up @@ -95,7 +101,7 @@ protected override void OnConnected()
}
}

_surfaceBrush = Window.Current.Compositor.CreateSurfaceBrush(surface);
_surfaceBrush = compositor.CreateSurfaceBrush(surface);
_surfaceBrush.Stretch = CompositionStretch.Fill;

CompositionBrush = _surfaceBrush;
Expand Down
13 changes: 10 additions & 3 deletions components/Media/src/Brushes/ImageBlendBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,22 @@ private static void OnModeChanged(DependencyObject d, DependencyPropertyChangedE
/// <inheritdoc/>
protected override void OnConnected()
{
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif

// Delay creating composition resources until they're required.
if (CompositionBrush == null && Source != null && Source is BitmapImage bitmap)
{
// Use LoadedImageSurface API to get ICompositionSurface from image uri provided
// If UriSource is invalid, StartLoadFromUri will return a blank texture.
_surface = LoadedImageSurface.StartLoadFromUri(bitmap.UriSource);


// Load Surface onto SurfaceBrush
_surfaceBrush = Window.Current.Compositor.CreateSurfaceBrush(_surface);
_surfaceBrush = compositor.CreateSurfaceBrush(_surface);
_surfaceBrush.Stretch = CompositionStretchFromStretch(Stretch);

#if WINUI2
Expand All @@ -150,7 +157,7 @@ protected override void OnConnected()
return;
}

var backdrop = Window.Current.Compositor.CreateBackdropBrush();
var backdrop = compositor.CreateBackdropBrush();

// Use a Win2D invert affect applied to a CompositionBackdropBrush.
var graphicsEffect = new CanvasBlendEffect
Expand All @@ -161,7 +168,7 @@ protected override void OnConnected()
Foreground = new CompositionEffectSourceParameter("image")
};

var effectFactory = Window.Current.Compositor.CreateEffectFactory(graphicsEffect);
var effectFactory = compositor.CreateEffectFactory(graphicsEffect);
var effectBrush = effectFactory.CreateBrush();

effectBrush.SetSourceParameter("backdrop", backdrop);
Expand Down
7 changes: 6 additions & 1 deletion components/Media/src/Helpers/SurfaceLoader.Instance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public sealed partial class SurfaceLoader : IDisposable
/// <returns>A <see cref="SurfaceLoader"/> instance to use in the current window</returns>
public static SurfaceLoader GetInstance()
{
return GetInstance(Window.Current.Compositor);
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif
return GetInstance(compositor);
}

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions components/Media/src/Helpers/SurfaceLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public sealed partial class SurfaceLoader
/// <returns>A <see cref="Task{T}"/> that returns the loaded <see cref="CompositionBrush"/> instance</returns>
public static async Task<CompositionBrush?> LoadImageAsync(Uri uri, DpiMode dpiMode, CacheMode cacheMode = CacheMode.Default)
{
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif

// Lock and check the cache first
using (await Win2DMutex.LockAsync())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ public static PipelineBuilder FromBackdrop()
{
ValueTask<CompositionBrush> Factory()
{
var brush = BackdropBrushCache.GetValue(Window.Current.Compositor, c => c.CreateBackdropBrush());
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif
var brush = BackdropBrushCache.GetValue(compositor, c => c.CreateBackdropBrush());

return new ValueTask<CompositionBrush>(brush);
}
Expand Down
18 changes: 15 additions & 3 deletions components/Media/src/Pipelines/PipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ private PipelineBuilder(
[Pure]
public async Task<CompositionBrush> BuildAsync()
{
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif

var effect = await this.sourceProducer() as IGraphicsEffect;

// Validate the pipeline
Expand All @@ -170,8 +176,8 @@ public async Task<CompositionBrush> BuildAsync()

// Build the effects factory
var factory = this.animationProperties.Count > 0
? Window.Current.Compositor.CreateEffectFactory(effect, this.animationProperties)
: Window.Current.Compositor.CreateEffectFactory(effect);
? compositor.CreateEffectFactory(effect, this.animationProperties)
: compositor.CreateEffectFactory(effect);

// Create the effect factory and apply the final effect
var effectBrush = factory.CreateBrush();
Expand All @@ -191,7 +197,13 @@ public async Task<CompositionBrush> BuildAsync()
/// <returns>A <see cref="Task{T}"/> that returns the final <see cref="SpriteVisual"/> instance to use</returns>
public async Task<SpriteVisual> AttachAsync(UIElement target, UIElement? reference = null)
{
SpriteVisual visual = Window.Current.Compositor.CreateSpriteVisual();
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif

SpriteVisual visual = compositor.CreateSpriteVisual();

visual.Brush = await BuildAsync();

Expand Down

0 comments on commit 8e4bd7a

Please sign in to comment.