blob: 988615524cca0c866d6f44be977d6604a3d513ff [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
Derek Sollenberger5368eda2019-10-25 11:20:03 -040018#undef LOG_TAG
Dianne Hackborn8cae1242009-09-10 14:32:16 -070019#define LOG_TAG "9patch"
20#define LOG_NDEBUG 1
21
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080022#include <androidfw/ResourceTypes.h>
sergeyvdccca442016-03-21 15:38:21 -070023#include <hwui/Canvas.h>
24#include <hwui/Paint.h>
Dianne Hackborn8cae1242009-09-10 14:32:16 -070025#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
Dianne Hackborn11ea3342009-07-22 21:48:55 -070027#include "SkCanvas.h"
Matt Sarett7de73852016-10-25 18:36:39 -040028#include "SkLatticeIter.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include "SkRegion.h"
30#include "GraphicsJNI.h"
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040031#include "NinePatchPeeker.h"
Matt Sarett7de73852016-10-25 18:36:39 -040032#include "NinePatchUtils.h"
Derek Sollenberger4c5efe92015-07-10 13:56:39 -040033
Steven Moreland2279b252017-07-19 09:50:45 -070034#include <nativehelper/JNIHelp.h>
Andreas Gampeed6b9df2014-11-20 22:02:20 -080035#include "core_jni_helpers.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040037jclass gInsetStruct_class;
38jmethodID gInsetStruct_constructorMethodID;
39
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040using namespace android;
41
Romain Guye3b0a012013-06-26 15:45:41 -070042/**
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 Project9066cfe2009-03-03 19:31:44 -080050class SkNinePatchGlue {
51public:
Romain Guye3b0a012013-06-26 15:45:41 -070052 static jboolean isNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 if (NULL == obj) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000054 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 }
56 if (env->GetArrayLength(obj) < (int)sizeof(Res_png_9patch)) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000057 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 }
59 const jbyte* array = env->GetByteArrayElements(obj, 0);
60 if (array != NULL) {
Romain Guye3b0a012013-06-26 15:45:41 -070061 const Res_png_9patch* chunk = reinterpret_cast<const Res_png_9patch*>(array);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 int8_t wasDeserialized = chunk->wasDeserialized;
Romain Guye3b0a012013-06-26 15:45:41 -070063 env->ReleaseByteArrayElements(obj, const_cast<jbyte*>(array), JNI_ABORT);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000064 return (wasDeserialized != -1) ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 }
Ashok Bhat36bef0b2014-01-20 20:08:01 +000066 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 }
68
John Reck7c103a32015-04-15 15:52:10 -070069 static jlong validateNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) {
Romain Guye3b0a012013-06-26 15:45:41 -070070 size_t chunkSize = env->GetArrayLength(obj);
71 if (chunkSize < (int) (sizeof(Res_png_9patch))) {
Elliott Hughes69a017b2011-04-08 14:10:28 -070072 jniThrowRuntimeException(env, "Array too small for chunk.");
Romain Guye3b0a012013-06-26 15:45:41 -070073 return NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 }
75
Romain Guye3b0a012013-06-26 15:45:41 -070076 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 Bhat36bef0b2014-01-20 20:08:01 +000080 return reinterpret_cast<jlong>(Res_png_9patch::deserialize(storage));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 }
82
Ashok Bhat36bef0b2014-01-20 20:08:01 +000083 static void finalize(JNIEnv* env, jobject, jlong patchHandle) {
84 int8_t* patch = reinterpret_cast<int8_t*>(patchHandle);
Derek Sollenberger72903272018-09-20 14:56:27 -040085 delete[] patch;
Romain Guye3b0a012013-06-26 15:45:41 -070086 }
Elliott Hughes69a017b2011-04-08 14:10:28 -070087
Leon Scroggins III71fae622019-03-26 16:28:41 -040088 static jlong getTransparentRegion(JNIEnv* env, jobject, jlong bitmapPtr,
Matt Sarett7de73852016-10-25 18:36:39 -040089 jlong chunkHandle, jobject dstRect) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000090 Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
Romain Guye3b0a012013-06-26 15:45:41 -070091 SkASSERT(chunk);
Elliott Hughes69a017b2011-04-08 14:10:28 -070092
John Reck7c103a32015-04-15 15:52:10 -070093 SkBitmap bitmap;
Leon Scroggins III71fae622019-03-26 16:28:41 -040094 bitmap::toBitmap(bitmapPtr).getSkBitmap(&bitmap);
Matt Sarett7de73852016-10-25 18:36:39 -040095 SkRect dst;
96 GraphicsJNI::jrect_to_rect(env, dstRect, &dst);
Romain Guye3b0a012013-06-26 15:45:41 -070097
Matt Sarett7de73852016-10-25 18:36:39 -040098 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 Ilieve12d7312017-12-04 14:48:27 -0500102 lattice.fRectTypes = nullptr;
103 lattice.fColors = nullptr;
Matt Sarett7de73852016-10-25 18:36:39 -0400104
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 Guye3b0a012013-06-26 15:45:41 -0700123
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000124 return reinterpret_cast<jlong>(region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 }
126
127};
128
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400129jobject 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
142void 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 Project9066cfe2009-03-03 19:31:44 -0800153/////////////////////////////////////////////////////////////////////////////////////////
154
Daniel Micay76f6a862015-09-19 17:31:01 -0400155static const JNINativeMethod gNinePatchMethods[] = {
John Reck7c103a32015-04-15 15:52:10 -0700156 { "isNinePatchChunk", "([B)Z", (void*) SkNinePatchGlue::isNinePatchChunk },
157 { "validateNinePatchChunk", "([B)J",
158 (void*) SkNinePatchGlue::validateNinePatchChunk },
159 { "nativeFinalize", "(J)V", (void*) SkNinePatchGlue::finalize },
Leon Scroggins III71fae622019-03-26 16:28:41 -0400160 { "nativeGetTransparentRegion", "(JJLandroid/graphics/Rect;)J",
John Reck7c103a32015-04-15 15:52:10 -0700161 (void*) SkNinePatchGlue::getTransparentRegion }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162};
163
Romain Guye3b0a012013-06-26 15:45:41 -0700164int register_android_graphics_NinePatch(JNIEnv* env) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400165 gInsetStruct_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
166 "android/graphics/NinePatch$InsetStruct"));
167 gInsetStruct_constructorMethodID = GetMethodIDOrDie(env, gInsetStruct_class, "<init>",
168 "(IIIIIIIIFIF)V");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800169 return android::RegisterMethodsOrDie(env,
170 "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171}