blob: 481445258e3c29e17ea230f2c8dcf04c5bd261b6 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* libs/android_runtime/android/graphics/Path.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
Antonio Calabrese24609582014-06-12 16:32:03 -07005** 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008**
Antonio Calabrese24609582014-06-12 16:32:03 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010**
Antonio Calabrese24609582014-06-12 16:32:03 -070011** 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015** 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.
Antonio Calabrese24609582014-06-12 16:32:03 -070020// To change this file, either edit the include, or device/tools/gluemaker/main.cpp,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021// or one of the auxilary file specifications in device/tools/gluemaker.
22
23#include "jni.h"
24#include "GraphicsJNI.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080025#include "core_jni_helpers.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27#include "SkPath.h"
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050028#include "SkPathOps.h"
Chris Craik96202d52017-04-27 15:26:17 -070029#include "SkGeometry.h" // WARNING: Internal Skia Header
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
George Mountc96c7b22013-08-23 13:31:31 -070031#include <vector>
32#include <map>
Romain Guya2341a92010-09-08 18:04:33 -070033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034namespace android {
35
36class SkPathGlue {
37public:
38
John Reck205d83e2018-03-28 14:36:58 -070039 static void finalizer(SkPath* obj) {
John Reck205d83e2018-03-28 14:36:58 -070040 delete obj;
41 }
42
Chris Craik2414e1b2016-12-12 13:56:15 -080043 // ---------------- Regular JNI -----------------------------
44
45 static jlong init(JNIEnv* env, jclass clazz) {
46 return reinterpret_cast<jlong>(new SkPath());
47 }
48
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
John Reck205d83e2018-03-28 14:36:58 -070054 static jlong getFinalizer(JNIEnv* env, jclass clazz) {
55 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&finalizer));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 }
57
Chris Craik2414e1b2016-12-12 13:56:15 -080058 static void set(JNIEnv* env, jclass clazz, jlong dstHandle, jlong srcHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000059 SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
60 const SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 *dst = *src;
62 }
Ashok Bhat36bef0b2014-01-20 20:08:01 +000063
Chris Craik2414e1b2016-12-12 13:56:15 -080064 static void computeBounds(JNIEnv* env, jclass clazz, jlong objHandle, jobject jbounds) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000065 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040066 const SkRect& bounds = obj->getBounds();
67 GraphicsJNI::rect_to_jrectf(bounds, env, jbounds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 }
Antonio Calabrese24609582014-06-12 16:32:03 -070069
Chris Craik2414e1b2016-12-12 13:56:15 -080070 static void incReserve(JNIEnv* env, jclass clazz, jlong objHandle, jint extraPtCount) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000071 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 obj->incReserve(extraPtCount);
73 }
Antonio Calabrese24609582014-06-12 16:32:03 -070074
Chris Craik2414e1b2016-12-12 13:56:15 -080075 static void moveTo__FF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x, jfloat y) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000076 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040077 obj->moveTo(x, y);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 }
Antonio Calabrese24609582014-06-12 16:32:03 -070079
Chris Craik2414e1b2016-12-12 13:56:15 -080080 static void rMoveTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000081 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040082 obj->rMoveTo(dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 }
Antonio Calabrese24609582014-06-12 16:32:03 -070084
Chris Craik2414e1b2016-12-12 13:56:15 -080085 static void lineTo__FF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x, jfloat y) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000086 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040087 obj->lineTo(x, y);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 }
Antonio Calabrese24609582014-06-12 16:32:03 -070089
Chris Craik2414e1b2016-12-12 13:56:15 -080090 static void rLineTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000091 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040092 obj->rLineTo(dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 }
Antonio Calabrese24609582014-06-12 16:32:03 -070094
Chris Craik2414e1b2016-12-12 13:56:15 -080095 static void quadTo__FFFF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x1, jfloat y1,
96 jfloat x2, jfloat y2) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000097 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040098 obj->quadTo(x1, y1, x2, y2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700100
Chris Craik2414e1b2016-12-12 13:56:15 -0800101 static void rQuadTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx1, jfloat dy1,
102 jfloat dx2, jfloat dy2) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000103 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400104 obj->rQuadTo(dx1, dy1, dx2, dy2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700106
Chris Craik2414e1b2016-12-12 13:56:15 -0800107 static void cubicTo__FFFFFF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x1, jfloat y1,
108 jfloat x2, jfloat y2, jfloat x3, jfloat y3) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000109 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400110 obj->cubicTo(x1, y1, x2, y2, x3, y3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700112
Chris Craik2414e1b2016-12-12 13:56:15 -0800113 static void rCubicTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x1, jfloat y1,
114 jfloat x2, jfloat y2, jfloat x3, jfloat y3) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000115 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400116 obj->rCubicTo(x1, y1, x2, y2, x3, y3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700118
Chris Craik2414e1b2016-12-12 13:56:15 -0800119 static void arcTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
Antonio Calabrese24609582014-06-12 16:32:03 -0700120 jfloat right, jfloat bottom, jfloat startAngle, jfloat sweepAngle,
121 jboolean forceMoveTo) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000122 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Antonio Calabrese24609582014-06-12 16:32:03 -0700123 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
124 obj->arcTo(oval, startAngle, sweepAngle, forceMoveTo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700126
Chris Craik2414e1b2016-12-12 13:56:15 -0800127 static void close(JNIEnv* env, jclass clazz, jlong objHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000128 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 obj->close();
130 }
Chris Craik79793882014-04-25 13:08:25 -0700131
Chris Craik2414e1b2016-12-12 13:56:15 -0800132 static void addRect(JNIEnv* env, jclass clazz, jlong objHandle,
Chris Craik79793882014-04-25 13:08:25 -0700133 jfloat left, jfloat top, jfloat right, jfloat bottom, jint dirHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000134 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500135 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400136 obj->addRect(left, top, right, bottom, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 }
Chris Craik79793882014-04-25 13:08:25 -0700138
Chris Craik2414e1b2016-12-12 13:56:15 -0800139 static void addOval(JNIEnv* env, jclass clazz, jlong objHandle,
Chris Craik79793882014-04-25 13:08:25 -0700140 jfloat left, jfloat top, jfloat right, jfloat bottom, jint dirHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000141 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500142 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
Chris Craik79793882014-04-25 13:08:25 -0700143 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
144 obj->addOval(oval, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 }
Chris Craik79793882014-04-25 13:08:25 -0700146
Chris Craik2414e1b2016-12-12 13:56:15 -0800147 static void addCircle(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x, jfloat y,
148 jfloat radius, jint dirHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000149 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500150 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400151 obj->addCircle(x, y, radius, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 }
Chris Craik79793882014-04-25 13:08:25 -0700153
Chris Craik2414e1b2016-12-12 13:56:15 -0800154 static void addArc(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
Antonio Calabrese24609582014-06-12 16:32:03 -0700155 jfloat right, jfloat bottom, jfloat startAngle, jfloat sweepAngle) {
156 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000157 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Antonio Calabrese24609582014-06-12 16:32:03 -0700158 obj->addArc(oval, startAngle, sweepAngle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 }
Chris Craik79793882014-04-25 13:08:25 -0700160
Chris Craik2414e1b2016-12-12 13:56:15 -0800161 static void addRoundRectXY(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
Antonio Calabrese24609582014-06-12 16:32:03 -0700162 jfloat right, jfloat bottom, jfloat rx, jfloat ry, jint dirHandle) {
163 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000164 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500165 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400166 obj->addRoundRect(rect, rx, ry, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700168
Chris Craik2414e1b2016-12-12 13:56:15 -0800169 static void addRoundRect8(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
170 jfloat right, jfloat bottom, jfloatArray array, jint dirHandle) {
Antonio Calabrese24609582014-06-12 16:32:03 -0700171 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000172 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500173 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 AutoJavaFloatArray afa(env, array, 8);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400175#ifdef SK_SCALAR_IS_FLOAT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 const float* src = afa.ptr();
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400177#else
178 #error Need to convert float array to SkScalar array before calling the following function.
179#endif
180 obj->addRoundRect(rect, src, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700182
Chris Craik2414e1b2016-12-12 13:56:15 -0800183 static void addPath__PathFF(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle,
184 jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000185 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
186 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400187 obj->addPath(*src, dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700189
Chris Craik2414e1b2016-12-12 13:56:15 -0800190 static void addPath__Path(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000191 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
192 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 obj->addPath(*src);
194 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700195
Chris Craik2414e1b2016-12-12 13:56:15 -0800196 static void addPath__PathMatrix(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle,
197 jlong matrixHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000198 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
199 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
200 SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 obj->addPath(*src, *matrix);
202 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700203
Chris Craik2414e1b2016-12-12 13:56:15 -0800204 static void offset__FF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000205 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400206 obj->offset(dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 }
208
Chris Craik2414e1b2016-12-12 13:56:15 -0800209 static void setLastPoint(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000210 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400211 obj->setLastPt(dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700213
Chris Craik2414e1b2016-12-12 13:56:15 -0800214 static void transform__MatrixPath(JNIEnv* env, jclass clazz, jlong objHandle, jlong matrixHandle,
215 jlong dstHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000216 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
217 SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
218 SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 obj->transform(*matrix, dst);
220 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700221
Chris Craik2414e1b2016-12-12 13:56:15 -0800222 static void transform__Matrix(JNIEnv* env, jclass clazz, jlong objHandle, jlong matrixHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000223 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
224 SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 obj->transform(*matrix);
226 }
Romain Guy8018c8d2013-07-29 19:17:59 -0700227
Chris Craik2414e1b2016-12-12 13:56:15 -0800228 static jboolean op(JNIEnv* env, jclass clazz, jlong p1Handle, jlong p2Handle, jint opHandle,
229 jlong rHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000230 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);
Romain Guy8018c8d2013-07-29 19:17:59 -0700235 }
George Mountc96c7b22013-08-23 13:31:31 -0700236
George Mountc96c7b22013-08-23 13:31:31 -0700237 typedef SkPoint (*bezierCalculation)(float t, const SkPoint* points);
238
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
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
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
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
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
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.
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.
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
Chris Craik96202d52017-04-27 15:26:17 -0700357 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) {
George Mountc96c7b22013-08-23 13:31:31 -0700360 switch (verb) {
361 case SkPath::kMove_Verb:
362 addMove(segmentPoints, lengths, points[0]);
363 break;
364 case SkPath::kClose_Verb:
George Mount7fe03a22013-11-22 17:08:28 -0800365 addLine(segmentPoints, lengths, points[0]);
366 break;
George Mountc96c7b22013-08-23 13:31:31 -0700367 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;
Chris Craik96202d52017-04-27 15:26:17 -0700378 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 }
George Mountc96c7b22013-08-23 13:31:31 -0700390 default:
Chris Craik96202d52017-04-27 15:26:17 -0700391 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.");
George Mountc96c7b22013-08-23 13:31:31 -0700399 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.
Chris Craik2414e1b2016-12-12 13:56:15 -0800410 static jfloatArray approximate(JNIEnv* env, jclass clazz, jlong pathHandle,
411 float acceptableError) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000412 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
George Mountc96c7b22013-08-23 13:31:31 -0700413 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;
Chris Craik96202d52017-04-27 15:26:17 -0700420 float errorConic = acceptableError / 2; // somewhat arbitrary
George Mountc96c7b22013-08-23 13:31:31 -0700421
Mike Reed7d489692019-11-27 11:15:43 -0500422 while ((verb = pathIter.next(points)) != SkPath::kDone_Verb) {
Chris Craik96202d52017-04-27 15:26:17 -0700423 createVerbSegments(pathIter, verb, points, segmentPoints, lengths,
424 errorSquared, errorConic);
George Mountc96c7b22013-08-23 13:31:31 -0700425 }
426
427 if (segmentPoints.empty()) {
George Mount68cfdad2014-09-11 10:25:31 -0700428 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;
George Mountc96c7b22013-08-23 13:31:31 -0700443 }
444
445 size_t numPoints = segmentPoints.size();
446 size_t approximationArraySize = numPoints * 3;
447
448 float* approximation = new float[approximationArraySize];
George Mountc96c7b22013-08-23 13:31:31 -0700449
450 int approximationIndex = 0;
George Mount7fe03a22013-11-22 17:08:28 -0800451 for (size_t i = 0; i < numPoints; i++) {
George Mountc96c7b22013-08-23 13:31:31 -0700452 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 }
Chris Craik2414e1b2016-12-12 13:56:15 -0800463
464 // ---------------- @FastNative -----------------------------
465
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);
John Reck205d83e2018-03-28 14:36:58 -0700470 if (jrect) {
471 GraphicsJNI::rect_to_jrectf(rect, env, jrect);
472 }
Chris Craik2414e1b2016-12-12 13:56:15 -0800473 return result;
474 }
475
476 // ---------------- @CriticalNative -------------------------
477
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100478 static void reset(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800479 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
480 obj->reset();
481 }
482
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100483 static void rewind(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800484 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
485 obj->rewind();
486 }
487
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100488 static jboolean isEmpty(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800489 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
490 return obj->isEmpty();
491 }
492
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100493 static jboolean isConvex(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800494 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
495 return obj->isConvex();
496 }
497
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100498 static jint getFillType(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800499 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed6a8bf8e2019-12-03 13:01:07 -0500500 return static_cast<int>(obj->getFillType());
Chris Craik2414e1b2016-12-12 13:56:15 -0800501 }
502
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100503 static void setFillType(CRITICAL_JNI_PARAMS_COMMA jlong pathHandle, jint ftHandle) {;
Chris Craik2414e1b2016-12-12 13:56:15 -0800504 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
Mike Reed6a8bf8e2019-12-03 13:01:07 -0500505 SkPathFillType ft = static_cast<SkPathFillType>(ftHandle);
Chris Craik2414e1b2016-12-12 13:56:15 -0800506 path->setFillType(ft);
507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508};
509
Daniel Micay76f6a862015-09-19 17:31:01 -0400510static const JNINativeMethod methods[] = {
Chris Craik2414e1b2016-12-12 13:56:15 -0800511 {"nInit","()J", (void*) SkPathGlue::init},
512 {"nInit","(J)J", (void*) SkPathGlue::init_Path},
John Reck205d83e2018-03-28 14:36:58 -0700513 {"nGetFinalizer", "()J", (void*) SkPathGlue::getFinalizer},
Chris Craik2414e1b2016-12-12 13:56:15 -0800514 {"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},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553};
554
555int register_android_graphics_Path(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800556 return RegisterMethodsOrDie(env, "android/graphics/Path", methods, NELEM(methods));
Derek Sollenberger1ad545d2015-08-20 14:23:14 -0400557
Mike Reed15760c92019-11-25 11:43:48 -0500558 static_assert(0 == (int)SkPathDirection::kCW, "direction_mismatch");
559 static_assert(1 == (int)SkPathDirection::kCCW, "direction_mismatch");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560}
561
562}