blob: 3f8bfe29101395b191308c0d74e5a5082eb1c194 [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>
Dianne Hackborn8cae1242009-09-10 14:32:16 -070022#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
John Recka35778c72014-11-06 09:45:10 -080024#include <ResourceCache.h>
Romain Guye3b0a012013-06-26 15:45:41 -070025
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040026#include "Paint.h"
Derek Sollenberger8872b382014-06-23 14:13:53 -040027#include "Canvas.h"
Dianne Hackborn11ea3342009-07-22 21:48:55 -070028#include "SkCanvas.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include "SkRegion.h"
30#include "GraphicsJNI.h"
31
32#include "JNIHelp.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080033#include "core_jni_helpers.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
Romain Guye3b0a012013-06-26 15:45:41 -070035extern void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, const SkBitmap& bitmap,
36 const android::Res_png_9patch& chunk, const SkPaint* paint, SkRegion** outRegion);
Elliott Hughes69a017b2011-04-08 14:10:28 -070037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038using namespace android;
39
Romain Guye3b0a012013-06-26 15:45:41 -070040/**
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 Project9066cfe2009-03-03 19:31:44 -080048class SkNinePatchGlue {
49public:
Romain Guye3b0a012013-06-26 15:45:41 -070050 static jboolean isNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 if (NULL == obj) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000052 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 }
54 if (env->GetArrayLength(obj) < (int)sizeof(Res_png_9patch)) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +000055 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 }
57 const jbyte* array = env->GetByteArrayElements(obj, 0);
58 if (array != NULL) {
Romain Guye3b0a012013-06-26 15:45:41 -070059 const Res_png_9patch* chunk = reinterpret_cast<const Res_png_9patch*>(array);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 int8_t wasDeserialized = chunk->wasDeserialized;
Romain Guye3b0a012013-06-26 15:45:41 -070061 env->ReleaseByteArrayElements(obj, const_cast<jbyte*>(array), JNI_ABORT);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000062 return (wasDeserialized != -1) ? JNI_TRUE : JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 }
Ashok Bhat36bef0b2014-01-20 20:08:01 +000064 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 }
66
John Reck1ff961d2015-04-17 20:45:15 +000067 static jlong validateNinePatchChunk(JNIEnv* env, jobject, jlong, jbyteArray obj) {
Romain Guye3b0a012013-06-26 15:45:41 -070068 size_t chunkSize = env->GetArrayLength(obj);
69 if (chunkSize < (int) (sizeof(Res_png_9patch))) {
Elliott Hughes69a017b2011-04-08 14:10:28 -070070 jniThrowRuntimeException(env, "Array too small for chunk.");
Romain Guye3b0a012013-06-26 15:45:41 -070071 return NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 }
73
Romain Guye3b0a012013-06-26 15:45:41 -070074 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 Bhat36bef0b2014-01-20 20:08:01 +000078 return reinterpret_cast<jlong>(Res_png_9patch::deserialize(storage));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 }
80
Ashok Bhat36bef0b2014-01-20 20:08:01 +000081 static void finalize(JNIEnv* env, jobject, jlong patchHandle) {
82 int8_t* patch = reinterpret_cast<int8_t*>(patchHandle);
John Recka35778c72014-11-06 09:45:10 -080083 if (android::uirenderer::ResourceCache::hasInstance()) {
Romain Guye3b0a012013-06-26 15:45:41 -070084 Res_png_9patch* p = (Res_png_9patch*) patch;
John Recka35778c72014-11-06 09:45:10 -080085 android::uirenderer::ResourceCache::getInstance().destructor(p);
John Recka2732a22014-12-18 13:52:33 -080086 } else {
87 delete[] patch;
Romain Guye3b0a012013-06-26 15:45:41 -070088 }
Romain Guye3b0a012013-06-26 15:45:41 -070089 }
Elliott Hughes69a017b2011-04-08 14:10:28 -070090
John Reck1ff961d2015-04-17 20:45:15 +000091 static void draw(JNIEnv* env, SkCanvas* canvas, SkRect& bounds, const SkBitmap* bitmap,
Romain Guye3b0a012013-06-26 15:45:41 -070092 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 Reck1ff961d2015-04-17 20:45:15 +000097 NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL);
Romain Guye3b0a012013-06-26 15:45:41 -070098 } else {
99 canvas->save();
Elliott Hughes69a017b2011-04-08 14:10:28 -0700100
Leon Scroggins III2e0103e2014-04-04 17:05:24 -0400101 SkScalar scale = destDensity / (float)srcDensity;
Romain Guye3b0a012013-06-26 15:45:41 -0700102 canvas->translate(bounds.fLeft, bounds.fTop);
103 canvas->scale(scale, scale);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700104
Romain Guye3b0a012013-06-26 15:45:41 -0700105 bounds.fRight = SkScalarDiv(bounds.fRight-bounds.fLeft, scale);
106 bounds.fBottom = SkScalarDiv(bounds.fBottom-bounds.fTop, scale);
107 bounds.fLeft = bounds.fTop = 0;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700108
Romain Guye3b0a012013-06-26 15:45:41 -0700109 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 Hughes69a017b2011-04-08 14:10:28 -0700113
John Reck1ff961d2015-04-17 20:45:15 +0000114 NinePatch_Draw(canvas, bounds, *bitmap, *chunk, paint, NULL);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700115
Romain Guye3b0a012013-06-26 15:45:41 -0700116 canvas->restore();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000120 static void drawF(JNIEnv* env, jobject, jlong canvasHandle, jobject boundsRectF,
John Reck1ff961d2015-04-17 20:45:15 +0000121 jlong bitmapHandle, jlong chunkHandle, jlong paintHandle,
Romain Guye3b0a012013-06-26 15:45:41 -0700122 jint destDensity, jint srcDensity) {
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500123 SkCanvas* canvas = reinterpret_cast<Canvas*>(canvasHandle)->asSkCanvas();
John Reck1ff961d2015-04-17 20:45:15 +0000124 const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000125 Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400126 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 SkASSERT(canvas);
128 SkASSERT(boundsRectF);
John Reck1ff961d2015-04-17 20:45:15 +0000129 SkASSERT(bitmap);
Romain Guye3b0a012013-06-26 15:45:41 -0700130 SkASSERT(chunk);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 // paint is optional
132
Romain Guye3b0a012013-06-26 15:45:41 -0700133 SkRect bounds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 GraphicsJNI::jrectf_to_rect(env, boundsRectF, &bounds);
135
Romain Guye3b0a012013-06-26 15:45:41 -0700136 draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700138
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000139 static void drawI(JNIEnv* env, jobject, jlong canvasHandle, jobject boundsRect,
John Reck1ff961d2015-04-17 20:45:15 +0000140 jlong bitmapHandle, jlong chunkHandle, jlong paintHandle,
Romain Guye3b0a012013-06-26 15:45:41 -0700141 jint destDensity, jint srcDensity) {
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500142 SkCanvas* canvas = reinterpret_cast<Canvas*>(canvasHandle)->asSkCanvas();
John Reck1ff961d2015-04-17 20:45:15 +0000143 const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000144 Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400145 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 SkASSERT(canvas);
147 SkASSERT(boundsRect);
John Reck1ff961d2015-04-17 20:45:15 +0000148 SkASSERT(bitmap);
Romain Guye3b0a012013-06-26 15:45:41 -0700149 SkASSERT(chunk);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 // paint is optional
151
Romain Guye3b0a012013-06-26 15:45:41 -0700152 SkRect bounds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds);
Romain Guye3b0a012013-06-26 15:45:41 -0700154 draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700156
John Reck1ff961d2015-04-17 20:45:15 +0000157 static jlong getTransparentRegion(JNIEnv* env, jobject, jlong bitmapHandle,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000158 jlong chunkHandle, jobject boundsRect) {
John Reck1ff961d2015-04-17 20:45:15 +0000159 const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000160 Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
John Reck1ff961d2015-04-17 20:45:15 +0000161 SkASSERT(bitmap);
Romain Guye3b0a012013-06-26 15:45:41 -0700162 SkASSERT(chunk);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 SkASSERT(boundsRect);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700164
Romain Guye3b0a012013-06-26 15:45:41 -0700165 SkRect bounds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds);
Romain Guye3b0a012013-06-26 15:45:41 -0700167
168 SkRegion* region = NULL;
John Reck1ff961d2015-04-17 20:45:15 +0000169 NinePatch_Draw(NULL, bounds, *bitmap, *chunk, NULL, &region);
Romain Guye3b0a012013-06-26 15:45:41 -0700170
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000171 return reinterpret_cast<jlong>(region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 }
173
174};
175
176/////////////////////////////////////////////////////////////////////////////////////////
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178static JNINativeMethod gNinePatchMethods[] = {
John Reck1ff961d2015-04-17 20:45:15 +0000179 { "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 Project9066cfe2009-03-03 19:31:44 -0800186};
187
Romain Guye3b0a012013-06-26 15:45:41 -0700188int register_android_graphics_NinePatch(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800189 return android::RegisterMethodsOrDie(env,
190 "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191}