- 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
import type { BASIS_FORMATS } from './Basis';
import type { ITranscodeResponse } from './TranscoderWorkerWrapper';
import { TranscoderWorkerWrapper } from './TranscoderWorkerWrapper';
/**
* Worker class for transcoding *.basis files in background threads.
*
* To enable asynchronous transcoding, you need to provide the URL to the basis_universal transcoding
* library.
* @memberof PIXI.BasisLoader
*/
export class TranscoderWorker
{
// IMPLEMENTATION NOTE: TranscoderWorker tracks transcoding requests with a requestID; the worker can be issued
// multiple requests (once it is initialized) and the response contains the requestID of the triggering request. Based on
// the response, the transcodeAsync promise is fulfilled or rejected.
// TODO: Publish our own @pixi/basis package & set default URL to jsdelivr/cdnjs
/** URL for the script containing the basis_universal library. */
static bindingURL: string;
static jsSource: string;
static wasmSource: ArrayBuffer;
private static _onTranscoderInitializedResolve: () => void;
/** a promise that when reslved means the transcoder is ready to be used */
public static onTranscoderInitialized = new Promise<void>((resolve) =>
{
TranscoderWorker._onTranscoderInitializedResolve = resolve;
});
isInit: boolean;
load: number;
requests: { [id: number]: {
resolve: (data: ITranscodeResponse) => void,
reject: () => void
} } = {};
private static _workerURL: string;
private static _tempID = 0;
/** Generated URL for the transcoder worker script. */
static get workerURL(): string
{
if (!TranscoderWorker._workerURL)
{
let workerSource = TranscoderWorkerWrapper.toString();
const beginIndex = workerSource.indexOf('{');
const endIndex = workerSource.lastIndexOf('}');
workerSource = workerSource.slice(beginIndex + 1, endIndex);
if (TranscoderWorker.jsSource)
{
workerSource = `${TranscoderWorker.jsSource}\n${workerSource}`;
}
TranscoderWorker._workerURL = URL.createObjectURL(new Blob([workerSource]));
}
return TranscoderWorker._workerURL;
}
protected worker: Worker;
protected initPromise: Promise<void>;
protected onInit: () => void;
constructor()
{
this.isInit = false;
this.load = 0;
this.initPromise = new Promise((resolve) => { this.onInit = resolve; });
if (!TranscoderWorker.wasmSource)
{
console.warn('PIXI.resources.BasisResource.TranscoderWorker has not been given the transcoder WASM binary!');
}
this.worker = new Worker(TranscoderWorker.workerURL);
this.worker.onmessage = this.onMessage;
this.worker.postMessage({
type: 'init',
jsSource: TranscoderWorker.jsSource,
wasmSource: TranscoderWorker.wasmSource
});
}
/** @returns a promise that is resolved when the web-worker is initialized */
initAsync(): Promise<void>
{
return this.initPromise;
}
/**
* Creates a promise that will resolve when the transcoding of a *.basis file is complete.
* @param basisData - *.basis file contents
* @param rgbaFormat - transcoding format for RGBA files
* @param rgbFormat - transcoding format for RGB files
* @returns a promise that is resolved with the transcoding response of the web-worker
*/
async transcodeAsync(
basisData: Uint8Array,
rgbaFormat: BASIS_FORMATS,
rgbFormat: BASIS_FORMATS
): Promise<ITranscodeResponse>
{
++this.load;
const requestID = TranscoderWorker._tempID++;
const requestPromise = new Promise((resolve: (data: ITranscodeResponse) => void, reject: () => void) =>
{
this.requests[requestID] = {
resolve,
reject
};
});
this.worker.postMessage({
requestID,
basisData,
rgbaFormat,
rgbFormat,
type: 'transcode'
});
return requestPromise;
}
/**
* Handles responses from the web-worker
* @param e - a message event containing the transcoded response
*/
protected onMessage = (e: MessageEvent): void =>
{
const data = e.data as ITranscodeResponse;
if (data.type === 'init')
{
if (!data.success)
{
throw new Error('BasisResource.TranscoderWorker failed to initialize.');
}
this.isInit = true;
this.onInit();
}
else if (data.type === 'transcode')
{
--this.load;
const requestID = data.requestID;
if (data.success)
{
this.requests[requestID].resolve(data);
}
else
{
this.requests[requestID].reject();
}
delete this.requests[requestID];
}
};
/**
* Loads the transcoder source code
* @param jsURL - URL to the javascript basis transcoder
* @param wasmURL - URL to the wasm basis transcoder
* @returns A promise that resolves when both the js and wasm transcoders have been loaded.
*/
static loadTranscoder(jsURL: string, wasmURL: string): Promise<[void, void]>
{
const jsPromise = fetch(jsURL)
.then((res: Response) => res.text())
.then((text: string) => { TranscoderWorker.jsSource = text; });
const wasmPromise = fetch(wasmURL)
.then((res: Response) => res.arrayBuffer())
.then((arrayBuffer: ArrayBuffer) => { TranscoderWorker.wasmSource = arrayBuffer; });
return Promise.all([jsPromise, wasmPromise]).then((data) =>
{
this._onTranscoderInitializedResolve();
return data;
});
}
/**
* Set the transcoder source code directly
* @param jsSource - source for the javascript basis transcoder
* @param wasmSource - source for the wasm basis transcoder
*/
static setTranscoder(jsSource: string, wasmSource: ArrayBuffer): void
{
TranscoderWorker.jsSource = jsSource;
TranscoderWorker.wasmSource = wasmSource;
}
}