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