Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: safari 9 HTMLImageElement check #473

Merged
merged 6 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/core/renderers/canvas/CanvasCoreRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export class CanvasCoreRenderer extends CoreRenderer {
}

reset(): void {
// eslint-disable-next-line no-self-assign
this.canvas.width = this.canvas.width; // quick reset canvas

const ctx = this.context;
Expand Down Expand Up @@ -191,7 +190,12 @@ export class CanvasCoreRenderer extends CoreRenderer {
height,
);
} else {
ctx.drawImage(image, tx, ty, width, height);
try {
ctx.drawImage(image, tx, ty, width, height);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (error) {
// noop
}
}
ctx.globalAlpha = 1;
} else if (hasGradient) {
Expand Down
31 changes: 12 additions & 19 deletions src/core/renderers/webgl/WebGlCoreCtxTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,32 +140,31 @@ export class WebGlCoreCtxTexture extends CoreContextTexture {

assertTruthy(this._nativeCtxTexture);
glw.activeTexture(0);
const tdata = textureData.data;
// If textureData is null, the texture is empty (0, 0) and we don't need to
// upload any data to the GPU.
if (
(typeof ImageBitmap !== 'undefined' &&
textureData.data instanceof ImageBitmap) ||
textureData.data instanceof ImageData ||
(typeof ImageBitmap !== 'undefined' && tdata instanceof ImageBitmap) ||
tdata instanceof ImageData ||
// not using typeof HTMLImageElement due to web worker
isHTMLImageElement(textureData.data)
isHTMLImageElement(tdata)
) {
const data = textureData.data;
width = data.width;
height = data.height;
width = tdata.width;
height = tdata.height;
glw.bindTexture(this._nativeCtxTexture);
glw.pixelStorei(
glw.UNPACK_PREMULTIPLY_ALPHA_WEBGL,
!!textureData.premultiplyAlpha,
);

glw.texImage2D(0, glw.RGBA, glw.RGBA, glw.UNSIGNED_BYTE, data);
glw.texImage2D(0, glw.RGBA, glw.RGBA, glw.UNSIGNED_BYTE, tdata);
this.setTextureMemUse(width * height * 4);

// generate mipmaps for power-of-2 textures or in WebGL2RenderingContext
if (glw.isWebGl2() || (isPowerOfTwo(width) && isPowerOfTwo(height))) {
glw.generateMipmap();
}
} else if (textureData.data === null) {
} else if (tdata === null) {
width = 0;
height = 0;
// Reset to a 1x1 transparent texture
Expand All @@ -182,14 +181,8 @@ export class WebGlCoreCtxTexture extends CoreContextTexture {
TRANSPARENT_TEXTURE_DATA,
);
this.setTextureMemUse(TRANSPARENT_TEXTURE_DATA.byteLength);
} else if ('mipmaps' in textureData.data && textureData.data.mipmaps) {
const {
mipmaps,
width = 0,
height = 0,
type,
glInternalFormat,
} = textureData.data;
} else if ('mipmaps' in tdata && tdata.mipmaps) {
const { mipmaps, width = 0, height = 0, type, glInternalFormat } = tdata;
const view =
type === 'ktx'
? new DataView(mipmaps[0] ?? new ArrayBuffer(0))
Expand All @@ -204,7 +197,7 @@ export class WebGlCoreCtxTexture extends CoreContextTexture {
glw.texParameteri(glw.TEXTURE_MIN_FILTER, glw.LINEAR);

this.setTextureMemUse(view.byteLength);
} else if (textureData.data && textureData.data instanceof Uint8Array) {
} else if (tdata && tdata instanceof Uint8Array) {
// Color Texture
width = 1;
height = 1;
Expand All @@ -223,7 +216,7 @@ export class WebGlCoreCtxTexture extends CoreContextTexture {
0,
glw.RGBA,
glw.UNSIGNED_BYTE,
textureData.data,
tdata,
);

this.setTextureMemUse(width * height * 4);
Expand Down
8 changes: 5 additions & 3 deletions src/core/renderers/webgl/internal/RendererUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ export function createIndexBuffer(glw: WebGlContextWrapper, size: number) {
export function isHTMLImageElement(obj: unknown): obj is HTMLImageElement {
return (
obj !== null &&
typeof obj === 'object' &&
obj.constructor &&
obj.constructor.name === 'HTMLImageElement'
((typeof obj === 'object' &&
obj.constructor &&
obj.constructor.name === 'HTMLImageElement') ||
(typeof HTMLImageElement !== 'undefined' &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully this works if we're rendering in a webworker again - though granted if we're doing offscreen rendering the chances are that we're using Canvas rendering are quite low.

That plus ThreadX being months out from a useable state again, its fine.

obj instanceof HTMLImageElement))
);
}

Expand Down
7 changes: 3 additions & 4 deletions src/core/text-rendering/renderers/CanvasTextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import {
getNormalizedRgbaComponents,
getNormalizedAlphaComponent,
} from '../../lib/utils.js';
import type { ImageTexture } from '../../textures/ImageTexture.js';
import { TrFontManager, type FontFamilyMap } from '../TrFontManager.js';
import { type FontFamilyMap } from '../TrFontManager.js';
import type { TrFontFace } from '../font-face-types/TrFontFace.js';
import { WebTrFontFace } from '../font-face-types/WebTrFontFace.js';
import {
Expand All @@ -47,7 +46,7 @@ const resolvedGlobal = typeof self === 'undefined' ? globalThis : self;
/**
* Global font set regardless of if run in the main thread or a web worker
*/
const globalFontSet = ((resolvedGlobal.document as any)?.fonts ||
const globalFontSet: FontFaceSet = (resolvedGlobal.document?.fonts ||
(resolvedGlobal as any).fonts) as FontFaceSet;

declare module './TextRenderer.js' {
Expand Down Expand Up @@ -99,7 +98,7 @@ export class CanvasTextRenderer extends TextRenderer<CanvasTextRendererState> {
} else {
this.canvas = document.createElement('canvas');
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion

let context = this.canvas.getContext('2d', {
willReadFrequently: true,
}) as OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D | null;
Expand Down
3 changes: 0 additions & 3 deletions src/core/textures/ImageTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ export class ImageTexture extends Texture {

async loadImageFallback(src: string, hasAlpha: boolean) {
const img = new Image();
if (!(src.startsWith('data:'))) {
img.crossOrigin = 'Anonymous';
pecoram marked this conversation as resolved.
Show resolved Hide resolved
}

if (!src.startsWith('data:')) {
img.crossOrigin = 'Anonymous';
Expand Down
Loading