blob: 15f951688d43c6ee391e70c95ded56a1ab5d5c2b [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>
sergeyvdccca442016-03-21 15:38:21 -070022#include <hwui/Canvas.h>
23#include <hwui/Paint.h>
Dianne Hackborn8cae1242009-09-10 14:32:16 -070024#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
Dianne Hackborn11ea3342009-07-22 21:48:55 -070026#include "SkCanvas.h"
Matt Sarett7de73852016-10-25 18:36:39 -040027#include "SkLatticeIter.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include "SkRegion.h"
29#include "GraphicsJNI.h"
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040030#include "NinePatchPeeker.h"
Matt Sarett7de73852016-10-25 18:36:39 -040031#include "NinePatchUtils.h"
Derek Sollenberger4c5efe92015-07-10 13:56:39 -040032
Steven Moreland2279b252017-07-19 09:50:45 -070033#include <nativehelper/JNIHelp.h>
Andreas Gampeed6b9df2014-11-20 22:02:20 -080034#include "core_jni_helpers.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040036jclass gInsetStruct_class;
37jmethodID gInsetStruct_constructorMethodID;
38
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039using namespace android;
40
Romain Guye3b0a012013-06-26 15:45:41 -070041/**
42 * IMPORTANT NOTE: 9patch chunks can be manipuated either as an array of bytes
43 * or as a Res_png_9patch instance. It is important to note that the size of the
44 * array required to hold a 9patch chunk is greater than sizeof(Res_png_9patch).
45 * The code below manipulates chunks as Res_png_9patch* types to draw and as
46 * int8_t* to allocate and free the backing storage.
47 */
48
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049class SkNinePatchGlue {
50public:
Romain Guye3b0a012013-06-26 15:45:41 -070051 static jboolean isNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 if (NULL == obj) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000053 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 }
55 if (env->GetArrayLength(obj) < (int)sizeof(Res_png_9patch)) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000056 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 }
58 const jbyte* array = env->GetByteArrayElements(obj, 0);
59 if (array != NULL) {
Romain Guye3b0a012013-06-26 15:45:41 -070060 const Res_png_9patch* chunk = reinterpret_cast<const Res_png_9patch*>(array);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 int8_t wasDeserialized = chunk->wasDeserialized;
Romain Guye3b0a012013-06-26 15:45:41 -070062 env->ReleaseByteArrayElements(obj, const_cast<jbyte*>(array), JNI_ABORT);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000063 return (wasDeserialized != -1) ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 }
Ashok Bhat36bef0b2014-01-20 20:08:01 +000065 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 }
67
John Reck7c103a32015-04-15 15:52:10 -070068 static jlong validateNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) {
Romain Guye3b0a012013-06-26 15:45:41 -070069 size_t chunkSize = env->GetArrayLength(obj);
70 if (chunkSize < (int) (sizeof(Res_png_9patch))) {
Elliott Hughes69a017b2011-04-08 14:10:28 -070071 jniThrowRuntimeException(env, "Array too small for chunk.");
Romain Guye3b0a012013-06-26 15:45:41 -070072 return NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 }
74
Romain Guye3b0a012013-06-26 15:45:41 -070075 int8_t* storage = new int8_t[chunkSize];
76 // This call copies the content of the jbyteArray
77 env->GetByteArrayRegion(obj, 0, chunkSize, reinterpret_cast<jbyte*>(storage));
78 // Deserialize in place, return the array we just allocated
Ashok Bhat36bef0b2014-01-20 20:08:01 +000079 return reinterpret_cast<jlong>(Res_png_9patch::deserialize(storage));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 }
81
Ashok Bhat36bef0b2014-01-20 20:08:01 +000082 static void finalize(JNIEnv* env, jobject, jlong patchHandle) {
83 int8_t* patch = reinterpret_cast<int8_t*>(patchHandle);
Derek Sollenberger72903272018-09-20 14:56:27 -040084 delete[] patch;
Romain Guye3b0a012013-06-26 15:45:41 -070085 }
Elliott Hughes69a017b2011-04-08 14:10:28 -070086
Leon Scroggins III71fae622019-03-26 16:28:41 -040087 static jlong getTransparentRegion(JNIEnv* env, jobject, jlong bitmapPtr,
Matt Sarett7de73852016-10-25 18:36:39 -040088 jlong chunkHandle, jobject dstRect) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000089 Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
Romain Guye3b0a012013-06-26 15:45:41 -070090 SkASSERT(chunk);
Elliott Hughes69a017b2011-04-08 14:10:28 -070091
John Reck7c103a32015-04-15 15:52:10 -070092 SkBitmap bitmap;
Leon Scroggins III71fae622019-03-26 16:28:41 -040093 bitmap::toBitmap(bitmapPtr).getSkBitmap(&bitmap);
Matt Sarett7de73852016-10-25 18:36:39 -040094 SkRect dst;
95 GraphicsJNI::jrect_to_rect(env, dstRect, &dst);
Romain Guye3b0a012013-06-26 15:45:41 -070096
Matt Sarett7de73852016-10-25 18:36:39 -040097 SkCanvas::Lattice lattice;
98 SkIRect src = SkIRect::MakeWH(bitmap.width(), bitmap.height());
99 lattice.fBounds = &src;
100 NinePatchUtils::SetLatticeDivs(&lattice, *chunk, bitmap.width(), bitmap.height());
Stan Ilieve12d7312017-12-04 14:48:27 -0500101 lattice.fRectTypes = nullptr;
102 lattice.fColors = nullptr;
Matt Sarett7de73852016-10-25 18:36:39 -0400103
104 SkRegion* region = nullptr;
105 if (SkLatticeIter::Valid(bitmap.width(), bitmap.height(), lattice)) {
106 SkLatticeIter iter(lattice, dst);
107 if (iter.numRectsToDraw() == chunk->numColors) {
108 SkRect dummy;
109 SkRect iterDst;
110 int index = 0;
111 while (iter.next(&dummy, &iterDst)) {
112 if (0 == chunk->getColors()[index++] && !iterDst.isEmpty()) {
113 if (!region) {
114 region = new SkRegion();
115 }
116
117 region->op(iterDst.round(), SkRegion::kUnion_Op);
118 }
119 }
120 }
121 }
Romain Guye3b0a012013-06-26 15:45:41 -0700122
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000123 return reinterpret_cast<jlong>(region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 }
125
126};
127
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400128jobject NinePatchPeeker::createNinePatchInsets(JNIEnv* env, float scale) const {
129 if (!mHasInsets) {
130 return nullptr;
131 }
132
133 return env->NewObject(gInsetStruct_class, gInsetStruct_constructorMethodID,
134 mOpticalInsets[0], mOpticalInsets[1],
135 mOpticalInsets[2], mOpticalInsets[3],
136 mOutlineInsets[0], mOutlineInsets[1],
137 mOutlineInsets[2], mOutlineInsets[3],
138 mOutlineRadius, mOutlineAlpha, scale);
139}
140
141void NinePatchPeeker::getPadding(JNIEnv* env, jobject outPadding) const {
142 if (mPatch) {
143 GraphicsJNI::set_jrect(env, outPadding,
144 mPatch->paddingLeft, mPatch->paddingTop,
145 mPatch->paddingRight, mPatch->paddingBottom);
146
147 } else {
148 GraphicsJNI::set_jrect(env, outPadding, -1, -1, -1, -1);
149 }
150}
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152/////////////////////////////////////////////////////////////////////////////////////////
153
Daniel Micay76f6a862015-09-19 17:31:01 -0400154static const JNINativeMethod gNinePatchMethods[] = {
John Reck7c103a32015-04-15 15:52:10 -0700155 { "isNinePatchChunk", "([B)Z", (void*) SkNinePatchGlue::isNinePatchChunk },
156 { "validateNinePatchChunk", "([B)J",
157 (void*) SkNinePatchGlue::validateNinePatchChunk },
158 { "nativeFinalize", "(J)V", (void*) SkNinePatchGlue::finalize },
Leon Scroggins III71fae622019-03-26 16:28:41 -0400159 { "nativeGetTransparentRegion", "(JJLandroid/graphics/Rect;)J",
John Reck7c103a32015-04-15 15:52:10 -0700160 (void*) SkNinePatchGlue::getTransparentRegion }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161};
162
Romain Guye3b0a012013-06-26 15:45:41 -0700163int register_android_graphics_NinePatch(JNIEnv* env) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400164 gInsetStruct_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
165 "android/graphics/NinePatch$InsetStruct"));
166 gInsetStruct_constructorMethodID = GetMethodIDOrDie(env, gInsetStruct_class, "<init>",
167 "(IIIIIIIIFIF)V");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800168 return android::RegisterMethodsOrDie(env,
169 "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170}