Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Compositor.CreateCompositionVisualSnapshot API #16599

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions api/Avalonia.Skia.nupkg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0006</DiagnosticId>
<Target>M:Avalonia.Skia.ISkiaGpuWithPlatformGraphicsContext.TryGetGrContext</Target>
<Left>baseline/netstandard2.0/Avalonia.Skia.dll</Left>
<Right>target/netstandard2.0/Avalonia.Skia.dll</Right>
</Suppression>
</Suppressions>
5 changes: 4 additions & 1 deletion samples/ControlCatalog/Pages/OpenGlPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
x:Class="ControlCatalog.Pages.OpenGlPage"
xmlns:pages="using:ControlCatalog.Pages"
xmlns:openGl="clr-namespace:ControlCatalog.Pages.OpenGl">
<Grid>
<Grid x:Name="MainGrid">
<pages:OpenGlPageControl x:Name="GL"/>
<openGl:GlPageKnobs x:Name="Knobs" />
<Button x:Name="Snapshot"
IsVisible="False"
VerticalAlignment="Bottom" HorizontalAlignment="Right" Click="SnapshotClick">Snapshot</Button>
</Grid>
</UserControl>
28 changes: 28 additions & 0 deletions samples/ControlCatalog/Pages/OpenGlPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.Runtime.InteropServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.OpenGL;
using Avalonia.OpenGL.Controls;
using Avalonia.Rendering.Composition;
using ControlCatalog.Pages.OpenGl;

// ReSharper disable StringLiteralTypo
Expand All @@ -16,6 +20,30 @@ public OpenGlPage()
AvaloniaXamlLoader.Load(this);
this.FindControl<OpenGlPageControl>("GL")
!.Init(this.FindControl<GlPageKnobs>("Knobs")!);

AttachedToVisualTree += delegate
{
if (TopLevel.GetTopLevel(this) is Window)
this.FindControl<Button>("Snapshot")!.IsVisible = true;
};
}

private async void SnapshotClick(object sender, RoutedEventArgs e)
{
var v = ElementComposition.GetElementVisual(this)!;
var snap = await v.Compositor.CreateCompositionVisualSnapshot(v, 1.5);
await new Window()
{
Content = new ScrollViewer()
{
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
Content = new Image()
{
Source = snap
}
}
}.ShowDialog((Window)TopLevel.GetTopLevel(this));
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/Avalonia.Base/Platform/IDrawingContextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,10 @@ public interface IDrawingContextLayerImpl : IRenderTargetBitmapImpl
/// </summary>
bool CanBlit { get; }
}

public interface IDrawingContextLayerWithRenderContextAffinityImpl : IDrawingContextLayerImpl
{
bool HasRenderContextAffinity { get; }
IBitmapImpl CreateNonAffinedSnapshot();
}
}
8 changes: 8 additions & 0 deletions src/Avalonia.Base/Platform/IPlatformRenderInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ public interface IPlatformRenderInterfaceContext : IOptionalFeatureProvider, IDi
/// </param>
/// <returns>An <see cref="IRenderTarget"/>.</returns>
IRenderTarget CreateRenderTarget(IEnumerable<object> surfaces);

/// <summary>
/// Creates an offscreen render target
/// </summary>
/// <param name="pixelSize">The size, in pixels, of the render target</param>
/// <param name="scaling">The scaling which will be reported by IBitmap.Dpi</param>
/// <returns></returns>
IDrawingContextLayerImpl CreateOffscreenRenderTarget(PixelSize pixelSize, double scaling);

/// <summary>
/// Indicates that the context is no longer usable. This method should be thread-safe
Expand Down
44 changes: 44 additions & 0 deletions src/Avalonia.Base/Platform/IScopedResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Threading;

namespace Avalonia.Platform;

public interface IScopedResource<T> : IDisposable
{
public T Value { get; }
}

public class ScopedResource<T> : IScopedResource<T>
{
private int _disposed = 0;
private T _value;
private Action? _dispose;
private ScopedResource(T value, Action dispose)
{
_value = value;
_dispose = dispose;
}

public static IScopedResource<T> Create(T value, Action dispose) => new ScopedResource<T>(value, dispose);

public void Dispose()
{
if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0)
{
var disp = _dispose!;
_value = default!;
_dispose = null;
disp();
}
}

