• 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 "core_jni_helpers.h"
26 
27 #include "SkPath.h"
28 #include "SkPathOps.h"
29 #include "SkGeometry.h" // WARNING: Internal Skia Header
30 
31 #include <Caches.h>
32 #include <vector>
33 #include <map>
34 
35 namespace android {
36 
37 class SkPathGlue {
38 public:
39 
40     // ---------------- Regular JNI -----------------------------
41 
init(JNIEnv * env,jclass clazz)42     static jlong init(JNIEnv* env, jclass clazz) {
43         return reinterpret_cast<jlong>(new SkPath());
44     }
45 
init_Path(JNIEnv * env,jclass clazz,jlong valHandle)46     static jlong init_Path(JNIEnv* env, jclass clazz, jlong valHandle) {
47         SkPath* val = reinterpret_cast<SkPath*>(valHandle);
48         return reinterpret_cast<jlong>(new SkPath(*val));
49     }
50 
finalize(JNIEnv * env,jclass clazz,jlong objHandle)51     static void finalize(JNIEnv* env, jclass clazz, jlong objHandle) {
52         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
53         // Purge entries from the HWUI path cache if this path's data is unique
54         if (obj->unique() && android::uirenderer::Caches::hasInstance()) {
55             android::uirenderer::Caches::getInstance().pathCache.removeDeferred(obj);
56         }
57         delete obj;
58     }
59 
set(JNIEnv * env,jclass clazz,jlong dstHandle,jlong srcHandle)60     static void set(JNIEnv* env, jclass clazz, jlong dstHandle, jlong srcHandle) {
61         SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
62         const SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
63         *dst = *src;
64     }
65 
computeBounds(JNIEnv * env,jclass clazz,jlong objHandle,jobject jbounds)66     static void computeBounds(JNIEnv* env, jclass clazz, jlong objHandle, jobject jbounds) {
67         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
68         const SkRect& bounds = obj->getBounds();
69         GraphicsJNI::rect_to_jrectf(bounds, env, jbounds);
70     }
71 
incReserve(JNIEnv * env,jclass clazz,jlong objHandle,jint extraPtCount)72     static void incReserve(JNIEnv* env, jclass clazz, jlong objHandle, jint extraPtCount) {
73         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
74         obj->incReserve(extraPtCount);
75     }
76 
moveTo__FF(JNIEnv * env,jclass clazz,jlong objHandle,jfloat x,jfloat y)77     static void moveTo__FF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x, jfloat y) {
78         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
79         obj->moveTo(x, y);
80     }
81 
rMoveTo(JNIEnv * env,jclass clazz,jlong objHandle,jfloat dx,jfloat dy)82     static void rMoveTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
83         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
84         obj->rMoveTo(dx, dy);
85     }
86 
lineTo__FF(JNIEnv * env,jclass clazz,jlong objHandle,jfloat x,jfloat y)87     static void lineTo__FF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x, jfloat y) {
88         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
89         obj->lineTo(x, y);
90     }
91 
rLineTo(JNIEnv * env,jclass clazz,jlong objHandle,jfloat dx,jfloat dy)92     static void rLineTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
93         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
94         obj->rLineTo(dx, dy);
95     }
96 
quadTo__FFFF(JNIEnv * env,jclass clazz,jlong objHandle,jfloat x1,jfloat y1,jfloat x2,jfloat y2)97     static void quadTo__FFFF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x1, jfloat y1,
98             jfloat x2, jfloat y2) {
99         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
100         obj->quadTo(x1, y1, x2, y2);
101     }
102 
rQuadTo(JNIEnv * env,jclass clazz,jlong objHandle,jfloat dx1,jfloat dy1,jfloat dx2,jfloat dy2)103     static void rQuadTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx1, jfloat dy1,
104             jfloat dx2, jfloat dy2) {
105         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
106         obj->rQuadTo(dx1, dy1, dx2, dy2);
107     }
108 
cubicTo__FFFFFF(JNIEnv * env,jclass clazz,jlong objHandle,jfloat x1,jfloat y1,jfloat x2,jfloat y2,jfloat x3,jfloat y3)109     static void cubicTo__FFFFFF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x1, jfloat y1,
110             jfloat x2, jfloat y2, jfloat x3, jfloat y3) {
111         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
112         obj->cubicTo(x1, y1, x2, y2, x3, y3);
113     }
114 
rCubicTo(JNIEnv * env,jclass clazz,jlong objHandle,jfloat x1,jfloat y1,jfloat x2,jfloat y2,jfloat x3,jfloat y3)115     static void rCubicTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x1, jfloat y1,
116             jfloat x2, jfloat y2, jfloat x3, jfloat y3) {
117         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
118         obj->rCubicTo(x1, y1, x2, y2, x3, y3);
119     }
120 
arcTo(JNIEnv * env,jclass clazz,jlong objHandle,jfloat left,jfloat top,jfloat right,jfloat bottom,jfloat startAngle,jfloat sweepAngle,jboolean forceMoveTo)121     static void arcTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
122             jfloat right, jfloat bottom, jfloat startAngle, jfloat sweepAngle,
123             jboolean forceMoveTo) {
124         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
125         SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
126         obj->arcTo(oval, startAngle, sweepAngle, forceMoveTo);
127     }
128 
close(JNIEnv * env,jclass clazz,jlong objHandle)129     static void close(JNIEnv* env, jclass clazz, jlong objHandle) {
130         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
131         obj->close();
132     }
133 
addRect(JNIEnv * env,jclass clazz,jlong objHandle,jfloat left,jfloat top,jfloat right,jfloat bottom,jint dirHandle)134     static void addRect(JNIEnv* env, jclass clazz, jlong objHandle,
135             jfloat left, jfloat top, jfloat right, jfloat bottom, jint dirHandle) {
136         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
137         SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle);
138         obj->addRect(left, top, right, bottom, dir);
139     }
140 
addOval(JNIEnv * env,jclass clazz,jlong objHandle,jfloat left,jfloat top,jfloat right,jfloat bottom,jint dirHandle)141     static void addOval(JNIEnv* env, jclass clazz, jlong objHandle,
142             jfloat left, jfloat top, jfloat right, jfloat bottom, jint dirHandle) {
143         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
144         SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle);
145         SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
146         obj->addOval(oval, dir);
147     }
148 
addCircle(JNIEnv * env,jclass clazz,jlong objHandle,jfloat x,jfloat y,jfloat radius,jint dirHandle)149     static void addCircle(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x, jfloat y,
150             jfloat radius, jint dirHandle) {
151         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
152         SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle);
153         obj->addCircle(x, y, radius, dir);
154     }
155 
addArc(JNIEnv * env,jclass clazz,jlong objHandle,jfloat left,jfloat top,jfloat right,jfloat bottom,jfloat startAngle,jfloat sweepAngle)156     static void addArc(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
157             jfloat right, jfloat bottom, jfloat startAngle, jfloat sweepAngle) {
158         SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
159         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
160         obj->addArc(oval, startAngle, sweepAngle);
161     }
162 
addRoundRectXY(JNIEnv * env,jclass clazz,jlong objHandle,jfloat left,jfloat top,jfloat right,jfloat bottom,jfloat rx,jfloat ry,jint dirHandle)163     static void addRoundRectXY(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
164             jfloat right, jfloat bottom, jfloat rx, jfloat ry, jint dirHandle) {
165         SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
166         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
167         SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle);
168         obj->addRoundRect(rect, rx, ry, dir);
169     }
170 
addRoundRect8(JNIEnv * env,jclass clazz,jlong objHandle,jfloat left,jfloat top,jfloat right,jfloat bottom,jfloatArray array,jint dirHandle)171     static void addRoundRect8(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
172                 jfloat right, jfloat bottom, jfloatArray array, jint dirHandle) {
173         SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
174         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
175         SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle);
176         AutoJavaFloatArray  afa(env, array, 8);
177 #ifdef SK_SCALAR_IS_FLOAT
178         const float* src = afa.ptr();
179 #else
180         #error Need to convert float array to SkScalar array before calling the following function.
181 #endif
182         obj->addRoundRect(rect, src, dir);
183     }
184 
addPath__PathFF(JNIEnv * env,jclass clazz,jlong objHandle,jlong srcHandle,jfloat dx,jfloat dy)185     static void addPath__PathFF(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle,
186             jfloat dx, jfloat dy) {
187         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
188         SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
189         obj->addPath(*src, dx, dy);
190     }
191 
addPath__Path(JNIEnv * env,jclass clazz,jlong objHandle,jlong srcHandle)192     static void addPath__Path(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle) {
193         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
194         SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
195         obj->addPath(*src);
196     }
197 
addPath__PathMatrix(JNIEnv * env,jclass clazz,jlong objHandle,jlong srcHandle,jlong matrixHandle)198     static void addPath__PathMatrix(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle,
199             jlong matrixHandle) {
200         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
201         SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
202         SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
203         obj->addPath(*src, *matrix);
204     }
205 
offset__FF(JNIEnv * env,jclass clazz,jlong objHandle,jfloat dx,jfloat dy)206     static void offset__FF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
207         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
208         obj->offset(dx, dy);
209     }
210 
setLastPoint(JNIEnv * env,jclass clazz,jlong objHandle,jfloat dx,jfloat dy)211     static void setLastPoint(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
212         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
213         obj->setLastPt(dx, dy);
214     }
215 
transform__MatrixPath(JNIEnv * env,jclass clazz,jlong objHandle,jlong matrixHandle,jlong dstHandle)216     static void transform__MatrixPath(JNIEnv* env, jclass clazz, jlong objHandle, jlong matrixHandle,
217             jlong dstHandle) {
218         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
219         SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
220         SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
221         obj->transform(*matrix, dst);
222     }
223 
transform__Matrix(JNIEnv * env,jclass clazz,jlong objHandle,jlong matrixHandle)224     static void transform__Matrix(JNIEnv* env, jclass clazz, jlong objHandle, jlong matrixHandle) {
225         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
226         SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
227         obj->transform(*matrix);
228     }
229 
op(JNIEnv * env,jclass clazz,jlong p1Handle,jlong p2Handle,jint opHandle,jlong rHandle)230     static jboolean op(JNIEnv* env, jclass clazz, jlong p1Handle, jlong p2Handle, jint opHandle,
231             jlong rHandle) {
232         SkPath* p1  = reinterpret_cast<SkPath*>(p1Handle);
233         SkPath* p2  = reinterpret_cast<SkPath*>(p2Handle);
234         SkPathOp op = static_cast<SkPathOp>(opHandle);
235         SkPath* r   = reinterpret_cast<SkPath*>(rHandle);
236         return Op(*p1, *p2, op, r);
237      }
238 
239     typedef SkPoint (*bezierCalculation)(float t, const SkPoint* points);
240 
addMove(std::vector<SkPoint> & segmentPoints,std::vector<float> & lengths,const SkPoint & point)241     static void addMove(std::vector<SkPoint>& segmentPoints, std::vector<float>& lengths,
242             const SkPoint& point) {
243         float length = 0;
244         if (!lengths.empty()) {
245             length = lengths.back();
246         }
247         segmentPoints.push_back(point);
248         lengths.push_back(length);
249     }
250 
addLine(std::vector<SkPoint> & segmentPoints,std::vector<float> & lengths,const SkPoint & toPoint)251     static void addLine(std::vector<SkPoint>& segmentPoints, std::vector<float>& lengths,
252             const SkPoint& toPoint) {
253         if (segmentPoints.empty()) {
254             segmentPoints.push_back(SkPoint::Make(0, 0));
255             lengths.push_back(0);
256         } else if (segmentPoints.back() == toPoint) {
257             return; // Empty line
258         }
259         float length = lengths.back() + SkPoint::Distance(segmentPoints.back(), toPoint);
260         segmentPoints.push_back(toPoint);
261         lengths.push_back(length);
262     }
263 
cubicCoordinateCalculation(float t,float p0,float p1,float p2,float p3)264     static float cubicCoordinateCalculation(float t, float p0, float p1, float p2, float p3) {
265         float oneMinusT = 1 - t;
266         float oneMinusTSquared = oneMinusT * oneMinusT;
267         float oneMinusTCubed = oneMinusTSquared * oneMinusT;
268         float tSquared = t * t;
269         float tCubed = tSquared * t;
270         return (oneMinusTCubed * p0) + (3 * oneMinusTSquared * t * p1)
271                 + (3 * oneMinusT * tSquared * p2) + (tCubed * p3);
272     }
273 
cubicBezierCalculation(float t,const SkPoint * points)274     static SkPoint cubicBezierCalculation(float t, const SkPoint* points) {
275         float x = cubicCoordinateCalculation(t, points[0].x(), points[1].x(),
276             points[2].x(), points[3].x());
277         float y = cubicCoordinateCalculation(t, points[0].y(), points[1].y(),
278             points[2].y(), points[3].y());
279         return SkPoint::Make(x, y);
280     }
281 
quadraticCoordinateCalculation(float t,float p0,float p1,float p2)282     static float quadraticCoordinateCalculation(float t, float p0, float p1, float p2) {
283         float oneMinusT = 1 - t;
284         return oneMinusT * ((oneMinusT * p0) + (t * p1)) + t * ((oneMinusT * p1) + (t * p2));
285     }
286 
quadraticBezierCalculation(float t,const SkPoint * points)287     static SkPoint quadraticBezierCalculation(float t, const SkPoint* points) {
288         float x = quadraticCoordinateCalculation(t, points[0].x(), points[1].x(), points[2].x());
289         float y = quadraticCoordinateCalculation(t, points[0].y(), points[1].y(), points[2].y());
290         return SkPoint::Make(x, y);
291     }
292 
293     // Subdivide a section of the Bezier curve, set the mid-point and the mid-t value.
294     // 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)295     static bool subdividePoints(const SkPoint* points, bezierCalculation bezierFunction,
296             float t0, const SkPoint &p0, float t1, const SkPoint &p1,
297             float& midT, SkPoint &midPoint, float errorSquared) {
298         midT = (t1 + t0) / 2;
299         float midX = (p1.x() + p0.x()) / 2;
300         float midY = (p1.y() + p0.y()) / 2;
301 
302         midPoint = (*bezierFunction)(midT, points);
303         float xError = midPoint.x() - midX;
304         float yError = midPoint.y() - midY;
305         float midErrorSquared = (xError * xError) + (yError * yError);
306         return midErrorSquared > errorSquared;
307     }
308 
309     // Divides Bezier curves until linear interpolation is very close to accurate, using
310     // errorSquared as a metric. Cubic Bezier curves can have an inflection point that improperly
311     // short-circuit subdivision. If you imagine an S shape, the top and bottom points being the
312     // starting and end points, linear interpolation would mark the center where the curve places
313     // the point. It is clearly not the case that we can linearly interpolate at that point.
314     // doubleCheckDivision forces a second examination between subdivisions to ensure that linear
315     // interpolation works.
addBezier(const SkPoint * points,bezierCalculation bezierFunction,std::vector<SkPoint> & segmentPoints,std::vector<float> & lengths,float errorSquared,bool doubleCheckDivision)316     static void addBezier(const SkPoint* points,
317             bezierCalculation bezierFunction, std::vector<SkPoint>& segmentPoints,
318             std::vector<float>& lengths, float errorSquared, bool doubleCheckDivision) {
319         typedef std::map<float, SkPoint> PointMap;
320         PointMap tToPoint;
321 
322         tToPoint[0] = (*bezierFunction)(0, points);
323         tToPoint[1] = (*bezierFunction)(1, points);
324 
325         PointMap::iterator iter = tToPoint.begin();
326         PointMap::iterator next = iter;
327         ++next;
328         while (next != tToPoint.end()) {
329             bool needsSubdivision = true;
330             SkPoint midPoint;
331             do {
332                 float midT;
333                 needsSubdivision = subdividePoints(points, bezierFunction, iter->first,
334                     iter->second, next->first, next->second, midT, midPoint, errorSquared);
335                 if (!needsSubdivision && doubleCheckDivision) {
336                     SkPoint quarterPoint;
337                     float quarterT;
338                     needsSubdivision = subdividePoints(points, bezierFunction, iter->first,
339                         iter->second, midT, midPoint, quarterT, quarterPoint, errorSquared);
340                     if (needsSubdivision) {
341                         // Found an inflection point. No need to double-check.
342                         doubleCheckDivision = false;
343                     }
344                 }
345                 if (needsSubdivision) {
346                     next = tToPoint.insert(iter, PointMap::value_type(midT, midPoint));
347                 }
348             } while (needsSubdivision);
349             iter = next;
350             next++;
351         }
352 
353         // Now that each division can use linear interpolation with less than the allowed error
354         for (iter = tToPoint.begin(); iter != tToPoint.end(); ++iter) {
355             addLine(segmentPoints, lengths, iter->second);
356         }
357     }
358 
createVerbSegments(const SkPath::Iter & pathIter,SkPath::Verb verb,const SkPoint * points,std::vector<SkPoint> & segmentPoints,std::vector<float> & lengths,float errorSquared,float errorConic)359     static void createVerbSegments(const SkPath::Iter& pathIter, SkPath::Verb verb,
360             const SkPoint* points, std::vector<SkPoint>& segmentPoints,
361             std::vector<float>& lengths, float errorSquared, float errorConic) {
362         switch (verb) {
363             case SkPath::kMove_Verb:
364                 addMove(segmentPoints, lengths, points[0]);
365                 break;
366             case SkPath::kClose_Verb:
367                 addLine(segmentPoints, lengths, points[0]);
368                 break;
369             case SkPath::kLine_Verb:
370                 addLine(segmentPoints, lengths, points[1]);
371                 break;
372             case SkPath::kQuad_Verb:
373                 addBezier(points, quadraticBezierCalculation, segmentPoints, lengths,
374                     errorSquared, false);
375                 break;
376             case SkPath::kCubic_Verb:
377                 addBezier(points, cubicBezierCalculation, segmentPoints, lengths,
378                     errorSquared, true);
379                 break;
380             case SkPath::kConic_Verb: {
381                 SkAutoConicToQuads converter;
382                 const SkPoint* quads = converter.computeQuads(
383                         points, pathIter.conicWeight(), errorConic);
384                 for (int i = 0; i < converter.countQuads(); i++) {
385                     // Note: offset each subsequent quad by 2, since end points are shared
386                     const SkPoint* quad = quads + i * 2;
387                     addBezier(quad, quadraticBezierCalculation, segmentPoints, lengths,
388                         errorConic, false);
389                 }
390                 break;
391             }
392             default:
393                 static_assert(SkPath::kMove_Verb == 0
394                                 && SkPath::kLine_Verb == 1
395                                 && SkPath::kQuad_Verb == 2
396                                 && SkPath::kConic_Verb == 3
397                                 && SkPath::kCubic_Verb == 4
398                                 && SkPath::kClose_Verb == 5
399                                 && SkPath::kDone_Verb == 6,
400                         "Path enum changed, new types may have been added.");
401                 break;
402         }
403     }
404 
405     // Returns a float[] with each point along the path represented by 3 floats
406     // * fractional length along the path that the point resides
407     // * x coordinate
408     // * y coordinate
409     // Note that more than one point may have the same length along the path in
410     // the case of a move.
411     // NULL can be returned if the Path is empty.
approximate(JNIEnv * env,jclass clazz,jlong pathHandle,float acceptableError)412     static jfloatArray approximate(JNIEnv* env, jclass clazz, jlong pathHandle,
413             float acceptableError) {
414         SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
415         SkASSERT(path);
416         SkPath::Iter pathIter(*path, false);
417         SkPath::Verb verb;
418         SkPoint points[4];
419         std::vector<SkPoint> segmentPoints;
420         std::vector<float> lengths;
421         float errorSquared = acceptableError * acceptableError;
422         float errorConic = acceptableError / 2; // somewhat arbitrary
423 
424         while ((verb = pathIter.next(points, false)) != SkPath::kDone_Verb) {
425             createVerbSegments(pathIter, verb, points, segmentPoints, lengths,
426                     errorSquared, errorConic);
427         }
428 
429         if (segmentPoints.empty()) {
430             int numVerbs = path->countVerbs();
431             if (numVerbs == 1) {
432                 addMove(segmentPoints, lengths, path->getPoint(0));
433             } else {
434                 // Invalid or empty path. Fall back to point(0,0)
435                 addMove(segmentPoints, lengths, SkPoint());
436             }
437         }
438 
439         float totalLength = lengths.back();
440         if (totalLength == 0) {
441             // Lone Move instructions should still be able to animate at the same value.
442             segmentPoints.push_back(segmentPoints.back());
443             lengths.push_back(1);
444             totalLength = 1;
445         }
446 
447         size_t numPoints = segmentPoints.size();
448         size_t approximationArraySize = numPoints * 3;
449 
450         float* approximation = new float[approximationArraySize];
451 
452         int approximationIndex = 0;
453         for (size_t i = 0; i < numPoints; i++) {
454             const SkPoint& point = segmentPoints[i];
455             approximation[approximationIndex++] = lengths[i] / totalLength;
456             approximation[approximationIndex++] = point.x();
457             approximation[approximationIndex++] = point.y();
458         }
459 
460         jfloatArray result = env->NewFloatArray(approximationArraySize);
461         env->SetFloatArrayRegion(result, 0, approximationArraySize, approximation);
462         delete[] approximation;
463         return result;
464     }
465 
466     // ---------------- @FastNative -----------------------------
467 
isRect(JNIEnv * env,jclass clazz,jlong objHandle,jobject jrect)468     static jboolean isRect(JNIEnv* env, jclass clazz, jlong objHandle, jobject jrect) {
469         SkRect rect;
470         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
471         jboolean result = obj->isRect(&rect);
472         GraphicsJNI::rect_to_jrectf(rect, env, jrect);
473         return result;
474     }
475 
476     // ---------------- @CriticalNative -------------------------
477 
reset(jlong objHandle)478     static void reset(jlong objHandle) {
479         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
480         obj->reset();
481     }
482 
rewind(jlong objHandle)483     static void rewind(jlong objHandle) {
484         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
485         obj->rewind();
486     }
487 
isEmpty(jlong objHandle)488     static jboolean isEmpty(jlong objHandle) {
489         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
490         return obj->isEmpty();
491     }
492 
isConvex(jlong objHandle)493     static jboolean isConvex(jlong objHandle) {
494         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
495         return obj->isConvex();
496     }
497 
getFillType(jlong objHandle)498     static jint getFillType(jlong objHandle) {
499         SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
500         return obj->getFillType();
501     }
502 
setFillType(jlong pathHandle,jint ftHandle)503     static void setFillType(jlong pathHandle, jint ftHandle) {;
504         SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
505         SkPath::FillType ft = static_cast<SkPath::FillType>(ftHandle);
506         path->setFillType(ft);
507     }
508 };
509 
510 static const JNINativeMethod methods[] = {
511     {"nInit","()J", (void*) SkPathGlue::init},
512     {"nInit","(J)J", (void*) SkPathGlue::init_Path},
513     {"nFinalize", "(J)V", (void*) SkPathGlue::finalize},
514     {"nSet","(JJ)V", (void*) SkPathGlue::set},
515     {"nComputeBounds","(JLandroid/graphics/RectF;)V", (void*) SkPathGlue::computeBounds},
516     {"nIncReserve","(JI)V", (void*) SkPathGlue::incReserve},
517     {"nMoveTo","(JFF)V", (void*) SkPathGlue::moveTo__FF},
518     {"nRMoveTo","(JFF)V", (void*) SkPathGlue::rMoveTo},
519     {"nLineTo","(JFF)V", (void*) SkPathGlue::lineTo__FF},
520     {"nRLineTo","(JFF)V", (void*) SkPathGlue::rLineTo},
521     {"nQuadTo","(JFFFF)V", (void*) SkPathGlue::quadTo__FFFF},
522     {"nRQuadTo","(JFFFF)V", (void*) SkPathGlue::rQuadTo},
523     {"nCubicTo","(JFFFFFF)V", (void*) SkPathGlue::cubicTo__FFFFFF},
524     {"nRCubicTo","(JFFFFFF)V", (void*) SkPathGlue::rCubicTo},
525     {"nArcTo","(JFFFFFFZ)V", (void*) SkPathGlue::arcTo},
526     {"nClose","(J)V", (void*) SkPathGlue::close},
527     {"nAddRect","(JFFFFI)V", (void*) SkPathGlue::addRect},
528     {"nAddOval","(JFFFFI)V", (void*) SkPathGlue::addOval},
529     {"nAddCircle","(JFFFI)V", (void*) SkPathGlue::addCircle},
530     {"nAddArc","(JFFFFFF)V", (void*) SkPathGlue::addArc},
531     {"nAddRoundRect","(JFFFFFFI)V", (void*) SkPathGlue::addRoundRectXY},
532     {"nAddRoundRect","(JFFFF[FI)V", (void*) SkPathGlue::addRoundRect8},
533     {"nAddPath","(JJFF)V", (void*) SkPathGlue::addPath__PathFF},
534     {"nAddPath","(JJ)V", (void*) SkPathGlue::addPath__Path},
535     {"nAddPath","(JJJ)V", (void*) SkPathGlue::addPath__PathMatrix},
536     {"nOffset","(JFF)V", (void*) SkPathGlue::offset__FF},
537     {"nSetLastPoint","(JFF)V", (void*) SkPathGlue::setLastPoint},
538     {"nTransform","(JJJ)V", (void*) SkPathGlue::transform__MatrixPath},
539     {"nTransform","(JJ)V", (void*) SkPathGlue::transform__Matrix},
540     {"nOp","(JJIJ)Z", (void*) SkPathGlue::op},
541     {"nApproximate", "(JF)[F", (void*) SkPathGlue::approximate},
542 
543     // ------- @FastNative below here ----------------------
544     {"nIsRect","(JLandroid/graphics/RectF;)Z", (void*) SkPathGlue::isRect},
545 
546     // ------- @CriticalNative below here ------------------
547     {"nReset","(J)V", (void*) SkPathGlue::reset},
548     {"nRewind","(J)V", (void*) SkPathGlue::rewind},
549     {"nIsEmpty","(J)Z", (void*) SkPathGlue::isEmpty},
550     {"nIsConvex","(J)Z", (void*) SkPathGlue::isConvex},
551     {"nGetFillType","(J)I", (void*) SkPathGlue::getFillType},
552     {"nSetFillType","(JI)V", (void*) SkPathGlue::setFillType},
553 };
554 
register_android_graphics_Path(JNIEnv * env)555 int register_android_graphics_Path(JNIEnv* env) {
556     return RegisterMethodsOrDie(env, "android/graphics/Path", methods, NELEM(methods));
557 
558     static_assert(0  == SkPath::kCW_Direction,  "direction_mismatch");
559     static_assert(1  == SkPath::kCCW_Direction, "direction_mismatch");
560 }
561 
562 }
563