dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkPatchUtils.h" |
| 9 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
| 11 | #include "SkGeometry.h" |
| 12 | |
| 13 | /** |
| 14 | * Evaluator to sample the values of a cubic bezier using forward differences. |
| 15 | * Forward differences is a method for evaluating a nth degree polynomial at a uniform step by only |
| 16 | * adding precalculated values. |
| 17 | * For a linear example we have the function f(t) = m*t+b, then the value of that function at t+h |
| 18 | * would be f(t+h) = m*(t+h)+b. If we want to know the uniform step that we must add to the first |
| 19 | * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t + b = mh. After |
| 20 | * obtaining this value (mh) we could just add this constant step to our first sampled point |
| 21 | * to compute the next one. |
| 22 | * |
| 23 | * For the cubic case the first difference gives as a result a quadratic polynomial to which we can |
| 24 | * apply again forward differences and get linear function to which we can apply again forward |
| 25 | * differences to get a constant difference. This is why we keep an array of size 4, the 0th |
| 26 | * position keeps the sampled value while the next ones keep the quadratic, linear and constant |
| 27 | * difference values. |
| 28 | */ |
| 29 | |
| 30 | class FwDCubicEvaluator { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 31 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 32 | public: |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 33 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Receives the 4 control points of the cubic bezier. |
| 36 | */ |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 37 | |
caryclark | 5ba2b96 | 2016-01-26 17:02:30 -0800 | [diff] [blame] | 38 | explicit FwDCubicEvaluator(const SkPoint points[4]) |
| 39 | : fCoefs(points) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 40 | memcpy(fPoints, points, 4 * sizeof(SkPoint)); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 41 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 42 | this->restart(1); |
| 43 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 44 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Restarts the forward differences evaluator to the first value of t = 0. |
| 47 | */ |
| 48 | void restart(int divisions) { |
| 49 | fDivisions = divisions; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 50 | fCurrent = 0; |
| 51 | fMax = fDivisions + 1; |
caryclark | 5ba2b96 | 2016-01-26 17:02:30 -0800 | [diff] [blame] | 52 | Sk2s h = Sk2s(1.f / fDivisions); |
| 53 | Sk2s h2 = h * h; |
| 54 | Sk2s h3 = h2 * h; |
| 55 | Sk2s fwDiff3 = Sk2s(6) * fCoefs.fA * h3; |
| 56 | fFwDiff[3] = to_point(fwDiff3); |
| 57 | fFwDiff[2] = to_point(fwDiff3 + times_2(fCoefs.fB) * h2); |
| 58 | fFwDiff[1] = to_point(fCoefs.fA * h3 + fCoefs.fB * h2 + fCoefs.fC * h); |
| 59 | fFwDiff[0] = to_point(fCoefs.fD); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 60 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 61 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Check if the evaluator is still within the range of 0<=t<=1 |
| 64 | */ |
| 65 | bool done() const { |
| 66 | return fCurrent > fMax; |
| 67 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 68 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Call next to obtain the SkPoint sampled and move to the next one. |
| 71 | */ |
| 72 | SkPoint next() { |
| 73 | SkPoint point = fFwDiff[0]; |
| 74 | fFwDiff[0] += fFwDiff[1]; |
| 75 | fFwDiff[1] += fFwDiff[2]; |
| 76 | fFwDiff[2] += fFwDiff[3]; |
| 77 | fCurrent++; |
| 78 | return point; |
| 79 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 80 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 81 | const SkPoint* getCtrlPoints() const { |
| 82 | return fPoints; |
| 83 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 84 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 85 | private: |
caryclark | 5ba2b96 | 2016-01-26 17:02:30 -0800 | [diff] [blame] | 86 | SkCubicCoeff fCoefs; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 87 | int fMax, fCurrent, fDivisions; |
caryclark | 5ba2b96 | 2016-01-26 17:02:30 -0800 | [diff] [blame] | 88 | SkPoint fFwDiff[4], fPoints[4]; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | //////////////////////////////////////////////////////////////////////////////// |
| 92 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 93 | // size in pixels of each partition per axis, adjust this knob |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 94 | static const int kPartitionSize = 10; |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Calculate the approximate arc length given a bezier curve's control points. |
| 98 | */ |
| 99 | static SkScalar approx_arc_length(SkPoint* points, int count) { |
| 100 | if (count < 2) { |
| 101 | return 0; |
| 102 | } |
| 103 | SkScalar arcLength = 0; |
| 104 | for (int i = 0; i < count - 1; i++) { |
| 105 | arcLength += SkPoint::Distance(points[i], points[i + 1]); |
| 106 | } |
| 107 | return arcLength; |
| 108 | } |
| 109 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 110 | static SkScalar bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01, |
| 111 | SkScalar c11) { |
| 112 | SkScalar a = c00 * (1.f - tx) + c10 * tx; |
| 113 | SkScalar b = c01 * (1.f - tx) + c11 * tx; |
| 114 | return a * (1.f - ty) + b * ty; |
| 115 | } |
| 116 | |
| 117 | SkISize SkPatchUtils::GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix* matrix) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 118 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 119 | // Approximate length of each cubic. |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 120 | SkPoint pts[kNumPtsCubic]; |
| 121 | SkPatchUtils::getTopCubic(cubics, pts); |
| 122 | matrix->mapPoints(pts, kNumPtsCubic); |
| 123 | SkScalar topLength = approx_arc_length(pts, kNumPtsCubic); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 124 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 125 | SkPatchUtils::getBottomCubic(cubics, pts); |
| 126 | matrix->mapPoints(pts, kNumPtsCubic); |
| 127 | SkScalar bottomLength = approx_arc_length(pts, kNumPtsCubic); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 128 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 129 | SkPatchUtils::getLeftCubic(cubics, pts); |
| 130 | matrix->mapPoints(pts, kNumPtsCubic); |
| 131 | SkScalar leftLength = approx_arc_length(pts, kNumPtsCubic); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 132 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 133 | SkPatchUtils::getRightCubic(cubics, pts); |
| 134 | matrix->mapPoints(pts, kNumPtsCubic); |
| 135 | SkScalar rightLength = approx_arc_length(pts, kNumPtsCubic); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 136 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 137 | // Level of detail per axis, based on the larger side between top and bottom or left and right |
| 138 | int lodX = static_cast<int>(SkMaxScalar(topLength, bottomLength) / kPartitionSize); |
| 139 | int lodY = static_cast<int>(SkMaxScalar(leftLength, rightLength) / kPartitionSize); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 140 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 141 | return SkISize::Make(SkMax32(8, lodX), SkMax32(8, lodY)); |
| 142 | } |
| 143 | |
| 144 | void SkPatchUtils::getTopCubic(const SkPoint cubics[12], SkPoint points[4]) { |
| 145 | points[0] = cubics[kTopP0_CubicCtrlPts]; |
| 146 | points[1] = cubics[kTopP1_CubicCtrlPts]; |
| 147 | points[2] = cubics[kTopP2_CubicCtrlPts]; |
| 148 | points[3] = cubics[kTopP3_CubicCtrlPts]; |
| 149 | } |
| 150 | |
| 151 | void SkPatchUtils::getBottomCubic(const SkPoint cubics[12], SkPoint points[4]) { |
| 152 | points[0] = cubics[kBottomP0_CubicCtrlPts]; |
| 153 | points[1] = cubics[kBottomP1_CubicCtrlPts]; |
| 154 | points[2] = cubics[kBottomP2_CubicCtrlPts]; |
| 155 | points[3] = cubics[kBottomP3_CubicCtrlPts]; |
| 156 | } |
| 157 | |
| 158 | void SkPatchUtils::getLeftCubic(const SkPoint cubics[12], SkPoint points[4]) { |
| 159 | points[0] = cubics[kLeftP0_CubicCtrlPts]; |
| 160 | points[1] = cubics[kLeftP1_CubicCtrlPts]; |
| 161 | points[2] = cubics[kLeftP2_CubicCtrlPts]; |
| 162 | points[3] = cubics[kLeftP3_CubicCtrlPts]; |
| 163 | } |
| 164 | |
| 165 | void SkPatchUtils::getRightCubic(const SkPoint cubics[12], SkPoint points[4]) { |
| 166 | points[0] = cubics[kRightP0_CubicCtrlPts]; |
| 167 | points[1] = cubics[kRightP1_CubicCtrlPts]; |
| 168 | points[2] = cubics[kRightP2_CubicCtrlPts]; |
| 169 | points[3] = cubics[kRightP3_CubicCtrlPts]; |
| 170 | } |
| 171 | |
| 172 | bool SkPatchUtils::getVertexData(SkPatchUtils::VertexData* data, const SkPoint cubics[12], |
| 173 | const SkColor colors[4], const SkPoint texCoords[4], int lodX, int lodY) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 174 | if (lodX < 1 || lodY < 1 || nullptr == cubics || nullptr == data) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 175 | return false; |
| 176 | } |
dandov | 45f7842 | 2014-08-15 06:06:47 -0700 | [diff] [blame] | 177 | |
| 178 | // check for overflow in multiplication |
| 179 | const int64_t lodX64 = (lodX + 1), |
| 180 | lodY64 = (lodY + 1), |
| 181 | mult64 = lodX64 * lodY64; |
| 182 | if (mult64 > SK_MaxS32) { |
| 183 | return false; |
| 184 | } |
| 185 | data->fVertexCount = SkToS32(mult64); |
| 186 | |
| 187 | // it is recommended to generate draw calls of no more than 65536 indices, so we never generate |
| 188 | // more than 60000 indices. To accomplish that we resize the LOD and vertex count |
| 189 | if (data->fVertexCount > 10000 || lodX > 200 || lodY > 200) { |
| 190 | SkScalar weightX = static_cast<SkScalar>(lodX) / (lodX + lodY); |
| 191 | SkScalar weightY = static_cast<SkScalar>(lodY) / (lodX + lodY); |
| 192 | |
| 193 | // 200 comes from the 100 * 2 which is the max value of vertices because of the limit of |
| 194 | // 60000 indices ( sqrt(60000 / 6) that comes from data->fIndexCount = lodX * lodY * 6) |
| 195 | lodX = static_cast<int>(weightX * 200); |
| 196 | lodY = static_cast<int>(weightY * 200); |
| 197 | data->fVertexCount = (lodX + 1) * (lodY + 1); |
| 198 | } |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 199 | data->fIndexCount = lodX * lodY * 6; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 200 | |
| 201 | data->fPoints = new SkPoint[data->fVertexCount]; |
| 202 | data->fIndices = new uint16_t[data->fIndexCount]; |
| 203 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 204 | // if colors is not null then create array for colors |
| 205 | SkPMColor colorsPM[kNumCorners]; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 206 | if (colors) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 207 | // premultiply colors to avoid color bleeding. |
| 208 | for (int i = 0; i < kNumCorners; i++) { |
| 209 | colorsPM[i] = SkPreMultiplyColor(colors[i]); |
| 210 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 211 | data->fColors = new uint32_t[data->fVertexCount]; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 212 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 213 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 214 | // if texture coordinates are not null then create array for them |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 215 | if (texCoords) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 216 | data->fTexCoords = new SkPoint[data->fVertexCount]; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 217 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 218 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 219 | SkPoint pts[kNumPtsCubic]; |
| 220 | SkPatchUtils::getBottomCubic(cubics, pts); |
| 221 | FwDCubicEvaluator fBottom(pts); |
| 222 | SkPatchUtils::getTopCubic(cubics, pts); |
| 223 | FwDCubicEvaluator fTop(pts); |
| 224 | SkPatchUtils::getLeftCubic(cubics, pts); |
| 225 | FwDCubicEvaluator fLeft(pts); |
| 226 | SkPatchUtils::getRightCubic(cubics, pts); |
| 227 | FwDCubicEvaluator fRight(pts); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 228 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 229 | fBottom.restart(lodX); |
| 230 | fTop.restart(lodX); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 231 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 232 | SkScalar u = 0.0f; |
| 233 | int stride = lodY + 1; |
| 234 | for (int x = 0; x <= lodX; x++) { |
| 235 | SkPoint bottom = fBottom.next(), top = fTop.next(); |
| 236 | fLeft.restart(lodY); |
| 237 | fRight.restart(lodY); |
| 238 | SkScalar v = 0.f; |
| 239 | for (int y = 0; y <= lodY; y++) { |
| 240 | int dataIndex = x * (lodY + 1) + y; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 241 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 242 | SkPoint left = fLeft.next(), right = fRight.next(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 243 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 244 | SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(), |
| 245 | (1.0f - v) * top.y() + v * bottom.y()); |
| 246 | SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(), |
| 247 | (1.0f - u) * left.y() + u * right.y()); |
| 248 | SkPoint s2 = SkPoint::Make( |
| 249 | (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].x() |
| 250 | + u * fTop.getCtrlPoints()[3].x()) |
| 251 | + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].x() |
| 252 | + u * fBottom.getCtrlPoints()[3].x()), |
| 253 | (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].y() |
| 254 | + u * fTop.getCtrlPoints()[3].y()) |
| 255 | + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].y() |
| 256 | + u * fBottom.getCtrlPoints()[3].y())); |
| 257 | data->fPoints[dataIndex] = s0 + s1 - s2; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 258 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 259 | if (colors) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 260 | uint8_t a = uint8_t(bilerp(u, v, |
| 261 | SkScalar(SkColorGetA(colorsPM[kTopLeft_Corner])), |
| 262 | SkScalar(SkColorGetA(colorsPM[kTopRight_Corner])), |
| 263 | SkScalar(SkColorGetA(colorsPM[kBottomLeft_Corner])), |
| 264 | SkScalar(SkColorGetA(colorsPM[kBottomRight_Corner])))); |
| 265 | uint8_t r = uint8_t(bilerp(u, v, |
| 266 | SkScalar(SkColorGetR(colorsPM[kTopLeft_Corner])), |
| 267 | SkScalar(SkColorGetR(colorsPM[kTopRight_Corner])), |
| 268 | SkScalar(SkColorGetR(colorsPM[kBottomLeft_Corner])), |
| 269 | SkScalar(SkColorGetR(colorsPM[kBottomRight_Corner])))); |
| 270 | uint8_t g = uint8_t(bilerp(u, v, |
| 271 | SkScalar(SkColorGetG(colorsPM[kTopLeft_Corner])), |
| 272 | SkScalar(SkColorGetG(colorsPM[kTopRight_Corner])), |
| 273 | SkScalar(SkColorGetG(colorsPM[kBottomLeft_Corner])), |
| 274 | SkScalar(SkColorGetG(colorsPM[kBottomRight_Corner])))); |
| 275 | uint8_t b = uint8_t(bilerp(u, v, |
| 276 | SkScalar(SkColorGetB(colorsPM[kTopLeft_Corner])), |
| 277 | SkScalar(SkColorGetB(colorsPM[kTopRight_Corner])), |
| 278 | SkScalar(SkColorGetB(colorsPM[kBottomLeft_Corner])), |
| 279 | SkScalar(SkColorGetB(colorsPM[kBottomRight_Corner])))); |
| 280 | data->fColors[dataIndex] = SkPackARGB32(a,r,g,b); |
| 281 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 282 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 283 | if (texCoords) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 284 | data->fTexCoords[dataIndex] = SkPoint::Make( |
| 285 | bilerp(u, v, texCoords[kTopLeft_Corner].x(), |
| 286 | texCoords[kTopRight_Corner].x(), |
| 287 | texCoords[kBottomLeft_Corner].x(), |
| 288 | texCoords[kBottomRight_Corner].x()), |
| 289 | bilerp(u, v, texCoords[kTopLeft_Corner].y(), |
| 290 | texCoords[kTopRight_Corner].y(), |
| 291 | texCoords[kBottomLeft_Corner].y(), |
| 292 | texCoords[kBottomRight_Corner].y())); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 293 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 294 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 295 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 296 | if(x < lodX && y < lodY) { |
| 297 | int i = 6 * (x * lodY + y); |
| 298 | data->fIndices[i] = x * stride + y; |
| 299 | data->fIndices[i + 1] = x * stride + 1 + y; |
| 300 | data->fIndices[i + 2] = (x + 1) * stride + 1 + y; |
| 301 | data->fIndices[i + 3] = data->fIndices[i]; |
| 302 | data->fIndices[i + 4] = data->fIndices[i + 2]; |
| 303 | data->fIndices[i + 5] = (x + 1) * stride + y; |
| 304 | } |
| 305 | v = SkScalarClampMax(v + 1.f / lodY, 1); |
| 306 | } |
| 307 | u = SkScalarClampMax(u + 1.f / lodX, 1); |
| 308 | } |
| 309 | return true; |
| 310 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 311 | } |
Mike Reed | 795c5ea | 2017-03-17 14:29:05 -0400 | [diff] [blame] | 312 | |
| 313 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 314 | |
| 315 | sk_sp<SkVertices> SkPatchUtils::MakeVertices(const SkPoint cubics[12], const SkColor srcColors[4], |
| 316 | const SkPoint srcTexCoords[4], int lodX, int lodY) { |
| 317 | if (lodX < 1 || lodY < 1 || nullptr == cubics) { |
| 318 | return nullptr; |
| 319 | } |
| 320 | |
| 321 | // check for overflow in multiplication |
| 322 | const int64_t lodX64 = (lodX + 1), |
| 323 | lodY64 = (lodY + 1), |
| 324 | mult64 = lodX64 * lodY64; |
| 325 | if (mult64 > SK_MaxS32) { |
| 326 | return nullptr; |
| 327 | } |
| 328 | |
| 329 | int vertexCount = SkToS32(mult64); |
| 330 | // it is recommended to generate draw calls of no more than 65536 indices, so we never generate |
| 331 | // more than 60000 indices. To accomplish that we resize the LOD and vertex count |
| 332 | if (vertexCount > 10000 || lodX > 200 || lodY > 200) { |
| 333 | float weightX = static_cast<float>(lodX) / (lodX + lodY); |
| 334 | float weightY = static_cast<float>(lodY) / (lodX + lodY); |
| 335 | |
| 336 | // 200 comes from the 100 * 2 which is the max value of vertices because of the limit of |
| 337 | // 60000 indices ( sqrt(60000 / 6) that comes from data->fIndexCount = lodX * lodY * 6) |
| 338 | lodX = static_cast<int>(weightX * 200); |
| 339 | lodY = static_cast<int>(weightY * 200); |
| 340 | vertexCount = (lodX + 1) * (lodY + 1); |
| 341 | } |
| 342 | const int indexCount = lodX * lodY * 6; |
| 343 | uint32_t flags = 0; |
| 344 | if (srcTexCoords) { |
| 345 | flags |= SkVertices::kHasTexCoords_BuilderFlag; |
| 346 | } |
| 347 | if (srcColors) { |
| 348 | flags |= SkVertices::kHasColors_BuilderFlag; |
| 349 | } |
| 350 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 351 | SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vertexCount, indexCount, flags); |
Mike Reed | 795c5ea | 2017-03-17 14:29:05 -0400 | [diff] [blame] | 352 | SkPoint* pos = builder.positions(); |
| 353 | SkPoint* texs = builder.texCoords(); |
| 354 | SkColor* colors = builder.colors(); |
| 355 | uint16_t* indices = builder.indices(); |
| 356 | |
| 357 | // if colors is not null then create array for colors |
| 358 | SkPMColor colorsPM[kNumCorners]; |
| 359 | if (srcColors) { |
| 360 | // premultiply colors to avoid color bleeding. |
| 361 | for (int i = 0; i < kNumCorners; i++) { |
| 362 | colorsPM[i] = SkPreMultiplyColor(srcColors[i]); |
| 363 | } |
| 364 | srcColors = colorsPM; |
| 365 | } |
| 366 | |
| 367 | SkPoint pts[kNumPtsCubic]; |
| 368 | SkPatchUtils::getBottomCubic(cubics, pts); |
| 369 | FwDCubicEvaluator fBottom(pts); |
| 370 | SkPatchUtils::getTopCubic(cubics, pts); |
| 371 | FwDCubicEvaluator fTop(pts); |
| 372 | SkPatchUtils::getLeftCubic(cubics, pts); |
| 373 | FwDCubicEvaluator fLeft(pts); |
| 374 | SkPatchUtils::getRightCubic(cubics, pts); |
| 375 | FwDCubicEvaluator fRight(pts); |
| 376 | |
| 377 | fBottom.restart(lodX); |
| 378 | fTop.restart(lodX); |
| 379 | |
| 380 | SkScalar u = 0.0f; |
| 381 | int stride = lodY + 1; |
| 382 | for (int x = 0; x <= lodX; x++) { |
| 383 | SkPoint bottom = fBottom.next(), top = fTop.next(); |
| 384 | fLeft.restart(lodY); |
| 385 | fRight.restart(lodY); |
| 386 | SkScalar v = 0.f; |
| 387 | for (int y = 0; y <= lodY; y++) { |
| 388 | int dataIndex = x * (lodY + 1) + y; |
| 389 | |
| 390 | SkPoint left = fLeft.next(), right = fRight.next(); |
| 391 | |
| 392 | SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(), |
| 393 | (1.0f - v) * top.y() + v * bottom.y()); |
| 394 | SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(), |
| 395 | (1.0f - u) * left.y() + u * right.y()); |
| 396 | SkPoint s2 = SkPoint::Make( |
| 397 | (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].x() |
| 398 | + u * fTop.getCtrlPoints()[3].x()) |
| 399 | + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].x() |
| 400 | + u * fBottom.getCtrlPoints()[3].x()), |
| 401 | (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].y() |
| 402 | + u * fTop.getCtrlPoints()[3].y()) |
| 403 | + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].y() |
| 404 | + u * fBottom.getCtrlPoints()[3].y())); |
| 405 | pos[dataIndex] = s0 + s1 - s2; |
| 406 | |
| 407 | if (colors) { |
| 408 | uint8_t a = uint8_t(bilerp(u, v, |
| 409 | SkScalar(SkColorGetA(colorsPM[kTopLeft_Corner])), |
| 410 | SkScalar(SkColorGetA(colorsPM[kTopRight_Corner])), |
| 411 | SkScalar(SkColorGetA(colorsPM[kBottomLeft_Corner])), |
| 412 | SkScalar(SkColorGetA(colorsPM[kBottomRight_Corner])))); |
| 413 | uint8_t r = uint8_t(bilerp(u, v, |
| 414 | SkScalar(SkColorGetR(colorsPM[kTopLeft_Corner])), |
| 415 | SkScalar(SkColorGetR(colorsPM[kTopRight_Corner])), |
| 416 | SkScalar(SkColorGetR(colorsPM[kBottomLeft_Corner])), |
| 417 | SkScalar(SkColorGetR(colorsPM[kBottomRight_Corner])))); |
| 418 | uint8_t g = uint8_t(bilerp(u, v, |
| 419 | SkScalar(SkColorGetG(colorsPM[kTopLeft_Corner])), |
| 420 | SkScalar(SkColorGetG(colorsPM[kTopRight_Corner])), |
| 421 | SkScalar(SkColorGetG(colorsPM[kBottomLeft_Corner])), |
| 422 | SkScalar(SkColorGetG(colorsPM[kBottomRight_Corner])))); |
| 423 | uint8_t b = uint8_t(bilerp(u, v, |
| 424 | SkScalar(SkColorGetB(colorsPM[kTopLeft_Corner])), |
| 425 | SkScalar(SkColorGetB(colorsPM[kTopRight_Corner])), |
| 426 | SkScalar(SkColorGetB(colorsPM[kBottomLeft_Corner])), |
| 427 | SkScalar(SkColorGetB(colorsPM[kBottomRight_Corner])))); |
| 428 | colors[dataIndex] = SkPackARGB32(a,r,g,b); |
| 429 | } |
| 430 | |
| 431 | if (texs) { |
| 432 | texs[dataIndex] = SkPoint::Make(bilerp(u, v, srcTexCoords[kTopLeft_Corner].x(), |
| 433 | srcTexCoords[kTopRight_Corner].x(), |
| 434 | srcTexCoords[kBottomLeft_Corner].x(), |
| 435 | srcTexCoords[kBottomRight_Corner].x()), |
| 436 | bilerp(u, v, srcTexCoords[kTopLeft_Corner].y(), |
| 437 | srcTexCoords[kTopRight_Corner].y(), |
| 438 | srcTexCoords[kBottomLeft_Corner].y(), |
| 439 | srcTexCoords[kBottomRight_Corner].y())); |
| 440 | |
| 441 | } |
| 442 | |
| 443 | if(x < lodX && y < lodY) { |
| 444 | int i = 6 * (x * lodY + y); |
| 445 | indices[i] = x * stride + y; |
| 446 | indices[i + 1] = x * stride + 1 + y; |
| 447 | indices[i + 2] = (x + 1) * stride + 1 + y; |
| 448 | indices[i + 3] = indices[i]; |
| 449 | indices[i + 4] = indices[i + 2]; |
| 450 | indices[i + 5] = (x + 1) * stride + y; |
| 451 | } |
| 452 | v = SkScalarClampMax(v + 1.f / lodY, 1); |
| 453 | } |
| 454 | u = SkScalarClampMax(u + 1.f / lodX, 1); |
| 455 | } |
| 456 | return builder.detach(); |
| 457 | } |