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