Browse Source

Some fixes

flabbet 11 tháng trước cách đây
mục cha
commit
19008bf2c2

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

@@ -21,5 +21,9 @@ public class Program
                 RenderingMode = new Win32RenderingMode[] { Win32RenderingMode.Wgl, Win32RenderingMode.AngleEgl },
                 RenderingMode = new Win32RenderingMode[] { Win32RenderingMode.Wgl, Win32RenderingMode.AngleEgl },
                 OverlayPopups = true
                 OverlayPopups = true
             })
             })
+            .With(new SkiaOptions()
+            {
+                MaxGpuResourceSizeBytes = 1024 * 1024 * 1024,
+            })
             .LogToTrace();
             .LogToTrace();
 }
 }

+ 5 - 4
src/PixiEditor.DrawingApi.Core/Texture.cs

@@ -39,7 +39,7 @@ public class Texture : IDisposable
 
 
         Texture texture = new Texture(image.Size);
         Texture texture = new Texture(image.Size);
         texture.GpuSurface.Canvas.DrawImage(image, 0, 0);
         texture.GpuSurface.Canvas.DrawImage(image, 0, 0);
-
+        
         return texture;
         return texture;
     }
     }
 
 
@@ -48,7 +48,7 @@ public class Texture : IDisposable
         using Image image = Image.FromEncodedData(data);
         using Image image = Image.FromEncodedData(data);
         Texture texture = new Texture(image.Size);
         Texture texture = new Texture(image.Size);
         texture.GpuSurface.Canvas.DrawImage(image, 0, 0);
         texture.GpuSurface.Canvas.DrawImage(image, 0, 0);
-
+        
         return texture;
         return texture;
     }
     }
 
 
@@ -60,7 +60,7 @@ public class Texture : IDisposable
 
 
         var surface = new Texture(new VecI(image.Width, image.Height));
         var surface = new Texture(new VecI(image.Width, image.Height));
         surface.GpuSurface.Canvas.DrawImage(image, 0, 0);
         surface.GpuSurface.Canvas.DrawImage(image, 0, 0);
-
+        
         return surface;
         return surface;
     }
     }
 
 
@@ -81,6 +81,7 @@ public class Texture : IDisposable
         paint.FilterQuality = filterQuality;
         paint.FilterQuality = filterQuality;
 
 
         newTexture.GpuSurface.Canvas.DrawImage(image, new RectD(0, 0, newSize.X, newSize.Y), paint);
         newTexture.GpuSurface.Canvas.DrawImage(image, new RectD(0, 0, newSize.X, newSize.Y), paint);
+        
         return newTexture;
         return newTexture;
     }
     }
 
 
@@ -88,7 +89,7 @@ public class Texture : IDisposable
     {
     {
         if (vecI.X < 0 || vecI.X >= Size.X || vecI.Y < 0 || vecI.Y >= Size.Y)
         if (vecI.X < 0 || vecI.X >= Size.X || vecI.Y < 0 || vecI.Y >= Size.Y)
             return null;
             return null;
-        
+
         //TODO:
         //TODO:
         return Color.Empty;
         return Color.Empty;
     }
     }

+ 31 - 24
src/PixiEditor.DrawingApi.Skia/Implementations/SkiaSurfaceImplementation.cs

@@ -17,14 +17,15 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
 
 
         public GRContext GrContext { get; set; }
         public GRContext GrContext { get; set; }
 
 
-        public SkiaSurfaceImplementation(GRContext context, SkiaPixmapImplementation pixmapImplementation, SkiaCanvasImplementation canvasImplementation, SkiaPaintImplementation paintImplementation)
+        public SkiaSurfaceImplementation(GRContext context, SkiaPixmapImplementation pixmapImplementation,
+            SkiaCanvasImplementation canvasImplementation, SkiaPaintImplementation paintImplementation)
         {
         {
             _pixmapImplementation = pixmapImplementation;
             _pixmapImplementation = pixmapImplementation;
             _canvasImplementation = canvasImplementation;
             _canvasImplementation = canvasImplementation;
             _paintImplementation = paintImplementation;
             _paintImplementation = paintImplementation;
             GrContext = context;
             GrContext = context;
         }
         }
