1using System.Runtime.Versioning;
4using Microsoft.AspNetCore.Components;
5using Microsoft.Extensions.Logging;
6using Microsoft.JSInterop;
8using SkiaSharp.Views.Blazor;
35[SupportedOSPlatform(
"browser")]
38 [Inject]
private ILogger<BlazorGpuRenderSurfaceComponent> Logger {
get;
set; } =
null!;
40 private readonly
string _generatedCanvasId = $
"gondwana-webgl-{Guid.NewGuid():N}";
42 private IJSObjectReference? _module;
43 private GRContext? _grContext;
44 private IReadOnlyDictionary<string, object>? _gpuCanvasAttributes;
45 private string _canvasId =
string.Empty;
46 private bool _backbufferNeedsRender =
true;
47 private bool _disposed;
61 public RenderSurfaceHost<GpuBackbuffer>
Host {
get;
private set; } =
null!;
64 private IReadOnlyDictionary<string, object>? GpuCanvasAttributes => _gpuCanvasAttributes;
66 internal override ValueTask<CanvasOffset> GetCanvasOffsetAsync()
68 ? ValueTask.FromResult(
default(CanvasOffset))
69 : _module.InvokeAsync<CanvasOffset>(
"getCanvasOffset", _canvasId);
79 Host =
new RenderSurfaceHost<GpuBackbuffer>(
Adapter);
82 catch (Exception ex) when (SkiaNativeInitializationDiagnostic.IsMissingSkiaNativeLibrary(ex))
84 Logger.LogError(ex,
"{Guidance}", SkiaNativeInitializationDiagnostic.Guidance);
92 base.OnParametersSet();
94 var attributes =
new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
99 attributes[attribute.Key] = attribute.Value;
102 if (attributes.TryGetValue(
"id", out var
id) &&
id is not
null)
103 _canvasId =
id.ToString() ?? _generatedCanvasId;
105 attributes[
"id"] = _canvasId = _generatedCanvasId;
107 attributes[nameof(SKGLView.OnPaintSurface)] =
108 (Action<SKPaintGLSurfaceEventArgs>)HandlePaintSurface;
110 _gpuCanvasAttributes = attributes;
119 _module = await JS.InvokeAsync<IJSObjectReference>(
121 "./_content/Gondwana.Blazor/gondwana-blazor.js");
125 await _module.InvokeVoidAsync(
"focusElementById", _canvasId);
126 await _module.InvokeVoidAsync(
"suppressBrowserInputDefaultsById", _canvasId);
134 private void HandlePaintSurface(SKPaintGLSurfaceEventArgs e)
136 long telemetryGeneration =
Host?.
Telemetry?.BeginSample() ?? 0;
141 var width = e.Info.Width;
142 var height = e.Info.Height;
143 Adapter.BeginPaint(width, height);
145 if (width <= 0 || height <= 0 || e.Surface.Context is not GRContext grContext)
147 e.Surface.Canvas.Clear(SKColors.Black);
152 _grContext = grContext;
154 if (backbuffer.EnsureInitialized(grContext))
155 _backbufferNeedsRender =
true;
159 if (!engine.IsRunning)
161 e.Surface.Canvas.Clear(backbuffer.ClearColor);
169 bool engineFrameRequested =
Adapter.ConsumeFrameRequest();
170 bool renderScene = _backbufferNeedsRender || engineFrameRequested;
172 bool presented = renderScene
178 e.Surface.Canvas.Clear(backbuffer.ClearColor);
183 _backbufferNeedsRender =
false;
187 backbuffer.RecordFrame();
189 if (telemetryGeneration != 0)
190 Host!.
Telemetry!.Record(telemetryGeneration,
"presentation.count", 1);
209 if (_module is not
null)
211 _ = _module.DisposeAsync();
Coordinates Gondwana foreground-frame requests with a Blazor WebGL render surface.
A Blazor WebGL render surface that draws Gondwana scenes through an GpuBackbuffer without transferrin...
override void OnInitialized()
override async Task OnAfterRenderAsync(bool firstRender)
Action< bool >? FramePresented
Raised after a WebGL frame has been successfully presented on the browser canvas. The boolean indicat...
RenderSurfaceHost< GpuBackbuffer > Host
Gets the GPU render-surface host bound to this component.
BlazorGpuRenderSurfaceAdapter Adapter
Gets the adapter that coordinates engine frame requests with this component.
override void OnParametersSet()
Provides the shared DOM input surface used by Gondwana's Blazor bitmap and GPU render components.
IReadOnlyDictionary< string, object >? CanvasAttributes
Gets the canvas attributes with the component-owned style attribute removed.
Gives Razor a local component name for SkiaSharp's WebGL view. This avoids Razor resolving SkiaSharp ...
Represents the core game engine singleton responsible for managing the main update loop,...
void Tick()
Advances the timer-driven engine by one externally presented frame.
static Engine Instance
Gets the singleton instance of the Engine.
Provides a fully GPU-rendered backbuffer implementation using SkiaSharp and OpenGL.
Represents the abstract base class for render surface adapters that present backbuffer output to a de...
override BackbufferBase Backbuffer
Gets the backbuffer used for off-screen rendering before presentation to the adapter.
Diagnostics.? TelemetrySource Telemetry
Neutral session-local measurements for this host; null when source capacity is exhausted or the share...
bool GlDrawCurrentFrameToCanvas(SKCanvas destinationCanvas)
Draws the existing GPU backbuffer surface directly to another GPU canvas without rendering a new scen...
bool GlRenderToCanvas(SKCanvas destinationCanvas)
Renders the current scene frame and draws the GPU backbuffer surface directly to another GPU canvas....