2018-10-17 11:57:18 +00:00
|
|
|
// Adds compile-time JS functions to augment the CanvasKit interface.
|
|
|
|
// Specifically, anything that should only be on the GPU version of canvaskit.
|
2020-05-29 23:51:21 +00:00
|
|
|
// Functions in this file are supplemented by cpu.js.
|
2018-10-17 11:57:18 +00:00
|
|
|
(function(CanvasKit){
|
|
|
|
CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
|
|
|
|
CanvasKit._extraInitializations.push(function() {
|
2019-01-03 21:20:04 +00:00
|
|
|
function get(obj, attr, defaultValue) {
|
|
|
|
if (obj && obj.hasOwnProperty(attr)) {
|
|
|
|
return obj[attr];
|
|
|
|
}
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2020-08-19 18:30:58 +00:00
|
|
|
CanvasKit.GetWebGLContext = function(canvas, attrs) {
|
2018-10-17 11:57:18 +00:00
|
|
|
if (!canvas) {
|
2020-05-14 18:50:54 +00:00
|
|
|
throw 'null canvas passed into makeWebGLContext';
|
2019-01-03 21:20:04 +00:00
|
|
|
}
|
2020-05-14 18:50:54 +00:00
|
|
|
var contextAttributes = {
|
|
|
|
'alpha': get(attrs, 'alpha', 1),
|
|
|
|
'depth': get(attrs, 'depth', 1),
|
|
|
|
'stencil': get(attrs, 'stencil', 8),
|
2020-05-21 21:46:16 +00:00
|
|
|
'antialias': get(attrs, 'antialias', 0),
|
2020-05-14 18:50:54 +00:00
|
|
|
'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),
|
|
|
|
};
|
|
|
|
|
2020-07-17 19:20:44 +00:00
|
|
|
if (attrs && attrs['majorVersion']) {
|
|
|
|
contextAttributes['majorVersion'] = attrs['majorVersion']
|
2020-08-19 18:30:58 +00:00
|
|
|
} else {
|
|
|
|
// Default to WebGL 2 if available and not specified.
|
|
|
|
contextAttributes['majorVersion'] = (typeof WebGL2RenderingContext !== 'undefined') ? 2 : 1;
|
2020-07-17 19:20:44 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 21:20:04 +00:00
|
|
|
// This check is from the emscripten version
|
|
|
|
if (contextAttributes['explicitSwapControl']) {
|
2020-05-14 18:50:54 +00:00
|
|
|
throw 'explicitSwapControl is not supported';
|
2019-01-03 21:20:04 +00:00
|
|
|
}
|
2020-05-14 18:50:54 +00:00
|
|
|
// Creates a WebGL context and sets it to be the current context.
|
2020-08-19 18:30:58 +00:00
|
|
|
// These functions are defined in emscripten's library_webgl.js
|
|
|
|
var handle = GL.createContext(canvas, contextAttributes);
|
|
|
|
if (!handle) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
GL.makeContextCurrent(handle);
|
|
|
|
return handle;
|
2020-12-10 20:21:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CanvasKit.deleteContext = function(handle) {
|
|
|
|
GL.deleteContext(handle);
|
|
|
|
};
|
2019-01-03 21:20:04 +00:00
|
|
|
|
2021-06-03 12:02:03 +00:00
|
|
|
CanvasKit._setTextureCleanup({
|
|
|
|
"deleteTexture": function(webglHandle, texHandle) {
|
|
|
|
var tex = GL.textures[texHandle];
|
|
|
|
if (tex) {
|
|
|
|
GL.getContext(webglHandle).GLctx.deleteTexture(tex);
|
|
|
|
}
|
|
|
|
GL.textures[texHandle] = null;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-08-25 17:13:09 +00:00
|
|
|
CanvasKit.MakeGrContext = function(ctx) {
|
|
|
|
// Make sure we are pointing at the right WebGL context.
|
|
|
|
if (!this.setCurrentContext(ctx)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var grCtx = this._MakeGrContext();
|
|
|
|
if (!grCtx) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// This context is an index into the emscripten-provided GL wrapper.
|
|
|
|
grCtx._context = ctx;
|
|
|
|
return grCtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
CanvasKit.MakeOnScreenGLSurface = function(grCtx, w, h, colorspace) {
|
|
|
|
var surface = this._MakeOnScreenGLSurface(grCtx, w, h, colorspace);
|
|
|
|
if (!surface) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
surface._context = grCtx._context;
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
CanvasKit.MakeRenderTarget = function(grCtx, w, h) {
|
|
|
|
var surface = this._MakeRenderTargetWH(grCtx, w, h);
|
|
|
|
if (!surface) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
surface._context = grCtx._context;
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
CanvasKit.MakeRenderTarget = function(grCtx, imageInfo) {
|
|
|
|
var surface = this._MakeRenderTargetII(grCtx, imageInfo);
|
|
|
|
if (!surface) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
surface._context = grCtx._context;
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:10:20 +00:00
|
|
|
// idOrElement can be of types:
|
2019-01-03 21:20:04 +00:00
|
|
|
// - String - in which case it is interpreted as an id of a
|
|
|
|
// canvas element.
|
|
|
|
// - HTMLCanvasElement - in which the provided canvas element will
|
|
|
|
// be used directly.
|
2020-10-07 20:09:22 +00:00
|
|
|
// colorSpace - sk_sp<ColorSpace> - one of the supported color spaces:
|
|
|
|
// CanvasKit.ColorSpace.SRGB
|
|
|
|
// CanvasKit.ColorSpace.DISPLAY_P3
|
|
|
|
// CanvasKit.ColorSpace.ADOBE_RGB
|
2020-07-17 19:20:44 +00:00
|
|
|
CanvasKit.MakeWebGLCanvasSurface = function(idOrElement, colorSpace, attrs) {
|
2020-05-26 17:10:20 +00:00
|
|
|
colorSpace = colorSpace || null;
|
|
|
|
var canvas = idOrElement;
|
2020-07-21 21:46:58 +00:00
|
|
|
var isHTMLCanvas = typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement;
|
2020-08-21 13:47:54 +00:00
|
|
|
var isOffscreenCanvas = typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas;
|
2020-07-21 21:46:58 +00:00
|
|
|
if (!isHTMLCanvas && !isOffscreenCanvas) {
|
2020-05-26 17:10:20 +00:00
|
|
|
canvas = document.getElementById(idOrElement);
|
2019-03-08 15:04:28 +00:00
|
|
|
if (!canvas) {
|
2020-05-26 17:10:20 +00:00
|
|
|
throw 'Canvas with id ' + idOrElement + ' was not found';
|
2019-01-03 21:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-27 14:22:51 +00:00
|
|
|
|
2020-07-17 19:20:44 +00:00
|
|
|
var ctx = this.GetWebGLContext(canvas, attrs);
|
2019-01-03 21:20:04 +00:00
|
|
|
if (!ctx || ctx < 0) {
|
|
|
|
throw 'failed to create webgl context: err ' + ctx;
|
|
|
|
}
|
|
|
|
|
2019-03-08 15:04:28 +00:00
|
|
|
var grcontext = this.MakeGrContext(ctx);
|
2019-04-05 17:29:51 +00:00
|
|
|
|
2020-05-26 17:10:20 +00:00
|
|
|
// Note that canvas.width/height here is used because it gives the size of the buffer we're
|
|
|
|
// rendering into. This may not be the same size the element is displayed on the page, which
|
|
|
|
// constrolled by css, and available in canvas.clientWidth/height.
|
|
|
|
var surface = this.MakeOnScreenGLSurface(grcontext, canvas.width, canvas.height, colorSpace);
|
2018-11-20 19:07:42 +00:00
|
|
|
if (!surface) {
|
2020-10-07 20:09:22 +00:00
|
|
|
Debug('falling back from GPU implementation to a SW based one');
|
2019-03-14 15:25:57 +00:00
|
|
|
// we need to throw away the old canvas (which was locked to
|
|
|
|
// a webGL context) and create a new one so we can
|
|
|
|
var newCanvas = canvas.cloneNode(true);
|
|
|
|
var parent = canvas.parentNode;
|
|
|
|
parent.replaceChild(newCanvas, canvas);
|
|
|
|
// add a class so the user can detect that it was replaced.
|
|
|
|
newCanvas.classList.add('ck-replaced');
|
|
|
|
|
|
|
|
return CanvasKit.MakeSWCanvasSurface(newCanvas);
|
2018-11-20 19:07:42 +00:00
|
|
|
}
|
|
|
|
return surface;
|
2018-10-17 11:57:18 +00:00
|
|
|
};
|
2018-11-20 19:07:42 +00:00
|
|
|
// Default to trying WebGL first.
|
|
|
|
CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface;
|
2021-06-03 12:02:03 +00:00
|
|
|
|
|
|
|
CanvasKit.Surface.prototype.makeImageFromTexture = function(tex, info) {
|
|
|
|
if (!info['colorSpace']) {
|
|
|
|
info['colorSpace'] = CanvasKit.ColorSpace.SRGB;
|
|
|
|
}
|
|
|
|
// GL is an emscripten object that holds onto WebGL state. One item in that state is
|
|
|
|
// an array of textures, of which the index is the handle/id.
|
|
|
|
var texHandle = GL.textures.length;
|
|
|
|
if (!texHandle) {
|
|
|
|
// If our texture handle is 0, Skia interprets that as an invalid texture id.
|
|
|
|
// As a special case, we push a null texture there so the first texture has id 1.
|
|
|
|
GL.textures.push(null);
|
|
|
|
texHandle = 1;
|
|
|
|
}
|
|
|
|
GL.textures.push(tex);
|
|
|
|
return this._makeImageFromTexture(GL.currentContext.handle, texHandle, info);
|
|
|
|
};
|
|
|
|
|
|
|
|
CanvasKit.Surface.prototype.makeImageFromTextureSource = function(src, w, h) {
|
|
|
|
// If the user specified a height or width in the image info, we use that. Otherwise,
|
|
|
|
// we try to find the natural media type (for <img> and <video>) and then fall back to
|
|
|
|
// the height and width (to cover <canvas>, ImageBitmap or ImageData).
|
|
|
|
var height = h || src.naturalHeight || src.videoHeight || src.height;
|
|
|
|
var width = w || src.naturalWidth || src.videoWidth || src.width;
|
2021-08-25 17:13:09 +00:00
|
|
|
// We want to be pointing at the context associated with this surface.
|
|
|
|
CanvasKit.setCurrentContext(this._context);
|
2021-06-03 12:02:03 +00:00
|
|
|
var glCtx = GL.currentContext.GLctx;
|
|
|
|
var newTex = glCtx.createTexture();
|
|
|
|
glCtx.bindTexture(glCtx.TEXTURE_2D, newTex);
|
|
|
|
if (GL.currentContext.version === 2) {
|
|
|
|
glCtx.texImage2D(glCtx.TEXTURE_2D, 0, glCtx.RGBA, width, height, 0, glCtx.RGBA, glCtx.UNSIGNED_BYTE, src);
|
|
|
|
} else {
|
|
|
|
glCtx.texImage2D(glCtx.TEXTURE_2D, 0, glCtx.RGBA, glCtx.RGBA, glCtx.UNSIGNED_BYTE, src);
|
|
|
|
}
|
|
|
|
glCtx.bindTexture(glCtx.TEXTURE_2D, null);
|
|
|
|
var info = {
|
|
|
|
'height': height,
|
|
|
|
'width': width,
|
|
|
|
'colorType': CanvasKit.ColorType.RGBA_8888,
|
|
|
|
'alphaType': CanvasKit.AlphaType.Unpremul,
|
|
|
|
'colorSpace': CanvasKit.ColorSpace.SRGB,
|
|
|
|
};
|
|
|
|
return this.makeImageFromTexture(newTex, info);
|
|
|
|
};
|
2021-08-25 17:13:09 +00:00
|
|
|
|
|
|
|
CanvasKit.setCurrentContext = function(ctx) {
|
|
|
|
if (!ctx) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return GL.makeContextCurrent(ctx);
|
|
|
|
};
|
2018-10-17 11:57:18 +00:00
|
|
|
});
|
|
|
|
}(Module)); // When this file is loaded in, the high level object is "Module";
|