NinePatch.cpp revision f5d6c555c3430f6e423952ba3ab024380e550bba
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 <Caches.h>
25
26#include "Canvas.h"
27#include "SkCanvas.h"
28#include "SkRegion.h"
29#include "GraphicsJNI.h"
30
31#include "JNIHelp.h"
32
33extern void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, const SkBitmap& bitmap,
34        const android::Res_png_9patch& chunk, const SkPaint* paint, SkRegion** outRegion);
35
36using namespace android;
37
38/**
39 * IMPORTANT NOTE: 9patch chunks can be manipuated either as an array of bytes
40 * or as a Res_png_9patch instance. It is important to note that the size of the
41 * array required to hold a 9patch chunk is greater than sizeof(Res_png_9patch).
42 * The code below manipulates chunks as Res_png_9patch* types to draw and as
43 * int8_t* to allocate and free the backing storage.
44 */
45
46class SkNinePatchGlue {
47public:
48    static jboolean isNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) {
49        if (NULL == obj) {
50            return JNI_FALSE;
51        }
52        if (env->GetArrayLength(obj) < (int)sizeof(Res_png_9patch)) {
53            return JNI_FALSE;
54        }
55        const jbyte* array = env->GetByteArrayElements(obj, 0);
56        if (array != NULL) {
57            const Res_png_9patch* chunk = reinterpret_cast<const Res_png_9patch*>(array);
58            int8_t wasDeserialized = chunk->wasDeserialized;
59            env->ReleaseByteArrayElements(obj, const_cast<jbyte*>(array), JNI_ABORT);
60            return (wasDeserialized != -1) ? JNI_TRUE : JNI_FALSE;
61        }
62        return JNI_FALSE;
63    }
64
65    static jlong validateNinePatchChunk(JNIEnv* env, jobject, jlong, jbyteArray obj) {
66        size_t chunkSize = env->GetArrayLength(obj);
67        if (chunkSize < (int) (sizeof(Res_png_9patch))) {
68            jniThrowRuntimeException(env, "Array too small for chunk.");
69            return NULL;
70        }
71
72        int8_t* storage = new int8_t[chunkSize];
73        // This call copies the content of the jbyteArray
74        env->GetByteArrayRegion(obj, 0, chunkSize, reinterpret_cast<jbyte*>(storage));
75        // Deserialize in place, return the array we just allocated
76        return reinterpret_cast<jlong>(Res_png_9patch::deserialize(storage));
77    }
78
79    static void finalize(JNIEnv* env, jobject, jlong patchHandle) {
80        int8_t* patch = reinterpret_cast<int8_t*>(patchHandle);
81#ifdef USE_OPENGL_RENDERER
82        if (android::uirenderer::Caches::hasInstance()) {
83            Res_png_9patch* p = (Res_png_9patch*) patch;
84            android::uirenderer::Caches::getInstance().resourceCache.destructor(p);
85            return;
86        }
87#endif // USE_OPENGL_RENDERER
88        delete[] patch;
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 SkPaint* paint   = reinterpret_cast<SkPaint*>(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 SkPaint* paint   = reinterpret_cast<SkPaint*>(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, &region);
170
171        return reinterpret_cast<jlong>(region);
172    }
173
174};
175
176/////////////////////////////////////////////////////////////////////////////////////////
177
178#include <android_runtime/AndroidRuntime.h>
179
180static JNINativeMethod gNinePatchMethods[] = {
181    { "isNinePatchChunk", "([B)Z",                        (void*) SkNinePatchGlue::isNinePatchChunk },
182    { "validateNinePatchChunk", "(J[B)J",                 (void*) SkNinePatchGlue::validateNinePatchChunk },
183    { "nativeFinalize", "(J)V",                           (void*) SkNinePatchGlue::finalize },
184    { "nativeDraw", "(JLandroid/graphics/RectF;JJJII)V",  (void*) SkNinePatchGlue::drawF },
185    { "nativeDraw", "(JLandroid/graphics/Rect;JJJII)V",   (void*) SkNinePatchGlue::drawI },
186    { "nativeGetTransparentRegion", "(JJLandroid/graphics/Rect;)J",
187                                                          (void*) SkNinePatchGlue::getTransparentRegion }
188};
189
190int register_android_graphics_NinePatch(JNIEnv* env) {
191    return android::AndroidRuntime::registerNativeMethods(env,
192            "android/graphics/NinePatch", gNinePatchMethods, SK_ARRAY_COUNT(gNinePatchMethods));
193}
194