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 | |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 24 | #include <ResourceCache.h> |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 25 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 26 | #include "Paint.h" |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 27 | #include "Canvas.h" |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 28 | #include "SkCanvas.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include "SkRegion.h" |
| 30 | #include "GraphicsJNI.h" |
| 31 | |
| 32 | #include "JNIHelp.h" |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 33 | #include "core_jni_helpers.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 35 | extern void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, const SkBitmap& bitmap, |
| 36 | const android::Res_png_9patch& chunk, const SkPaint* paint, SkRegion** outRegion); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 37 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | using namespace android; |
| 39 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | class SkNinePatchGlue { |
| 49 | public: |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 50 | static jboolean isNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | if (NULL == obj) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 52 | return JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | } |
| 54 | if (env->GetArrayLength(obj) < (int)sizeof(Res_png_9patch)) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 55 | return JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | } |
| 57 | const jbyte* array = env->GetByteArrayElements(obj, 0); |
| 58 | if (array != NULL) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 59 | 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] | 60 | int8_t wasDeserialized = chunk->wasDeserialized; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 61 | env->ReleaseByteArrayElements(obj, const_cast<jbyte*>(array), JNI_ABORT); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 62 | return (wasDeserialized != -1) ? JNI_TRUE : JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 64 | return JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | } |
| 66 | |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 67 | static jlong validateNinePatchChunk(JNIEnv* env, jobject, jlong, jbyteArray obj) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 68 | size_t chunkSize = env->GetArrayLength(obj); |
| 69 | if (chunkSize < (int) (sizeof(Res_png_9patch))) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 70 | jniThrowRuntimeException(env, "Array too small for chunk."); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 71 | return NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 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 |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 78 | return reinterpret_cast<jlong>(Res_png_9patch::deserialize(storage)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 81 | static void finalize(JNIEnv* env, jobject, jlong patchHandle) { |
| 82 | int8_t* patch = reinterpret_cast<int8_t*>(patchHandle); |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 83 | if (android::uirenderer::ResourceCache::hasInstance()) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 84 | Res_png_9patch* p = (Res_png_9patch*) patch; |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 85 | android::uirenderer::ResourceCache::getInstance().destructor(p); |
John Reck | a2732a2 | 2014-12-18 13:52:33 -0800 | [diff] [blame] | 86 | } else { |
| 87 | delete[] patch; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 88 | } |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 89 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 90 | |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 91 | static void draw(JNIEnv* env, SkCanvas* canvas, SkRect& bounds, const SkBitmap* bitmap, |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 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)); |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 97 | NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 98 | } else { |
| 99 | canvas->save(); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 100 | |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 101 | SkScalar scale = destDensity / (float)srcDensity; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 102 | canvas->translate(bounds.fLeft, bounds.fTop); |
| 103 | canvas->scale(scale, scale); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 104 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 105 | bounds.fRight = SkScalarDiv(bounds.fRight-bounds.fLeft, scale); |
| 106 | bounds.fBottom = SkScalarDiv(bounds.fBottom-bounds.fTop, scale); |
| 107 | bounds.fLeft = bounds.fTop = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 108 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 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); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 113 | |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 114 | NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 115 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 116 | canvas->restore(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 118 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 120 | static void drawF(JNIEnv* env, jobject, jlong canvasHandle, jobject boundsRectF, |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 121 | jlong bitmapHandle, jlong chunkHandle, jlong paintHandle, |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 122 | jint destDensity, jint srcDensity) { |
Derek Sollenberger | b3d50e0 | 2015-01-29 11:19:31 -0500 | [diff] [blame] | 123 | SkCanvas* canvas = reinterpret_cast<Canvas*>(canvasHandle)->asSkCanvas(); |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 124 | const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 125 | Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 126 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | SkASSERT(canvas); |
| 128 | SkASSERT(boundsRectF); |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 129 | SkASSERT(bitmap); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 130 | SkASSERT(chunk); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | // paint is optional |
| 132 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 133 | SkRect bounds; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | GraphicsJNI::jrectf_to_rect(env, boundsRectF, &bounds); |
| 135 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 136 | draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 138 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 139 | static void drawI(JNIEnv* env, jobject, jlong canvasHandle, jobject boundsRect, |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 140 | jlong bitmapHandle, jlong chunkHandle, jlong paintHandle, |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 141 | jint destDensity, jint srcDensity) { |
Derek Sollenberger | b3d50e0 | 2015-01-29 11:19:31 -0500 | [diff] [blame] | 142 | SkCanvas* canvas = reinterpret_cast<Canvas*>(canvasHandle)->asSkCanvas(); |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 143 | const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 144 | Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 145 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | SkASSERT(canvas); |
| 147 | SkASSERT(boundsRect); |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 148 | SkASSERT(bitmap); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 149 | SkASSERT(chunk); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | // paint is optional |
| 151 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 152 | SkRect bounds; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 154 | draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 156 | |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 157 | static jlong getTransparentRegion(JNIEnv* env, jobject, jlong bitmapHandle, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 158 | jlong chunkHandle, jobject boundsRect) { |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 159 | const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 160 | Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle); |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 161 | SkASSERT(bitmap); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 162 | SkASSERT(chunk); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | SkASSERT(boundsRect); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 164 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 165 | SkRect bounds; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 167 | |
| 168 | SkRegion* region = NULL; |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 169 | NinePatch_Draw(NULL, bounds, *bitmap, *chunk, NULL, ®ion); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 170 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 171 | return reinterpret_cast<jlong>(region); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | }; |
| 175 | |
| 176 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 177 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | static JNINativeMethod gNinePatchMethods[] = { |
John Reck | 1ff961d | 2015-04-17 20:45:15 +0000 | [diff] [blame] | 179 | { "isNinePatchChunk", "([B)Z", (void*) SkNinePatchGlue::isNinePatchChunk }, |
| 180 | { "validateNinePatchChunk", "(J[B)J", (void*) SkNinePatchGlue::validateNinePatchChunk }, |
| 181 | { "nativeFinalize", "(J)V", (void*) SkNinePatchGlue::finalize }, |
| 182 | { "nativeDraw", "(JLandroid/graphics/RectF;JJJII)V", (void*) SkNinePatchGlue::drawF }, |
| 183 | { "nativeDraw", "(JLandroid/graphics/Rect;JJJII)V", (void*) SkNinePatchGlue::drawI }, |
| 184 | { "nativeGetTransparentRegion", "(JJLandroid/graphics/Rect;)J", |
| 185 | (void*) SkNinePatchGlue::getTransparentRegion } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | }; |
| 187 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 188 | int register_android_graphics_NinePatch(JNIEnv* env) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 189 | return android::RegisterMethodsOrDie(env, |
| 190 | "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | } |