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 | |
Derek Sollenberger | 5368eda | 2019-10-25 11:20:03 -0400 | [diff] [blame^] | 18 | #undef LOG_TAG |
Dianne Hackborn | 8cae124 | 2009-09-10 14:32:16 -0700 | [diff] [blame] | 19 | #define LOG_TAG "9patch" |
| 20 | #define LOG_NDEBUG 1 |
| 21 | |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 22 | #include <androidfw/ResourceTypes.h> |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 23 | #include <hwui/Canvas.h> |
| 24 | #include <hwui/Paint.h> |
Dianne Hackborn | 8cae124 | 2009-09-10 14:32:16 -0700 | [diff] [blame] | 25 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 27 | #include "SkCanvas.h" |
Matt Sarett | 7de7385 | 2016-10-25 18:36:39 -0400 | [diff] [blame] | 28 | #include "SkLatticeIter.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include "SkRegion.h" |
| 30 | #include "GraphicsJNI.h" |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 31 | #include "NinePatchPeeker.h" |
Matt Sarett | 7de7385 | 2016-10-25 18:36:39 -0400 | [diff] [blame] | 32 | #include "NinePatchUtils.h" |
Derek Sollenberger | 4c5efe9 | 2015-07-10 13:56:39 -0400 | [diff] [blame] | 33 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 34 | #include <nativehelper/JNIHelp.h> |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 35 | #include "core_jni_helpers.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 37 | jclass gInsetStruct_class; |
| 38 | jmethodID gInsetStruct_constructorMethodID; |
| 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | using namespace android; |
| 41 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 42 | /** |
| 43 | * IMPORTANT NOTE: 9patch chunks can be manipuated either as an array of bytes |
| 44 | * or as a Res_png_9patch instance. It is important to note that the size of the |
| 45 | * array required to hold a 9patch chunk is greater than sizeof(Res_png_9patch). |
| 46 | * The code below manipulates chunks as Res_png_9patch* types to draw and as |
| 47 | * int8_t* to allocate and free the backing storage. |
| 48 | */ |
| 49 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | class SkNinePatchGlue { |
| 51 | public: |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 52 | static jboolean isNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | if (NULL == obj) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 54 | return JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | } |
| 56 | if (env->GetArrayLength(obj) < (int)sizeof(Res_png_9patch)) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 57 | return JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | } |
| 59 | const jbyte* array = env->GetByteArrayElements(obj, 0); |
| 60 | if (array != NULL) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 61 | 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] | 62 | int8_t wasDeserialized = chunk->wasDeserialized; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 63 | env->ReleaseByteArrayElements(obj, const_cast<jbyte*>(array), JNI_ABORT); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 64 | return (wasDeserialized != -1) ? JNI_TRUE : JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 66 | return JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | } |
| 68 | |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 69 | static jlong validateNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 70 | size_t chunkSize = env->GetArrayLength(obj); |
| 71 | if (chunkSize < (int) (sizeof(Res_png_9patch))) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 72 | jniThrowRuntimeException(env, "Array too small for chunk."); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 73 | return NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 76 | int8_t* storage = new int8_t[chunkSize]; |
| 77 | // This call copies the content of the jbyteArray |
| 78 | env->GetByteArrayRegion(obj, 0, chunkSize, reinterpret_cast<jbyte*>(storage)); |
| 79 | // Deserialize in place, return the array we just allocated |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 80 | return reinterpret_cast<jlong>(Res_png_9patch::deserialize(storage)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 83 | static void finalize(JNIEnv* env, jobject, jlong patchHandle) { |
| 84 | int8_t* patch = reinterpret_cast<int8_t*>(patchHandle); |
Derek Sollenberger | 7290327 | 2018-09-20 14:56:27 -0400 | [diff] [blame] | 85 | delete[] patch; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 86 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 87 | |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 88 | static jlong getTransparentRegion(JNIEnv* env, jobject, jlong bitmapPtr, |
Matt Sarett | 7de7385 | 2016-10-25 18:36:39 -0400 | [diff] [blame] | 89 | jlong chunkHandle, jobject dstRect) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 90 | Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 91 | SkASSERT(chunk); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 92 | |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 93 | SkBitmap bitmap; |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 94 | bitmap::toBitmap(bitmapPtr).getSkBitmap(&bitmap); |
Matt Sarett | 7de7385 | 2016-10-25 18:36:39 -0400 | [diff] [blame] | 95 | SkRect dst; |
| 96 | GraphicsJNI::jrect_to_rect(env, dstRect, &dst); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 97 | |
Matt Sarett | 7de7385 | 2016-10-25 18:36:39 -0400 | [diff] [blame] | 98 | SkCanvas::Lattice lattice; |
| 99 | SkIRect src = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| 100 | lattice.fBounds = &src; |
| 101 | NinePatchUtils::SetLatticeDivs(&lattice, *chunk, bitmap.width(), bitmap.height()); |
Stan Iliev | e12d731 | 2017-12-04 14:48:27 -0500 | [diff] [blame] | 102 | lattice.fRectTypes = nullptr; |
| 103 | lattice.fColors = nullptr; |
Matt Sarett | 7de7385 | 2016-10-25 18:36:39 -0400 | [diff] [blame] | 104 | |
| 105 | SkRegion* region = nullptr; |
| 106 | if (SkLatticeIter::Valid(bitmap.width(), bitmap.height(), lattice)) { |
| 107 | SkLatticeIter iter(lattice, dst); |
| 108 | if (iter.numRectsToDraw() == chunk->numColors) { |
| 109 | SkRect dummy; |
| 110 | SkRect iterDst; |
| 111 | int index = 0; |
| 112 | while (iter.next(&dummy, &iterDst)) { |
| 113 | if (0 == chunk->getColors()[index++] && !iterDst.isEmpty()) { |
| 114 | if (!region) { |
| 115 | region = new SkRegion(); |
| 116 | } |
| 117 | |
| 118 | region->op(iterDst.round(), SkRegion::kUnion_Op); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 123 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 124 | return reinterpret_cast<jlong>(region); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | }; |
| 128 | |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 129 | jobject NinePatchPeeker::createNinePatchInsets(JNIEnv* env, float scale) const { |
| 130 | if (!mHasInsets) { |
| 131 | return nullptr; |
| 132 | } |
| 133 | |
| 134 | return env->NewObject(gInsetStruct_class, gInsetStruct_constructorMethodID, |
| 135 | mOpticalInsets[0], mOpticalInsets[1], |
| 136 | mOpticalInsets[2], mOpticalInsets[3], |
| 137 | mOutlineInsets[0], mOutlineInsets[1], |
| 138 | mOutlineInsets[2], mOutlineInsets[3], |
| 139 | mOutlineRadius, mOutlineAlpha, scale); |
| 140 | } |
| 141 | |
| 142 | void NinePatchPeeker::getPadding(JNIEnv* env, jobject outPadding) const { |
| 143 | if (mPatch) { |
| 144 | GraphicsJNI::set_jrect(env, outPadding, |
| 145 | mPatch->paddingLeft, mPatch->paddingTop, |
| 146 | mPatch->paddingRight, mPatch->paddingBottom); |
| 147 | |
| 148 | } else { |
| 149 | GraphicsJNI::set_jrect(env, outPadding, -1, -1, -1, -1); |
| 150 | } |
| 151 | } |
| 152 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 154 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 155 | static const JNINativeMethod gNinePatchMethods[] = { |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 156 | { "isNinePatchChunk", "([B)Z", (void*) SkNinePatchGlue::isNinePatchChunk }, |
| 157 | { "validateNinePatchChunk", "([B)J", |
| 158 | (void*) SkNinePatchGlue::validateNinePatchChunk }, |
| 159 | { "nativeFinalize", "(J)V", (void*) SkNinePatchGlue::finalize }, |
Leon Scroggins III | 71fae62 | 2019-03-26 16:28:41 -0400 | [diff] [blame] | 160 | { "nativeGetTransparentRegion", "(JJLandroid/graphics/Rect;)J", |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 161 | (void*) SkNinePatchGlue::getTransparentRegion } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 164 | int register_android_graphics_NinePatch(JNIEnv* env) { |
Leon Scroggins III | 0c01dbf | 2017-10-20 14:08:11 -0400 | [diff] [blame] | 165 | gInsetStruct_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, |
| 166 | "android/graphics/NinePatch$InsetStruct")); |
| 167 | gInsetStruct_constructorMethodID = GetMethodIDOrDie(env, gInsetStruct_class, "<init>", |
| 168 | "(IIIIIIIIFIF)V"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 169 | return android::RegisterMethodsOrDie(env, |
| 170 | "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |