blob: a1aeaf79d0cac64ea7d9769937c6e74f1660050e [file] [log] [blame]
Kevin Lubick5b90b842018-10-17 07:57:18 -04001// Adds compile-time JS functions to augment the CanvasKit interface.
2// Specifically, anything that should only be on the GPU version of canvaskit.
Bryce Thomas9331ca02020-05-29 16:51:21 -07003// Functions in this file are supplemented by cpu.js.
Kevin Lubick5b90b842018-10-17 07:57:18 -04004(function(CanvasKit){
5 CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
6 CanvasKit._extraInitializations.push(function() {
Kevin Lubick5f1692c2019-01-03 16:20:04 -05007 function get(obj, attr, defaultValue) {
8 if (obj && obj.hasOwnProperty(attr)) {
9 return obj[attr];
Kevin Lubick5b90b842018-10-17 07:57:18 -040010 }
Kevin Lubick5f1692c2019-01-03 16:20:04 -050011 return defaultValue;
12 }
13
Kevin Lubicke70af512020-05-14 14:50:54 -040014 CanvasKit.makeWebGLContextAsCurrent = function(canvas, attrs) {
Kevin Lubick5f1692c2019-01-03 16:20:04 -050015 if (!canvas) {
Kevin Lubicke70af512020-05-14 14:50:54 -040016 throw 'null canvas passed into makeWebGLContext';
Kevin Lubick5f1692c2019-01-03 16:20:04 -050017 }
Kevin Lubicke70af512020-05-14 14:50:54 -040018 var contextAttributes = {
19 'alpha': get(attrs, 'alpha', 1),
20 'depth': get(attrs, 'depth', 1),
21 'stencil': get(attrs, 'stencil', 8),
Chris Dalton38e33df2020-05-21 15:46:16 -060022 'antialias': get(attrs, 'antialias', 0),
Kevin Lubicke70af512020-05-14 14:50:54 -040023 'premultipliedAlpha': get(attrs, 'premultipliedAlpha', 1),
24 'preserveDrawingBuffer': get(attrs, 'preserveDrawingBuffer', 0),
25 'preferLowPowerToHighPerformance': get(attrs, 'preferLowPowerToHighPerformance', 0),
26 'failIfMajorPerformanceCaveat': get(attrs, 'failIfMajorPerformanceCaveat', 0),
27 'enableExtensionsByDefault': get(attrs, 'enableExtensionsByDefault', 1),
28 'explicitSwapControl': get(attrs, 'explicitSwapControl', 0),
29 'renderViaOffscreenBackBuffer': get(attrs, 'renderViaOffscreenBackBuffer', 0),
30 };
31
Nathaniel Nifonga237f9e2020-07-17 15:20:44 -040032 if (attrs && attrs['majorVersion']) {
33 contextAttributes['majorVersion'] = attrs['majorVersion']
34 }
35
Kevin Lubick5f1692c2019-01-03 16:20:04 -050036 // This check is from the emscripten version
37 if (contextAttributes['explicitSwapControl']) {
Kevin Lubicke70af512020-05-14 14:50:54 -040038 throw 'explicitSwapControl is not supported';
Kevin Lubick5f1692c2019-01-03 16:20:04 -050039 }
Kevin Lubicke70af512020-05-14 14:50:54 -040040 // Creates a WebGL context and sets it to be the current context.
41 this.createContext(canvas, true, true, contextAttributes);
Kevin Lubick5f1692c2019-01-03 16:20:04 -050042 }
43
Kevin Lubick543f3522019-03-08 10:04:28 -050044 CanvasKit.GetWebGLContext = function(canvas, attrs) {
Kevin Lubicke70af512020-05-14 14:50:54 -040045 this.makeWebGLContextAsCurrent(canvas, attrs);
46 return CanvasKit.currentContext() || 0;
Kevin Lubick543f3522019-03-08 10:04:28 -050047 };
48
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -040049 // idOrElement can be of types:
Kevin Lubick5f1692c2019-01-03 16:20:04 -050050 // - String - in which case it is interpreted as an id of a
51 // canvas element.
52 // - HTMLCanvasElement - in which the provided canvas element will
53 // be used directly.
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -040054 // colorSpace - sk_sp<SkColorSpace> - one of the supported color spaces:
55 // CanvasKit.SkColorSpace.SRGB
56 // CanvasKit.SkColorSpace.DISPLAY_P3
57 // CanvasKit.SkColorSpace.ADOBE_RGB
Nathaniel Nifonga237f9e2020-07-17 15:20:44 -040058 CanvasKit.MakeWebGLCanvasSurface = function(idOrElement, colorSpace, attrs) {
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -040059 colorSpace = colorSpace || null;
60 var canvas = idOrElement;
Kevin Lubick543f3522019-03-08 10:04:28 -050061 if (canvas.tagName !== 'CANVAS') {
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -040062 canvas = document.getElementById(idOrElement);
Kevin Lubick543f3522019-03-08 10:04:28 -050063 if (!canvas) {
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -040064 throw 'Canvas with id ' + idOrElement + ' was not found';
Kevin Lubick5f1692c2019-01-03 16:20:04 -050065 }
Kevin Lubick5f1692c2019-01-03 16:20:04 -050066 }
Kevin Lubick5766e3b2020-04-27 10:22:51 -040067
Nathaniel Nifonga237f9e2020-07-17 15:20:44 -040068 var ctx = this.GetWebGLContext(canvas, attrs);
69 var openGLversion = canvas.GLctxObject.version;
Kevin Lubick5f1692c2019-01-03 16:20:04 -050070
71 if (!ctx || ctx < 0) {
72 throw 'failed to create webgl context: err ' + ctx;
73 }
74
Kevin Lubick543f3522019-03-08 10:04:28 -050075 var grcontext = this.MakeGrContext(ctx);
Kevin Lubickc7755d92019-04-05 13:29:51 -040076
Nathaniel Nifongb1ebbb12020-05-26 13:10:20 -040077 // Note that canvas.width/height here is used because it gives the size of the buffer we're
78 // rendering into. This may not be the same size the element is displayed on the page, which
79 // constrolled by css, and available in canvas.clientWidth/height.
80 var surface = this.MakeOnScreenGLSurface(grcontext, canvas.width, canvas.height, colorSpace);
Kevin Lubickb07204a2018-11-20 14:07:42 -050081 if (!surface) {
Kevin Lubick6fccc9d2018-11-20 15:55:10 -050082 SkDebug('falling back from GPU implementation to a SW based one');
Kevin Lubick832787a2019-03-14 11:25:57 -040083 // we need to throw away the old canvas (which was locked to
84 // a webGL context) and create a new one so we can
85 var newCanvas = canvas.cloneNode(true);
86 var parent = canvas.parentNode;
87 parent.replaceChild(newCanvas, canvas);
88 // add a class so the user can detect that it was replaced.
89 newCanvas.classList.add('ck-replaced');
90
91 return CanvasKit.MakeSWCanvasSurface(newCanvas);
Kevin Lubickb07204a2018-11-20 14:07:42 -050092 }
Kevin Lubick359a7e32019-03-19 09:34:37 -040093 surface._context = ctx;
Kevin Lubickcd544662019-03-22 15:41:36 -040094 surface.grContext = grcontext;
Nathaniel Nifonga237f9e2020-07-17 15:20:44 -040095 surface.openGLversion = openGLversion;
Kevin Lubickb07204a2018-11-20 14:07:42 -050096 return surface;
Kevin Lubick5b90b842018-10-17 07:57:18 -040097 };
Kevin Lubickb07204a2018-11-20 14:07:42 -050098 // Default to trying WebGL first.
99 CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface;
Kevin Lubick5b90b842018-10-17 07:57:18 -0400100 });
101}(Module)); // When this file is loaded in, the high level object is "Module";