Skip to content
Draft
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
2 changes: 2 additions & 0 deletions samples/RenderDemo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
</MenuItem>
<MenuItem Header="Tests">
<MenuItem Command="{Binding ResizeWindow}" Header="Resize window" />
<MenuItem x:Name="SetNotTransparencyMenuItem" Header="Set window not Transparency" Click="SetNotTransparencyMenuItem_OnClick"/>
<MenuItem x:Name="SetTransparencyMenuItem" Header="Set window Transparency" Click="SetTransparencyMenuItem_OnClick"/>
</MenuItem>
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
Expand Down
11 changes: 11 additions & 0 deletions samples/RenderDemo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq.Expressions;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Rendering;
using RenderDemo.ViewModels;
Expand Down Expand Up @@ -37,5 +38,15 @@ private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

private void SetNotTransparencyMenuItem_OnClick(object? sender, RoutedEventArgs e)
{
TransparencyLevelHint = [WindowTransparencyLevel.None];
}

private void SetTransparencyMenuItem_OnClick(object? sender, RoutedEventArgs e)
{
TransparencyLevelHint = [WindowTransparencyLevel.Transparent];
}
}
}
4 changes: 2 additions & 2 deletions src/Avalonia.Controls/TopLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ internal TopLevel(ITopLevelImpl impl, IAvaloniaDependencyResolver? dependencyRes

_scaling = ValidateScaling(impl.RenderScaling);
_actualTransparencyLevel = PlatformImpl.TransparencyLevel;






_accessKeyHandler = TryGetService<IAccessKeyHandler>(dependencyResolver);
_inputManager = TryGetService<IInputManager>(dependencyResolver);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Numerics;
using System.Threading;
using Avalonia.Controls;
using Avalonia.OpenGL.Egl;
using Avalonia.Reactive;
using MicroCom.Runtime;
Expand Down Expand Up @@ -51,4 +51,13 @@ public IDisposable BeginTransaction()
Monitor.Exit(_shared.SyncRoot);
});
}

public bool IsTransparency => _transparencyLevel != WindowTransparencyLevel.None;

public void SetTransparencyLevel(WindowTransparencyLevel transparencyLevel)
{
_transparencyLevel = transparencyLevel;
}

private WindowTransparencyLevel _transparencyLevel;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.OpenGL.Egl;
using Avalonia.Platform;
using Avalonia.Win32.DirectX;
using Avalonia.Win32.Interop;
using Avalonia.Win32.WinRT;

using MicroCom.Runtime;

namespace Avalonia.Win32.DComposition;
Expand All @@ -25,6 +31,7 @@ public IDirect3D11TextureRenderTarget CreateRenderTarget(IPlatformGraphicsContex
{
_window ??= new DirectCompositedWindow(_info, _shared);
SetBlur(_blurEffect);
_window.SetTransparencyLevel(_windowTransparencyLevel);

return new DirectCompositedWindowRenderTarget(context, d3dDevice, _shared, _window);
}
Expand All @@ -43,18 +50,28 @@ public void SetBlur(BlurEffect enable)
_blurEffect = enable;
// _window?.SetBlur(enable);
}

public void SetTransparencyLevel(WindowTransparencyLevel transparencyLevel)
{
_windowTransparencyLevel = transparencyLevel;
_window?.SetTransparencyLevel(transparencyLevel);
}

private WindowTransparencyLevel _windowTransparencyLevel;
}

internal class DirectCompositedWindowRenderTarget : IDirect3D11TextureRenderTarget
{
private static readonly Guid IID_ID3D11Texture2D = Guid.Parse("6f15aaf2-d208-4e89-9ab4-489535d34f9c");

private readonly IPlatformGraphicsContext _context;
private readonly DirectCompositionShared _shared;
private readonly DirectCompositedWindow _window;
private readonly IDCompositionVirtualSurface _surface;
private IDCompositionVirtualSurface _surface;
private bool _lost;
private PixelSize _size;
private readonly IUnknown _d3dDevice;
private bool _isSurfaceSupportTransparency;

public DirectCompositedWindowRenderTarget(
IPlatformGraphicsContext context, IntPtr d3dDevice,
Expand All @@ -63,13 +80,25 @@ public DirectCompositedWindowRenderTarget(
_d3dDevice = MicroComRuntime.CreateProxyFor<IUnknown>(d3dDevice, false).CloneReference();

_context = context;
_shared = shared;
_window = window;

using (var surfaceFactory = shared.Device.CreateSurfaceFactory(_d3dDevice))
{
_surface = surfaceFactory.CreateVirtualSurface(1, 1, DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM,
DXGI_ALPHA_MODE.DXGI_ALPHA_MODE_PREMULTIPLIED);
}
CreateSurface(window);
}

[MemberNotNull(nameof(_surface))]
private void CreateSurface(DirectCompositedWindow window)
{
using var surfaceFactory = _shared.Device.CreateSurfaceFactory(_d3dDevice);

const uint initialSize = 1;
var alphaMode = window.IsTransparency ?
DXGI_ALPHA_MODE.DXGI_ALPHA_MODE_PREMULTIPLIED :
DXGI_ALPHA_MODE.DXGI_ALPHA_MODE_IGNORE;
_isSurfaceSupportTransparency = window.IsTransparency;

_surface = surfaceFactory.CreateVirtualSurface(initialSize, initialSize, DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM,
alphaMode);
}

public void Dispose()
Expand All @@ -88,9 +117,19 @@ public unsafe IDirect3D11TextureRenderTargetRenderSession BeginDraw()
bool needsEndDraw = false;
try
{
bool forceResize = false;
if (_window.IsTransparency != _isSurfaceSupportTransparency)
{
_surface.Dispose();

CreateSurface(_window);

forceResize = true;
}

var size = _window.WindowInfo.Size;
var scale = _window.WindowInfo.Scaling;
if (_size != size)
if (forceResize || _size != size)
{
_surface.Resize((ushort)size.Width, (ushort)size.Height);
_size = size;
Expand Down
11 changes: 10 additions & 1 deletion src/Windows/Avalonia.Win32/DirectX/DxgiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,16 @@ private static bool TryCreateAndRegisterCore()
return tcs.Task.Result;
}

public bool RequiresNoRedirectionBitmap => false;
public bool RequiresNoRedirectionBitmap => IsTransparencySupported()
? true
: false;

public IPlatformRenderSurface CreateSurface(EglGlPlatformSurface.IEglWindowGlPlatformSurfaceInfo info) => new DxgiSwapchainWindow(this, info);

public static bool IsTransparencySupported()
{
// We can use the DirectComposited+CreateSwapChainForComposition to create the Transparency window.
return Win32Platform.WindowsVersion >= PlatformConstants.Windows8_1;
}
}
}
Loading