blob: e69f64e05bdb12c1cb5ea0cfbe4dd5d8662cdaf8 [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
John Reck773bbe02015-08-17 15:18:29 -070035extern void NinePatch_Draw(SkCanvas* canvas, const SkRect& bounds, const SkBitmap& bitmap,
36 const android::Res_png_9patch& chunk, const SkPaint* paint, SkRegion** outRegion);
37
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 Reck7c103a32015-04-15 15:52:10 -070067 static jlong validateNinePatchChunk(JNIEnv* env, jobject, 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 Reck773bbe02015-08-17 15:18:29 -070091 static void draw(JNIEnv* env, SkCanvas* canvas, SkRect& bounds, const SkBitmap& bitmap,
92 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));
97 NinePatch_Draw(canvas, bounds, bitmap, *chunk, paint, NULL);
98 } else {
99 canvas->save();
100
101 SkScalar scale = destDensity / (float)srcDensity;
102 canvas->translate(bounds.fLeft, bounds.fTop);
103 canvas->scale(scale, scale);
104
105 bounds.fRight = (bounds.fRight-bounds.fLeft) / scale;
106 bounds.fBottom = (bounds.fBottom-bounds.fTop) / scale;
107 bounds.fLeft = bounds.fTop = 0;
108
109 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);
113
114 NinePatch_Draw(canvas, bounds, bitmap, *chunk, paint, NULL);
115
116 canvas->restore();
117 }
118 }
119
120 static void drawF(JNIEnv* env, jobject, jlong canvasHandle, jobject boundsRectF,
121 jobject jbitmap, jlong chunkHandle, jlong paintHandle,
122 jint destDensity, jint srcDensity) {
123 SkCanvas* canvas = reinterpret_cast<Canvas*>(canvasHandle)->asSkCanvas();
124 Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
125 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
126 SkASSERT(canvas);
127 SkASSERT(boundsRectF);
128 SkASSERT(chunk);
129 // paint is optional
130
131 SkBitmap bitmap;
132 GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
133 SkRect bounds;
134 GraphicsJNI::jrectf_to_rect(env, boundsRectF, &bounds);
135
136 draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity);
137 }
138
139 static void drawI(JNIEnv* env, jobject, jlong canvasHandle, jobject boundsRect,
140 jobject jbitmap, jlong chunkHandle, jlong paintHandle,
141 jint destDensity, jint srcDensity) {
142 SkCanvas* canvas = reinterpret_cast<Canvas*>(canvasHandle)->asSkCanvas();
143 Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
144 const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
145 SkASSERT(canvas);
146 SkASSERT(boundsRect);
147 SkASSERT(chunk);
148 // paint is optional
149
150 SkBitmap bitmap;
151 GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
152 SkRect bounds;
153 GraphicsJNI::jrect_to_rect(env, boundsRect, &bounds);
154 draw(env, canvas, bounds, bitmap, chunk, paint, destDensity, srcDensity);
155 }
156
John Reck7c103a32015-04-15 15:52:10 -0700157 static jlong getTransparentRegion(JNIEnv* env, jobject, jobject jbitmap,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000158 jlong chunkHandle, jobject boundsRect) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000159 Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
Romain Guye3b0a012013-06-26 15:45:41 -0700160 SkASSERT(chunk);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 SkASSERT(boundsRect);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700162
John Reck7c103a32015-04-15 15:52:10 -0700163 SkBitmap bitmap;
164 GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
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 Reck773bbe02015-08-17 15:18:29 -0700169 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 Reck7c103a32015-04-15 15:52:10 -0700179 { "isNinePatchChunk", "([B)Z", (void*) SkNinePatchGlue::isNinePatchChunk },
180 { "validateNinePatchChunk", "([B)J",
181 (void*) SkNinePatchGlue::validateNinePatchChunk },
182 { "nativeFinalize", "(J)V", (void*) SkNinePatchGlue::finalize },
John Reck773bbe02015-08-17 15:18:29 -0700183 { "nativeDraw", "(JLandroid/graphics/RectF;Landroid/graphics/Bitmap;JJII)V",
184 (void*) SkNinePatchGlue::drawF },
185 { "nativeDraw", "(JLandroid/graphics/Rect;Landroid/graphics/Bitmap;JJII)V",
186 (void*) SkNinePatchGlue::drawI },
John Reck7c103a32015-04-15 15:52:10 -0700187 { "nativeGetTransparentRegion", "(Landroid/graphics/Bitmap;JLandroid/graphics/Rect;)J",
188 (void*) SkNinePatchGlue::getTransparentRegion }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189};
190
Romain Guye3b0a012013-06-26 15:45:41 -0700191int register_android_graphics_NinePatch(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800192 return android::RegisterMethodsOrDie(env,
193 "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194}