NinePatch.cpp revision a2732a2bf98f7dbd063f4e5679f5b8bfcbec2698
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 <utils/Log.h> 23 24#include <ResourceCache.h> 25 26#include "Paint.h" 27#include "Canvas.h" 28#include "SkCanvas.h" 29#include "SkRegion.h" 30#include "GraphicsJNI.h" 31 32#include "JNIHelp.h" 33#include "core_jni_helpers.h" 34 35extern void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, const SkBitmap& bitmap, 36 const android::Res_png_9patch& chunk, const SkPaint* paint, SkRegion** outRegion); 37 38using namespace android; 39 40/** 41 * IMPORTANT NOTE: 9patch chunks can be manipuated either as an array of bytes 42 * or as a Res_png_9patch instance. It is important to note that the size of the 43 * array required to hold a 9patch chunk is greater than sizeof(Res_png_9patch). 44 * The code below manipulates chunks as Res_png_9patch* types to draw and as 45 * int8_t* to allocate and free the backing storage. 46 */ 47 48class SkNinePatchGlue { 49public: 50 static jboolean isNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) { 51 if (NULL == obj) { 52 return JNI_FALSE; 53 } 54 if (env->GetArrayLength(obj) < (int)sizeof(Res_png_9patch)) { 55 return JNI_FALSE; 56 } 57 const jbyte* array = env->GetByteArrayElements(obj, 0); 58 if (array != NULL) { 59 const Res_png_9patch* chunk = reinterpret_cast<const Res_png_9patch*>(array); 60 int8_t wasDeserialized = chunk->wasDeserialized; 61 env->ReleaseByteArrayElements(obj, const_cast<jbyte*>(array), JNI_ABORT); 62 return (wasDeserialized != -1) ? JNI_TRUE : JNI_FALSE; 63 } 64 return JNI_FALSE; 65 } 66 67 static jlong validateNinePatchChunk(JNIEnv* env, jobject, jlong, jbyteArray obj) { 68 size_t chunkSize = env->GetArrayLength(obj); 69 if (chunkSize < (int) (sizeof(Res_png_9patch))) { 70 jniThrowRuntimeException(env, "Array too small for chunk."); 71 return NULL; 72 } 73 74 int8_t* storage = new int8_t[chunkSize]; 75 // This call copies the content of the jbyteArray 76 env->GetByteArrayRegion(obj, 0, chunkSize, reinterpret_cast<jbyte*>(storage)); 77 // Deserialize in place, return the array we just allocated 78 return reinterpret_cast<jlong>(Res_png_9patch::deserialize(storage)); 79 } 80 81 static void finalize(JNIEnv* env, jobject, jlong patchHandle) { 82 int8_t* patch = reinterpret_cast<int8_t*>(patchHandle); 83 if (android::uirenderer::ResourceCache::hasInstance()) { 84 Res_png_9patch* p = (Res_png_9patch*) patch; 85 android::uirenderer::ResourceCache::getInstance().destructor(p); 86 } else { 87 delete[] patch; 88 } 89 } 90 91 static void draw(JNIEnv* env, SkCanvas* canvas, SkRect& bounds, const SkBitmap* bitmap, 92 Res_png_9patch* chunk, const SkPaint* paint, jint destDensity, jint srcDensity) { 93 if (destDensity == srcDensity || destDensity == 0 || srcDensity == 0) { 94 ALOGV("Drawing unscaled 9-patch: (%g,%g)-(%g,%g)", 95 SkScalarToFloat(bounds.fLeft), SkScalarToFloat(bounds.fTop), 96 SkScalarToFloat(bounds.fRight), SkScalarToFloat(bounds.fBottom)); 97 NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL); 98 } else { 99 canvas->save(); 100 101 SkScalar scale = destDensity / (float)srcDensity; 102 canvas->translate(bounds.fLeft, bounds.fTop); 103 canvas->scale(scale, scale); 104 105 bounds.fRight = SkScalarDiv(bounds.fRight-bounds.fLeft, scale); 106 bounds.fBottom = SkScalarDiv(bounds.fBottom-bounds.fTop, scale); 107 bounds.fLeft = bounds.fTop = 0; 108 109 ALOGV("Drawing scaled 9-patch: (%g,%g)-(%g,%g) srcDensity=%d destDensity=%d", 110 SkScalarToFloat(bounds.fLeft), SkScalarToFloat(bounds.fTop), 111 SkScalarToFloat(bounds.fRight), SkScalarToFloat(bounds.fBottom), 112 srcDensity, destDensity); 113 114 NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL); 115 116 canvas->restore(); 117 } 118 } 119 120 static void drawF(JNIEnv* env, jobject, jlong canvasHandle, jobject boundsRectF, 121 jlong bitmapHandle, jlong chunkHandle, jlong paintHandle, 122 jint destDensity, jint srcDensity) { 123 SkCanvas* canvas = reinterpret_cast<Canvas*>(canvasHandle)->getSkCanvas(); 124 const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle); 125 Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle); 126 const Paint* paint = reinterpret_cast<Paint*>(paintHandle); 127 SkASSERT(canvas); 128 SkASSERT(boundsRectF); 129 SkASSERT(bitmap); 130 SkASSERT(chunk); 131 // paint is optional 132 133 SkRect bounds; 134 GraphicsJNI::jrectf_to_rect(env, boundsRectF, &bounds); 135 136 draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity); 137 } 138 139 static void drawI(JNIEnv* env, jobject, jlong canvasHandle, jobject boundsRect, 140 jlong bitmapHandle, jlong chunkHandle, jlong paintHandle, 141 jint destDensity, jint srcDensity) { 142 SkCanvas* canvas = reinterpret_cast<Canvas*>(canvasHandle)->getSkCanvas(); 143 const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle); 144 Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle); 145 const Paint* paint = reinterpret_cast<Paint*>(paintHandle); 146 SkASSERT(canvas); 147 SkASSERT(boundsRect); 148 SkASSERT(bitmap); 149 SkASSERT(chunk); 150 // paint is optional 151 152 SkRect bounds; 153 GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds); 154 draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity); 155 } 156 157 static jlong getTransparentRegion(JNIEnv* env, jobject, jlong bitmapHandle, 158 jlong chunkHandle, jobject boundsRect) { 159 const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle); 160 Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle); 161 SkASSERT(bitmap); 162 SkASSERT(chunk); 163 SkASSERT(boundsRect); 164 165 SkRect bounds; 166 GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds); 167 168 SkRegion* region = NULL; 169 NinePatch_Draw(NULL, bounds, *bitmap, *chunk, NULL, ®ion); 170 171 return reinterpret_cast<jlong>(region); 172 } 173 174}; 175 176///////////////////////////////////////////////////////////////////////////////////////// 177 178static JNINativeMethod gNinePatchMethods[] = { 179 { "isNinePatchChunk", "([B)Z", (void*) SkNinePatchGlue::isNinePatchChunk }, 180 { "validateNinePatchChunk", "(J[B)J", (void*) SkNinePatchGlue::validateNinePatchChunk }, 181 { "nativeFinalize", "(J)V", (void*) SkNinePatchGlue::finalize }, 182 { "nativeDraw", "(JLandroid/graphics/RectF;JJJII)V", (void*) SkNinePatchGlue::drawF }, 183 { "nativeDraw", "(JLandroid/graphics/Rect;JJJII)V", (void*) SkNinePatchGlue::drawI }, 184 { "nativeGetTransparentRegion", "(JJLandroid/graphics/Rect;)J", 185 (void*) SkNinePatchGlue::getTransparentRegion } 186}; 187 188int register_android_graphics_NinePatch(JNIEnv* env) { 189 return android::RegisterMethodsOrDie(env, 190 "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods)); 191} 192