tav-media
Version:
Cross platform media editing framework
100 lines (99 loc) • 3.73 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var PAGSurface_1;
import { PAGModule } from './pag-module';
import { destroyVerify, wasmAwaitRewind } from './utils/decorators';
let PAGSurface = PAGSurface_1 = class PAGSurface {
constructor(wasmIns) {
this.isDestroyed = false;
this.wasmIns = wasmIns;
}
/**
* Make a PAGSurface from canvas.
*/
static fromCanvas(canvasID) {
const wasmIns = PAGModule._PAGSurface._FromCanvas(canvasID);
if (!wasmIns)
throw new Error(`Make PAGSurface from canvas ${canvasID} fail!`);
return new PAGSurface_1(wasmIns);
}
/**
* Make a PAGSurface from texture.
*/
static fromTexture(textureID, width, height, flipY) {
const wasmIns = PAGModule._PAGSurface._FromTexture(textureID, width, height, flipY);
if (!wasmIns)
throw new Error(`Make PAGSurface from texture ${textureID} fail!`);
return new PAGSurface_1(wasmIns);
}
/**
* Make a PAGSurface from frameBuffer.
*/
static fromRenderTarget(frameBufferID, width, height, flipY) {
const wasmIns = PAGModule._PAGSurface._FromRenderTarget(frameBufferID, width, height, flipY);
if (!wasmIns)
throw new Error(`Make PAGSurface from frameBuffer ${frameBufferID} fail!`);
return new PAGSurface_1(wasmIns);
}
/**
* The width of surface in pixels.
*/
width() {
return this.wasmIns._width();
}
/**
* The height of surface in pixels.
*/
height() {
return this.wasmIns._height();
}
/**
* Update the size of surface, and reset the internal surface.
*/
updateSize() {
this.wasmIns._updateSize();
}
/**
* Erases all pixels of this surface with transparent color. Returns true if the content has
* changed.
*/
clearAll() {
return this.wasmIns._clearAll();
}
/**
* Free the cache created by the surface immediately. Can be called to reduce memory pressure.
*/
freeCache() {
this.wasmIns._freeCache();
}
/**
* Copies pixels from current PAGSurface to dstPixels with specified color type, alpha type and
* row bytes. Returns true if pixels are copied to dstPixels.
*/
readPixels(colorType, alphaType) {
if (colorType === 0 /* ColorType.Unknown */)
return null;
const rowBytes = this.width() * (colorType === 1 /* ColorType.ALPHA_8 */ ? 1 : 4);
const length = rowBytes * this.height();
const dataUint8Array = new Uint8Array(length);
const dataPtr = PAGModule._malloc(dataUint8Array.byteLength);
const dataOnHeap = new Uint8Array(PAGModule.HEAPU8.buffer, dataPtr, dataUint8Array.byteLength);
const res = this.wasmIns._readPixels(colorType, alphaType, dataPtr, rowBytes);
dataUint8Array.set(dataOnHeap);
PAGModule._free(dataPtr);
return res ? dataUint8Array : null;
}
destroy() {
this.wasmIns.delete();
this.isDestroyed = true;
}
};
PAGSurface = PAGSurface_1 = __decorate([
destroyVerify,
wasmAwaitRewind
], PAGSurface);
export { PAGSurface };