blob: 3694ce07b972490f4daee7307da1272f669cb8c2 [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);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400185#ifdef SK_SCALAR_IS_FLOAT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 const float* src = afa.ptr();
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400187#else
188 #error Need to convert float array to SkScalar array before calling the following function.
189#endif
190 obj->addRoundRect(rect, src, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700192
Chris Craik2414e1b2016-12-12 13:56:15 -0800193 static void addPath__PathFF(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle,
194 jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000195 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
196 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400197 obj->addPath(*src, dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700199
Chris Craik2414e1b2016-12-12 13:56:15 -0800200 static void addPath__Path(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000201 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
202 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 obj->addPath(*src);
204 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700205
Chris Craik2414e1b2016-12-12 13:56:15 -0800206 static void addPath__PathMatrix(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle,
207 jlong matrixHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000208 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
209 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
210 SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 obj->addPath(*src, *matrix);
212 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700213
Chris Craik2414e1b2016-12-12 13:56:15 -0800214 static void offset__FF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000215 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400216 obj->offset(dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 }
218
Chris Craik2414e1b2016-12-12 13:56:15 -0800219 static void setLastPoint(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000220 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400221 obj->setLastPt(dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700223
Chet Haase0834e9f2022-08-15 23:01:03 +0000224 static jboolean interpolate(JNIEnv* env, jclass clazz, jlong startHandle, jlong endHandle,
225 jfloat t, jlong interpolatedHandle) {
226 SkPath* startPath = reinterpret_cast<SkPath*>(startHandle);
227 SkPath* endPath = reinterpret_cast<SkPath*>(endHandle);
228 SkPath* interpolatedPath = reinterpret_cast<SkPath*>(interpolatedHandle);
229 return startPath->interpolate(*endPath, t, interpolatedPath);
230 }
231
Chris Craik2414e1b2016-12-12 13:56:15 -0800232 static void transform__MatrixPath(JNIEnv* env, jclass clazz, jlong objHandle, jlong matrixHandle,
233 jlong dstHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000234 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
235 SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
236 SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 obj->transform(*matrix, dst);
238 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700239
Chris Craik2414e1b2016-12-12 13:56:15 -0800240 static void transform__Matrix(JNIEnv* env, jclass clazz, jlong objHandle, jlong matrixHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000241 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
242 SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 obj->transform(*matrix);
244 }
Romain Guy8018c8d2013-07-29 19:17:59 -0700245
Chris Craik2414e1b2016-12-12 13:56:15 -0800246 static jboolean op(JNIEnv* env, jclass clazz, jlong p1Handle, jlong p2Handle, jint opHandle,
247 jlong rHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000248 SkPath* p1 = reinterpret_cast<SkPath*>(p1Handle);
249 SkPath* p2 = reinterpret_cast<SkPath*>(p2Handle);
250 SkPathOp op = static_cast<SkPathOp>(opHandle);
251 SkPath* r = reinterpret_cast<SkPath*>(rHandle);
252 return Op(*p1, *p2, op, r);
Romain Guy8018c8d2013-07-29 19:17:59 -0700253 }
George Mountc96c7b22013-08-23 13:31:31 -0700254
George Mountc96c7b22013-08-23 13:31:31 -0700255 typedef SkPoint (*bezierCalculation)(float t, const SkPoint* points);
256
257 static void addMove(std::vector<SkPoint>& segmentPoints, std::vector<float>& lengths,
258 const SkPoint& point) {
259 float length = 0;
260 if (!lengths.empty()) {
261 length = lengths.back();
262 }
263 segmentPoints.push_back(point);
264 lengths.push_back(length);
265 }
266
267 static void addLine(std::vector<SkPoint>& segmentPoints, std::vector<float>& lengths,
268 const SkPoint& toPoint) {
269 if (segmentPoints.empty()) {
270 segmentPoints.push_back(SkPoint::Make(0, 0));
271 lengths.push_back(0);
272 } else if (segmentPoints.back() == toPoint) {
273 return; // Empty line
274 }
275 float length = lengths.back() + SkPoint::Distance(segmentPoints.back(), toPoint);
276 segmentPoints.push_back(toPoint);
277 lengths.push_back(length);
278 }
279
280 static float cubicCoordinateCalculation(float t, float p0, float p1, float p2, float p3) {
281 float oneMinusT = 1 - t;
282 float oneMinusTSquared = oneMinusT * oneMinusT;
283 float oneMinusTCubed = oneMinusTSquared * oneMinusT;
284 float tSquared = t * t;
285 float tCubed = tSquared * t;
286 return (oneMinusTCubed * p0) + (3 * oneMinusTSquared * t * p1)
287 + (3 * oneMinusT * tSquared * p2) + (tCubed * p3);
288 }
289
290 static SkPoint cubicBezierCalculation(float t, const SkPoint* points) {
291 float x = cubicCoordinateCalculation(t, points[0].x(), points[1].x(),
292 points[2].x(), points[3].x());
293 float y = cubicCoordinateCalculation(t, points[0].y(), points[1].y(),
294 points[2].y(), points[3].y());
295 return SkPoint::Make(x, y);
296 }
297
298 static float quadraticCoordinateCalculation(float t, float p0, float p1, float p2) {
299 float oneMinusT = 1 - t;
300 return oneMinusT * ((oneMinusT * p0) + (t * p1)) + t * ((oneMinusT * p1) + (t * p2));
301 }
302
303 static SkPoint quadraticBezierCalculation(float t, const SkPoint* points) {
304 float x = quadraticCoordinateCalculation(t, points[0].x(), points[1].x(), points[2].x());
305 float y = quadraticCoordinateCalculation(t, points[0].y(), points[1].y(), points[2].y());
306 return SkPoint::Make(x, y);
307 }
308
309 // Subdivide a section of the Bezier curve, set the mid-point and the mid-t value.
310 // Returns true if further subdivision is necessary as defined by errorSquared.
311 static bool subdividePoints(const SkPoint* points, bezierCalculation bezierFunction,
312 float t0, const SkPoint &p0, float t1, const SkPoint &p1,
313 float& midT, SkPoint &midPoint, float errorSquared) {
314 midT = (t1 + t0) / 2;
315 float midX = (p1.x() + p0.x()) / 2;
316 float midY = (p1.y() + p0.y()) / 2;
317
318 midPoint = (*bezierFunction)(midT, points);
319 float xError = midPoint.x() - midX;
320 float yError = midPoint.y() - midY;
321 float midErrorSquared = (xError * xError) + (yError * yError);
322 return midErrorSquared > errorSquared;
323 }
324
325 // Divides Bezier curves until linear interpolation is very close to accurate, using
326 // errorSquared as a metric. Cubic Bezier curves can have an inflection point that improperly
327 // short-circuit subdivision. If you imagine an S shape, the top and bottom points being the
328 // starting and end points, linear interpolation would mark the center where the curve places
329 // the point. It is clearly not the case that we can linearly interpolate at that point.
330 // doubleCheckDivision forces a second examination between subdivisions to ensure that linear
331 // interpolation works.
332 static void addBezier(const SkPoint* points,
333 bezierCalculation bezierFunction, std::vector<SkPoint>& segmentPoints,
334 std::vector<float>& lengths, float errorSquared, bool doubleCheckDivision) {
335 typedef std::map<float, SkPoint> PointMap;
336 PointMap tToPoint;
337
338 tToPoint[0] = (*bezierFunction)(0, points);
339 tToPoint[1] = (*bezierFunction)(1, points);
340
341 PointMap::iterator iter = tToPoint.begin();
342 PointMap::iterator next = iter;
343 ++next;
344 while (next != tToPoint.end()) {
345 bool needsSubdivision = true;
346 SkPoint midPoint;
347 do {
348 float midT;
349 needsSubdivision = subdividePoints(points, bezierFunction, iter->first,
350 iter->second, next->first, next->second, midT, midPoint, errorSquared);
351 if (!needsSubdivision && doubleCheckDivision) {
352 SkPoint quarterPoint;
353 float quarterT;
354 needsSubdivision = subdividePoints(points, bezierFunction, iter->first,
355 iter->second, midT, midPoint, quarterT, quarterPoint, errorSquared);
356 if (needsSubdivision) {
357 // Found an inflection point. No need to double-check.
358 doubleCheckDivision = false;
359 }
360 }
361 if (needsSubdivision) {
362 next = tToPoint.insert(iter, PointMap::value_type(midT, midPoint));
363 }
364 } while (needsSubdivision);
365 iter = next;
366 next++;
367 }
368
369 // Now that each division can use linear interpolation with less than the allowed error
370 for (iter = tToPoint.begin(); iter != tToPoint.end(); ++iter) {
371 addLine(segmentPoints, lengths, iter->second);
372 }
373 }
374
Chris Craik96202d52017-04-27 15:26:17 -0700375 static void createVerbSegments(const SkPath::Iter& pathIter, SkPath::Verb verb,
376 const SkPoint* points, std::vector<SkPoint>& segmentPoints,
377 std::vector<float>& lengths, float errorSquared, float errorConic) {
George Mountc96c7b22013-08-23 13:31:31 -0700378 switch (verb) {
379 case SkPath::kMove_Verb:
380 addMove(segmentPoints, lengths, points[0]);
381 break;
382 case SkPath::kClose_Verb:
George Mount7fe03a22013-11-22 17:08:28 -0800383 addLine(segmentPoints, lengths, points[0]);
384 break;
George Mountc96c7b22013-08-23 13:31:31 -0700385 case SkPath::kLine_Verb:
386 addLine(segmentPoints, lengths, points[1]);
387 break;
388 case SkPath::kQuad_Verb:
389 addBezier(points, quadraticBezierCalculation, segmentPoints, lengths,
390 errorSquared, false);
391 break;
392 case SkPath::kCubic_Verb:
393 addBezier(points, cubicBezierCalculation, segmentPoints, lengths,
394 errorSquared, true);
395 break;
Chris Craik96202d52017-04-27 15:26:17 -0700396 case SkPath::kConic_Verb: {
397 SkAutoConicToQuads converter;
398 const SkPoint* quads = converter.computeQuads(
399 points, pathIter.conicWeight(), errorConic);
400 for (int i = 0; i < converter.countQuads(); i++) {
401 // Note: offset each subsequent quad by 2, since end points are shared
402 const SkPoint* quad = quads + i * 2;
403 addBezier(quad, quadraticBezierCalculation, segmentPoints, lengths,
404 errorConic, false);
405 }
406 break;
407 }
George Mountc96c7b22013-08-23 13:31:31 -0700408 default:
Chris Craik96202d52017-04-27 15:26:17 -0700409 static_assert(SkPath::kMove_Verb == 0
410 && SkPath::kLine_Verb == 1
411 && SkPath::kQuad_Verb == 2
412 && SkPath::kConic_Verb == 3
413 && SkPath::kCubic_Verb == 4
414 && SkPath::kClose_Verb == 5
415 && SkPath::kDone_Verb == 6,
416 "Path enum changed, new types may have been added.");
George Mountc96c7b22013-08-23 13:31:31 -0700417 break;
418 }
419 }
420
421 // Returns a float[] with each point along the path represented by 3 floats
422 // * fractional length along the path that the point resides
423 // * x coordinate
424 // * y coordinate
425 // Note that more than one point may have the same length along the path in
426 // the case of a move.
427 // NULL can be returned if the Path is empty.
Chris Craik2414e1b2016-12-12 13:56:15 -0800428 static jfloatArray approximate(JNIEnv* env, jclass clazz, jlong pathHandle,
429 float acceptableError) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000430 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
George Mountc96c7b22013-08-23 13:31:31 -0700431 SkASSERT(path);
432 SkPath::Iter pathIter(*path, false);
433 SkPath::Verb verb;
434 SkPoint points[4];
435 std::vector<SkPoint> segmentPoints;
436 std::vector<float> lengths;
437 float errorSquared = acceptableError * acceptableError;
Chris Craik96202d52017-04-27 15:26:17 -0700438 float errorConic = acceptableError / 2; // somewhat arbitrary
George Mountc96c7b22013-08-23 13:31:31 -0700439
Mike Reed7d489692019-11-27 11:15:43 -0500440 while ((verb = pathIter.next(points)) != SkPath::kDone_Verb) {
Chris Craik96202d52017-04-27 15:26:17 -0700441 createVerbSegments(pathIter, verb, points, segmentPoints, lengths,
442 errorSquared, errorConic);
George Mountc96c7b22013-08-23 13:31:31 -0700443 }
444
445 if (segmentPoints.empty()) {
George Mount68cfdad2014-09-11 10:25:31 -0700446 int numVerbs = path->countVerbs();
447 if (numVerbs == 1) {
448 addMove(segmentPoints, lengths, path->getPoint(0));
449 } else {
450 // Invalid or empty path. Fall back to point(0,0)
451 addMove(segmentPoints, lengths, SkPoint());
452 }
453 }
454
455 float totalLength = lengths.back();
456 if (totalLength == 0) {
457 // Lone Move instructions should still be able to animate at the same value.
458 segmentPoints.push_back(segmentPoints.back());
459 lengths.push_back(1);
460 totalLength = 1;
George Mountc96c7b22013-08-23 13:31:31 -0700461 }
462
463 size_t numPoints = segmentPoints.size();
464 size_t approximationArraySize = numPoints * 3;
465
466 float* approximation = new float[approximationArraySize];
George Mountc96c7b22013-08-23 13:31:31 -0700467
468 int approximationIndex = 0;
George Mount7fe03a22013-11-22 17:08:28 -0800469 for (size_t i = 0; i < numPoints; i++) {
George Mountc96c7b22013-08-23 13:31:31 -0700470 const SkPoint& point = segmentPoints[i];
471 approximation[approximationIndex++] = lengths[i] / totalLength;
472 approximation[approximationIndex++] = point.x();
473 approximation[approximationIndex++] = point.y();
474 }
475
476 jfloatArray result = env->NewFloatArray(approximationArraySize);
477 env->SetFloatArrayRegion(result, 0, approximationArraySize, approximation);
478 delete[] approximation;
479 return result;
480 }
Chris Craik2414e1b2016-12-12 13:56:15 -0800481
482 // ---------------- @FastNative -----------------------------
483
484 static jboolean isRect(JNIEnv* env, jclass clazz, jlong objHandle, jobject jrect) {
485 SkRect rect;
486 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
487 jboolean result = obj->isRect(&rect);
John Reck205d83e2018-03-28 14:36:58 -0700488 if (jrect) {
489 GraphicsJNI::rect_to_jrectf(rect, env, jrect);
490 }
Chris Craik2414e1b2016-12-12 13:56:15 -0800491 return result;
492 }
493
494 // ---------------- @CriticalNative -------------------------
495
Chet Haase0834e9f2022-08-15 23:01:03 +0000496 static jint getGenerationID(CRITICAL_JNI_PARAMS_COMMA jlong pathHandle) {
497 return (reinterpret_cast<SkPath*>(pathHandle)->getGenerationID());
498 }
499
500 static jboolean isInterpolatable(CRITICAL_JNI_PARAMS_COMMA jlong startHandle, jlong endHandle) {
501 SkPath* startPath = reinterpret_cast<SkPath*>(startHandle);
502 SkPath* endPath = reinterpret_cast<SkPath*>(endHandle);
503 return startPath->isInterpolatable(*endPath);
504 }
505
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100506 static void reset(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800507 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
508 obj->reset();
509 }
510
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100511 static void rewind(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800512 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
513 obj->rewind();
514 }
515
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100516 static jboolean isEmpty(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800517 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
518 return obj->isEmpty();
519 }
520
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100521 static jboolean isConvex(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800522 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
523 return obj->isConvex();
524 }
525
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100526 static jint getFillType(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800527 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed6a8bf8e2019-12-03 13:01:07 -0500528 return static_cast<int>(obj->getFillType());
Chris Craik2414e1b2016-12-12 13:56:15 -0800529 }
530
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100531 static void setFillType(CRITICAL_JNI_PARAMS_COMMA jlong pathHandle, jint ftHandle) {;
Chris Craik2414e1b2016-12-12 13:56:15 -0800532 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
Mike Reed6a8bf8e2019-12-03 13:01:07 -0500533 SkPathFillType ft = static_cast<SkPathFillType>(ftHandle);
Chris Craik2414e1b2016-12-12 13:56:15 -0800534 path->setFillType(ft);
535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536};
537
Daniel Micay76f6a862015-09-19 17:31:01 -0400538static const JNINativeMethod methods[] = {
Chet Haase0834e9f2022-08-15 23:01:03 +0000539 {"nInit", "()J", (void*)SkPathGlue::init},
540 {"nInit", "(J)J", (void*)SkPathGlue::init_Path},
541 {"nGetFinalizer", "()J", (void*)SkPathGlue::getFinalizer},
542 {"nSet", "(JJ)V", (void*)SkPathGlue::set},
543 {"nComputeBounds", "(JLandroid/graphics/RectF;)V", (void*)SkPathGlue::computeBounds},
544 {"nIncReserve", "(JI)V", (void*)SkPathGlue::incReserve},
545 {"nMoveTo", "(JFF)V", (void*)SkPathGlue::moveTo__FF},
546 {"nRMoveTo", "(JFF)V", (void*)SkPathGlue::rMoveTo},
547 {"nLineTo", "(JFF)V", (void*)SkPathGlue::lineTo__FF},
548 {"nRLineTo", "(JFF)V", (void*)SkPathGlue::rLineTo},
549 {"nQuadTo", "(JFFFF)V", (void*)SkPathGlue::quadTo__FFFF},
550 {"nRQuadTo", "(JFFFF)V", (void*)SkPathGlue::rQuadTo},
551 {"nConicTo", "(JFFFFF)V", (void*)SkPathGlue::conicTo},
552 {"nRConicTo", "(JFFFFF)V", (void*)SkPathGlue::rConicTo},
553 {"nCubicTo", "(JFFFFFF)V", (void*)SkPathGlue::cubicTo__FFFFFF},
554 {"nRCubicTo", "(JFFFFFF)V", (void*)SkPathGlue::rCubicTo},
555 {"nArcTo", "(JFFFFFFZ)V", (void*)SkPathGlue::arcTo},
556 {"nClose", "(J)V", (void*)SkPathGlue::close},
557 {"nAddRect", "(JFFFFI)V", (void*)SkPathGlue::addRect},
558 {"nAddOval", "(JFFFFI)V", (void*)SkPathGlue::addOval},
559 {"nAddCircle", "(JFFFI)V", (void*)SkPathGlue::addCircle},
560 {"nAddArc", "(JFFFFFF)V", (void*)SkPathGlue::addArc},
561 {"nAddRoundRect", "(JFFFFFFI)V", (void*)SkPathGlue::addRoundRectXY},
562 {"nAddRoundRect", "(JFFFF[FI)V", (void*)SkPathGlue::addRoundRect8},
563 {"nAddPath", "(JJFF)V", (void*)SkPathGlue::addPath__PathFF},
564 {"nAddPath", "(JJ)V", (void*)SkPathGlue::addPath__Path},
565 {"nAddPath", "(JJJ)V", (void*)SkPathGlue::addPath__PathMatrix},
566 {"nInterpolate", "(JJFJ)Z", (void*)SkPathGlue::interpolate},
567 {"nOffset", "(JFF)V", (void*)SkPathGlue::offset__FF},
568 {"nSetLastPoint", "(JFF)V", (void*)SkPathGlue::setLastPoint},
569 {"nTransform", "(JJJ)V", (void*)SkPathGlue::transform__MatrixPath},
570 {"nTransform", "(JJ)V", (void*)SkPathGlue::transform__Matrix},
571 {"nOp", "(JJIJ)Z", (void*)SkPathGlue::op},
572 {"nApproximate", "(JF)[F", (void*)SkPathGlue::approximate},
Chris Craik2414e1b2016-12-12 13:56:15 -0800573
Chet Haase0834e9f2022-08-15 23:01:03 +0000574 // ------- @FastNative below here ----------------------
575 {"nIsRect", "(JLandroid/graphics/RectF;)Z", (void*)SkPathGlue::isRect},
Chris Craik2414e1b2016-12-12 13:56:15 -0800576
Chet Haase0834e9f2022-08-15 23:01:03 +0000577 // ------- @CriticalNative below here ------------------
578 {"nGetGenerationID", "(J)I", (void*)SkPathGlue::getGenerationID},
579 {"nIsInterpolatable", "(JJ)Z", (void*)SkPathGlue::isInterpolatable},
580 {"nReset", "(J)V", (void*)SkPathGlue::reset},
581 {"nRewind", "(J)V", (void*)SkPathGlue::rewind},
582 {"nIsEmpty", "(J)Z", (void*)SkPathGlue::isEmpty},
583 {"nIsConvex", "(J)Z", (void*)SkPathGlue::isConvex},
584 {"nGetFillType", "(J)I", (void*)SkPathGlue::getFillType},
585 {"nSetFillType", "(JI)V", (void*)SkPathGlue::setFillType},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586};
587
588int register_android_graphics_Path(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800589 return RegisterMethodsOrDie(env, "android/graphics/Path", methods, NELEM(methods));
Derek Sollenberger1ad545d2015-08-20 14:23:14 -0400590
Mike Reed15760c92019-11-25 11:43:48 -0500591 static_assert(0 == (int)SkPathDirection::kCW, "direction_mismatch");
592 static_assert(1 == (int)SkPathDirection::kCCW, "direction_mismatch");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593}
594
595}