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
AvaloniaBitmapRenderSurfaceAdapter.cs
1using System.Collections.Concurrent;
2using Avalonia;
3using Avalonia.Media.Imaging;
4using Avalonia.Platform;
5using Avalonia.Threading;
7using SkiaSharp;
8
10
18{
19 private readonly AvaloniaBitmapRenderSurfaceControl _control;
20 private readonly object _presentSync = new();
21
22 // Owned by the UI thread: created/replaced in SetBitmap on the UI thread.
23 private WriteableBitmap? _bitmap;
24
25 // Current rendered image and accumulated dirty area; guarded by _presentSync.
26 private SKImage? _currentImage;
27 private SKRectI _pendingSourceRect;
28 private bool _blitScheduled;
29
30 // Images queued for disposal; written from any thread (Present), drained on UI thread.
31 private readonly ConcurrentQueue<SKImage> _toDispose = new();
32
33 private bool _disposed;
34
41 : base(
42 Math.Max(1, (int)control.Bounds.Width),
43 Math.Max(1, (int)control.Bounds.Height),
44 initialSizeAvailable: false)
45 {
46 _control = control ?? throw new ArgumentNullException(nameof(control));
47 _control.SizeChanged += OnSizeChanged;
48 }
49
50 private void OnSizeChanged(object? sender, global::Avalonia.Controls.SizeChangedEventArgs e)
51 {
53 }
54
59 {
60 if (_disposed)
61 return;
62
63 var w = (int)_control.Bounds.Width;
64 var h = (int)_control.Bounds.Height;
65
66 if (w > 0 && h > 0)
68 }
69
77 public override void Present(SKImage bufferImage, SKRectI bufferRect, SKRect destRect)
78 {
79 var sourceRect = SKRectI.Intersect(
80 bufferRect,
81 new SKRectI(
82 0,
83 0,
84 bufferImage.Width,
85 bufferImage.Height));
86
87 if (sourceRect.IsEmpty)
88 {
89 bufferImage.Dispose();
90 return;
91 }
92
93 bool scheduleBlit;
94
95 lock (_presentSync)
96 {
97 if (_disposed)
98 {
99 bufferImage.Dispose();
100 return;
101 }
102
103 var old = _currentImage;
104 _currentImage = bufferImage;
105
106 if (!ReferenceEquals(old, _currentImage) && old is not null)
107 _toDispose.Enqueue(old);
108
109 _pendingSourceRect = Union(_pendingSourceRect, sourceRect);
110 scheduleBlit = !_blitScheduled;
111 _blitScheduled = true;
112 }
113
114 // Several presents may arrive before the UI thread runs. One scheduled blit copies the
115 // union of their dirty regions from the newest (complete) backbuffer snapshot.
116 if (scheduleBlit)
117 Dispatcher.UIThread.Post(BlitAndInvalidate, DispatcherPriority.Render);
118 }
119
120 private void BlitAndInvalidate()
121 {
122 SKImage? img;
123 SKRectI sourceRect;
124
125 lock (_presentSync)
126 {
127 if (_disposed)
128 {
129 _blitScheduled = false;
130 return;
131 }
132
133 img = _currentImage;
134 sourceRect = _pendingSourceRect;
135 _pendingSourceRect = SKRectI.Empty;
136 _blitScheduled = false;
137 }
138
139 if (img == null)
140 return;
141
142 try
143 {
144 var w = img.Width;
145 var h = img.Height;
146
147 if (w <= 0 || h <= 0)
148 return;
149
150 var imageBounds = new SKRectI(
151 0,
152 0,
153 w,
154 h);
155
156 sourceRect = SKRectI.Intersect(sourceRect, imageBounds);
157
158 if (sourceRect.IsEmpty)
159 return;
160
161 // A new bitmap has no retained pixels, so its first copy must cover the full image.
162 var bitmap = _bitmap;
163
164 if (bitmap == null || bitmap.PixelSize.Width != w || bitmap.PixelSize.Height != h)
165 {
166 bitmap?.Dispose();
167
168 bitmap = new WriteableBitmap(
169 new PixelSize(w, h),
170 new Vector(96, 96),
171 PixelFormat.Bgra8888,
172 AlphaFormat.Premul);
173
174 _bitmap = bitmap;
175 sourceRect = imageBounds;
176 }
177
178 // Copy only the accumulated dirty patch into its matching location in the bitmap.
179 bool copied;
180
181 using (var fb = bitmap.Lock())
182 {
183 var info = new SKImageInfo(
184 sourceRect.Width,
185 sourceRect.Height,
186 SKColorType.Bgra8888,
187 SKAlphaType.Premul);
188
189 var destinationOffset = checked((sourceRect.Top * fb.RowBytes) + (sourceRect.Left * 4));
190 var destination = IntPtr.Add(fb.Address, destinationOffset);
191
192 copied = img.ReadPixels(
193 info,
194 destination,
195 fb.RowBytes,
196 sourceRect.Left,
197 sourceRect.Top);
198 }
199
200 if (!copied)
201 return;
202
203 _control.SetBitmap(bitmap);
204 _control.InvalidateVisual();
205 }
206 finally
207 {
208 DisposeStaleImages();
209 }
210 }
211
212 private static SKRectI Union(SKRectI left, SKRectI right)
213 {
214 if (left.IsEmpty)
215 return right;
216
217 if (right.IsEmpty)
218 return left;
219
220 return new SKRectI(
221 Math.Min(left.Left, right.Left),
222 Math.Min(left.Top, right.Top),
223 Math.Max(left.Right, right.Right),
224 Math.Max(left.Bottom, right.Bottom));
225 }
226
227 private void DisposeStaleImages()
228 {
229 while (_toDispose.TryDequeue(out var stale))
230 stale.Dispose();
231 }
232
236 public void Dispose()
237 {
238 SKImage? currentImage;
239
240 lock (_presentSync)
241 {
242 if (_disposed)
243 return;
244
245 _disposed = true;
246
247 currentImage = _currentImage;
248 _currentImage = null;
249 _pendingSourceRect = SKRectI.Empty;
250 _blitScheduled = false;
251 }
252
253 _control.SizeChanged -= OnSizeChanged;
254
255 DisposeStaleImages();
256
257 currentImage?.Dispose();
258
259 _bitmap?.Dispose();
260 _bitmap = null;
261
262 GC.SuppressFinalize(this);
263 }
264}
override void Present(SKImage bufferImage, SKRectI bufferRect, SKRect destRect)
Presents the specified buffer image to the render surface by copying pixels into an Avalonia Writeabl...
void Dispose()
Releases all resources used by the AvaloniaBitmapRenderSurfaceAdapter.
AvaloniaBitmapRenderSurfaceAdapter(AvaloniaBitmapRenderSurfaceControl control)
Initializes a new instance of the AvaloniaBitmapRenderSurfaceAdapter class.
void RefreshDestinationSize()
Refreshes the destination size to match the control's current bounds.
An Avalonia Control that displays game frames produced by the Gondwana engine. Frames are received as...
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...
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.