tav-media
Version:
Cross platform media editing framework
97 lines (96 loc) • 3.05 kB
JavaScript
import { PAGModule } from '../pag-module';
import { WEBGL_CONTEXT_ATTRIBUTES } from '../constant';
export class BackendContext {
constructor(handle, adopted = false) {
this.isDestroyed = false;
this.oldHandle = 0;
this.handle = handle;
this.adopted = adopted;
}
static from(gl) {
if (gl instanceof BackendContext) {
return new BackendContext(gl.handle, true);
}
else {
const majorVersion = window.WebGL2RenderingContext && gl instanceof window.WebGL2RenderingContext ? 2 : 1;
const { GL } = PAGModule;
let id = 0;
if (GL.contexts.length > 0) {
id = GL.contexts.findIndex((context) => (context === null || context === void 0 ? void 0 : context.GLctx) === gl);
}
if (id < 1) {
id = GL.registerContext(gl, Object.assign({ majorVersion: majorVersion, minorVersion: 0 }, WEBGL_CONTEXT_ATTRIBUTES));
return new BackendContext(id);
}
return new BackendContext(id, true);
}
}
getContext() {
return PAGModule.GL.getContext(this.handle).GLctx;
}
makeCurrent() {
var _a;
if (this.isDestroyed)
return false;
this.oldHandle = ((_a = PAGModule.GL.currentContext) === null || _a === void 0 ? void 0 : _a.handle) || 0;
if (this.oldHandle === this.handle)
return true;
return PAGModule.GL.makeContextCurrent(this.handle);
}
clearCurrent() {
if (this.isDestroyed)
return;
if (this.oldHandle === this.handle)
return;
PAGModule.GL.makeContextCurrent(0);
if (this.oldHandle) {
PAGModule.GL.makeContextCurrent(this.oldHandle);
}
}
/**
* Register WebGLTexture in EmscriptenGL, And return handle.
*/
registerTexture(texture) {
return this.register(PAGModule.GL.textures, texture);
}
/**
* Get WebGLTexture by handle.
*/
getTexture(handled) {
return PAGModule.GL.textures[handled];
}
/**
* Unregister WebGLTexture reference in EmscriptenGL.
*/
unregisterTexture(handle) {
PAGModule.GL.textures[handle] = null;
}
/**
* Register WebGLFramebuffer in EmscriptenGL, And return handle.
*/
registerRenderTarget(framebuffer) {
return this.register(PAGModule.GL.framebuffers, framebuffer);
}
/**
* Get WebGLFramebuffer by handle.
*/
getRenderTarget(handle) {
return PAGModule.GL.framebuffers[handle];
}
/**
* Unregister WebGLTexture reference in EmscriptenGL.
*/
unregisterRenderTarget(handle) {
PAGModule.GL.framebuffers[handle] = null;
}
destroy() {
if (this.adopted)
return;
PAGModule.GL.deleteContext(this.handle);
}
register(table, item) {
const handle = PAGModule.GL.getNewId(table);
table[handle] = item;
return handle;
}
}