The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* libs/android_runtime/android/graphics/Path.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | // This file was generated from the C++ include file: SkPath.h |
| 19 | // Any changes made to this file will be discarded by the build. |
| 20 | // To change this file, either edit the include, or device/tools/gluemaker/main.cpp, |
| 21 | // or one of the auxilary file specifications in device/tools/gluemaker. |
| 22 | |
| 23 | #include "jni.h" |
| 24 | #include "GraphicsJNI.h" |
| 25 | #include <android_runtime/AndroidRuntime.h> |
| 26 | |
| 27 | #include "SkPath.h" |
Romain Guy | 8018c8d | 2013-07-29 19:17:59 -0700 | [diff] [blame] | 28 | #include "pathops/SkPathOps.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 30 | #include <Caches.h> |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 31 | #include <vector> |
| 32 | #include <map> |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 33 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | namespace android { |
| 35 | |
| 36 | class SkPathGlue { |
| 37 | public: |
| 38 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 39 | static void finalizer(JNIEnv* env, jobject clazz, jlong objHandle) { |
| 40 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 41 | #ifdef USE_OPENGL_RENDERER |
| 42 | if (android::uirenderer::Caches::hasInstance()) { |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 43 | android::uirenderer::Caches::getInstance().resourceCache.destructor(obj); |
| 44 | return; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 45 | } |
| 46 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | delete obj; |
| 48 | } |
| 49 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 50 | static jlong init1(JNIEnv* env, jobject clazz) { |
| 51 | return reinterpret_cast<jlong>(new SkPath()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 53 | |
| 54 | static jlong init2(JNIEnv* env, jobject clazz, jlong valHandle) { |
| 55 | SkPath* val = reinterpret_cast<SkPath*>(valHandle); |
| 56 | return reinterpret_cast<jlong>(new SkPath(*val)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 58 | |
| 59 | static void reset(JNIEnv* env, jobject clazz, jlong objHandle) { |
| 60 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | obj->reset(); |
| 62 | } |
| 63 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 64 | static void rewind(JNIEnv* env, jobject clazz, jlong objHandle) { |
| 65 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | obj->rewind(); |
| 67 | } |
| 68 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 69 | static void assign(JNIEnv* env, jobject clazz, jlong dstHandle, jlong srcHandle) { |
| 70 | SkPath* dst = reinterpret_cast<SkPath*>(dstHandle); |
| 71 | const SkPath* src = reinterpret_cast<SkPath*>(srcHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | *dst = *src; |
| 73 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 74 | |
| 75 | static jint getFillType(JNIEnv* env, jobject clazz, jlong objHandle) { |
| 76 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | return obj->getFillType(); |
| 78 | } |
| 79 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 80 | static void setFillType(JNIEnv* env, jobject clazz, jlong pathHandle, jint ftHandle) { |
| 81 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
| 82 | SkPath::FillType ft = static_cast<SkPath::FillType>(ftHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | path->setFillType(ft); |
| 84 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 85 | |
| 86 | static jboolean isEmpty(JNIEnv* env, jobject clazz, jlong objHandle) { |
| 87 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | return obj->isEmpty(); |
| 89 | } |
| 90 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 91 | static jboolean isRect(JNIEnv* env, jobject clazz, jlong objHandle, jobject rect) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | SkRect rect_; |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 93 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | jboolean result = obj->isRect(&rect_); |
| 95 | GraphicsJNI::rect_to_jrectf(rect_, env, rect); |
| 96 | return result; |
| 97 | } |
| 98 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 99 | static void computeBounds(JNIEnv* env, jobject clazz, jlong objHandle, jobject bounds) { |
| 100 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
Mike Reed | 8ba8877 | 2009-04-02 09:06:19 -0700 | [diff] [blame] | 101 | const SkRect& bounds_ = obj->getBounds(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | GraphicsJNI::rect_to_jrectf(bounds_, env, bounds); |
| 103 | } |
| 104 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 105 | static void incReserve(JNIEnv* env, jobject clazz, jlong objHandle, jint extraPtCount) { |
| 106 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | obj->incReserve(extraPtCount); |
| 108 | } |
| 109 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 110 | static void moveTo__FF(JNIEnv* env, jobject clazz, jlong objHandle, jfloat x, jfloat y) { |
| 111 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | SkScalar x_ = SkFloatToScalar(x); |
| 113 | SkScalar y_ = SkFloatToScalar(y); |
| 114 | obj->moveTo(x_, y_); |
| 115 | } |
| 116 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 117 | static void rMoveTo(JNIEnv* env, jobject clazz, jlong objHandle, jfloat dx, jfloat dy) { |
| 118 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | SkScalar dx_ = SkFloatToScalar(dx); |
| 120 | SkScalar dy_ = SkFloatToScalar(dy); |
| 121 | obj->rMoveTo(dx_, dy_); |
| 122 | } |
| 123 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 124 | static void lineTo__FF(JNIEnv* env, jobject clazz, jlong objHandle, jfloat x, jfloat y) { |
| 125 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | SkScalar x_ = SkFloatToScalar(x); |
| 127 | SkScalar y_ = SkFloatToScalar(y); |
| 128 | obj->lineTo(x_, y_); |
| 129 | } |
| 130 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 131 | static void rLineTo(JNIEnv* env, jobject clazz, jlong objHandle, jfloat dx, jfloat dy) { |
| 132 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | SkScalar dx_ = SkFloatToScalar(dx); |
| 134 | SkScalar dy_ = SkFloatToScalar(dy); |
| 135 | obj->rLineTo(dx_, dy_); |
| 136 | } |
| 137 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 138 | static void quadTo__FFFF(JNIEnv* env, jobject clazz, jlong objHandle, jfloat x1, jfloat y1, jfloat x2, jfloat y2) { |
| 139 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | SkScalar x1_ = SkFloatToScalar(x1); |
| 141 | SkScalar y1_ = SkFloatToScalar(y1); |
| 142 | SkScalar x2_ = SkFloatToScalar(x2); |
| 143 | SkScalar y2_ = SkFloatToScalar(y2); |
| 144 | obj->quadTo(x1_, y1_, x2_, y2_); |
| 145 | } |
| 146 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 147 | static void rQuadTo(JNIEnv* env, jobject clazz, jlong objHandle, jfloat dx1, jfloat dy1, jfloat dx2, jfloat dy2) { |
| 148 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | SkScalar dx1_ = SkFloatToScalar(dx1); |
| 150 | SkScalar dy1_ = SkFloatToScalar(dy1); |
| 151 | SkScalar dx2_ = SkFloatToScalar(dx2); |
| 152 | SkScalar dy2_ = SkFloatToScalar(dy2); |
| 153 | obj->rQuadTo(dx1_, dy1_, dx2_, dy2_); |
| 154 | } |
| 155 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 156 | static void cubicTo__FFFFFF(JNIEnv* env, jobject clazz, jlong objHandle, jfloat x1, jfloat y1, jfloat x2, jfloat y2, jfloat x3, jfloat y3) { |
| 157 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | SkScalar x1_ = SkFloatToScalar(x1); |
| 159 | SkScalar y1_ = SkFloatToScalar(y1); |
| 160 | SkScalar x2_ = SkFloatToScalar(x2); |
| 161 | SkScalar y2_ = SkFloatToScalar(y2); |
| 162 | SkScalar x3_ = SkFloatToScalar(x3); |
| 163 | SkScalar y3_ = SkFloatToScalar(y3); |
| 164 | obj->cubicTo(x1_, y1_, x2_, y2_, x3_, y3_); |
| 165 | } |
| 166 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 167 | static void rCubicTo(JNIEnv* env, jobject clazz, jlong objHandle, jfloat x1, jfloat y1, jfloat x2, jfloat y2, jfloat x3, jfloat y3) { |
| 168 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | SkScalar x1_ = SkFloatToScalar(x1); |
| 170 | SkScalar y1_ = SkFloatToScalar(y1); |
| 171 | SkScalar x2_ = SkFloatToScalar(x2); |
| 172 | SkScalar y2_ = SkFloatToScalar(y2); |
| 173 | SkScalar x3_ = SkFloatToScalar(x3); |
| 174 | SkScalar y3_ = SkFloatToScalar(y3); |
| 175 | obj->rCubicTo(x1_, y1_, x2_, y2_, x3_, y3_); |
| 176 | } |
| 177 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 178 | static void arcTo(JNIEnv* env, jobject clazz, jlong objHandle, jobject oval, jfloat startAngle, jfloat sweepAngle, jboolean forceMoveTo) { |
| 179 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | SkRect oval_; |
| 181 | GraphicsJNI::jrectf_to_rect(env, oval, &oval_); |
| 182 | SkScalar startAngle_ = SkFloatToScalar(startAngle); |
| 183 | SkScalar sweepAngle_ = SkFloatToScalar(sweepAngle); |
| 184 | obj->arcTo(oval_, startAngle_, sweepAngle_, forceMoveTo); |
| 185 | } |
| 186 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 187 | static void close(JNIEnv* env, jobject clazz, jlong objHandle) { |
| 188 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | obj->close(); |
| 190 | } |
| 191 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 192 | static void addRect__RectFI(JNIEnv* env, jobject clazz, jlong objHandle, jobject rect, jint dirHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | SkRect rect_; |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 194 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
| 195 | SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | GraphicsJNI::jrectf_to_rect(env, rect, &rect_); |
| 197 | obj->addRect(rect_, dir); |
| 198 | } |
| 199 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 200 | static void addRect__FFFFI(JNIEnv* env, jobject clazz, jlong objHandle, jfloat left, jfloat top, jfloat right, jfloat bottom, jint dirHandle) { |
| 201 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
| 202 | SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | SkScalar left_ = SkFloatToScalar(left); |
| 204 | SkScalar top_ = SkFloatToScalar(top); |
| 205 | SkScalar right_ = SkFloatToScalar(right); |
| 206 | SkScalar bottom_ = SkFloatToScalar(bottom); |
| 207 | obj->addRect(left_, top_, right_, bottom_, dir); |
| 208 | } |
| 209 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 210 | static void addOval(JNIEnv* env, jobject clazz, jlong objHandle, jobject oval, jint dirHandle) { |
| 211 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
| 212 | SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | SkRect oval_; |
| 214 | GraphicsJNI::jrectf_to_rect(env, oval, &oval_); |
| 215 | obj->addOval(oval_, dir); |
| 216 | } |
| 217 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 218 | static void addCircle(JNIEnv* env, jobject clazz, jlong objHandle, jfloat x, jfloat y, jfloat radius, jint dirHandle) { |
| 219 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
| 220 | SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | SkScalar x_ = SkFloatToScalar(x); |
| 222 | SkScalar y_ = SkFloatToScalar(y); |
| 223 | SkScalar radius_ = SkFloatToScalar(radius); |
| 224 | obj->addCircle(x_, y_, radius_, dir); |
| 225 | } |
| 226 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 227 | static void addArc(JNIEnv* env, jobject clazz, jlong objHandle, jobject oval, jfloat startAngle, jfloat sweepAngle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | SkRect oval_; |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 229 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | GraphicsJNI::jrectf_to_rect(env, oval, &oval_); |
| 231 | SkScalar startAngle_ = SkFloatToScalar(startAngle); |
| 232 | SkScalar sweepAngle_ = SkFloatToScalar(sweepAngle); |
| 233 | obj->addArc(oval_, startAngle_, sweepAngle_); |
| 234 | } |
| 235 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 236 | static void addRoundRectXY(JNIEnv* env, jobject clazz, jlong objHandle, jobject rect, |
| 237 | jfloat rx, jfloat ry, jint dirHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | SkRect rect_; |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 239 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
| 240 | SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | GraphicsJNI::jrectf_to_rect(env, rect, &rect_); |
| 242 | SkScalar rx_ = SkFloatToScalar(rx); |
| 243 | SkScalar ry_ = SkFloatToScalar(ry); |
| 244 | obj->addRoundRect(rect_, rx_, ry_, dir); |
| 245 | } |
| 246 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 247 | static void addRoundRect8(JNIEnv* env, jobject, jlong objHandle, jobject rect, |
| 248 | jfloatArray array, jint dirHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | SkRect rect_; |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 250 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
| 251 | SkPath::Direction dir = static_cast<SkPath::Direction>(dirHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | GraphicsJNI::jrectf_to_rect(env, rect, &rect_); |
| 253 | AutoJavaFloatArray afa(env, array, 8); |
| 254 | const float* src = afa.ptr(); |
| 255 | SkScalar dst[8]; |
| 256 | |
| 257 | for (int i = 0; i < 8; i++) { |
| 258 | dst[i] = SkFloatToScalar(src[i]); |
| 259 | } |
| 260 | obj->addRoundRect(rect_, dst, dir); |
| 261 | } |
| 262 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 263 | static void addPath__PathFF(JNIEnv* env, jobject clazz, jlong objHandle, jlong srcHandle, jfloat dx, jfloat dy) { |
| 264 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
| 265 | SkPath* src = reinterpret_cast<SkPath*>(srcHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | SkScalar dx_ = SkFloatToScalar(dx); |
| 267 | SkScalar dy_ = SkFloatToScalar(dy); |
| 268 | obj->addPath(*src, dx_, dy_); |
| 269 | } |
| 270 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 271 | static void addPath__Path(JNIEnv* env, jobject clazz, jlong objHandle, jlong srcHandle) { |
| 272 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
| 273 | SkPath* src = reinterpret_cast<SkPath*>(srcHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | obj->addPath(*src); |
| 275 | } |
| 276 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 277 | static void addPath__PathMatrix(JNIEnv* env, jobject clazz, jlong objHandle, jlong srcHandle, jlong matrixHandle) { |
| 278 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
| 279 | SkPath* src = reinterpret_cast<SkPath*>(srcHandle); |
| 280 | SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | obj->addPath(*src, *matrix); |
| 282 | } |
| 283 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 284 | static void offset__FFPath(JNIEnv* env, jobject clazz, jlong objHandle, jfloat dx, jfloat dy, jlong dstHandle) { |
| 285 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
| 286 | SkPath* dst = reinterpret_cast<SkPath*>(dstHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | SkScalar dx_ = SkFloatToScalar(dx); |
| 288 | SkScalar dy_ = SkFloatToScalar(dy); |
| 289 | obj->offset(dx_, dy_, dst); |
| 290 | } |
| 291 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 292 | static void offset__FF(JNIEnv* env, jobject clazz, jlong objHandle, jfloat dx, jfloat dy) { |
| 293 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | SkScalar dx_ = SkFloatToScalar(dx); |
| 295 | SkScalar dy_ = SkFloatToScalar(dy); |
| 296 | obj->offset(dx_, dy_); |
| 297 | } |
| 298 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 299 | static void setLastPoint(JNIEnv* env, jobject clazz, jlong objHandle, jfloat dx, jfloat dy) { |
| 300 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | SkScalar dx_ = SkFloatToScalar(dx); |
| 302 | SkScalar dy_ = SkFloatToScalar(dy); |
| 303 | obj->setLastPt(dx_, dy_); |
| 304 | } |
| 305 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 306 | static void transform__MatrixPath(JNIEnv* env, jobject clazz, jlong objHandle, jlong matrixHandle, jlong dstHandle) { |
| 307 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
| 308 | SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle); |
| 309 | SkPath* dst = reinterpret_cast<SkPath*>(dstHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | obj->transform(*matrix, dst); |
| 311 | } |
| 312 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 313 | static void transform__Matrix(JNIEnv* env, jobject clazz, jlong objHandle, jlong matrixHandle) { |
| 314 | SkPath* obj = reinterpret_cast<SkPath*>(objHandle); |
| 315 | SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | obj->transform(*matrix); |
| 317 | } |
Romain Guy | 8018c8d | 2013-07-29 19:17:59 -0700 | [diff] [blame] | 318 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 319 | static jboolean op(JNIEnv* env, jobject clazz, jlong p1Handle, jlong p2Handle, jint opHandle, jlong rHandle) { |
| 320 | SkPath* p1 = reinterpret_cast<SkPath*>(p1Handle); |
| 321 | SkPath* p2 = reinterpret_cast<SkPath*>(p2Handle); |
| 322 | SkPathOp op = static_cast<SkPathOp>(opHandle); |
| 323 | SkPath* r = reinterpret_cast<SkPath*>(rHandle); |
| 324 | return Op(*p1, *p2, op, r); |
Romain Guy | 8018c8d | 2013-07-29 19:17:59 -0700 | [diff] [blame] | 325 | } |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 326 | |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 327 | typedef SkPoint (*bezierCalculation)(float t, const SkPoint* points); |
| 328 | |
| 329 | static void addMove(std::vector<SkPoint>& segmentPoints, std::vector<float>& lengths, |
| 330 | const SkPoint& point) { |
| 331 | float length = 0; |
| 332 | if (!lengths.empty()) { |
| 333 | length = lengths.back(); |
| 334 | } |
| 335 | segmentPoints.push_back(point); |
| 336 | lengths.push_back(length); |
| 337 | } |
| 338 | |
| 339 | static void addLine(std::vector<SkPoint>& segmentPoints, std::vector<float>& lengths, |
| 340 | const SkPoint& toPoint) { |
| 341 | if (segmentPoints.empty()) { |
| 342 | segmentPoints.push_back(SkPoint::Make(0, 0)); |
| 343 | lengths.push_back(0); |
| 344 | } else if (segmentPoints.back() == toPoint) { |
| 345 | return; // Empty line |
| 346 | } |
| 347 | float length = lengths.back() + SkPoint::Distance(segmentPoints.back(), toPoint); |
| 348 | segmentPoints.push_back(toPoint); |
| 349 | lengths.push_back(length); |
| 350 | } |
| 351 | |
| 352 | static float cubicCoordinateCalculation(float t, float p0, float p1, float p2, float p3) { |
| 353 | float oneMinusT = 1 - t; |
| 354 | float oneMinusTSquared = oneMinusT * oneMinusT; |
| 355 | float oneMinusTCubed = oneMinusTSquared * oneMinusT; |
| 356 | float tSquared = t * t; |
| 357 | float tCubed = tSquared * t; |
| 358 | return (oneMinusTCubed * p0) + (3 * oneMinusTSquared * t * p1) |
| 359 | + (3 * oneMinusT * tSquared * p2) + (tCubed * p3); |
| 360 | } |
| 361 | |
| 362 | static SkPoint cubicBezierCalculation(float t, const SkPoint* points) { |
| 363 | float x = cubicCoordinateCalculation(t, points[0].x(), points[1].x(), |
| 364 | points[2].x(), points[3].x()); |
| 365 | float y = cubicCoordinateCalculation(t, points[0].y(), points[1].y(), |
| 366 | points[2].y(), points[3].y()); |
| 367 | return SkPoint::Make(x, y); |
| 368 | } |
| 369 | |
| 370 | static float quadraticCoordinateCalculation(float t, float p0, float p1, float p2) { |
| 371 | float oneMinusT = 1 - t; |
| 372 | return oneMinusT * ((oneMinusT * p0) + (t * p1)) + t * ((oneMinusT * p1) + (t * p2)); |
| 373 | } |
| 374 | |
| 375 | static SkPoint quadraticBezierCalculation(float t, const SkPoint* points) { |
| 376 | float x = quadraticCoordinateCalculation(t, points[0].x(), points[1].x(), points[2].x()); |
| 377 | float y = quadraticCoordinateCalculation(t, points[0].y(), points[1].y(), points[2].y()); |
| 378 | return SkPoint::Make(x, y); |
| 379 | } |
| 380 | |
| 381 | // Subdivide a section of the Bezier curve, set the mid-point and the mid-t value. |
| 382 | // Returns true if further subdivision is necessary as defined by errorSquared. |
| 383 | static bool subdividePoints(const SkPoint* points, bezierCalculation bezierFunction, |
| 384 | float t0, const SkPoint &p0, float t1, const SkPoint &p1, |
| 385 | float& midT, SkPoint &midPoint, float errorSquared) { |
| 386 | midT = (t1 + t0) / 2; |
| 387 | float midX = (p1.x() + p0.x()) / 2; |
| 388 | float midY = (p1.y() + p0.y()) / 2; |
| 389 | |
| 390 | midPoint = (*bezierFunction)(midT, points); |
| 391 | float xError = midPoint.x() - midX; |
| 392 | float yError = midPoint.y() - midY; |
| 393 | float midErrorSquared = (xError * xError) + (yError * yError); |
| 394 | return midErrorSquared > errorSquared; |
| 395 | } |
| 396 | |
| 397 | // Divides Bezier curves until linear interpolation is very close to accurate, using |
| 398 | // errorSquared as a metric. Cubic Bezier curves can have an inflection point that improperly |
| 399 | // short-circuit subdivision. If you imagine an S shape, the top and bottom points being the |
| 400 | // starting and end points, linear interpolation would mark the center where the curve places |
| 401 | // the point. It is clearly not the case that we can linearly interpolate at that point. |
| 402 | // doubleCheckDivision forces a second examination between subdivisions to ensure that linear |
| 403 | // interpolation works. |
| 404 | static void addBezier(const SkPoint* points, |
| 405 | bezierCalculation bezierFunction, std::vector<SkPoint>& segmentPoints, |
| 406 | std::vector<float>& lengths, float errorSquared, bool doubleCheckDivision) { |
| 407 | typedef std::map<float, SkPoint> PointMap; |
| 408 | PointMap tToPoint; |
| 409 | |
| 410 | tToPoint[0] = (*bezierFunction)(0, points); |
| 411 | tToPoint[1] = (*bezierFunction)(1, points); |
| 412 | |
| 413 | PointMap::iterator iter = tToPoint.begin(); |
| 414 | PointMap::iterator next = iter; |
| 415 | ++next; |
| 416 | while (next != tToPoint.end()) { |
| 417 | bool needsSubdivision = true; |
| 418 | SkPoint midPoint; |
| 419 | do { |
| 420 | float midT; |
| 421 | needsSubdivision = subdividePoints(points, bezierFunction, iter->first, |
| 422 | iter->second, next->first, next->second, midT, midPoint, errorSquared); |
| 423 | if (!needsSubdivision && doubleCheckDivision) { |
| 424 | SkPoint quarterPoint; |
| 425 | float quarterT; |
| 426 | needsSubdivision = subdividePoints(points, bezierFunction, iter->first, |
| 427 | iter->second, midT, midPoint, quarterT, quarterPoint, errorSquared); |
| 428 | if (needsSubdivision) { |
| 429 | // Found an inflection point. No need to double-check. |
| 430 | doubleCheckDivision = false; |
| 431 | } |
| 432 | } |
| 433 | if (needsSubdivision) { |
| 434 | next = tToPoint.insert(iter, PointMap::value_type(midT, midPoint)); |
| 435 | } |
| 436 | } while (needsSubdivision); |
| 437 | iter = next; |
| 438 | next++; |
| 439 | } |
| 440 | |
| 441 | // Now that each division can use linear interpolation with less than the allowed error |
| 442 | for (iter = tToPoint.begin(); iter != tToPoint.end(); ++iter) { |
| 443 | addLine(segmentPoints, lengths, iter->second); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | static void createVerbSegments(SkPath::Verb verb, const SkPoint* points, |
| 448 | std::vector<SkPoint>& segmentPoints, std::vector<float>& lengths, float errorSquared) { |
| 449 | switch (verb) { |
| 450 | case SkPath::kMove_Verb: |
| 451 | addMove(segmentPoints, lengths, points[0]); |
| 452 | break; |
| 453 | case SkPath::kClose_Verb: |
George Mount | 7fe03a2 | 2013-11-22 17:08:28 -0800 | [diff] [blame] | 454 | addLine(segmentPoints, lengths, points[0]); |
| 455 | break; |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 456 | case SkPath::kLine_Verb: |
| 457 | addLine(segmentPoints, lengths, points[1]); |
| 458 | break; |
| 459 | case SkPath::kQuad_Verb: |
| 460 | addBezier(points, quadraticBezierCalculation, segmentPoints, lengths, |
| 461 | errorSquared, false); |
| 462 | break; |
| 463 | case SkPath::kCubic_Verb: |
| 464 | addBezier(points, cubicBezierCalculation, segmentPoints, lengths, |
| 465 | errorSquared, true); |
| 466 | break; |
| 467 | default: |
| 468 | // Leave element as NULL, Conic sections are not supported. |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | // Returns a float[] with each point along the path represented by 3 floats |
| 474 | // * fractional length along the path that the point resides |
| 475 | // * x coordinate |
| 476 | // * y coordinate |
| 477 | // Note that more than one point may have the same length along the path in |
| 478 | // the case of a move. |
| 479 | // NULL can be returned if the Path is empty. |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 480 | static jfloatArray approximate(JNIEnv* env, jclass, jlong pathHandle, float acceptableError) |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 481 | { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 482 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 483 | SkASSERT(path); |
| 484 | SkPath::Iter pathIter(*path, false); |
| 485 | SkPath::Verb verb; |
| 486 | SkPoint points[4]; |
| 487 | std::vector<SkPoint> segmentPoints; |
| 488 | std::vector<float> lengths; |
| 489 | float errorSquared = acceptableError * acceptableError; |
| 490 | |
| 491 | while ((verb = pathIter.next(points)) != SkPath::kDone_Verb) { |
| 492 | createVerbSegments(verb, points, segmentPoints, lengths, errorSquared); |
| 493 | } |
| 494 | |
| 495 | if (segmentPoints.empty()) { |
| 496 | return NULL; |
| 497 | } |
| 498 | |
| 499 | size_t numPoints = segmentPoints.size(); |
| 500 | size_t approximationArraySize = numPoints * 3; |
| 501 | |
| 502 | float* approximation = new float[approximationArraySize]; |
| 503 | float totalLength = lengths.back(); |
| 504 | |
| 505 | int approximationIndex = 0; |
George Mount | 7fe03a2 | 2013-11-22 17:08:28 -0800 | [diff] [blame] | 506 | for (size_t i = 0; i < numPoints; i++) { |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 507 | const SkPoint& point = segmentPoints[i]; |
| 508 | approximation[approximationIndex++] = lengths[i] / totalLength; |
| 509 | approximation[approximationIndex++] = point.x(); |
| 510 | approximation[approximationIndex++] = point.y(); |
| 511 | } |
| 512 | |
| 513 | jfloatArray result = env->NewFloatArray(approximationArraySize); |
| 514 | env->SetFloatArrayRegion(result, 0, approximationArraySize, approximation); |
| 515 | delete[] approximation; |
| 516 | return result; |
| 517 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | }; |
| 519 | |
| 520 | static JNINativeMethod methods[] = { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 521 | {"finalizer", "(J)V", (void*) SkPathGlue::finalizer}, |
| 522 | {"init1","()J", (void*) SkPathGlue::init1}, |
| 523 | {"init2","(J)J", (void*) SkPathGlue::init2}, |
| 524 | {"native_reset","(J)V", (void*) SkPathGlue::reset}, |
| 525 | {"native_rewind","(J)V", (void*) SkPathGlue::rewind}, |
| 526 | {"native_set","(JJ)V", (void*) SkPathGlue::assign}, |
| 527 | {"native_getFillType","(J)I", (void*) SkPathGlue::getFillType}, |
| 528 | {"native_setFillType","(JI)V", (void*) SkPathGlue::setFillType}, |
| 529 | {"native_isEmpty","(J)Z", (void*) SkPathGlue::isEmpty}, |
| 530 | {"native_isRect","(JLandroid/graphics/RectF;)Z", (void*) SkPathGlue::isRect}, |
| 531 | {"native_computeBounds","(JLandroid/graphics/RectF;)V", (void*) SkPathGlue::computeBounds}, |
| 532 | {"native_incReserve","(JI)V", (void*) SkPathGlue::incReserve}, |
| 533 | {"native_moveTo","(JFF)V", (void*) SkPathGlue::moveTo__FF}, |
| 534 | {"native_rMoveTo","(JFF)V", (void*) SkPathGlue::rMoveTo}, |
| 535 | {"native_lineTo","(JFF)V", (void*) SkPathGlue::lineTo__FF}, |
| 536 | {"native_rLineTo","(JFF)V", (void*) SkPathGlue::rLineTo}, |
| 537 | {"native_quadTo","(JFFFF)V", (void*) SkPathGlue::quadTo__FFFF}, |
| 538 | {"native_rQuadTo","(JFFFF)V", (void*) SkPathGlue::rQuadTo}, |
| 539 | {"native_cubicTo","(JFFFFFF)V", (void*) SkPathGlue::cubicTo__FFFFFF}, |
| 540 | {"native_rCubicTo","(JFFFFFF)V", (void*) SkPathGlue::rCubicTo}, |
| 541 | {"native_arcTo","(JLandroid/graphics/RectF;FFZ)V", (void*) SkPathGlue::arcTo}, |
| 542 | {"native_close","(J)V", (void*) SkPathGlue::close}, |
| 543 | {"native_addRect","(JLandroid/graphics/RectF;I)V", (void*) SkPathGlue::addRect__RectFI}, |
| 544 | {"native_addRect","(JFFFFI)V", (void*) SkPathGlue::addRect__FFFFI}, |
| 545 | {"native_addOval","(JLandroid/graphics/RectF;I)V", (void*) SkPathGlue::addOval}, |
| 546 | {"native_addCircle","(JFFFI)V", (void*) SkPathGlue::addCircle}, |
| 547 | {"native_addArc","(JLandroid/graphics/RectF;FF)V", (void*) SkPathGlue::addArc}, |
| 548 | {"native_addRoundRect","(JLandroid/graphics/RectF;FFI)V", (void*) SkPathGlue::addRoundRectXY}, |
| 549 | {"native_addRoundRect","(JLandroid/graphics/RectF;[FI)V", (void*) SkPathGlue::addRoundRect8}, |
| 550 | {"native_addPath","(JJFF)V", (void*) SkPathGlue::addPath__PathFF}, |
| 551 | {"native_addPath","(JJ)V", (void*) SkPathGlue::addPath__Path}, |
| 552 | {"native_addPath","(JJJ)V", (void*) SkPathGlue::addPath__PathMatrix}, |
| 553 | {"native_offset","(JFFJ)V", (void*) SkPathGlue::offset__FFPath}, |
| 554 | {"native_offset","(JFF)V", (void*) SkPathGlue::offset__FF}, |
| 555 | {"native_setLastPoint","(JFF)V", (void*) SkPathGlue::setLastPoint}, |
| 556 | {"native_transform","(JJJ)V", (void*) SkPathGlue::transform__MatrixPath}, |
| 557 | {"native_transform","(JJ)V", (void*) SkPathGlue::transform__Matrix}, |
| 558 | {"native_op","(JJIJ)Z", (void*) SkPathGlue::op}, |
| 559 | {"native_approximate", "(JF)[F", (void*) SkPathGlue::approximate}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | }; |
| 561 | |
| 562 | int register_android_graphics_Path(JNIEnv* env) { |
| 563 | int result = AndroidRuntime::registerNativeMethods(env, "android/graphics/Path", methods, |
| 564 | sizeof(methods) / sizeof(methods[0])); |
| 565 | return result; |
| 566 | } |
| 567 | |
| 568 | } |