Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 1 | // 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 Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 6 | function get(obj, attr, defaultValue) { |
| 7 | if (obj && obj.hasOwnProperty(attr)) { |
| 8 | return obj[attr]; |
Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 9 | } |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 10 | return defaultValue; |
| 11 | } |
| 12 | |
| 13 | function makeWebGLContext(canvas, attrs) { |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 14 | var contextAttributes = { |
Kevin Lubick | 2031f34 | 2019-02-20 15:03:59 -0500 | [diff] [blame] | 15 | alpha: get(attrs, 'alpha', 1), |
| 16 | depth: get(attrs, 'depth', 1), |
Kevin Lubick | 1496758 | 2019-11-15 11:02:15 -0500 | [diff] [blame] | 17 | stencil: get(attrs, 'stencil', 8), |
Kevin Lubick | 2031f34 | 2019-02-20 15:03:59 -0500 | [diff] [blame] | 18 | 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 Canary | e054d3c | 2019-10-16 10:17:05 -0400 | [diff] [blame] | 23 | majorVersion: get(attrs, 'majorVersion', 2), |
Kevin Lubick | 2031f34 | 2019-02-20 15:03:59 -0500 | [diff] [blame] | 24 | minorVersion: get(attrs, 'minorVersion', 0), |
| 25 | enableExtensionsByDefault: get(attrs, 'enableExtensionsByDefault', 1), |
| 26 | explicitSwapControl: get(attrs, 'explicitSwapControl', 0), |
| 27 | renderViaOffscreenBackBuffer: get(attrs, 'renderViaOffscreenBackBuffer', 0), |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 28 | }; |
| 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 Lubick | 2031f34 | 2019-02-20 15:03:59 -0500 | [diff] [blame] | 38 | // GL is an enscripten provided helper |
| 39 | // See https://github.com/emscripten-core/emscripten/blob/incoming/src/library_webgl.js |
Kevin Lubick | 5766e3b | 2020-04-27 10:22:51 -0400 | [diff] [blame^] | 40 | return GL.createContext(canvas, contextAttributes); |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 41 | } |
| 42 | |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 43 | CanvasKit.GetWebGLContext = function(canvas, attrs) { |
| 44 | return makeWebGLContext(canvas, attrs); |
| 45 | }; |
| 46 | |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 47 | // 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 Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 52 | // 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 Lubick | 5766e3b | 2020-04-27 10:22:51 -0400 | [diff] [blame^] | 54 | CanvasKit.MakeWebGLCanvasSurface = function(arg, width, height, webGLVersion) { |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 55 | 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 Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 60 | } |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 61 | } |
Kevin Lubick | 5766e3b | 2020-04-27 10:22:51 -0400 | [diff] [blame^] | 62 | 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 Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 73 | |
| 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 Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 82 | var grcontext = this.MakeGrContext(ctx); |
Kevin Lubick | c7755d9 | 2019-04-05 13:29:51 -0400 | [diff] [blame] | 83 | |
Kevin Lubick | f6a9d20 | 2019-11-08 06:17:38 -0800 | [diff] [blame] | 84 | 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 Lubick | c7755d9 | 2019-04-05 13:29:51 -0400 | [diff] [blame] | 90 | |
Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 91 | // Maybe better to use clientWidth/height. See: |
| 92 | // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 93 | var surface = this.MakeOnScreenGLSurface(grcontext, |
| 94 | width || canvas.width, |
| 95 | height || canvas.height); |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 96 | if (!surface) { |
Kevin Lubick | 6fccc9d | 2018-11-20 15:55:10 -0500 | [diff] [blame] | 97 | SkDebug('falling back from GPU implementation to a SW based one'); |
Kevin Lubick | 832787a | 2019-03-14 11:25:57 -0400 | [diff] [blame] | 98 | // 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 Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 107 | } |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 108 | surface._context = ctx; |
Kevin Lubick | cd54466 | 2019-03-22 15:41:36 -0400 | [diff] [blame] | 109 | surface.grContext = grcontext; |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 110 | return surface; |
Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 111 | }; |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 112 | // Default to trying WebGL first. |
| 113 | CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface; |
Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 114 | }); |
| 115 | }(Module)); // When this file is loaded in, the high level object is "Module"; |