GraphicsJNI.h revision 42a1d08df7d417fd4e67eabc91ff05ee77fd9995
1#ifndef GraphicsJNI_DEFINED
2#define GraphicsJNI_DEFINED
3
4#include "SkBitmap.h"
5#include "SkDevice.h"
6#include "SkPixelRef.h"
7#include "SkMallocPixelRef.h"
8#include "SkPoint.h"
9#include "SkRect.h"
10#include "SkImageDecoder.h"
11#include <jni.h>
12
13class SkBitmapRegionDecoder;
14class SkCanvas;
15class SkPaint;
16
17namespace android {
18class TypefaceImpl;
19}
20
21class GraphicsJNI {
22public:
23    enum BitmapCreateFlags {
24        kBitmapCreateFlag_None = 0x0,
25        kBitmapCreateFlag_Mutable = 0x1,
26        kBitmapCreateFlag_Premultiplied = 0x2,
27    };
28
29    // returns true if an exception is set (and dumps it out to the Log)
30    static bool hasException(JNIEnv*);
31
32    static void get_jrect(JNIEnv*, jobject jrect, int* L, int* T, int* R, int* B);
33    static void set_jrect(JNIEnv*, jobject jrect, int L, int T, int R, int B);
34
35    static SkIRect* jrect_to_irect(JNIEnv*, jobject jrect, SkIRect*);
36    static void irect_to_jrect(const SkIRect&, JNIEnv*, jobject jrect);
37
38    static SkRect* jrectf_to_rect(JNIEnv*, jobject jrectf, SkRect*);
39    static SkRect* jrect_to_rect(JNIEnv*, jobject jrect, SkRect*);
40    static void rect_to_jrectf(const SkRect&, JNIEnv*, jobject jrectf);
41
42    static void set_jpoint(JNIEnv*, jobject jrect, int x, int y);
43
44    static SkIPoint* jpoint_to_ipoint(JNIEnv*, jobject jpoint, SkIPoint* point);
45    static void ipoint_to_jpoint(const SkIPoint& point, JNIEnv*, jobject jpoint);
46
47    static SkPoint* jpointf_to_point(JNIEnv*, jobject jpointf, SkPoint* point);
48    static void point_to_jpointf(const SkPoint& point, JNIEnv*, jobject jpointf);
49
50    static SkCanvas* getNativeCanvas(jlong nativeHandle);
51    static SkCanvas* getNativeCanvas(JNIEnv*, jobject canvas);
52    static SkPaint*  getNativePaint(JNIEnv*, jobject paint);
53    static android::TypefaceImpl* getNativeTypeface(JNIEnv*, jobject paint);
54    static SkBitmap* getNativeBitmap(JNIEnv*, jobject bitmap);
55    static SkRegion* getNativeRegion(JNIEnv*, jobject region);
56
57    // Given the 'native' long held by the Rasterizer.java object, return a
58    // ref to its SkRasterizer* (or NULL).
59    static SkRasterizer* refNativeRasterizer(jlong rasterizerHandle);
60
61    /** Return the corresponding native colorType from the java Config enum,
62        or kUnknown_SkColorType if the java object is null.
63    */
64    static SkColorType getNativeBitmapColorType(JNIEnv*, jobject jconfig);
65
66    /** Create a java Bitmap object given the native bitmap (required) and optional
67        storage array (may be null).
68        bitmap's SkAlphaType must already be in sync with bitmapCreateFlags.
69    */
70    static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer,
71            int bitmapCreateFlags, jbyteArray ninepatch, jintArray layoutbounds, int density = -1);
72
73    static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, int bitmapCreateFlags,
74            jbyteArray ninepatch, int density = -1);
75
76    /** Reinitialize a bitmap. bitmap must already have its SkAlphaType set in
77        sync with isPremultiplied
78    */
79    static void reinitBitmap(JNIEnv* env, jobject javaBitmap, SkBitmap* bitmap,
80            bool isPremultiplied);
81
82    static int getBitmapAllocationByteCount(JNIEnv* env, jobject javaBitmap);
83
84    static jobject createRegion(JNIEnv* env, SkRegion* region);
85
86    static jobject createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap);
87
88    static jbyteArray allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap,
89            SkColorTable* ctable);
90
91    /** Copy the colors in colors[] to the bitmap, convert to the correct
92        format along the way.
93        Whether to use premultiplied pixels is determined by dstBitmap's alphaType.
94    */
95    static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset,
96            int srcStride, int x, int y, int width, int height,
97            const SkBitmap& dstBitmap);
98
99    static jbyteArray getBitmapStorageObj(SkPixelRef *pixref);
100};
101
102class AndroidPixelRef : public SkMallocPixelRef {
103public:
104    AndroidPixelRef(JNIEnv* env, const SkImageInfo& info, void* storage, size_t rowBytes,
105            jbyteArray storageObj, SkColorTable* ctable);
106
107    /**
108     * Creates an AndroidPixelRef that wraps (and refs) another to reuse/share
109     * the same storage and java byte array refcounting, yet have a different
110     * color table.
111     */
112    AndroidPixelRef(AndroidPixelRef& wrappedPixelRef, const SkImageInfo& info,
113            size_t rowBytes, SkColorTable* ctable);
114
115    virtual ~AndroidPixelRef();
116
117    jbyteArray getStorageObj();
118
119    void setLocalJNIRef(jbyteArray arr);
120
121    /** Used to hold a ref to the pixels when the Java bitmap may be collected.
122     *  If specified, 'localref' is a valid JNI local reference to the byte array
123     *  containing the pixel data.
124     *
125     *  'localref' may only be NULL if setLocalJNIRef() was already called with
126     *  a JNI local ref that is still valid.
127     */
128    virtual void globalRef(void* localref=NULL);
129
130    /** Release a ref that was acquired using globalRef(). */
131    virtual void globalUnref();
132
133private:
134    AndroidPixelRef* const fWrappedPixelRef; // if set, delegate memory management calls to this
135
136    JavaVM* fVM;
137    bool fOnJavaHeap; // If true, the memory was allocated on the Java heap
138
139    jbyteArray fStorageObj; // The Java byte[] object used as the bitmap backing store
140    bool fHasGlobalRef; // If true, fStorageObj holds a JNI global ref
141
142    mutable int32_t fGlobalRefCnt;
143};
144
145/** A helper class for accessing Java-heap-allocated bitmaps.
146 *  This should be used when calling into a JNI method that retains a
147 *  reference to the bitmap longer than the lifetime of the Java Bitmap.
148 *
149 *  After creating an instance of this class, a call to
150 *  AndroidPixelRef::globalRef() will allocate a JNI global reference
151 *  to the backing buffer object.
152 */
153class JavaHeapBitmapRef {
154public:
155
156    JavaHeapBitmapRef(JNIEnv *env, SkBitmap* nativeBitmap, jbyteArray buffer);
157    ~JavaHeapBitmapRef();
158
159private:
160    JNIEnv* fEnv;
161    SkBitmap* fNativeBitmap;
162    jbyteArray fBuffer;
163};
164
165/** Allocator which allocates the backing buffer in the Java heap.
166 *  Instances can only be used to perform a single allocation, which helps
167 *  ensure that the allocated buffer is properly accounted for with a
168 *  reference in the heap (or a JNI global reference).
169 */
170class JavaPixelAllocator : public SkBitmap::Allocator {
171public:
172    JavaPixelAllocator(JNIEnv* env);
173    // overrides
174    virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable);
175
176    /** Return the Java array object created for the last allocation.
177     *  This returns a local JNI reference which the caller is responsible
178     *  for storing appropriately (usually by passing it to the Bitmap
179     *  constructor).
180     */
181    jbyteArray getStorageObj() { return fStorageObj; }
182
183    /** Same as getStorageObj(), but also resets the allocator so that it
184     *  can allocate again.
185     */
186    jbyteArray getStorageObjAndReset() {
187        jbyteArray result = fStorageObj;
188        fStorageObj = NULL;
189        fAllocCount = 0;
190        return result;
191    };
192
193private:
194    JavaVM* fVM;
195    bool fAllocateInJavaHeap;
196    jbyteArray fStorageObj;
197    int fAllocCount;
198};
199
200enum JNIAccess {
201    kRO_JNIAccess,
202    kRW_JNIAccess
203};
204
205class AutoJavaFloatArray {
206public:
207    AutoJavaFloatArray(JNIEnv* env, jfloatArray array,
208                       int minLength = 0, JNIAccess = kRW_JNIAccess);
209    ~AutoJavaFloatArray();
210
211    float* ptr() const { return fPtr; }
212    int    length() const { return fLen; }
213
214private:
215    JNIEnv*     fEnv;
216    jfloatArray fArray;
217    float*      fPtr;
218    int         fLen;
219    int         fReleaseMode;
220};
221
222class AutoJavaIntArray {
223public:
224    AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0);
225    ~AutoJavaIntArray();
226
227    jint* ptr() const { return fPtr; }
228    int    length() const { return fLen; }
229
230private:
231    JNIEnv*     fEnv;
232    jintArray fArray;
233    jint*      fPtr;
234    int         fLen;
235};
236
237class AutoJavaShortArray {
238public:
239    AutoJavaShortArray(JNIEnv* env, jshortArray array,
240                       int minLength = 0, JNIAccess = kRW_JNIAccess);
241    ~AutoJavaShortArray();
242
243    jshort* ptr() const { return fPtr; }
244    int    length() const { return fLen; }
245
246private:
247    JNIEnv*     fEnv;
248    jshortArray fArray;
249    jshort*      fPtr;
250    int         fLen;
251    int         fReleaseMode;
252};
253
254class AutoJavaByteArray {
255public:
256    AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0);
257    ~AutoJavaByteArray();
258
259    jbyte* ptr() const { return fPtr; }
260    int    length() const { return fLen; }
261
262private:
263    JNIEnv*     fEnv;
264    jbyteArray fArray;
265    jbyte*      fPtr;
266    int         fLen;
267};
268
269void doThrowNPE(JNIEnv* env);
270void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception
271void doThrowIAE(JNIEnv* env, const char* msg = NULL);   // Illegal Argument
272void doThrowRE(JNIEnv* env, const char* msg = NULL);   // Runtime
273void doThrowISE(JNIEnv* env, const char* msg = NULL);   // Illegal State
274void doThrowOOME(JNIEnv* env, const char* msg = NULL);   // Out of memory
275void doThrowIOE(JNIEnv* env, const char* msg = NULL);   // IO Exception
276
277#define NPE_CHECK_RETURN_ZERO(env, object)    \
278    do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0)
279
280#define NPE_CHECK_RETURN_VOID(env, object)    \
281    do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0)
282
283#endif
284