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