blob: 7e6aeae0ac7ce2962017c81408ee3f04d8d40139 [file] [log] [blame]
Dianne Hackborn8cae1242009-09-10 14:32:16 -07001/*
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
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080021#include <androidfw/ResourceTypes.h>
Dianne Hackborn8cae1242009-09-10 14:32:16 -070022#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
Romain Guye3b0a012013-06-26 15:45:41 -070024#include <Caches.h>
25
Dianne Hackborn11ea3342009-07-22 21:48:55 -070026#include "SkCanvas.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include "SkRegion.h"
28#include "GraphicsJNI.h"
29
30#include "JNIHelp.h"
31
Romain Guye3b0a012013-06-26 15:45:41 -070032extern void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, const SkBitmap& bitmap,
33 const android::Res_png_9patch& chunk, const SkPaint* paint, SkRegion** outRegion);
Elliott Hughes69a017b2011-04-08 14:10:28 -070034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035using namespace android;
36
Romain Guye3b0a012013-06-26 15:45:41 -070037/**
38 * IMPORTANT NOTE: 9patch chunks can be manipuated either as an array of bytes
39 * or as a Res_png_9patch instance. It is important to note that the size of the
40 * array required to hold a 9patch chunk is greater than sizeof(Res_png_9patch).
41 * The code below manipulates chunks as Res_png_9patch* types to draw and as
42 * int8_t* to allocate and free the backing storage.
43 */
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045class SkNinePatchGlue {
46public:
Romain Guye3b0a012013-06-26 15:45:41 -070047 static jboolean isNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 if (NULL == obj) {
49 return false;
50 }
51 if (env->GetArrayLength(obj) < (int)sizeof(Res_png_9patch)) {
52 return false;
53 }
54 const jbyte* array = env->GetByteArrayElements(obj, 0);
55 if (array != NULL) {
Romain Guye3b0a012013-06-26 15:45:41 -070056 const Res_png_9patch* chunk = reinterpret_cast<const Res_png_9patch*>(array);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 int8_t wasDeserialized = chunk->wasDeserialized;
Romain Guye3b0a012013-06-26 15:45:41 -070058 env->ReleaseByteArrayElements(obj, const_cast<jbyte*>(array), JNI_ABORT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 return wasDeserialized != -1;
60 }
61 return false;
62 }
63
Romain Guye3b0a012013-06-26 15:45:41 -070064 static int8_t* validateNinePatchChunk(JNIEnv* env, jobject, jint, jbyteArray obj) {
65 size_t chunkSize = env->GetArrayLength(obj);
66 if (chunkSize < (int) (sizeof(Res_png_9patch))) {
Elliott Hughes69a017b2011-04-08 14:10:28 -070067 jniThrowRuntimeException(env, "Array too small for chunk.");
Romain Guye3b0a012013-06-26 15:45:41 -070068 return NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 }
70
Romain Guye3b0a012013-06-26 15:45:41 -070071 int8_t* storage = new int8_t[chunkSize];
72 // This call copies the content of the jbyteArray
73 env->GetByteArrayRegion(obj, 0, chunkSize, reinterpret_cast<jbyte*>(storage));
74 // Deserialize in place, return the array we just allocated
75 return (int8_t*) Res_png_9patch::deserialize(storage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 }
77
Romain Guye3b0a012013-06-26 15:45:41 -070078 static void finalize(JNIEnv* env, jobject, int8_t* patch) {
79#ifdef USE_OPENGL_RENDERER
80 if (android::uirenderer::Caches::hasInstance()) {
81 Res_png_9patch* p = (Res_png_9patch*) patch;
82 android::uirenderer::Caches::getInstance().resourceCache.destructor(p);
83 return;
84 }
85#endif // USE_OPENGL_RENDERER
86 delete[] patch;
87 }
Elliott Hughes69a017b2011-04-08 14:10:28 -070088
Romain Guye3b0a012013-06-26 15:45:41 -070089 static void draw(JNIEnv* env, SkCanvas* canvas, SkRect& bounds, const SkBitmap* bitmap,
90 Res_png_9patch* chunk, const SkPaint* paint, jint destDensity, jint srcDensity) {
91 if (destDensity == srcDensity || destDensity == 0 || srcDensity == 0) {
92 ALOGV("Drawing unscaled 9-patch: (%g,%g)-(%g,%g)",
93 SkScalarToFloat(bounds.fLeft), SkScalarToFloat(bounds.fTop),
94 SkScalarToFloat(bounds.fRight), SkScalarToFloat(bounds.fBottom));
95 NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL);
96 } else {
97 canvas->save();
Elliott Hughes69a017b2011-04-08 14:10:28 -070098
Romain Guye3b0a012013-06-26 15:45:41 -070099 SkScalar scale = SkFloatToScalar(destDensity / (float)srcDensity);
100 canvas->translate(bounds.fLeft, bounds.fTop);
101 canvas->scale(scale, scale);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700102
Romain Guye3b0a012013-06-26 15:45:41 -0700103 bounds.fRight = SkScalarDiv(bounds.fRight-bounds.fLeft, scale);
104 bounds.fBottom = SkScalarDiv(bounds.fBottom-bounds.fTop, scale);
105 bounds.fLeft = bounds.fTop = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700106
Romain Guye3b0a012013-06-26 15:45:41 -0700107 ALOGV("Drawing scaled 9-patch: (%g,%g)-(%g,%g) srcDensity=%d destDensity=%d",
108 SkScalarToFloat(bounds.fLeft), SkScalarToFloat(bounds.fTop),
109 SkScalarToFloat(bounds.fRight), SkScalarToFloat(bounds.fBottom),
110 srcDensity, destDensity);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700111
Romain Guye3b0a012013-06-26 15:45:41 -0700112 NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700113
Romain Guye3b0a012013-06-26 15:45:41 -0700114 canvas->restore();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
118 static void drawF(JNIEnv* env, jobject, SkCanvas* canvas, jobject boundsRectF,
Romain Guye3b0a012013-06-26 15:45:41 -0700119 const SkBitmap* bitmap, Res_png_9patch* chunk, const SkPaint* paint,
120 jint destDensity, jint srcDensity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 SkASSERT(canvas);
122 SkASSERT(boundsRectF);
123 SkASSERT(bitmap);
Romain Guye3b0a012013-06-26 15:45:41 -0700124 SkASSERT(chunk);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 // paint is optional
126
Romain Guye3b0a012013-06-26 15:45:41 -0700127 SkRect bounds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 GraphicsJNI::jrectf_to_rect(env, boundsRectF, &bounds);
129
Romain Guye3b0a012013-06-26 15:45:41 -0700130 draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 static void drawI(JNIEnv* env, jobject, SkCanvas* canvas, jobject boundsRect,
Romain Guye3b0a012013-06-26 15:45:41 -0700134 const SkBitmap* bitmap, Res_png_9patch* chunk, const SkPaint* paint,
135 jint destDensity, jint srcDensity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 SkASSERT(canvas);
137 SkASSERT(boundsRect);
138 SkASSERT(bitmap);
Romain Guye3b0a012013-06-26 15:45:41 -0700139 SkASSERT(chunk);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 // paint is optional
141
Romain Guye3b0a012013-06-26 15:45:41 -0700142 SkRect bounds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds);
Romain Guye3b0a012013-06-26 15:45:41 -0700144 draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700146
Romain Guye3b0a012013-06-26 15:45:41 -0700147 static jint getTransparentRegion(JNIEnv* env, jobject, const SkBitmap* bitmap,
148 Res_png_9patch* chunk, jobject boundsRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 SkASSERT(bitmap);
Romain Guye3b0a012013-06-26 15:45:41 -0700150 SkASSERT(chunk);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 SkASSERT(boundsRect);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700152
Romain Guye3b0a012013-06-26 15:45:41 -0700153 SkRect bounds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds);
Romain Guye3b0a012013-06-26 15:45:41 -0700155
156 SkRegion* region = NULL;
157 NinePatch_Draw(NULL, bounds, *bitmap, *chunk, NULL, &region);
158
159 return (jint) region;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 }
161
162};
163
164/////////////////////////////////////////////////////////////////////////////////////////
165
166#include <android_runtime/AndroidRuntime.h>
167
168static JNINativeMethod gNinePatchMethods[] = {
Romain Guye3b0a012013-06-26 15:45:41 -0700169 { "isNinePatchChunk", "([B)Z", (void*) SkNinePatchGlue::isNinePatchChunk },
170 { "validateNinePatchChunk", "(I[B)I", (void*) SkNinePatchGlue::validateNinePatchChunk },
171 { "nativeFinalize", "(I)V", (void*) SkNinePatchGlue::finalize },
172 { "nativeDraw", "(ILandroid/graphics/RectF;IIIII)V", (void*) SkNinePatchGlue::drawF },
173 { "nativeDraw", "(ILandroid/graphics/Rect;IIIII)V", (void*) SkNinePatchGlue::drawI },
174 { "nativeGetTransparentRegion", "(IILandroid/graphics/Rect;)I",
175 (void*) SkNinePatchGlue::getTransparentRegion }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176};
177
Romain Guye3b0a012013-06-26 15:45:41 -0700178int register_android_graphics_NinePatch(JNIEnv* env) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 return android::AndroidRuntime::registerNativeMethods(env,
Romain Guye3b0a012013-06-26 15:45:41 -0700180 "android/graphics/NinePatch", gNinePatchMethods, SK_ARRAY_COUNT(gNinePatchMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181}