1/*
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
21#include <androidfw/ResourceTypes.h>
22#include <hwui/Canvas.h>
23#include <hwui/Paint.h>
24#include <utils/Log.h>
25
26#include <ResourceCache.h>
27
28#include "SkCanvas.h"
29#include "SkLatticeIter.h"
30#include "SkRegion.h"
31#include "GraphicsJNI.h"
32#include "NinePatchUtils.h"
33
34#include "JNIHelp.h"
35#include "core_jni_helpers.h"
36
37using namespace android;
38
39/**
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
47class SkNinePatchGlue {
48public:
49    static jboolean isNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) {
50        if (NULL == obj) {
51            return JNI_FALSE;
52        }
53        if (env->GetArrayLength(obj) < (int)sizeof(Res_png_9patch)) {
54            return JNI_FALSE;
55        }
56        const jbyte* array = env->GetByteArrayElements(obj, 0);
57        if (array != NULL) {
58            const Res_png_9patch* chunk = reinterpret_cast<const Res_png_9patch*>(array);
59            int8_t wasDeserialized = chunk->wasDeserialized;
60            env->ReleaseByteArrayElements(obj, const_cast<jbyte*>(array), JNI_ABORT);
61            return (wasDeserialized != -1) ? JNI_TRUE : JNI_FALSE;
62        }
63        return JNI_FALSE;
64    }
65
66    static jlong validateNinePatchChunk(JNIEnv* env, jobject, jbyteArray obj) {
67        size_t chunkSize = env->GetArrayLength(obj);
68        if (chunkSize < (int) (sizeof(Res_png_9patch))) {
69            jniThrowRuntimeException(env, "Array too small for chunk.");
70            return NULL;
71        }
72
73        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
77        return reinterpret_cast<jlong>(Res_png_9patch::deserialize(storage));
78    }
79
80    static void finalize(JNIEnv* env, jobject, jlong patchHandle) {
81        int8_t* patch = reinterpret_cast<int8_t*>(patchHandle);
82        if (android::uirenderer::ResourceCache::hasInstance()) {
83            Res_png_9patch* p = (Res_png_9patch*) patch;
84            android::uirenderer::ResourceCache::getInstance().destructor(p);
85        } else {
86            delete[] patch;
87        }
88    }
89
90    static jlong getTransparentRegion(JNIEnv* env, jobject, jobject jbitmap,
91            jlong chunkHandle, jobject dstRect) {
92        Res_png_9patch* chunk = reinterpret_cast<Res_png_9patch*>(chunkHandle);
93        SkASSERT(chunk);
94        SkASSERT(boundsRect);
95
96        SkBitmap bitmap;
97        GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
98        SkRect dst;
99        GraphicsJNI::jrect_to_rect(env, dstRect, &dst);
100
101        SkCanvas::Lattice lattice;
102        SkIRect src = SkIRect::MakeWH(bitmap.width(), bitmap.height());
103        lattice.fBounds = &src;
104        NinePatchUtils::SetLatticeDivs(&lattice, *chunk, bitmap.width(), bitmap.height());
105        lattice.fFlags = nullptr;
106
107        SkRegion* region = nullptr;
108        if (SkLatticeIter::Valid(bitmap.width(), bitmap.height(), lattice)) {
109            SkLatticeIter iter(lattice, dst);
110            if (iter.numRectsToDraw() == chunk->numColors) {
111                SkRect dummy;
112                SkRect iterDst;
113                int index = 0;
114                while (iter.next(&dummy, &iterDst)) {
115                    if (0 == chunk->getColors()[index++] && !iterDst.isEmpty()) {
116                        if (!region) {
117                            region = new SkRegion();
118                        }
119
120                        region->op(iterDst.round(), SkRegion::kUnion_Op);
121                    }
122                }
123            }
124        }
125
126        return reinterpret_cast<jlong>(region);
127    }
128
129};
130
131/////////////////////////////////////////////////////////////////////////////////////////
132
133static const JNINativeMethod gNinePatchMethods[] = {
134    { "isNinePatchChunk", "([B)Z", (void*) SkNinePatchGlue::isNinePatchChunk },
135    { "validateNinePatchChunk", "([B)J",
136            (void*) SkNinePatchGlue::validateNinePatchChunk },
137    { "nativeFinalize", "(J)V", (void*) SkNinePatchGlue::finalize },
138    { "nativeGetTransparentRegion", "(Landroid/graphics/Bitmap;JLandroid/graphics/Rect;)J",
139            (void*) SkNinePatchGlue::getTransparentRegion }
140};
141
142int register_android_graphics_NinePatch(JNIEnv* env) {
143    return android::RegisterMethodsOrDie(env,
144            "android/graphics/NinePatch", gNinePatchMethods, NELEM(gNinePatchMethods));
145}
146