• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libs/android_runtime/android/graphics/Path.cpp
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 // This file was generated from the C++ include file: SkPath.h
19 // Any changes made to this file will be discarded by the build.
20 // To change this file, either edit the include, or device/tools/gluemaker/main.cpp,
21 // or one of the auxilary file specifications in device/tools/gluemaker.
22 
23 #include "jni.h"
24 #include "GraphicsJNI.h"
25 #include <android_runtime/AndroidRuntime.h>
26 
27 #include "SkPath.h"
28 #include "SkPathOps.h"
29 
30 #include <ResourceCache.h>
31 #include <vector>
32 #include <map>
33 
34 namespace android {
35 
36 class SkPathGlue {
37 public:
38 
finalizer(JNIEnv * env,jobject clazz,jlong objHandle)39     static void finalizer(JNIEnv* env, jobject clazz, jlong objHandle) {
40         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
41 #ifdef USE_OPENGL_RENDERER
42         if (android::uirenderer::ResourceCache::hasInstance()) {
43             android::uirenderer::ResourceCache::getInstance().destructor(obj);
44             return;
45         }
46 #endif
47         delete obj;
48     }
49 
init1(JNIEnv * env,jobject clazz)50     static jlong init1(JNIEnv* env, jobject clazz) {
51         return reinterpret_cast<jlong>(new SkPath());
52     }
53 
init2(JNIEnv * env,jobject clazz,jlong valHandle)54     static jlong init2(JNIEnv* env, jobject clazz, jlong valHandle) {
55         SkPath* val = reinterpret_cast<SkPath*>(valHandle);
56         return reinterpret_cast<jlong>(new SkPath(*val));
57     }
58 
reset(JNIEnv * env,jobject clazz,jlong objHandle)59     static void reset(JNIEnv* env, jobject clazz, jlong objHandle) {
60         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
61         obj->reset();
62     }
63 
rewind(JNIEnv * env,jobject clazz,jlong objHandle)64     static void rewind(JNIEnv* env, jobject clazz, jlong objHandle) {
65         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
66         obj->rewind();
67     }
68 
assign(JNIEnv * env,jobject clazz,jlong dstHandle,jlong srcHandle)69     static void assign(JNIEnv* env, jobject clazz, jlong dstHandle, jlong srcHandle) {
70         SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
71         const SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
72         *dst = *src;
73     }
74 
isConvex(JNIEnv * env,jobject clazz,jlong objHandle)75     static jboolean isConvex(JNIEnv* env, jobject clazz, jlong objHandle) {
76         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
77         return obj->isConvex();
78     }
79 
getFillType(JNIEnv * env,jobject clazz,jlong objHandle)80     static jint getFillType(JNIEnv* env, jobject clazz, jlong objHandle) {
81         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
82         return obj->getFillType();
83     }
84 
setFillType(JNIEnv * env,jobject clazz,jlong pathHandle,jint ftHandle)85     static void setFillType(JNIEnv* env, jobject clazz, jlong pathHandle, jint ftHandle) {
86         SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
87         SkPath::FillType ft = static_cast<SkPath::FillType>(ftHandle);
88         path->setFillType(ft);
89     }
90 
isEmpty(JNIEnv * env,jobject clazz,jlong objHandle)91     static jboolean isEmpty(JNIEnv* env, jobject clazz, jlong objHandle) {
92         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
93         return obj->isEmpty();
94     }
95 
isRect(JNIEnv * env,jobject clazz,jlong objHandle,jobject jrect)96     static jboolean isRect(JNIEnv* env, jobject clazz, jlong objHandle, jobject jrect) {
97         SkRect rect;
98         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
99         jboolean result = obj->isRect(&rect);
100         GraphicsJNI::rect_to_jrectf(rect, env, jrect);
101         return result;
102     }
103 
computeBounds(JNIEnv * env,jobject clazz,jlong objHandle,jobject jbounds)104     static void computeBounds(JNIEnv* env, jobject clazz, jlong objHandle, jobject jbounds) {
105         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
106         const SkRect& bounds = obj->getBounds();
107         GraphicsJNI::rect_to_jrectf(bounds, env, jbounds);
108     }
109 
incReserve(JNIEnv * env,jobject clazz,jlong objHandle,jint extraPtCount)110     static void incReserve(JNIEnv* env, jobject clazz, jlong objHandle, jint extraPtCount) {
111         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
112         obj->incReserve(extraPtCount);
113     }
114 
moveTo__FF(JNIEnv * env,jobject clazz,jlong objHandle,jfloat x,jfloat y)115     static void moveTo__FF(JNIEnv* env, jobject clazz, jlong objHandle, jfloat x, jfloat y) {
116         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
117         obj->moveTo(x, y);
118     }
119 
rMoveTo(JNIEnv * env,jobject clazz,jlong objHandle,jfloat dx,jfloat dy)120     static void rMoveTo(JNIEnv* env, jobject clazz, jlong objHandle, jfloat dx, jfloat dy) {
121         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
122         obj->rMoveTo(dx, dy);
123     }
124 
lineTo__FF(JNIEnv * env,jobject clazz,jlong objHandle,jfloat x,jfloat y)125     static void lineTo__FF(JNIEnv* env, jobject clazz, jlong objHandle, jfloat x, jfloat y) {
126         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
127         obj->lineTo(x, y);
128     }
129 
rLineTo(JNIEnv * env,jobject clazz,jlong objHandle,jfloat dx,jfloat dy)130     static void rLineTo(JNIEnv* env, jobject clazz, jlong objHandle, jfloat dx, jfloat dy) {
131         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
132         obj->rLineTo(dx, dy);
133     }
134 
quadTo__FFFF(JNIEnv * env,jobject clazz,jlong objHandle,jfloat x1,jfloat y1,jfloat x2,jfloat y2)135     static void quadTo__FFFF(JNIEnv* env, jobject clazz, jlong objHandle, jfloat x1, jfloat y1, jfloat x2, jfloat y2) {
136         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
137         obj->quadTo(x1, y1, x2, y2);
138     }
139 
rQuadTo(JNIEnv * env,jobject clazz,jlong objHandle,jfloat dx1,jfloat dy1,jfloat dx2,jfloat dy2)140     static void rQuadTo(JNIEnv* env, jobject clazz, jlong objHandle, jfloat dx1, jfloat dy1, jfloat dx2, jfloat dy2) {
141         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
142         obj->rQuadTo(dx1, dy1, dx2, dy2);
143     }
144 
cubicTo__FFFFFF(JNIEnv * env,jobject clazz,jlong objHandle,jfloat x1,jfloat y1,jfloat x2,jfloat y2,jfloat x3,jfloat y3)145     static void cubicTo__FFFFFF(JNIEnv* env, jobject clazz, jlong objHandle, jfloat x1, jfloat y1, jfloat x2, jfloat y2, jfloat x3, jfloat y3) {
146         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
147         obj->cubicTo(x1, y1, x2, y2, x3, y3);
148     }
149 
rCubicTo(JNIEnv * env,jobject clazz,jlong objHandle,jfloat x1,jfloat y1,jfloat x2,jfloat y2,jfloat x3,jfloat y3)150     static void rCubicTo(JNIEnv* env, jobject clazz, jlong objHandle, jfloat x1, jfloat y1, jfloat x2, jfloat y2, jfloat x3, jfloat y3) {
151         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
152         obj->rCubicTo(x1, y1, x2, y2, x3, y3);
153     }
154 
arcTo(JNIEnv * env,jobject clazz,jlong objHandle,jfloat left,jfloat top,jfloat right,jfloat bottom,jfloat startAngle,jfloat sweepAngle,jboolean forceMoveTo)155     static void arcTo(JNIEnv* env, jobject clazz, jlong objHandle, jfloat left, jfloat top,
156             jfloat right, jfloat bottom, jfloat startAngle, jfloat sweepAngle,
157             jboolean forceMoveTo) {
158         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
159         SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
160         obj->arcTo(oval, startAngle, sweepAngle, forceMoveTo);
161     }
162 
close(JNIEnv * env,jobject clazz,jlong objHandle)163     static void close(JNIEnv* env, jobject clazz, jlong objHandle) {
164         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
165         obj->close();
166     }
167 
addRect(JNIEnv * env,jobject clazz,jlong objHandle,jfloat left,jfloat top,jfloat right,jfloat bottom,jint dirHandle)168     static void addRect(JNIEnv* env, jobject clazz, jlong objHandle,
169             jfloat left, jfloat top, jfloat right, jfloat bottom, jint dirHandle) {
170         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
171         SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle);
172         obj->addRect(left, top, right, bottom, dir);
173     }
174 
addOval(JNIEnv * env,jobject clazz,jlong objHandle,jfloat left,jfloat top,jfloat right,jfloat bottom,jint dirHandle)175     static void addOval(JNIEnv* env, jobject clazz, jlong objHandle,
176             jfloat left, jfloat top, jfloat right, jfloat bottom, jint dirHandle) {
177         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
178         SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle);
179         SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
180         obj->addOval(oval, dir);
181     }
182 
addCircle(JNIEnv * env,jobject clazz,jlong objHandle,jfloat x,jfloat y,jfloat radius,jint dirHandle)183     static void addCircle(JNIEnv* env, jobject clazz, jlong objHandle, jfloat x, jfloat y, jfloat radius, jint dirHandle) {
184         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
185         SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle);
186         obj->addCircle(x, y, radius, dir);
187     }
188 
addArc(JNIEnv * env,jobject clazz,jlong objHandle,jfloat left,jfloat top,jfloat right,jfloat bottom,jfloat startAngle,jfloat sweepAngle)189     static void addArc(JNIEnv* env, jobject clazz, jlong objHandle, jfloat left, jfloat top,
190             jfloat right, jfloat bottom, jfloat startAngle, jfloat sweepAngle) {
191         SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
192         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
193         obj->addArc(oval, startAngle, sweepAngle);
194     }
195 
addRoundRectXY(JNIEnv * env,jobject clazz,jlong objHandle,jfloat left,jfloat top,jfloat right,jfloat bottom,jfloat rx,jfloat ry,jint dirHandle)196     static void addRoundRectXY(JNIEnv* env, jobject clazz, jlong objHandle, jfloat left, jfloat top,
197             jfloat right, jfloat bottom, jfloat rx, jfloat ry, jint dirHandle) {
198         SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
199         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
200         SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle);
201         obj->addRoundRect(rect, rx, ry, dir);
202     }
203 
addRoundRect8(JNIEnv * env,jobject,jlong objHandle,jfloat left,jfloat top,jfloat right,jfloat bottom,jfloatArray array,jint dirHandle)204     static void addRoundRect8(JNIEnv* env, jobject, jlong objHandle, jfloat left, jfloat top,
205             jfloat right, jfloat bottom, jfloatArray array, jint dirHandle) {
206         SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
207         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
208         SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle);
209         AutoJavaFloatArray  afa(env, array, 8);
210 #ifdef SK_SCALAR_IS_FLOAT
211         const float* src = afa.ptr();
212 #else
213         #error Need to convert float array to SkScalar array before calling the following function.
214 #endif
215         obj->addRoundRect(rect, src, dir);
216     }
217 
addPath__PathFF(JNIEnv * env,jobject clazz,jlong objHandle,jlong srcHandle,jfloat dx,jfloat dy)218     static void addPath__PathFF(JNIEnv* env, jobject clazz, jlong objHandle, jlong srcHandle, jfloat dx, jfloat dy) {
219         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
220         SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
221         obj->addPath(*src, dx, dy);
222     }
223 
addPath__Path(JNIEnv * env,jobject clazz,jlong objHandle,jlong srcHandle)224     static void addPath__Path(JNIEnv* env, jobject clazz, jlong objHandle, jlong srcHandle) {
225         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
226         SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
227         obj->addPath(*src);
228     }
229 
addPath__PathMatrix(JNIEnv * env,jobject clazz,jlong objHandle,jlong srcHandle,jlong matrixHandle)230     static void addPath__PathMatrix(JNIEnv* env, jobject clazz, jlong objHandle, jlong srcHandle, jlong matrixHandle) {
231         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
232         SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
233         SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
234         obj->addPath(*src, *matrix);
235     }
236 
offset__FFPath(JNIEnv * env,jobject clazz,jlong objHandle,jfloat dx,jfloat dy,jlong dstHandle)237     static void offset__FFPath(JNIEnv* env, jobject clazz, jlong objHandle, jfloat dx, jfloat dy, jlong dstHandle) {
238         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
239         SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
240         obj->offset(dx, dy, dst);
241     }
242 
offset__FF(JNIEnv * env,jobject clazz,jlong objHandle,jfloat dx,jfloat dy)243     static void offset__FF(JNIEnv* env, jobject clazz, jlong objHandle, jfloat dx, jfloat dy) {
244         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
245         obj->offset(dx, dy);
246     }
247 
setLastPoint(JNIEnv * env,jobject clazz,jlong objHandle,jfloat dx,jfloat dy)248     static void setLastPoint(JNIEnv* env, jobject clazz, jlong objHandle, jfloat dx, jfloat dy) {
249         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
250         obj->setLastPt(dx, dy);
251     }
252 
transform__MatrixPath(JNIEnv * env,jobject clazz,jlong objHandle,jlong matrixHandle,jlong dstHandle)253     static void transform__MatrixPath(JNIEnv* env, jobject clazz, jlong objHandle, jlong matrixHandle, jlong dstHandle) {
254         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
255         SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
256         SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
257         obj->transform(*matrix, dst);
258     }
259 
transform__Matrix(JNIEnv * env,jobject clazz,jlong objHandle,jlong matrixHandle)260     static void transform__Matrix(JNIEnv* env, jobject clazz, jlong objHandle, jlong matrixHandle) {
261         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
262         SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
263         obj->transform(*matrix);
264     }
265 
op(JNIEnv * env,jobject clazz,jlong p1Handle,jlong p2Handle,jint opHandle,jlong rHandle)266     static jboolean op(JNIEnv* env, jobject clazz, jlong p1Handle, jlong p2Handle, jint opHandle, jlong rHandle) {
267         SkPath* p1  = reinterpret_cast<SkPath*>(p1Handle);
268         SkPath* p2  = reinterpret_cast<SkPath*>(p2Handle);
269         SkPathOp op = static_cast<SkPathOp>(opHandle);
270         SkPath* r   = reinterpret_cast<SkPath*>(rHandle);
271         return Op(*p1, *p2, op, r);
272      }
273 
274     typedef SkPoint (*bezierCalculation)(float t, const SkPoint* points);
275 
addMove(std::vector<SkPoint> & segmentPoints,std::vector<float> & lengths,const SkPoint & point)276     static void addMove(std::vector<SkPoint>& segmentPoints, std::vector<float>& lengths,
277             const SkPoint& point) {
278         float length = 0;
279         if (!lengths.empty()) {
280             length = lengths.back();
281         }
282         segmentPoints.push_back(point);
283         lengths.push_back(length);
284     }
285 
addLine(std::vector<SkPoint> & segmentPoints,std::vector<float> & lengths,const SkPoint & toPoint)286     static void addLine(std::vector<SkPoint>& segmentPoints, std::vector<float>& lengths,
287             const SkPoint& toPoint) {
288         if (segmentPoints.empty()) {
289             segmentPoints.push_back(SkPoint::Make(0, 0));
290             lengths.push_back(0);
291         } else if (segmentPoints.back() == toPoint) {
292             return; // Empty line
293         }
294         float length = lengths.back() + SkPoint::Distance(segmentPoints.back(), toPoint);
295         segmentPoints.push_back(toPoint);
296         lengths.push_back(length);
297     }
298 
cubicCoordinateCalculation(float t,float p0,float p1,float p2,float p3)299     static float cubicCoordinateCalculation(float t, float p0, float p1, float p2, float p3) {
300         float oneMinusT = 1 - t;
301         float oneMinusTSquared = oneMinusT * oneMinusT;
302         float oneMinusTCubed = oneMinusTSquared * oneMinusT;
303         float tSquared = t * t;
304         float tCubed = tSquared * t;
305         return (oneMinusTCubed * p0) + (3 * oneMinusTSquared * t * p1)
306                 + (3 * oneMinusT * tSquared * p2) + (tCubed * p3);
307     }
308 
cubicBezierCalculation(float t,const SkPoint * points)309     static SkPoint cubicBezierCalculation(float t, const SkPoint* points) {
310         float x = cubicCoordinateCalculation(t, points[0].x(), points[1].x(),
311             points[2].x(), points[3].x());
312         float y = cubicCoordinateCalculation(t, points[0].y(), points[1].y(),
313             points[2].y(), points[3].y());
314         return SkPoint::Make(x, y);
315     }
316 
quadraticCoordinateCalculation(float t,float p0,float p1,float p2)317     static float quadraticCoordinateCalculation(float t, float p0, float p1, float p2) {
318         float oneMinusT = 1 - t;
319         return oneMinusT * ((oneMinusT * p0) + (t * p1)) + t * ((oneMinusT * p1) + (t * p2));
320     }
321 
quadraticBezierCalculation(float t,const SkPoint * points)322     static SkPoint quadraticBezierCalculation(float t, const SkPoint* points) {
323         float x = quadraticCoordinateCalculation(t, points[0].x(), points[1].x(), points[2].x());
324         float y = quadraticCoordinateCalculation(t, points[0].y(), points[1].y(), points[2].y());
325         return SkPoint::Make(x, y);
326     }
327 
328     // Subdivide a section of the Bezier curve, set the mid-point and the mid-t value.
329     // Returns true if further subdivision is necessary as defined by errorSquared.
subdividePoints(const SkPoint * points,bezierCalculation bezierFunction,float t0,const SkPoint & p0,float t1,const SkPoint & p1,float & midT,SkPoint & midPoint,float errorSquared)330     static bool subdividePoints(const SkPoint* points, bezierCalculation bezierFunction,
331             float t0, const SkPoint &p0, float t1, const SkPoint &p1,
332             float& midT, SkPoint &midPoint, float errorSquared) {
333         midT = (t1 + t0) / 2;
334         float midX = (p1.x() + p0.x()) / 2;
335         float midY = (p1.y() + p0.y()) / 2;
336 
337         midPoint = (*bezierFunction)(midT, points);
338         float xError = midPoint.x() - midX;
339         float yError = midPoint.y() - midY;
340         float midErrorSquared = (xError * xError) + (yError * yError);
341         return midErrorSquared > errorSquared;
342     }
343 
344     // Divides Bezier curves until linear interpolation is very close to accurate, using
345     // errorSquared as a metric. Cubic Bezier curves can have an inflection point that improperly
346     // short-circuit subdivision. If you imagine an S shape, the top and bottom points being the
347     // starting and end points, linear interpolation would mark the center where the curve places
348     // the point. It is clearly not the case that we can linearly interpolate at that point.
349     // doubleCheckDivision forces a second examination between subdivisions to ensure that linear
350     // interpolation works.
addBezier(const SkPoint * points,bezierCalculation bezierFunction,std::vector<SkPoint> & segmentPoints,std::vector<float> & lengths,float errorSquared,bool doubleCheckDivision)351     static void addBezier(const SkPoint* points,
352             bezierCalculation bezierFunction, std::vector<SkPoint>& segmentPoints,
353             std::vector<float>& lengths, float errorSquared, bool doubleCheckDivision) {
354         typedef std::map<float, SkPoint> PointMap;
355         PointMap tToPoint;
356 
357         tToPoint[0] = (*bezierFunction)(0, points);
358         tToPoint[1] = (*bezierFunction)(1, points);
359 
360         PointMap::iterator iter = tToPoint.begin();
361         PointMap::iterator next = iter;
362         ++next;
363         while (next != tToPoint.end()) {
364             bool needsSubdivision = true;
365             SkPoint midPoint;
366             do {
367                 float midT;
368                 needsSubdivision = subdividePoints(points, bezierFunction, iter->first,
369                     iter->second, next->first, next->second, midT, midPoint, errorSquared);
370                 if (!needsSubdivision && doubleCheckDivision) {
371                     SkPoint quarterPoint;
372                     float quarterT;
373                     needsSubdivision = subdividePoints(points, bezierFunction, iter->first,
374                         iter->second, midT, midPoint, quarterT, quarterPoint, errorSquared);
375                     if (needsSubdivision) {
376                         // Found an inflection point. No need to double-check.
377                         doubleCheckDivision = false;
378                     }
379                 }
380                 if (needsSubdivision) {
381                     next = tToPoint.insert(iter, PointMap::value_type(midT, midPoint));
382                 }
383             } while (needsSubdivision);
384             iter = next;
385             next++;
386         }
387 
388         // Now that each division can use linear interpolation with less than the allowed error
389         for (iter = tToPoint.begin(); iter != tToPoint.end(); ++iter) {
390             addLine(segmentPoints, lengths, iter->second);
391         }
392     }
393 
createVerbSegments(SkPath::Verb verb,const SkPoint * points,std::vector<SkPoint> & segmentPoints,std::vector<float> & lengths,float errorSquared)394     static void createVerbSegments(SkPath::Verb verb, const SkPoint* points,
395         std::vector<SkPoint>& segmentPoints, std::vector<float>& lengths, float errorSquared) {
396         switch (verb) {
397             case SkPath::kMove_Verb:
398                 addMove(segmentPoints, lengths, points[0]);
399                 break;
400             case SkPath::kClose_Verb:
401                 addLine(segmentPoints, lengths, points[0]);
402                 break;
403             case SkPath::kLine_Verb:
404                 addLine(segmentPoints, lengths, points[1]);
405                 break;
406             case SkPath::kQuad_Verb:
407                 addBezier(points, quadraticBezierCalculation, segmentPoints, lengths,
408                     errorSquared, false);
409                 break;
410             case SkPath::kCubic_Verb:
411                 addBezier(points, cubicBezierCalculation, segmentPoints, lengths,
412                     errorSquared, true);
413                 break;
414             default:
415                 // Leave element as NULL, Conic sections are not supported.
416                 break;
417         }
418     }
419 
420     // Returns a float[] with each point along the path represented by 3 floats
421     // * fractional length along the path that the point resides
422     // * x coordinate
423     // * y coordinate
424     // Note that more than one point may have the same length along the path in
425     // the case of a move.
426     // NULL can be returned if the Path is empty.
approximate(JNIEnv * env,jclass,jlong pathHandle,float acceptableError)427     static jfloatArray approximate(JNIEnv* env, jclass, jlong pathHandle, float acceptableError)
428     {
429         SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
430         SkASSERT(path);
431         SkPath::Iter pathIter(*path, false);
432         SkPath::Verb verb;
433         SkPoint points[4];
434         std::vector<SkPoint> segmentPoints;
435         std::vector<float> lengths;
436         float errorSquared = acceptableError * acceptableError;
437 
438         while ((verb = pathIter.next(points, false)) != SkPath::kDone_Verb) {
439             createVerbSegments(verb, points, segmentPoints, lengths, errorSquared);
440         }
441 
442         if (segmentPoints.empty()) {
443             int numVerbs = path->countVerbs();
444             if (numVerbs == 1) {
445                 addMove(segmentPoints, lengths, path->getPoint(0));
446             } else {
447                 // Invalid or empty path. Fall back to point(0,0)
448                 addMove(segmentPoints, lengths, SkPoint());
449             }
450         }
451 
452         float totalLength = lengths.back();
453         if (totalLength == 0) {
454             // Lone Move instructions should still be able to animate at the same value.
455             segmentPoints.push_back(segmentPoints.back());
456             lengths.push_back(1);
457             totalLength = 1;
458         }
459 
460         size_t numPoints = segmentPoints.size();
461         size_t approximationArraySize = numPoints * 3;
462 
463         float* approximation = new float[approximationArraySize];
464 
465         int approximationIndex = 0;
466         for (size_t i = 0; i < numPoints; i++) {
467             const SkPoint& point = segmentPoints[i];
468             approximation[approximationIndex++] = lengths[i] / totalLength;
469             approximation[approximationIndex++] = point.x();
470             approximation[approximationIndex++] = point.y();
471         }
472 
473         jfloatArray result = env->NewFloatArray(approximationArraySize);
474         env->SetFloatArrayRegion(result, 0, approximationArraySize, approximation);
475         delete[] approximation;
476         return result;
477     }
478 };
479 
480 static JNINativeMethod methods[] = {
481     {"finalizer", "(J)V", (void*) SkPathGlue::finalizer},
482     {"init1","()J", (void*) SkPathGlue::init1},
483     {"init2","(J)J", (void*) SkPathGlue::init2},
484     {"native_reset","(J)V", (void*) SkPathGlue::reset},
485     {"native_rewind","(J)V", (void*) SkPathGlue::rewind},
486     {"native_set","(JJ)V", (void*) SkPathGlue::assign},
487     {"native_isConvex","(J)Z", (void*) SkPathGlue::isConvex},
488     {"native_getFillType","(J)I", (void*) SkPathGlue::getFillType},
489     {"native_setFillType","(JI)V", (void*) SkPathGlue::setFillType},
490     {"native_isEmpty","(J)Z", (void*) SkPathGlue::isEmpty},
491     {"native_isRect","(JLandroid/graphics/RectF;)Z", (void*) SkPathGlue::isRect},
492     {"native_computeBounds","(JLandroid/graphics/RectF;)V", (void*) SkPathGlue::computeBounds},
493     {"native_incReserve","(JI)V", (void*) SkPathGlue::incReserve},
494     {"native_moveTo","(JFF)V", (void*) SkPathGlue::moveTo__FF},
495     {"native_rMoveTo","(JFF)V", (void*) SkPathGlue::rMoveTo},
496     {"native_lineTo","(JFF)V", (void*) SkPathGlue::lineTo__FF},
497     {"native_rLineTo","(JFF)V", (void*) SkPathGlue::rLineTo},
498     {"native_quadTo","(JFFFF)V", (void*) SkPathGlue::quadTo__FFFF},
499     {"native_rQuadTo","(JFFFF)V", (void*) SkPathGlue::rQuadTo},
500     {"native_cubicTo","(JFFFFFF)V", (void*) SkPathGlue::cubicTo__FFFFFF},
501     {"native_rCubicTo","(JFFFFFF)V", (void*) SkPathGlue::rCubicTo},
502     {"native_arcTo","(JFFFFFFZ)V", (void*) SkPathGlue::arcTo},
503     {"native_close","(J)V", (void*) SkPathGlue::close},
504     {"native_addRect","(JFFFFI)V", (void*) SkPathGlue::addRect},
505     {"native_addOval","(JFFFFI)V", (void*) SkPathGlue::addOval},
506     {"native_addCircle","(JFFFI)V", (void*) SkPathGlue::addCircle},
507     {"native_addArc","(JFFFFFF)V", (void*) SkPathGlue::addArc},
508     {"native_addRoundRect","(JFFFFFFI)V", (void*) SkPathGlue::addRoundRectXY},
509     {"native_addRoundRect","(JFFFF[FI)V", (void*) SkPathGlue::addRoundRect8},
510     {"native_addPath","(JJFF)V", (void*) SkPathGlue::addPath__PathFF},
511     {"native_addPath","(JJ)V", (void*) SkPathGlue::addPath__Path},
512     {"native_addPath","(JJJ)V", (void*) SkPathGlue::addPath__PathMatrix},
513     {"native_offset","(JFFJ)V", (void*) SkPathGlue::offset__FFPath},
514     {"native_offset","(JFF)V", (void*) SkPathGlue::offset__FF},
515     {"native_setLastPoint","(JFF)V", (void*) SkPathGlue::setLastPoint},
516     {"native_transform","(JJJ)V", (void*) SkPathGlue::transform__MatrixPath},
517     {"native_transform","(JJ)V", (void*) SkPathGlue::transform__Matrix},
518     {"native_op","(JJIJ)Z", (void*) SkPathGlue::op},
519     {"native_approximate", "(JF)[F", (void*) SkPathGlue::approximate},
520 };
521 
register_android_graphics_Path(JNIEnv * env)522 int register_android_graphics_Path(JNIEnv* env) {
523     int result = AndroidRuntime::registerNativeMethods(env, "android/graphics/Path", methods,
524         sizeof(methods) / sizeof(methods[0]));
525     return result;
526 }
527 
528 }
529