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