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