-        
+
         public Pixmap PeekPixels(DrawingSurface drawingSurface)
         public Pixmap PeekPixels(DrawingSurface drawingSurface)
         {
         {
             SKPixmap pixmap = ManagedInstances[drawingSurface.ObjectPointer].PeekPixels();
             SKPixmap pixmap = ManagedInstances[drawingSurface.ObjectPointer].PeekPixels();
@@ -32,11 +33,12 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
             {
             {
                 return drawingSurface.Snapshot().PeekPixels();
                 return drawingSurface.Snapshot().PeekPixels();
             }
             }
-            
+
             return _pixmapImplementation.CreateFrom(pixmap);
             return _pixmapImplementation.CreateFrom(pixmap);
         }
         }
 
 
-        public bool ReadPixels(DrawingSurface drawingSurface, ImageInfo dstInfo, IntPtr dstPixels, int dstRowBytes, int srcX,
+        public bool ReadPixels(DrawingSurface drawingSurface, ImageInfo dstInfo, IntPtr dstPixels, int dstRowBytes,
+            int srcX,
             int srcY)
             int srcY)
         {
         {
             return ManagedInstances[drawingSurface.ObjectPointer]
             return ManagedInstances[drawingSurface.ObjectPointer]
@@ -50,24 +52,29 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
             var instance = ManagedInstances[drawingSurface.ObjectPointer];
             var instance = ManagedInstances[drawingSurface.ObjectPointer];
             instance.Draw(canvas, x, y, paint);
             instance.Draw(canvas, x, y, paint);
         }
         }
-        
+
         public DrawingSurface Create(ImageInfo imageInfo, IntPtr pixels, int rowBytes)
         public DrawingSurface Create(ImageInfo imageInfo, IntPtr pixels, int rowBytes)
         {
         {
-            SKSurface skSurface = CreateSkiaSurface(imageInfo.Size, imageInfo.GpuBacked);
-            
+            SKSurface skSurface = CreateSkiaSurface(imageInfo.ToSkImageInfo(), imageInfo.GpuBacked);
+
+            using var image = SKImage.FromPixelCopy(imageInfo.ToSkImageInfo(), pixels, rowBytes);
+
             var canvas = skSurface.Canvas;
             var canvas = skSurface.Canvas;
-            canvas.DrawImage(SKImage.FromPixelCopy(imageInfo.ToSkImageInfo(), pixels, rowBytes), new SKPoint(0, 0));
-            
+            canvas.DrawImage(image, new SKPoint(0, 0));
+
             return CreateDrawingSurface(skSurface);
             return CreateDrawingSurface(skSurface);
         }
         }
 
 
         public DrawingSurface Create(ImageInfo imageInfo, IntPtr pixelBuffer)
         public DrawingSurface Create(ImageInfo imageInfo, IntPtr pixelBuffer)
         {
         {
-            SKSurface skSurface = CreateSkiaSurface(imageInfo.Size, imageInfo.GpuBacked);
-            
+            SKImageInfo info = imageInfo.ToSkImageInfo();
+            SKSurface skSurface = CreateSkiaSurface(info, imageInfo.GpuBacked);
+
+            using var image = SKImage.FromPixelCopy(info, pixelBuffer);
+
             var canvas = skSurface.Canvas;
             var canvas = skSurface.Canvas;
-            canvas.DrawImage(SKImage.FromPixelCopy(imageInfo.ToSkImageInfo(), pixelBuffer), new SKPoint(0, 0));
-            
+            canvas.DrawImage(image, new SKPoint(0, 0));
+
             return CreateDrawingSurface(skSurface);
             return CreateDrawingSurface(skSurface);
         }
         }
 
 
@@ -75,28 +82,28 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
         {
         {
             SKPixmap skPixmap = _pixmapImplementation[pixmap.ObjectPointer];
             SKPixmap skPixmap = _pixmapImplementation[pixmap.ObjectPointer];
             SKImageInfo info = skPixmap.Info;
             SKImageInfo info = skPixmap.Info;
-            SKSurface skSurface = CreateSkiaSurface(new VecI(info.Width, info.Height), true);
-            
+            SKSurface skSurface = CreateSkiaSurface(info, true);
+
             var canvas = skSurface.Canvas;
             var canvas = skSurface.Canvas;
             canvas.DrawImage(SKImage.FromPixels(skPixmap), new SKPoint(0, 0));
             canvas.DrawImage(SKImage.FromPixels(skPixmap), new SKPoint(0, 0));
-            
+
             return CreateDrawingSurface(skSurface);
             return CreateDrawingSurface(skSurface);
         }
         }
 
 
         public DrawingSurface Create(ImageInfo imageInfo)
         public DrawingSurface Create(ImageInfo imageInfo)
         {
         {
-            SKSurface skSurface = CreateSkiaSurface(imageInfo.Size, imageInfo.GpuBacked);
+            SKSurface skSurface = CreateSkiaSurface(imageInfo.ToSkImageInfo(), imageInfo.GpuBacked);
             return CreateDrawingSurface(skSurface);
             return CreateDrawingSurface(skSurface);
         }
         }
 
 
