@@ -379,7 +379,17 @@ export abstract class BaseRenderLayer extends Disposable implements IRenderLayer
379379 }
380380 this . _ctx . save ( ) ;
381381 this . _clipRow ( y ) ;
382+
382383 // Draw the image, use the bitmap if it's available
384+
385+ // HACK: If the canvas doesn't match, delete the generator. It's not clear how this happens but
386+ // something is wrong with either the lifecycle of _bitmapGenerator or the page canvases are
387+ // swapped out unexpectedly
388+ if ( this . _bitmapGenerator [ glyph . texturePage ] && this . _charAtlas . pages [ glyph . texturePage ] . canvas !== this . _bitmapGenerator [ glyph . texturePage ] ! . canvas ) {
389+ this . _bitmapGenerator [ glyph . texturePage ] ?. bitmap ?. close ( ) ;
390+ delete this . _bitmapGenerator [ glyph . texturePage ] ;
391+ }
392+
383393 if ( this . _charAtlas . pages [ glyph . texturePage ] . version !== this . _bitmapGenerator [ glyph . texturePage ] ?. version ) {
384394 if ( ! this . _bitmapGenerator [ glyph . texturePage ] ) {
385395 this . _bitmapGenerator [ glyph . texturePage ] = new BitmapGenerator ( this . _charAtlas . pages [ glyph . texturePage ] . canvas ) ;
@@ -446,11 +456,12 @@ class BitmapGenerator {
446456 public get bitmap ( ) : ImageBitmap | undefined { return this . _bitmap ; }
447457 public version : number = - 1 ;
448458
449- constructor ( private readonly _canvas : HTMLCanvasElement ) {
459+ constructor ( public readonly canvas : HTMLCanvasElement ) {
450460 }
451461
452462 public refresh ( ) : void {
453463 // Clear the bitmap immediately as it's stale
464+ this . _bitmap ?. close ( ) ;
454465 this . _bitmap = undefined ;
455466 // Disable ImageBitmaps on Safari because of https://bugs.webkit.org/show_bug.cgi?id=149990
456467 if ( isSafari ) {
@@ -466,9 +477,10 @@ class BitmapGenerator {
466477
467478 private _generate ( ) : void {
468479 if ( this . _state === BitmapGeneratorState . IDLE ) {
480+ this . _bitmap ?. close ( ) ;
469481 this . _bitmap = undefined ;
470482 this . _state = BitmapGeneratorState . GENERATING ;
471- window . createImageBitmap ( this . _canvas ) . then ( bitmap => {
483+ window . createImageBitmap ( this . canvas ) . then ( bitmap => {
472484 if ( this . _state === BitmapGeneratorState . GENERATING_INVALID ) {
473485 this . refresh ( ) ;
474486 } else {
0 commit comments