20 private readonly SKGLControl _glControl;
21 private readonly Control _presentationControl;
22 private readonly EventHandler _resizeHandler;
27 private Action? _afterFrameRenderHandler;
31 private bool? _appliedVSync;
35 private bool _grContextReady;
38 private int _pendingResize;
43 private int _pendingInvalidate;
48 public SKColor
ClearColor {
get;
set; } = SKColors.Black;
81 if (_presentationControl.IsDisposed || !_presentationControl.IsHandleCreated)
84 var sz = _presentationControl.ClientSize;
87 if (sz.Width > 0 && sz.Height > 0)
88 Interlocked.Exchange(ref _pendingResize, 1);
90 Interlocked.Exchange(ref _pendingResize, 0);
110 Math.Max(1, presentationControl.ClientSize.
Width),
111 Math.Max(1, presentationControl.ClientSize.
Height),
112 presentationControl.ClientSize.
Width > 0 && presentationControl.ClientSize.
Height > 0)
114 _glControl = gl ??
throw new ArgumentNullException(nameof(gl));
115 _presentationControl = presentationControl ??
throw new ArgumentNullException(nameof(presentationControl));
123 _glControl.PaintSurface += OnPaintSurface;
124 _presentationControl.Resize += _resizeHandler;
144 _host = host ??
throw new ArgumentNullException(nameof(host));
145 _host.Telemetry?.Define(
"presentation.cpu.ms");
146 _host.Telemetry?.Define(
"render.snapshot.cpu.ms");
147 _host.Telemetry?.Define(
"blit.cpu.ms");
148 _host.Telemetry?.Define(
"flush.cpu.ms");
157 _afterFrameRenderHandler = QueueInvalidate;
158 Engine.Instance.AfterFrameRender += _afterFrameRenderHandler;
168 public override void Present(SKImage bufferImage, SKRectI bufferRect, SKRect destRect)
170 bufferImage.Dispose();
173 private void QueueInvalidate()
175 if (_glControl.IsDisposed || _presentationControl.IsDisposed)
178 if (Interlocked.CompareExchange(ref _pendingInvalidate, 1, 0) != 0)
185 if (!_glControl.IsDisposed
186 && !_presentationControl.IsDisposed
187 && _presentationControl.ClientSize.Width > 0
188 && _presentationControl.ClientSize.Height > 0)
190 _glControl.Invalidate();
197 Interlocked.Exchange(ref _pendingInvalidate, 0);
202 private void OnPaintSurface(
object? sender, SKPaintGLSurfaceEventArgs e)
204 long telemetryGeneration = _host?.Telemetry?.BeginSample() ?? 0;
206 long callbackStartTick = collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
207 long renderAndSnapshotTicks = 0;
212 if (_gpuBackbuffer !=
null)
214 bool desiredVSync = _gpuBackbuffer.VSync;
216 if (_appliedVSync != desiredVSync)
218 _glControl.VSync = desiredVSync;
219 _appliedVSync = desiredVSync;
228 if (!_grContextReady)
232 _grContextReady =
true;
233 Interlocked.Exchange(ref _pendingResize, 0);
236 else if (Interlocked.Exchange(ref _pendingResize, 0) == 1)
245 if (width > 0 && height > 0)
251 _gpuBackbuffer?.EnsureInitialized(
GrContext);
253 var canvas = e.Surface.Canvas;
269 long renderStartTick = collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
270 using var img = _host.GlRenderAndSnapshot();
271 long renderEndTick = collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
273 if (collectDiagnostics)
274 renderAndSnapshotTicks = renderEndTick - renderStartTick;
278 long blitStartTick = collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
280 long blitEndTick = collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
282 if (collectDiagnostics)
283 blitTicks = blitEndTick - blitStartTick;
296 long flushStartTick = collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
297 _glControl.GRContext?.Flush();
298 long flushEndTick = collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
300 if (collectDiagnostics)
301 flushTicks = flushEndTick - flushStartTick;
304 _gpuBackbuffer?.RecordFrame();
306 if (collectDiagnostics)
308 long callbackEndTick = HighResTimer.GetCurrentTick();
310 if (telemetryGeneration != 0)
312 var source = _host!.Telemetry!;
313 source.Record(telemetryGeneration,
"presentation.count", 1);
317 "presentation.cpu.ms",
318 HighResTimer.GetDuration(callbackStartTick, callbackEndTick) * 1000d);
322 "render.snapshot.cpu.ms",
323 HighResTimer.GetDuration(0, renderAndSnapshotTicks) * 1000d);
325 source.Record(telemetryGeneration,
"blit.cpu.ms", HighResTimer.GetDuration(0, blitTicks) * 1000d);
326 source.Record(telemetryGeneration,
"flush.cpu.ms", HighResTimer.GetDuration(0, flushTicks) * 1000d);
331 HighResTimer.GetDuration(callbackStartTick, callbackEndTick) * 1000d,
332 HighResTimer.GetDuration(0, renderAndSnapshotTicks) * 1000d,
333 HighResTimer.GetDuration(0, blitTicks) * 1000d,
334 HighResTimer.GetDuration(0, flushTicks) * 1000d));
343 if (_afterFrameRenderHandler !=
null)
345 Engine.Instance.AfterFrameRender -= _afterFrameRenderHandler;
346 _afterFrameRenderHandler =
null;
349 if (!_glControl.IsDisposed)
350 _glControl.PaintSurface -= OnPaintSurface;
352 if (!_presentationControl.IsDisposed)
353 _presentationControl.Resize -= _resizeHandler;