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.
|
|
|
|
(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-05-14 18:50:54 +00:00
|
|
|
CanvasKit.makeWebGLContextAsCurrent = 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),
|
|
|
|
'antialias': get(attrs, 'antialias', 1),
|
|
|
|
'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),
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
this.createContext(canvas, true, true, contextAttributes);
|
2019-01-03 21:20:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 15:04:28 +00:00
|
|
|
CanvasKit.GetWebGLContext = function(canvas, attrs) {
|
2020-05-14 18:50:54 +00:00
|
|
|
this.makeWebGLContextAsCurrent(canvas, attrs);
|
|
|
|
return CanvasKit.currentContext() || 0;
|
2019-03-08 15:04:28 +00:00
|
|
|
};
|
|
|
|
|
2020-05-21 18:53:41 +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-05-21 18:53:41 +00:00
|
|
|
// colorSpace - sk_sp<SkColorSpace> - one of the supported color spaces:
|
|
|
|
// CanvasKit.SkColorSpace.SRGB
|
|
|
|
// CanvasKit.SkColorSpace.DISPLAY_P3
|
|
|
|
// CanvasKit.SkColorSpace.ADOBE_RGB
|
|
|
|
CanvasKit.MakeWebGLCanvasSurface = function(idOrElement, colorSpace) {
|
|
|
|
colorSpace = colorSpace || null;
|
|
|
|
var canvas = idOrElement;
|
2019-03-08 15:04:28 +00:00
|
|
|
if (canvas.tagName !== 'CANVAS') {
|
2020-05-21 18:53:41 +00:00
|
|
|
canvas = document.getElementById(idOrElement);
|
2019-03-08 15:04:28 +00:00
|
|
|
if (!canvas) {
|
2020-05-21 18:53:41 +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
|
|
|
|
|
|
|
// we are ok with all the other defaults.
|
2020-05-14 18:50:54 +00:00
|
|
|
var ctx = this.GetWebGLContext(canvas);
|
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
|
|
|
|
2019-11-08 14:17:38 +00:00
|
|
|
if (grcontext) {
|
|
|
|
// Bump the default resource cache limit.
|
|
|
|
var RESOURCE_CACHE_BYTES = 256 * 1024 * 1024;
|
|
|
|
grcontext.setResourceCacheLimitBytes(RESOURCE_CACHE_BYTES);
|
|
|
|
}
|
|
|
|
|
2020-05-21 18:53:41 +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) {
|
2018-11-20 20:55:10 +00:00
|
|
|
SkDebug('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
|
|
|
}
|
2019-03-19 13:34:37 +00:00
|
|
|
surface._context = ctx;
|
2019-03-22 19:41:36 +00:00
|
|
|
surface.grContext = grcontext;
|
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;
|
2018-10-17 11:57:18 +00:00
|
|
|
});
|
|
|
|
}(Module)); // When this file is loaded in, the high level object is "Module";
|