blob: a5e04763d885854d5a748ddad7ef18068c5455ac [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include "GraphicsJNI.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25#include "SkPath.h"
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050026#include "SkPathOps.h"
Chris Craik96202d52017-04-27 15:26:17 -070027#include "SkGeometry.h" // WARNING: Internal Skia Header
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
George Mountc96c7b22013-08-23 13:31:31 -070029#include <vector>
30#include <map>
Romain Guya2341a92010-09-08 18:04:33 -070031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032namespace android {
33
34class SkPathGlue {
35public:
36
John Reck205d83e2018-03-28 14:36:58 -070037 static void finalizer(SkPath* obj) {
John Reck205d83e2018-03-28 14:36:58 -070038 delete obj;
39 }
40
Chris Craik2414e1b2016-12-12 13:56:15 -080041 // ---------------- Regular JNI -----------------------------
42
43 static jlong init(JNIEnv* env, jclass clazz) {
44 return reinterpret_cast<jlong>(new SkPath());
45 }
46
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
John Reck205d83e2018-03-28 14:36:58 -070052 static jlong getFinalizer(JNIEnv* env, jclass clazz) {
53 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&finalizer));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 }
55
Chris Craik2414e1b2016-12-12 13:56:15 -080056 static void set(JNIEnv* env, jclass clazz, jlong dstHandle, jlong srcHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000057 SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
58 const SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 *dst = *src;
60 }
Ashok Bhat36bef0b2014-01-20 20:08:01 +000061
Chris Craik2414e1b2016-12-12 13:56:15 -080062 static void computeBounds(JNIEnv* env, jclass clazz, jlong objHandle, jobject jbounds) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000063 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040064 const SkRect& bounds = obj->getBounds();
65 GraphicsJNI::rect_to_jrectf(bounds, env, jbounds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 }
Antonio Calabrese24609582014-06-12 16:32:03 -070067
Chris Craik2414e1b2016-12-12 13:56:15 -080068 static void incReserve(JNIEnv* env, jclass clazz, jlong objHandle, jint extraPtCount) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000069 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 obj->incReserve(extraPtCount);
71 }
Antonio Calabrese24609582014-06-12 16:32:03 -070072
Chris Craik2414e1b2016-12-12 13:56:15 -080073 static void moveTo__FF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x, jfloat y) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000074 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040075 obj->moveTo(x, y);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 }
Antonio Calabrese24609582014-06-12 16:32:03 -070077
Chris Craik2414e1b2016-12-12 13:56:15 -080078 static void rMoveTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000079 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040080 obj->rMoveTo(dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 }
Antonio Calabrese24609582014-06-12 16:32:03 -070082
Chris Craik2414e1b2016-12-12 13:56:15 -080083 static void lineTo__FF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x, jfloat y) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000084 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040085 obj->lineTo(x, y);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 }
Antonio Calabrese24609582014-06-12 16:32:03 -070087
Chris Craik2414e1b2016-12-12 13:56:15 -080088 static void rLineTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000089 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040090 obj->rLineTo(dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 }
Antonio Calabrese24609582014-06-12 16:32:03 -070092
Chris Craik2414e1b2016-12-12 13:56:15 -080093 static void quadTo__FFFF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x1, jfloat y1,
94 jfloat x2, jfloat y2) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000095 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -040096 obj->quadTo(x1, y1, x2, y2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 }
Antonio Calabrese24609582014-06-12 16:32:03 -070098
Chris Craik2414e1b2016-12-12 13:56:15 -080099 static void rQuadTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx1, jfloat dy1,
100 jfloat dx2, jfloat dy2) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000101 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400102 obj->rQuadTo(dx1, dy1, dx2, dy2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700104
Chet Haase0834e9f2022-08-15 23:01:03 +0000105 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
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
Chris Craik2414e1b2016-12-12 13:56:15 -0800117 static void cubicTo__FFFFFF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x1, jfloat y1,
118 jfloat x2, jfloat y2, jfloat x3, jfloat y3) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000119 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400120 obj->cubicTo(x1, y1, x2, y2, x3, y3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700122
Chris Craik2414e1b2016-12-12 13:56:15 -0800123 static void rCubicTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x1, jfloat y1,
124 jfloat x2, jfloat y2, jfloat x3, jfloat y3) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000125 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400126 obj->rCubicTo(x1, y1, x2, y2, x3, y3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700128
Chris Craik2414e1b2016-12-12 13:56:15 -0800129 static void arcTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
Antonio Calabrese24609582014-06-12 16:32:03 -0700130 jfloat right, jfloat bottom, jfloat startAngle, jfloat sweepAngle,
131 jboolean forceMoveTo) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000132 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Antonio Calabrese24609582014-06-12 16:32:03 -0700133 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
134 obj->arcTo(oval, startAngle, sweepAngle, forceMoveTo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700136
Chris Craik2414e1b2016-12-12 13:56:15 -0800137 static void close(JNIEnv* env, jclass clazz, jlong objHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000138 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 obj->close();
140 }
Chris Craik79793882014-04-25 13:08:25 -0700141
Chris Craik2414e1b2016-12-12 13:56:15 -0800142 static void addRect(JNIEnv* env, jclass clazz, jlong objHandle,
Chris Craik79793882014-04-25 13:08:25 -0700143 jfloat left, jfloat top, jfloat right, jfloat bottom, jint dirHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000144 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500145 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400146 obj->addRect(left, top, right, bottom, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 }
Chris Craik79793882014-04-25 13:08:25 -0700148
Chris Craik2414e1b2016-12-12 13:56:15 -0800149 static void addOval(JNIEnv* env, jclass clazz, jlong objHandle,
Chris Craik79793882014-04-25 13:08:25 -0700150 jfloat left, jfloat top, jfloat right, jfloat bottom, jint dirHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000151 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500152 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
Chris Craik79793882014-04-25 13:08:25 -0700153 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
154 obj->addOval(oval, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 }
Chris Craik79793882014-04-25 13:08:25 -0700156
Chris Craik2414e1b2016-12-12 13:56:15 -0800157 static void addCircle(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x, jfloat y,
158 jfloat radius, jint dirHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000159 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500160 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400161 obj->addCircle(x, y, radius, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 }
Chris Craik79793882014-04-25 13:08:25 -0700163
Chris Craik2414e1b2016-12-12 13:56:15 -0800164 static void addArc(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
Antonio Calabrese24609582014-06-12 16:32:03 -0700165 jfloat right, jfloat bottom, jfloat startAngle, jfloat sweepAngle) {
166 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000167 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Antonio Calabrese24609582014-06-12 16:32:03 -0700168 obj->addArc(oval, startAngle, sweepAngle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 }
Chris Craik79793882014-04-25 13:08:25 -0700170
Chris Craik2414e1b2016-12-12 13:56:15 -0800171 static void addRoundRectXY(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
Antonio Calabrese24609582014-06-12 16:32:03 -0700172 jfloat right, jfloat bottom, jfloat rx, jfloat ry, jint dirHandle) {
173 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000174 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500175 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400176 obj->addRoundRect(rect, rx, ry, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700178
Chris Craik2414e1b2016-12-12 13:56:15 -0800179 static void addRoundRect8(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
180 jfloat right, jfloat bottom, jfloatArray array, jint dirHandle) {
Antonio Calabrese24609582014-06-12 16:32:03 -0700181 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000182 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500183 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 AutoJavaFloatArray afa(env, array, 8);
185 const float* src = afa.ptr();
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400186 obj->addRoundRect(rect, src, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700188
Chris Craik2414e1b2016-12-12 13:56:15 -0800189 static void addPath__PathFF(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle,
190 jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000191 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
192 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400193 obj->addPath(*src, dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700195
Chris Craik2414e1b2016-12-12 13:56:15 -0800196 static void addPath__Path(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000197 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
198 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 obj->addPath(*src);
200 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700201
Chris Craik2414e1b2016-12-12 13:56:15 -0800202 static void addPath__PathMatrix(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle,
203 jlong matrixHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000204 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
205 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
206 SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 obj->addPath(*src, *matrix);
208 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700209
Chris Craik2414e1b2016-12-12 13:56:15 -0800210 static void offset__FF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000211 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400212 obj->offset(dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 }
214
Chris Craik2414e1b2016-12-12 13:56:15 -0800215 static void setLastPoint(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000216 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400217 obj->setLastPt(dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700219
Chet Haase0834e9f2022-08-15 23:01:03 +0000220 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
Chris Craik2414e1b2016-12-12 13:56:15 -0800228 static void transform__MatrixPath(JNIEnv* env, jclass clazz, jlong objHandle, jlong matrixHandle,
229 jlong dstHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000230 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
231 SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
232 SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 obj->transform(*matrix, dst);
234 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700235
Chris Craik2414e1b2016-12-12 13:56:15 -0800236 static void transform__Matrix(JNIEnv* env, jclass clazz, jlong objHandle, jlong matrixHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000237 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
238 SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 obj->transform(*matrix);
240 }
Romain Guy8018c8d2013-07-29 19:17:59 -0700241
Chris Craik2414e1b2016-12-12 13:56:15 -0800242 static jboolean op(JNIEnv* env, jclass clazz, jlong p1Handle, jlong p2Handle, jint opHandle,
243 jlong rHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000244 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);
Romain Guy8018c8d2013-07-29 19:17:59 -0700249 }
George Mountc96c7b22013-08-23 13:31:31 -0700250
George Mountc96c7b22013-08-23 13:31:31 -0700251 typedef SkPoint (*bezierCalculation)(float t, const SkPoint* points);
252
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
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
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
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
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
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.
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.
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
Chris Craik96202d52017-04-27 15:26:17 -0700371 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) {
George Mountc96c7b22013-08-23 13:31:31 -0700374 switch (verb) {
375 case SkPath::kMove_Verb:
376 addMove(segmentPoints, lengths, points[0]);
377 break;
378 case SkPath::kClose_Verb:
George Mount7fe03a22013-11-22 17:08:28 -0800379 addLine(segmentPoints, lengths, points[0]);
380 break;
George Mountc96c7b22013-08-23 13:31:31 -0700381 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;
Chris Craik96202d52017-04-27 15:26:17 -0700392 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 }
George Mountc96c7b22013-08-23 13:31:31 -0700404 default:
Chris Craik96202d52017-04-27 15:26:17 -0700405 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.");
George Mountc96c7b22013-08-23 13:31:31 -0700413 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.
Chris Craik2414e1b2016-12-12 13:56:15 -0800424 static jfloatArray approximate(JNIEnv* env, jclass clazz, jlong pathHandle,
425 float acceptableError) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000426 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
George Mountc96c7b22013-08-23 13:31:31 -0700427 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;
Chris Craik96202d52017-04-27 15:26:17 -0700434 float errorConic = acceptableError / 2; // somewhat arbitrary
George Mountc96c7b22013-08-23 13:31:31 -0700435
Mike Reed7d489692019-11-27 11:15:43 -0500436 while ((verb = pathIter.next(points)) != SkPath::kDone_Verb) {
Chris Craik96202d52017-04-27 15:26:17 -0700437 createVerbSegments(pathIter, verb, points, segmentPoints, lengths,
438 errorSquared, errorConic);
George Mountc96c7b22013-08-23 13:31:31 -0700439 }
440
441 if (segmentPoints.empty()) {
George Mount68cfdad2014-09-11 10:25:31 -0700442 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;
George Mountc96c7b22013-08-23 13:31:31 -0700457 }
458
459 size_t numPoints = segmentPoints.size();
460 size_t approximationArraySize = numPoints * 3;
461
462 float* approximation = new float[approximationArraySize];
George Mountc96c7b22013-08-23 13:31:31 -0700463
464 int approximationIndex = 0;
George Mount7fe03a22013-11-22 17:08:28 -0800465 for (size_t i = 0; i < numPoints; i++) {
George Mountc96c7b22013-08-23 13:31:31 -0700466 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 }
Chris Craik2414e1b2016-12-12 13:56:15 -0800477
478 // ---------------- @FastNative -----------------------------
479
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);
John Reck205d83e2018-03-28 14:36:58 -0700484 if (jrect) {
485 GraphicsJNI::rect_to_jrectf(rect, env, jrect);
486 }
Chris Craik2414e1b2016-12-12 13:56:15 -0800487 return result;
488 }
489
490 // ---------------- @CriticalNative -------------------------
491
Chet Haase0834e9f2022-08-15 23:01:03 +0000492 static jint getGenerationID(CRITICAL_JNI_PARAMS_COMMA jlong pathHandle) {
493 return (reinterpret_cast<SkPath*>(pathHandle)->getGenerationID());
494 }
495
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
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100502 static void reset(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800503 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
504 obj->reset();
505 }
506
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100507 static void rewind(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800508 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
509 obj->rewind();
510 }
511
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100512 static jboolean isEmpty(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800513 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
514 return obj->isEmpty();
515 }
516
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100517 static jboolean isConvex(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800518 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
519 return obj->isConvex();
520 }
521
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100522 static jint getFillType(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800523 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed6a8bf8e2019-12-03 13:01:07 -0500524 return static_cast<int>(obj->getFillType());
Chris Craik2414e1b2016-12-12 13:56:15 -0800525 }
526
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100527 static void setFillType(CRITICAL_JNI_PARAMS_COMMA jlong pathHandle, jint ftHandle) {;
Chris Craik2414e1b2016-12-12 13:56:15 -0800528 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
Mike Reed6a8bf8e2019-12-03 13:01:07 -0500529 SkPathFillType ft = static_cast<SkPathFillType>(ftHandle);
Chris Craik2414e1b2016-12-12 13:56:15 -0800530 path->setFillType(ft);
531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532};
533
Daniel Micay76f6a862015-09-19 17:31:01 -0400534static const JNINativeMethod methods[] = {
Chet Haase0834e9f2022-08-15 23:01:03 +0000535 {"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},
Chris Craik2414e1b2016-12-12 13:56:15 -0800569
Chet Haase0834e9f2022-08-15 23:01:03 +0000570 // ------- @FastNative below here ----------------------
571 {"nIsRect", "(JLandroid/graphics/RectF;)Z", (void*)SkPathGlue::isRect},
Chris Craik2414e1b2016-12-12 13:56:15 -0800572
Chet Haase0834e9f2022-08-15 23:01:03 +0000573 // ------- @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},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582};
583
584int register_android_graphics_Path(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800585 return RegisterMethodsOrDie(env, "android/graphics/Path", methods, NELEM(methods));
Derek Sollenberger1ad545d2015-08-20 14:23:14 -0400586
Mike Reed15760c92019-11-25 11:43:48 -0500587 static_assert(0 == (int)SkPathDirection::kCW, "direction_mismatch");
588 static_assert(1 == (int)SkPathDirection::kCCW, "direction_mismatch");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589}
590
591}