• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
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 {
31 
32 public:
33 
34     /**
35      * Receives the 4 control points of the cubic bezier.
36      */
37 
FwDCubicEvaluator(const SkPoint points[4])38     explicit FwDCubicEvaluator(const SkPoint points[4])
39             : fCoefs(points) {
40         memcpy(fPoints, points, 4 * sizeof(SkPoint));
41 
42         this->restart(1);
43     }
44 
45     /**
46      * Restarts the forward differences evaluator to the first value of t = 0.
47      */
restart(int divisions)48     void restart(int divisions)  {
49         fDivisions = divisions;
50         fCurrent    = 0;
51         fMax        = fDivisions + 1;
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);
60     }
61 
62     /**
63      * Check if the evaluator is still within the range of 0<=t<=1
64      */
done() const65     bool done() const {
66         return fCurrent > fMax;
67     }
68 
69     /**
70      * Call next to obtain the SkPoint sampled and move to the next one.
71      */
next()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     }
80 
getCtrlPoints() const81     const SkPoint* getCtrlPoints() const {
82         return fPoints;
83     }
84 
85 private:
86     SkCubicCoeff fCoefs;
87     int fMax, fCurrent, fDivisions;
88     SkPoint fFwDiff[4], fPoints[4];
89 };
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 
93 // size in pixels of each partition per axis, adjust this knob
94 static const int kPartitionSize = 10;
95 
96 /**
97  * Calculate the approximate arc length given a bezier curve's control points.
98  */
approx_arc_length(SkPoint * points,int count)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 
bilerp(SkScalar tx,SkScalar ty,SkScalar c00,SkScalar c10,SkScalar c01,SkScalar c11)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 
GetLevelOfDetail(const SkPoint cubics[12],const SkMatrix * matrix)117 SkISize SkPatchUtils::GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix* matrix) {
118 
119     // Approximate length of each cubic.
120     SkPoint pts[kNumPtsCubic];
121     SkPatchUtils::getTopCubic(cubics, pts);
122     matrix->mapPoints(pts, kNumPtsCubic);
123     SkScalar topLength = approx_arc_length(pts, kNumPtsCubic);
124 
125     SkPatchUtils::getBottomCubic(cubics, pts);
126     matrix->mapPoints(pts, kNumPtsCubic);
127     SkScalar bottomLength = approx_arc_length(pts, kNumPtsCubic);
128 
129     SkPatchUtils::getLeftCubic(cubics, pts);
130     matrix->mapPoints(pts, kNumPtsCubic);
131     SkScalar leftLength = approx_arc_length(pts, kNumPtsCubic);
132 
133     SkPatchUtils::getRightCubic(cubics, pts);
134     matrix->mapPoints(pts, kNumPtsCubic);
135     SkScalar rightLength = approx_arc_length(pts, kNumPtsCubic);
136 
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);
140 
141     return SkISize::Make(SkMax32(8, lodX), SkMax32(8, lodY));
142 }
143 
getTopCubic(const SkPoint cubics[12],SkPoint points[4])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 
getBottomCubic(const SkPoint cubics[12],SkPoint points[4])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 
getLeftCubic(const SkPoint cubics[12],SkPoint points[4])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 
getRightCubic(const SkPoint cubics[12],SkPoint points[4])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 
getVertexData(SkPatchUtils::VertexData * data,const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],int lodX,int lodY)172 bool SkPatchUtils::getVertexData(SkPatchUtils::VertexData* data, const SkPoint cubics[12],
173                    const SkColor colors[4], const SkPoint texCoords[4], int lodX, int lodY) {
174     if (lodX < 1 || lodY < 1 || nullptr == cubics || nullptr == data) {
175         return false;
176     }
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     }
199     data->fIndexCount = lodX * lodY * 6;
200 
201     data->fPoints = new SkPoint[data->fVertexCount];
202     data->fIndices = new uint16_t[data->fIndexCount];
203 
204     // if colors is not null then create array for colors
205     SkPMColor colorsPM[kNumCorners];
206     if (colors) {
207         // premultiply colors to avoid color bleeding.
208         for (int i = 0; i < kNumCorners; i++) {
209             colorsPM[i] = SkPreMultiplyColor(colors[i]);
210         }
211         data->fColors = new uint32_t[data->fVertexCount];
212     }
213 
214     // if texture coordinates are not null then create array for them
215     if (texCoords) {
216         data->fTexCoords = new SkPoint[data->fVertexCount];
217     }
218 
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);
228 
229     fBottom.restart(lodX);
230     fTop.restart(lodX);
231 
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;
241 
242             SkPoint left = fLeft.next(), right = fRight.next();
243 
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;
258 
259             if (colors) {
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             }
282 
283             if (texCoords) {
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()));
293 
294             }
295 
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 
311 }
312