Dianne Hackborn | 8cae124 | 2009-09-10 14:32:16 -0700 | [diff] [blame] | 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 | |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 21 | #include <androidfw/ResourceTypes.h> |
Dianne Hackborn | 8cae124 | 2009-09-10 14:32:16 -0700 | [diff] [blame] | 22 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 24 | #include <Caches.h> |
| 25 | |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 26 | #include "SkCanvas.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include "SkRegion.h" |
| 28 | #include "GraphicsJNI.h" |
| 29 | |
| 30 | #include "JNIHelp.h" |
| 31 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 32 | extern void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, const SkBitmap& bitmap, |
| 33 | const android::Res_png_9patch& chunk, const SkPaint* paint, SkRegion** outRegion); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 34 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | using namespace android; |
| 36 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 37 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | class SkNinePatchGlue { |
| 46 | public: |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 47 | static jboolean isNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | 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 Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 56 | const Res_png_9patch* chunk = reinterpret_cast<const Res_png_9patch*>(array); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | int8_t wasDeserialized = chunk->wasDeserialized; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 58 | env->ReleaseByteArrayElements(obj, const_cast<jbyte*>(array), JNI_ABORT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | return wasDeserialized != -1; |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 64 | 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 Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 67 | jniThrowRuntimeException(env, "Array too small for chunk."); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 68 | return NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 71 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 78 | 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 Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 88 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 89 | 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 Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 98 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 99 | SkScalar scale = SkFloatToScalar(destDensity / (float)srcDensity); |
| 100 | canvas->translate(bounds.fLeft, bounds.fTop); |
| 101 | canvas->scale(scale, scale); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 102 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 103 | bounds.fRight = SkScalarDiv(bounds.fRight-bounds.fLeft, scale); |
| 104 | bounds.fBottom = SkScalarDiv(bounds.fBottom-bounds.fTop, scale); |
| 105 | bounds.fLeft = bounds.fTop = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 106 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 107 | 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 Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 111 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 112 | NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 113 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 114 | canvas->restore(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 116 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | |
| 118 | static void drawF(JNIEnv* env, jobject, SkCanvas* canvas, jobject boundsRectF, |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 119 | const SkBitmap* bitmap, Res_png_9patch* chunk, const SkPaint* paint, |
| 120 | jint destDensity, jint srcDensity) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | SkASSERT(canvas); |
| 122 | SkASSERT(boundsRectF); |
| 123 | SkASSERT(bitmap); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 124 | SkASSERT(chunk); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | // paint is optional |
| 126 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 127 | SkRect bounds; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | GraphicsJNI::jrectf_to_rect(env, boundsRectF, &bounds); |
| 129 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 130 | draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 132 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | static void drawI(JNIEnv* env, jobject, SkCanvas* canvas, jobject boundsRect, |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 134 | const SkBitmap* bitmap, Res_png_9patch* chunk, const SkPaint* paint, |
| 135 | jint destDensity, jint srcDensity) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | SkASSERT(canvas); |
| 137 | SkASSERT(boundsRect); |
| 138 | SkASSERT(bitmap); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 139 | SkASSERT(chunk); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | // paint is optional |
| 141 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 142 | SkRect bounds; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 144 | draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 146 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 147 | static jint getTransparentRegion(JNIEnv* env, jobject, const SkBitmap* bitmap, |
| 148 | Res_png_9patch* chunk, jobject boundsRect) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | SkASSERT(bitmap); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 150 | SkASSERT(chunk); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | SkASSERT(boundsRect); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 152 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 153 | SkRect bounds; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 155 | |
| 156 | SkRegion* region = NULL; |
| 157 | NinePatch_Draw(NULL, bounds, *bitmap, *chunk, NULL, ®ion); |
| 158 | |
| 159 | return (jint) region; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | }; |
| 163 | |
| 164 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 165 | |
| 166 | #include <android_runtime/AndroidRuntime.h> |
| 167 | |
| 168 | static JNINativeMethod gNinePatchMethods[] = { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 169 | { "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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | }; |
| 177 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 178 | int register_android_graphics_NinePatch(JNIEnv* env) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | return android::AndroidRuntime::registerNativeMethods(env, |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 180 | "android/graphics/NinePatch", gNinePatchMethods, SK_ARRAY_COUNT(gNinePatchMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | } |