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