فهرست منبع

glfw window is showing

flabbet 1 سال پیش
والد
کامیت
3db718ca50

+ 1 - 0
src/PixiEditor.AvaloniaUI/PixiEditor.AvaloniaUI.csproj

@@ -89,6 +89,7 @@
     <ProjectReference Include="..\PixiEditor.ChangeableDocument\PixiEditor.ChangeableDocument.csproj"/>
     <ProjectReference Include="..\PixiEditor.DrawingApi.Core\PixiEditor.DrawingApi.Core.csproj"/>
     <ProjectReference Include="..\PixiEditor.DrawingApi.Skia\PixiEditor.DrawingApi.Skia.csproj"/>
+    <ProjectReference Include="..\PixiEditor.Engine\PixiEditor.Engine.csproj" />
     <ProjectReference Include="..\PixiEditor.Extensions.Runtime\PixiEditor.Extensions.Runtime.csproj"/>
     <ProjectReference Include="..\PixiEditor.Extensions.WasmRuntime\PixiEditor.Extensions.WasmRuntime.csproj"/>
     <ProjectReference Include="..\PixiEditor.Extensions\PixiEditor.Extensions.csproj"/>

+ 10 - 5
src/PixiEditor.AvaloniaUI/Views/MainWindow.axaml.cs

@@ -17,10 +17,12 @@ using PixiEditor.AvaloniaUI.Models.IO;
 using PixiEditor.AvaloniaUI.ViewModels.SubViewModels;
 using PixiEditor.DrawingApi.Core.Bridge;
 using PixiEditor.DrawingApi.Skia;
+using PixiEditor.Engine;
 using PixiEditor.Extensions.CommonApi.UserPreferences;
 using PixiEditor.Extensions.Runtime;
 using PixiEditor.Platform;
 using ViewModelMain = PixiEditor.AvaloniaUI.ViewModels.ViewModelMain;
+using Window = Avalonia.Controls.Window;
 
 namespace PixiEditor.AvaloniaUI.Views;
 
@@ -57,10 +59,13 @@ internal partial class MainWindow : Window
 
         AsyncImageLoader.ImageLoader.AsyncImageLoader = new DiskCachedWebImageLoader();
         
-        GRContext recordingContext = GetGrRecordingContext();
+        //GRContext recordingContext = GetGrRecordingContext();
 
-        SkiaDrawingBackend skiaDrawingBackend = new SkiaDrawingBackend(recordingContext);
-        DrawingBackendApi.SetupBackend(skiaDrawingBackend);
+        /*SkiaDrawingBackend skiaDrawingBackend = new SkiaDrawingBackend(recordingContext);
+        DrawingBackendApi.SetupBackend(skiaDrawingBackend);*/
+        
+        SkiaPixiEngine engine = SkiaPixiEngine.Create();
+        engine.SetupBackendWindowLess();
 
         preferences = services.GetRequiredService<IPreferences>();
         platform = services.GetRequiredService<IPlatform>();
@@ -70,7 +75,7 @@ internal partial class MainWindow : Window
         InitializeComponent();
     }
 
