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
RenderFrameMailbox.cs
1namespace Gondwana.Rendering;
2
8internal sealed class RenderFrameMailbox : IDisposable
9{
10 internal enum State
11 {
15 Free,
16
20 Building,
21
25 Published,
26
30 Rendering,
31
35 Releasing
36 }
37
38 internal sealed class Slot
39 {
40 internal RenderFrameSnapshot? Frame { get; set; }
41 internal State Ownership;
42
43 internal Slot() { }
44 }
45
46 private readonly object _gate = new();
47 private readonly Slot[] _slots = [new(), new(), new()];
48 private Slot? _published;
49 private bool _closed;
50 private long _publishedCount;
51 private long _droppedCount;
52
53 internal (long Published, long Dropped, int InUse) Counters
54 {
55 get
56 {
57 lock (_gate)
58 return (_publishedCount, _droppedCount, _slots.Count(s => s.Ownership != State.Free));
59 }
60 }
61
62 internal Slot? TryBeginBuild()
63 {
64 lock (_gate)
65 {
66 if (_closed)
67 return null;
68
69 if (_slots.Any(s => s.Ownership == State.Building))
70 throw new InvalidOperationException("Only one snapshot producer is supported.");
71
72 var slot = _slots.FirstOrDefault(s => s.Ownership == State.Free);
73
74 if (slot is null)
75 return null;
76
77 slot.Ownership = State.Building;
78 return slot;
79 }
80 }
81
82 // Takes ownership of frame even if the mailbox was closed during construction.
83 internal void Publish(Slot slot, RenderFrameSnapshot frame)
84 {
85 Slot? retired;
86
87 lock (_gate)
88 {
89 Require(slot, State.Building);
90 slot.Frame = frame;
91
92 if (_closed)
93 {
94 slot.Ownership = State.Releasing;
95 retired = slot;
96 }
97 else
98 {
99 retired = _published;
100
101 if (retired is not null)
102 {
103 retired.Ownership = State.Releasing;
104 _droppedCount++;
105 }
106
107 slot.Ownership = State.Published;
108 _published = slot;
109 _publishedCount++;
110 }
111 }
112
113 if (retired is not null)
114 Recycle(retired);
115 }
116
117 internal void AbortBuild(Slot slot)
118 {
119 lock (_gate)
120 {
121 Require(slot, State.Building);
122 slot.Ownership = State.Free;
123 }
124 }
125
126 internal Slot? TryAcquire()
127 {
128 lock (_gate)
129 {
130 if (_closed || _published is null)
131 return null;
132
133 if (_slots.Any(s => s.Ownership == State.Rendering))
134 throw new InvalidOperationException("Only one snapshot consumer is supported.");
135
136 var slot = _published;
137 _published = null;
138 slot.Ownership = State.Rendering;
139 return slot;
140 }
141 }
142
143 internal void Release(Slot slot)
144 {
145 lock (_gate)
146 {
147 Require(slot, State.Rendering);
148 slot.Ownership = State.Releasing;
149 }
150
151 Recycle(slot);
152 }
153
154 private void Require(Slot slot, State state)
155 {
156 if (!Array.Exists(_slots, candidate => ReferenceEquals(candidate, slot)) || slot.Ownership != state)
157 throw new InvalidOperationException("Snapshot slot is not owned by this operation.");
158 }
159
160 private void Recycle(Slot slot)
161 {
162 try
163 {
164 slot.Frame?.Dispose();
165 }
166 finally
167 {
168 lock (_gate)
169 {
170 slot.Frame = null;
171 slot.Ownership = State.Free;
172 }
173 }
174 }
175
179 public void Dispose()
180 {
181 Slot? retired;
182
183 lock (_gate)
184 {
185 if (_closed)
186 return;
187
188 _closed = true;
189 retired = _published;
190 _published = null;
191
192 if (retired is not null)
193 retired.Ownership = State.Releasing;
194 // Building and Rendering belong to their callers until Publish/Abort/Release.
195 }
196
197 if (retired is not null)
198 Recycle(retired);
199 }
200}