-        private SKSurface CreateSkiaSurface(VecI size, bool gpu)
+        private SKSurface CreateSkiaSurface(SKImageInfo info, bool gpu)
         {
         {
             if (!gpu)
             if (!gpu)
             {
             {
-                return SKSurface.Create(new SKImageInfo(size.X, size.Y));
+                return SKSurface.Create(info);
             }
             }
-            
-            return SKSurface.Create(GrContext, false, new SKImageInfo(size.X, size.Y));
+
+            return SKSurface.Create(GrContext, false, info);
         }
         }
 
 
         public void Dispose(DrawingSurface drawingSurface)
         public void Dispose(DrawingSurface drawingSurface)
@@ -117,13 +124,13 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
 
 
             DrawingSurface surface = new DrawingSurface(skSurface.Handle, canvas);
             DrawingSurface surface = new DrawingSurface(skSurface.Handle, canvas);
             ManagedInstances[skSurface.Handle] = skSurface;
             ManagedInstances[skSurface.Handle] = skSurface;
-            
+
             return surface;
             return surface;
         }
         }
-        
+
         public void Flush(DrawingSurface drawingSurface)
         public void Flush(DrawingSurface drawingSurface)
         {
         {
-            ManagedInstances[drawingSurface.ObjectPointer].Flush(true);
+            ManagedInstances[drawingSurface.ObjectPointer].Flush(true, true);
         }
         }
     }
     }
 }
 }

+ 1 - 1
src/PixiEditor/ViewModels/Document/ReferenceLayerViewModel.cs

