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 "src/utils/SkPatchUtils.h"
9
10 #include "include/core/SkVertices.h"
11 #include "include/private/SkColorData.h"
12 #include "include/private/SkTPin.h"
13 #include "include/private/SkTo.h"
14 #include "src/core/SkArenaAlloc.h"
15 #include "src/core/SkColorSpacePriv.h"
16 #include "src/core/SkConvertPixels.h"
17 #include "src/core/SkGeometry.h"
18
19 namespace {
20 enum CubicCtrlPts {
21 kTopP0_CubicCtrlPts = 0,
22 kTopP1_CubicCtrlPts = 1,
23 kTopP2_CubicCtrlPts = 2,
24 kTopP3_CubicCtrlPts = 3,
25
26 kRightP0_CubicCtrlPts = 3,
27 kRightP1_CubicCtrlPts = 4,
28 kRightP2_CubicCtrlPts = 5,
29 kRightP3_CubicCtrlPts = 6,
30
31 kBottomP0_CubicCtrlPts = 9,
32 kBottomP1_CubicCtrlPts = 8,
33 kBottomP2_CubicCtrlPts = 7,
34 kBottomP3_CubicCtrlPts = 6,
35
36 kLeftP0_CubicCtrlPts = 0,
37 kLeftP1_CubicCtrlPts = 11,
38 kLeftP2_CubicCtrlPts = 10,
39 kLeftP3_CubicCtrlPts = 9,
40 };
41
42 // Enum for corner also clockwise.
43 enum Corner {
44 kTopLeft_Corner = 0,
45 kTopRight_Corner,
46 kBottomRight_Corner,
47 kBottomLeft_Corner
48 };
49 } // namespace
50
51 /**
52 * Evaluator to sample the values of a cubic bezier using forward differences.
53 * Forward differences is a method for evaluating a nth degree polynomial at a uniform step by only
54 * adding precalculated values.
55 * For a linear example we have the function f(t) = m*t+b, then the value of that function at t+h
56 * would be f(t+h) = m*(t+h)+b. If we want to know the uniform step that we must add to the first
57 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t + b = mh. After
58 * obtaining this value (mh) we could just add this constant step to our first sampled point
59 * to compute the next one.
60 *
61 * For the cubic case the first difference gives as a result a quadratic polynomial to which we can
62 * apply again forward differences and get linear function to which we can apply again forward
63 * differences to get a constant difference. This is why we keep an array of size 4, the 0th
64 * position keeps the sampled value while the next ones keep the quadratic, linear and constant
65 * difference values.
66 */
67
68 class FwDCubicEvaluator {
69
70 public:
71
72 /**
73 * Receives the 4 control points of the cubic bezier.
74 */
75
FwDCubicEvaluator(const SkPoint points[4])76 explicit FwDCubicEvaluator(const SkPoint points[4])
77 : fCoefs(points) {
78 memcpy(fPoints, points, 4 * sizeof(SkPoint));
79
80 this->restart(1);
81 }
82
83 /**
84 * Restarts the forward differences evaluator to the first value of t = 0.
85 */
restart(int divisions)86 void restart(int divisions) {
87 fDivisions = divisions;
88 fCurrent = 0;
89 fMax = fDivisions + 1;
90 Sk2s h = Sk2s(1.f / fDivisions);
91 Sk2s h2 = h * h;
92 Sk2s h3 = h2 * h;
93 Sk2s fwDiff3 = Sk2s(6) * fCoefs.fA * h3;
94 fFwDiff[3] = to_point(fwDiff3);
95 fFwDiff[2] = to_point(fwDiff3 + times_2(fCoefs.fB) * h2);
96 fFwDiff[1] = to_point(fCoefs.fA * h3 + fCoefs.fB * h2 + fCoefs.fC * h);
97 fFwDiff[0] = to_point(fCoefs.fD);
98 }
99
100 /**
101 * Check if the evaluator is still within the range of 0<=t<=1
102 */
done() const103 bool done() const {
104 return fCurrent > fMax;
105 }
106
107 /**
108 * Call next to obtain the SkPoint sampled and move to the next one.
109 */
next()110 SkPoint next() {
111 SkPoint point = fFwDiff[0];
112 fFwDiff[0] += fFwDiff[1];
113 fFwDiff[1] += fFwDiff[2];
114 fFwDiff[2] += fFwDiff[3];
115 fCurrent++;
116 return point;
117 }
118
getCtrlPoints() const119 const SkPoint* getCtrlPoints() const {
120 return fPoints;
121 }
122
123 private:
124 SkCubicCoeff fCoefs;
125 int fMax, fCurrent, fDivisions;
126 SkPoint fFwDiff[4], fPoints[4];
127 };
128
129 ////////////////////////////////////////////////////////////////////////////////
130
131 // size in pixels of each partition per axis, adjust this knob
132 static const int kPartitionSize = 10;
133
134 /**
135 * Calculate the approximate arc length given a bezier curve's control points.
136 * Returns -1 if bad calc (i.e. non-finite)
137 */
approx_arc_length(const SkPoint points[],int count)138 static SkScalar approx_arc_length(const SkPoint points[], int count) {
139 if (count < 2) {
140 return 0;
141 }
142 SkScalar arcLength = 0;
143 for (int i = 0; i < count - 1; i++) {
144 arcLength += SkPoint::Distance(points[i], points[i + 1]);
145 }
146 return SkScalarIsFinite(arcLength) ? arcLength : -1;
147 }
148
bilerp(SkScalar tx,SkScalar ty,SkScalar c00,SkScalar c10,SkScalar c01,SkScalar c11)149 static SkScalar bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01,
150 SkScalar c11) {
151 SkScalar a = c00 * (1.f - tx) + c10 * tx;
152 SkScalar b = c01 * (1.f - tx) + c11 * tx;
153 return a * (1.f - ty) + b * ty;
154 }
155
bilerp(SkScalar tx,SkScalar ty,const Sk4f & c00,const Sk4f & c10,const Sk4f & c01,const Sk4f & c11)156 static Sk4f bilerp(SkScalar tx, SkScalar ty,
157 const Sk4f& c00, const Sk4f& c10, const Sk4f& c01, const Sk4f& c11) {
158 Sk4f a = c00 * (1.f - tx) + c10 * tx;
159 Sk4f b = c01 * (1.f - tx) + c11 * tx;
160 return a * (1.f - ty) + b * ty;
161 }
162
GetLevelOfDetail(const SkPoint cubics[12],const SkMatrix * matrix)163 SkISize SkPatchUtils::GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix* matrix) {
164 // Approximate length of each cubic.
165 SkPoint pts[kNumPtsCubic];
166 SkPatchUtils::GetTopCubic(cubics, pts);
167 matrix->mapPoints(pts, kNumPtsCubic);
168 SkScalar topLength = approx_arc_length(pts, kNumPtsCubic);
169
170 SkPatchUtils::GetBottomCubic(cubics, pts);
171 matrix->mapPoints(pts, kNumPtsCubic);
172 SkScalar bottomLength = approx_arc_length(pts, kNumPtsCubic);
173
174 SkPatchUtils::GetLeftCubic(cubics, pts);
175 matrix->mapPoints(pts, kNumPtsCubic);
176 SkScalar leftLength = approx_arc_length(pts, kNumPtsCubic);
177
178 SkPatchUtils::GetRightCubic(cubics, pts);
179 matrix->mapPoints(pts, kNumPtsCubic);
180 SkScalar rightLength = approx_arc_length(pts, kNumPtsCubic);
181
182 if (topLength < 0 || bottomLength < 0 || leftLength < 0 || rightLength < 0) {
183 return {0, 0}; // negative length is a sentinel for bad length (i.e. non-finite)
184 }
185
186 // Level of detail per axis, based on the larger side between top and bottom or left and right
187 int lodX = static_cast<int>(std::max(topLength, bottomLength) / kPartitionSize);
188 int lodY = static_cast<int>(std::max(leftLength, rightLength) / kPartitionSize);
189
190 return SkISize::Make(std::max(8, lodX), std::max(8, lodY));
191 }
192
GetTopCubic(const SkPoint cubics[12],SkPoint points[4])193 void SkPatchUtils::GetTopCubic(const SkPoint cubics[12], SkPoint points[4]) {
194 points[0] = cubics[kTopP0_CubicCtrlPts];
195 points[1] = cubics[kTopP1_CubicCtrlPts];
196 points[2] = cubics[kTopP2_CubicCtrlPts];
197 points[3] = cubics[kTopP3_CubicCtrlPts];
198 }
199
GetBottomCubic(const SkPoint cubics[12],SkPoint points[4])200 void SkPatchUtils::GetBottomCubic(const SkPoint cubics[12], SkPoint points[4]) {
201 points[0] = cubics[kBottomP0_CubicCtrlPts];
202 points[1] = cubics[kBottomP1_CubicCtrlPts];
203 points[2] = cubics[kBottomP2_CubicCtrlPts];
204 points[3] = cubics[kBottomP3_CubicCtrlPts];
205 }
206
GetLeftCubic(const SkPoint cubics[12],SkPoint points[4])207 void SkPatchUtils::GetLeftCubic(const SkPoint cubics[12], SkPoint points[4]) {
208 points[0] = cubics[kLeftP0_CubicCtrlPts];
209 points[1] = cubics[kLeftP1_CubicCtrlPts];
210 points[2] = cubics[kLeftP2_CubicCtrlPts];
211 points[3] = cubics[kLeftP3_CubicCtrlPts];
212 }
213
GetRightCubic(const SkPoint cubics[12],SkPoint points[4])214 void SkPatchUtils::GetRightCubic(const SkPoint cubics[12], SkPoint points[4]) {
215 points[0] = cubics[kRightP0_CubicCtrlPts];
216 points[1] = cubics[kRightP1_CubicCtrlPts];
217 points[2] = cubics[kRightP2_CubicCtrlPts];
218 points[3] = cubics[kRightP3_CubicCtrlPts];
219 }
220
skcolor_to_float(SkPMColor4f * dst,const SkColor * src,int count,SkColorSpace * dstCS)221 static void skcolor_to_float(SkPMColor4f* dst, const SkColor* src, int count, SkColorSpace* dstCS) {
222 SkImageInfo srcInfo = SkImageInfo::Make(count, 1, kBGRA_8888_SkColorType,
223 kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB());
224 SkImageInfo dstInfo = SkImageInfo::Make(count, 1, kRGBA_F32_SkColorType,
225 kPremul_SkAlphaType, sk_ref_sp(dstCS));
226 SkAssertResult(SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0));
227 }
228
float_to_skcolor(SkColor * dst,const SkPMColor4f * src,int count,SkColorSpace * srcCS)229 static void float_to_skcolor(SkColor* dst, const SkPMColor4f* src, int count, SkColorSpace* srcCS) {
230 SkImageInfo srcInfo = SkImageInfo::Make(count, 1, kRGBA_F32_SkColorType,
231 kPremul_SkAlphaType, sk_ref_sp(srcCS));
232 SkImageInfo dstInfo = SkImageInfo::Make(count, 1, kBGRA_8888_SkColorType,
233 kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB());
234 SkAssertResult(SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0));
235 }
236
MakeVertices(const SkPoint cubics[12],const SkColor srcColors[4],const SkPoint srcTexCoords[4],int lodX,int lodY,SkColorSpace * colorSpace)237 sk_sp<SkVertices> SkPatchUtils::MakeVertices(const SkPoint cubics[12], const SkColor srcColors[4],
238 const SkPoint srcTexCoords[4], int lodX, int lodY,
239 SkColorSpace* colorSpace) {
240 if (lodX < 1 || lodY < 1 || nullptr == cubics) {
241 return nullptr;
242 }
243
244 // check for overflow in multiplication
245 const int64_t lodX64 = (lodX + 1),
246 lodY64 = (lodY + 1),
247 mult64 = lodX64 * lodY64;
248 if (mult64 > SK_MaxS32) {
249 return nullptr;
250 }
251
252 // Treat null interpolation space as sRGB.
253 if (!colorSpace) {
254 colorSpace = sk_srgb_singleton();
255 }
256
257 int vertexCount = SkToS32(mult64);
258 // it is recommended to generate draw calls of no more than 65536 indices, so we never generate
259 // more than 60000 indices. To accomplish that we resize the LOD and vertex count
260 if (vertexCount > 10000 || lodX > 200 || lodY > 200) {
261 float weightX = static_cast<float>(lodX) / (lodX + lodY);
262 float weightY = static_cast<float>(lodY) / (lodX + lodY);
263
264 // 200 comes from the 100 * 2 which is the max value of vertices because of the limit of
265 // 60000 indices ( sqrt(60000 / 6) that comes from data->fIndexCount = lodX * lodY * 6)
266 // Need a min of 1 since we later divide by lod
267 lodX = std::max(1, sk_float_floor2int_no_saturate(weightX * 200));
268 lodY = std::max(1, sk_float_floor2int_no_saturate(weightY * 200));
269 vertexCount = (lodX + 1) * (lodY + 1);
270 }
271 const int indexCount = lodX * lodY * 6;
272 uint32_t flags = 0;
273 if (srcTexCoords) {
274 flags |= SkVertices::kHasTexCoords_BuilderFlag;
275 }
276 if (srcColors) {
277 flags |= SkVertices::kHasColors_BuilderFlag;
278 }
279
280 SkSTArenaAlloc<2048> alloc;
281 SkPMColor4f* cornerColors = srcColors ? alloc.makeArray<SkPMColor4f>(4) : nullptr;
282 SkPMColor4f* tmpColors = srcColors ? alloc.makeArray<SkPMColor4f>(vertexCount) : nullptr;
283
284 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vertexCount, indexCount, flags);
285 SkPoint* pos = builder.positions();
286 SkPoint* texs = builder.texCoords();
287 uint16_t* indices = builder.indices();
288
289 if (cornerColors) {
290 skcolor_to_float(cornerColors, srcColors, kNumCorners, colorSpace);
291 }
292
293 SkPoint pts[kNumPtsCubic];
294 SkPatchUtils::GetBottomCubic(cubics, pts);
295 FwDCubicEvaluator fBottom(pts);
296 SkPatchUtils::GetTopCubic(cubics, pts);
297 FwDCubicEvaluator fTop(pts);
298 SkPatchUtils::GetLeftCubic(cubics, pts);
299 FwDCubicEvaluator fLeft(pts);
300 SkPatchUtils::GetRightCubic(cubics, pts);
301 FwDCubicEvaluator fRight(pts);
302
303 fBottom.restart(lodX);
304 fTop.restart(lodX);
305
306 SkScalar u = 0.0f;
307 int stride = lodY + 1;
308 for (int x = 0; x <= lodX; x++) {
309 SkPoint bottom = fBottom.next(), top = fTop.next();
310 fLeft.restart(lodY);
311 fRight.restart(lodY);
312 SkScalar v = 0.f;
313 for (int y = 0; y <= lodY; y++) {
314 int dataIndex = x * (lodY + 1) + y;
315
316 SkPoint left = fLeft.next(), right = fRight.next();
317
318 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(),
319 (1.0f - v) * top.y() + v * bottom.y());
320 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(),
321 (1.0f - u) * left.y() + u * right.y());
322 SkPoint s2 = SkPoint::Make(
323 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].x()
324 + u * fTop.getCtrlPoints()[3].x())
325 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].x()
326 + u * fBottom.getCtrlPoints()[3].x()),
327 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].y()
328 + u * fTop.getCtrlPoints()[3].y())
329 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].y()
330 + u * fBottom.getCtrlPoints()[3].y()));
331 pos[dataIndex] = s0 + s1 - s2;
332
333 if (cornerColors) {
334 bilerp(u, v, Sk4f::Load(cornerColors[kTopLeft_Corner].vec()),
335 Sk4f::Load(cornerColors[kTopRight_Corner].vec()),
336 Sk4f::Load(cornerColors[kBottomLeft_Corner].vec()),
337 Sk4f::Load(cornerColors[kBottomRight_Corner].vec()))
338 .store(tmpColors[dataIndex].vec());
339 }
340
341 if (texs) {
342 texs[dataIndex] = SkPoint::Make(bilerp(u, v, srcTexCoords[kTopLeft_Corner].x(),
343 srcTexCoords[kTopRight_Corner].x(),
344 srcTexCoords[kBottomLeft_Corner].x(),
345 srcTexCoords[kBottomRight_Corner].x()),
346 bilerp(u, v, srcTexCoords[kTopLeft_Corner].y(),
347 srcTexCoords[kTopRight_Corner].y(),
348 srcTexCoords[kBottomLeft_Corner].y(),
349 srcTexCoords[kBottomRight_Corner].y()));
350
351 }
352
353 if(x < lodX && y < lodY) {
354 int i = 6 * (x * lodY + y);
355 indices[i] = x * stride + y;
356 indices[i + 1] = x * stride + 1 + y;
357 indices[i + 2] = (x + 1) * stride + 1 + y;
358 indices[i + 3] = indices[i];
359 indices[i + 4] = indices[i + 2];
360 indices[i + 5] = (x + 1) * stride + y;
361 }
362 v = SkTPin(v + 1.f / lodY, 0.0f, 1.0f);
363 }
364 u = SkTPin(u + 1.f / lodX, 0.0f, 1.0f);
365 }
366
367 if (tmpColors) {
368 float_to_skcolor(builder.colors(), tmpColors, vertexCount, colorSpace);
369 }
370 return builder.detach();
371 }
372