Browse Source

SkiaBackend implementation draft

flabbet 2 years ago
parent
commit
b74d3212b1

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

@@ -7,15 +7,15 @@ namespace PixiEditor.DrawingApi.Core.Bridge
     {
         public void Setup();
         public IColorImplementation ColorImplementation { get; }
-        public IImageOperations ImageOperations { get; }
+        public IImageImplementation ImageImplementation { get; }
         public ICanvasOperations CanvasOperations { get; }
-        public IPaintImplementation PaintImplementation { get; set; }
-        public IVectorPathImplementation PathImplementation { get; set; }
-        public IMatrix3X3Implementation MatrixImplementation { get; set; }
-        public IPixmapImplementation PixmapImplementation { get; set; }
-        public ISurfaceOperations SurfaceOperations { get; set; }
-        public IColorSpaceImplementation ColorSpaceImplementation { get; set; }
-        public IImgDataImplementation ImgDataImplementation { get; set; }
-        public IBitmapImplementation BitmapImplementation { get; set; }
+        public IPaintImplementation PaintImplementation { get; }
+        public IVectorPathImplementation PathImplementation { get; }
+        public IMatrix3X3Implementation MatrixImplementation { get; }
+        public IPixmapImplementation PixmapImplementation { get; }
+        public ISurfaceOperations SurfaceOperations { get; }
+        public IColorSpaceImplementation ColorSpaceImplementation { get; }
+        public IImgDataImplementation ImgDataImplementation { get; }
+        public IBitmapImplementation BitmapImplementation { get; }
     }
 }

+ 5 - 2
src/PixiEditor.DrawingApi.Core/Bridge/Operations/IImageOperations.cs → src/PixiEditor.DrawingApi.Core/Bridge/Operations/IImageImplementation.cs

@@ -1,14 +1,17 @@
-using PixiEditor.DrawingApi.Core.Surface;
+using System;
+using PixiEditor.DrawingApi.Core.Surface;
 using PixiEditor.DrawingApi.Core.Surface.ImageData;
 
 namespace PixiEditor.DrawingApi.Core.Bridge.Operations
 {
-    public interface IImageOperations
+    public interface IImageImplementation
     {
         public Image Snapshot(DrawingSurface drawingSurface);
         public void DisposeImage(Image image);
         public Image FromEncodedData(string path);
         public void GetColorShifts(ref int platformColorAlphaShift, ref int platformColorRedShift, ref int platformColorGreenShift, ref int platformColorBlueShift);
         public ImgData Encode(Image image);
+        public int GetWidth(IntPtr objectPointer);
+        public int GetHeight(IntPtr objectPointer);
     }
 }

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

@@ -28,7 +28,7 @@ namespace PixiEditor.DrawingApi.Core.Surface
 
         public Image Snapshot()
         {
-            return DrawingBackendApi.Current.ImageOperations.Snapshot(this);
+            return DrawingBackendApi.Current.ImageImplementation.Snapshot(this);
         }
 
         public Pixmap PeekPixels()

+ 11 - 6
src/PixiEditor.DrawingApi.Core/Surface/ImageData/Image.cs

@@ -11,24 +11,29 @@ namespace PixiEditor.DrawingApi.Core.Surface.ImageData
     ///     <para />
     ///     <para>An image always has a non-zero dimensions. If there is a request to create a new image, either directly or via a surface, and either of the requested dimensions are zero, then <see langword="null" /> will be returned.</para>
     /// </remarks>
-    public class Image : PixelDataObject
+    public class Image : NativeObject
     {
-        public int Width { get; set; }
-        public int Height { get; set; }
+        public int Width => DrawingBackendApi.Current.ImageImplementation.GetWidth(ObjectPointer);
         
+        public int Height => DrawingBackendApi.Current.ImageImplementation.GetHeight(ObjectPointer);
+        
+        public Image(IntPtr objPtr) : base(objPtr)
+        {
+        }
+
         public override void Dispose()
         {
-            DrawingBackendApi.Current.ImageOperations.DisposeImage(this);
+            DrawingBackendApi.Current.ImageImplementation.DisposeImage(this);
         }
 
         public static Image FromEncodedData(string path)
         {
-            return DrawingBackendApi.Current.ImageOperations.FromEncodedData(path);
+            return DrawingBackendApi.Current.ImageImplementation.FromEncodedData(path);
         }
 
         public ImgData Encode()
         {
-            return DrawingBackendApi.Current.ImageOperations.Encode(this);
+            return DrawingBackendApi.Current.ImageImplementation.Encode(this);
         }
     }
 }

