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