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