Browse Source

Matrix, pixmap and surface impl wip

flabbet 2 years ago
parent
commit
7ce8c83d91

+ 1 - 1
src/PixiEditor.DrawingApi.Core/Bridge/IDrawingBackend.cs

@@ -13,7 +13,7 @@ namespace PixiEditor.DrawingApi.Core.Bridge
         public IVectorPathImplementation PathImplementation { get; }
         public IMatrix3X3Implementation MatrixImplementation { get; }
         public IPixmapImplementation PixmapImplementation { get; }
-        public ISurfaceOperations SurfaceOperations { get; }
+        public ISurfaceImplementation SurfaceImplementation { get; }
         public IColorSpaceImplementation ColorSpaceImplementation { get; }
         public IImgDataImplementation ImgDataImplementation { get; }
         public IBitmapImplementation BitmapImplementation { get; }

+ 1 - 1
src/PixiEditor.DrawingApi.Core/Bridge/NativeObjectsImpl/IPixmapImplementation.cs

@@ -8,6 +8,6 @@ public interface IPixmapImplementation
 {
     public void Dispose(IntPtr objectPointer);
     public IntPtr GetPixels(IntPtr objectPointer);
-    public Span<T> GetPixelSpan<T>(Pixmap pixmap);
+    public Span<T> GetPixelSpan<T>(Pixmap pixmap) where T : unmanaged;
     public IntPtr Construct(IntPtr dataPtr, ImageInfo imgInfo);
 }

+ 1 - 1
src/PixiEditor.DrawingApi.Core/Bridge/Operations/ISurfaceOperations.cs → src/PixiEditor.DrawingApi.Core/Bridge/Operations/ISurfaceImplementation.cs

@@ -4,7 +4,7 @@ using PixiEditor.DrawingApi.Core.Surface.ImageData;
 
 namespace PixiEditor.DrawingApi.Core.Bridge.Operations;
 
