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. |
Bryce Thomas | 9331ca0 | 2020-05-29 16:51:21 -0700 | [diff] [blame] | 3 | // Functions in this file are supplemented by cpu.js. |
Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 4 | (function(CanvasKit){ |
| 5 | CanvasKit._extraInitializations = CanvasKit._extraInitializations || []; |
| 6 | CanvasKit._extraInitializations.push(function() { |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 7 | function get(obj, attr, defaultValue) { |
| 8 | if (obj && obj.hasOwnProperty(attr)) { |
| 9 | return obj[attr]; |
Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 10 | } |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 11 | return defaultValue; |
| 12 | } |
| 13 | |
Kevin Lubick | 204c3be | 2020-08-19 14:30:58 -0400 | [diff] [blame] | 14 | CanvasKit.GetWebGLContext = function(canvas, attrs) { |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 15 | if (!canvas) { |
Kevin Lubick | e70af51 | 2020-05-14 14:50:54 -0400 | [diff] [blame] | 16 | throw 'null canvas passed into makeWebGLContext'; |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 17 | } |
Kevin Lubick | e70af51 | 2020-05-14 14:50:54 -0400 | [diff] [blame] | 18 | var contextAttributes = { |
| 19 | 'alpha': get(attrs, 'alpha', 1), |
| 20 | 'depth': get(attrs, 'depth', 1), |
| 21 | 'stencil': get(attrs, 'stencil', 8), |
Chris Dalton | 38e33df | 2020-05-21 15:46:16 -0600 | [diff] [blame] | 22 | 'antialias': get(attrs, 'antialias', 0), |
Kevin Lubick | e70af51 | 2020-05-14 14:50:54 -0400 | [diff] [blame] | 23 | 'premultipliedAlpha': get(attrs, 'premultipliedAlpha', 1), |
| 24 | 'preserveDrawingBuffer': get(attrs, 'preserveDrawingBuffer', 0), |
| 25 | 'preferLowPowerToHighPerformance': get(attrs, 'preferLowPowerToHighPerformance', 0), |
| 26 | 'failIfMajorPerformanceCaveat': get(attrs, 'failIfMajorPerformanceCaveat', 0), |
| 27 | 'enableExtensionsByDefault': get(attrs, 'enableExtensionsByDefault', 1), |
| 28 | 'explicitSwapControl': get(attrs, 'explicitSwapControl', 0), |
| 29 | 'renderViaOffscreenBackBuffer': get(attrs, 'renderViaOffscreenBackBuffer', 0), |
| 30 | }; |
| 31 | |
Nathaniel Nifong | a237f9e | 2020-07-17 15:20:44 -0400 | [diff] [blame] | 32 | if (attrs && attrs['majorVersion']) { |
| 33 | contextAttributes['majorVersion'] = attrs['majorVersion'] |
Kevin Lubick | 204c3be | 2020-08-19 14:30:58 -0400 | [diff] [blame] | 34 | } else { |
| 35 | // Default to WebGL 2 if available and not specified. |
| 36 | contextAttributes['majorVersion'] = (typeof WebGL2RenderingContext !== 'undefined') ? 2 : 1; |
Nathaniel Nifong | a237f9e | 2020-07-17 15:20:44 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 39 | // This check is from the emscripten version |
| 40 | if (contextAttributes['explicitSwapControl']) { |
Kevin Lubick | e70af51 | 2020-05-14 14:50:54 -0400 | [diff] [blame] | 41 | throw 'explicitSwapControl is not supported'; |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 42 | } |
Kevin Lubick | e70af51 | 2020-05-14 14:50:54 -0400 | [diff] [blame] | 43 | // Creates a WebGL context and sets it to be the current context. |
Kevin Lubick | 204c3be | 2020-08-19 14:30:58 -0400 | [diff] [blame] | 44 | // These functions are defined in emscripten's library_webgl.js |
| 45 | var handle = GL.createContext(canvas, contextAttributes); |
| 46 | if (!handle) { |
| 47 | return 0; |
| 48 | } |
| 49 | GL.makeContextCurrent(handle); |
| 50 | return handle; |
Kevin Lubick | f7fdf1a | 2020-12-10 15:21:01 -0500 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | CanvasKit.deleteContext = function(handle) { |
| 54 | GL.deleteContext(handle); |
| 55 | }; |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 56 | |
Kevin Lubick | 7b6a928 | 2021-06-03 08:02:03 -0400 | [diff] [blame] | 57 | CanvasKit._setTextureCleanup({ |
| 58 | "deleteTexture": function(webglHandle, texHandle) { |
| 59 | var tex = GL.textures[texHandle]; |
| 60 | if (tex) { |
| 61 | GL.getContext(webglHandle).GLctx.deleteTexture(tex); |
| 62 | } |
| 63 | GL.textures[texHandle] = null; |
| 64 | }, |
| 65 | }); |
| 66 | |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 67 | CanvasKit.MakeGrContext = function(ctx) { |
| 68 | // Make sure we are pointing at the right WebGL context. |
| 69 | if (!this.setCurrentContext(ctx)) { |
| 70 | return null; |
| 71 | } |
| 72 | var grCtx = this._MakeGrContext(); |
| 73 | if (!grCtx) { |
| 74 | return null; |
| 75 | } |
| 76 | // This context is an index into the emscripten-provided GL wrapper. |
| 77 | grCtx._context = ctx; |
| 78 | return grCtx; |
| 79 | } |
| 80 | |
| 81 | CanvasKit.MakeOnScreenGLSurface = function(grCtx, w, h, colorspace) { |
| 82 | var surface = this._MakeOnScreenGLSurface(grCtx, w, h, colorspace); |
| 83 | if (!surface) { |
| 84 | return null; |
| 85 | } |
| 86 | surface._context = grCtx._context; |
| 87 | return surface; |
| 88 | } |
| 89 | |
| 90 | CanvasKit.MakeRenderTarget = function(grCtx, w, h) { |
| 91 | var surface = this._MakeRenderTargetWH(grCtx, w, h); |
| 92 | if (!surface) { |
| 93 | return null; |
| 94 | } |
| 95 | surface._context = grCtx._context; |
| 96 | return surface; |
| 97 | } |
| 98 | |
| 99 | CanvasKit.MakeRenderTarget = function(grCtx, imageInfo) { |
| 100 | var surface = this._MakeRenderTargetII(grCtx, imageInfo); |
| 101 | if (!surface) { |
| 102 | return null; |
| 103 | } |
| 104 | surface._context = grCtx._context; |
| 105 | return surface; |
| 106 | } |
| 107 | |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 108 | // idOrElement can be of types: |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 109 | // - String - in which case it is interpreted as an id of a |
| 110 | // canvas element. |
| 111 | // - HTMLCanvasElement - in which the provided canvas element will |
| 112 | // be used directly. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 113 | // colorSpace - sk_sp<ColorSpace> - one of the supported color spaces: |
| 114 | // CanvasKit.ColorSpace.SRGB |
| 115 | // CanvasKit.ColorSpace.DISPLAY_P3 |
| 116 | // CanvasKit.ColorSpace.ADOBE_RGB |
Nathaniel Nifong | a237f9e | 2020-07-17 15:20:44 -0400 | [diff] [blame] | 117 | CanvasKit.MakeWebGLCanvasSurface = function(idOrElement, colorSpace, attrs) { |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 118 | colorSpace = colorSpace || null; |
| 119 | var canvas = idOrElement; |
Elliot Evans | 683bbe0 | 2020-07-21 17:46:58 -0400 | [diff] [blame] | 120 | var isHTMLCanvas = typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement; |
Florin Malita | d7389b0 | 2020-08-21 09:47:54 -0400 | [diff] [blame] | 121 | var isOffscreenCanvas = typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas; |
Elliot Evans | 683bbe0 | 2020-07-21 17:46:58 -0400 | [diff] [blame] | 122 | if (!isHTMLCanvas && !isOffscreenCanvas) { |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 123 | canvas = document.getElementById(idOrElement); |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 124 | if (!canvas) { |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 125 | throw 'Canvas with id ' + idOrElement + ' was not found'; |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 126 | } |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 127 | } |
Kevin Lubick | 5766e3b | 2020-04-27 10:22:51 -0400 | [diff] [blame] | 128 | |
Nathaniel Nifong | a237f9e | 2020-07-17 15:20:44 -0400 | [diff] [blame] | 129 | var ctx = this.GetWebGLContext(canvas, attrs); |
Kevin Lubick | 5f1692c | 2019-01-03 16:20:04 -0500 | [diff] [blame] | 130 | if (!ctx || ctx < 0) { |
| 131 | throw 'failed to create webgl context: err ' + ctx; |
| 132 | } |
| 133 | |
Kevin Lubick | 543f352 | 2019-03-08 10:04:28 -0500 | [diff] [blame] | 134 | var grcontext = this.MakeGrContext(ctx); |
Kevin Lubick | c7755d9 | 2019-04-05 13:29:51 -0400 | [diff] [blame] | 135 | |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 136 | // Note that canvas.width/height here is used because it gives the size of the buffer we're |
| 137 | // rendering into. This may not be the same size the element is displayed on the page, which |
| 138 | // constrolled by css, and available in canvas.clientWidth/height. |
| 139 | var surface = this.MakeOnScreenGLSurface(grcontext, canvas.width, canvas.height, colorSpace); |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 140 | if (!surface) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 141 | Debug('falling back from GPU implementation to a SW based one'); |
Kevin Lubick | 832787a | 2019-03-14 11:25:57 -0400 | [diff] [blame] | 142 | // we need to throw away the old canvas (which was locked to |
| 143 | // a webGL context) and create a new one so we can |
| 144 | var newCanvas = canvas.cloneNode(true); |
| 145 | var parent = canvas.parentNode; |
| 146 | parent.replaceChild(newCanvas, canvas); |
| 147 | // add a class so the user can detect that it was replaced. |
| 148 | newCanvas.classList.add('ck-replaced'); |
| 149 | |
| 150 | return CanvasKit.MakeSWCanvasSurface(newCanvas); |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 151 | } |
| 152 | return surface; |
Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 153 | }; |
Kevin Lubick | b07204a | 2018-11-20 14:07:42 -0500 | [diff] [blame] | 154 | // Default to trying WebGL first. |
| 155 | CanvasKit.MakeCanvasSurface = CanvasKit.MakeWebGLCanvasSurface; |
Kevin Lubick | 7b6a928 | 2021-06-03 08:02:03 -0400 | [diff] [blame] | 156 | |
| 157 | CanvasKit.Surface.prototype.makeImageFromTexture = function(tex, info) { |
| 158 | if (!info['colorSpace']) { |
| 159 | info['colorSpace'] = CanvasKit.ColorSpace.SRGB; |
| 160 | } |
| 161 | // GL is an emscripten object that holds onto WebGL state. One item in that state is |
| 162 | // an array of textures, of which the index is the handle/id. |
| 163 | var texHandle = GL.textures.length; |
| 164 | if (!texHandle) { |
| 165 | // If our texture handle is 0, Skia interprets that as an invalid texture id. |
| 166 | // As a special case, we push a null texture there so the first texture has id 1. |
| 167 | GL.textures.push(null); |
| 168 | texHandle = 1; |
| 169 | } |
| 170 | GL.textures.push(tex); |
| 171 | return this._makeImageFromTexture(GL.currentContext.handle, texHandle, info); |
| 172 | }; |
| 173 | |
| 174 | CanvasKit.Surface.prototype.makeImageFromTextureSource = function(src, w, h) { |
| 175 | // If the user specified a height or width in the image info, we use that. Otherwise, |
| 176 | // we try to find the natural media type (for <img> and <video>) and then fall back to |
| 177 | // the height and width (to cover <canvas>, ImageBitmap or ImageData). |
| 178 | var height = h || src.naturalHeight || src.videoHeight || src.height; |
| 179 | var width = w || src.naturalWidth || src.videoWidth || src.width; |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 180 | // We want to be pointing at the context associated with this surface. |
| 181 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | 7b6a928 | 2021-06-03 08:02:03 -0400 | [diff] [blame] | 182 | var glCtx = GL.currentContext.GLctx; |
| 183 | var newTex = glCtx.createTexture(); |
| 184 | glCtx.bindTexture(glCtx.TEXTURE_2D, newTex); |
| 185 | if (GL.currentContext.version === 2) { |
| 186 | glCtx.texImage2D(glCtx.TEXTURE_2D, 0, glCtx.RGBA, width, height, 0, glCtx.RGBA, glCtx.UNSIGNED_BYTE, src); |
| 187 | } else { |
| 188 | glCtx.texImage2D(glCtx.TEXTURE_2D, 0, glCtx.RGBA, glCtx.RGBA, glCtx.UNSIGNED_BYTE, src); |
| 189 | } |
| 190 | glCtx.bindTexture(glCtx.TEXTURE_2D, null); |
| 191 | var info = { |
| 192 | 'height': height, |
| 193 | 'width': width, |
| 194 | 'colorType': CanvasKit.ColorType.RGBA_8888, |
| 195 | 'alphaType': CanvasKit.AlphaType.Unpremul, |
| 196 | 'colorSpace': CanvasKit.ColorSpace.SRGB, |
| 197 | }; |
| 198 | return this.makeImageFromTexture(newTex, info); |
| 199 | }; |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 200 | |
| 201 | CanvasKit.setCurrentContext = function(ctx) { |
| 202 | if (!ctx) { |
| 203 | return false; |
| 204 | } |
| 205 | return GL.makeContextCurrent(ctx); |
| 206 | }; |
Kevin Lubick | 5b90b84 | 2018-10-17 07:57:18 -0400 | [diff] [blame] | 207 | }); |
| 208 | }(Module)); // When this file is loaded in, the high level object is "Module"; |