+ 1 - 1
src/PixiEditor.DrawingApi.Core/Surface/ImageData/ImageInfo.cs

@@ -27,7 +27,7 @@ public struct ImageInfo : System.IEquatable<ImageInfo>
 
     static unsafe ImageInfo()
     {
-      DrawingBackendApi.Current.ImageOperations.GetColorShifts(ref PlatformColorAlphaShift, ref PlatformColorRedShift, ref PlatformColorGreenShift, ref PlatformColorBlueShift);
+      DrawingBackendApi.Current.ImageImplementation.GetColorShifts(ref PlatformColorAlphaShift, ref PlatformColorRedShift, ref PlatformColorGreenShift, ref PlatformColorBlueShift);
     }
 
     /// <summary>Gets or sets the width.</summary>

+ 39 - 0
src/PixiEditor.DrawingApi.Skia/Implementations/SkiaColorImplementation.cs

@@ -0,0 +1,39 @@
+using PixiEditor.DrawingApi.Core.Bridge.NativeObjectsImpl;
+using PixiEditor.DrawingApi.Core.ColorsImpl;
+using PixiEditor.DrawingApi.Core.Surface.ImageData;
+using SkiaSharp;
+
+namespace PixiEditor.DrawingApi.Skia.Implementations
+{
+    public sealed class SkiaColorImplementation : IColorImplementation
+    {
+        public ColorF ColorToColorF(uint colorValue)
+        {
+            SKColor color = new SKColor(colorValue);
+            return SKColorFToColorF((SKColorF)color);
+        }
+
+        public Color ColorFToColor(ColorF color)
+        {
+            SKColorF skColorF = ColorFToSKColorF(color);
+            SKColor skColor = (SKColor)skColorF;
+            return new Color(skColor.Red, skColor.Green, skColor.Blue, skColor.Alpha);
+        }
+
+        public ColorType GetPlatformColorType()
+        {
+            SKColorType colorType = SKImageInfo.PlatformColorType;
+            return (ColorType)colorType;
+        }
+        
+        public static ColorF SKColorFToColorF(SKColorF color)
+        {
+            return new ColorF(color.Red, color.Green, color.Blue, color.Alpha);
+        }
+        
+        public static SKColorF ColorFToSKColorF(ColorF color)
+        {
+            return new SKColorF(color.R, color.G, color.B, color.A);
+        }
+    }
+}

+ 66 - 0
src/PixiEditor.DrawingApi.Skia/Implementations/SkiaImageImplementation.cs

@@ -0,0 +1,66 @@
+using System;
+using System.Collections.Generic;
+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 SkiaImageImplementation : IImageImplementation
+    {
+        internal readonly Dictionary<IntPtr, SKImage> ManagedImages = new Dictionary<IntPtr, SKImage>();
+
+        private readonly SkiaImgDataImplementation _imgImplementation;
+        
+        public SkiaImageImplementation(SkiaImgDataImplementation imgDataImplementation)
+        {
+            _imgImplementation = imgDataImplementation;
+        }
+        
+        public Image Snapshot(DrawingSurface drawingSurface)
+        {
+            throw new NotImplementedException();
+        }
+
+        public void DisposeImage(Image image)
+        {
+            ManagedImages[image.ObjectPointer].Dispose();
+            ManagedImages.Remove(image.ObjectPointer);
+        }
+
+        public Image FromEncodedData(string path)
+        {
+            var nativeImg = SKImage.FromEncodedData(path);
+            ManagedImages[nativeImg.Handle] = nativeImg;
+            return new Image(nativeImg.Handle);
+        }
+
+        public void GetColorShifts(ref int platformColorAlphaShift, ref int platformColorRedShift, ref int platformColorGreenShift,
+            ref int platformColorBlueShift)
+        {
+            platformColorAlphaShift = SKImageInfo.PlatformColorAlphaShift;
+            platformColorRedShift = SKImageInfo.PlatformColorRedShift;
+            platformColorGreenShift = SKImageInfo.PlatformColorGreenShift;
+            platformColorBlueShift = SKImageInfo.PlatformColorBlueShift;
+        }
+
+        public ImgData Encode(Image image)
+        {
+            var native = ManagedImages[image.ObjectPointer];
+            var encoded = native.Encode();
+            _imgImplementation.ManagedImgDataObjects[encoded.Handle] = encoded;
+            return new ImgData(encoded.Handle);
+        }
+
+        public int GetWidth(IntPtr objectPointer)
+        {
+            return ManagedImages[objectPointer].Width;
+        }
+
+        public int GetHeight(IntPtr objectPointer)
+        {
+            return ManagedImages[objectPointer].Height;
+        }
+    }
+}

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

