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 <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 
     35 extern void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, const SkBitmap& bitmap,
     36         const android::Res_png_9patch& chunk, const SkPaint* paint, SkRegion** outRegion);
     37 
     38 using 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 
     48 class SkNinePatchGlue {
     49 public:
     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, 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 = (bounds.fRight-bounds.fLeft) / scale;
    106             bounds.fBottom = (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             jobject jbitmap, jlong chunkHandle, jlong paintHandle,
    122             jint destDensity, jint srcDensity) {
    123         SkCanvas* canvas       = reinterpret_cast<Canvas*>(canvasHandle)->asSkCanvas();
    124         Res_png_9patch* chunk  = reinterpret_cast<Res_png_9patch*>(chunkHandle);
    125         const Paint* paint     = reinterpret_cast<Paint*>(paintHandle);
    126         SkASSERT(canvas);
    127         SkASSERT(boundsRectF);
    128         SkASSERT(chunk);
    129         // paint is optional
    130 
    131         SkBitmap bitmap;
    132         GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
    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             jobject jbitmap, jlong chunkHandle, jlong paintHandle,
    141             jint destDensity, jint srcDensity) {
    142         SkCanvas* canvas       = reinterpret_cast<Canvas*>(canvasHandle)->asSkCanvas();
    143         Res_png_9patch* chunk  = reinterpret_cast<Res_png_9patch*>(chunkHandle);
    144         const Paint* paint     = reinterpret_cast<Paint*>(paintHandle);
    145         SkASSERT(canvas);
    146         SkASSERT(boundsRect);
    147         SkASSERT(chunk);
    148         // paint is optional
    149 
    150         SkBitmap bitmap;
    151         GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
    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, jobject jbitmap,
    158             jlong chunkHandle, jobject boundsRect) {
    159         Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
    160         SkASSERT(chunk);
    161         SkASSERT(boundsRect);
    162 
    163         SkBitmap bitmap;
    164         GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
    165         SkRect bounds;
    166         GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds);
    167 
    168         SkRegion* region = NULL;
    169         NinePatch_Draw(NULL, bounds, bitmap, *chunk, NULL, &region);
    170 
    171         return reinterpret_cast<jlong>(region);
    172     }
    173 
    174 };
    175 
    176 /////////////////////////////////////////////////////////////////////////////////////////
    177 
    178 static JNINativeMethod gNinePatchMethods[] = {
    179     { "isNinePatchChunk", "([B)Z", (void*) SkNinePatchGlue::isNinePatchChunk },
    180     { "validateNinePatchChunk", "([B)J",
    181             (void*) SkNinePatchGlue::validateNinePatchChunk },
    182     { "nativeFinalize", "(J)V", (void*) SkNinePatchGlue::finalize },
    183     { "nativeDraw", "(JLandroid/graphics/RectF;Landroid/graphics/Bitmap;JJII)V",
    184             (void*) SkNinePatchGlue::drawF },
    185     { "nativeDraw", "(JLandroid/graphics/Rect;Landroid/graphics/Bitmap;JJII)V",
    186             (void*) SkNinePatchGlue::drawI },
    187     { "nativeGetTransparentRegion", "(Landroid/graphics/Bitmap;JLandroid/graphics/Rect;)J",
    188             (void*) SkNinePatchGlue::getTransparentRegion }
    189 };
    190 
    191 int register_android_graphics_NinePatch(JNIEnv* env) {
    192     return android::RegisterMethodsOrDie(env,
    193             "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods));
    194 }
    195