blob: 160593a995154c7b51a82e46b1480cfb533fedd2 [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),
24 majorVersion: get(attrs, 'majorVersion', 1),
25 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
Kevin Lubick5f1692c2019-01-03 16:20:04 -050041 return GL.createContext(canvas, contextAttributes);
42 }
43
Kevin Lubick543f3522019-03-08 10:04:28 -050044 CanvasKit.GetWebGLContext = function(canvas, attrs) {
45 return makeWebGLContext(canvas, attrs);
46 };
47
Kevin Lubick5f1692c2019-01-03 16:20:04 -050048 // arg can be of types:
49 // - String - in which case it is interpreted as an id of a
50 // canvas element.
51 // - HTMLCanvasElement - in which the provided canvas element will
52 // be used directly.
Kevin Lubick5f1692c2019-01-03 16:20:04 -050053 // Width and height can be provided to override those on the canvas
54 // element, or specify a height for when a context is provided.
55 CanvasKit.MakeWebGLCanvasSurface = function(arg, width, height) {
Kevin Lubick543f3522019-03-08 10:04:28 -050056 var canvas = arg;
57 if (canvas.tagName !== 'CANVAS') {
58 canvas = document.getElementById(arg);
59 if (!canvas) {
60 throw 'Canvas with id ' + arg + ' was not found';
Kevin Lubick5f1692c2019-01-03 16:20:04 -050061 }
Kevin Lubick5f1692c2019-01-03 16:20:04 -050062 }
Kevin Lubick543f3522019-03-08 10:04:28 -050063 // we are ok with all the defaults
Kevin Lubickcd544662019-03-22 15:41:36 -040064 var ctx = this.GetWebGLContext(canvas);
Kevin Lubick5f1692c2019-01-03 16:20:04 -050065
66 if (!ctx || ctx < 0) {
67 throw 'failed to create webgl context: err ' + ctx;
68 }
69
70 if (!canvas && (!width || !height)) {
71 throw 'height and width must be provided with context';
72 }
73
Kevin Lubick543f3522019-03-08 10:04:28 -050074 var grcontext = this.MakeGrContext(ctx);
Kevin Lubickc7755d92019-04-05 13:29:51 -040075
76 // Bump the default resource cache limit.
77 var RESOURCE_CACHE_BYTES = 256 * 1024 * 1024;
78 grcontext.setResourceCacheLimitBytes(RESOURCE_CACHE_BYTES);
79
Kevin Lubick5b90b842018-10-17 07:57:18 -040080 // Maybe better to use clientWidth/height. See:
81 // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html
Kevin Lubick543f3522019-03-08 10:04:28 -050082 var surface = this.MakeOnScreenGLSurface(grcontext,
83 width || canvas.width,
84 height || canvas.height);
Kevin Lubickb07204a2018-11-20 14:07:42 -050085 if (!surface) {
Kevin Lubick6fccc9d2018-11-20 15:55:10 -050086 SkDebug('falling back from GPU implementation to a SW based one');
Kevin Lubick832787a2019-03-14 11:25:57 -040087 // we need to throw away the old canvas (which was locked to
88 // a webGL context) and create a new one so we can
89 var newCanvas = canvas.cloneNode(true);
90 var parent = canvas.parentNode;
91 parent.replaceChild(newCanvas, canvas);
92 // add a class so the user can detect that it was replaced.
93 newCanvas.classList.add('ck-replaced');
94
95 return CanvasKit.MakeSWCanvasSurface(newCanvas);
Kevin Lubickb07204a2018-11-20 14:07:42 -050096 }
Kevin Lubick359a7e32019-03-19 09:34:37 -040097 surface._context = ctx;
Kevin Lubickcd544662019-03-22 15:41:36 -040098 surface.grContext = grcontext;
Kevin Lubickb07204a2018-11-20 14:07:42 -050099 return surface;
Kevin Lubick5b90b842018-10-17 07:57:18 -0400100 };
Kevin Lubickb07204a2018-11-20 14:07:42 -0500101 // Default to trying WebGL first.
102 CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface;
Kevin Lubick5b90b842018-10-17 07:57:18 -0400103 });
104}(Module)); // When this file is loaded in, the high level object is "Module";