Browse Source

Fixed non compiling tests

Krzysztof Krysiński 1 week ago
parent
commit
dd0e3d9c2a

+ 4 - 3
src/PixiEditor/Models/Rendering/SceneRenderer.cs

@@ -1,6 +1,7 @@
 using Avalonia.Threading;
 using ChunkyImageLib.DataHolders;
 using Drawie.Backend.Core;
+using Drawie.Backend.Core.Bridge;
 using Drawie.Backend.Core.Numerics;
 using PixiEditor.ChangeableDocument.Changeables.Interfaces;
 using PixiEditor.ChangeableDocument.Rendering;
@@ -38,7 +39,7 @@ internal class SceneRenderer : IDisposable
     public async Task RenderAsync(Dictionary<Guid, ViewportInfo> stateViewports, AffectedArea affectedArea,
         bool updateDelayed, Dictionary<Guid, List<PreviewRenderRequest>>? previewTextures)
     {
-        await Dispatcher.UIThread.InvokeAsync(() =>
+        await DrawingBackendApi.Current.RenderingDispatcher.InvokeAsync(() =>
         {
             int renderedCount = 0;
             foreach (var viewport in stateViewports)
@@ -65,7 +66,7 @@ internal class SceneRenderer : IDisposable
             {
                 RenderOnlyPreviews(affectedArea, previewTextures);
             }
-        }, DispatcherPriority.Background);
+        });
     }
 
     private void RenderOnlyPreviews(AffectedArea affectedArea,
@@ -93,7 +94,7 @@ internal class SceneRenderer : IDisposable
             target.DeviceClipBounds.Size.ShortestAxis <= 0) return;*/
 
         /*TODO:
-         - [ ] Onion skinning
+         - [ ] Fix masks preview along with test case that fails
          - [ ] Rendering optimizer
          - [?] Render thread and proper locking/synchronization
          */

+ 38 - 4
tests/PixiEditor.Tests/PixiEditorTest.cs

@@ -1,5 +1,11 @@
+using Drawie.Backend.Core;
 using Drawie.Backend.Core.Bridge;
+using Drawie.Interop.Avalonia.Core;
 using Drawie.Numerics;
+using Drawie.RenderApi;
+using Drawie.RenderApi.OpenGL;
+using Drawie.RenderApi.Vulkan;
+using Drawie.Silk;
 using Drawie.Skia;
 using Drawie.Windowing;
 using DrawiEngine;
@@ -28,7 +34,15 @@ public class PixiEditorTest
 
         try
         {
-            var engine = DesktopDrawingEngine.CreateDefaultDesktop();
+            IRenderApi renderApi = new VulkanRenderApi();
+
+            if (System.OperatingSystem.IsMacOS())
+            {
+                renderApi = new OpenGlRenderApi();
+            }
+
+            var engine = new DrawingEngine(renderApi, new GlfwWindowingPlatform(renderApi), new SkiaDrawingBackend(),
+                new TestsRenderingDispatcher());
             var app = new TestingApp();
             Console.WriteLine("Running DrawieEngine with configuration:");
             Console.WriteLine($"\t- RenderApi: {engine.RenderApi}");
@@ -47,7 +61,7 @@ public class PixiEditorTest
         catch (Exception ex)
         {
             if (!DrawingBackendApi.HasBackend)
-                DrawingBackendApi.SetupBackend(new SkiaDrawingBackend(), new DrawieRenderingDispatcher());
+                DrawingBackendApi.SetupBackend(new SkiaDrawingBackend(), new TestsRenderingDispatcher());
         }
     }
 }
@@ -92,7 +106,6 @@ public class FullPixiEditorTest : PixiEditorTest
             .AddExtensionServices(loader)
             .BuildServiceProvider();
 
-
         var vm = services.GetRequiredService<ViewModelMain>();
         vm.Setup(services);
     }
@@ -112,7 +125,7 @@ public class FullPixiEditorTest : PixiEditorTest
         }
 
         public IAdditionalContentProvider? AdditionalContentProvider { get; } = new NullAdditionalContentProvider();
-        public IIdentityProvider? IdentityProvider { get; } 
+        public IIdentityProvider? IdentityProvider { get; }
     }
 }
 
@@ -130,4 +143,25 @@ public class TestingApp : DrawieApp
     {
         window.IsVisible = false;
     }
+}
+
+class TestsRenderingDispatcher : IRenderingDispatcher
+{
+    public Action<Action> Invoke { get; } = action => action();
+
+    public Task<TResult> InvokeAsync<TResult>(Func<TResult> function)
+    {
+        return Task.FromResult(function());
+    }
+
+    public Task InvokeAsync(Action function)
+    {
+        function();
+        return Task.CompletedTask;
+    }
+
+    public IDisposable EnsureContext()
+    {
+        return new EmptyDisposable();
+    }
 }

+ 14 - 3
tests/PixiEditor.Tests/RenderTests.cs

@@ -1,12 +1,15 @@
 using Avalonia.Headless.XUnit;
+using Avalonia.Threading;
 using ChunkyImageLib;
 using ChunkyImageLib.DataHolders;
 using Drawie.Backend.Core;
 using Drawie.Backend.Core.Bridge;
 using Drawie.Backend.Core.ColorsImpl;
+using Drawie.Backend.Core.Numerics;
 using Drawie.Backend.Core.Surfaces;
 using Drawie.Numerics;
 using PixiEditor.Models.IO;
+using PixiEditor.Models.Position;
 using Xunit.Abstractions;
 using Color = Drawie.Backend.Core.ColorsImpl.Color;
 
@@ -89,12 +92,20 @@ public class RenderTests : FullPixiEditorTest
         string pixiFile = Path.Combine("TestFiles", "ResolutionTests", pixiName + ".pixi");
 
         var document = Importer.ImportDocument(pixiFile);
-        using Surface output = Surface.ForDisplay(document.SizeBindable);
-        document.SceneRenderer.RenderScene(output.DrawingSurface, ChunkResolution.Half, SamplingOptions.Default);
+        ViewportInfo info = new ViewportInfo(
+            0,
+            document.SizeBindable / 2f,
+            document.SizeBindable,
+            Matrix3X3.Identity, null, "DEFAULT", SamplingOptions.Default, document.SizeBindable, ChunkResolution.Half,
+            Guid.NewGuid(), false, false, () => { });
+        using var output = document.SceneRenderer.RenderScene(info, new AffectedArea(), null);
 
         Color expectedColor = Colors.Yellow;
 
-        Assert.True(AllPixelsAreColor(output, expectedColor));
+        using Surface surface = Surface.ForDisplay(document.SizeBindable);
+        surface.DrawingSurface.Canvas.DrawSurface(output.DrawingSurface, 0, 0);
+
+        Assert.True(AllPixelsAreColor(surface, expectedColor));
     }
 
     private static bool PixelCompare(Surface image, Surface compareTo)