Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 1 | // Adds JS functions to augment the CanvasKit interface. |
| 2 | // For example, if there is a wrapper around the C++ call or logic to allow |
| 3 | // chaining, it should go here. |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 4 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 5 | // CanvasKit.onRuntimeInitialized is called after the WASM library has loaded. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 6 | // Anything that modifies an exposed class (e.g. Path) should be set |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 7 | // after onRuntimeInitialized, otherwise, it can happen outside of that scope. |
| 8 | CanvasKit.onRuntimeInitialized = function() { |
| 9 | // All calls to 'this' need to go in externs.js so closure doesn't minify them away. |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 10 | |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 11 | _scratchColor = CanvasKit.Malloc(Float32Array, 4); // 4 color scalars. |
Kevin Lubick | b42660f | 2020-06-03 09:58:27 -0400 | [diff] [blame] | 12 | _scratchColorPtr = _scratchColor['byteOffset']; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 13 | |
| 14 | _scratch4x4Matrix = CanvasKit.Malloc(Float32Array, 16); // 16 matrix scalars. |
Kevin Lubick | b42660f | 2020-06-03 09:58:27 -0400 | [diff] [blame] | 15 | _scratch4x4MatrixPtr = _scratch4x4Matrix['byteOffset']; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 16 | |
| 17 | _scratch3x3Matrix = CanvasKit.Malloc(Float32Array, 9); // 9 matrix scalars. |
Kevin Lubick | b42660f | 2020-06-03 09:58:27 -0400 | [diff] [blame] | 18 | _scratch3x3MatrixPtr = _scratch3x3Matrix['byteOffset']; |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 19 | |
| 20 | _scratchRRect = CanvasKit.Malloc(Float32Array, 12); // 4 scalars for rrect, 8 for radii. |
| 21 | _scratchRRectPtr = _scratchRRect['byteOffset']; |
| 22 | |
| 23 | _scratchRRect2 = CanvasKit.Malloc(Float32Array, 12); // 4 scalars for rrect, 8 for radii. |
| 24 | _scratchRRect2Ptr = _scratchRRect2['byteOffset']; |
| 25 | |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 26 | _scratchFourFloatsA = CanvasKit.Malloc(Float32Array, 4); |
| 27 | _scratchFourFloatsAPtr = _scratchFourFloatsA['byteOffset']; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 28 | |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 29 | _scratchFourFloatsB = CanvasKit.Malloc(Float32Array, 4); |
| 30 | _scratchFourFloatsBPtr = _scratchFourFloatsB['byteOffset']; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 31 | |
Kevin Lubick | f6040ef | 2021-02-02 08:26:48 -0500 | [diff] [blame] | 32 | _scratchThreeFloatsA = CanvasKit.Malloc(Float32Array, 3); // 3 floats to represent SkVector3 |
| 33 | _scratchThreeFloatsAPtr = _scratchThreeFloatsA['byteOffset']; |
| 34 | |
| 35 | _scratchThreeFloatsB = CanvasKit.Malloc(Float32Array, 3); // 3 floats to represent SkVector3 |
| 36 | _scratchThreeFloatsBPtr = _scratchThreeFloatsB['byteOffset']; |
| 37 | |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 38 | _scratchIRect = CanvasKit.Malloc(Int32Array, 4); |
| 39 | _scratchIRectPtr = _scratchIRect['byteOffset']; |
| 40 | |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 41 | // Create single copies of all three supported color spaces |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 42 | // These are sk_sp<ColorSpace> |
| 43 | CanvasKit.ColorSpace.SRGB = CanvasKit.ColorSpace._MakeSRGB(); |
| 44 | CanvasKit.ColorSpace.DISPLAY_P3 = CanvasKit.ColorSpace._MakeDisplayP3(); |
| 45 | CanvasKit.ColorSpace.ADOBE_RGB = CanvasKit.ColorSpace._MakeAdobeRGB(); |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 46 | |
Kevin Lubick | 995f2e6 | 2021-04-30 11:12:01 -0400 | [diff] [blame] | 47 | // Use quotes to tell closure compiler not to minify the names |
| 48 | CanvasKit['GlyphRunFlags'] = { |
| 49 | 'IsWhiteSpace': CanvasKit['_GlyphRunFlags_isWhiteSpace'], |
| 50 | }; |
| 51 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 52 | CanvasKit.Path.MakeFromCmds = function(cmds) { |
Kevin Lubick | 82186e9 | 2021-06-02 10:29:11 -0400 | [diff] [blame] | 53 | var cmdPtr = copy1dArray(cmds, 'HEAPF32'); |
| 54 | var path = CanvasKit.Path._MakeFromCmds(cmdPtr, cmds.length); |
| 55 | freeArraysThatAreNotMallocedByUsers(cmdPtr, cmds); |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 56 | return path; |
| 57 | }; |
| 58 | |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 59 | // The weights array is optional (only used for conics). |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 60 | CanvasKit.Path.MakeFromVerbsPointsWeights = function(verbs, pts, weights) { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 61 | var verbsPtr = copy1dArray(verbs, 'HEAPU8'); |
| 62 | var pointsPtr = copy1dArray(pts, 'HEAPF32'); |
| 63 | var weightsPtr = copy1dArray(weights, 'HEAPF32'); |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 64 | var numWeights = (weights && weights.length) || 0; |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 65 | var path = CanvasKit.Path._MakeFromVerbsPointsWeights( |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 66 | verbsPtr, verbs.length, pointsPtr, pts.length, weightsPtr, numWeights); |
| 67 | freeArraysThatAreNotMallocedByUsers(verbsPtr, verbs); |
| 68 | freeArraysThatAreNotMallocedByUsers(pointsPtr, pts); |
| 69 | freeArraysThatAreNotMallocedByUsers(weightsPtr, weights); |
| 70 | return path; |
| 71 | }; |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 72 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 73 | CanvasKit.Path.prototype.addArc = function(oval, startAngle, sweepAngle) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 74 | // see arc() for the HTMLCanvas version |
| 75 | // note input angles are degrees. |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 76 | var oPtr = copyRectToWasm(oval); |
| 77 | this._addArc(oPtr, startAngle, sweepAngle); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 78 | return this; |
| 79 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 80 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 81 | CanvasKit.Path.prototype.addOval = function(oval, isCCW, startIndex) { |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 82 | if (startIndex === undefined) { |
| 83 | startIndex = 1; |
| 84 | } |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 85 | var oPtr = copyRectToWasm(oval); |
| 86 | this._addOval(oPtr, !!isCCW, startIndex); |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 87 | return this; |
| 88 | }; |
| 89 | |
Kevin Lubick | a2535af | 2020-10-02 08:01:57 -0400 | [diff] [blame] | 90 | // TODO(kjlubick) clean up this API - split it apart if necessary |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 91 | CanvasKit.Path.prototype.addPath = function() { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 92 | // Takes 1, 2, 7, or 10 required args, where the first arg is always the path. |
| 93 | // The last arg is optional and chooses between add or extend mode. |
| 94 | // The options for the remaining args are: |
| 95 | // - an array of 6 or 9 parameters (perspective is optional) |
| 96 | // - the 9 parameters of a full matrix or |
| 97 | // the 6 non-perspective params of a matrix. |
| 98 | var args = Array.prototype.slice.call(arguments); |
| 99 | var path = args[0]; |
| 100 | var extend = false; |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 101 | if (typeof args[args.length-1] === 'boolean') { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 102 | extend = args.pop(); |
| 103 | } |
| 104 | if (args.length === 1) { |
| 105 | // Add path, unchanged. Use identity matrix |
| 106 | this._addPath(path, 1, 0, 0, |
| 107 | 0, 1, 0, |
| 108 | 0, 0, 1, |
| 109 | extend); |
| 110 | } else if (args.length === 2) { |
| 111 | // User provided the 9 params of a full matrix as an array. |
| 112 | var a = args[1]; |
| 113 | this._addPath(path, a[0], a[1], a[2], |
| 114 | a[3], a[4], a[5], |
| 115 | a[6] || 0, a[7] || 0, a[8] || 1, |
| 116 | extend); |
| 117 | } else if (args.length === 7 || args.length === 10) { |
| 118 | // User provided the 9 params of a (full) matrix directly. |
| 119 | // (or just the 6 non perspective ones) |
| 120 | // These are in the same order as what Skia expects. |
| 121 | var a = args; |
| 122 | this._addPath(path, a[1], a[2], a[3], |
| 123 | a[4], a[5], a[6], |
| 124 | a[7] || 0, a[8] || 0, a[9] || 1, |
| 125 | extend); |
| 126 | } else { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 127 | Debug('addPath expected to take 1, 2, 7, or 10 required args. Got ' + args.length); |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 128 | return null; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 129 | } |
| 130 | return this; |
| 131 | }; |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 132 | |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 133 | // points is a 1d array of length 2n representing n points where the even indices |
| 134 | // will be treated as x coordinates and the odd indices will be treated as y coordinates. |
| 135 | // Like other APIs, this accepts a malloced type array or malloc obj. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 136 | CanvasKit.Path.prototype.addPoly = function(points, close) { |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 137 | var ptr = copy1dArray(points, 'HEAPF32'); |
| 138 | this._addPoly(ptr, points.length / 2, close); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 139 | freeArraysThatAreNotMallocedByUsers(ptr, points); |
Kevin Lubick | 37ab53e | 2019-11-11 10:06:08 -0500 | [diff] [blame] | 140 | return this; |
| 141 | }; |
| 142 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 143 | CanvasKit.Path.prototype.addRect = function(rect, isCCW) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 144 | var rPtr = copyRectToWasm(rect); |
| 145 | this._addRect(rPtr, !!isCCW); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 146 | return this; |
| 147 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 148 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 149 | CanvasKit.Path.prototype.addRRect = function(rrect, isCCW) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 150 | var rPtr = copyRRectToWasm(rrect); |
| 151 | this._addRRect(rPtr, !!isCCW); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 152 | return this; |
| 153 | }; |
| 154 | |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 155 | // The weights array is optional (only used for conics). |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 156 | CanvasKit.Path.prototype.addVerbsPointsWeights = function(verbs, points, weights) { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 157 | var verbsPtr = copy1dArray(verbs, 'HEAPU8'); |
| 158 | var pointsPtr = copy1dArray(points, 'HEAPF32'); |
| 159 | var weightsPtr = copy1dArray(weights, 'HEAPF32'); |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 160 | var numWeights = (weights && weights.length) || 0; |
| 161 | this._addVerbsPointsWeights(verbsPtr, verbs.length, pointsPtr, points.length, |
| 162 | weightsPtr, numWeights); |
| 163 | freeArraysThatAreNotMallocedByUsers(verbsPtr, verbs); |
| 164 | freeArraysThatAreNotMallocedByUsers(pointsPtr, points); |
| 165 | freeArraysThatAreNotMallocedByUsers(weightsPtr, weights); |
| 166 | }; |
| 167 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 168 | CanvasKit.Path.prototype.arc = function(x, y, radius, startAngle, endAngle, ccw) { |
| 169 | // emulates the HTMLCanvas behavior. See addArc() for the Path version. |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 170 | // Note input angles are radians. |
| 171 | var bounds = CanvasKit.LTRBRect(x-radius, y-radius, x+radius, y+radius); |
| 172 | var sweep = radiansToDegrees(endAngle - startAngle) - (360 * !!ccw); |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 173 | var temp = new CanvasKit.Path(); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 174 | temp.addArc(bounds, radiansToDegrees(startAngle), sweep); |
| 175 | this.addPath(temp, true); |
| 176 | temp.delete(); |
| 177 | return this; |
| 178 | }; |
| 179 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 180 | // Appends arc to Path. Arc added is part of ellipse |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 181 | // bounded by oval, from startAngle through sweepAngle. Both startAngle and |
| 182 | // sweepAngle are measured in degrees, where zero degrees is aligned with the |
| 183 | // positive x-axis, and positive sweeps extends arc clockwise. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 184 | CanvasKit.Path.prototype.arcToOval = function(oval, startAngle, sweepAngle, forceMoveTo) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 185 | var oPtr = copyRectToWasm(oval); |
| 186 | this._arcToOval(oPtr, startAngle, sweepAngle, forceMoveTo); |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 187 | return this; |
| 188 | }; |
| 189 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 190 | // Appends arc to Path. Arc is implemented by one or more conics weighted to |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 191 | // describe part of oval with radii (rx, ry) rotated by xAxisRotate degrees. Arc |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 192 | // curves from last point to (x, y), choosing one of four possible routes: |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 193 | // clockwise or counterclockwise, and smaller or larger. |
| 194 | |
| 195 | // Arc sweep is always less than 360 degrees. arcTo() appends line to (x, y) if |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 196 | // either radii are zero, or if last point equals (x, y). arcTo() scales radii |
| 197 | // (rx, ry) to fit last point and (x, y) if both are greater than zero but |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 198 | // too small. |
| 199 | |
| 200 | // arcToRotated() appends up to four conic curves. |
| 201 | // arcToRotated() implements the functionality of SVG arc, although SVG sweep-flag value |
| 202 | // is opposite the integer value of sweep; SVG sweep-flag uses 1 for clockwise, |
| 203 | // while kCW_Direction cast to int is zero. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 204 | CanvasKit.Path.prototype.arcToRotated = function(rx, ry, xAxisRotate, useSmallArc, isCCW, x, y) { |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 205 | this._arcToRotated(rx, ry, xAxisRotate, !!useSmallArc, !!isCCW, x, y); |
| 206 | return this; |
| 207 | }; |
| 208 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 209 | // Appends arc to Path, after appending line if needed. Arc is implemented by conic |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 210 | // weighted to describe part of circle. Arc is contained by tangent from |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 211 | // last Path point to (x1, y1), and tangent from (x1, y1) to (x2, y2). Arc |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 212 | // is part of circle sized to radius, positioned so it touches both tangent lines. |
| 213 | |
| 214 | // If last Path Point does not start Arc, arcTo appends connecting Line to Path. |
| 215 | // The length of Vector from (x1, y1) to (x2, y2) does not affect Arc. |
| 216 | |
| 217 | // Arc sweep is always less than 180 degrees. If radius is zero, or if |
| 218 | // tangents are nearly parallel, arcTo appends Line from last Path Point to (x1, y1). |
| 219 | |
| 220 | // arcToTangent appends at most one Line and one conic. |
| 221 | // arcToTangent implements the functionality of PostScript arct and HTML Canvas arcTo. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 222 | CanvasKit.Path.prototype.arcToTangent = function(x1, y1, x2, y2, radius) { |
Nathaniel Nifong | d0c9d0c | 2020-07-15 16:46:17 -0400 | [diff] [blame] | 223 | this._arcToTangent(x1, y1, x2, y2, radius); |
| 224 | return this; |
| 225 | }; |
| 226 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 227 | CanvasKit.Path.prototype.close = function() { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 228 | this._close(); |
| 229 | return this; |
| 230 | }; |
| 231 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 232 | CanvasKit.Path.prototype.conicTo = function(x1, y1, x2, y2, w) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 233 | this._conicTo(x1, y1, x2, y2, w); |
| 234 | return this; |
| 235 | }; |
| 236 | |
Kevin Lubick | 7d96c5c | 2020-10-01 10:55:16 -0400 | [diff] [blame] | 237 | // Clients can pass in a Float32Array with length 4 to this and the results |
| 238 | // will be copied into that array. Otherwise, a new TypedArray will be allocated |
| 239 | // and returned. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 240 | CanvasKit.Path.prototype.computeTightBounds = function(optionalOutputArray) { |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 241 | this._computeTightBounds(_scratchFourFloatsAPtr); |
| 242 | var ta = _scratchFourFloatsA['toTypedArray'](); |
Kevin Lubick | 7d96c5c | 2020-10-01 10:55:16 -0400 | [diff] [blame] | 243 | if (optionalOutputArray) { |
| 244 | optionalOutputArray.set(ta); |
| 245 | return optionalOutputArray; |
| 246 | } |
| 247 | return ta.slice(); |
| 248 | }; |
| 249 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 250 | CanvasKit.Path.prototype.cubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 251 | this._cubicTo(cp1x, cp1y, cp2x, cp2y, x, y); |
| 252 | return this; |
| 253 | }; |
| 254 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 255 | CanvasKit.Path.prototype.dash = function(on, off, phase) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 256 | if (this._dash(on, off, phase)) { |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 257 | return this; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 258 | } |
| 259 | return null; |
| 260 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 261 | |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 262 | // Clients can pass in a Float32Array with length 4 to this and the results |
| 263 | // will be copied into that array. Otherwise, a new TypedArray will be allocated |
| 264 | // and returned. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 265 | CanvasKit.Path.prototype.getBounds = function(optionalOutputArray) { |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 266 | this._getBounds(_scratchFourFloatsAPtr); |
| 267 | var ta = _scratchFourFloatsA['toTypedArray'](); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 268 | if (optionalOutputArray) { |
| 269 | optionalOutputArray.set(ta); |
| 270 | return optionalOutputArray; |
| 271 | } |
| 272 | return ta.slice(); |
| 273 | }; |
| 274 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 275 | CanvasKit.Path.prototype.lineTo = function(x, y) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 276 | this._lineTo(x, y); |
| 277 | return this; |
| 278 | }; |
Kevin Lubick | 217056c | 2018-09-20 17:39:31 -0400 | [diff] [blame] | 279 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 280 | CanvasKit.Path.prototype.moveTo = function(x, y) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 281 | this._moveTo(x, y); |
| 282 | return this; |
| 283 | }; |
Kevin Lubick | b5ae3b5 | 2018-11-03 07:51:19 -0400 | [diff] [blame] | 284 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 285 | CanvasKit.Path.prototype.offset = function(dx, dy) { |
Kevin Lubick | e384df4 | 2019-08-26 15:48:09 -0400 | [diff] [blame] | 286 | this._transform(1, 0, dx, |
| 287 | 0, 1, dy, |
| 288 | 0, 0, 1); |
| 289 | return this; |
| 290 | }; |
| 291 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 292 | CanvasKit.Path.prototype.quadTo = function(cpx, cpy, x, y) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 293 | this._quadTo(cpx, cpy, x, y); |
| 294 | return this; |
| 295 | }; |
| 296 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 297 | CanvasKit.Path.prototype.rArcTo = function(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy) { |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 298 | this._rArcTo(rx, ry, xAxisRotate, useSmallArc, isCCW, dx, dy); |
| 299 | return this; |
| 300 | }; |
| 301 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 302 | CanvasKit.Path.prototype.rConicTo = function(dx1, dy1, dx2, dy2, w) { |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 303 | this._rConicTo(dx1, dy1, dx2, dy2, w); |
| 304 | return this; |
| 305 | }; |
| 306 | |
| 307 | // These params are all relative |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 308 | CanvasKit.Path.prototype.rCubicTo = function(cp1x, cp1y, cp2x, cp2y, x, y) { |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 309 | this._rCubicTo(cp1x, cp1y, cp2x, cp2y, x, y); |
| 310 | return this; |
| 311 | }; |
| 312 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 313 | CanvasKit.Path.prototype.rLineTo = function(dx, dy) { |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 314 | this._rLineTo(dx, dy); |
| 315 | return this; |
| 316 | }; |
| 317 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 318 | CanvasKit.Path.prototype.rMoveTo = function(dx, dy) { |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 319 | this._rMoveTo(dx, dy); |
| 320 | return this; |
| 321 | }; |
| 322 | |
| 323 | // These params are all relative |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 324 | CanvasKit.Path.prototype.rQuadTo = function(cpx, cpy, x, y) { |
Kevin Lubick | 79b7134 | 2019-11-01 14:36:52 -0400 | [diff] [blame] | 325 | this._rQuadTo(cpx, cpy, x, y); |
| 326 | return this; |
| 327 | }; |
| 328 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 329 | CanvasKit.Path.prototype.stroke = function(opts) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 330 | // Fill out any missing values with the default values. |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 331 | opts = opts || {}; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 332 | opts['width'] = opts['width'] || 1; |
| 333 | opts['miter_limit'] = opts['miter_limit'] || 4; |
| 334 | opts['cap'] = opts['cap'] || CanvasKit.StrokeCap.Butt; |
| 335 | opts['join'] = opts['join'] || CanvasKit.StrokeJoin.Miter; |
| 336 | opts['precision'] = opts['precision'] || 1; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 337 | if (this._stroke(opts)) { |
| 338 | return this; |
| 339 | } |
| 340 | return null; |
| 341 | }; |
| 342 | |
Kevin Lubick | a2535af | 2020-10-02 08:01:57 -0400 | [diff] [blame] | 343 | // TODO(kjlubick) Change this to take a 3x3 or 4x4 matrix (optionally malloc'd) |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 344 | CanvasKit.Path.prototype.transform = function() { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 345 | // Takes 1 or 9 args |
| 346 | if (arguments.length === 1) { |
| 347 | // argument 1 should be a 6 or 9 element array. |
| 348 | var a = arguments[0]; |
| 349 | this._transform(a[0], a[1], a[2], |
| 350 | a[3], a[4], a[5], |
| 351 | a[6] || 0, a[7] || 0, a[8] || 1); |
| 352 | } else if (arguments.length === 6 || arguments.length === 9) { |
| 353 | // these arguments are the 6 or 9 members of the matrix |
| 354 | var a = arguments; |
| 355 | this._transform(a[0], a[1], a[2], |
| 356 | a[3], a[4], a[5], |
| 357 | a[6] || 0, a[7] || 0, a[8] || 1); |
| 358 | } else { |
| 359 | throw 'transform expected to take 1 or 9 arguments. Got ' + arguments.length; |
| 360 | } |
| 361 | return this; |
| 362 | }; |
| 363 | // isComplement is optional, defaults to false |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 364 | CanvasKit.Path.prototype.trim = function(startT, stopT, isComplement) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 365 | if (this._trim(startT, stopT, !!isComplement)) { |
| 366 | return this; |
| 367 | } |
| 368 | return null; |
| 369 | }; |
| 370 | |
Kevin Lubick | 652d790 | 2020-12-11 14:51:36 -0500 | [diff] [blame] | 371 | // makeShaderCubic returns a shader for a given image, allowing it to be used on |
| 372 | // a paint as well as other purposes. This shader will be higher quality than |
| 373 | // other shader functions. See CubicResampler in SkSamplingOptions.h for more information |
| 374 | // on the cubicResampler params. |
| 375 | CanvasKit.Image.prototype.makeShaderCubic = function(xTileMode, yTileMode, |
| 376 | cubicResamplerB, cubicResamplerC, |
| 377 | localMatrix) { |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 378 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Kevin Lubick | 652d790 | 2020-12-11 14:51:36 -0500 | [diff] [blame] | 379 | return this._makeShaderCubic(xTileMode, yTileMode, cubicResamplerB, |
| 380 | cubicResamplerC, localMatrixPtr); |
| 381 | }; |
| 382 | |
| 383 | // makeShaderCubic returns a shader for a given image, allowing it to be used on |
| 384 | // a paint as well as other purposes. This shader will draw more quickly than |
| 385 | // other shader functions, but at a lower quality. |
| 386 | CanvasKit.Image.prototype.makeShaderOptions = function(xTileMode, yTileMode, |
| 387 | filterMode, mipmapMode, |
| 388 | localMatrix) { |
| 389 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
| 390 | return this._makeShaderOptions(xTileMode, yTileMode, filterMode, mipmapMode, localMatrixPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 391 | }; |
Kevin Lubick | a064c28 | 2019-04-04 09:28:53 -0400 | [diff] [blame] | 392 | |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 393 | function readPixels(source, srcX, srcY, imageInfo, destMallocObj, bytesPerRow) { |
| 394 | if (!bytesPerRow) { |
| 395 | bytesPerRow = 4 * imageInfo['width']; |
| 396 | if (imageInfo['colorType'] === CanvasKit.ColorType.RGBA_F16) { |
| 397 | bytesPerRow *= 2; |
| 398 | } |
| 399 | else if (imageInfo['colorType'] === CanvasKit.ColorType.RGBA_F32) { |
| 400 | bytesPerRow *= 4; |
| 401 | } |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 402 | } |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 403 | var pBytes = bytesPerRow * imageInfo.height; |
Kevin Lubick | c4ab087 | 2020-11-03 17:13:09 -0500 | [diff] [blame] | 404 | var pPtr; |
| 405 | if (destMallocObj) { |
| 406 | pPtr = destMallocObj['byteOffset']; |
| 407 | } else { |
| 408 | pPtr = CanvasKit._malloc(pBytes); |
| 409 | } |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 410 | |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 411 | if (!source._readPixels(imageInfo, pPtr, bytesPerRow, srcX, srcY)) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 412 | Debug('Could not read pixels with the given inputs'); |
Kevin Lubick | c4ab087 | 2020-11-03 17:13:09 -0500 | [diff] [blame] | 413 | if (!destMallocObj) { |
| 414 | CanvasKit._free(pPtr); |
| 415 | } |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 416 | return null; |
| 417 | } |
| 418 | |
Kevin Lubick | c4ab087 | 2020-11-03 17:13:09 -0500 | [diff] [blame] | 419 | // If the user provided us a buffer to copy into, we don't need to allocate a new TypedArray. |
| 420 | if (destMallocObj) { |
| 421 | return destMallocObj['toTypedArray'](); // Return the typed array wrapper w/o allocating. |
| 422 | } |
| 423 | |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 424 | // Put those pixels into a typed array of the right format and then |
| 425 | // make a copy with slice() that we can return. |
| 426 | var retVal = null; |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 427 | switch (imageInfo['colorType']) { |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 428 | case CanvasKit.ColorType.RGBA_8888: |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 429 | case CanvasKit.ColorType.RGBA_F16: // there is no half-float JS type, so we return raw bytes. |
Bryce Thomas | 1fa5404 | 2020-01-14 13:46:30 -0800 | [diff] [blame] | 430 | retVal = new Uint8Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice(); |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 431 | break; |
| 432 | case CanvasKit.ColorType.RGBA_F32: |
Bryce Thomas | 1fa5404 | 2020-01-14 13:46:30 -0800 | [diff] [blame] | 433 | retVal = new Float32Array(CanvasKit.HEAPU8.buffer, pPtr, pBytes).slice(); |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 434 | break; |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 435 | default: |
| 436 | Debug('ColorType not yet supported'); |
| 437 | return null; |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | // Free the allocated pixels in the WASM memory |
| 441 | CanvasKit._free(pPtr); |
| 442 | return retVal; |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | CanvasKit.Image.prototype.readPixels = function(srcX, srcY, imageInfo, destMallocObj, |
| 446 | bytesPerRow) { |
| 447 | return readPixels(this, srcX, srcY, imageInfo, destMallocObj, bytesPerRow); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 448 | }; |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 449 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 450 | // Accepts an array of four numbers in the range of 0-1 representing a 4f color |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 451 | CanvasKit.Canvas.prototype.clear = function(color4f) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 452 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 453 | var cPtr = copyColorToWasm(color4f); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 454 | this._clear(cPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 455 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 456 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 457 | CanvasKit.Canvas.prototype.clipRRect = function(rrect, op, antialias) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 458 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 459 | var rPtr = copyRRectToWasm(rrect); |
| 460 | this._clipRRect(rPtr, op, antialias); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 461 | }; |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 462 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 463 | CanvasKit.Canvas.prototype.clipRect = function(rect, op, antialias) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 464 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 465 | var rPtr = copyRectToWasm(rect); |
| 466 | this._clipRect(rPtr, op, antialias); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 467 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 468 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 469 | // concat takes a 3x2, a 3x3, or a 4x4 matrix and upscales it (if needed) to 4x4. This is because |
| 470 | // under the hood, SkCanvas uses a 4x4 matrix. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 471 | CanvasKit.Canvas.prototype.concat = function(matr) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 472 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 473 | var matrPtr = copy4x4MatrixToWasm(matr); |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 474 | this._concat(matrPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 475 | }; |
Kevin Lubick | d6b32ed | 2019-05-06 13:04:03 -0400 | [diff] [blame] | 476 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 477 | CanvasKit.Canvas.prototype.drawArc = function(oval, startAngle, sweepAngle, useCenter, paint) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 478 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 479 | var oPtr = copyRectToWasm(oval); |
| 480 | this._drawArc(oPtr, startAngle, sweepAngle, useCenter, paint); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 481 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 482 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 483 | // atlas is an Image, e.g. from CanvasKit.MakeImageFromEncoded |
Kevin Lubick | 78297af | 2021-06-02 10:36:06 -0400 | [diff] [blame] | 484 | // srcRects, dstXformsshould be arrays of floats of length 4*number of destinations. |
| 485 | // The colors param is optional and is used to tint the drawn images using the optional blend |
| 486 | // mode. Colors can be a Uint32Array of int colors or a flat Float32Array of float colors. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 487 | CanvasKit.Canvas.prototype.drawAtlas = function(atlas, srcRects, dstXforms, paint, |
Mike Reed | 5514431 | 2021-03-25 13:13:23 -0400 | [diff] [blame] | 488 | /* optional */ blendMode, /* optional */ colors, |
| 489 | /* optional */ sampling) { |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 490 | if (!atlas || !paint || !srcRects || !dstXforms) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 491 | Debug('Doing nothing since missing a required input'); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 492 | return; |
| 493 | } |
Nathaniel Nifong | cd75b17 | 2020-06-05 11:17:43 -0400 | [diff] [blame] | 494 | |
| 495 | // builder arguments report the length as the number of rects, but when passed as arrays |
| 496 | // their.length attribute is 4x higher because it's the number of total components of all rects. |
| 497 | // colors is always going to report the same length, at least until floats colors are supported |
| 498 | // by this function. |
| 499 | if (srcRects.length !== dstXforms.length) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 500 | Debug('Doing nothing since input arrays length mismatches'); |
Nathaniel Nifong | e5d3254 | 2020-03-26 09:27:48 -0400 | [diff] [blame] | 501 | return; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 502 | } |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 503 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 504 | if (!blendMode) { |
| 505 | blendMode = CanvasKit.BlendMode.SrcOver; |
| 506 | } |
| 507 | |
Kevin Lubick | 78297af | 2021-06-02 10:36:06 -0400 | [diff] [blame] | 508 | var srcRectPtr = copy1dArray(srcRects, 'HEAPF32'); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 509 | |
Kevin Lubick | 78297af | 2021-06-02 10:36:06 -0400 | [diff] [blame] | 510 | var dstXformPtr = copy1dArray(dstXforms, 'HEAPF32'); |
| 511 | var count = dstXforms.length / 4; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 512 | |
Kevin Lubick | 78297af | 2021-06-02 10:36:06 -0400 | [diff] [blame] | 513 | var colorPtr = copy1dArray(assureIntColors(colors), 'HEAPU32'); |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 514 | |
Mike Reed | 5514431 | 2021-03-25 13:13:23 -0400 | [diff] [blame] | 515 | // We require one of these: |
| 516 | // 1. sampling is null (we default to linear/none) |
| 517 | // 2. sampling.B and sampling.C --> CubicResampler |
| 518 | // 3. sampling.filter [and sampling.mipmap] --> FilterOptions |
| 519 | // |
| 520 | // Thus if all fields are available, we will choose cubic (since we search for B,C first) |
| 521 | |
| 522 | if (sampling && ('B' in sampling) && ('C' in sampling)) { |
| 523 | this._drawAtlasCubic(atlas, dstXformPtr, srcRectPtr, colorPtr, count, blendMode, |
| 524 | sampling['B'], sampling['C'], paint); |
| 525 | } else { |
| 526 | let filter = CanvasKit.FilterMode.Linear; |
| 527 | let mipmap = CanvasKit.MipmapMode.None; |
| 528 | if (sampling) { |
| 529 | filter = sampling['filter']; // 'filter' is a required field |
| 530 | if ('mipmap' in sampling) { // 'mipmap' is optional |
| 531 | mipmap = sampling['mipmap']; |
| 532 | } |
| 533 | } |
| 534 | this._drawAtlasOptions(atlas, dstXformPtr, srcRectPtr, colorPtr, count, blendMode, |
| 535 | filter, mipmap, paint); |
| 536 | } |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 537 | |
Kevin Lubick | 78297af | 2021-06-02 10:36:06 -0400 | [diff] [blame] | 538 | freeArraysThatAreNotMallocedByUsers(srcRectPtr, srcRects); |
| 539 | freeArraysThatAreNotMallocedByUsers(dstXformPtr, dstXforms); |
| 540 | freeArraysThatAreNotMallocedByUsers(colorPtr, colors); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 541 | }; |
Kevin Lubick | ee91c07 | 2019-03-29 10:39:52 -0400 | [diff] [blame] | 542 | |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 543 | CanvasKit.Canvas.prototype.drawCircle = function(cx, cy, r, paint) { |
| 544 | CanvasKit.setCurrentContext(this._context); |
| 545 | this._drawCircle(cx, cy, r, paint); |
| 546 | } |
| 547 | |
| 548 | CanvasKit.Canvas.prototype.drawColor = function(color4f, mode) { |
| 549 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 550 | var cPtr = copyColorToWasm(color4f); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 551 | if (mode !== undefined) { |
| 552 | this._drawColor(cPtr, mode); |
| 553 | } else { |
| 554 | this._drawColor(cPtr); |
| 555 | } |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 556 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 557 | |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 558 | CanvasKit.Canvas.prototype.drawColorInt = function(color, mode) { |
| 559 | CanvasKit.setCurrentContext(this._context); |
| 560 | this._drawColorInt(color, mode || CanvasKit.BlendMode.SrcOver); |
| 561 | } |
| 562 | |
| 563 | CanvasKit.Canvas.prototype.drawColorComponents = function(r, g, b, a, mode) { |
| 564 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 565 | var cPtr = copyColorComponentsToWasm(r, g, b, a); |
| 566 | if (mode !== undefined) { |
| 567 | this._drawColor(cPtr, mode); |
| 568 | } else { |
| 569 | this._drawColor(cPtr); |
| 570 | } |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 571 | }; |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 572 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 573 | CanvasKit.Canvas.prototype.drawDRRect = function(outer, inner, paint) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 574 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 575 | var oPtr = copyRRectToWasm(outer, _scratchRRectPtr); |
| 576 | var iPtr = copyRRectToWasm(inner, _scratchRRect2Ptr); |
| 577 | this._drawDRRect(oPtr, iPtr, paint); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 578 | }; |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 579 | |
Mike Reed | 9983b1e | 2021-04-14 14:49:35 -0400 | [diff] [blame] | 580 | CanvasKit.Canvas.prototype.drawGlyphs = function(glyphs, positions, x, y, font, paint) { |
Mike Reed | ae33954 | 2021-04-27 17:46:41 -0400 | [diff] [blame] | 581 | if (!(glyphs.length*2 <= positions.length)) { |
| 582 | throw 'Not enough positions for the array of gyphs'; |
Mike Reed | 9983b1e | 2021-04-14 14:49:35 -0400 | [diff] [blame] | 583 | } |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 584 | CanvasKit.setCurrentContext(this._context); |
Mike Reed | 9983b1e | 2021-04-14 14:49:35 -0400 | [diff] [blame] | 585 | const glyphs_ptr = copy1dArray(glyphs, 'HEAPU16'); |
| 586 | const positions_ptr = copy1dArray(positions, 'HEAPF32'); |
| 587 | |
| 588 | this._drawGlyphs(glyphs.length, glyphs_ptr, positions_ptr, x, y, font, paint); |
| 589 | |
| 590 | freeArraysThatAreNotMallocedByUsers(positions_ptr, positions); |
| 591 | freeArraysThatAreNotMallocedByUsers(glyphs_ptr, glyphs); |
| 592 | }; |
| 593 | |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 594 | CanvasKit.Canvas.prototype.drawImage = function(img, x, y, paint) { |
| 595 | CanvasKit.setCurrentContext(this._context); |
| 596 | this._drawImage(img, x, y, paint || null); |
| 597 | }; |
| 598 | |
| 599 | CanvasKit.Canvas.prototype.drawImageCubic = function(img, x, y, b, c, paint) { |
| 600 | CanvasKit.setCurrentContext(this._context); |
| 601 | this._drawImageCubic(img, x, y, b, c, paint || null); |
| 602 | }; |
| 603 | |
| 604 | CanvasKit.Canvas.prototype.drawImageOptions = function(img, x, y, filter, mipmap, paint) { |
| 605 | CanvasKit.setCurrentContext(this._context); |
| 606 | this._drawImageOptions(img, x, y, filter, mipmap, paint || null); |
| 607 | }; |
| 608 | |
Kevin Lubick | 2d63349 | 2020-12-17 09:58:32 -0500 | [diff] [blame] | 609 | CanvasKit.Canvas.prototype.drawImageNine = function(img, center, dest, filter, paint) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 610 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 611 | var cPtr = copyIRectToWasm(center); |
| 612 | var dPtr = copyRectToWasm(dest); |
Kevin Lubick | 2d63349 | 2020-12-17 09:58:32 -0500 | [diff] [blame] | 613 | this._drawImageNine(img, cPtr, dPtr, filter, paint || null); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 614 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 615 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 616 | CanvasKit.Canvas.prototype.drawImageRect = function(img, src, dest, paint, fastSample) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 617 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 618 | copyRectToWasm(src, _scratchFourFloatsAPtr); |
| 619 | copyRectToWasm(dest, _scratchFourFloatsBPtr); |
| 620 | this._drawImageRect(img, _scratchFourFloatsAPtr, _scratchFourFloatsBPtr, paint, !!fastSample); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 621 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 622 | |
Kevin Lubick | 72f4076 | 2020-12-16 16:00:55 -0500 | [diff] [blame] | 623 | CanvasKit.Canvas.prototype.drawImageRectCubic = function(img, src, dest, B, C, paint) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 624 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 625 | copyRectToWasm(src, _scratchFourFloatsAPtr); |
| 626 | copyRectToWasm(dest, _scratchFourFloatsBPtr); |
| 627 | this._drawImageRectCubic(img, _scratchFourFloatsAPtr, _scratchFourFloatsBPtr, B, C, |
| 628 | paint || null); |
Kevin Lubick | 72f4076 | 2020-12-16 16:00:55 -0500 | [diff] [blame] | 629 | }; |
| 630 | |
| 631 | CanvasKit.Canvas.prototype.drawImageRectOptions = function(img, src, dest, filter, mipmap, paint) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 632 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 633 | copyRectToWasm(src, _scratchFourFloatsAPtr); |
| 634 | copyRectToWasm(dest, _scratchFourFloatsBPtr); |
| 635 | this._drawImageRectOptions(img, _scratchFourFloatsAPtr, _scratchFourFloatsBPtr, filter, mipmap, |
| 636 | paint || null); |
Kevin Lubick | 72f4076 | 2020-12-16 16:00:55 -0500 | [diff] [blame] | 637 | }; |
| 638 | |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 639 | CanvasKit.Canvas.prototype.drawLine = function(x1, y1, x2, y2, paint) { |
| 640 | CanvasKit.setCurrentContext(this._context); |
| 641 | this._drawLine(x1, y1, x2, y2, paint); |
| 642 | } |
| 643 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 644 | CanvasKit.Canvas.prototype.drawOval = function(oval, paint) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 645 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 646 | var oPtr = copyRectToWasm(oval); |
| 647 | this._drawOval(oPtr, paint); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 648 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 649 | |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 650 | CanvasKit.Canvas.prototype.drawPaint = function(paint) { |
| 651 | CanvasKit.setCurrentContext(this._context); |
| 652 | this._drawPaint(paint); |
| 653 | } |
| 654 | |
| 655 | CanvasKit.Canvas.prototype.drawParagraph = function(p, x, y) { |
| 656 | CanvasKit.setCurrentContext(this._context); |
| 657 | this._drawParagraph(p, x, y); |
| 658 | } |
| 659 | |
Mike Reed | 6e7d2b3 | 2021-03-29 16:38:01 -0400 | [diff] [blame] | 660 | CanvasKit.Canvas.prototype.drawPatch = function(cubics, colors, texs, mode, paint) { |
| 661 | if (cubics.length < 24) { |
Mike Reed | be834bf | 2021-04-14 14:51:59 -0400 | [diff] [blame] | 662 | throw 'Need 12 cubic points'; |
Mike Reed | 6e7d2b3 | 2021-03-29 16:38:01 -0400 | [diff] [blame] | 663 | } |
| 664 | if (colors && colors.length < 4) { |
Mike Reed | be834bf | 2021-04-14 14:51:59 -0400 | [diff] [blame] | 665 | throw 'Need 4 colors'; |
Mike Reed | 6e7d2b3 | 2021-03-29 16:38:01 -0400 | [diff] [blame] | 666 | } |
| 667 | if (texs && texs.length < 8) { |
Mike Reed | be834bf | 2021-04-14 14:51:59 -0400 | [diff] [blame] | 668 | throw 'Need 4 shader coordinates'; |
Mike Reed | 6e7d2b3 | 2021-03-29 16:38:01 -0400 | [diff] [blame] | 669 | } |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 670 | CanvasKit.setCurrentContext(this._context); |
Mike Reed | 6e7d2b3 | 2021-03-29 16:38:01 -0400 | [diff] [blame] | 671 | |
| 672 | const cubics_ptr = copy1dArray(cubics, 'HEAPF32'); |
| 673 | const colors_ptr = colors ? copy1dArray(assureIntColors(colors), 'HEAPU32') : nullptr; |
| 674 | const texs_ptr = texs ? copy1dArray(texs, 'HEAPF32') : nullptr; |
| 675 | if (!mode) { |
| 676 | mode = CanvasKit.BlendMode.Modulate; |
| 677 | } |
| 678 | |
| 679 | this._drawPatch(cubics_ptr, colors_ptr, texs_ptr, mode, paint); |
| 680 | |
| 681 | freeArraysThatAreNotMallocedByUsers(texs_ptr, texs); |
| 682 | freeArraysThatAreNotMallocedByUsers(colors_ptr, colors); |
| 683 | freeArraysThatAreNotMallocedByUsers(cubics_ptr, cubics); |
| 684 | }; |
| 685 | |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 686 | CanvasKit.Canvas.prototype.drawPath = function(path, paint) { |
| 687 | CanvasKit.setCurrentContext(this._context); |
| 688 | this._drawPath(path, paint); |
| 689 | } |
| 690 | |
| 691 | CanvasKit.Canvas.prototype.drawPicture = function(pic) { |
| 692 | CanvasKit.setCurrentContext(this._context); |
| 693 | this._drawPicture(pic); |
| 694 | } |
Mike Reed | 6e7d2b3 | 2021-03-29 16:38:01 -0400 | [diff] [blame] | 695 | |
Kevin Lubick | 658cb71 | 2020-12-01 14:55:02 -0500 | [diff] [blame] | 696 | // points is a 1d array of length 2n representing n points where the even indices |
| 697 | // will be treated as x coordinates and the odd indices will be treated as y coordinates. |
| 698 | // Like other APIs, this accepts a malloced type array or malloc obj. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 699 | CanvasKit.Canvas.prototype.drawPoints = function(mode, points, paint) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 700 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | 658cb71 | 2020-12-01 14:55:02 -0500 | [diff] [blame] | 701 | var ptr = copy1dArray(points, 'HEAPF32'); |
| 702 | this._drawPoints(mode, ptr, points.length / 2, paint); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 703 | freeArraysThatAreNotMallocedByUsers(ptr, points); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 704 | }; |
Kevin Lubick | 37ab53e | 2019-11-11 10:06:08 -0500 | [diff] [blame] | 705 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 706 | CanvasKit.Canvas.prototype.drawRRect = function(rrect, paint) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 707 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 708 | var rPtr = copyRRectToWasm(rrect); |
| 709 | this._drawRRect(rPtr, paint); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 710 | }; |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 711 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 712 | CanvasKit.Canvas.prototype.drawRect = function(rect, paint) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 713 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 714 | var rPtr = copyRectToWasm(rect); |
| 715 | this._drawRect(rPtr, paint); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 716 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 717 | |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 718 | CanvasKit.Canvas.prototype.drawRect4f = function(l, t, r, b, paint) { |
| 719 | CanvasKit.setCurrentContext(this._context); |
| 720 | this._drawRect4f(l, t, r, b, paint); |
| 721 | } |
| 722 | |
Kevin Lubick | cbaea29 | 2021-01-13 14:16:58 -0500 | [diff] [blame] | 723 | CanvasKit.Canvas.prototype.drawShadow = function(path, zPlaneParams, lightPos, lightRadius, |
| 724 | ambientColor, spotColor, flags) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 725 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 726 | var ambiPtr = copyColorToWasmNoScratch(ambientColor); |
| 727 | var spotPtr = copyColorToWasmNoScratch(spotColor); |
Kevin Lubick | f6040ef | 2021-02-02 08:26:48 -0500 | [diff] [blame] | 728 | // We use the return value from copy1dArray in case the passed in arrays are malloc'd. |
| 729 | var zPlanePtr = copy1dArray(zPlaneParams, 'HEAPF32', _scratchThreeFloatsAPtr); |
| 730 | var lightPosPtr = copy1dArray(lightPos, 'HEAPF32', _scratchThreeFloatsBPtr); |
| 731 | this._drawShadow(path, zPlanePtr, lightPosPtr, lightRadius, ambiPtr, spotPtr, flags); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 732 | freeArraysThatAreNotMallocedByUsers(ambiPtr, ambientColor); |
| 733 | freeArraysThatAreNotMallocedByUsers(spotPtr, spotColor); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 734 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 735 | |
Kevin Lubick | cbaea29 | 2021-01-13 14:16:58 -0500 | [diff] [blame] | 736 | CanvasKit.getShadowLocalBounds = function(ctm, path, zPlaneParams, lightPos, lightRadius, |
| 737 | flags, optOutputRect) { |
| 738 | var ctmPtr = copy3x3MatrixToWasm(ctm); |
Kevin Lubick | f6040ef | 2021-02-02 08:26:48 -0500 | [diff] [blame] | 739 | // We use the return value from copy1dArray in case the passed in arrays are malloc'd. |
| 740 | var zPlanePtr = copy1dArray(zPlaneParams, 'HEAPF32', _scratchThreeFloatsAPtr); |
| 741 | var lightPosPtr = copy1dArray(lightPos, 'HEAPF32', _scratchThreeFloatsBPtr); |
| 742 | var ok = this._getShadowLocalBounds(ctmPtr, path, zPlanePtr, lightPosPtr, lightRadius, |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 743 | flags, _scratchFourFloatsAPtr); |
Kevin Lubick | cbaea29 | 2021-01-13 14:16:58 -0500 | [diff] [blame] | 744 | if (!ok) { |
| 745 | return null; |
| 746 | } |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 747 | var ta = _scratchFourFloatsA['toTypedArray'](); |
Kevin Lubick | cbaea29 | 2021-01-13 14:16:58 -0500 | [diff] [blame] | 748 | if (optOutputRect) { |
| 749 | optOutputRect.set(ta); |
| 750 | return optOutputRect; |
| 751 | } |
| 752 | return ta.slice(); |
| 753 | }; |
| 754 | |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 755 | CanvasKit.Canvas.prototype.drawTextBlob = function(blob, x, y, paint) { |
| 756 | CanvasKit.setCurrentContext(this._context); |
| 757 | this._drawTextBlob(blob, x, y, paint); |
| 758 | } |
| 759 | |
| 760 | CanvasKit.Canvas.prototype.drawVertices = function(verts, mode, paint) { |
| 761 | CanvasKit.setCurrentContext(this._context); |
| 762 | this._drawVertices(verts, mode, paint); |
| 763 | } |
| 764 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 765 | // getLocalToDevice returns a 4x4 matrix. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 766 | CanvasKit.Canvas.prototype.getLocalToDevice = function() { |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 767 | // _getLocalToDevice will copy the values into the pointer. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 768 | this._getLocalToDevice(_scratch4x4MatrixPtr); |
| 769 | return copy4x4MatrixFromWasm(_scratch4x4MatrixPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 770 | }; |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 771 | |
Nathaniel Nifong | 00de91c | 2020-05-06 16:22:33 -0400 | [diff] [blame] | 772 | // findMarkedCTM returns a 4x4 matrix, or null if a matrix was not found at |
| 773 | // the provided marker. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 774 | CanvasKit.Canvas.prototype.findMarkedCTM = function(marker) { |
Nathaniel Nifong | 00de91c | 2020-05-06 16:22:33 -0400 | [diff] [blame] | 775 | // _getLocalToDevice will copy the values into the pointer. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 776 | var found = this._findMarkedCTM(marker, _scratch4x4MatrixPtr); |
Nathaniel Nifong | 00de91c | 2020-05-06 16:22:33 -0400 | [diff] [blame] | 777 | if (!found) { |
| 778 | return null; |
| 779 | } |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 780 | return copy4x4MatrixFromWasm(_scratch4x4MatrixPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 781 | }; |
Nathaniel Nifong | 00de91c | 2020-05-06 16:22:33 -0400 | [diff] [blame] | 782 | |
Kevin Lubick | c1d0898 | 2020-04-06 13:52:15 -0400 | [diff] [blame] | 783 | // getTotalMatrix returns the current matrix as a 3x3 matrix. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 784 | CanvasKit.Canvas.prototype.getTotalMatrix = function() { |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 785 | // _getTotalMatrix will copy the values into the pointer. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 786 | this._getTotalMatrix(_scratch3x3MatrixPtr); |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 787 | // read them out into an array. TODO(kjlubick): If we change Matrix to be |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 788 | // typedArrays, then we should return a typed array here too. |
| 789 | var rv = new Array(9); |
| 790 | for (var i = 0; i < 9; i++) { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 791 | rv[i] = CanvasKit.HEAPF32[_scratch3x3MatrixPtr/4 + i]; // divide by 4 to "cast" to float. |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 792 | } |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 793 | return rv; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 794 | }; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 795 | |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 796 | CanvasKit.Canvas.prototype.makeSurface = function(imageInfo) { |
| 797 | var s = this._makeSurface(imageInfo); |
| 798 | s._context = this._context; |
| 799 | return s; |
| 800 | }; |
| 801 | |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 802 | CanvasKit.Canvas.prototype.readPixels = function(srcX, srcY, imageInfo, destMallocObj, |
| 803 | bytesPerRow) { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 804 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 805 | return readPixels(this, srcX, srcY, imageInfo, destMallocObj, bytesPerRow); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 806 | }; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 807 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 808 | CanvasKit.Canvas.prototype.saveLayer = function(paint, boundsRect, backdrop, flags) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 809 | // bPtr will be 0 (nullptr) if boundsRect is undefined/null. |
| 810 | var bPtr = copyRectToWasm(boundsRect); |
| 811 | // These or clauses help emscripten, which does not deal with undefined well. |
| 812 | return this._saveLayer(paint || null, bPtr, backdrop || null, flags || 0); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 813 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 814 | |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 815 | // pixels should be a Uint8Array or a plain JS array. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 816 | CanvasKit.Canvas.prototype.writePixels = function(pixels, srcWidth, srcHeight, |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 817 | destX, destY, alphaType, colorType, colorSpace) { |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 818 | if (pixels.byteLength % (srcWidth * srcHeight)) { |
| 819 | throw 'pixels length must be a multiple of the srcWidth * srcHeight'; |
| 820 | } |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 821 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 822 | var bytesPerPixel = pixels.byteLength / (srcWidth * srcHeight); |
| 823 | // supply defaults (which are compatible with HTMLCanvas's putImageData) |
| 824 | alphaType = alphaType || CanvasKit.AlphaType.Unpremul; |
| 825 | colorType = colorType || CanvasKit.ColorType.RGBA_8888; |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 826 | colorSpace = colorSpace || CanvasKit.ColorSpace.SRGB; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 827 | var srcRowBytes = bytesPerPixel * srcWidth; |
| 828 | |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 829 | var pptr = copy1dArray(pixels, 'HEAPU8'); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 830 | var ok = this._writePixels({ |
| 831 | 'width': srcWidth, |
| 832 | 'height': srcHeight, |
| 833 | 'colorType': colorType, |
| 834 | 'alphaType': alphaType, |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 835 | 'colorSpace': colorSpace, |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 836 | }, pptr, srcRowBytes, destX, destY); |
| 837 | |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 838 | freeArraysThatAreNotMallocedByUsers(pptr, pixels); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 839 | return ok; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 840 | }; |
Kevin Lubick | 52b9f37 | 2018-12-04 13:57:36 -0500 | [diff] [blame] | 841 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 842 | CanvasKit.ColorFilter.MakeBlend = function(color4f, mode) { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 843 | var cPtr = copyColorToWasm(color4f); |
Kevin Lubick | 3364579 | 2021-01-29 08:37:41 -0500 | [diff] [blame] | 844 | return CanvasKit.ColorFilter._MakeBlend(cPtr, mode); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 845 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 846 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 847 | // colorMatrix is an ColorMatrix (e.g. Float32Array of length 20) |
| 848 | CanvasKit.ColorFilter.MakeMatrix = function(colorMatrix) { |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 849 | if (!colorMatrix || colorMatrix.length !== 20) { |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 850 | throw 'invalid color matrix'; |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 851 | } |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 852 | var fptr = copy1dArray(colorMatrix, 'HEAPF32'); |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 853 | // We know skia memcopies the floats, so we can free our memory after the call returns. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 854 | var m = CanvasKit.ColorFilter._makeMatrix(fptr); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 855 | freeArraysThatAreNotMallocedByUsers(fptr, colorMatrix); |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 856 | return m; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 857 | }; |
Kevin Lubick | d372934 | 2019-09-12 11:11:25 -0400 | [diff] [blame] | 858 | |
Kevin Lubick | e9e8e5d | 2021-02-02 10:21:24 -0500 | [diff] [blame] | 859 | CanvasKit.ContourMeasure.prototype.getPosTan = function(distance, optionalOutput) { |
| 860 | this._getPosTan(distance, _scratchFourFloatsAPtr); |
| 861 | var ta = _scratchFourFloatsA['toTypedArray'](); |
| 862 | if (optionalOutput) { |
| 863 | optionalOutput.set(ta); |
| 864 | return optionalOutput; |
| 865 | } |
| 866 | return ta.slice(); |
| 867 | }; |
| 868 | |
Mike Reed | 5caab8e | 2021-07-20 09:52:27 -0400 | [diff] [blame] | 869 | CanvasKit.ImageFilter.MakeMatrixTransform = function(matrix, sampling, input) { |
| 870 | var matrPtr = copy3x3MatrixToWasm(matrix); |
| 871 | |
| 872 | if ('B' in sampling && 'C' in sampling) { |
| 873 | return CanvasKit.ImageFilter._MakeMatrixTransformCubic(matrPtr, |
| 874 | sampling.B, sampling.C, |
| 875 | input); |
| 876 | } else { |
| 877 | const filter = sampling['filter']; // 'filter' is a required field |
| 878 | let mipmap = CanvasKit.MipmapMode.None; |
| 879 | if ('mipmap' in sampling) { // 'mipmap' is optional |
| 880 | mipmap = sampling['mipmap']; |
| 881 | } |
| 882 | return CanvasKit.ImageFilter._MakeMatrixTransformOptions(matrPtr, |
| 883 | filter, mipmap, |
| 884 | input); |
| 885 | } |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 886 | }; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 887 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 888 | CanvasKit.Paint.prototype.getColor = function() { |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 889 | this._getColor(_scratchColorPtr); |
| 890 | return copyColorFromWasm(_scratchColorPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 891 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 892 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 893 | CanvasKit.Paint.prototype.setColor = function(color4f, colorSpace) { |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 894 | colorSpace = colorSpace || null; // null will be replaced with sRGB in the C++ method. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 895 | // emscripten wouldn't bind undefined to the sk_sp<ColorSpace> expected here. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 896 | var cPtr = copyColorToWasm(color4f); |
Nathaniel Nifong | b1ebbb1 | 2020-05-26 13:10:20 -0400 | [diff] [blame] | 897 | this._setColor(cPtr, colorSpace); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 898 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 899 | |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 900 | // The color components here are expected to be floating point values (nominally between |
| 901 | // 0.0 and 1.0, but with wider color gamuts, the values could exceed this range). To convert |
| 902 | // between standard 8 bit colors and floats, just divide by 255 before passing them in. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 903 | CanvasKit.Paint.prototype.setColorComponents = function(r, g, b, a, colorSpace) { |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 904 | colorSpace = colorSpace || null; // null will be replaced with sRGB in the C++ method. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 905 | // emscripten wouldn't bind undefined to the sk_sp<ColorSpace> expected here. |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 906 | var cPtr = copyColorComponentsToWasm(r, g, b, a); |
| 907 | this._setColor(cPtr, colorSpace); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 908 | }; |
Kevin Lubick | 93f1a38 | 2020-06-02 16:15:23 -0400 | [diff] [blame] | 909 | |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 910 | CanvasKit.Path.prototype.getPoint = function(idx, optionalOutput) { |
| 911 | // This will copy 2 floats into a space for 4 floats |
| 912 | this._getPoint(idx, _scratchFourFloatsAPtr); |
| 913 | var ta = _scratchFourFloatsA['toTypedArray'](); |
| 914 | if (optionalOutput) { |
| 915 | // We cannot call optionalOutput.set() because it is an error to call .set() with |
| 916 | // a source bigger than the destination. |
| 917 | optionalOutput[0] = ta[0]; |
| 918 | optionalOutput[1] = ta[1]; |
| 919 | return optionalOutput; |
| 920 | } |
| 921 | // Be sure to return a copy of just the first 2 values. |
| 922 | return ta.slice(0, 2); |
| 923 | }; |
| 924 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 925 | CanvasKit.PictureRecorder.prototype.beginRecording = function(bounds) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 926 | var bPtr = copyRectToWasm(bounds); |
| 927 | return this._beginRecording(bPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 928 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 929 | |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 930 | CanvasKit.Surface.prototype.getCanvas = function() { |
| 931 | var c = this._getCanvas(); |
| 932 | c._context = this._context; |
| 933 | return c; |
| 934 | }; |
| 935 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 936 | CanvasKit.Surface.prototype.makeImageSnapshot = function(optionalBoundsRect) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 937 | var bPtr = copyIRectToWasm(optionalBoundsRect); |
| 938 | return this._makeImageSnapshot(bPtr); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 939 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 940 | |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 941 | CanvasKit.Surface.prototype.makeSurface = function(imageInfo) { |
| 942 | var s = this._makeSurface(imageInfo); |
| 943 | s._context = this._context; |
| 944 | return s; |
| 945 | }; |
| 946 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 947 | CanvasKit.Surface.prototype.requestAnimationFrame = function(callback, dirtyRect) { |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 948 | if (!this._cached_canvas) { |
| 949 | this._cached_canvas = this.getCanvas(); |
| 950 | } |
Elliot Evans | 1ec3b1a | 2020-07-23 10:25:58 -0400 | [diff] [blame] | 951 | requestAnimationFrame(function() { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 952 | CanvasKit.setCurrentContext(this._context); |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 953 | |
| 954 | callback(this._cached_canvas); |
| 955 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 956 | // We do not dispose() of the Surface here, as the client will typically |
Bryce Thomas | 2c5b856 | 2020-01-22 13:49:41 -0800 | [diff] [blame] | 957 | // call requestAnimationFrame again from within the supplied callback. |
| 958 | // For drawing a single frame, prefer drawOnce(). |
Bryce Thomas | 9331ca0 | 2020-05-29 16:51:21 -0700 | [diff] [blame] | 959 | this.flush(dirtyRect); |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 960 | }.bind(this)); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 961 | }; |
Kevin Lubick | 359a7e3 | 2019-03-19 09:34:37 -0400 | [diff] [blame] | 962 | |
Kevin Lubick | 5237933 | 2020-01-27 10:01:25 -0500 | [diff] [blame] | 963 | // drawOnce will dispose of the surface after drawing the frame using the provided |
| 964 | // callback. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 965 | CanvasKit.Surface.prototype.drawOnce = function(callback, dirtyRect) { |
Bryce Thomas | 2c5b856 | 2020-01-22 13:49:41 -0800 | [diff] [blame] | 966 | if (!this._cached_canvas) { |
| 967 | this._cached_canvas = this.getCanvas(); |
| 968 | } |
Elliot Evans | 1ec3b1a | 2020-07-23 10:25:58 -0400 | [diff] [blame] | 969 | requestAnimationFrame(function() { |
Kevin Lubick | f611404 | 2021-08-25 13:13:09 -0400 | [diff] [blame] | 970 | CanvasKit.setCurrentContext(this._context); |
Bryce Thomas | 2c5b856 | 2020-01-22 13:49:41 -0800 | [diff] [blame] | 971 | callback(this._cached_canvas); |
| 972 | |
Bryce Thomas | 9331ca0 | 2020-05-29 16:51:21 -0700 | [diff] [blame] | 973 | this.flush(dirtyRect); |
Bryce Thomas | 2c5b856 | 2020-01-22 13:49:41 -0800 | [diff] [blame] | 974 | this.dispose(); |
| 975 | }.bind(this)); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 976 | }; |
Bryce Thomas | 2c5b856 | 2020-01-22 13:49:41 -0800 | [diff] [blame] | 977 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 978 | CanvasKit.PathEffect.MakeDash = function(intervals, phase) { |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 979 | if (!phase) { |
| 980 | phase = 0; |
| 981 | } |
| 982 | if (!intervals.length || intervals.length % 2 === 1) { |
| 983 | throw 'Intervals array must have even length'; |
| 984 | } |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 985 | var ptr = copy1dArray(intervals, 'HEAPF32'); |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 986 | var dpe = CanvasKit.PathEffect._MakeDash(ptr, intervals.length, phase); |
Kevin Lubick | cf11892 | 2020-05-28 14:43:38 -0400 | [diff] [blame] | 987 | freeArraysThatAreNotMallocedByUsers(ptr, intervals); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 988 | return dpe; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 989 | }; |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 990 | |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 991 | CanvasKit.Shader.MakeColor = function(color4f, colorSpace) { |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 992 | colorSpace = colorSpace || null; |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 993 | var cPtr = copyColorToWasm(color4f); |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 994 | return CanvasKit.Shader._MakeColor(cPtr, colorSpace); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 995 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 996 | |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 997 | // TODO(kjlubick) remove deprecated names. |
| 998 | CanvasKit.Shader.Blend = CanvasKit.Shader.MakeBlend; |
| 999 | CanvasKit.Shader.Color = CanvasKit.Shader.MakeColor; |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1000 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1001 | CanvasKit.Shader.MakeLinearGradient = function(start, end, colors, pos, mode, localMatrix, flags, colorSpace) { |
Kevin Lubick | b8123cc | 2020-11-06 13:05:37 -0500 | [diff] [blame] | 1002 | colorSpace = colorSpace || null; |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1003 | var cPtrInfo = copyFlexibleColorArray(colors); |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1004 | var posPtr = copy1dArray(pos, 'HEAPF32'); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1005 | flags = flags || 0; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1006 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1007 | |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 1008 | // Copy start and end to _scratchFourFloatsAPtr. |
| 1009 | var startEndPts = _scratchFourFloatsA['toTypedArray'](); |
| 1010 | startEndPts.set(start); |
| 1011 | startEndPts.set(end, 2); |
| 1012 | |
| 1013 | var lgs = CanvasKit.Shader._MakeLinearGradient(_scratchFourFloatsAPtr, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1014 | cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1015 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1016 | freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1017 | pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1018 | return lgs; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1019 | }; |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1020 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1021 | CanvasKit.Shader.MakeRadialGradient = function(center, radius, colors, pos, mode, localMatrix, flags, colorSpace) { |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 1022 | colorSpace = colorSpace || null; |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1023 | var cPtrInfo = copyFlexibleColorArray(colors); |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1024 | var posPtr = copy1dArray(pos, 'HEAPF32'); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1025 | flags = flags || 0; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1026 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1027 | |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 1028 | var rgs = CanvasKit.Shader._MakeRadialGradient(center[0], center[1], radius, cPtrInfo.colorPtr, |
| 1029 | cPtrInfo.colorType, posPtr, cPtrInfo.count, mode, |
| 1030 | flags, localMatrixPtr, colorSpace); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1031 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1032 | freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1033 | pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1034 | return rgs; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1035 | }; |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1036 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1037 | CanvasKit.Shader.MakeSweepGradient = function(cx, cy, colors, pos, mode, localMatrix, flags, startAngle, endAngle, colorSpace) { |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 1038 | colorSpace = colorSpace || null; |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1039 | var cPtrInfo = copyFlexibleColorArray(colors); |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1040 | var posPtr = copy1dArray(pos, 'HEAPF32'); |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1041 | flags = flags || 0; |
| 1042 | startAngle = startAngle || 0; |
| 1043 | endAngle = endAngle || 360; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1044 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1045 | |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1046 | var sgs = CanvasKit.Shader._MakeSweepGradient(cx, cy, cPtrInfo.colorPtr, cPtrInfo.colorType, posPtr, |
| 1047 | cPtrInfo.count, mode, |
| 1048 | startAngle, endAngle, flags, |
| 1049 | localMatrixPtr, colorSpace); |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1050 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1051 | freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1052 | pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1053 | return sgs; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1054 | }; |
Dan Field | 3d44f73 | 2020-03-16 09:17:30 -0700 | [diff] [blame] | 1055 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1056 | CanvasKit.Shader.MakeTwoPointConicalGradient = function(start, startRadius, end, endRadius, |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1057 | colors, pos, mode, localMatrix, flags, colorSpace) { |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 1058 | colorSpace = colorSpace || null; |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1059 | var cPtrInfo = copyFlexibleColorArray(colors); |
Kevin Lubick | 421ba88 | 2020-10-15 13:07:33 -0400 | [diff] [blame] | 1060 | var posPtr = copy1dArray(pos, 'HEAPF32'); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1061 | flags = flags || 0; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 1062 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1063 | |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 1064 | // Copy start and end to _scratchFourFloatsAPtr. |
| 1065 | var startEndPts = _scratchFourFloatsA['toTypedArray'](); |
| 1066 | startEndPts.set(start); |
| 1067 | startEndPts.set(end, 2); |
| 1068 | |
| 1069 | var rgs = CanvasKit.Shader._MakeTwoPointConicalGradient(_scratchFourFloatsAPtr, |
| 1070 | startRadius, endRadius, cPtrInfo.colorPtr, cPtrInfo.colorType, |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1071 | posPtr, cPtrInfo.count, mode, flags, localMatrixPtr, colorSpace); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1072 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1073 | freeArraysThatAreNotMallocedByUsers(cPtrInfo.colorPtr, colors); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1074 | pos && freeArraysThatAreNotMallocedByUsers(posPtr, pos); |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1075 | return rgs; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1076 | }; |
Nathaniel Nifong | 23b0ed9 | 2020-03-04 15:43:50 -0500 | [diff] [blame] | 1077 | |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1078 | // Clients can pass in a Float32Array with length 4 to this and the results |
| 1079 | // will be copied into that array. Otherwise, a new TypedArray will be allocated |
| 1080 | // and returned. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1081 | CanvasKit.Vertices.prototype.bounds = function(optionalOutputArray) { |
Kevin Lubick | ed96264 | 2021-02-02 08:18:11 -0500 | [diff] [blame] | 1082 | this._bounds(_scratchFourFloatsAPtr); |
| 1083 | var ta = _scratchFourFloatsA['toTypedArray'](); |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1084 | if (optionalOutputArray) { |
| 1085 | optionalOutputArray.set(ta); |
| 1086 | return optionalOutputArray; |
| 1087 | } |
| 1088 | return ta.slice(); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1089 | }; |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1090 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1091 | // Run through the JS files that are added at compile time. |
| 1092 | if (CanvasKit._extraInitializations) { |
| 1093 | CanvasKit._extraInitializations.forEach(function(init) { |
| 1094 | init(); |
| 1095 | }); |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 1096 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1097 | }; // end CanvasKit.onRuntimeInitialized, that is, anything changing prototypes or dynamic. |
Kevin Lubick | eb2f6b0 | 2018-11-29 15:07:02 -0500 | [diff] [blame] | 1098 | |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1099 | // Accepts an object holding two canvaskit colors. |
| 1100 | // { |
Kevin Lubick | 3d00e3a | 2020-10-02 14:59:28 -0400 | [diff] [blame] | 1101 | // ambient: [r, g, b, a], |
| 1102 | // spot: [r, g, b, a], |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1103 | // } |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1104 | // Returns the same format. Note, if malloced colors are passed in, the memory |
| 1105 | // housing the passed in colors passed in will be overwritten with the computed |
| 1106 | // tonal colors. |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1107 | CanvasKit.computeTonalColors = function(tonalColors) { |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1108 | // copy the colors into WASM |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 1109 | var cPtrAmbi = copyColorToWasmNoScratch(tonalColors['ambient']); |
| 1110 | var cPtrSpot = copyColorToWasmNoScratch(tonalColors['spot']); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1111 | // The output of this function will be the same pointers we passed in. |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1112 | this._computeTonalColors(cPtrAmbi, cPtrSpot); |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1113 | // Read the results out. |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1114 | var result = { |
| 1115 | 'ambient': copyColorFromWasm(cPtrAmbi), |
| 1116 | 'spot': copyColorFromWasm(cPtrSpot), |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1117 | }; |
Kevin Lubick | e7b329e | 2020-06-05 15:58:01 -0400 | [diff] [blame] | 1118 | // If the user passed us malloced colors in here, we don't want to clean them up. |
| 1119 | freeArraysThatAreNotMallocedByUsers(cPtrAmbi, tonalColors['ambient']); |
| 1120 | freeArraysThatAreNotMallocedByUsers(cPtrSpot, tonalColors['spot']); |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1121 | return result; |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1122 | }; |
Nathaniel Nifong | 1bedbeb | 2020-05-04 16:46:17 -0400 | [diff] [blame] | 1123 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1124 | CanvasKit.LTRBRect = function(l, t, r, b) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1125 | return Float32Array.of(l, t, r, b); |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1126 | }; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 1127 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1128 | CanvasKit.XYWHRect = function(x, y, w, h) { |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1129 | return Float32Array.of(x, y, x+w, y+h); |
| 1130 | }; |
| 1131 | |
| 1132 | CanvasKit.LTRBiRect = function(l, t, r, b) { |
| 1133 | return Int32Array.of(l, t, r, b); |
| 1134 | }; |
| 1135 | |
| 1136 | CanvasKit.XYWHiRect = function(x, y, w, h) { |
| 1137 | return Int32Array.of(x, y, x+w, y+h); |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1138 | }; |
Kevin Lubick | 1a05fce | 2018-11-20 12:51:16 -0500 | [diff] [blame] | 1139 | |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1140 | // RRectXY returns a TypedArray representing an RRect with the given rect and a radiusX and |
| 1141 | // radiusY for all 4 corners. |
Kevin Lubick | 7d644e1 | 2019-09-11 14:22:22 -0400 | [diff] [blame] | 1142 | CanvasKit.RRectXY = function(rect, rx, ry) { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1143 | return Float32Array.of( |
Kevin Lubick | f8823b5 | 2020-09-03 10:02:10 -0400 | [diff] [blame] | 1144 | rect[0], rect[1], rect[2], rect[3], |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1145 | rx, ry, |
| 1146 | rx, ry, |
| 1147 | rx, ry, |
| 1148 | rx, ry, |
| 1149 | ); |
Kevin Lubick | d9b9e5e | 2020-06-23 16:58:10 -0400 | [diff] [blame] | 1150 | }; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1151 | |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1152 | // data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer()) |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 1153 | CanvasKit.MakeAnimatedImageFromEncoded = function(data) { |
| 1154 | data = new Uint8Array(data); |
| 1155 | |
| 1156 | var iptr = CanvasKit._malloc(data.byteLength); |
| 1157 | CanvasKit.HEAPU8.set(data, iptr); |
| 1158 | var img = CanvasKit._decodeAnimatedImage(iptr, data.byteLength); |
| 1159 | if (!img) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1160 | Debug('Could not decode animated image'); |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 1161 | return null; |
| 1162 | } |
| 1163 | return img; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1164 | }; |
Kevin Lubick | 6b921b7 | 2019-09-18 16:18:17 -0400 | [diff] [blame] | 1165 | |
| 1166 | // data is a TypedArray or ArrayBuffer e.g. from fetch().then(resp.arrayBuffer()) |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1167 | CanvasKit.MakeImageFromEncoded = function(data) { |
| 1168 | data = new Uint8Array(data); |
| 1169 | |
| 1170 | var iptr = CanvasKit._malloc(data.byteLength); |
| 1171 | CanvasKit.HEAPU8.set(data, iptr); |
| 1172 | var img = CanvasKit._decodeImage(iptr, data.byteLength); |
| 1173 | if (!img) { |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1174 | Debug('Could not decode image'); |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1175 | return null; |
| 1176 | } |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1177 | return img; |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1178 | }; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1179 | |
Elliot Evans | 2879619 | 2020-06-15 12:53:27 -0600 | [diff] [blame] | 1180 | // A variable to hold a canvasElement which can be reused once created the first time. |
| 1181 | var memoizedCanvas2dElement = null; |
| 1182 | |
| 1183 | // Alternative to CanvasKit.MakeImageFromEncoded. Allows for CanvasKit users to take advantage of |
| 1184 | // browser APIs to decode images instead of using codecs included in the CanvasKit wasm binary. |
| 1185 | // Expects that the canvasImageSource has already loaded/decoded. |
| 1186 | // CanvasImageSource reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasImageSource |
| 1187 | CanvasKit.MakeImageFromCanvasImageSource = function(canvasImageSource) { |
| 1188 | var width = canvasImageSource.width; |
| 1189 | var height = canvasImageSource.height; |
| 1190 | |
| 1191 | if (!memoizedCanvas2dElement) { |
| 1192 | memoizedCanvas2dElement = document.createElement('canvas'); |
| 1193 | } |
| 1194 | memoizedCanvas2dElement.width = width; |
| 1195 | memoizedCanvas2dElement.height = height; |
| 1196 | |
| 1197 | var ctx2d = memoizedCanvas2dElement.getContext('2d'); |
| 1198 | ctx2d.drawImage(canvasImageSource, 0, 0); |
| 1199 | |
| 1200 | var imageData = ctx2d.getImageData(0, 0, width, height); |
| 1201 | |
Kevin Lubick | ae0d3ff | 2020-11-18 11:23:15 -0500 | [diff] [blame] | 1202 | return CanvasKit.MakeImage({ |
| 1203 | 'width': width, |
| 1204 | 'height': height, |
| 1205 | 'alphaType': CanvasKit.AlphaType.Unpremul, |
| 1206 | 'colorType': CanvasKit.ColorType.RGBA_8888, |
| 1207 | 'colorSpace': CanvasKit.ColorSpace.SRGB |
| 1208 | }, imageData.data, 4 * width); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1209 | }; |
Elliot Evans | 2879619 | 2020-06-15 12:53:27 -0600 | [diff] [blame] | 1210 | |
Kevin Lubick | ae0d3ff | 2020-11-18 11:23:15 -0500 | [diff] [blame] | 1211 | // pixels may be an array but Uint8Array or Uint8ClampedArray is recommended, |
| 1212 | // with the bytes representing the pixel values. |
Kevin Lubick | eda0b43 | 2019-12-02 08:26:48 -0500 | [diff] [blame] | 1213 | // (e.g. each set of 4 bytes could represent RGBA values for a single pixel). |
Kevin Lubick | ae0d3ff | 2020-11-18 11:23:15 -0500 | [diff] [blame] | 1214 | CanvasKit.MakeImage = function(info, pixels, bytesPerRow) { |
| 1215 | var pptr = CanvasKit._malloc(pixels.length); |
| 1216 | CanvasKit.HEAPU8.set(pixels, pptr); // We always want to copy the bytes into the WASM heap. |
Kevin Lubick | eda0b43 | 2019-12-02 08:26:48 -0500 | [diff] [blame] | 1217 | // No need to _free pptr, Image takes it with SkData::MakeFromMalloc |
Kevin Lubick | ae0d3ff | 2020-11-18 11:23:15 -0500 | [diff] [blame] | 1218 | return CanvasKit._MakeImage(info, pptr, pixels.length, bytesPerRow); |
Kevin Lubick | 9fe8391 | 2020-11-03 17:08:34 -0500 | [diff] [blame] | 1219 | }; |
Kevin Lubick | f5ea37f | 2019-02-28 10:06:18 -0500 | [diff] [blame] | 1220 | |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1221 | // Colors may be a Uint32Array of int colors, a Flat Float32Array of float colors |
| 1222 | // or a 2d Array of Float32Array(4) (deprecated) |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 1223 | // the underlying Skia function accepts only int colors so it is recommended |
Nathaniel Nifong | d05fd0c | 2020-06-11 08:44:20 -0400 | [diff] [blame] | 1224 | // to pass an array of int colors to avoid an extra conversion. |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 1225 | CanvasKit.MakeVertices = function(mode, positions, textureCoordinates, colors, |
| 1226 | indices, isVolatile) { |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 1227 | // Default isVolatile to true if not set |
Kevin Lubick | b3574c9 | 2019-03-06 08:25:36 -0500 | [diff] [blame] | 1228 | isVolatile = isVolatile === undefined ? true : isVolatile; |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1229 | var idxCount = (indices && indices.length) || 0; |
| 1230 | |
| 1231 | var flags = 0; |
| 1232 | // These flags are from SkVertices.h and should be kept in sync with those. |
| 1233 | if (textureCoordinates && textureCoordinates.length) { |
| 1234 | flags |= (1 << 0); |
| 1235 | } |
| 1236 | if (colors && colors.length) { |
| 1237 | flags |= (1 << 1); |
| 1238 | } |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1239 | if (!isVolatile) { |
Mike Reed | 5caf935 | 2020-03-02 14:57:09 -0500 | [diff] [blame] | 1240 | flags |= (1 << 2); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1241 | } |
| 1242 | |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 1243 | var builder = new CanvasKit._VerticesBuilder(mode, positions.length / 2, idxCount, flags); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1244 | |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 1245 | copy1dArray(positions, 'HEAPF32', builder.positions()); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1246 | if (builder.texCoords()) { |
Kevin Lubick | e7c1a73 | 2020-12-04 09:10:39 -0500 | [diff] [blame] | 1247 | copy1dArray(textureCoordinates, 'HEAPF32', builder.texCoords()); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1248 | } |
| 1249 | if (builder.colors()) { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1250 | copy1dArray(assureIntColors(colors), 'HEAPU32', builder.colors()); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1251 | } |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1252 | if (builder.indices()) { |
Kevin Lubick | be72801 | 2020-09-03 11:57:12 +0000 | [diff] [blame] | 1253 | copy1dArray(indices, 'HEAPU16', builder.indices()); |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1254 | } |
Kevin Lubick | b3574c9 | 2019-03-06 08:25:36 -0500 | [diff] [blame] | 1255 | |
Kevin Lubick | d6ba725 | 2019-06-03 14:38:05 -0400 | [diff] [blame] | 1256 | // Create the vertices, which owns the memory that the builder had allocated. |
| 1257 | return builder.detach(); |
Kevin Lubick | a4f218d | 2020-01-14 08:39:09 -0500 | [diff] [blame] | 1258 | }; |