Skip to content
Draft
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
18 changes: 15 additions & 3 deletions src/core/lib/validateImageBitmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,21 @@ export async function validateCreateImageBitmap(
// Test basic createImageBitmap support
const blob = new Blob([pngBinaryData], { type: 'image/png' });
const bitmap = await platform.createImageBitmap(blob);
bitmap.close?.();
if ('close' in bitmap && typeof bitmap.close === 'function') {
bitmap.close();
}
support.basic = true;

// Test createImageBitmap with options support
try {
const options = { premultiplyAlpha: 'none' as const };
const bitmapWithOptions = await platform.createImageBitmap(blob, options);
bitmapWithOptions.close?.();
if (
'close' in bitmapWithOptions &&
typeof bitmapWithOptions.close === 'function'
) {
bitmapWithOptions.close();
}
support.options = true;
} catch (e) {
/* ignore */
Expand All @@ -77,7 +84,12 @@ export async function validateCreateImageBitmap(
premultiplyAlpha: 'none',
},
);
bitmapWithFullOptions.close?.();
if (
'close' in bitmapWithFullOptions &&
typeof bitmapWithFullOptions.close === 'function'
) {
bitmapWithFullOptions.close();
}
support.full = true;
} catch (e) {
/* ignore */
Expand Down
10 changes: 6 additions & 4 deletions src/core/platforms/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,28 @@ export abstract class Platform {
* @param options - The ImageBitmapOptions.
* @returns A promise that resolves with the created ImageBitmap.
*/
abstract createImageBitmap(blob: ImageBitmapSource): Promise<ImageBitmap>;
abstract createImageBitmap(
blob: ImageBitmapSource,
): Promise<ImageBitmap | WebGLTexture>;
abstract createImageBitmap(
blob: ImageBitmapSource,
options: ImageBitmapOptions,
): Promise<ImageBitmap>;
): Promise<ImageBitmap | WebGLTexture>;
abstract createImageBitmap(
blob: ImageBitmapSource,
sx: number,
sy: number,
sw: number,
sh: number,
): Promise<ImageBitmap>;
): Promise<ImageBitmap | WebGLTexture>;
abstract createImageBitmap(
blob: ImageBitmapSource,
sx: number,
sy: number,
sw: number,
sh: number,
options: ImageBitmapOptions,
): Promise<ImageBitmap>;
): Promise<ImageBitmap | WebGLTexture>;

/**
* Retrieves the current timestamp.
Expand Down
1 change: 1 addition & 0 deletions src/core/renderers/webgl/WebGlCtxSubTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class WebGlCtxSubTexture extends WebGlCtxTexture {
| SubTextureProps
| CompressedData
| HTMLImageElement
| WebGLTexture
| null,
): Dimensions {
if (data === null) {
Expand Down
11 changes: 11 additions & 0 deletions src/core/renderers/webgl/WebGlCtxTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ export class WebGlCtxTexture extends CoreContextTexture {
this.state = 'loading';
this.textureSource.setState('loading');

// Special case for the ION runtime which creates WebGLTextures directly
// as a result of the createImageBitmap platform method.
if (this.textureSource.textureData?.data instanceof WebGLTexture) {
this._nativeCtxTexture = this.textureSource.textureData.data;
this.state = 'loaded';
this.textureSource.setState('loaded');

//FIXME get width and height from texture somehow
return;
}

// Await the native texture creation to ensure GPU buffer is fully allocated
this._nativeCtxTexture = this.createNativeCtxTexture();

Expand Down
2 changes: 1 addition & 1 deletion src/core/textures/ImageTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class ImageTexture extends Texture {
sw: number | null,
sh: number | null,
): Promise<{
data: ImageBitmap | HTMLImageElement;
data: ImageBitmap | HTMLImageElement | WebGLTexture;
premultiplyAlpha: boolean;
}> {
const hasAlphaChannel = premultiplyAlpha ?? blob.type.includes('image/png');
Expand Down
1 change: 1 addition & 0 deletions src/core/textures/Texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export interface TextureData {
| CompressedData
| HTMLImageElement
| Uint8Array
| WebGLTexture
| null;
/**
* Premultiply alpha when uploading texture data to the GPU
Expand Down