@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using PixiEditor.DrawingApi.Core.Bridge.NativeObjectsImpl;
+using PixiEditor.DrawingApi.Core.Surface.ImageData;
+using SkiaSharp;
+
+namespace PixiEditor.DrawingApi.Skia.Implementations
+{
+    public sealed class SkiaImgDataImplementation : IImgDataImplementation
+    {
+        internal readonly Dictionary<IntPtr, SKData> ManagedImgDataObjects = new Dictionary<IntPtr, SKData>();
+
+        public void Dispose(IntPtr objectPointer)
+        {
+            if (ManagedImgDataObjects.ContainsKey(objectPointer))
+            {
+                ManagedImgDataObjects[objectPointer].Dispose();
+                ManagedImgDataObjects.Remove(objectPointer);
+            }
+        }
+
+        public void SaveTo(ImgData imgData, FileStream stream)
+        {
+            throw new NotImplementedException();
+        }
+
+        public Stream AsStream(ImgData imgData)
+        {
+            throw new NotImplementedException();
+        }
+    }
+}

+ 4 - 0
src/PixiEditor.DrawingApi.Skia/PixiEditor.DrawingApi.Skia.csproj

@@ -9,4 +9,8 @@
       <PackageReference Include="SkiaSharp" Version="2.80.3" />
     </ItemGroup>
 
+    <ItemGroup>
+      <ProjectReference Include="..\PixiEditor.DrawingApi.Core\PixiEditor.DrawingApi.Core.csproj" />
+    </ItemGroup>
+
 </Project>

+ 35 - 0
src/PixiEditor.DrawingApi.Skia/SkiaDrawingBackend.cs

@@ -0,0 +1,35 @@
+using PixiEditor.DrawingApi.Core.Bridge;
+using PixiEditor.DrawingApi.Core.Bridge.NativeObjectsImpl;
+using PixiEditor.DrawingApi.Core.Bridge.Operations;
+using PixiEditor.DrawingApi.Skia.Implementations;
+
+namespace PixiEditor.DrawingApi.Skia
+{
+    public class SkiaDrawingBackend : IDrawingBackend
+    {
+        public IColorImplementation ColorImplementation { get; }
+        public IImageImplementation ImageImplementation { get; }
+        public IImgDataImplementation ImgDataImplementation { get; }
+        public ICanvasOperations CanvasOperations { get; }
+        public IPaintImplementation PaintImplementation { get; }
+        public IVectorPathImplementation PathImplementation { get; }
+        public IMatrix3X3Implementation MatrixImplementation { get; }
+        public IPixmapImplementation PixmapImplementation { get; }
+        public ISurfaceOperations SurfaceOperations { get; }
+        public IColorSpaceImplementation ColorSpaceImplementation { get; }
+        public IBitmapImplementation BitmapImplementation { get; }
+        
+        public SkiaDrawingBackend()
+        {
+            ColorImplementation = new SkiaColorImplementation();
+            SkiaImgDataImplementation dataImpl = new SkiaImgDataImplementation();
+            ImgDataImplementation = dataImpl;
+            ImageImplementation = new SkiaImageImplementation(dataImpl);
+        }
+        
+        public void Setup()
+        {
+            
+        }
+    }
+}