public T Value
{
get
{
if (_disposed == 1)
throw new ObjectDisposedException(this.GetType().FullName);
return _value;
}
}
}
45 changes: 33 additions & 12 deletions src/Avalonia.Base/Rendering/Composition/Compositor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Avalonia.Animation.Easings;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Metadata;
using Avalonia.Platform;
using Avalonia.Rendering.Composition.Server;
Expand Down Expand Up @@ -34,6 +35,7 @@ public partial class Compositor
private CompositionBatch? _pendingBatch;
private readonly object _pendingBatchLock = new();
private readonly List<Action> _pendingServerCompositorJobs = new();
private readonly List<Action> _pendingServerCompositorPostTargetJobs = new();
private DiagnosticTextRenderer? _diagnosticTextRenderer;
private readonly Action _triggerCommitRequested;

Expand Down Expand Up @@ -170,14 +172,23 @@ CompositionBatch CommitCore()
_disposeOnNextBatch.Clear();
}

if (_pendingServerCompositorJobs.Count > 0)

static void SerializeServerJobs(BatchStreamWriter writer, List<Action> list, object startMarker, object endMarker)
{
writer.WriteObject(ServerCompositor.RenderThreadJobsStartMarker);
foreach (var job in _pendingServerCompositorJobs)
writer.WriteObject(job);
writer.WriteObject(ServerCompositor.RenderThreadJobsEndMarker);
if (list.Count > 0)
{
writer.WriteObject(startMarker);
foreach (var job in list)
writer.WriteObject(job);
writer.WriteObject(endMarker);
}
list.Clear();
}
_pendingServerCompositorJobs.Clear();

SerializeServerJobs(writer, _pendingServerCompositorJobs, ServerCompositor.RenderThreadJobsStartMarker,
ServerCompositor.RenderThreadJobsEndMarker);
SerializeServerJobs(writer, _pendingServerCompositorPostTargetJobs, ServerCompositor.RenderThreadPostTargetJobsStartMarker,
ServerCompositor.RenderThreadPostTargetJobsEndMarker);
}

_nextCommit.CommittedAt = Server.Clock.Elapsed;
Expand Down Expand Up @@ -227,21 +238,21 @@ public void RequestCompositionUpdate(Action action)
RequestCommitAsync();
}

internal void PostServerJob(Action job)
internal void PostServerJob(Action job, bool postTarget = false)
{
Dispatcher.VerifyAccess();
_pendingServerCompositorJobs.Add(job);
(postTarget ? _pendingServerCompositorPostTargetJobs : _pendingServerCompositorJobs).Add(job);
RequestCommitAsync();
}

internal Task InvokeServerJobAsync(Action job) =>
internal Task InvokeServerJobAsync(Action job, bool postTarget = false) =>
InvokeServerJobAsync<object?>(() =>
{
job();
return null;
});
}, postTarget);

internal Task<T> InvokeServerJobAsync<T>(Func<T> job)
internal Task<T> InvokeServerJobAsync<T>(Func<T> job, bool postTarget = false)
{
var tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
PostServerJob(() =>
Expand All @@ -254,7 +265,7 @@ internal Task<T> InvokeServerJobAsync<T>(Func<T> job)
{
tcs.TrySetException(e);
}
});
}, postTarget);
return tcs.Task;
}

Expand All @@ -275,6 +286,16 @@ internal ValueTask<IReadOnlyDictionary<Type, object>> GetRenderInterfacePublicFe
(await GetRenderInterfacePublicFeatures().ConfigureAwait(false)).TryGetValue(featureType, out var rv);
return rv;
}

public async Task<Bitmap> CreateCompositionVisualSnapshot(CompositionVisual visual, double scaling)
{
if (visual.Compositor != this)
throw new InvalidOperationException();
if (visual.Root == null)
throw new InvalidOperationException();
var impl = await InvokeServerJobAsync(() => _server.CreateCompositionVisualSnapshot(visual.Server, scaling), true);
return new Bitmap(RefCountable.Create(impl));
}

/// <summary>
/// Attempts to query for GPU interop feature from the platform render interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ internal partial class ServerCompositionContainerVisual : ServerCompositionVisua
private LtrbRect? _transformedContentBounds;
private IImmutableEffect? _oldEffect;

