Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/core/lib/ImageWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class ImageWorkerManager {
);
}

if (createImageBitmapSupport.premultiplyHonored === false) {
if (createImageBitmapSupport.premultiplyHonored !== true) {
workerCode = workerCode.replace(
'var premultiplyAlphaHonored = true;',
'var premultiplyAlphaHonored = false;',
Expand Down
38 changes: 11 additions & 27 deletions src/core/textures/ImageTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,9 @@ export class ImageTexture extends Texture {
}> {
const hasAlphaChannel = premultiplyAlpha ?? blob.type.includes('image/png');
const imageBitmapSupported = this.txManager.imageBitmapSupported;

// When the device does NOT honor the createImageBitmap premultiply option
// (e.g. older Safari), request a straight ('none') bitmap and let WebGL
// premultiply on upload instead. `premultiplyAlpha` in the returned
// TextureData means "WebGL should premultiply this source on upload".
const useGlPremultiply =
hasAlphaChannel === true &&
imageBitmapSupported.premultiplyHonored === false;
const bitmapMode: 'premultiply' | 'none' =
hasAlphaChannel === true && useGlPremultiply === false
? 'premultiply'
: 'none';
const bitmapPremultiply: 'premultiply' | 'none' = hasAlphaChannel
? 'premultiply'
: 'none';

if (imageBitmapSupported.full === true && sw !== null && sh !== null) {
// createImageBitmap with crop
Expand All @@ -213,29 +204,27 @@ export class ImageTexture extends Texture {
sw,
sh,
{
premultiplyAlpha: bitmapMode,
premultiplyAlpha: bitmapPremultiply,
colorSpaceConversion: 'none',
imageOrientation: 'none',
},
);
return { data: bitmap, premultiplyAlpha: useGlPremultiply };
return { data: bitmap, premultiplyAlpha: hasAlphaChannel };
} else if (imageBitmapSupported.basic === true) {
// basic createImageBitmap without options or crop
// this is supported for Chrome v50 to v52/54 that doesn't support options.
// The browser default premultiplies, so WebGL must not premultiply again.
// basic createImageBitmap without options or crop (Chrome v50–54)
return {
data: await this.platform.createImageBitmap(blob),
premultiplyAlpha: false,
premultiplyAlpha: hasAlphaChannel,
};
}

// default createImageBitmap without crop but with options
// default: createImageBitmap without crop but with options
const bitmap = await this.platform.createImageBitmap(blob, {
premultiplyAlpha: bitmapMode,
premultiplyAlpha: bitmapPremultiply,
colorSpaceConversion: 'none',
imageOrientation: 'none',
});
return { data: bitmap, premultiplyAlpha: useGlPremultiply };
return { data: bitmap, premultiplyAlpha: hasAlphaChannel };
}

async loadImage(src: string) {
Expand Down Expand Up @@ -305,14 +294,9 @@ export class ImageTexture extends Texture {
};
}

// The loader computes whether WebGL should premultiply this source on
// upload (it depends on the source type and on whether createImageBitmap
// honored the premultiply option). Preserve it; fall back to the prop only
// when the loader didn't decide.
return {
data: resp.data,
premultiplyAlpha:
resp.premultiplyAlpha ?? this.props.premultiplyAlpha ?? true,
premultiplyAlpha: this.props.premultiplyAlpha ?? true,
};
}

Expand Down
Loading