@solidtv/renderer 1.2.9 → 1.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -174,6 +174,8 @@ export class WebGlRenderer extends CoreRenderer {
174
174
  const glw = (this.glw = new WebGlContextWrapper(gl));
175
175
  glw.viewport(0, 0, options.canvas.width, options.canvas.height);
176
176
 
177
+ this.attachContextLossListeners(options.canvas);
178
+
177
179
  this.updateClearColor(this.stage.clearColor);
178
180
 
179
181
  glw.setBlend(true);
@@ -300,6 +302,32 @@ export class WebGlRenderer extends CoreRenderer {
300
302
  ]);
301
303
  }
302
304
 
305
+ /**
306
+ * Listen for WebGL context loss on the canvas.
307
+ *
308
+ * @remarks
309
+ * On low-RAM devices (e.g. Chromium 123+ after backgrounding) the GPU
310
+ * context is dropped, after which `gl.createTexture()` and friends return
311
+ * null and the engine would crash. We pause the render loop via the Stage
312
+ * flag and surface a `contextLost` event so consumers can react.
313
+ *
314
+ * We intentionally do NOT call `event.preventDefault()` (which would ask the
315
+ * browser to restore the context) and do NOT listen for
316
+ * `webglcontextrestored`: the engine cannot rebuild its GPU resources
317
+ * in-place, so the supported recovery is to reload the app. See BROWSERS.md.
318
+ */
319
+ private attachContextLossListeners(
320
+ canvas: HTMLCanvasElement | OffscreenCanvas,
321
+ ): void {
322
+ if ('addEventListener' in canvas === false) {
323
+ return;
324
+ }
325
+ const target = canvas as HTMLCanvasElement;
326
+ target.addEventListener('webglcontextlost', () => {
327
+ this.stage.setContextLost();
328
+ });
329
+ }
330
+
303
331
  reset() {
304
332
  const { glw } = this;
305
333
  if (DIRTY_QUAD_BUFFER) {
@@ -145,6 +145,28 @@ export interface RendererMainCriticalCleanupFailedEvent {
145
145
  criticalThreshold: number;
146
146
  }
147
147
 
148
+ /**
149
+ * WebGL Context Lost Event Data
150
+ *
151
+ * @remarks
152
+ * Fired when the underlying WebGL context is lost (e.g. on low-RAM devices
153
+ * running Chromium 123+ after the app has been backgrounded). The render loop
154
+ * stops; in-engine GL resources are NOT rebuilt, so the supported handling is
155
+ * to reload the app.
156
+ *
157
+ * @category Events
158
+ * @example
159
+ * ```typescript
160
+ * renderer.on('contextLost', () => {
161
+ * window.location.reload();
162
+ * });
163
+ * ```
164
+ */
165
+ export interface RendererMainContextLostEvent {
166
+ /** This event has no payload - listen without parameters */
167
+ readonly __eventHasNoPayload?: never;
168
+ }
169
+
148
170
  /**
149
171
  * Settings for the Renderer that can be updated during runtime.
150
172
  */
@@ -506,6 +528,7 @@ export type RendererMainSettings = RendererRuntimeSettings & {
506
528
  * @see {@link RendererMainIdleEvent}
507
529
  * @see {@link RendererMainCriticalCleanupEvent}
508
530
  * @see {@link RendererMainCriticalCleanupFailedEvent}
531
+ * @see {@link RendererMainContextLostEvent}
509
532
  *
510
533
  * @fires RendererMain#fpsUpdate
511
534
  * @fires RendererMain#frameTick
@@ -513,6 +536,7 @@ export type RendererMainSettings = RendererRuntimeSettings & {
513
536
  * @fires RendererMain#idle
514
537
  * @fires RendererMain#criticalCleanup
515
538
  * @fires RendererMain#criticalCleanupFailed
539
+ * @fires RendererMain#contextLost
516
540
  */
517
541
  export class RendererMain extends EventEmitter {
518
542
  readonly root: INode;