@@ -114,7 +114,7 @@ internal class ReferenceLayerViewModel : ObservableObject, IReferenceLayerHandle
     
     
     public void SetReferenceLayer(ImmutableArray<byte> imageBgra8888Bytes, VecI imageSize, ShapeCorners shape)
     public void SetReferenceLayer(ImmutableArray<byte> imageBgra8888Bytes, VecI imageSize, ShapeCorners shape)
     {
     {
-        ReferenceBitmap = Texture.Load(imageBgra8888Bytes.ToArray(), ColorType.Bgra8888, imageSize); //TODO: Was WriteableBitmapUtility.FromBgra8888Array(imageBgra8888Bytes.ToArray(), imageSize);
+        ReferenceBitmap = Texture.Load(imageBgra8888Bytes.ToArray(), ColorType.Bgra8888, imageSize); 
         referenceShape = shape;
         referenceShape = shape;
         isVisible = true;
         isVisible = true;
         isTransforming = false;
         isTransforming = false;

+ 8 - 5
src/PixiEditor/Views/Overlays/ReferenceLayerOverlay.cs

@@ -24,6 +24,7 @@ namespace PixiEditor.Views.Overlays;
 internal class ReferenceLayerOverlay : Overlay
 internal class ReferenceLayerOverlay : Overlay
 {
 {
     private const float OpacityTransitionDuration = 0.1f;
     private const float OpacityTransitionDuration = 0.1f;
+
     public static readonly StyledProperty<ReferenceLayerViewModel> ReferenceLayerProperty =
     public static readonly StyledProperty<ReferenceLayerViewModel> ReferenceLayerProperty =
         AvaloniaProperty.Register<ReferenceLayerOverlay, ReferenceLayerViewModel>(
         AvaloniaProperty.Register<ReferenceLayerOverlay, ReferenceLayerViewModel>(
             nameof(ReferenceLayerViewModel));
             nameof(ReferenceLayerViewModel));
@@ -83,19 +84,21 @@ internal class ReferenceLayerOverlay : Overlay
 
 
             RectD dirty = new RectD(0, 0, ReferenceLayer.ReferenceBitmap.Size.X, ReferenceLayer.ReferenceBitmap.Size.Y);
             RectD dirty = new RectD(0, 0, ReferenceLayer.ReferenceBitmap.Size.X, ReferenceLayer.ReferenceBitmap.Size.Y);
             Rect dirtyRect = new Rect(dirty.X, dirty.Y, dirty.Width, dirty.Height);
             Rect dirtyRect = new Rect(dirty.X, dirty.Y, dirty.Width, dirty.Height);
-            
+
             double opacity = Opacity;
             double opacity = Opacity;
             var referenceBitmap = ReferenceLayer.ReferenceBitmap;
             var referenceBitmap = ReferenceLayer.ReferenceBitmap;
+
             DrawTextureOperation drawOperation =
             DrawTextureOperation drawOperation =
-                new DrawTextureOperation(dirtyRect, Stretch.None, ReferenceLayer.ReferenceBitmap.Size, canvas =>
+                new DrawTextureOperation(dirtyRect, Stretch.None, referenceBitmap/*, canvas =>
                 {
                 {
                     using Paint opacityPaint = new Paint();
                     using Paint opacityPaint = new Paint();
                     opacityPaint.Color = new Color(255, 255, 255, (byte)(255 * opacity));
                     opacityPaint.Color = new Color(255, 255, 255, (byte)(255 * opacity));
                     opacityPaint.BlendMode = BlendMode.SrcOver;
                     opacityPaint.BlendMode = BlendMode.SrcOver;
 
 
-                    canvas.DrawSurface(referenceBitmap.GpuSurface.Native as SKSurface, 0, 0, opacityPaint.Native as SKPaint);
-                });
-            
+                    canvas.DrawSurface(referenceBitmap.GpuSurface.Native as SKSurface, 0, 0,
+                        opacityPaint.Native as SKPaint);
+                }*/);
+
             context.Custom(drawOperation);
             context.Custom(drawOperation);
 
 
             matrix.Dispose();
             matrix.Dispose();

+ 9 - 11
src/PixiEditor/Views/Visuals/TextureControl.cs

@@ -89,11 +89,7 @@ public class TextureControl : Control
         ICustomDrawOperation drawOperation = new DrawTextureOperation(
         ICustomDrawOperation drawOperation = new DrawTextureOperation(
             new Rect(0, 0, Bounds.Width, Bounds.Height),
             new Rect(0, 0, Bounds.Width, Bounds.Height),
             Stretch,
             Stretch,
-            texture.Size,
-            canvas =>
-            {
-                canvas.DrawSurface(texture.GpuSurface.Native as SKSurface, 0, 0);
-            });
+            texture);
 
 
         context.Custom(drawOperation);
         context.Custom(drawOperation);
     }
     }
@@ -101,27 +97,29 @@ public class TextureControl : Control
 
 
 internal class DrawTextureOperation : SkiaDrawOperation
 internal class DrawTextureOperation : SkiaDrawOperation
 {
 {
-    public event Action<SKCanvas> Draw;
     public Stretch Stretch { get; }
     public Stretch Stretch { get; }
     public VecD TargetSize { get; }
     public VecD TargetSize { get; }
+    public Texture Texture { get; }
 
 
-    public DrawTextureOperation(Rect dirtyBounds, Stretch stretch, VecD targetSize, Action<SKCanvas> draw) :
+    public DrawTextureOperation(Rect dirtyBounds, Stretch stretch, Texture texture) :
         base(dirtyBounds)
         base(dirtyBounds)
     {
     {
-        Draw += draw;
         Stretch = stretch;
         Stretch = stretch;
-        TargetSize = targetSize;
+        Texture = texture;
+        TargetSize = new VecD(texture.Size.X, texture.Size.Y);
     }
     }
 
 
     public override void Render(ISkiaSharpApiLease lease)
     public override void Render(ISkiaSharpApiLease lease)
     {
     {
         SKCanvas canvas = lease.SkCanvas;
         SKCanvas canvas = lease.SkCanvas;
         
         
+        Texture.GpuSurface.Canvas.Flush();
+        
         (DrawingBackendApi.Current.SurfaceImplementation as SkiaSurfaceImplementation).GrContext = lease.GrContext;
         (DrawingBackendApi.Current.SurfaceImplementation as SkiaSurfaceImplementation).GrContext = lease.GrContext;
-
+        
         canvas.Save();
         canvas.Save();
         ScaleCanvas(canvas);
         ScaleCanvas(canvas);
-        Draw?.Invoke(lease.SkCanvas);
+        canvas.DrawSurface(Texture.GpuSurface.Native as SKSurface, 0, 0); 
         canvas.Restore();
         canvas.Restore();
     }
     }