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