19 internal RenderFrameMailbox FrameMailbox {
get; } =
new();
21 internal virtual void ProduceRenderFrameSnapshot(
long tick) { }
34 RenderSurfaceHostRegistry.Register(
this);
51 internal virtual void RequestRenderScale(
float scale) { }
52 internal virtual void InvalidatePresentation() { }
120 if (UsesRenderFrameSnapshots)
121 return ReplayRenderFrameSnapshot();
123 bool collectSynchronizationDiagnostics =
126 long waitStarted = collectSynchronizationDiagnostics
127 ? HighResTimer.GetCurrentTick()
130 long lockAcquired = 0;
131 long lockReleased = 0;
134 RenderStateSynchronization.EnterGpuRender();
138 if (collectSynchronizationDiagnostics)
139 lockAcquired = HighResTimer.GetCurrentTick();
141 var tick = HighResTimer.GetCurrentTick();
143 RenderToBackbuffer(tick);
150 if (collectSynchronizationDiagnostics)
151 lockReleased = HighResTimer.GetCurrentTick();
155 RenderStateSynchronization.ExitGpuRender();
158 if (collectSynchronizationDiagnostics)
162 HighResTimer.GetDuration(waitStarted, lockAcquired) * 1000d,
163 HighResTimer.GetDuration(lockAcquired, lockReleased) * 1000d));
169 private SKImage? ReplayRenderFrameSnapshot()
172 long telemetryGeneration =
Telemetry?.BeginSample() ?? 0;
173 bool collectDiagnostics = diagnostics is not
null || telemetryGeneration != 0;
174 var slot = FrameMailbox.TryAcquire();
179 long acquired = !collectDiagnostics ? 0 : HighResTimer.GetCurrentTick();
180 var counters = !collectDiagnostics ? default : FrameMailbox.Counters;
181 long replayEnd = acquired;
182 bool replayCompleted =
false;
183 long pictureReplayTicks = 0;
184 long backbufferFlushTicks = 0;
185 long snapshotTicks = 0;
186 int commands = !collectDiagnostics ? 0 : slot.Frame!.CommandCount;
187 double age = !collectDiagnostics ? 0 : HighResTimer.GetDuration(slot.Frame!.ProducedTick, acquired) * 1000d;
191 var frame = slot.Frame!;
197 long snapshotStart = !collectDiagnostics ? 0 : HighResTimer.GetCurrentTick();
200 if (collectDiagnostics)
201 snapshotTicks = HighResTimer.GetCurrentTick() - snapshotStart;
203 return resizedSnapshot;
208 long pictureStart = !collectDiagnostics ? 0 : HighResTimer.GetCurrentTick();
210 long pictureEnd = !collectDiagnostics ? 0 : HighResTimer.GetCurrentTick();
213 long flushEnd = !collectDiagnostics ? 0 : HighResTimer.GetCurrentTick();
214 replayCompleted =
true;
216 if (collectDiagnostics)
218 pictureReplayTicks = pictureEnd - pictureStart;
219 backbufferFlushTicks = flushEnd - pictureEnd;
220 replayEnd = flushEnd;
223 long snapshotStartTick = !collectDiagnostics ? 0 : HighResTimer.GetCurrentTick();
226 if (collectDiagnostics)
227 snapshotTicks = HighResTimer.GetCurrentTick() - snapshotStartTick;
239 FrameMailbox.Release(slot);
242 if (collectDiagnostics)
244 double replayMs = HighResTimer.GetDuration(acquired, replayEnd) * 1000d;
245 double pictureMs = HighResTimer.GetDuration(0, pictureReplayTicks) * 1000d;
246 double flushMs = HighResTimer.GetDuration(0, backbufferFlushTicks) * 1000d;
247 double snapshotMs = HighResTimer.GetDuration(0, snapshotTicks) * 1000d;
249 if (telemetryGeneration != 0)
253 Telemetry!.Record(telemetryGeneration,
"replay.cpu.ms", replayMs);
254 Telemetry.Record(telemetryGeneration,
"picture.cpu.ms", pictureMs);
255 Telemetry.Record(telemetryGeneration,
"backbuffer.flush.cpu.ms", flushMs);
258 Telemetry!.Record(telemetryGeneration,
"snapshot.cpu.ms", snapshotMs);
259 Telemetry.Record(telemetryGeneration,
"snapshot.age.ms", age);
260 Telemetry.Record(telemetryGeneration,
"mailbox.published.lifetime", counters.Published);
261 Telemetry.Record(telemetryGeneration,
"mailbox.dropped.lifetime", counters.Dropped);
262 Telemetry.Record(telemetryGeneration,
"mailbox.slots", counters.InUse);
263 Telemetry.Record(telemetryGeneration,
"snapshot.commands.approximate", commands);
269 ReplayMilliseconds = replayMs,
270 PictureReplayMilliseconds = pictureMs,
271 BackbufferFlushMilliseconds = flushMs,
272 SnapshotMilliseconds = snapshotMs,
273 SnapshotAgeMilliseconds = age,
274 PublishedSnapshots = counters.Published,
275 DroppedSnapshots = counters.Dropped,
276 SnapshotSlotsInUse = counters.InUse,
277 SnapshotCommandCount = commands
297 ArgumentNullException.ThrowIfNull(destinationCanvas);
302 if (UsesRenderFrameSnapshots)
304 using var image = ReplayRenderFrameSnapshot();
305 DrawCurrentSurface(destinationCanvas);
309 RenderStateSynchronization.EnterGpuRender();
313 var tick = HighResTimer.GetCurrentTick();
315 RenderToBackbuffer(tick);
320 DrawCurrentSurface(destinationCanvas);
330 RenderStateSynchronization.ExitGpuRender();
347 ArgumentNullException.ThrowIfNull(destinationCanvas);
352 DrawCurrentSurface(destinationCanvas);
356 private void DrawCurrentSurface(SKCanvas canvas)
361 if (presentation.Scale <= 0)
368 canvas.Translate(presentation.DestinationRect.Left, presentation.DestinationRect.Top);
369 canvas.Scale(presentation.Scale);
371 bool needsLinearSampling =
373 (presentation.Scale != 1f ||
374 presentation.DestinationRect.Left != MathF.Floor(presentation.DestinationRect.Left) ||
375 presentation.DestinationRect.Top != MathF.Floor(presentation.DestinationRect.Top));
377 if (needsLinearSampling)
431 internal abstract void RenderToBackbuffer(
long tick);
437 internal abstract void PresentBackbufferToAdapter();
455 lock (RenderStateSynchronization.SyncRoot)
458 GC.SuppressFinalize(
this);
474 protected virtual void Dispose(
bool disposing)
480 FrameMailbox.Dispose();
484 RenderSurfaceHostRegistry.Unregister(
this);