Ver Fonte

Opengl experiment - failed

Krzysztof Krysiński há 1 ano atrás
pai
commit
7d873f6a4e

+ 4 - 0
src/PixiEditor.AvaloniaUI.Desktop/Program.cs

@@ -18,5 +18,9 @@ public class Program
         => AppBuilder.Configure<App>()
             .UsePlatformDetect()
             .WithInterFont()
+            .With(new Win32PlatformOptions()
+            {
+                RenderingMode = new [] { Win32RenderingMode.Wgl }
+            })
             .LogToTrace();
 }

+ 2 - 1
src/PixiEditor.AvaloniaUI/Views/Main/ViewportControls/Viewport.axaml

@@ -231,9 +231,10 @@
                     </Canvas>
                     <visuals:SurfaceControl
                         Focusable="False"
+                        Surface="{Binding TargetBitmap}"
                         Width="{Binding Document.Width}"
                         Height="{Binding Document.Height}"
-                        Surface="{Binding TargetBitmap}"
+                        Scale="{Binding Zoombox.Scale}"
                         ui1:RenderOptionsBindable.BitmapInterpolationMode="{Binding Zoombox.Scale, Converter={converters:ScaleToBitmapScalingModeConverter}}"
                         FlowDirection="LeftToRight">
                         <visuals:SurfaceControl.Styles>

+ 3 - 0
src/PixiEditor.AvaloniaUI/Views/MainWindow.axaml.cs

@@ -4,6 +4,9 @@ using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Controls.ApplicationLifetimes;
 using Avalonia.Interactivity;
+using Avalonia.OpenGL;
+using Avalonia.Rendering.Composition;
+using Avalonia.Skia;
 using Avalonia.Threading;
 using Microsoft.Extensions.DependencyInjection;
 using PixiEditor.AvaloniaUI.Helpers;

+ 58 - 6
src/PixiEditor.AvaloniaUI/Views/Visuals/SurfaceControl.cs

@@ -1,20 +1,23 @@
-using System.Diagnostics;
-using Avalonia;
+using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Media;
+using Avalonia.OpenGL;
+using Avalonia.OpenGL.Controls;
 using Avalonia.Platform;
 using Avalonia.Rendering.SceneGraph;
 using Avalonia.Skia;
+using Avalonia.Threading;
 using ChunkyImageLib;
+using PixiEditor.DrawingApi.Core.Bridge;
 using PixiEditor.DrawingApi.Core.Numerics;
-using PixiEditor.DrawingApi.Core.Surface;
 using PixiEditor.DrawingApi.Core.Surface.PaintImpl;
-using Image = PixiEditor.DrawingApi.Core.Surface.ImageData.Image;
+using PixiEditor.DrawingApi.Skia;
+using Colors = PixiEditor.DrawingApi.Core.ColorsImpl.Colors;
 using Point = Avalonia.Point;
 
 namespace PixiEditor.AvaloniaUI.Views.Visuals;
 
-public class SurfaceControl : Control
+public class SurfaceControl : OpenGlControlBase
 {
     public static readonly StyledProperty<Surface> SurfaceProperty = AvaloniaProperty.Register<SurfaceControl, Surface>(
         nameof(Surface));
@@ -22,6 +25,10 @@ public class SurfaceControl : Control
     public static readonly StyledProperty<Stretch> StretchProperty = AvaloniaProperty.Register<SurfaceControl, Stretch>(
         nameof(Stretch), Stretch.Uniform);
 
+    public static readonly StyledProperty<double> ScaleProperty = AvaloniaProperty.Register<SurfaceControl, double>(
+        nameof(Scale), 1);
+
+
     public Stretch Stretch
     {
         get => GetValue(StretchProperty);
@@ -34,7 +41,16 @@ public class SurfaceControl : Control
         set => SetValue(SurfaceProperty, value);
     }
 
+    public double Scale
+    {
+        get { return (double)GetValue(ScaleProperty); }
+        set { SetValue(ScaleProperty, value); }
+    }
+
     private DrawingSurfaceOp _drawingSurfaceOp;
+    private GRContext grContext;
+    private GRGlFramebufferInfo frameBuffer;
+    private SKSurface surface;
 
     static SurfaceControl()
     {
@@ -46,7 +62,7 @@ public class SurfaceControl : Control
         HeightProperty.Changed.AddClassHandler<SurfaceControl>(BoundsChanged);
     }
 
-    public override void Render(DrawingContext context)
+    /*public override void Render(DrawingContext context)
     {
         if (Surface == null)
         {
@@ -54,6 +70,42 @@ public class SurfaceControl : Control
         }
 
         context.Custom(_drawingSurfaceOp);
+    }*/
+
+    protected override void OnOpenGlInit(GlInterface gl)
+    {
+        base.OnOpenGlInit(gl);
+        if (DrawingBackendApi.Current is SkiaDrawingBackend skiaDrawingBackend)
+        {
+            grContext = GRContext.CreateGl(GRGlInterface.Create(gl.GetProcAddress));
+            skiaDrawingBackend.SurfaceImplementation.SetGrContext(grContext);
+        }
+
+        grContext = GRContext.CreateGl(GRGlInterface.Create(gl.GetProcAddress));
+                SKImage snapshot = ((SKSurface)Surface.DrawingSurface.Native).Snapshot();
+        frameBuffer = new GRGlFramebufferInfo(0, SKColorType.Rgba8888.ToGlSizedFormat());
+        GRBackendRenderTarget desc = new GRBackendRenderTarget((int)Bounds.Width, (int)Bounds.Height, 4, 0, frameBuffer);
+
+        surface = SKSurface.Create(grContext, desc, GRSurfaceOrigin.BottomLeft, snapshot.ColorType);
+    }
+
+    protected override void OnOpenGlRender(GlInterface gl, int fb)
+    {
+        if (Surface == null)
+        {
+            return;
+        }
+
+        SKCanvas canvas = surface.Canvas;
+        canvas.Clear(SKColors.Transparent);
+        using (var paint = new SKPaint())
+        {
+            canvas.DrawSurface((SKSurface)Surface.DrawingSurface.Native, 0, 0, paint);
+        }
+
+        canvas.Flush();
+
+        Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background);
     }
 
     private static void StretchChanged(SurfaceControl sender, AvaloniaPropertyChangedEventArgs e)

