blob: 6942017d5f275ea76124f5652e91eba1e7a6403c [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
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -040034jclass gInsetStruct_class;
35jmethodID gInsetStruct_constructorMethodID;
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037using namespace android;
38
Romain Guye3b0a012013-06-26 15:45:41 -070039/**
40 * IMPORTANT NOTE: 9patch chunks can be manipuated either as an array of bytes
41 * or as a Res_png_9patch instance. It is important to note that the size of the
42 * array required to hold a 9patch chunk is greater than sizeof(Res_png_9patch).
43 * The code below manipulates chunks as Res_png_9patch* types to draw and as
44 * int8_t* to allocate and free the backing storage.
45 */
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047class SkNinePatchGlue {
48public:
Romain Guye3b0a012013-06-26 15:45:41 -070049 static jboolean isNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 if (NULL == obj) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000051 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 }
53 if (env->GetArrayLength(obj) < (int)sizeof(Res_png_9patch)) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000054 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 }
56 const jbyte* array = env->GetByteArrayElements(obj, 0);
57 if (array != NULL) {
Romain Guye3b0a012013-06-26 15:45:41 -070058 const Res_png_9patch* chunk = reinterpret_cast<const Res_png_9patch*>(array);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 int8_t wasDeserialized = chunk->wasDeserialized;
Romain Guye3b0a012013-06-26 15:45:41 -070060 env->ReleaseByteArrayElements(obj, const_cast<jbyte*>(array), JNI_ABORT);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000061 return (wasDeserialized != -1) ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 }
Ashok Bhat36bef0b2014-01-20 20:08:01 +000063 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 }
65
John Reck7c103a32015-04-15 15:52:10 -070066 static jlong validateNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) {
Romain Guye3b0a012013-06-26 15:45:41 -070067 size_t chunkSize = env->GetArrayLength(obj);
68 if (chunkSize < (int) (sizeof(Res_png_9patch))) {
Elliott Hughes69a017b2011-04-08 14:10:28 -070069 jniThrowRuntimeException(env, "Array too small for chunk.");
Romain Guye3b0a012013-06-26 15:45:41 -070070 return NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 }
72
Romain Guye3b0a012013-06-26 15:45:41 -070073 int8_t* storage = new int8_t[chunkSize];
74 // This call copies the content of the jbyteArray
75 env->GetByteArrayRegion(obj, 0, chunkSize, reinterpret_cast<jbyte*>(storage));
76 // Deserialize in place, return the array we just allocated
Ashok Bhat36bef0b2014-01-20 20:08:01 +000077 return reinterpret_cast<jlong>(Res_png_9patch::deserialize(storage));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 }
79
Ashok Bhat36bef0b2014-01-20 20:08:01 +000080 static void finalize(JNIEnv* env, jobject, jlong patchHandle) {
81 int8_t* patch = reinterpret_cast<int8_t*>(patchHandle);
Derek Sollenberger72903272018-09-20 14:56:27 -040082 delete[] patch;
Romain Guye3b0a012013-06-26 15:45:41 -070083 }
Elliott Hughes69a017b2011-04-08 14:10:28 -070084
Leon Scroggins III71fae622019-03-26 16:28:41 -040085 static jlong getTransparentRegion(JNIEnv* env, jobject, jlong bitmapPtr,
Matt Sarett7de73852016-10-25 18:36:39 -040086 jlong chunkHandle, jobject dstRect) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000087 Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
Romain Guye3b0a012013-06-26 15:45:41 -070088 SkASSERT(chunk);
Elliott Hughes69a017b2011-04-08 14:10:28 -070089
John Reck7c103a32015-04-15 15:52:10 -070090 SkBitmap bitmap;
Leon Scroggins III71fae622019-03-26 16:28:41 -040091 bitmap::toBitmap(bitmapPtr).getSkBitmap(&bitmap);
Matt Sarett7de73852016-10-25 18:36:39 -040092 SkRect dst;
93 GraphicsJNI::jrect_to_rect(env, dstRect, &dst);
Romain Guye3b0a012013-06-26 15:45:41 -070094
Matt Sarett7de73852016-10-25 18:36:39 -040095 SkCanvas::Lattice lattice;
96 SkIRect src = SkIRect::MakeWH(bitmap.width(), bitmap.height());
97 lattice.fBounds = &src;
98 NinePatchUtils::SetLatticeDivs(&lattice, *chunk, bitmap.width(), bitmap.height());
Stan Ilieve12d7312017-12-04 14:48:27 -050099 lattice.fRectTypes = nullptr;
100 lattice.fColors = nullptr;
Matt Sarett7de73852016-10-25 18:36:39 -0400101
102 SkRegion* region = nullptr;
103 if (SkLatticeIter::Valid(bitmap.width(), bitmap.height(), lattice)) {
104 SkLatticeIter iter(lattice, dst);
105 if (iter.numRectsToDraw() == chunk->numColors) {
106 SkRect dummy;
107 SkRect iterDst;
108 int index = 0;
109 while (iter.next(&dummy, &iterDst)) {
110 if (0 == chunk->getColors()[index++] && !iterDst.isEmpty()) {
111 if (!region) {
112 region = new SkRegion();
113 }
114
115 region->op(iterDst.round(), SkRegion::kUnion_Op);
116 }
117 }
118 }
119 }
Romain Guye3b0a012013-06-26 15:45:41 -0700120
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000121 return reinterpret_cast<jlong>(region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 }
123
124};
125
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400126jobject NinePatchPeeker::createNinePatchInsets(JNIEnv* env, float scale) const {
127 if (!mHasInsets) {
128 return nullptr;
129 }
130
131 return env->NewObject(gInsetStruct_class, gInsetStruct_constructorMethodID,
132 mOpticalInsets[0], mOpticalInsets[1],
133 mOpticalInsets[2], mOpticalInsets[3],
134 mOutlineInsets[0], mOutlineInsets[1],
135 mOutlineInsets[2], mOutlineInsets[3],
136 mOutlineRadius, mOutlineAlpha, scale);
137}
138
139void NinePatchPeeker::getPadding(JNIEnv* env, jobject outPadding) const {
140 if (mPatch) {
141 GraphicsJNI::set_jrect(env, outPadding,
142 mPatch->paddingLeft, mPatch->paddingTop,
143 mPatch->paddingRight, mPatch->paddingBottom);
144
145 } else {
146 GraphicsJNI::set_jrect(env, outPadding, -1, -1, -1, -1);
147 }
148}
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150/////////////////////////////////////////////////////////////////////////////////////////
151
Daniel Micay76f6a862015-09-19 17:31:01 -0400152static const JNINativeMethod gNinePatchMethods[] = {
John Reck7c103a32015-04-15 15:52:10 -0700153 { "isNinePatchChunk", "([B)Z", (void*) SkNinePatchGlue::isNinePatchChunk },
154 { "validateNinePatchChunk", "([B)J",
155 (void*) SkNinePatchGlue::validateNinePatchChunk },
156 { "nativeFinalize", "(J)V", (void*) SkNinePatchGlue::finalize },
Leon Scroggins III71fae622019-03-26 16:28:41 -0400157 { "nativeGetTransparentRegion", "(JJLandroid/graphics/Rect;)J",
John Reck7c103a32015-04-15 15:52:10 -0700158 (void*) SkNinePatchGlue::getTransparentRegion }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159};
160
Romain Guye3b0a012013-06-26 15:45:41 -0700161int register_android_graphics_NinePatch(JNIEnv* env) {
Leon Scroggins III0c01dbf2017-10-20 14:08:11 -0400162 gInsetStruct_class = MakeGlobalRefOrDie(env, FindClassOrDie(env,
163 "android/graphics/NinePatch$InsetStruct"));
164 gInsetStruct_constructorMethodID = GetMethodIDOrDie(env, gInsetStruct_class, "<init>",
165 "(IIIIIIIIFIF)V");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800166 return android::RegisterMethodsOrDie(env,
167 "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168}