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) { |
| 14 | // These defaults come from the emscripten _emscripten_webgl_create_context |
| 15 | var contextAttributes = { |
Kevin Lubick | 2031f34 | 2019-02-20 15:03:59 -0500 | [diff] [blame] | 16 | 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 Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 29 | }; |
| 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 Lubick | 2031f34 | 2019-02-20 15:03:59 -0500 | [diff] [blame] | 39 | // GL is an enscripten provided helper |
| 40 | // See https://github.com/emscripten-core/emscripten/blob/incoming/src/library_webgl.js |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 41 | return GL.createContext(canvas, contextAttributes); |
| 42 | } |
| 43 | |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 44 | CanvasKit.GetWebGLContext = function(canvas, attrs) { |
| 45 | return makeWebGLContext(canvas, attrs); |
| 46 | }; |
| 47 | |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 48 | // 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 Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 53 | // 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 Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 56 | 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 Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 61 | } |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 62 | } |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 63 | // we are ok with all the defaults |
| 64 | var ctx = CanvasKit.GetWebGLContext(canvas); |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 65 | |
| 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 Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 74 | var grcontext = this.MakeGrContext(ctx); |
Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 75 | // Maybe better to use clientWidth/height. See: |
| 76 | // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 77 | var surface = this.MakeOnScreenGLSurface(grcontext, |
| 78 | width || canvas.width, |
| 79 | height || canvas.height); |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 80 | if (!surface) { |
Kevin Lubick | 6fccc9d | 2018-11-20 15:55:10 -0500 | [diff] [blame] | 81 | SkDebug('falling back from GPU implementation to a SW based one'); |
Kevin Lubick | 832787a | 2019-03-14 11:25:57 -0400 | [diff] [blame^] | 82 | // we need to throw away the old canvas (which was locked to |
| 83 | // a webGL context) and create a new one so we can |
| 84 | var newCanvas = canvas.cloneNode(true); |
| 85 | var parent = canvas.parentNode; |
| 86 | parent.replaceChild(newCanvas, canvas); |
| 87 | // add a class so the user can detect that it was replaced. |
| 88 | newCanvas.classList.add('ck-replaced'); |
| 89 | |
| 90 | return CanvasKit.MakeSWCanvasSurface(newCanvas); |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 91 | } |
| 92 | return surface; |
Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 93 | }; |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 94 | // Default to trying WebGL first. |
| 95 | CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface; |
Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 96 | }); |
| 97 | }(Module)); // When this file is loaded in, the high level object is "Module"; |