+ 1 - 1
src/PixiEditor.DrawingApi.Core/PixiEditor.DrawingApi.Core.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
     <PropertyGroup>
-        <TargetFramework>netstandard2.1</TargetFramework>
+        <TargetFramework>net8.0</TargetFramework>
         <Nullable>enable</Nullable>
         <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
         <LangVersion>10</LangVersion>

+ 61 - 4
src/PixiEditor.DrawingApi.Skia/Implementations/SkiaSurfaceImplementation.cs

@@ -14,6 +14,8 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
         private readonly SkiaCanvasImplementation _canvasImplementation;
         private readonly SkiaPaintImplementation _paintImplementation;
 
+        private GRContext? _grContext;
+
         public SkiaSurfaceImplementation(SkiaPixmapImplementation pixmapImplementation, SkiaCanvasImplementation canvasImplementation, SkiaPaintImplementation paintImplementation)
         {
             _pixmapImplementation = pixmapImplementation;
@@ -43,26 +45,62 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
         
         public DrawingSurface Create(ImageInfo imageInfo, IntPtr pixels, int rowBytes)
         {
-            SKSurface skSurface = SKSurface.Create(imageInfo.ToSkImageInfo(), pixels, rowBytes);
+            SKSurface skSurface;
+            if (_grContext != null)
+            {
+                skSurface = CreateGrContextSurface(imageInfo.ToSkImageInfo());
+            }
+            else
+            {
+                skSurface = SKSurface.Create(imageInfo.ToSkImageInfo(), pixels, rowBytes);
+            }
+
             return CreateDrawingSurface(skSurface);
         }
 
         public DrawingSurface Create(ImageInfo imageInfo, IntPtr pixelBuffer)
         {
-            SKSurface skSurface = SKSurface.Create(imageInfo.ToSkImageInfo(), pixelBuffer);
+            SKSurface skSurface;
+            if (_grContext != null)
+            {
+                skSurface = CreateGrContextSurface(imageInfo.ToSkImageInfo(), pixelBuffer);
+            }
+            else
+            {
+                skSurface = SKSurface.Create(imageInfo.ToSkImageInfo(), pixelBuffer);
+            }
             return CreateDrawingSurface(skSurface);
         }
 
         public DrawingSurface Create(Pixmap pixmap)
         {
             SKPixmap skPixmap = _pixmapImplementation[pixmap.ObjectPointer];
-            SKSurface skSurface = SKSurface.Create(skPixmap);
+            SKSurface skSurface;
+            if (_grContext != null)
+            {
+                // TODO: This is not correct lol, leaving for debugging purposes right now
+                skSurface = CreateGrContextSurface(skPixmap.Info);
+            }
+            else
+            {
+                skSurface = SKSurface.Create(skPixmap);
+            }
+
             return CreateDrawingSurface(skSurface);
         }
 
         public DrawingSurface Create(ImageInfo imageInfo)
         {
-            SKSurface skSurface = SKSurface.Create(imageInfo.ToSkImageInfo());
+            SKSurface skSurface;
+            if (_grContext != null)
+            {
+                skSurface = CreateGrContextSurface(imageInfo.ToSkImageInfo());
+            }
+            else
+            {
+                skSurface = SKSurface.Create(imageInfo.ToSkImageInfo());
+            }
+
             return CreateDrawingSurface(skSurface);
         }
 
@@ -77,6 +115,20 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
             return ManagedInstances[objectPointer];
         }
 
