blob: 10c5038285c1a29fd96ba17cb4af4783431443ae [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
Kevin Lubicke70af512020-05-14 14:50:54 -040013 CanvasKit.makeWebGLContextAsCurrent = function(canvas, attrs) {
Kevin Lubick5f1692c2019-01-03 16:20:04 -050014 if (!canvas) {
Kevin Lubicke70af512020-05-14 14:50:54 -040015 throw 'null canvas passed into makeWebGLContext';
Kevin Lubick5f1692c2019-01-03 16:20:04 -050016 }
Kevin Lubicke70af512020-05-14 14:50:54 -040017 var contextAttributes = {
18 'alpha': get(attrs, 'alpha', 1),
19 'depth': get(attrs, 'depth', 1),
20 'stencil': get(attrs, 'stencil', 8),
21 'antialias': get(attrs, 'antialias', 1),
22 'premultipliedAlpha': get(attrs, 'premultipliedAlpha', 1),
23 'preserveDrawingBuffer': get(attrs, 'preserveDrawingBuffer', 0),
24 'preferLowPowerToHighPerformance': get(attrs, 'preferLowPowerToHighPerformance', 0),
25 'failIfMajorPerformanceCaveat': get(attrs, 'failIfMajorPerformanceCaveat', 0),
26 'enableExtensionsByDefault': get(attrs, 'enableExtensionsByDefault', 1),
27 'explicitSwapControl': get(attrs, 'explicitSwapControl', 0),
28 'renderViaOffscreenBackBuffer': get(attrs, 'renderViaOffscreenBackBuffer', 0),
29 };
30
Kevin Lubick5f1692c2019-01-03 16:20:04 -050031 // This check is from the emscripten version
32 if (contextAttributes['explicitSwapControl']) {
Kevin Lubicke70af512020-05-14 14:50:54 -040033 throw 'explicitSwapControl is not supported';
Kevin Lubick5f1692c2019-01-03 16:20:04 -050034 }
Kevin Lubicke70af512020-05-14 14:50:54 -040035 // Creates a WebGL context and sets it to be the current context.
36 this.createContext(canvas, true, true, contextAttributes);
Kevin Lubick5f1692c2019-01-03 16:20:04 -050037 }
38
Kevin Lubick543f3522019-03-08 10:04:28 -050039 CanvasKit.GetWebGLContext = function(canvas, attrs) {
Kevin Lubicke70af512020-05-14 14:50:54 -040040 this.makeWebGLContextAsCurrent(canvas, attrs);
41 return CanvasKit.currentContext() || 0;
Kevin Lubick543f3522019-03-08 10:04:28 -050042 };
43
Kevin Lubick5f1692c2019-01-03 16:20:04 -050044 // arg can be of types:
45 // - String - in which case it is interpreted as an id of a
46 // canvas element.
47 // - HTMLCanvasElement - in which the provided canvas element will
48 // be used directly.
Kevin Lubick5f1692c2019-01-03 16:20:04 -050049 // Width and height can be provided to override those on the canvas
50 // element, or specify a height for when a context is provided.
Kevin Lubicke70af512020-05-14 14:50:54 -040051 CanvasKit.MakeWebGLCanvasSurface = function(arg, width, height) {
Kevin Lubick543f3522019-03-08 10:04:28 -050052 var canvas = arg;
53 if (canvas.tagName !== 'CANVAS') {
54 canvas = document.getElementById(arg);
55 if (!canvas) {
56 throw 'Canvas with id ' + arg + ' was not found';
Kevin Lubick5f1692c2019-01-03 16:20:04 -050057 }
Kevin Lubick5f1692c2019-01-03 16:20:04 -050058 }
Kevin Lubick5766e3b2020-04-27 10:22:51 -040059
60 // we are ok with all the other defaults.
Kevin Lubicke70af512020-05-14 14:50:54 -040061 var ctx = this.GetWebGLContext(canvas);
Kevin Lubick5f1692c2019-01-03 16:20:04 -050062
63 if (!ctx || ctx < 0) {
64 throw 'failed to create webgl context: err ' + ctx;
65 }
66
67 if (!canvas && (!width || !height)) {
68 throw 'height and width must be provided with context';
69 }
70
Kevin Lubick543f3522019-03-08 10:04:28 -050071 var grcontext = this.MakeGrContext(ctx);
Kevin Lubickc7755d92019-04-05 13:29:51 -040072
Kevin Lubickf6a9d202019-11-08 06:17:38 -080073 if (grcontext) {
74 // Bump the default resource cache limit.
75 var RESOURCE_CACHE_BYTES = 256 * 1024 * 1024;
76 grcontext.setResourceCacheLimitBytes(RESOURCE_CACHE_BYTES);
77 }
78
Kevin Lubickc7755d92019-04-05 13:29:51 -040079
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";