protected override void RenderCore(CompositorDrawingContextProxy canvas, LtrbRect currentTransformedClip,
IDirtyRectTracker dirtyRects)
protected override void RenderCore(ServerVisualRenderContext context, LtrbRect currentTransformedClip)
{
base.RenderCore(canvas, currentTransformedClip, dirtyRects);
base.RenderCore(context, currentTransformedClip);

foreach (var ch in Children)
{
ch.Render(canvas, currentTransformedClip, dirtyRects);
ch.Render(context, currentTransformedClip);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@ protected override void DeserializeChangesCore(BatchStreamReader reader, TimeSpa
base.DeserializeChangesCore(reader, committedAt);
}

protected override void RenderCore(CompositorDrawingContextProxy canvas, LtrbRect currentTransformedClip,
IDirtyRectTracker dirtyRects)
protected override void RenderCore(ServerVisualRenderContext context, LtrbRect currentTransformedClip)
{
if (_renderCommands != null
&& currentTransformedClip.Intersects(TransformedOwnContentBounds)
&& dirtyRects.Intersects(TransformedOwnContentBounds))
&& context.ShouldRenderOwnContent(this, currentTransformedClip))
{
_renderCommands.Render(canvas);
_renderCommands.Render(context.Canvas);
}

base.RenderCore(canvas, currentTransformedClip, dirtyRects);
base.RenderCore(context, currentTransformedClip);
}

public void DependencyQueuedInvalidate(IServerRenderResource sender) => ValuesInvalidated();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ namespace Avalonia.Rendering.Composition.Server;

internal partial class ServerCompositionExperimentalAcrylicVisual
{
protected override void RenderCore(CompositorDrawingContextProxy canvas, LtrbRect currentTransformedClip,
IDirtyRectTracker dirtyRects)
protected override void RenderCore(ServerVisualRenderContext context, LtrbRect currentTransformedClip)
{
var cornerRadius = CornerRadius;
canvas.DrawRectangle(
context.Canvas.DrawRectangle(
Material,
new RoundedRect(
new Rect(0, 0, Size.X, Size.Y),
cornerRadius.TopLeft, cornerRadius.TopRight,
cornerRadius.BottomRight, cornerRadius.BottomLeft));

base.RenderCore(canvas, currentTransformedClip, dirtyRects);
base.RenderCore(context, currentTransformedClip);
}

public override LtrbRect OwnContentBounds => new(0, 0, Size.X, Size.Y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ namespace Avalonia.Rendering.Composition.Server;

internal partial class ServerCompositionSolidColorVisual
{
protected override void RenderCore(CompositorDrawingContextProxy canvas, LtrbRect currentTransformedClip,
IDirtyRectTracker dirtyRects)
protected override void RenderCore(ServerVisualRenderContext context, LtrbRect currentTransformedClip)
{
canvas.DrawRectangle(new ImmutableSolidColorBrush(Color), null, new Rect(0, 0, Size.X, Size.Y));
context.Canvas.DrawRectangle(new ImmutableSolidColorBrush(Color), null, new Rect(0, 0, Size.X, Size.Y));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ namespace Avalonia.Rendering.Composition.Server;

internal partial class ServerCompositionSurfaceVisual
{
protected override void RenderCore(CompositorDrawingContextProxy canvas, LtrbRect currentTransformedClip,
IDirtyRectTracker dirtyRects)
protected override void RenderCore(ServerVisualRenderContext context, LtrbRect currentTransformedClip)
{
if (Surface == null)
return;
Expand All @@ -15,7 +14,7 @@ protected override void RenderCore(CompositorDrawingContextProxy canvas, LtrbRec
var bmp = Surface.Bitmap.Item;

//TODO: add a way to always render the whole bitmap instead of just assuming 96 DPI
canvas.DrawBitmap(Surface.Bitmap.Item, 1, new Rect(bmp.PixelSize.ToSize(1)), new Rect(
context.Canvas.DrawBitmap(Surface.Bitmap.Item, 1, new Rect(bmp.PixelSize.ToSize(1)), new Rect(
new Size(Size.X, Size.Y)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void AddDirtyRect(LtrbRect rect)
public Rect SnapToDevicePixels(Rect rect) => SnapToDevicePixels(new(rect), Scaling).ToRect();
public LtrbRect SnapToDevicePixels(LtrbRect rect) => SnapToDevicePixels(rect, Scaling);

private static LtrbRect SnapToDevicePixels(LtrbRect rect, double scale)
public static LtrbRect SnapToDevicePixels(LtrbRect rect, double scale)
{
return new LtrbRect(
Math.Floor(rect.Left * scale) / scale,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ void RenderRootToContextWithClip(IDrawingContextImpl context, ServerCompositionV
context.PushLayer(DirtyRects.CombinedRect.ToRectUnscaled());

using (var proxy = new CompositorDrawingContextProxy(context))
root.Render(proxy, null, DirtyRects);
{
var ctx = new ServerVisualRenderContext(proxy, DirtyRects, false);
root.Render(ctx, null);
}

if (useLayerClip)
context.PopLayer();
Expand Down
Loading
Loading