+        private SKSurface CreateGrContextSurface(SKImageInfo info)
+        {
+            GRGlFramebufferInfo framebuffer = new GRGlFramebufferInfo((uint)0, info.ColorType.ToGlSizedFormat());
+            GRBackendRenderTarget renderTarget = new GRBackendRenderTarget(info.Width, info.Height, 4, 0, framebuffer);
+            return SKSurface.Create(_grContext, renderTarget, GRSurfaceOrigin.BottomLeft, SKImageInfo.PlatformColorType);
+        }
+
+        private SKSurface CreateGrContextSurface(SKImageInfo info, IntPtr pixelBuffer)
+        {
+            GRGlFramebufferInfo framebuffer = new GRGlFramebufferInfo((uint)0, info.ColorType.ToGlSizedFormat());
+            GRBackendRenderTarget renderTarget = new GRBackendRenderTarget(info.Width, info.Height, 4, 0, framebuffer);
+            return SKSurface.Create(_grContext, renderTarget, GRSurfaceOrigin.BottomLeft, info.ColorType, info.ColorSpace);
+        }
+
         private DrawingSurface CreateDrawingSurface(SKSurface skSurface)
         {
             _canvasImplementation.ManagedInstances[skSurface.Canvas.Handle] = skSurface.Canvas;
@@ -86,5 +138,10 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
             ManagedInstances[skSurface.Handle] = skSurface;
             return surface;
         }
+
+        public void SetGrContext(GRContext grContext)
+        {
+            _grContext = grContext;
+        }
     }
 }

+ 1 - 1
src/PixiEditor.DrawingApi.Skia/PixiEditor.DrawingApi.Skia.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
     <PropertyGroup>
-        <TargetFramework>netstandard2.1</TargetFramework>
+        <TargetFramework>net8.0</TargetFramework>
         <Nullable>enable</Nullable>
         <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
         <Configurations>Debug;Release;Steam;DevRelease;DevSteam</Configurations>

+ 30 - 13
src/PixiEditor.DrawingApi.Skia/SkiaDrawingBackend.cs

@@ -2,23 +2,41 @@
 using PixiEditor.DrawingApi.Core.Bridge.NativeObjectsImpl;
 using PixiEditor.DrawingApi.Core.Bridge.Operations;
 using PixiEditor.DrawingApi.Skia.Implementations;
+using SkiaSharp;
 
 namespace PixiEditor.DrawingApi.Skia
 {
     public class SkiaDrawingBackend : IDrawingBackend
     {
-        public IColorImplementation ColorImplementation { get; }
-        public IImageImplementation ImageImplementation { get; }
-        public IImgDataImplementation ImgDataImplementation { get; }
-        public ICanvasImplementation CanvasImplementation { get; }
-        public IPaintImplementation PaintImplementation { get; }
-        public IVectorPathImplementation PathImplementation { get; }
-        public IMatrix3X3Implementation MatrixImplementation { get; }
-        public IPixmapImplementation PixmapImplementation { get; }
-        public ISurfaceImplementation SurfaceImplementation { get; }
-        public IColorSpaceImplementation ColorSpaceImplementation { get; }
-        public IBitmapImplementation BitmapImplementation { get; }
-        public IColorFilterImplementation ColorFilterImplementation { get; set; }
+        public SkiaColorImplementation ColorImplementation { get; }
+        public SkiaImageImplementation ImageImplementation { get; }
+        public SkiaImgDataImplementation ImgDataImplementation { get; }
+        public SkiaCanvasImplementation CanvasImplementation { get; }
+        public SkiaPaintImplementation PaintImplementation { get; }
+        public SkiaPathImplementation PathImplementation { get; }
+        public SkiaMatrixImplementation MatrixImplementation { get; }
+        public SkiaPixmapImplementation PixmapImplementation { get; }
+        public SkiaSurfaceImplementation SurfaceImplementation { get; }
+        public SkiaColorSpaceImplementation ColorSpaceImplementation { get; }
+        public SkiaBitmapImplementation BitmapImplementation { get; }
+        public SkiaColorFilterImplementation ColorFilterImplementation { get; set; }
+        IColorImplementation IDrawingBackend.ColorImplementation => ColorImplementation;
+        IImageImplementation IDrawingBackend.ImageImplementation => ImageImplementation;
+        IImgDataImplementation IDrawingBackend.ImgDataImplementation => ImgDataImplementation;
+        ICanvasImplementation IDrawingBackend.CanvasImplementation => CanvasImplementation;
+        IPaintImplementation IDrawingBackend.PaintImplementation => PaintImplementation;
+        IVectorPathImplementation IDrawingBackend.PathImplementation => PathImplementation;
+        IMatrix3X3Implementation IDrawingBackend.MatrixImplementation => MatrixImplementation;
+        IPixmapImplementation IDrawingBackend.PixmapImplementation => PixmapImplementation;
+        ISurfaceImplementation IDrawingBackend.SurfaceImplementation => SurfaceImplementation;
+        IColorSpaceImplementation IDrawingBackend.ColorSpaceImplementation => ColorSpaceImplementation;
+        IBitmapImplementation IDrawingBackend.BitmapImplementation => BitmapImplementation;
+
+        IColorFilterImplementation IDrawingBackend.ColorFilterImplementation
+        {
+            get => ColorFilterImplementation;
+            set => ColorFilterImplementation = (SkiaColorFilterImplementation)value;
+        }
 
         public SkiaDrawingBackend()
         {
@@ -65,7 +83,6 @@ namespace PixiEditor.DrawingApi.Skia
         
         public void Setup()
         {
-            
         }
     }
 }