NinePatch.cpp revision 7d13d9db1ef90063cb542ccd6554042a6a3263b7
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#ifdef USE_OPENGL_RENDERER
84        if (android::uirenderer::ResourceCache::hasInstance()) {
85            Res_png_9patch* p = (Res_png_9patch*) patch;
86            android::uirenderer::ResourceCache::getInstance().destructor(p);
87            return;
88        }
89#endif // USE_OPENGL_RENDERER
90        delete[] patch;
91    }
92
93    static void draw(JNIEnv* env, SkCanvas* canvas, SkRect& bounds, const SkBitmap* bitmap,
94            Res_png_9patch* chunk, const SkPaint* paint, jint destDensity, jint srcDensity) {
95        if (destDensity == srcDensity || destDensity == 0 || srcDensity == 0) {
96            ALOGV("Drawing unscaled 9-patch: (%g,%g)-(%g,%g)",
97                    SkScalarToFloat(bounds.fLeft), SkScalarToFloat(bounds.fTop),
98                    SkScalarToFloat(bounds.fRight), SkScalarToFloat(bounds.fBottom));
99            NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL);
100        } else {
101            canvas->save();
102
103            SkScalar scale = destDensity / (float)srcDensity;
104            canvas->translate(bounds.fLeft, bounds.fTop);
105            canvas->scale(scale, scale);
106
107            bounds.fRight = SkScalarDiv(bounds.fRight-bounds.fLeft, scale);
108            bounds.fBottom = SkScalarDiv(bounds.fBottom-bounds.fTop, scale);
109            bounds.fLeft = bounds.fTop = 0;
110
111            ALOGV("Drawing scaled 9-patch: (%g,%g)-(%g,%g) srcDensity=%d destDensity=%d",
112                    SkScalarToFloat(bounds.fLeft), SkScalarToFloat(bounds.fTop),
113                    SkScalarToFloat(bounds.fRight), SkScalarToFloat(bounds.fBottom),
114                    srcDensity, destDensity);
115
116            NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL);
117
118            canvas->restore();
119        }
120    }
121
122    static void drawF(JNIEnv* env, jobject, jlong canvasHandle, jobject boundsRectF,
123            jlong bitmapHandle, jlong chunkHandle, jlong paintHandle,
124            jint destDensity, jint srcDensity) {
125        SkCanvas* canvas       = reinterpret_cast<Canvas*>(canvasHandle)->getSkCanvas();
126        const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle);
127        Res_png_9patch* chunk  = reinterpret_cast<Res_png_9patch*>(chunkHandle);
128        const Paint* paint     = reinterpret_cast<Paint*>(paintHandle);
129        SkASSERT(canvas);
130        SkASSERT(boundsRectF);
131        SkASSERT(bitmap);
132        SkASSERT(chunk);
133        // paint is optional
134
135        SkRect bounds;
136        GraphicsJNI::jrectf_to_rect(env, boundsRectF, &bounds);
137
138        draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity);
139    }
140
141    static void drawI(JNIEnv* env, jobject, jlong canvasHandle, jobject boundsRect,
142            jlong bitmapHandle, jlong chunkHandle, jlong paintHandle,
143            jint destDensity, jint srcDensity) {
144        SkCanvas* canvas       = reinterpret_cast<Canvas*>(canvasHandle)->getSkCanvas();
145        const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle);
146        Res_png_9patch* chunk  = reinterpret_cast<Res_png_9patch*>(chunkHandle);
147        const Paint* paint     = reinterpret_cast<Paint*>(paintHandle);
148        SkASSERT(canvas);
149        SkASSERT(boundsRect);
150        SkASSERT(bitmap);
151        SkASSERT(chunk);
152        // paint is optional
153
154        SkRect bounds;
155        GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds);
156        draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity);
157    }
158
159    static jlong getTransparentRegion(JNIEnv* env, jobject, jlong bitmapHandle,
160            jlong chunkHandle, jobject boundsRect) {
161        const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle);
162        Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
163        SkASSERT(bitmap);
164        SkASSERT(chunk);
165        SkASSERT(boundsRect);
166
167        SkRect bounds;
168        GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds);
169
170        SkRegion* region = NULL;
171        NinePatch_Draw(NULL, bounds, *bitmap, *chunk, NULL, &region);
172
173        return reinterpret_cast<jlong>(region);
174    }
175
176};
177
178/////////////////////////////////////////////////////////////////////////////////////////
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::RegisterMethodsOrDie(env,
192            "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods));
193}
194