8internal sealed class RenderFrameMailbox : IDisposable
38 internal sealed class Slot
40 internal RenderFrameSnapshot? Frame {
get;
set; }
41 internal State Ownership;
46 private readonly
object _gate =
new();
47 private readonly Slot[] _slots = [
new(),
new(),
new()];
48 private Slot? _published;
50 private long _publishedCount;
51 private long _droppedCount;
53 internal (
long Published,
long Dropped,
int InUse) Counters
58 return (_publishedCount, _droppedCount, _slots.Count(s => s.Ownership != State.Free));
62 internal Slot? TryBeginBuild()
69 if (_slots.Any(s => s.Ownership == State.Building))
70 throw new InvalidOperationException(
"Only one snapshot producer is supported.");
72 var slot = _slots.FirstOrDefault(s => s.Ownership == State.Free);
77 slot.Ownership = State.Building;
83 internal void Publish(Slot slot, RenderFrameSnapshot frame)
89 Require(slot, State.Building);
94 slot.Ownership = State.Releasing;
101 if (retired is not
null)
103 retired.Ownership = State.Releasing;
107 slot.Ownership = State.Published;
113 if (retired is not
null)
117 internal void AbortBuild(Slot slot)
121 Require(slot, State.Building);
122 slot.Ownership = State.Free;
126 internal Slot? TryAcquire()
130 if (_closed || _published is
null)
133 if (_slots.Any(s => s.Ownership == State.Rendering))
134 throw new InvalidOperationException(
"Only one snapshot consumer is supported.");
136 var slot = _published;
138 slot.Ownership = State.Rendering;
143 internal void Release(Slot slot)
147 Require(slot, State.Rendering);
148 slot.Ownership = State.Releasing;
154 private void Require(Slot slot, State state)
156 if (!Array.Exists(_slots, candidate => ReferenceEquals(candidate, slot)) || slot.Ownership != state)
157 throw new InvalidOperationException(
"Snapshot slot is not owned by this operation.");
160 private void Recycle(Slot slot)
164 slot.Frame?.Dispose();
171 slot.Ownership = State.Free;
179 public void Dispose()
189 retired = _published;
192 if (retired is not
null)
193 retired.Ownership = State.Releasing;
197 if (retired is not
null)