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