Jeff Brown | fbf0977 | 2011-01-16 14:06:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include "SkRegion.h" |
| 18 | #include "SkPath.h" |
| 19 | #include "GraphicsJNI.h" |
| 20 | |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 21 | #ifdef __ANDROID__ // Layoutlib does not support parcel |
Derek Sollenberger | 15da7e2 | 2020-02-14 14:16:34 -0500 | [diff] [blame^] | 22 | #include <android/binder_parcel.h> |
| 23 | #include <android/binder_parcel_jni.h> |
| 24 | #include <android/binder_parcel_utils.h> |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 25 | #endif |
Jeff Brown | fbf0977 | 2011-01-16 14:06:57 -0800 | [diff] [blame] | 26 | |
Jeff Brown | fbf0977 | 2011-01-16 14:06:57 -0800 | [diff] [blame] | 27 | namespace android { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | static jfieldID gRegion_nativeInstanceFieldID; |
| 30 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 31 | static inline jboolean boolTojboolean(bool value) { |
| 32 | return value ? JNI_TRUE : JNI_FALSE; |
| 33 | } |
| 34 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | static inline SkRegion* GetSkRegion(JNIEnv* env, jobject regionObject) { |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 36 | jlong regionHandle = env->GetLongField(regionObject, gRegion_nativeInstanceFieldID); |
| 37 | SkRegion* region = reinterpret_cast<SkRegion*>(regionHandle); |
| 38 | SkASSERT(region != NULL); |
| 39 | return region; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 42 | static jlong Region_constructor(JNIEnv* env, jobject) { |
| 43 | return reinterpret_cast<jlong>(new SkRegion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 46 | static void Region_destructor(JNIEnv* env, jobject, jlong regionHandle) { |
| 47 | SkRegion* region = reinterpret_cast<SkRegion*>(regionHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | SkASSERT(region); |
| 49 | delete region; |
| 50 | } |
| 51 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 52 | static void Region_setRegion(JNIEnv* env, jobject, jlong dstHandle, jlong srcHandle) { |
| 53 | SkRegion* dst = reinterpret_cast<SkRegion*>(dstHandle); |
| 54 | const SkRegion* src = reinterpret_cast<SkRegion*>(srcHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | SkASSERT(dst && src); |
| 56 | *dst = *src; |
| 57 | } |
| 58 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 59 | static jboolean Region_setRect(JNIEnv* env, jobject, jlong dstHandle, jint left, jint top, jint right, jint bottom) { |
| 60 | SkRegion* dst = reinterpret_cast<SkRegion*>(dstHandle); |
Mike Reed | 39adc88 | 2019-08-22 11:53:05 -0400 | [diff] [blame] | 61 | bool result = dst->setRect({left, top, right, bottom}); |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 62 | return boolTojboolean(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 65 | static jboolean Region_setPath(JNIEnv* env, jobject, jlong dstHandle, |
| 66 | jlong pathHandle, jlong clipHandle) { |
| 67 | SkRegion* dst = reinterpret_cast<SkRegion*>(dstHandle); |
| 68 | const SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
| 69 | const SkRegion* clip = reinterpret_cast<SkRegion*>(clipHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | SkASSERT(dst && path && clip); |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 71 | bool result = dst->setPath(*path, *clip); |
| 72 | return boolTojboolean(result); |
| 73 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 76 | static jboolean Region_getBounds(JNIEnv* env, jobject, jlong regionHandle, jobject rectBounds) { |
| 77 | SkRegion* region = reinterpret_cast<SkRegion*>(regionHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | GraphicsJNI::irect_to_jrect(region->getBounds(), env, rectBounds); |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 79 | bool result = !region->isEmpty(); |
| 80 | return boolTojboolean(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 83 | static jboolean Region_getBoundaryPath(JNIEnv* env, jobject, jlong regionHandle, jlong pathHandle) { |
| 84 | const SkRegion* region = reinterpret_cast<SkRegion*>(regionHandle); |
| 85 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
| 86 | bool result = region->getBoundaryPath(path); |
| 87 | return boolTojboolean(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 90 | static jboolean Region_op0(JNIEnv* env, jobject, jlong dstHandle, jint left, jint top, jint right, jint bottom, jint op) { |
| 91 | SkRegion* dst = reinterpret_cast<SkRegion*>(dstHandle); |
Mike Reed | 39adc88 | 2019-08-22 11:53:05 -0400 | [diff] [blame] | 92 | bool result = dst->op({left, top, right, bottom}, (SkRegion::Op)op); |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 93 | return boolTojboolean(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 96 | static jboolean Region_op1(JNIEnv* env, jobject, jlong dstHandle, jobject rectObject, jlong regionHandle, jint op) { |
| 97 | SkRegion* dst = reinterpret_cast<SkRegion*>(dstHandle); |
| 98 | const SkRegion* region = reinterpret_cast<SkRegion*>(regionHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | SkIRect ir; |
| 100 | GraphicsJNI::jrect_to_irect(env, rectObject, &ir); |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 101 | bool result = dst->op(ir, *region, (SkRegion::Op)op); |
| 102 | return boolTojboolean(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 105 | static jboolean Region_op2(JNIEnv* env, jobject, jlong dstHandle, jlong region1Handle, jlong region2Handle, jint op) { |
| 106 | SkRegion* dst = reinterpret_cast<SkRegion*>(dstHandle); |
| 107 | const SkRegion* region1 = reinterpret_cast<SkRegion*>(region1Handle); |
| 108 | const SkRegion* region2 = reinterpret_cast<SkRegion*>(region2Handle); |
| 109 | bool result = dst->op(*region1, *region2, (SkRegion::Op)op); |
| 110 | return boolTojboolean(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 113 | //////////////////////////////////// These are methods, not static |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | |
| 115 | static jboolean Region_isEmpty(JNIEnv* env, jobject region) { |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 116 | bool result = GetSkRegion(env, region)->isEmpty(); |
| 117 | return boolTojboolean(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | } |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 119 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | static jboolean Region_isRect(JNIEnv* env, jobject region) { |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 121 | bool result = GetSkRegion(env, region)->isRect(); |
| 122 | return boolTojboolean(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | } |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | static jboolean Region_isComplex(JNIEnv* env, jobject region) { |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 126 | bool result = GetSkRegion(env, region)->isComplex(); |
| 127 | return boolTojboolean(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 130 | static jboolean Region_contains(JNIEnv* env, jobject region, jint x, jint y) { |
| 131 | bool result = GetSkRegion(env, region)->contains(x, y); |
| 132 | return boolTojboolean(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 134 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 135 | static jboolean Region_quickContains(JNIEnv* env, jobject region, jint left, jint top, jint right, jint bottom) { |
Mike Reed | 39adc88 | 2019-08-22 11:53:05 -0400 | [diff] [blame] | 136 | bool result = GetSkRegion(env, region)->quickContains({left, top, right, bottom}); |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 137 | return boolTojboolean(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | } |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 139 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 140 | static jboolean Region_quickRejectIIII(JNIEnv* env, jobject region, jint left, jint top, jint right, jint bottom) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | SkIRect ir; |
Mike Reed | 39adc88 | 2019-08-22 11:53:05 -0400 | [diff] [blame] | 142 | ir.setLTRB(left, top, right, bottom); |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 143 | bool result = GetSkRegion(env, region)->quickReject(ir); |
| 144 | return boolTojboolean(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 146 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | static jboolean Region_quickRejectRgn(JNIEnv* env, jobject region, jobject other) { |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 148 | bool result = GetSkRegion(env, region)->quickReject(*GetSkRegion(env, other)); |
| 149 | return boolTojboolean(result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | } |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 151 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 152 | static void Region_translate(JNIEnv* env, jobject region, jint x, jint y, jobject dst) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | SkRegion* rgn = GetSkRegion(env, region); |
| 154 | if (dst) |
| 155 | rgn->translate(x, y, GetSkRegion(env, dst)); |
| 156 | else |
| 157 | rgn->translate(x, y); |
| 158 | } |
| 159 | |
Mitsuru Oshima | b10f138 | 2009-05-11 17:11:37 -0700 | [diff] [blame] | 160 | // Scale the rectangle by given scale and set the reuslt to the dst. |
| 161 | static void scale_rect(SkIRect* dst, const SkIRect& src, float scale) { |
| 162 | dst->fLeft = (int)::roundf(src.fLeft * scale); |
| 163 | dst->fTop = (int)::roundf(src.fTop * scale); |
| 164 | dst->fRight = (int)::roundf(src.fRight * scale); |
| 165 | dst->fBottom = (int)::roundf(src.fBottom * scale); |
| 166 | } |
| 167 | |
| 168 | // Scale the region by given scale and set the reuslt to the dst. |
| 169 | // dest and src can be the same region instance. |
| 170 | static void scale_rgn(SkRegion* dst, const SkRegion& src, float scale) { |
| 171 | SkRegion tmp; |
| 172 | SkRegion::Iterator iter(src); |
| 173 | |
| 174 | for (; !iter.done(); iter.next()) { |
| 175 | SkIRect r; |
| 176 | scale_rect(&r, iter.rect(), scale); |
| 177 | tmp.op(r, SkRegion::kUnion_Op); |
| 178 | } |
| 179 | dst->swap(tmp); |
| 180 | } |
| 181 | |
| 182 | static void Region_scale(JNIEnv* env, jobject region, jfloat scale, jobject dst) { |
| 183 | SkRegion* rgn = GetSkRegion(env, region); |
| 184 | if (dst) |
| 185 | scale_rgn(GetSkRegion(env, dst), *rgn, scale); |
| 186 | else |
| 187 | scale_rgn(rgn, *rgn, scale); |
| 188 | } |
| 189 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 190 | static jstring Region_toString(JNIEnv* env, jobject clazz, jlong regionHandle) { |
| 191 | SkRegion* region = reinterpret_cast<SkRegion*>(regionHandle); |
Joe Onorato | 120856c | 2011-01-19 14:52:08 -0800 | [diff] [blame] | 192 | char* str = region->toString(); |
| 193 | if (str == NULL) { |
| 194 | return NULL; |
| 195 | } |
| 196 | jstring result = env->NewStringUTF(str); |
| 197 | free(str); |
| 198 | return result; |
| 199 | } |
| 200 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | //////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 202 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 203 | static jlong Region_createFromParcel(JNIEnv* env, jobject clazz, jobject parcel) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | { |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 205 | #ifdef __ANDROID__ // Layoutlib does not support parcel |
Derek Sollenberger | 3082fe4 | 2015-05-13 15:45:04 -0400 | [diff] [blame] | 206 | if (parcel == nullptr) { |
| 207 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | } |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 209 | |
Derek Sollenberger | fd00820 | 2016-02-17 12:14:50 -0500 | [diff] [blame] | 210 | std::vector<int32_t> rects; |
Derek Sollenberger | 15da7e2 | 2020-02-14 14:16:34 -0500 | [diff] [blame^] | 211 | |
| 212 | AParcel* p = AParcel_fromJavaParcel(env, parcel); |
| 213 | ndk::AParcel_readVector(p, &rects); |
| 214 | AParcel_delete(p); |
Derek Sollenberger | fd00820 | 2016-02-17 12:14:50 -0500 | [diff] [blame] | 215 | |
| 216 | if ((rects.size() % 4) != 0) { |
Derek Sollenberger | cdadfc2 | 2015-05-18 14:29:02 -0400 | [diff] [blame] | 217 | return 0; |
| 218 | } |
Derek Sollenberger | 3082fe4 | 2015-05-13 15:45:04 -0400 | [diff] [blame] | 219 | |
Derek Sollenberger | cdadfc2 | 2015-05-18 14:29:02 -0400 | [diff] [blame] | 220 | SkRegion* region = new SkRegion; |
Derek Sollenberger | fd00820 | 2016-02-17 12:14:50 -0500 | [diff] [blame] | 221 | for (size_t x = 0; x + 4 <= rects.size(); x += 4) { |
Mike Reed | 39adc88 | 2019-08-22 11:53:05 -0400 | [diff] [blame] | 222 | region->op({rects[x], rects[x+1], rects[x+2], rects[x+3]}, SkRegion::kUnion_Op); |
Derek Sollenberger | 3082fe4 | 2015-05-13 15:45:04 -0400 | [diff] [blame] | 223 | } |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 224 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 225 | return reinterpret_cast<jlong>(region); |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 226 | #else |
| 227 | return 0; |
| 228 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 231 | static jboolean Region_writeToParcel(JNIEnv* env, jobject clazz, jlong regionHandle, jobject parcel) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | { |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 233 | #ifdef __ANDROID__ // Layoutlib does not support parcel |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 234 | const SkRegion* region = reinterpret_cast<SkRegion*>(regionHandle); |
Leon Scroggins III | 2a6d6e5 | 2015-06-17 11:56:43 -0400 | [diff] [blame] | 235 | if (parcel == nullptr) { |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 236 | return JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | } |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 238 | |
Derek Sollenberger | fd00820 | 2016-02-17 12:14:50 -0500 | [diff] [blame] | 239 | std::vector<int32_t> rects; |
| 240 | SkRegion::Iterator it(*region); |
| 241 | while (!it.done()) { |
| 242 | const SkIRect& r = it.rect(); |
| 243 | rects.push_back(r.fLeft); |
| 244 | rects.push_back(r.fTop); |
| 245 | rects.push_back(r.fRight); |
| 246 | rects.push_back(r.fBottom); |
| 247 | it.next(); |
Leon Scroggins III | 2a6d6e5 | 2015-06-17 11:56:43 -0400 | [diff] [blame] | 248 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | |
Derek Sollenberger | 15da7e2 | 2020-02-14 14:16:34 -0500 | [diff] [blame^] | 250 | AParcel* p = AParcel_fromJavaParcel(env, parcel); |
| 251 | ndk::AParcel_writeVector(p, rects); |
| 252 | AParcel_delete(p); |
| 253 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 254 | return JNI_TRUE; |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 255 | #else |
| 256 | return JNI_FALSE; |
| 257 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | //////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 261 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 262 | static jboolean Region_equals(JNIEnv* env, jobject clazz, jlong r1Handle, jlong r2Handle) |
Mitsuru Oshima | b10f138 | 2009-05-11 17:11:37 -0700 | [diff] [blame] | 263 | { |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 264 | const SkRegion *r1 = reinterpret_cast<SkRegion*>(r1Handle); |
| 265 | const SkRegion *r2 = reinterpret_cast<SkRegion*>(r2Handle); |
| 266 | return boolTojboolean(*r1 == *r2); |
Mitsuru Oshima | b10f138 | 2009-05-11 17:11:37 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | //////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 270 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | struct RgnIterPair { |
| 272 | SkRegion fRgn; // a copy of the caller's region |
| 273 | SkRegion::Iterator fIter; // an iterator acting upon the copy (fRgn) |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 274 | |
Chih-Hung Hsieh | c6baf56 | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 275 | explicit RgnIterPair(const SkRegion& rgn) : fRgn(rgn) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | // have our iterator reference our copy (fRgn), so we know it will be |
| 277 | // unchanged for the lifetime of the iterator |
| 278 | fIter.reset(fRgn); |
| 279 | } |
| 280 | }; |
| 281 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 282 | static jlong RegionIter_constructor(JNIEnv* env, jobject, jlong regionHandle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | { |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 284 | const SkRegion* region = reinterpret_cast<SkRegion*>(regionHandle); |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 285 | SkASSERT(region); |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 286 | return reinterpret_cast<jlong>(new RgnIterPair(*region)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 289 | static void RegionIter_destructor(JNIEnv* env, jobject, jlong pairHandle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | { |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 291 | RgnIterPair* pair = reinterpret_cast<RgnIterPair*>(pairHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | SkASSERT(pair); |
| 293 | delete pair; |
| 294 | } |
| 295 | |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 296 | static jboolean RegionIter_next(JNIEnv* env, jobject, jlong pairHandle, jobject rectObject) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | { |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 298 | RgnIterPair* pair = reinterpret_cast<RgnIterPair*>(pairHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | // the caller has checked that rectObject is not nul |
| 300 | SkASSERT(pair); |
| 301 | SkASSERT(rectObject); |
| 302 | |
| 303 | if (!pair->fIter.done()) { |
| 304 | GraphicsJNI::irect_to_jrect(pair->fIter.rect(), env, rectObject); |
| 305 | pair->fIter.next(); |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 306 | return JNI_TRUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | } |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 308 | return JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | //////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 312 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 313 | static const JNINativeMethod gRegionIterMethods[] = { |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 314 | { "nativeConstructor", "(J)J", (void*)RegionIter_constructor }, |
| 315 | { "nativeDestructor", "(J)V", (void*)RegionIter_destructor }, |
| 316 | { "nativeNext", "(JLandroid/graphics/Rect;)Z", (void*)RegionIter_next } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | }; |
| 318 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 319 | static const JNINativeMethod gRegionMethods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | // these are static methods |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 321 | { "nativeConstructor", "()J", (void*)Region_constructor }, |
| 322 | { "nativeDestructor", "(J)V", (void*)Region_destructor }, |
| 323 | { "nativeSetRegion", "(JJ)V", (void*)Region_setRegion }, |
| 324 | { "nativeSetRect", "(JIIII)Z", (void*)Region_setRect }, |
| 325 | { "nativeSetPath", "(JJJ)Z", (void*)Region_setPath }, |
| 326 | { "nativeGetBounds", "(JLandroid/graphics/Rect;)Z", (void*)Region_getBounds }, |
| 327 | { "nativeGetBoundaryPath", "(JJ)Z", (void*)Region_getBoundaryPath }, |
| 328 | { "nativeOp", "(JIIIII)Z", (void*)Region_op0 }, |
| 329 | { "nativeOp", "(JLandroid/graphics/Rect;JI)Z", (void*)Region_op1 }, |
| 330 | { "nativeOp", "(JJJI)Z", (void*)Region_op2 }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | // these are methods that take the java region object |
| 332 | { "isEmpty", "()Z", (void*)Region_isEmpty }, |
| 333 | { "isRect", "()Z", (void*)Region_isRect }, |
| 334 | { "isComplex", "()Z", (void*)Region_isComplex }, |
| 335 | { "contains", "(II)Z", (void*)Region_contains }, |
| 336 | { "quickContains", "(IIII)Z", (void*)Region_quickContains }, |
| 337 | { "quickReject", "(IIII)Z", (void*)Region_quickRejectIIII }, |
| 338 | { "quickReject", "(Landroid/graphics/Region;)Z", (void*)Region_quickRejectRgn }, |
Mitsuru Oshima | b10f138 | 2009-05-11 17:11:37 -0700 | [diff] [blame] | 339 | { "scale", "(FLandroid/graphics/Region;)V", (void*)Region_scale }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | { "translate", "(IILandroid/graphics/Region;)V", (void*)Region_translate }, |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 341 | { "nativeToString", "(J)Ljava/lang/String;", (void*)Region_toString }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | // parceling methods |
Ashok Bhat | a039843 | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 343 | { "nativeCreateFromParcel", "(Landroid/os/Parcel;)J", (void*)Region_createFromParcel }, |
| 344 | { "nativeWriteToParcel", "(JLandroid/os/Parcel;)Z", (void*)Region_writeToParcel }, |
| 345 | { "nativeEquals", "(JJ)Z", (void*)Region_equals }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | }; |
| 347 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | int register_android_graphics_Region(JNIEnv* env) |
| 349 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 350 | jclass clazz = FindClassOrDie(env, "android/graphics/Region"); |
Elliott Hughes | 4cb1753 | 2011-04-12 16:10:26 -0700 | [diff] [blame] | 351 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 352 | gRegion_nativeInstanceFieldID = GetFieldIDOrDie(env, clazz, "mNativeRegion", "J"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 354 | RegisterMethodsOrDie(env, "android/graphics/Region", gRegionMethods, NELEM(gRegionMethods)); |
| 355 | return RegisterMethodsOrDie(env, "android/graphics/RegionIterator", gRegionIterMethods, |
| 356 | NELEM(gRegionIterMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | } |
Jeff Brown | fbf0977 | 2011-01-16 14:06:57 -0800 | [diff] [blame] | 358 | |
Jeff Brown | fbf0977 | 2011-01-16 14:06:57 -0800 | [diff] [blame] | 359 | } // namespace android |