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
34extern void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, const SkBitmap& bitmap,
35        const android::Res_png_9patch& chunk, const SkPaint* paint, SkRegion** outRegion);
36
37using 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
47class SkNinePatchGlue {
48public:
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, jlong, 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#ifdef USE_OPENGL_RENDERER
83        if (android::uirenderer::ResourceCache::hasInstance()) {
84            Res_png_9patch* p = (Res_png_9patch*) patch;
85            android::uirenderer::ResourceCache::getInstance().destructor(p);
86            return;
87        }
88#endif // USE_OPENGL_RENDERER
89        delete[] patch;
90    }
91
92    static void draw(JNIEnv* env, SkCanvas* canvas, SkRect& bounds, const SkBitmap* bitmap,
93            Res_png_9patch* chunk, const SkPaint* paint, jint destDensity, jint srcDensity) {
94        if (destDensity == srcDensity || destDensity == 0 || srcDensity == 0) {
95            ALOGV("Drawing unscaled 9-patch: (%g,%g)-(%g,%g)",
96                    SkScalarToFloat(bounds.fLeft), SkScalarToFloat(bounds.fTop),
97                    SkScalarToFloat(bounds.fRight), SkScalarToFloat(bounds.fBottom));
98            NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL);
99        } else {
100            canvas->save();
101
102            SkScalar scale = destDensity / (float)srcDensity;
103            canvas->translate(bounds.fLeft, bounds.fTop);
104            canvas->scale(scale, scale);
105
106            bounds.fRight = SkScalarDiv(bounds.fRight-bounds.fLeft, scale);
107            bounds.fBottom = SkScalarDiv(bounds.fBottom-bounds.fTop, scale);
108            bounds.fLeft = bounds.fTop = 0;
109
110            ALOGV("Drawing scaled 9-patch: (%g,%g)-(%g,%g) srcDensity=%d destDensity=%d",
111                    SkScalarToFloat(bounds.fLeft), SkScalarToFloat(bounds.fTop),
112                    SkScalarToFloat(bounds.fRight), SkScalarToFloat(bounds.fBottom),
113                    srcDensity, destDensity);
114
115            NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL);
116
117            canvas->restore();
118        }
119    }
120
121    static void drawF(JNIEnv* env, jobject, jlong canvasHandle, jobject boundsRectF,
122            jlong bitmapHandle, jlong chunkHandle, jlong paintHandle,
123            jint destDensity, jint srcDensity) {
124        SkCanvas* canvas       = reinterpret_cast<Canvas*>(canvasHandle)->getSkCanvas();
125        const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle);
126        Res_png_9patch* chunk  = reinterpret_cast<Res_png_9patch*>(chunkHandle);
127        const Paint* paint     = reinterpret_cast<Paint*>(paintHandle);
128        SkASSERT(canvas);
129        SkASSERT(boundsRectF);
130        SkASSERT(bitmap);
131        SkASSERT(chunk);
132        // paint is optional
133
134        SkRect bounds;
135        GraphicsJNI::jrectf_to_rect(env, boundsRectF, &bounds);
136
137        draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity);
138    }
139
140    static void drawI(JNIEnv* env, jobject, jlong canvasHandle, jobject boundsRect,
141            jlong bitmapHandle, jlong chunkHandle, jlong paintHandle,
142            jint destDensity, jint srcDensity) {
143        SkCanvas* canvas       = reinterpret_cast<Canvas*>(canvasHandle)->getSkCanvas();
144        const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle);
145        Res_png_9patch* chunk  = reinterpret_cast<Res_png_9patch*>(chunkHandle);
146        const Paint* paint     = reinterpret_cast<Paint*>(paintHandle);
147        SkASSERT(canvas);
148        SkASSERT(boundsRect);
149        SkASSERT(bitmap);
150        SkASSERT(chunk);
151        // paint is optional
152
153        SkRect bounds;
154        GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds);
155        draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity);
156    }
157
158    static jlong getTransparentRegion(JNIEnv* env, jobject, jlong bitmapHandle,
159            jlong chunkHandle, jobject boundsRect) {
160        const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle);
161        Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
162        SkASSERT(bitmap);
163        SkASSERT(chunk);
164        SkASSERT(boundsRect);
165
166        SkRect bounds;
167        GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds);
168
169        SkRegion* region = NULL;
170        NinePatch_Draw(NULL, bounds, *bitmap, *chunk, NULL, &region);
171
172        return reinterpret_cast<jlong>(region);
173    }
174
175};
176
177/////////////////////////////////////////////////////////////////////////////////////////
178
179#include <android_runtime/AndroidRuntime.h>
180
181static JNINativeMethod gNinePatchMethods[] = {
182    { "isNinePatchChunk", "([B)Z",                        (void*) SkNinePatchGlue::isNinePatchChunk },
183    { "validateNinePatchChunk", "(J[B)J",                 (void*) SkNinePatchGlue::validateNinePatchChunk },
184    { "nativeFinalize", "(J)V",                           (void*) SkNinePatchGlue::finalize },
185    { "nativeDraw", "(JLandroid/graphics/RectF;JJJII)V",  (void*) SkNinePatchGlue::drawF },
186    { "nativeDraw", "(JLandroid/graphics/Rect;JJJII)V",   (void*) SkNinePatchGlue::drawI },
187    { "nativeGetTransparentRegion", "(JJLandroid/graphics/Rect;)J",
188                                                          (void*) SkNinePatchGlue::getTransparentRegion }
189};
190
191int register_android_graphics_NinePatch(JNIEnv* env) {
192    return android::AndroidRuntime::registerNativeMethods(env,
193            "android/graphics/NinePatch", gNinePatchMethods, SK_ARRAY_COUNT(gNinePatchMethods));
194}
195