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