-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
ImageBitmapResource.ts
208 lines (170 loc) · 5.54 KB
/
ImageBitmapResource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { ALPHA_MODES } from '@pixi/constants';
import { settings } from '@pixi/settings';
import { BaseImageResource } from './BaseImageResource';
import type { ICanvas } from '@pixi/settings';
import type { Renderer } from '../../Renderer';
import type { BaseTexture } from '../BaseTexture';
import type { GLTexture } from '../GLTexture';
export interface IImageBitmapResourceOptions
{
/** Start loading process automatically when constructed. */
autoLoad?: boolean;
/** Load image using cross origin. */
crossOrigin?: boolean;
/** Alpha mode used when creating the ImageBitmap. */
alphaMode?: ALPHA_MODES;
}
/**
* Resource type for ImageBitmap.
* @memberof PIXI
*/
export class ImageBitmapResource extends BaseImageResource
{
/** URL of the image source. */
url: string | null;
/**
* Load image using cross origin.
* @default false
*/
crossOrigin: boolean;
/**
* Controls texture alphaMode field
* Copies from options
* Default is `null`, copies option from baseTexture
* @readonly
*/
alphaMode: ALPHA_MODES | null;
/**
* Promise when loading.
* @default null
*/
private _load: Promise<ImageBitmapResource>;
/**
* @param source - ImageBitmap or URL to use
* @param options
* @param {boolean} [options.autoLoad=true] - Start loading process automatically when constructed.
* @param {boolean} [options.crossOrigin=true] - Load image using cross origin.
* @param {PIXI.ALPHA_MODES} [options.alphaMode=null] - Alpha mode used when creating the ImageBitmap.
*/
constructor(source: ImageBitmap | string, options?: IImageBitmapResourceOptions)
{
options = options || {};
let baseSource;
let url;
if (typeof source === 'string')
{
baseSource = ImageBitmapResource.EMPTY;
url = source;
}
else
{
baseSource = source;
url = null;
}
// Using super() in if() can cause transpilation problems in some cases, so take it out of if().
// See https://github.com/pixijs/pixijs/pull/9093 for details.
super(baseSource);
this.url = url;
this.crossOrigin = options.crossOrigin ?? true;
this.alphaMode = typeof options.alphaMode === 'number' ? options.alphaMode : null;
this._load = null;
if (options.autoLoad !== false)
{
this.load();
}
}
load(): Promise<ImageBitmapResource>
{
if (this._load)
{
return this._load;
}
this._load = new Promise(async (resolve, reject) =>
{
if (this.url === null)
{
resolve(this);
return;
}
try
{
const response = await settings.ADAPTER.fetch(this.url, {
mode: this.crossOrigin ? 'cors' : 'no-cors'
});
if (this.destroyed) return;
const imageBlob = await response.blob();
if (this.destroyed) return;
const imageBitmap = await createImageBitmap(imageBlob, {
premultiplyAlpha: this.alphaMode === null || this.alphaMode === ALPHA_MODES.UNPACK
? 'premultiply' : 'none',
});
if (this.destroyed) return;
this.source = imageBitmap;
this.update();
resolve(this);
}
catch (e)
{
if (this.destroyed) return;
reject(e);
this.onError.emit(e);
}
});
return this._load;
}
/**
* Upload the image bitmap resource to GPU.
* @param renderer - Renderer to upload to
* @param baseTexture - BaseTexture for this resource
* @param glTexture - GLTexture to use
* @returns {boolean} true is success
*/
override upload(renderer: Renderer, baseTexture: BaseTexture, glTexture: GLTexture): boolean
{
if (!(this.source instanceof ImageBitmap))
{
this.load();
return false;
}
if (typeof this.alphaMode === 'number')
{
baseTexture.alphaMode = this.alphaMode;
}
return super.upload(renderer, baseTexture, glTexture);
}
/** Destroys this resource. */
override dispose(): void
{
if (this.source instanceof ImageBitmap)
{
this.source.close();
}
super.dispose();
this._load = null;
}
/**
* Used to auto-detect the type of resource.
* @param {*} source - The source object
* @returns {boolean} `true` if current environment support ImageBitmap, and source is string or ImageBitmap
*/
static override test(source: unknown): source is string | ImageBitmap
{
return !!globalThis.createImageBitmap && typeof ImageBitmap !== 'undefined'
&& (typeof source === 'string' || source instanceof ImageBitmap);
}
/**
* Cached empty placeholder canvas.
* @see EMPTY
*/
private static _EMPTY: ICanvas;
/**
* ImageBitmap cannot be created synchronously, so a empty placeholder canvas is needed when loading from URLs.
* Only for internal usage.
* @returns The cached placeholder canvas.
*/
private static get EMPTY(): ICanvas
{
ImageBitmapResource._EMPTY = ImageBitmapResource._EMPTY ?? settings.ADAPTER.createCanvas(0, 0);
return ImageBitmapResource._EMPTY;
}
}