-    private static GRContext GetGrRecordingContext()
+    /*private static GRContext GetGrRecordingContext()
     {
         Compositor compositor = Compositor.TryGetDefaultCompositor();
         var interop = compositor.TryGetCompositionGpuInterop();
@@ -91,7 +96,7 @@ internal partial class MainWindow : Window
         var ctxInterface = GRGlInterface.Create(ctx.GlInterface.GetProcAddress);
         var grContext = GRContext.CreateGl(ctxInterface);
         return grContext;
-    }
+    }*/
 
     public static MainWindow CreateWithRecoveredDocuments(CrashReport report, out bool showMissingFilesDialog)
     {

+ 3 - 3
src/PixiEditor.Engine.AvaloniaPlatform/PixiEngineGpuRenderSession.cs

@@ -7,8 +7,8 @@ public class PixiEngineGpuRenderSession : ISkiaGpuRenderSession
 {
     public PixiEngineSkiaSurface Surface { get; }
     public GRContext GrContext { get; }
-    
-    SKSurface ISkiaGpuRenderSession.SkSurface => Surface.BackendSurface.Native as SKSurface;
+
+    SKSurface ISkiaGpuRenderSession.SkSurface => Surface.Surface;
     double ISkiaGpuRenderSession.ScaleFactor => Surface.RenderScaling;
     GRSurfaceOrigin ISkiaGpuRenderSession.SurfaceOrigin => GRSurfaceOrigin.BottomLeft;
     
@@ -20,6 +20,6 @@ public class PixiEngineGpuRenderSession : ISkiaGpuRenderSession
     
     public void Dispose()
     {
-        (Surface.BackendSurface.Native as SKSurface)?.Flush(true);
+        //Surface.Surface?.Flush(true);
     }
 }

+ 3 - 0
src/PixiEditor.Engine.AvaloniaPlatform/PixiEngineSkiaGpu.cs

@@ -40,6 +40,9 @@ public class PixiEngineSkiaGpu : ISkiaGpu
 
     ISkiaSurface? ISkiaGpu.TryCreateSurface(PixelSize size, ISkiaGpuRenderSession? session)
     {
+        if(size.Width <= 0 || size.Height <= 0)
+            return null;
+        
         return new PixiEngineSkiaSurface(DrawingSurface.Create(new ImageInfo(size.Width, size.Height)), session?.ScaleFactor ?? 1);
     }
 

+ 22 - 3
src/PixiEditor.Engine.AvaloniaPlatform/PixiEngineSkiaSurface.cs

@@ -8,11 +8,24 @@ public class PixiEngineSkiaSurface : ISkiaSurface
 {
     public DrawingSurface BackendSurface { get; }
 
-    SKSurface ISkiaSurface.Surface => BackendSurface.Native as SKSurface;
+    public SKSurface Surface => nonDrawingSurface ?? BackendSurface?.Native as SKSurface;
     public bool CanBlit => false;
-    public bool IsDisposed => BackendSurface.IsDisposed;
+    public bool IsDisposed
+    {
+        get
+        {
+            if (nonDrawingSurface != null)
+            {
+                return false;
+            }
+            
+            return BackendSurface.IsDisposed;
+        }
+    }
+
     public double RenderScaling { get; }
-    
+
+    private SKSurface? nonDrawingSurface;
 
     public PixiEngineSkiaSurface(DrawingSurface surface, double renderScaling)
     {
@@ -20,6 +33,12 @@ public class PixiEngineSkiaSurface : ISkiaSurface
         RenderScaling = renderScaling;
     }
 
+    public PixiEngineSkiaSurface(SKSurface surface, double scaling)
+    {
+       nonDrawingSurface = surface;
+       RenderScaling = scaling;
+    }
+
     public void Blit(SKCanvas canvas)
     {
         throw new NotSupportedException();

+ 12 - 4
src/PixiEditor.Engine.AvaloniaPlatform/PixiEngineTopLevelImpl.cs

@@ -9,10 +9,10 @@ namespace PixiEditor.Engine.AvaloniaPlatform;
 
 internal sealed class PixiEngineTopLevelImpl : ITopLevelImpl
 {
-    public Size ClientSize { get; }
-    public Size? FrameSize { get; }
-    public double RenderScaling { get; }
-    public IEnumerable<object> Surfaces { get; }
+    public Size ClientSize { get; private set; }
+    public Size? FrameSize { get; private set; }
+    public double RenderScaling { get; } = 1;
+    public IEnumerable<object> Surfaces { get; private set; }
     public Action<RawInputEventArgs>? Input { get; set; }
     public Action<Rect>? Paint { get; set; }
     public Action<Size, WindowResizeReason>? Resized { get; set; }
@@ -38,6 +38,8 @@ internal sealed class PixiEngineTopLevelImpl : ITopLevelImpl
         }
     }
 
+    private PixiEngineSkiaSurface _surface;
+
     private IInputRoot? _inputRoot;
     private ICursorImpl _cursor;
     private WindowTransparencyLevel _transparencyLevel;
@@ -46,6 +48,12 @@ internal sealed class PixiEngineTopLevelImpl : ITopLevelImpl
     {
         Compositor = compositor;
     }
+    
+    public void SetRenderSize(Size size)
+    {
+        ClientSize = size;
+        FrameSize = size;
+    }
 
     public object? TryGetFeature(Type featureType)
     {

+ 2 - 1
src/PixiEditor.Engine.AvaloniaPlatform/PixiEngineWindowingPlatform.cs

@@ -7,8 +7,9 @@ public class PixiEngineWindowingPlatform() : IWindowingPlatform
 {
     public IWindowImpl CreateWindow()
     {
+        PixiEngineTopLevel topLevel = new PixiEngineTopLevel(new PixiEngineTopLevelImpl(PixiEnginePlatform.Compositor));
         Window window = new Window();
-        return new PixiEngineWindowImpl(window);
+        return new PixiEngineWindowImpl(window, topLevel);
     }
 
     public IWindowImpl CreateEmbeddableWindow()

+ 13 - 2
src/PixiEditor.Engine.AvaloniaPlatform/Windowing/PixiEngineWindowImpl.cs

@@ -4,6 +4,8 @@ using Avalonia.Input;
 using Avalonia.Input.Raw;
 using Avalonia.Platform;
 using Avalonia.Rendering.Composition;
+using PixiEditor.Engine.Helpers;
+using SkiaSharp;
 
 namespace PixiEditor.Engine.AvaloniaPlatform.Windowing;
 
@@ -62,12 +64,21 @@ public class PixiEngineWindowImpl : IWindowImpl
     private WindowTransparencyLevel _transparencyLevel;
     private Window _underlyingWindow;
 
-    public PixiEngineWindowImpl(Window underlyingWindow)
+    public PixiEngineWindowImpl(Window underlyingWindow, PixiEngineTopLevel topLevel)
     {
         _underlyingWindow = underlyingWindow;
         Compositor = PixiEnginePlatform.Compositor;
+        
+        topLevel.Impl.Surface = new PixiEngineSkiaSurface(underlyingWindow.FramebufferSurface, topLevel.RenderScaling);
+        topLevel.Impl.SetRenderSize(new Size(underlyingWindow.Size.X, underlyingWindow.Size.Y));
+        ClientSize = new Size(underlyingWindow.Size.X, underlyingWindow.Size.Y);
+        
+        topLevel.Prepare();
+        topLevel.StartRendering();
+        
+        topLevel.Content = underlyingWindow;
     }
-    
+
     object? IOptionalFeatureProvider.TryGetFeature(Type featureType) { return null; }
 
     void ITopLevelImpl.SetInputRoot(IInputRoot inputRoot)

+ 1 - 15
src/PixiEditor.Engine/SkiaPixiEngine.cs

@@ -26,20 +26,6 @@ public sealed class SkiaPixiEngine : PixiEngine
         return new SkiaPixiEngine(new SkiaDrawingBackend());
     }
 
-    public override IWindow GetWindow(WindowOptions options)
-    {
-        if (!_mainWindow.IsVisible)
-        {
-            _mainWindow.IsVisible = true;
-            _mainWindow.Size = options.Size;
-            _mainWindow.Title = options.Title;
-            
-            return _mainWindow;
-        }
-        
-        return base.GetWindow(options); 
-    }
-
     public void SetupBackendWindowLess()
     {
         _mainWindow = Silk.NET.Windowing.Window.Create(WindowOptions.Default with { IsVisible = false, Size = new Vector2D<int>(1, 1)}); 
@@ -50,7 +36,7 @@ public sealed class SkiaPixiEngine : PixiEngine
             GlContext = _mainWindow.GLContext;
             Setup(GrContext);
         };
-        
+
         _mainWindow.Initialize();
     }
 

+ 5 - 2
src/PixiEditor.Engine/Window.cs

@@ -1,4 +1,5 @@
-using PixiEditor.Engine.Helpers;
+using PixiEditor.DrawingApi.Core.Surface;
+using PixiEditor.Engine.Helpers;
 using PixiEditor.Numerics;
 using Silk.NET.GLFW;
 using Silk.NET.Maths;
@@ -36,6 +37,8 @@ public class Window
         set => _window.TopMost = value;
     }
 
+    public SKSurface FramebufferSurface => frontBufferSurface;
+
     public event Action<SKSurface, double> Render;
     public event Action Init;
     internal event Action<GRContext> InitWithGrContext;
@@ -53,7 +56,7 @@ public class Window
             throw new InvalidOperationException("PixiEngine is not initialized.");
 
         _window = PixiEngine.ActiveEngine.GetWindow(options);
-
+        
         if (!_window.IsInitialized)
         {
             _window.Load += () =>