SurfaceRenderer.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using PixiEditor.Models.DataHolders;
  2. using SkiaSharp;
  3. using System;
  4. using System.Windows;
  5. using System.Windows.Media;
  6. using System.Windows.Media.Imaging;
  7. namespace PixiEditor.Models.Controllers
  8. {
  9. class SurfaceRenderer : IDisposable
  10. {
  11. public SKSurface BackingSurface { get; private set; }
  12. public WriteableBitmap FinalBitmap { get; private set; }
  13. private SKPaint BlendingPaint { get; } = new SKPaint() { BlendMode = SKBlendMode.SrcOver };
  14. public SurfaceRenderer(int width, int height)
  15. {
  16. FinalBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Pbgra32, null);
  17. var imageInfo = new SKImageInfo(width, height, SKColorType.Bgra8888, SKAlphaType.Premul, SKColorSpace.CreateSrgb());
  18. BackingSurface = SKSurface.Create(imageInfo, FinalBitmap.BackBuffer, FinalBitmap.BackBufferStride);
  19. }
  20. public void Dispose()
  21. {
  22. BackingSurface.Dispose();
  23. BlendingPaint.Dispose();
  24. }
  25. public void Draw(Surface otherSurface, byte opacity)
  26. {
  27. BackingSurface.Canvas.Clear();
  28. FinalBitmap.Lock();
  29. BlendingPaint.Color = new SKColor(255, 255, 255, opacity);
  30. //otherSurface.SkiaSurface.Draw(BackingSurface.Canvas, 0, 0, BlendingPaint);
  31. BackingSurface.Canvas.DrawImage(otherSurface.SkiaSurface.Snapshot(), new SKRect(0, 0, FinalBitmap.PixelWidth, FinalBitmap.PixelHeight));
  32. FinalBitmap.AddDirtyRect(new Int32Rect(0, 0, FinalBitmap.PixelWidth, FinalBitmap.PixelHeight));
  33. FinalBitmap.Unlock();
  34. }
  35. }
  36. }