Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
WinFormBitmapRenderSurfaceAdapter.cs
1using System.Collections.Concurrent;
3using SkiaSharp;
4using SkiaSharp.Views.Desktop;
5
7
12{
13 private readonly SKControl _control;
14 private readonly object _presentSync = new();
15
16 private SKImage? _currentImage;
17 private readonly ConcurrentQueue<SKImage> _toDispose = new();
18 private bool _disposed;
19
25 public WinFormBitmapRenderSurfaceAdapter(SKControl control)
26 : base(
27 Math.Max(1, control.ClientSize.Width),
28 Math.Max(1, control.ClientSize.Height),
29 initialSizeAvailable: control.IsHandleCreated)
30 {
31 _control = control ?? throw new ArgumentNullException(nameof(control));
32
33 _control.PaintSurface += OnPaintSurface;
34 _control.HandleCreated += OnHandleCreated;
35 _control.ClientSizeChanged += OnSizeChanged;
36
37 // If the handle already exists, schedule one initial sync
38 if (_control.IsHandleCreated)
39 _control.BeginInvoke((Action)RefreshDestinationSize);
40 }
41
42 private void OnHandleCreated(object? s, EventArgs e) => RefreshDestinationSize();
43
44 private void OnSizeChanged(object? s, EventArgs e) => RefreshDestinationSize();
45
50 {
51 if (_control.IsDisposed || !_control.IsHandleCreated)
52 return;
53
54 var sz = _control.ClientSize; // ← ClientSize, not Width/Height
55 SetDestinationSize(sz.Width, sz.Height);
56 _control.Invalidate();
57 }
58
65 public override void Present(SKImage bufferImage, SKRectI bufferRect, SKRect destRect)
66 {
67 // No UI target — dispose immediately to avoid leak
68 if (_control.IsDisposed || !_control.IsHandleCreated)
69 {
70 bufferImage.Dispose();
71 return;
72 }
73
74 var sourceRect = SKRectI.Intersect(
75 bufferRect,
76 new SKRectI(
77 0,
78 0,
79 bufferImage.Width,
80 bufferImage.Height));
81
82 var dirty = global::System.Drawing.Rectangle.FromLTRB(
83 sourceRect.Left,
84 sourceRect.Top,
85 sourceRect.Right,
86 sourceRect.Bottom);
87
88 // Expand for the adjacent texels sampled by linear filtering, then round physical edges outwards.
89 dirty.Inflate(1, 1);
90
91 var invalidateRect = global::System.Drawing.Rectangle.Intersect(
92 Presentation.ScreenRectToAdapterRect(dirty),
93 _control.ClientRectangle);
94
95 if (sourceRect == new SKRectI(
96 0,
97 0,
98 bufferImage.Width,
99 bufferImage.Height))
100 invalidateRect = _control.ClientRectangle;
101
102 if (sourceRect.IsEmpty || invalidateRect.IsEmpty)
103 {
104 bufferImage.Dispose();
105 return;
106 }
107
108 lock (_presentSync)
109 {
110 if (_disposed)
111 {
112 bufferImage.Dispose();
113 return;
114 }
115
116 // Swap into current; old one is disposed after the paint that consumes the newest
117 // complete snapshot. Windows coalesces physical invalidation rectangles.
118 var old = _currentImage;
119 _currentImage = bufferImage;
120
121 if (!ReferenceEquals(old, _currentImage) && old is not null)
122 _toDispose.Enqueue(old);
123 }
124
125 _control.Invalidate(invalidateRect);
126 }
127
128 private void OnPaintSurface(object? sender, SKPaintSurfaceEventArgs e)
129 {
130 var canvas = e.Surface.Canvas;
131
132 SKImage? img;
133
134 lock (_presentSync)
135 {
136 img = _currentImage;
137 }
138
139 try
140 {
141 if (img == null)
142 return;
143
144 // Retain the complete image for OS expose/resize paints with no new dirty patch.
145 DrawImage(canvas, img, SKColors.Black);
146 }
147 finally
148 {
149 DisposeStaleImages();
150 }
151 }
152
153 private void DisposeStaleImages()
154 {
155 while (_toDispose.TryDequeue(out var stale))
156 stale.Dispose();
157 }
158
162 public void Dispose()
163 {
164 SKImage? currentImage;
165
166 lock (_presentSync)
167 {
168 if (_disposed)
169 return;
170
171 _disposed = true;
172
173 currentImage = _currentImage;
174 _currentImage = null;
175 }
176
177 if (!_control.IsDisposed)
178 {
179 _control.PaintSurface -= OnPaintSurface;
180 _control.HandleCreated -= OnHandleCreated;
181 _control.ClientSizeChanged -= OnSizeChanged;
182 }
183
184 DisposeStaleImages();
185
186 currentImage?.Dispose();
187 GC.SuppressFinalize(this);
188 }
189}
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.
void RefreshDestinationSize()
Refreshes the destination size based on the current client size of the control.
override void Present(SKImage bufferImage, SKRectI bufferRect, SKRect destRect)
Presents the specified buffer image to the render surface.
void Dispose()
Releases all resources used by the WinFormBitmapRenderSurfaceAdapter.
WinFormBitmapRenderSurfaceAdapter(SKControl control)
Initializes a new instance of the WinFormBitmapRenderSurfaceAdapter class.