1using System.Collections.Concurrent;
4using SkiaSharp.Views.Desktop;
13 private readonly SKControl _control;
14 private readonly
object _presentSync =
new();
16 private SKImage? _currentImage;
17 private readonly ConcurrentQueue<SKImage> _toDispose =
new();
18 private bool _disposed;
27 Math.Max(1, control.ClientSize.
Width),
28 Math.Max(1, control.ClientSize.
Height),
29 initialSizeAvailable: control.IsHandleCreated)
31 _control = control ??
throw new ArgumentNullException(nameof(control));
33 _control.PaintSurface += OnPaintSurface;
34 _control.HandleCreated += OnHandleCreated;
35 _control.ClientSizeChanged += OnSizeChanged;
38 if (_control.IsHandleCreated)
51 if (_control.IsDisposed || !_control.IsHandleCreated)
54 var sz = _control.ClientSize;
56 _control.Invalidate();
65 public override void Present(SKImage bufferImage, SKRectI bufferRect, SKRect destRect)
68 if (_control.IsDisposed || !_control.IsHandleCreated)
70 bufferImage.Dispose();
74 var sourceRect = SKRectI.Intersect(
82 var dirty = global::System.Drawing.Rectangle.FromLTRB(
91 var invalidateRect = global::System.Drawing.Rectangle.Intersect(
93 _control.ClientRectangle);
95 if (sourceRect ==
new SKRectI(
100 invalidateRect = _control.ClientRectangle;
102 if (sourceRect.IsEmpty || invalidateRect.IsEmpty)
104 bufferImage.Dispose();
112 bufferImage.Dispose();
118 var old = _currentImage;
119 _currentImage = bufferImage;
121 if (!ReferenceEquals(old, _currentImage) && old is not
null)
122 _toDispose.Enqueue(old);
125 _control.Invalidate(invalidateRect);
128 private void OnPaintSurface(
object? sender, SKPaintSurfaceEventArgs e)
130 var canvas = e.Surface.Canvas;
149 DisposeStaleImages();
153 private void DisposeStaleImages()
155 while (_toDispose.TryDequeue(out var stale))
164 SKImage? currentImage;
173 currentImage = _currentImage;
174 _currentImage =
null;
177 if (!_control.IsDisposed)
179 _control.PaintSurface -= OnPaintSurface;
180 _control.HandleCreated -= OnHandleCreated;
181 _control.ClientSizeChanged -= OnSizeChanged;
184 DisposeStaleImages();
186 currentImage?.Dispose();
187 GC.SuppressFinalize(
this);
int Width
Gets the current width of the render surface in pixels.
void SetDestinationSize(int destWidth, int destHeight)
Sets the destination size of the render surface and raises the Resized event if the dimensions have c...
PresentationTransform Presentation
Current immutable aspect-preserving mapping of Backbuffer ScreenPx to adapter pixels.
RenderSurfaceAdapterBase(int destWidth, int destHeight)
Initializes a new instance of the RenderSurfaceAdapterBase class with the specified dimensions.
int Height
Gets the current height of the render surface in pixels.
void DrawImage(SKCanvas canvas, SKImage image, SKColor clearColor)
Presents a complete image using the same transform as pointer normalization.