blob: d67bcf2216817052b4bec8042d68195dd3a74c05 [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
Chris Craik2414e1b2016-12-12 13:56:15 -0800105 static void cubicTo__FFFFFF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x1, jfloat y1,
106 jfloat x2, jfloat y2, jfloat x3, jfloat y3) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000107 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400108 obj->cubicTo(x1, y1, x2, y2, x3, y3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700110
Chris Craik2414e1b2016-12-12 13:56:15 -0800111 static void rCubicTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x1, jfloat y1,
112 jfloat x2, jfloat y2, jfloat x3, jfloat y3) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000113 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400114 obj->rCubicTo(x1, y1, x2, y2, x3, y3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700116
Chris Craik2414e1b2016-12-12 13:56:15 -0800117 static void arcTo(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
Antonio Calabrese24609582014-06-12 16:32:03 -0700118 jfloat right, jfloat bottom, jfloat startAngle, jfloat sweepAngle,
119 jboolean forceMoveTo) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000120 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Antonio Calabrese24609582014-06-12 16:32:03 -0700121 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
122 obj->arcTo(oval, startAngle, sweepAngle, forceMoveTo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700124
Chris Craik2414e1b2016-12-12 13:56:15 -0800125 static void close(JNIEnv* env, jclass clazz, jlong objHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000126 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 obj->close();
128 }
Chris Craik79793882014-04-25 13:08:25 -0700129
Chris Craik2414e1b2016-12-12 13:56:15 -0800130 static void addRect(JNIEnv* env, jclass clazz, jlong objHandle,
Chris Craik79793882014-04-25 13:08:25 -0700131 jfloat left, jfloat top, jfloat right, jfloat bottom, jint dirHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000132 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500133 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400134 obj->addRect(left, top, right, bottom, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 }
Chris Craik79793882014-04-25 13:08:25 -0700136
Chris Craik2414e1b2016-12-12 13:56:15 -0800137 static void addOval(JNIEnv* env, jclass clazz, jlong objHandle,
Chris Craik79793882014-04-25 13:08:25 -0700138 jfloat left, jfloat top, jfloat right, jfloat bottom, jint dirHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000139 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500140 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
Chris Craik79793882014-04-25 13:08:25 -0700141 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
142 obj->addOval(oval, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 }
Chris Craik79793882014-04-25 13:08:25 -0700144
Chris Craik2414e1b2016-12-12 13:56:15 -0800145 static void addCircle(JNIEnv* env, jclass clazz, jlong objHandle, jfloat x, jfloat y,
146 jfloat radius, jint dirHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000147 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500148 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400149 obj->addCircle(x, y, radius, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 }
Chris Craik79793882014-04-25 13:08:25 -0700151
Chris Craik2414e1b2016-12-12 13:56:15 -0800152 static void addArc(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
Antonio Calabrese24609582014-06-12 16:32:03 -0700153 jfloat right, jfloat bottom, jfloat startAngle, jfloat sweepAngle) {
154 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000155 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Antonio Calabrese24609582014-06-12 16:32:03 -0700156 obj->addArc(oval, startAngle, sweepAngle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 }
Chris Craik79793882014-04-25 13:08:25 -0700158
Chris Craik2414e1b2016-12-12 13:56:15 -0800159 static void addRoundRectXY(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
Antonio Calabrese24609582014-06-12 16:32:03 -0700160 jfloat right, jfloat bottom, jfloat rx, jfloat ry, jint dirHandle) {
161 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000162 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500163 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400164 obj->addRoundRect(rect, rx, ry, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700166
Chris Craik2414e1b2016-12-12 13:56:15 -0800167 static void addRoundRect8(JNIEnv* env, jclass clazz, jlong objHandle, jfloat left, jfloat top,
168 jfloat right, jfloat bottom, jfloatArray array, jint dirHandle) {
Antonio Calabrese24609582014-06-12 16:32:03 -0700169 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000170 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed15760c92019-11-25 11:43:48 -0500171 SkPathDirection dir = static_cast<SkPathDirection>(dirHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 AutoJavaFloatArray afa(env, array, 8);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400173#ifdef SK_SCALAR_IS_FLOAT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 const float* src = afa.ptr();
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400175#else
176 #error Need to convert float array to SkScalar array before calling the following function.
177#endif
178 obj->addRoundRect(rect, src, dir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700180
Chris Craik2414e1b2016-12-12 13:56:15 -0800181 static void addPath__PathFF(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle,
182 jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000183 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
184 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400185 obj->addPath(*src, dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700187
Chris Craik2414e1b2016-12-12 13:56:15 -0800188 static void addPath__Path(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000189 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
190 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 obj->addPath(*src);
192 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700193
Chris Craik2414e1b2016-12-12 13:56:15 -0800194 static void addPath__PathMatrix(JNIEnv* env, jclass clazz, jlong objHandle, jlong srcHandle,
195 jlong matrixHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000196 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
197 SkPath* src = reinterpret_cast<SkPath*>(srcHandle);
198 SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 obj->addPath(*src, *matrix);
200 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700201
Chris Craik2414e1b2016-12-12 13:56:15 -0800202 static void offset__FF(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000203 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400204 obj->offset(dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 }
206
Chris Craik2414e1b2016-12-12 13:56:15 -0800207 static void setLastPoint(JNIEnv* env, jclass clazz, jlong objHandle, jfloat dx, jfloat dy) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000208 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400209 obj->setLastPt(dx, dy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700211
Chris Craik2414e1b2016-12-12 13:56:15 -0800212 static void transform__MatrixPath(JNIEnv* env, jclass clazz, jlong objHandle, jlong matrixHandle,
213 jlong dstHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000214 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
215 SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
216 SkPath* dst = reinterpret_cast<SkPath*>(dstHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 obj->transform(*matrix, dst);
218 }
Antonio Calabrese24609582014-06-12 16:32:03 -0700219
Chris Craik2414e1b2016-12-12 13:56:15 -0800220 static void transform__Matrix(JNIEnv* env, jclass clazz, jlong objHandle, jlong matrixHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000221 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
222 SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 obj->transform(*matrix);
224 }
Romain Guy8018c8d2013-07-29 19:17:59 -0700225
Chris Craik2414e1b2016-12-12 13:56:15 -0800226 static jboolean op(JNIEnv* env, jclass clazz, jlong p1Handle, jlong p2Handle, jint opHandle,
227 jlong rHandle) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000228 SkPath* p1 = reinterpret_cast<SkPath*>(p1Handle);
229 SkPath* p2 = reinterpret_cast<SkPath*>(p2Handle);
230 SkPathOp op = static_cast<SkPathOp>(opHandle);
231 SkPath* r = reinterpret_cast<SkPath*>(rHandle);
232 return Op(*p1, *p2, op, r);
Romain Guy8018c8d2013-07-29 19:17:59 -0700233 }
George Mountc96c7b22013-08-23 13:31:31 -0700234
George Mountc96c7b22013-08-23 13:31:31 -0700235 typedef SkPoint (*bezierCalculation)(float t, const SkPoint* points);
236
237 static void addMove(std::vector<SkPoint>& segmentPoints, std::vector<float>& lengths,
238 const SkPoint& point) {
239 float length = 0;
240 if (!lengths.empty()) {
241 length = lengths.back();
242 }
243 segmentPoints.push_back(point);
244 lengths.push_back(length);
245 }
246
247 static void addLine(std::vector<SkPoint>& segmentPoints, std::vector<float>& lengths,
248 const SkPoint& toPoint) {
249 if (segmentPoints.empty()) {
250 segmentPoints.push_back(SkPoint::Make(0, 0));
251 lengths.push_back(0);
252 } else if (segmentPoints.back() == toPoint) {
253 return; // Empty line
254 }
255 float length = lengths.back() + SkPoint::Distance(segmentPoints.back(), toPoint);
256 segmentPoints.push_back(toPoint);
257 lengths.push_back(length);
258 }
259
260 static float cubicCoordinateCalculation(float t, float p0, float p1, float p2, float p3) {
261 float oneMinusT = 1 - t;
262 float oneMinusTSquared = oneMinusT * oneMinusT;
263 float oneMinusTCubed = oneMinusTSquared * oneMinusT;
264 float tSquared = t * t;
265 float tCubed = tSquared * t;
266 return (oneMinusTCubed * p0) + (3 * oneMinusTSquared * t * p1)
267 + (3 * oneMinusT * tSquared * p2) + (tCubed * p3);
268 }
269
270 static SkPoint cubicBezierCalculation(float t, const SkPoint* points) {
271 float x = cubicCoordinateCalculation(t, points[0].x(), points[1].x(),
272 points[2].x(), points[3].x());
273 float y = cubicCoordinateCalculation(t, points[0].y(), points[1].y(),
274 points[2].y(), points[3].y());
275 return SkPoint::Make(x, y);
276 }
277
278 static float quadraticCoordinateCalculation(float t, float p0, float p1, float p2) {
279 float oneMinusT = 1 - t;
280 return oneMinusT * ((oneMinusT * p0) + (t * p1)) + t * ((oneMinusT * p1) + (t * p2));
281 }
282
283 static SkPoint quadraticBezierCalculation(float t, const SkPoint* points) {
284 float x = quadraticCoordinateCalculation(t, points[0].x(), points[1].x(), points[2].x());
285 float y = quadraticCoordinateCalculation(t, points[0].y(), points[1].y(), points[2].y());
286 return SkPoint::Make(x, y);
287 }
288
289 // Subdivide a section of the Bezier curve, set the mid-point and the mid-t value.
290 // Returns true if further subdivision is necessary as defined by errorSquared.
291 static bool subdividePoints(const SkPoint* points, bezierCalculation bezierFunction,
292 float t0, const SkPoint &p0, float t1, const SkPoint &p1,
293 float& midT, SkPoint &midPoint, float errorSquared) {
294 midT = (t1 + t0) / 2;
295 float midX = (p1.x() + p0.x()) / 2;
296 float midY = (p1.y() + p0.y()) / 2;
297
298 midPoint = (*bezierFunction)(midT, points);
299 float xError = midPoint.x() - midX;
300 float yError = midPoint.y() - midY;
301 float midErrorSquared = (xError * xError) + (yError * yError);
302 return midErrorSquared > errorSquared;
303 }
304
305 // Divides Bezier curves until linear interpolation is very close to accurate, using
306 // errorSquared as a metric. Cubic Bezier curves can have an inflection point that improperly
307 // short-circuit subdivision. If you imagine an S shape, the top and bottom points being the
308 // starting and end points, linear interpolation would mark the center where the curve places
309 // the point. It is clearly not the case that we can linearly interpolate at that point.
310 // doubleCheckDivision forces a second examination between subdivisions to ensure that linear
311 // interpolation works.
312 static void addBezier(const SkPoint* points,
313 bezierCalculation bezierFunction, std::vector<SkPoint>& segmentPoints,
314 std::vector<float>& lengths, float errorSquared, bool doubleCheckDivision) {
315 typedef std::map<float, SkPoint> PointMap;
316 PointMap tToPoint;
317
318 tToPoint[0] = (*bezierFunction)(0, points);
319 tToPoint[1] = (*bezierFunction)(1, points);
320
321 PointMap::iterator iter = tToPoint.begin();
322 PointMap::iterator next = iter;
323 ++next;
324 while (next != tToPoint.end()) {
325 bool needsSubdivision = true;
326 SkPoint midPoint;
327 do {
328 float midT;
329 needsSubdivision = subdividePoints(points, bezierFunction, iter->first,
330 iter->second, next->first, next->second, midT, midPoint, errorSquared);
331 if (!needsSubdivision && doubleCheckDivision) {
332 SkPoint quarterPoint;
333 float quarterT;
334 needsSubdivision = subdividePoints(points, bezierFunction, iter->first,
335 iter->second, midT, midPoint, quarterT, quarterPoint, errorSquared);
336 if (needsSubdivision) {
337 // Found an inflection point. No need to double-check.
338 doubleCheckDivision = false;
339 }
340 }
341 if (needsSubdivision) {
342 next = tToPoint.insert(iter, PointMap::value_type(midT, midPoint));
343 }
344 } while (needsSubdivision);
345 iter = next;
346 next++;
347 }
348
349 // Now that each division can use linear interpolation with less than the allowed error
350 for (iter = tToPoint.begin(); iter != tToPoint.end(); ++iter) {
351 addLine(segmentPoints, lengths, iter->second);
352 }
353 }
354
Chris Craik96202d52017-04-27 15:26:17 -0700355 static void createVerbSegments(const SkPath::Iter& pathIter, SkPath::Verb verb,
356 const SkPoint* points, std::vector<SkPoint>& segmentPoints,
357 std::vector<float>& lengths, float errorSquared, float errorConic) {
George Mountc96c7b22013-08-23 13:31:31 -0700358 switch (verb) {
359 case SkPath::kMove_Verb:
360 addMove(segmentPoints, lengths, points[0]);
361 break;
362 case SkPath::kClose_Verb:
George Mount7fe03a22013-11-22 17:08:28 -0800363 addLine(segmentPoints, lengths, points[0]);
364 break;
George Mountc96c7b22013-08-23 13:31:31 -0700365 case SkPath::kLine_Verb:
366 addLine(segmentPoints, lengths, points[1]);
367 break;
368 case SkPath::kQuad_Verb:
369 addBezier(points, quadraticBezierCalculation, segmentPoints, lengths,
370 errorSquared, false);
371 break;
372 case SkPath::kCubic_Verb:
373 addBezier(points, cubicBezierCalculation, segmentPoints, lengths,
374 errorSquared, true);
375 break;
Chris Craik96202d52017-04-27 15:26:17 -0700376 case SkPath::kConic_Verb: {
377 SkAutoConicToQuads converter;
378 const SkPoint* quads = converter.computeQuads(
379 points, pathIter.conicWeight(), errorConic);
380 for (int i = 0; i < converter.countQuads(); i++) {
381 // Note: offset each subsequent quad by 2, since end points are shared
382 const SkPoint* quad = quads + i * 2;
383 addBezier(quad, quadraticBezierCalculation, segmentPoints, lengths,
384 errorConic, false);
385 }
386 break;
387 }
George Mountc96c7b22013-08-23 13:31:31 -0700388 default:
Chris Craik96202d52017-04-27 15:26:17 -0700389 static_assert(SkPath::kMove_Verb == 0
390 && SkPath::kLine_Verb == 1
391 && SkPath::kQuad_Verb == 2
392 && SkPath::kConic_Verb == 3
393 && SkPath::kCubic_Verb == 4
394 && SkPath::kClose_Verb == 5
395 && SkPath::kDone_Verb == 6,
396 "Path enum changed, new types may have been added.");
George Mountc96c7b22013-08-23 13:31:31 -0700397 break;
398 }
399 }
400
401 // Returns a float[] with each point along the path represented by 3 floats
402 // * fractional length along the path that the point resides
403 // * x coordinate
404 // * y coordinate
405 // Note that more than one point may have the same length along the path in
406 // the case of a move.
407 // NULL can be returned if the Path is empty.
Chris Craik2414e1b2016-12-12 13:56:15 -0800408 static jfloatArray approximate(JNIEnv* env, jclass clazz, jlong pathHandle,
409 float acceptableError) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000410 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
George Mountc96c7b22013-08-23 13:31:31 -0700411 SkASSERT(path);
412 SkPath::Iter pathIter(*path, false);
413 SkPath::Verb verb;
414 SkPoint points[4];
415 std::vector<SkPoint> segmentPoints;
416 std::vector<float> lengths;
417 float errorSquared = acceptableError * acceptableError;
Chris Craik96202d52017-04-27 15:26:17 -0700418 float errorConic = acceptableError / 2; // somewhat arbitrary
George Mountc96c7b22013-08-23 13:31:31 -0700419
Mike Reed7d489692019-11-27 11:15:43 -0500420 while ((verb = pathIter.next(points)) != SkPath::kDone_Verb) {
Chris Craik96202d52017-04-27 15:26:17 -0700421 createVerbSegments(pathIter, verb, points, segmentPoints, lengths,
422 errorSquared, errorConic);
George Mountc96c7b22013-08-23 13:31:31 -0700423 }
424
425 if (segmentPoints.empty()) {
George Mount68cfdad2014-09-11 10:25:31 -0700426 int numVerbs = path->countVerbs();
427 if (numVerbs == 1) {
428 addMove(segmentPoints, lengths, path->getPoint(0));
429 } else {
430 // Invalid or empty path. Fall back to point(0,0)
431 addMove(segmentPoints, lengths, SkPoint());
432 }
433 }
434
435 float totalLength = lengths.back();
436 if (totalLength == 0) {
437 // Lone Move instructions should still be able to animate at the same value.
438 segmentPoints.push_back(segmentPoints.back());
439 lengths.push_back(1);
440 totalLength = 1;
George Mountc96c7b22013-08-23 13:31:31 -0700441 }
442
443 size_t numPoints = segmentPoints.size();
444 size_t approximationArraySize = numPoints * 3;
445
446 float* approximation = new float[approximationArraySize];
George Mountc96c7b22013-08-23 13:31:31 -0700447
448 int approximationIndex = 0;
George Mount7fe03a22013-11-22 17:08:28 -0800449 for (size_t i = 0; i < numPoints; i++) {
George Mountc96c7b22013-08-23 13:31:31 -0700450 const SkPoint& point = segmentPoints[i];
451 approximation[approximationIndex++] = lengths[i] / totalLength;
452 approximation[approximationIndex++] = point.x();
453 approximation[approximationIndex++] = point.y();
454 }
455
456 jfloatArray result = env->NewFloatArray(approximationArraySize);
457 env->SetFloatArrayRegion(result, 0, approximationArraySize, approximation);
458 delete[] approximation;
459 return result;
460 }
Chris Craik2414e1b2016-12-12 13:56:15 -0800461
462 // ---------------- @FastNative -----------------------------
463
464 static jboolean isRect(JNIEnv* env, jclass clazz, jlong objHandle, jobject jrect) {
465 SkRect rect;
466 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
467 jboolean result = obj->isRect(&rect);
John Reck205d83e2018-03-28 14:36:58 -0700468 if (jrect) {
469 GraphicsJNI::rect_to_jrectf(rect, env, jrect);
470 }
Chris Craik2414e1b2016-12-12 13:56:15 -0800471 return result;
472 }
473
474 // ---------------- @CriticalNative -------------------------
475
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100476 static void reset(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800477 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
478 obj->reset();
479 }
480
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100481 static void rewind(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800482 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
483 obj->rewind();
484 }
485
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100486 static jboolean isEmpty(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800487 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
488 return obj->isEmpty();
489 }
490
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100491 static jboolean isConvex(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800492 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
493 return obj->isConvex();
494 }
495
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100496 static jint getFillType(CRITICAL_JNI_PARAMS_COMMA jlong objHandle) {
Chris Craik2414e1b2016-12-12 13:56:15 -0800497 SkPath* obj = reinterpret_cast<SkPath*>(objHandle);
Mike Reed6a8bf8e2019-12-03 13:01:07 -0500498 return static_cast<int>(obj->getFillType());
Chris Craik2414e1b2016-12-12 13:56:15 -0800499 }
500
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100501 static void setFillType(CRITICAL_JNI_PARAMS_COMMA jlong pathHandle, jint ftHandle) {;
Chris Craik2414e1b2016-12-12 13:56:15 -0800502 SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
Mike Reed6a8bf8e2019-12-03 13:01:07 -0500503 SkPathFillType ft = static_cast<SkPathFillType>(ftHandle);
Chris Craik2414e1b2016-12-12 13:56:15 -0800504 path->setFillType(ft);
505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506};
507
Daniel Micay76f6a862015-09-19 17:31:01 -0400508static const JNINativeMethod methods[] = {
Chris Craik2414e1b2016-12-12 13:56:15 -0800509 {"nInit","()J", (void*) SkPathGlue::init},
510 {"nInit","(J)J", (void*) SkPathGlue::init_Path},
John Reck205d83e2018-03-28 14:36:58 -0700511 {"nGetFinalizer", "()J", (void*) SkPathGlue::getFinalizer},
Chris Craik2414e1b2016-12-12 13:56:15 -0800512 {"nSet","(JJ)V", (void*) SkPathGlue::set},
513 {"nComputeBounds","(JLandroid/graphics/RectF;)V", (void*) SkPathGlue::computeBounds},
514 {"nIncReserve","(JI)V", (void*) SkPathGlue::incReserve},
515 {"nMoveTo","(JFF)V", (void*) SkPathGlue::moveTo__FF},
516 {"nRMoveTo","(JFF)V", (void*) SkPathGlue::rMoveTo},
517 {"nLineTo","(JFF)V", (void*) SkPathGlue::lineTo__FF},
518 {"nRLineTo","(JFF)V", (void*) SkPathGlue::rLineTo},
519 {"nQuadTo","(JFFFF)V", (void*) SkPathGlue::quadTo__FFFF},
520 {"nRQuadTo","(JFFFF)V", (void*) SkPathGlue::rQuadTo},
521 {"nCubicTo","(JFFFFFF)V", (void*) SkPathGlue::cubicTo__FFFFFF},
522 {"nRCubicTo","(JFFFFFF)V", (void*) SkPathGlue::rCubicTo},
523 {"nArcTo","(JFFFFFFZ)V", (void*) SkPathGlue::arcTo},
524 {"nClose","(J)V", (void*) SkPathGlue::close},
525 {"nAddRect","(JFFFFI)V", (void*) SkPathGlue::addRect},
526 {"nAddOval","(JFFFFI)V", (void*) SkPathGlue::addOval},
527 {"nAddCircle","(JFFFI)V", (void*) SkPathGlue::addCircle},
528 {"nAddArc","(JFFFFFF)V", (void*) SkPathGlue::addArc},
529 {"nAddRoundRect","(JFFFFFFI)V", (void*) SkPathGlue::addRoundRectXY},
530 {"nAddRoundRect","(JFFFF[FI)V", (void*) SkPathGlue::addRoundRect8},
531 {"nAddPath","(JJFF)V", (void*) SkPathGlue::addPath__PathFF},
532 {"nAddPath","(JJ)V", (void*) SkPathGlue::addPath__Path},
533 {"nAddPath","(JJJ)V", (void*) SkPathGlue::addPath__PathMatrix},
534 {"nOffset","(JFF)V", (void*) SkPathGlue::offset__FF},
535 {"nSetLastPoint","(JFF)V", (void*) SkPathGlue::setLastPoint},
536 {"nTransform","(JJJ)V", (void*) SkPathGlue::transform__MatrixPath},
537 {"nTransform","(JJ)V", (void*) SkPathGlue::transform__Matrix},
538 {"nOp","(JJIJ)Z", (void*) SkPathGlue::op},
539 {"nApproximate", "(JF)[F", (void*) SkPathGlue::approximate},
540
541 // ------- @FastNative below here ----------------------
542 {"nIsRect","(JLandroid/graphics/RectF;)Z", (void*) SkPathGlue::isRect},
543
544 // ------- @CriticalNative below here ------------------
545 {"nReset","(J)V", (void*) SkPathGlue::reset},
546 {"nRewind","(J)V", (void*) SkPathGlue::rewind},
547 {"nIsEmpty","(J)Z", (void*) SkPathGlue::isEmpty},
548 {"nIsConvex","(J)Z", (void*) SkPathGlue::isConvex},
549 {"nGetFillType","(J)I", (void*) SkPathGlue::getFillType},
550 {"nSetFillType","(JI)V", (void*) SkPathGlue::setFillType},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551};
552
553int register_android_graphics_Path(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800554 return RegisterMethodsOrDie(env, "android/graphics/Path", methods, NELEM(methods));
Derek Sollenberger1ad545d2015-08-20 14:23:14 -0400555
Mike Reed15760c92019-11-25 11:43:48 -0500556 static_assert(0 == (int)SkPathDirection::kCW, "direction_mismatch");
557 static_assert(1 == (int)SkPathDirection::kCCW, "direction_mismatch");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558}
559
560}