42 private SKBitmap? _cpuBitmap;
43 private SKSurface? _surface;
44 private bool _disposed;
45 private GRContext? _context;
47 private sealed record Resolution(
int Width,
int Height);
49 private Resolution? _requestedResolution;
51 private int _targetFps = 60;
52 private readonly
object _msaaSyncRoot =
new();
53 private int _msaaSampleCount = 1;
54 private long _msaaConfigurationRevision;
55 private long _appliedMsaaConfigurationRevision = -1;
56 private int _actualMsaaSampleCount = 1;
57 private int _maxSupportedMsaaSampleCount = 1;
61 private long _frameCount;
78 set => _targetFps = value < 0 ? 0 : value;
101 public bool VSync {
get;
set; } =
true;
136 return _msaaSampleCount;
140 int normalized = value < 1 ? 1 : value;
144 if (_msaaSampleCount == normalized)
147 _msaaSampleCount = normalized;
148 _msaaConfigurationRevision++;
177 internal bool IsMsaaSurfaceRecreationPending =>
178 Volatile.Read(ref _appliedMsaaConfigurationRevision) !=
179 Volatile.Read(ref _msaaConfigurationRevision);
187 : base(width, height)
193 VSync = config.VSync;
196 CreateCpuSurface(width, height);
217 public void Initialize(GRContext grContext,
int width,
int height)
219 if (width <= 0 || height <= 0)
225 (
int requestedSampleCount,
long configurationRevision) = GetMsaaConfigurationSnapshot();
226 int maxSupportedSampleCount = grContext.GetMaxSurfaceSampleCount(SKColorType.Rgba8888);
227 Volatile.Write(ref _maxSupportedMsaaSampleCount, maxSupportedSampleCount);
231 int actualSampleCount = CreateGpuSurface(
235 requestedSampleCount);
237 _context = grContext;
241 lock (RenderStateSynchronization.SyncRoot)
244 MarkMsaaConfigurationApplied(configurationRevision, actualSampleCount);
256 protected internal override void RequestResize(
int width,
int height)
257 => Interlocked.Exchange(ref _requestedResolution,
new(width, height));
268 ArgumentNullException.ThrowIfNull(context);
273 var request = Interlocked.Exchange(ref _requestedResolution,
null);
274 int width = request?.Width ??
Width;
275 int height = request?.Height ??
Height;
277 if (ReferenceEquals(_context, context) &&
280 !IsMsaaSurfaceRecreationPending)
292 public override SKCanvas
Canvas => _surface?.Canvas
293 ??
throw new InvalidOperationException(
294 $
"{nameof(GpuBackbuffer)} surface is not initialized.");
299 protected internal override void BeginFrame()
301 if (_disposed || _surface is
null)
304 var c = _surface.Canvas;
320 protected internal override void EndFrame()
322 if (_disposed || _surface is
null)
333 protected internal override void DrawTileFrame(
Tile tile, RectangleF destRectScreen)
337 if (image is
null || _surface is
null)
340 var canvas = _surface.Canvas;
342 canvas.DrawImage(image, destRectScreen.ToSKRect());
355 protected internal override SKImage Snapshot()
357 if (_disposed || _surface is
null)
360 return _surface.Snapshot();
369 public void RecordFrame() => Interlocked.Increment(ref _frameCount);
376 internal long ConsumeFrameCount() => Interlocked.Exchange(ref _frameCount, 0);
380 internal (
int SampleCount,
long Revision) GetMsaaConfigurationSnapshot()
383 return (_msaaSampleCount, _msaaConfigurationRevision);
386 internal void MarkMsaaConfigurationApplied(
long revision,
int actualSampleCount)
388 Volatile.Write(ref _actualMsaaSampleCount, actualSampleCount);
389 Volatile.Write(ref _appliedMsaaConfigurationRevision, revision);
392 private int CreateGpuSurface(
396 int requestedSampleCount)
399 var info =
new SKImageInfo(
402 SKColorType.Rgba8888,
405 _surface = SKSurface.Create(
409 requestedSampleCount);
411 if (_surface is not
null)
412 return requestedSampleCount;
417 if (requestedSampleCount > 1)
418 _surface = SKSurface.Create(
424 if (_surface is
null)
425 throw new InvalidOperationException(
426 $
"Failed to create a {nameof(GpuBackbuffer)} GPU surface ({width}x{height}). " +
427 "The GRContext may be invalid or the pixel format is not renderable.");
432 private void CreateCpuSurface(
int width,
int height)
436 var info =
new SKImageInfo(
439 SKColorType.Bgra8888,
442 _cpuBitmap =
new SKBitmap(info);
443 _surface = SKSurface.Create(info, _cpuBitmap.GetPixels(), _cpuBitmap.Info.RowBytes);
446 private void DisposeSurface()
450 _cpuBitmap?.Dispose();
457 internal void ReleaseContext()
474 GC.SuppressFinalize(
this);