Home | History | Annotate | Download | only in graphics
      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 "SkRegion.h"
     30 #include "GraphicsJNI.h"
     31 
     32 #include "utils/NinePatch.h"
     33 
     34 #include "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:
     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 
     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 
     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 
     90     static jlong getTransparentRegion(JNIEnv* env, jobject, jobject jbitmap,
     91             jlong chunkHandle, jobject boundsRect) {
     92         Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
     93         SkASSERT(chunk);
     94         SkASSERT(boundsRect);
     95 
     96         SkBitmap bitmap;
     97         GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
     98         SkRect bounds;
     99         GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds);
    100 
    101         SkRegion* region = NULL;
    102         NinePatch::Draw(NULL, bounds, bitmap, *chunk, NULL, &region);
    103 
    104         return reinterpret_cast<jlong>(region);
    105     }
    106 
    107 };
    108 
    109 /////////////////////////////////////////////////////////////////////////////////////////
    110 
    111 static const JNINativeMethod gNinePatchMethods[] = {
    112     { "isNinePatchChunk", "([B)Z", (void*) SkNinePatchGlue::isNinePatchChunk },
    113     { "validateNinePatchChunk", "([B)J",
    114             (void*) SkNinePatchGlue::validateNinePatchChunk },
    115     { "nativeFinalize", "(J)V", (void*) SkNinePatchGlue::finalize },
    116     { "nativeGetTransparentRegion", "(Landroid/graphics/Bitmap;JLandroid/graphics/Rect;)J",
    117             (void*) SkNinePatchGlue::getTransparentRegion }
    118 };
    119 
    120 int register_android_graphics_NinePatch(JNIEnv* env) {
    121     return android::RegisterMethodsOrDie(env,
    122             "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods));
    123 }
    124