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 | |
Kevin Lubick | e70af51 | 2020-05-14 14:50:54 -0400 | [diff] [blame] | 13 | CanvasKit.makeWebGLContextAsCurrent = function(canvas, attrs) { |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 14 | if (!canvas) { |
Kevin Lubick | e70af51 | 2020-05-14 14:50:54 -0400 | [diff] [blame] | 15 | throw 'null canvas passed into makeWebGLContext'; |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 16 | } |
Kevin Lubick | e70af51 | 2020-05-14 14:50:54 -0400 | [diff] [blame] | 17 | var contextAttributes = { |
| 18 | 'alpha': get(attrs, 'alpha', 1), |
| 19 | 'depth': get(attrs, 'depth', 1), |
| 20 | 'stencil': get(attrs, 'stencil', 8), |
Chris Dalton | 38e33df | 2020-05-21 15:46:16 -0600 | [diff] [blame] | 21 | 'antialias': get(attrs, 'antialias', 0), |
Kevin Lubick | e70af51 | 2020-05-14 14:50:54 -0400 | [diff] [blame] | 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 Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 31 | // This check is from the emscripten version |
| 32 | if (contextAttributes['explicitSwapControl']) { |
Kevin Lubick | e70af51 | 2020-05-14 14:50:54 -0400 | [diff] [blame] | 33 | throw 'explicitSwapControl is not supported'; |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 34 | } |
Kevin Lubick | e70af51 | 2020-05-14 14:50:54 -0400 | [diff] [blame] | 35 | // Creates a WebGL context and sets it to be the current context. |
| 36 | this.createContext(canvas, true, true, contextAttributes); |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 37 | } |
| 38 | |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 39 | CanvasKit.GetWebGLContext = function(canvas, attrs) { |
Kevin Lubick | e70af51 | 2020-05-14 14:50:54 -0400 | [diff] [blame] | 40 | this.makeWebGLContextAsCurrent(canvas, attrs); |
| 41 | return CanvasKit.currentContext() || 0; |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 42 | }; |
| 43 | |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 44 | // idOrElement can be of types: |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 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. |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 49 | // colorSpace - sk_sp<SkColorSpace> - one of the supported color spaces: |
| 50 | // CanvasKit.SkColorSpace.SRGB |
| 51 | // CanvasKit.SkColorSpace.DISPLAY_P3 |
| 52 | // CanvasKit.SkColorSpace.ADOBE_RGB |
| 53 | CanvasKit.MakeWebGLCanvasSurface = function(idOrElement, colorSpace) { |
| 54 | colorSpace = colorSpace || null; |
| 55 | var canvas = idOrElement; |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 56 | if (canvas.tagName !== 'CANVAS') { |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 57 | canvas = document.getElementById(idOrElement); |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 58 | if (!canvas) { |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 59 | throw 'Canvas with id ' + idOrElement + ' 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 | |
| 63 | // we are ok with all the other defaults. |
Kevin Lubick | e70af51 | 2020-05-14 14:50:54 -0400 | [diff] [blame] | 64 | var ctx = this.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 | |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 70 | var grcontext = this.MakeGrContext(ctx); |
Kevin Lubick | c7755d9 | 2019-04-05 13:29:51 -0400 | [diff] [blame] | 71 | |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 72 | // Note that canvas.width/height here is used because it gives the size of the buffer we're |
| 73 | // rendering into. This may not be the same size the element is displayed on the page, which |
| 74 | // constrolled by css, and available in canvas.clientWidth/height. |
| 75 | var surface = this.MakeOnScreenGLSurface(grcontext, canvas.width, canvas.height, colorSpace); |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 76 | if (!surface) { |
Kevin Lubick | 6fccc9d | 2018-11-20 15:55:10 -0500 | [diff] [blame] | 77 | SkDebug('falling back from GPU implementation to a SW based one'); |
Kevin Lubick | 832787a | 2019-03-14 11:25:57 -0400 | [diff] [blame] | 78 | // we need to throw away the old canvas (which was locked to |
| 79 | // a webGL context) and create a new one so we can |
| 80 | var newCanvas = canvas.cloneNode(true); |
| 81 | var parent = canvas.parentNode; |
| 82 | parent.replaceChild(newCanvas, canvas); |
| 83 | // add a class so the user can detect that it was replaced. |
| 84 | newCanvas.classList.add('ck-replaced'); |
| 85 | |
| 86 | return CanvasKit.MakeSWCanvasSurface(newCanvas); |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 87 | } |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 88 | surface._context = ctx; |
Kevin Lubick | cd54466 | 2019-03-22 15:41:36 -0400 | [diff] [blame] | 89 | surface.grContext = grcontext; |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 90 | return surface; |
Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 91 | }; |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 92 | // Default to trying WebGL first. |
| 93 | CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface; |
Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 94 | }); |
| 95 | }(Module)); // When this file is loaded in, the high level object is "Module"; |