blob: adfc7a172b258e3d644f18c800170d975178090f [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) {
Kevin Lubick5f1692c2019-01-03 16:20:04 -050014 var contextAttributes = {
Kevin Lubick2031f342019-02-20 15:03:59 -050015 alpha: get(attrs, 'alpha', 1),
16 depth: get(attrs, 'depth', 1),
Kevin Lubick14967582019-11-15 11:02:15 -050017 stencil: get(attrs, 'stencil', 8),
Kevin Lubick2031f342019-02-20 15:03:59 -050018 antialias: get(attrs, 'antialias', 1),
19 premultipliedAlpha: get(attrs, 'premultipliedAlpha', 1),
20 preserveDrawingBuffer: get(attrs, 'preserveDrawingBuffer', 0),
21 preferLowPowerToHighPerformance: get(attrs, 'preferLowPowerToHighPerformance', 0),
22 failIfMajorPerformanceCaveat: get(attrs, 'failIfMajorPerformanceCaveat', 0),
Hal Canarye054d3c2019-10-16 10:17:05 -040023 majorVersion: get(attrs, 'majorVersion', 2),
Kevin Lubick2031f342019-02-20 15:03:59 -050024 minorVersion: get(attrs, 'minorVersion', 0),
25 enableExtensionsByDefault: get(attrs, 'enableExtensionsByDefault', 1),
26 explicitSwapControl: get(attrs, 'explicitSwapControl', 0),
27 renderViaOffscreenBackBuffer: get(attrs, 'renderViaOffscreenBackBuffer', 0),
Kevin Lubick5f1692c2019-01-03 16:20:04 -050028 };
29 if (!canvas) {
30 SkDebug('null canvas passed into makeWebGLContext');
31 return 0;
32 }
33 // This check is from the emscripten version
34 if (contextAttributes['explicitSwapControl']) {
35 SkDebug('explicitSwapControl is not supported');
36 return 0;
37 }
Kevin Lubick2031f342019-02-20 15:03:59 -050038 // GL is an enscripten provided helper
39 // See https://github.com/emscripten-core/emscripten/blob/incoming/src/library_webgl.js
Kevin Lubick5766e3b2020-04-27 10:22:51 -040040 return GL.createContext(canvas, contextAttributes);
Kevin Lubick5f1692c2019-01-03 16:20:04 -050041 }
42
Kevin Lubick543f3522019-03-08 10:04:28 -050043 CanvasKit.GetWebGLContext = function(canvas, attrs) {
44 return makeWebGLContext(canvas, attrs);
45 };
46
Kevin Lubick5f1692c2019-01-03 16:20:04 -050047 // arg can be of types:
48 // - String - in which case it is interpreted as an id of a
49 // canvas element.
50 // - HTMLCanvasElement - in which the provided canvas element will
51 // be used directly.
Kevin Lubick5f1692c2019-01-03 16:20:04 -050052 // Width and height can be provided to override those on the canvas
53 // element, or specify a height for when a context is provided.
Kevin Lubick5766e3b2020-04-27 10:22:51 -040054 CanvasKit.MakeWebGLCanvasSurface = function(arg, width, height, webGLVersion) {
Kevin Lubick543f3522019-03-08 10:04:28 -050055 var canvas = arg;
56 if (canvas.tagName !== 'CANVAS') {
57 canvas = document.getElementById(arg);
58 if (!canvas) {
59 throw 'Canvas with id ' + arg + ' was not found';
Kevin Lubick5f1692c2019-01-03 16:20:04 -050060 }
Kevin Lubick5f1692c2019-01-03 16:20:04 -050061 }
Kevin Lubick5766e3b2020-04-27 10:22:51 -040062 if (!webGLVersion) {
63 if (!!window['safari']) {
64 // Safari, by default, should use WebGL 1. skbug.com/10171
65 webGLVersion = 1;
66 } else {
67 webGLVersion = 2;
68 }
69 }
70
71 // we are ok with all the other defaults.
72 var ctx = this.GetWebGLContext(canvas, {majorVersion: webGLVersion});
Kevin Lubick5f1692c2019-01-03 16:20:04 -050073
74 if (!ctx || ctx < 0) {
75 throw 'failed to create webgl context: err ' + ctx;
76 }
77
78 if (!canvas && (!width || !height)) {
79 throw 'height and width must be provided with context';
80 }
81
Kevin Lubick543f3522019-03-08 10:04:28 -050082 var grcontext = this.MakeGrContext(ctx);
Kevin Lubickc7755d92019-04-05 13:29:51 -040083
Kevin Lubickf6a9d202019-11-08 06:17:38 -080084 if (grcontext) {
85 // Bump the default resource cache limit.
86 var RESOURCE_CACHE_BYTES = 256 * 1024 * 1024;
87 grcontext.setResourceCacheLimitBytes(RESOURCE_CACHE_BYTES);
88 }
89
Kevin Lubickc7755d92019-04-05 13:29:51 -040090
Kevin Lubick5b90b842018-10-17 07:57:18 -040091 // Maybe better to use clientWidth/height. See:
92 // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html
Kevin Lubick543f3522019-03-08 10:04:28 -050093 var surface = this.MakeOnScreenGLSurface(grcontext,
94 width || canvas.width,
95 height || canvas.height);
Kevin Lubickb07204a2018-11-20 14:07:42 -050096 if (!surface) {
Kevin Lubick6fccc9d2018-11-20 15:55:10 -050097 SkDebug('falling back from GPU implementation to a SW based one');
Kevin Lubick832787a2019-03-14 11:25:57 -040098 // we need to throw away the old canvas (which was locked to
99 // a webGL context) and create a new one so we can
100 var newCanvas = canvas.cloneNode(true);
101 var parent = canvas.parentNode;
102 parent.replaceChild(newCanvas, canvas);
103 // add a class so the user can detect that it was replaced.
104 newCanvas.classList.add('ck-replaced');
105
106 return CanvasKit.MakeSWCanvasSurface(newCanvas);
Kevin Lubickb07204a2018-11-20 14:07:42 -0500107 }
Kevin Lubick359a7e32019-03-19 09:34:37 -0400108 surface._context = ctx;
Kevin Lubickcd544662019-03-22 15:41:36 -0400109 surface.grContext = grcontext;
Kevin Lubickb07204a2018-11-20 14:07:42 -0500110 return surface;
Kevin Lubick5b90b842018-10-17 07:57:18 -0400111 };
Kevin Lubickb07204a2018-11-20 14:07:42 -0500112 // Default to trying WebGL first.
113 CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface;
Kevin Lubick5b90b842018-10-17 07:57:18 -0400114 });
115}(Module)); // When this file is loaded in, the high level object is "Module";