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