LayerStackRenderer.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using PixiEditor.Helpers.Extensions;
  2. using PixiEditor.Models.DataHolders;
  3. using PixiEditor.Models.Layers;
  4. using PixiEditor.Models.Layers.Utils;
  5. using SkiaSharp;
  6. using System;
  7. using System.Collections.ObjectModel;
  8. using System.ComponentModel;
  9. using System.Windows;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. namespace PixiEditor.Models.Controllers
  13. {
  14. public class LayerStackRenderer : INotifyPropertyChanged, IDisposable
  15. {
  16. private SKPaint BlendingPaint { get; } = new SKPaint() { BlendMode = SKBlendMode.SrcOver };
  17. private SKPaint ClearPaint { get; } = new SKPaint() { BlendMode = SKBlendMode.Src, Color = SKColors.Transparent };
  18. private ObservableCollection<Layer> layers;
  19. private LayerStructure structure;
  20. private Surface finalSurface;
  21. private SKSurface backingSurface;
  22. private WriteableBitmap finalBitmap;
  23. public WriteableBitmap FinalBitmap
  24. {
  25. get => finalBitmap;
  26. set
  27. {
  28. finalBitmap = value;
  29. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FinalBitmap)));
  30. }
  31. }
  32. public Surface FinalSurface { get => finalSurface; }
  33. public event PropertyChangedEventHandler PropertyChanged;
  34. public LayerStackRenderer(ObservableCollection<Layer> layers, LayerStructure structure, int width, int height)
  35. {
  36. this.layers = layers;
  37. this.structure = structure;
  38. layers.CollectionChanged += OnLayersChanged;
  39. SubscribeToAllLayers(layers);
  40. Resize(width, height);
  41. }
  42. public void Resize(int newWidth, int newHeight)
  43. {
  44. finalSurface?.Dispose();
  45. backingSurface?.Dispose();
  46. finalSurface = new Surface(newWidth, newHeight);
  47. FinalBitmap = new WriteableBitmap(newWidth, newHeight, 96, 96, PixelFormats.Pbgra32, null);
  48. var imageInfo = new SKImageInfo(newWidth, newHeight, SKColorType.Bgra8888, SKAlphaType.Premul, SKColorSpace.CreateSrgb());
  49. backingSurface = SKSurface.Create(imageInfo, finalBitmap.BackBuffer, finalBitmap.BackBufferStride);
  50. Update(new Int32Rect(0, 0, newWidth, newHeight));
  51. }
  52. public void SetNewLayersCollection(ObservableCollection<Layer> layers)
  53. {
  54. layers.CollectionChanged -= OnLayersChanged;
  55. UnsubscribeFromAllLayers(this.layers);
  56. this.layers = layers;
  57. SubscribeToAllLayers(layers);
  58. layers.CollectionChanged += OnLayersChanged;
  59. Update(new Int32Rect(0, 0, finalSurface.Width, finalSurface.Height));
  60. }
  61. public void Dispose()
  62. {
  63. finalSurface.Dispose();
  64. backingSurface.Dispose();
  65. BlendingPaint.Dispose();
  66. ClearPaint.Dispose();
  67. layers.CollectionChanged -= OnLayersChanged;
  68. }
  69. private void SubscribeToAllLayers(ObservableCollection<Layer> layers)
  70. {
  71. foreach (var layer in layers)
  72. {
  73. layer.LayerBitmapChanged += OnLayerBitmapChanged;
  74. }
  75. }
  76. private void UnsubscribeFromAllLayers(ObservableCollection<Layer> layers)
  77. {
  78. foreach (var layer in layers)
  79. {
  80. layer.LayerBitmapChanged -= OnLayerBitmapChanged;
  81. }
  82. }
  83. private void Update(Int32Rect dirtyRectangle)
  84. {
  85. //finalSurface.SkiaSurface.Canvas.Clear();
  86. dirtyRectangle = dirtyRectangle.Intersect(new Int32Rect(0, 0, finalBitmap.PixelWidth, finalBitmap.PixelHeight));
  87. finalSurface.SkiaSurface.Canvas.DrawRect(
  88. new SKRect(
  89. dirtyRectangle.X, dirtyRectangle.Y,
  90. dirtyRectangle.X + dirtyRectangle.Width,
  91. dirtyRectangle.Y + dirtyRectangle.Height
  92. ),
  93. ClearPaint
  94. );
  95. foreach (var layer in layers)
  96. {
  97. if (!LayerStructureUtils.GetFinalLayerIsVisible(layer, structure))
  98. continue;
  99. BlendingPaint.Color = new SKColor(255, 255, 255, (byte)(LayerStructureUtils.GetFinalLayerOpacity(layer, structure) * 255));
  100. Int32Rect layerRect = new Int32Rect(layer.OffsetX, layer.OffsetY, layer.Width, layer.Height);
  101. Int32Rect layerPortion = layerRect.Intersect(dirtyRectangle);
  102. using (var snapshot = layer.LayerBitmap.SkiaSurface.Snapshot())
  103. {
  104. finalSurface.SkiaSurface.Canvas.DrawImage(
  105. snapshot,
  106. new SKRect(
  107. layerPortion.X - layer.OffsetX,
  108. layerPortion.Y - layer.OffsetY,
  109. layerPortion.X - layer.OffsetX + layerPortion.Width,
  110. layerPortion.Y - layer.OffsetY + layerPortion.Height),
  111. new SKRect(
  112. layerPortion.X,
  113. layerPortion.Y,
  114. layerPortion.X + layerPortion.Width,
  115. layerPortion.Y + layerPortion.Height
  116. ),
  117. BlendingPaint);
  118. }
  119. }
  120. finalBitmap.Lock();
  121. using (var snapshot = finalSurface.SkiaSurface.Snapshot())
  122. {
  123. SKRect rect = new(dirtyRectangle.X, dirtyRectangle.Y, dirtyRectangle.X + dirtyRectangle.Width, dirtyRectangle.Y + dirtyRectangle.Height);
  124. backingSurface.Canvas.DrawImage(snapshot, rect, rect, Surface.ReplacingPaint);
  125. }
  126. finalBitmap.AddDirtyRect(dirtyRectangle);
  127. finalBitmap.Unlock();
  128. }
  129. private void OnLayersChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  130. {
  131. if (e.NewItems != null)
  132. {
  133. foreach (var obj in e.NewItems)
  134. {
  135. Layer layer = (Layer)obj;
  136. layer.LayerBitmapChanged += OnLayerBitmapChanged;
  137. }
  138. }
  139. if (e.OldItems != null)
  140. {
  141. foreach (var obj in e.OldItems)
  142. {
  143. ((Layer)obj).LayerBitmapChanged -= OnLayerBitmapChanged;
  144. }
  145. }
  146. Update(new Int32Rect(0, 0, finalSurface.Width, finalSurface.Height));
  147. }
  148. private void OnLayerBitmapChanged(object sender, Int32Rect e)
  149. {
  150. Update(e);
  151. }
  152. }
  153. }