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