• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "9patch"
19 #define LOG_NDEBUG 1
20 
21 #include <androidfw/ResourceTypes.h>
22 #include <hwui/Canvas.h>
23 #include <hwui/Paint.h>
24 #include <utils/Log.h>
25 
26 #include <ResourceCache.h>
27 
28 #include "SkCanvas.h"
29 #include "SkLatticeIter.h"
30 #include "SkRegion.h"
31 #include "GraphicsJNI.h"
32 #include "NinePatchUtils.h"
33 
34 #include <nativehelper/JNIHelp.h>
35 #include "core_jni_helpers.h"
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         if (android::uirenderer::ResourceCache::hasInstance()) {
83             Res_png_9patch* p = (Res_png_9patch*) patch;
84             android::uirenderer::ResourceCache::getInstance().destructor(p);
85         } else {
86             delete[] patch;
87         }
88     }
89 
getTransparentRegion(JNIEnv * env,jobject,jobject jbitmap,jlong chunkHandle,jobject dstRect)90     static jlong getTransparentRegion(JNIEnv* env, jobject, jobject jbitmap,
91             jlong chunkHandle, jobject dstRect) {
92         Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
93         SkASSERT(chunk);
94 
95         SkBitmap bitmap;
96         GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
97         SkRect dst;
98         GraphicsJNI::jrect_to_rect(env, dstRect, &dst);
99 
100         SkCanvas::Lattice lattice;
101         SkIRect src = SkIRect::MakeWH(bitmap.width(), bitmap.height());
102         lattice.fBounds = &src;
103         NinePatchUtils::SetLatticeDivs(&lattice, *chunk, bitmap.width(), bitmap.height());
104         lattice.fFlags = nullptr;
105 
106         SkRegion* region = nullptr;
107         if (SkLatticeIter::Valid(bitmap.width(), bitmap.height(), lattice)) {
108             SkLatticeIter iter(lattice, dst);
109             if (iter.numRectsToDraw() == chunk->numColors) {
110                 SkRect dummy;
111                 SkRect iterDst;
112                 int index = 0;
113                 while (iter.next(&dummy, &iterDst)) {
114                     if (0 == chunk->getColors()[index++] && !iterDst.isEmpty()) {
115                         if (!region) {
116                             region = new SkRegion();
117                         }
118 
119                         region->op(iterDst.round(), SkRegion::kUnion_Op);
120                     }
121                 }
122             }
123         }
124 
125         return reinterpret_cast<jlong>(region);
126     }
127 
128 };
129 
130 /////////////////////////////////////////////////////////////////////////////////////////
131 
132 static const JNINativeMethod gNinePatchMethods[] = {
133     { "isNinePatchChunk", "([B)Z", (void*) SkNinePatchGlue::isNinePatchChunk },
134     { "validateNinePatchChunk", "([B)J",
135             (void*) SkNinePatchGlue::validateNinePatchChunk },
136     { "nativeFinalize", "(J)V", (void*) SkNinePatchGlue::finalize },
137     { "nativeGetTransparentRegion", "(Landroid/graphics/Bitmap;JLandroid/graphics/Rect;)J",
138             (void*) SkNinePatchGlue::getTransparentRegion }
139 };
140 
register_android_graphics_NinePatch(JNIEnv * env)141 int register_android_graphics_NinePatch(JNIEnv* env) {
142     return android::RegisterMethodsOrDie(env,
143             "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods));
144 }
145