1 /*
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 #undef LOG_TAG
19 #define LOG_TAG "9patch"
20 #define LOG_NDEBUG 1
21 
22 #include <androidfw/ResourceTypes.h>
23 #include <hwui/Canvas.h>
24 #include <hwui/Paint.h>
25 #include <utils/Log.h>
26 
27 #include "SkCanvas.h"
28 #include "SkLatticeIter.h"
29 #include "SkRegion.h"
30 #include "GraphicsJNI.h"
31 #include "NinePatchPeeker.h"
32 #include "NinePatchUtils.h"
33 
34 jclass      gInsetStruct_class;
35 jmethodID   gInsetStruct_constructorMethodID;
36 
37 using namespace android;
38 
39 /**
40  * IMPORTANT NOTE: 9patch chunks can be manipuated either as an array of bytes
41  * or as a Res_png_9patch instance. It is important to note that the size of the
42  * array required to hold a 9patch chunk is greater than sizeof(Res_png_9patch).
43  * The code below manipulates chunks as Res_png_9patch* types to draw and as
44  * int8_t* to allocate and free the backing storage.
45  */
46 
47 class SkNinePatchGlue {
48 public:
isNinePatchChunk(JNIEnv * env,jobject,jbyteArray obj)49     static jboolean isNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) {
50         if (NULL == obj) {
51             return JNI_FALSE;
52         }
53         if (env->GetArrayLength(obj) < (int)sizeof(Res_png_9patch)) {
54             return JNI_FALSE;
55         }
56         const jbyte* array = env->GetByteArrayElements(obj, 0);
57         if (array != NULL) {
58             const Res_png_9patch* chunk = reinterpret_cast<const Res_png_9patch*>(array);
59             int8_t wasDeserialized = chunk->wasDeserialized;
60             env->ReleaseByteArrayElements(obj, const_cast<jbyte*>(array), JNI_ABORT);
61             return (wasDeserialized != -1) ? JNI_TRUE : JNI_FALSE;
62         }
63         return JNI_FALSE;
64     }
65 
validateNinePatchChunk(JNIEnv * env,jobject,jbyteArray obj)66     static jlong validateNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) {
67         size_t chunkSize = env->GetArrayLength(obj);
68         if (chunkSize < (int) (sizeof(Res_png_9patch))) {
69             jniThrowRuntimeException(env, "Array too small for chunk.");
70             return NULL;
71         }
72 
73         int8_t* storage = new int8_t[chunkSize];
74         // This call copies the content of the jbyteArray
75         env->GetByteArrayRegion(obj, 0, chunkSize, reinterpret_cast<jbyte*>(storage));
76         // Deserialize in place, return the array we just allocated
77         return reinterpret_cast<jlong>(Res_png_9patch::deserialize(storage));
78     }
79 
finalize(JNIEnv * env,jobject,jlong patchHandle)80     static void finalize(JNIEnv* env, jobject, jlong patchHandle) {
81         int8_t* patch = reinterpret_cast<int8_t*>(patchHandle);
82         delete[] patch;
83     }
84 
getTransparentRegion(JNIEnv * env,jobject,jlong bitmapPtr,jlong chunkHandle,jobject dstRect)85     static jlong getTransparentRegion(JNIEnv* env, jobject, jlong bitmapPtr,
86             jlong chunkHandle, jobject dstRect) {
87         Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
88         SkASSERT(chunk);
89 
90         SkBitmap bitmap;
91         bitmap::toBitmap(bitmapPtr).getSkBitmap(&bitmap);
92         SkRect dst;
93         GraphicsJNI::jrect_to_rect(env, dstRect, &dst);
94 
95         SkCanvas::Lattice lattice;
96         SkIRect src = SkIRect::MakeWH(bitmap.width(), bitmap.height());
97         lattice.fBounds = &src;
98         NinePatchUtils::SetLatticeDivs(&lattice, *chunk, bitmap.width(), bitmap.height());
99         lattice.fRectTypes = nullptr;
100         lattice.fColors = nullptr;
101 
102         SkRegion* region = nullptr;
103         if (SkLatticeIter::Valid(bitmap.width(), bitmap.height(), lattice)) {
104             SkLatticeIter iter(lattice, dst);
105             if (iter.numRectsToDraw() == chunk->numColors) {
106                 SkRect dummy;
107                 SkRect iterDst;
108                 int index = 0;
109                 while (iter.next(&dummy, &iterDst)) {
110                     if (0 == chunk->getColors()[index++] && !iterDst.isEmpty()) {
111                         if (!region) {
112                             region = new SkRegion();
113                         }
114 
115                         region->op(iterDst.round(), SkRegion::kUnion_Op);
116                     }
117                 }
118             }
119         }
120 
121         return reinterpret_cast<jlong>(region);
122     }
123 
124 };
125 
createNinePatchInsets(JNIEnv * env,float scale) const126 jobject NinePatchPeeker::createNinePatchInsets(JNIEnv* env, float scale) const {
127     if (!mHasInsets) {
128         return nullptr;
129     }
130 
131     return env->NewObject(gInsetStruct_class, gInsetStruct_constructorMethodID,
132             mOpticalInsets[0], mOpticalInsets[1],
133             mOpticalInsets[2], mOpticalInsets[3],
134             mOutlineInsets[0], mOutlineInsets[1],
135             mOutlineInsets[2], mOutlineInsets[3],
136             mOutlineRadius, mOutlineAlpha, scale);
137 }
138 
getPadding(JNIEnv * env,jobject outPadding) const139 void NinePatchPeeker::getPadding(JNIEnv* env, jobject outPadding) const {
140     if (mPatch) {
141         GraphicsJNI::set_jrect(env, outPadding,
142                 mPatch->paddingLeft, mPatch->paddingTop,
143                 mPatch->paddingRight, mPatch->paddingBottom);
144 
145     } else {
146         GraphicsJNI::set_jrect(env, outPadding, -1, -1, -1, -1);
147     }
148 }
149 
150 /////////////////////////////////////////////////////////////////////////////////////////
151 
152 static const JNINativeMethod gNinePatchMethods[] = {
153     { "isNinePatchChunk", "([B)Z", (void*) SkNinePatchGlue::isNinePatchChunk },
154     { "validateNinePatchChunk", "([B)J",
155             (void*) SkNinePatchGlue::validateNinePatchChunk },
156     { "nativeFinalize", "(J)V", (void*) SkNinePatchGlue::finalize },
157     { "nativeGetTransparentRegion", "(JJLandroid/graphics/Rect;)J",
158             (void*) SkNinePatchGlue::getTransparentRegion }
159 };
160 
register_android_graphics_NinePatch(JNIEnv * env)161 int register_android_graphics_NinePatch(JNIEnv* env) {
162     gInsetStruct_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
163             "android/graphics/NinePatch$InsetStruct"));
164     gInsetStruct_constructorMethodID = GetMethodIDOrDie(env, gInsetStruct_class, "<init>",
165             "(IIIIIIIIFIF)V");
166     return android::RegisterMethodsOrDie(env,
167             "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods));
168 }
169