blob: 179b64b3dd3bbe0c64bdffd60cf168ed35c81105 [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.
3(function(CanvasKit){
4 CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
5 CanvasKit._extraInitializations.push(function() {
Kevin Lubick5f1692c2019-01-03 16:20:04 -05006 function get(obj, attr, defaultValue) {
7 if (obj && obj.hasOwnProperty(attr)) {
8 return obj[attr];
Kevin Lubick5b90b842018-10-17 07:57:18 -04009 }
Kevin Lubick5f1692c2019-01-03 16:20:04 -050010 return defaultValue;
11 }
12
13 function makeWebGLContext(canvas, attrs) {
14 // These defaults come from the emscripten _emscripten_webgl_create_context
15 var contextAttributes = {
Kevin Lubick2031f342019-02-20 15:03:59 -050016 alpha: get(attrs, 'alpha', 1),
17 depth: get(attrs, 'depth', 1),
18 stencil: get(attrs, 'stencil', 0),
19 antialias: get(attrs, 'antialias', 1),
20 premultipliedAlpha: get(attrs, 'premultipliedAlpha', 1),
21 preserveDrawingBuffer: get(attrs, 'preserveDrawingBuffer', 0),
22 preferLowPowerToHighPerformance: get(attrs, 'preferLowPowerToHighPerformance', 0),
23 failIfMajorPerformanceCaveat: get(attrs, 'failIfMajorPerformanceCaveat', 0),
Hal Canarye054d3c2019-10-16 10:17:05 -040024 majorVersion: get(attrs, 'majorVersion', 2),
Kevin Lubick2031f342019-02-20 15:03:59 -050025 minorVersion: get(attrs, 'minorVersion', 0),
26 enableExtensionsByDefault: get(attrs, 'enableExtensionsByDefault', 1),
27 explicitSwapControl: get(attrs, 'explicitSwapControl', 0),
28 renderViaOffscreenBackBuffer: get(attrs, 'renderViaOffscreenBackBuffer', 0),
Kevin Lubick5f1692c2019-01-03 16:20:04 -050029 };
30 if (!canvas) {
31 SkDebug('null canvas passed into makeWebGLContext');
32 return 0;
33 }
34 // This check is from the emscripten version
35 if (contextAttributes['explicitSwapControl']) {
36 SkDebug('explicitSwapControl is not supported');
37 return 0;
38 }
Kevin Lubick2031f342019-02-20 15:03:59 -050039 // GL is an enscripten provided helper
40 // See https://github.com/emscripten-core/emscripten/blob/incoming/src/library_webgl.js
Hal Canarye054d3c2019-10-16 10:17:05 -040041 var ctx = GL.createContext(canvas, contextAttributes);
42
43 if (!ctx && contextAttributes.majorVersion > 1) {
44 contextAttributes.majorVersion = 1; // fall back to WebGL 1.0
45 contextAttributes.minorVersion = 0;
46 ctx = GL.createContext(canvas, contextAttributes);
47 }
48 return ctx;
Kevin Lubick5f1692c2019-01-03 16:20:04 -050049 }
50
Kevin Lubick543f3522019-03-08 10:04:28 -050051 CanvasKit.GetWebGLContext = function(canvas, attrs) {
52 return makeWebGLContext(canvas, attrs);
53 };
54
Kevin Lubick5f1692c2019-01-03 16:20:04 -050055 // arg can be of types:
56 // - String - in which case it is interpreted as an id of a
57 // canvas element.
58 // - HTMLCanvasElement - in which the provided canvas element will
59 // be used directly.
Kevin Lubick5f1692c2019-01-03 16:20:04 -050060 // Width and height can be provided to override those on the canvas
61 // element, or specify a height for when a context is provided.
62 CanvasKit.MakeWebGLCanvasSurface = function(arg, width, height) {
Kevin Lubick543f3522019-03-08 10:04:28 -050063 var canvas = arg;
64 if (canvas.tagName !== 'CANVAS') {
65 canvas = document.getElementById(arg);
66 if (!canvas) {
67 throw 'Canvas with id ' + arg + ' was not found';
Kevin Lubick5f1692c2019-01-03 16:20:04 -050068 }
Kevin Lubick5f1692c2019-01-03 16:20:04 -050069 }
Kevin Lubick543f3522019-03-08 10:04:28 -050070 // we are ok with all the defaults
Kevin Lubickcd544662019-03-22 15:41:36 -040071 var ctx = this.GetWebGLContext(canvas);
Kevin Lubick5f1692c2019-01-03 16:20:04 -050072
73 if (!ctx || ctx < 0) {
74 throw 'failed to create webgl context: err ' + ctx;
75 }
76
77 if (!canvas && (!width || !height)) {
78 throw 'height and width must be provided with context';
79 }
80
Kevin Lubick543f3522019-03-08 10:04:28 -050081 var grcontext = this.MakeGrContext(ctx);
Kevin Lubickc7755d92019-04-05 13:29:51 -040082
83 // Bump the default resource cache limit.
84 var RESOURCE_CACHE_BYTES = 256 * 1024 * 1024;
85 grcontext.setResourceCacheLimitBytes(RESOURCE_CACHE_BYTES);
86
Kevin Lubick5b90b842018-10-17 07:57:18 -040087 // Maybe better to use clientWidth/height. See:
88 // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html
Kevin Lubick543f3522019-03-08 10:04:28 -050089 var surface = this.MakeOnScreenGLSurface(grcontext,
90 width || canvas.width,
91 height || canvas.height);
Kevin Lubickb07204a2018-11-20 14:07:42 -050092 if (!surface) {
Kevin Lubick6fccc9d2018-11-20 15:55:10 -050093 SkDebug('falling back from GPU implementation to a SW based one');
Kevin Lubick832787a2019-03-14 11:25:57 -040094 // we need to throw away the old canvas (which was locked to
95 // a webGL context) and create a new one so we can
96 var newCanvas = canvas.cloneNode(true);
97 var parent = canvas.parentNode;
98 parent.replaceChild(newCanvas, canvas);
99 // add a class so the user can detect that it was replaced.
100 newCanvas.classList.add('ck-replaced');
101
102 return CanvasKit.MakeSWCanvasSurface(newCanvas);
Kevin Lubickb07204a2018-11-20 14:07:42 -0500103 }
Kevin Lubick359a7e32019-03-19 09:34:37 -0400104 surface._context = ctx;
Kevin Lubickcd544662019-03-22 15:41:36 -0400105 surface.grContext = grcontext;
Kevin Lubickb07204a2018-11-20 14:07:42 -0500106 return surface;
Kevin Lubick5b90b842018-10-17 07:57:18 -0400107 };
Kevin Lubickb07204a2018-11-20 14:07:42 -0500108 // Default to trying WebGL first.
109 CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface;
Kevin Lubick5b90b842018-10-17 07:57:18 -0400110 });
111}(Module)); // When this file is loaded in, the high level object is "Module";