blob: f4590b956d78a7c7e4715307464ff00ab761441f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001#ifndef GraphicsJNI_DEFINED
2#define GraphicsJNI_DEFINED
3
Patrick Dubroye4ac2d62010-12-01 11:23:13 -08004#include "SkBitmap.h"
5#include "SkDevice.h"
6#include "SkPixelRef.h"
7#include "SkMallocPixelRef.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008#include "SkPoint.h"
9#include "SkRect.h"
Derek Sollenberger5827cb52013-07-26 14:58:06 -040010#include "SkImageDecoder.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011#include <jni.h>
12
Derek Sollenberger5827cb52013-07-26 14:58:06 -040013class SkBitmapRegionDecoder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014class SkCanvas;
15class SkPaint;
16class SkPicture;
17
18class GraphicsJNI {
19public:
Chris Craik1abf5d62013-08-16 12:47:03 -070020 enum BitmapCreateFlags {
21 kBitmapCreateFlag_None = 0x0,
22 kBitmapCreateFlag_Mutable = 0x1,
23 kBitmapCreateFlag_Premultiplied = 0x2,
24 };
25
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026 // 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);
Elliott Hughes8451b252011-04-07 19:17:57 -070038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 static void set_jpoint(JNIEnv*, jobject jrect, int x, int y);
Elliott Hughes8451b252011-04-07 19:17:57 -070040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 static SkIPoint* jpoint_to_ipoint(JNIEnv*, jobject jpoint, SkIPoint* point);
42 static void ipoint_to_jpoint(const SkIPoint& point, JNIEnv*, jobject jpoint);
Elliott Hughes8451b252011-04-07 19:17:57 -070043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 static SkPoint* jpointf_to_point(JNIEnv*, jobject jpointf, SkPoint* point);
45 static void point_to_jpointf(const SkPoint& point, JNIEnv*, jobject jpointf);
Elliott Hughes8451b252011-04-07 19:17:57 -070046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 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);
Elliott Hughes8451b252011-04-07 19:17:57 -070052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 /** 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);
Elliott Hughes8451b252011-04-07 19:17:57 -070057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 /** Create a java Bitmap object given the native bitmap (required) and optional
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080059 storage array (may be null).
60 */
61 static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer,
Chris Craik1abf5d62013-08-16 12:47:03 -070062 int bitmapCreateFlags, jbyteArray ninepatch, jintArray layoutbounds, int density = -1);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080063
Chris Craik1abf5d62013-08-16 12:47:03 -070064 static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, int bitmapCreateFlags,
65 jbyteArray ninepatch, int density = -1);
Elliott Hughes8451b252011-04-07 19:17:57 -070066
Chris Craik1abf5d62013-08-16 12:47:03 -070067 static void reinitBitmap(JNIEnv* env, jobject javaBitmap, SkBitmap* bitmap,
68 bool isPremultiplied);
Chris Craik9f583612013-05-20 18:13:47 -070069
70 static int getBitmapAllocationByteCount(JNIEnv* env, jobject javaBitmap);
71
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 static jobject createRegion(JNIEnv* env, SkRegion* region);
73
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080074 static jobject createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap);
Joseph Wenf1f48bc2010-07-19 16:59:51 +080075
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080076 static jbyteArray allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap,
Chris Craik1abf5d62013-08-16 12:47:03 -070077 SkColorTable* ctable);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 /** Copy the colors in colors[] to the bitmap, convert to the correct
80 format along the way.
81 */
82 static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset,
Chris Craik1abf5d62013-08-16 12:47:03 -070083 int srcStride, int x, int y, int width, int height,
84 const SkBitmap& dstBitmap, bool isPremultiplied);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080085
86 static jbyteArray getBitmapStorageObj(SkPixelRef *pixref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087};
88
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080089class AndroidPixelRef : public SkMallocPixelRef {
90public:
91 AndroidPixelRef(JNIEnv* env, void* storage, size_t size, jbyteArray storageObj,
92 SkColorTable* ctable);
93
Chris Craikcd0ba712013-09-06 14:40:30 -070094 /**
95 * Creates an AndroidPixelRef that wraps (and refs) another to reuse/share
96 * the same storage and java byte array refcounting, yet have a different
97 * color table.
98 */
99 AndroidPixelRef(AndroidPixelRef& wrappedPixelRef, SkColorTable* ctable);
100
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800101 virtual ~AndroidPixelRef();
102
Chris Craikcd0ba712013-09-06 14:40:30 -0700103 jbyteArray getStorageObj();
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800104
105 void setLocalJNIRef(jbyteArray arr);
106
Patrick Dubroya2f0e2d2010-12-15 15:03:47 -0800107 /** Used to hold a ref to the pixels when the Java bitmap may be collected.
108 * If specified, 'localref' is a valid JNI local reference to the byte array
109 * containing the pixel data.
110 *
111 * 'localref' may only be NULL if setLocalJNIRef() was already called with
112 * a JNI local ref that is still valid.
113 */
114 virtual void globalRef(void* localref=NULL);
115
116 /** Release a ref that was acquired using globalRef(). */
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800117 virtual void globalUnref();
118
119private:
Chris Craikcd0ba712013-09-06 14:40:30 -0700120 AndroidPixelRef* const fWrappedPixelRef; // if set, delegate memory management calls to this
121
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800122 JavaVM* fVM;
123 bool fOnJavaHeap; // If true, the memory was allocated on the Java heap
124
125 jbyteArray fStorageObj; // The Java byte[] object used as the bitmap backing store
126 bool fHasGlobalRef; // If true, fStorageObj holds a JNI global ref
127
128 mutable int32_t fGlobalRefCnt;
129};
130
131/** A helper class for accessing Java-heap-allocated bitmaps.
132 * This should be used when calling into a JNI method that retains a
133 * reference to the bitmap longer than the lifetime of the Java Bitmap.
134 *
135 * After creating an instance of this class, a call to
136 * AndroidPixelRef::globalRef() will allocate a JNI global reference
137 * to the backing buffer object.
138 */
139class JavaHeapBitmapRef {
140public:
141
142 JavaHeapBitmapRef(JNIEnv *env, SkBitmap* nativeBitmap, jbyteArray buffer);
143 ~JavaHeapBitmapRef();
144
145private:
146 JNIEnv* fEnv;
147 SkBitmap* fNativeBitmap;
148 jbyteArray fBuffer;
149};
150
151/** Allocator which allocates the backing buffer in the Java heap.
152 * Instances can only be used to perform a single allocation, which helps
153 * ensure that the allocated buffer is properly accounted for with a
154 * reference in the heap (or a JNI global reference).
155 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156class JavaPixelAllocator : public SkBitmap::Allocator {
157public:
Carl Shapiro12d81c42010-12-17 18:36:48 -0800158 JavaPixelAllocator(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 // overrides
160 virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable);
Elliott Hughes8451b252011-04-07 19:17:57 -0700161
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800162 /** Return the Java array object created for the last allocation.
163 * This returns a local JNI reference which the caller is responsible
164 * for storing appropriately (usually by passing it to the Bitmap
165 * constructor).
166 */
167 jbyteArray getStorageObj() { return fStorageObj; }
168
169 /** Same as getStorageObj(), but also resets the allocator so that it
170 * can allocate again.
171 */
172 jbyteArray getStorageObjAndReset() {
173 jbyteArray result = fStorageObj;
174 fStorageObj = NULL;
175 fAllocCount = 0;
176 return result;
177 };
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179private:
Wei-Ta Chen291303b2010-08-18 15:40:29 +0800180 JavaVM* fVM;
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800181 bool fAllocateInJavaHeap;
182 jbyteArray fStorageObj;
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800183 int fAllocCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184};
185
Mike Reedc04851f2009-10-28 15:09:45 -0400186enum JNIAccess {
187 kRO_JNIAccess,
188 kRW_JNIAccess
189};
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191class AutoJavaFloatArray {
192public:
Mike Reedc04851f2009-10-28 15:09:45 -0400193 AutoJavaFloatArray(JNIEnv* env, jfloatArray array,
194 int minLength = 0, JNIAccess = kRW_JNIAccess);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 ~AutoJavaFloatArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 float* ptr() const { return fPtr; }
198 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200private:
201 JNIEnv* fEnv;
202 jfloatArray fArray;
203 float* fPtr;
204 int fLen;
Mike Reedc04851f2009-10-28 15:09:45 -0400205 int fReleaseMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206};
207
208class AutoJavaIntArray {
209public:
210 AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0);
211 ~AutoJavaIntArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 jint* ptr() const { return fPtr; }
214 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216private:
217 JNIEnv* fEnv;
218 jintArray fArray;
219 jint* fPtr;
220 int fLen;
221};
222
223class AutoJavaShortArray {
224public:
Mike Reedc04851f2009-10-28 15:09:45 -0400225 AutoJavaShortArray(JNIEnv* env, jshortArray array,
226 int minLength = 0, JNIAccess = kRW_JNIAccess);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 ~AutoJavaShortArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 jshort* ptr() const { return fPtr; }
230 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232private:
233 JNIEnv* fEnv;
234 jshortArray fArray;
235 jshort* fPtr;
236 int fLen;
Mike Reedc04851f2009-10-28 15:09:45 -0400237 int fReleaseMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238};
239
240class AutoJavaByteArray {
241public:
242 AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0);
243 ~AutoJavaByteArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 jbyte* ptr() const { return fPtr; }
246 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248private:
249 JNIEnv* fEnv;
250 jbyteArray fArray;
251 jbyte* fPtr;
252 int fLen;
253};
254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255void doThrowNPE(JNIEnv* env);
256void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception
257void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument
258void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime
259void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State
260void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800261void doThrowIOE(JNIEnv* env, const char* msg = NULL); // IO Exception
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263#define NPE_CHECK_RETURN_ZERO(env, object) \
264 do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0)
265
266#define NPE_CHECK_RETURN_VOID(env, object) \
267 do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0)
268
269#endif