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