(function(CanvasKit){
CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
CanvasKit._extraInitializations.push(function() {
function get(obj, attr, defaultValue) {
if (obj && obj.hasOwnProperty(attr)) {
return obj[attr];
}
return defaultValue;
}
CanvasKit.GetWebGLContext = function(canvas, attrs) {
if (!canvas) {
throw 'null canvas passed into makeWebGLContext';
}
var contextAttributes = {
'alpha': get(attrs, 'alpha', 1),
'depth': get(attrs, 'depth', 1),
'stencil': get(attrs, 'stencil', 8),
'antialias': get(attrs, 'antialias', 0),
'premultipliedAlpha': get(attrs, 'premultipliedAlpha', 1),
'preserveDrawingBuffer': get(attrs, 'preserveDrawingBuffer', 0),
'preferLowPowerToHighPerformance': get(attrs, 'preferLowPowerToHighPerformance', 0),
'failIfMajorPerformanceCaveat': get(attrs, 'failIfMajorPerformanceCaveat', 0),
'enableExtensionsByDefault': get(attrs, 'enableExtensionsByDefault', 1),
'explicitSwapControl': get(attrs, 'explicitSwapControl', 0),
'renderViaOffscreenBackBuffer': get(attrs, 'renderViaOffscreenBackBuffer', 0),
};
if (attrs && attrs['majorVersion']) {
contextAttributes['majorVersion'] = attrs['majorVersion']
} else {
contextAttributes['majorVersion'] = (typeof WebGL2RenderingContext !== 'undefined') ? 2 : 1;
}
if (contextAttributes['explicitSwapControl']) {
throw 'explicitSwapControl is not supported';
}
var handle = GL.createContext(canvas, contextAttributes);
if (!handle) {
return 0;
}
GL.makeContextCurrent(handle);
GL.currentContext.GLctx.getExtension('WEBGL_debug_renderer_info');
return handle;
};
CanvasKit.deleteContext = function(handle) {
GL.deleteContext(handle);
};
CanvasKit._setTextureCleanup({
'deleteTexture': function(webglHandle, texHandle) {
var tex = GL.textures[texHandle];
if (tex) {
GL.getContext(webglHandle).GLctx.deleteTexture(tex);
}
GL.textures[texHandle] = null;
},
});
CanvasKit.MakeWebGLContext = function(ctx) {
if (!this.setCurrentContext(ctx)) {
return null;
}
var grCtx = this._MakeGrContext();
if (!grCtx) {
return null;
}
grCtx._context = ctx;
var oldDelete = grCtx.delete.bind(grCtx);
grCtx['delete'] = function() {
CanvasKit.setCurrentContext(this._context);
oldDelete();
}.bind(grCtx);
GL.currentContext.grDirectContext = grCtx;
return grCtx;
};
CanvasKit.MakeGrContext = CanvasKit.MakeWebGLContext;
CanvasKit.GrDirectContext.prototype.getResourceCacheLimitBytes = function() {
CanvasKit.setCurrentContext(this._context);
this._getResourceCacheLimitBytes();
};
CanvasKit.GrDirectContext.prototype.getResourceCacheUsageBytes = function() {
CanvasKit.setCurrentContext(this._context);
this._getResourceCacheUsageBytes();
};
CanvasKit.GrDirectContext.prototype.releaseResourcesAndAbandonContext = function() {
CanvasKit.setCurrentContext(this._context);
this._releaseResourcesAndAbandonContext();
};
CanvasKit.GrDirectContext.prototype.setResourceCacheLimitBytes = function(maxResourceBytes) {
CanvasKit.setCurrentContext(this._context);
this._setResourceCacheLimitBytes(maxResourceBytes);
};
CanvasKit.MakeOnScreenGLSurface = function(grCtx, w, h, colorspace, sc, st) {
if (!this.setCurrentContext(grCtx._context)) {
return null;
}
var surface;
if (sc === undefined || st === undefined) {
surface = this._MakeOnScreenGLSurface(grCtx, w, h, colorspace);
} else {
surface = this._MakeOnScreenGLSurface(grCtx, w, h, colorspace, sc, st);
}
if (!surface) {
return null;
}
surface._context = grCtx._context;
return surface;
}
CanvasKit.MakeRenderTarget = function() {
var grCtx = arguments[0];
if (!this.setCurrentContext(grCtx._context)) {
return null;
}
var surface;
if (arguments.length === 3) {
surface = this._MakeRenderTargetWH(grCtx, arguments[1], arguments[2]);
if (!surface) {
return null;
}
} else if (arguments.length === 2) {
surface = this._MakeRenderTargetII(grCtx, arguments[1]);
if (!surface) {
return null;
}
} else {
Debug('Expected 2 or 3 params');
return null;
}
surface._context = grCtx._context;
return surface;
}
CanvasKit.MakeWebGLCanvasSurface = function(idOrElement, colorSpace, attrs) {
colorSpace = colorSpace || null;
var canvas = idOrElement;
var isHTMLCanvas = typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement;
var isOffscreenCanvas = typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas;
if (!isHTMLCanvas && !isOffscreenCanvas) {
canvas = document.getElementById(idOrElement);
if (!canvas) {
throw 'Canvas with id ' + idOrElement + ' was not found';
}
}
var ctx = this.GetWebGLContext(canvas, attrs);
if (!ctx || ctx < 0) {
throw 'failed to create webgl context: err ' + ctx;
}
var grcontext = this.MakeWebGLContext(ctx);
var surface = this.MakeOnScreenGLSurface(grcontext, canvas.width, canvas.height, colorSpace);
if (!surface) {
Debug('falling back from GPU implementation to a SW based one');
var newCanvas = canvas.cloneNode(true);
var parent = canvas.parentNode;
parent.replaceChild(newCanvas, canvas);
newCanvas.classList.add('ck-replaced');
return CanvasKit.MakeSWCanvasSurface(newCanvas);
}
return surface;
};
CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface;
function pushTexture(tex) {
var texHandle = GL.getNewId(GL.textures);
GL.textures[texHandle] = tex;
return texHandle
}
CanvasKit.Surface.prototype.makeImageFromTexture = function(tex, info) {
CanvasKit.setCurrentContext(this._context);
var texHandle = pushTexture(tex);
var img = this._makeImageFromTexture(this._context, texHandle, info);
if (img) {
img._tex = texHandle;
}
return img;
};
function getHeight(src) {
return src['naturalHeight'] || src['videoHeight'] || src['displayHeight'] || src['height'];
}
function getWidth(src) {
return src['naturalWidth'] || src['videoWidth'] || src['displayWidth'] || src['width'];
}
function setupTexture(glCtx, newTex, imageInfo, srcIsPremul) {
glCtx.bindTexture(glCtx.TEXTURE_2D, newTex);
if (!srcIsPremul && imageInfo['alphaType'] === CanvasKit.AlphaType.Premul) {
glCtx.pixelStorei(glCtx.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
}
return newTex;
}
function resetTexture(glCtx, imageInfo, srcIsPremul) {
if (!srcIsPremul && imageInfo['alphaType'] === CanvasKit.AlphaType.Premul) {
glCtx.pixelStorei(glCtx.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
}
glCtx.bindTexture(glCtx.TEXTURE_2D, null);
}
CanvasKit.Surface.prototype.makeImageFromTextureSource = function(src, info, srcIsPremul) {
if (!info) {
info = {
'height': getHeight(src),
'width': getWidth(src),
'colorType': CanvasKit.ColorType.RGBA_8888,
'alphaType': srcIsPremul ? CanvasKit.AlphaType.Premul: CanvasKit.AlphaType.Unpremul,
};
}
if (!info['colorSpace']) {
info['colorSpace'] = CanvasKit.ColorSpace.SRGB;
}
if (info['colorType'] !== CanvasKit.ColorType.RGBA_8888) {
Debug('colorType currently has no impact on makeImageFromTextureSource');
}
CanvasKit.setCurrentContext(this._context);
var glCtx = GL.currentContext.GLctx;
var newTex = setupTexture(glCtx, glCtx.createTexture(), info, srcIsPremul);
if (GL.currentContext.version === 2) {
glCtx.texImage2D(glCtx.TEXTURE_2D, 0, glCtx.RGBA, info['width'], info['height'], 0, glCtx.RGBA, glCtx.UNSIGNED_BYTE, src);
} else {
glCtx.texImage2D(glCtx.TEXTURE_2D, 0, glCtx.RGBA, glCtx.RGBA, glCtx.UNSIGNED_BYTE, src);
}
resetTexture(glCtx, info);
this._resetContext();
return this.makeImageFromTexture(newTex, info);
};
CanvasKit.Surface.prototype.updateTextureFromSource = function(img, src, srcIsPremul) {
if (!img._tex) {
Debug('Image is not backed by a user-provided texture');
return;
}
CanvasKit.setCurrentContext(this._context);
var ii = img.getImageInfo();
var glCtx = GL.currentContext.GLctx;
var tex = setupTexture(glCtx, GL.textures[img._tex], ii, srcIsPremul);
if (GL.currentContext.version === 2) {
glCtx.texImage2D(glCtx.TEXTURE_2D, 0, glCtx.RGBA, getWidth(src), getHeight(src), 0, glCtx.RGBA, glCtx.UNSIGNED_BYTE, src);
} else {
glCtx.texImage2D(glCtx.TEXTURE_2D, 0, glCtx.RGBA, glCtx.RGBA, glCtx.UNSIGNED_BYTE, src);
}
resetTexture(glCtx, ii, srcIsPremul);
this._resetContext();
GL.textures[img._tex] = null;
img._tex = pushTexture(tex);
ii['colorSpace'] = img.getColorSpace();
var newImg = this._makeImageFromTexture(this._context, img._tex, ii);
var oldPtr = img.$$.ptr;
var oldSmartPtr = img.$$.smartPtr;
img.$$.ptr = newImg.$$.ptr;
img.$$.smartPtr = newImg.$$.smartPtr;
newImg.$$.ptr = oldPtr;
newImg.$$.smartPtr = oldSmartPtr;
newImg.delete();
ii['colorSpace'].delete();
}
CanvasKit.MakeLazyImageFromTextureSource = function(src, info, srcIsPremul) {
if (!info) {
info = {
'height': getHeight(src),
'width': getWidth(src),
'colorType': CanvasKit.ColorType.RGBA_8888,
'alphaType': srcIsPremul ? CanvasKit.AlphaType.Premul : CanvasKit.AlphaType.Unpremul,
};
}
if (!info['colorSpace']) {
info['colorSpace'] = CanvasKit.ColorSpace.SRGB;
}
if (info['colorType'] !== CanvasKit.ColorType.RGBA_8888) {
Debug('colorType currently has no impact on MakeLazyImageFromTextureSource');
}
var callbackObj = {
'makeTexture': function() {
var ctx = GL.currentContext;
var glCtx = ctx.GLctx;
var newTex = setupTexture(glCtx, glCtx.createTexture(), info, srcIsPremul);
if (ctx.version === 2) {
glCtx.texImage2D(glCtx.TEXTURE_2D, 0, glCtx.RGBA, info['width'], info['height'], 0, glCtx.RGBA, glCtx.UNSIGNED_BYTE, src);
} else {
glCtx.texImage2D(glCtx.TEXTURE_2D, 0, glCtx.RGBA, glCtx.RGBA, glCtx.UNSIGNED_BYTE, src);
}
resetTexture(glCtx, info, srcIsPremul);
return pushTexture(newTex);
},
'freeSrc': function() {
},
}
if (src.constructor.name === 'VideoFrame') {
callbackObj['freeSrc'] = function() {
src.close();
}
}
return CanvasKit.Image._makeFromGenerator(info, callbackObj);
}
CanvasKit.setCurrentContext = function(ctx) {
if (!ctx) {
return false;
}
return GL.makeContextCurrent(ctx);
};
CanvasKit.getCurrentGrDirectContext = function() {
if (GL.currentContext && GL.currentContext.grDirectContext &&
!GL.currentContext.grDirectContext['isDeleted']()) {
return GL.currentContext.grDirectContext;
}
return null;
};
});
}(Module));