-public interface ISurfaceOperations
+public interface ISurfaceImplementation
 {
     public Pixmap PeekPixels(DrawingSurface drawingSurface);
     public DrawingSurface Create(ImageInfo imageInfo, IntPtr pixels, int rowBytes);

+ 7 - 7
src/PixiEditor.DrawingApi.Core/Surface/DrawingSurface.cs

@@ -18,12 +18,12 @@ namespace PixiEditor.DrawingApi.Core.Surface
         
         public static DrawingSurface Create(Pixmap imageInfo)
         {
-            return DrawingBackendApi.Current.SurfaceOperations.Create(imageInfo);
+            return DrawingBackendApi.Current.SurfaceImplementation.Create(imageInfo);
         }
         
         public void Draw(Canvas drawingSurfaceCanvas, int x, int y, Paint drawingPaint)
         {
-            DrawingBackendApi.Current.SurfaceOperations.Draw(this, drawingSurfaceCanvas, x, y, drawingPaint);
+            DrawingBackendApi.Current.SurfaceImplementation.Draw(this, drawingSurfaceCanvas, x, y, drawingPaint);
         }
 
         public Image Snapshot()
@@ -33,27 +33,27 @@ namespace PixiEditor.DrawingApi.Core.Surface
 
         public Pixmap PeekPixels()
         {
-            return DrawingBackendApi.Current.SurfaceOperations.PeekPixels(this);
+            return DrawingBackendApi.Current.SurfaceImplementation.PeekPixels(this);
         }
         
         public bool ReadPixels(ImageInfo dstInfo, IntPtr dstPixels, int dstRowBytes, int srcX, int srcY)
         {
-            return DrawingBackendApi.Current.SurfaceOperations.ReadPixels(this, dstInfo, dstPixels, dstRowBytes, srcX, srcY);
+            return DrawingBackendApi.Current.SurfaceImplementation.ReadPixels(this, dstInfo, dstPixels, dstRowBytes, srcX, srcY);
         }
         
         public DrawingSurface Create(ImageInfo imageInfo)
         {
-            return DrawingBackendApi.Current.SurfaceOperations.Create(imageInfo);
+            return DrawingBackendApi.Current.SurfaceImplementation.Create(imageInfo);
         }
 
         public static DrawingSurface Create(ImageInfo imageInfo, IntPtr pixels, int rowBytes)
         {
-            return DrawingBackendApi.Current.SurfaceOperations.Create(imageInfo, pixels, rowBytes);
+            return DrawingBackendApi.Current.SurfaceImplementation.Create(imageInfo, pixels, rowBytes);
         }
         
         public DrawingSurface Create(ImageInfo imageInfo, IntPtr pixelBuffer)
         {
-            return DrawingBackendApi.Current.SurfaceOperations.Create(imageInfo, pixelBuffer);
+            return DrawingBackendApi.Current.SurfaceImplementation.Create(imageInfo, pixelBuffer);
         }
 
         public override void Dispose()

+ 4 - 3
src/PixiEditor.DrawingApi.Skia/CastUtility.cs

@@ -1,15 +1,16 @@
-using System.Runtime.CompilerServices;
+using System;
+using System.Runtime.CompilerServices;
 
 namespace PixiEditor.DrawingApi.Skia
 {
     public static class CastUtility
     {
-        public static T2[] UnsafeArrayCast<T1, T2>(T1[] source)
+        public static unsafe T2[] UnsafeArrayCast<T1, T2>(T1[] source) where T2 : unmanaged
         {
             unsafe
             {
                 T2[] target = new T2[source.Length];
-                fixed (void* p = &target)
+                fixed (void* p = target)
                 {
                     Unsafe.Copy(p, ref source);
                 }

+ 12 - 0
src/PixiEditor.DrawingApi.Skia/ConversionExtensions.cs

@@ -1,6 +1,7 @@
 using PixiEditor.DrawingApi.Core.ColorsImpl;
 using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.DrawingApi.Core.Surface;
+using PixiEditor.DrawingApi.Core.Surface.ImageData;
 using SkiaSharp;
 
 namespace PixiEditor.DrawingApi.Skia
@@ -23,6 +24,12 @@ namespace PixiEditor.DrawingApi.Skia
                 matrix.ScaleY, matrix.TransY, matrix.Persp0, matrix.Persp1, matrix.Persp2);
         }
         
+        public static Matrix3X3 ToMatrix3X3(this SKMatrix matrix)
+        {
+            return new Matrix3X3(matrix.ScaleX, matrix.SkewX, matrix.TransX, matrix.SkewY, 
+                matrix.ScaleY, matrix.TransY, matrix.Persp0, matrix.Persp1, matrix.Persp2);
+        }
+        
         public static SKPoint ToSkPoint(this Point vector)
         {
             return new SKPoint(vector.X, vector.Y);
@@ -37,5 +44,10 @@ namespace PixiEditor.DrawingApi.Skia
         {
             return new SKRect(rect.Left, rect.Top, rect.Right, rect.Bottom);
         }
+        
+        public static SKImageInfo ToSkImageInfo(this ImageInfo info)
+        {
+            return new SKImageInfo(info.Width, info.Height, (SKColorType)info.ColorType, (SKAlphaType)info.AlphaType);
+        }
     }
 }

+ 31 - 0
src/PixiEditor.DrawingApi.Skia/Implementations/SkiaMatrixImplementation.cs

@@ -0,0 +1,31 @@
+using PixiEditor.DrawingApi.Core.Bridge.NativeObjectsImpl;
+using PixiEditor.DrawingApi.Core.Numerics;
+
+namespace PixiEditor.DrawingApi.Skia.Implementations
+{
+    public class SkiaMatrixImplementation : IMatrix3X3Implementation
+    {
+        public bool TryInvert(Matrix3X3 matrix, out Matrix3X3 inversedResult)
+        {
+            bool inverted = matrix.ToSkMatrix().TryInvert(out var result);
+            inversedResult = result.ToMatrix3X3();
+            
+            return inverted;
+        }
+
+        public Matrix3X3 Concat(in Matrix3X3 first, in Matrix3X3 second)
+        {
+            throw new System.NotImplementedException();
+        }
+
+        public Matrix3X3 PostConcat(in Matrix3X3 first, in Matrix3X3 second)
+        {
+            throw new System.NotImplementedException();
+        }
+
+        public VecD MapPoint(Matrix3X3 matrix, int p0, int p1)
+        {
+            throw new System.NotImplementedException();
+        }
+    }
+}

+ 33 - 0
src/PixiEditor.DrawingApi.Skia/Implementations/SkiaPixmapImplementation.cs

@@ -0,0 +1,33 @@
+using System;
+using PixiEditor.DrawingApi.Core.Bridge.NativeObjectsImpl;
+using PixiEditor.DrawingApi.Core.Surface;
+using PixiEditor.DrawingApi.Core.Surface.ImageData;
+using SkiaSharp;
+
+namespace PixiEditor.DrawingApi.Skia.Implementations
+{
+    public class SkiaPixmapImplementation : SkObjectImplementation<SKPixmap>, IPixmapImplementation
+    {
+        public void Dispose(IntPtr objectPointer)
+        {
+            ManagedInstances[objectPointer].Dispose();
+        }
+
+        public IntPtr GetPixels(IntPtr objectPointer)
+        {
+            return ManagedInstances[objectPointer].GetPixels();
+        }
+
+        public Span<T> GetPixelSpan<T>(Pixmap pixmap) where T : unmanaged
+        {
+            return ManagedInstances[pixmap.ObjectPointer].GetPixelSpan<T>();
+        }
+
+        public IntPtr Construct(IntPtr dataPtr, ImageInfo imgInfo)
+        {
+            SKPixmap pixmap = new SKPixmap(imgInfo.ToSkImageInfo(), dataPtr);
+            ManagedInstances[pixmap.Handle] = pixmap;
+            return pixmap.Handle;
+        }
+    }
+}

+ 55 - 0
src/PixiEditor.DrawingApi.Skia/Implementations/SkiaSurfaceImplementation.cs

@@ -0,0 +1,55 @@
+using System;
+using PixiEditor.DrawingApi.Core.Bridge.Operations;
+using PixiEditor.DrawingApi.Core.Surface;
+using PixiEditor.DrawingApi.Core.Surface.ImageData;
+using SkiaSharp;
+
+namespace PixiEditor.DrawingApi.Skia.Implementations
+{
+    public class SkiaSurfaceImplementation : SkObjectImplementation<SKSurface>, ISurfaceImplementation
+    {
+        private readonly SkiaPixmapImplementation _pixmapImplementation;
+        
+        public SkiaSurfaceImplementation(SkiaPixmapImplementation pixmapImplementation)
+        {
+            _pixmapImplementation = pixmapImplementation;
+        }
+        
+        public Pixmap PeekPixels(DrawingSurface drawingSurface)
+        {
+            SKPixmap pixmap = ManagedInstances[drawingSurface.ObjectPointer].PeekPixels();
+            _pixmapImplementation.CreateFrom()
+        }
+
+        public DrawingSurface Create(ImageInfo imageInfo, IntPtr pixels, int rowBytes)
+        {
+            throw new NotImplementedException();
+        }
+
+        public bool ReadPixels(DrawingSurface drawingSurface, ImageInfo dstInfo, IntPtr dstPixels, int dstRowBytes, int srcX,
+            int srcY)
+        {
+            throw new NotImplementedException();
+        }
+
+        public void Draw(DrawingSurface drawingSurface, Canvas surfaceToDraw, int x, int y, Paint drawingPaint)
+        {
+            throw new NotImplementedException();
+        }
+
+        public DrawingSurface Create(ImageInfo imageInfo, IntPtr pixelBuffer)
+        {
+            throw new NotImplementedException();
+        }
+
+        public DrawingSurface Create(Pixmap pixmap)
+        {
+            throw new NotImplementedException();
+        }
+
+        public DrawingSurface Create(ImageInfo imageInfo)
+        {
+            throw new NotImplementedException();
+        }
+    }
+}

+ 8 - 1
src/PixiEditor.DrawingApi.Skia/SkiaDrawingBackend.cs

@@ -15,23 +15,30 @@ namespace PixiEditor.DrawingApi.Skia
         public IVectorPathImplementation PathImplementation { get; }
         public IMatrix3X3Implementation MatrixImplementation { get; }
         public IPixmapImplementation PixmapImplementation { get; }
-        public ISurfaceOperations SurfaceOperations { get; }
+        public ISurfaceImplementation SurfaceImplementation { get; }
         public IColorSpaceImplementation ColorSpaceImplementation { get; }
         public IBitmapImplementation BitmapImplementation { get; }
         
         public SkiaDrawingBackend()
         {
             ColorImplementation = new SkiaColorImplementation();
+            
             SkiaImgDataImplementation dataImpl = new SkiaImgDataImplementation();
             ImgDataImplementation = dataImpl;
+            
             SkiaImageImplementation imgImpl = new SkiaImageImplementation(dataImpl);
             ImageImplementation = imgImpl;
+            
             SkiaPaintImplementation paintImpl = new SkiaPaintImplementation();
             PaintImplementation = paintImpl;
             
             SkiaPathImplementation pathImpl = new SkiaPathImplementation();
             PathImplementation = pathImpl;
             
+            MatrixImplementation = new SkiaMatrixImplementation();
+            
+            PixmapImplementation = new SkiaPixmapImplementation();
+            
             CanvasImplementation = new SkiaCanvasImplementation(paintImpl, imgImpl, pathImpl);
         }