blob: cb154aaf28e58b6ef9e40b9d277b1b6fba604b48 [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).
Leon Scroggins III8790be62013-12-03 16:26:51 -050060 bitmap's SkAlphaType must already be in sync with bitmapCreateFlags.
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080061 */
62 static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer,
Chris Craik1abf5d62013-08-16 12:47:03 -070063 int bitmapCreateFlags, jbyteArray ninepatch, jintArray layoutbounds, int density = -1);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080064
Chris Craik1abf5d62013-08-16 12:47:03 -070065 static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, int bitmapCreateFlags,
66 jbyteArray ninepatch, int density = -1);
Elliott Hughes8451b252011-04-07 19:17:57 -070067
Leon Scroggins III8790be62013-12-03 16:26:51 -050068 /** Reinitialize a bitmap. bitmap must already have its SkAlphaType set in
69 sync with isPremultiplied
70 */
Chris Craik1abf5d62013-08-16 12:47:03 -070071 static void reinitBitmap(JNIEnv* env, jobject javaBitmap, SkBitmap* bitmap,
72 bool isPremultiplied);
Chris Craik9f583612013-05-20 18:13:47 -070073
74 static int getBitmapAllocationByteCount(JNIEnv* env, jobject javaBitmap);
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 static jobject createRegion(JNIEnv* env, SkRegion* region);
77
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080078 static jobject createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap);
Joseph Wenf1f48bc2010-07-19 16:59:51 +080079
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080080 static jbyteArray allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap,
Chris Craik1abf5d62013-08-16 12:47:03 -070081 SkColorTable* ctable);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 /** Copy the colors in colors[] to the bitmap, convert to the correct
84 format along the way.
85 */
86 static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset,
Chris Craik1abf5d62013-08-16 12:47:03 -070087 int srcStride, int x, int y, int width, int height,
88 const SkBitmap& dstBitmap, bool isPremultiplied);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080089
90 static jbyteArray getBitmapStorageObj(SkPixelRef *pixref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091};
92
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080093class AndroidPixelRef : public SkMallocPixelRef {
94public:
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -050095 AndroidPixelRef(JNIEnv* env, const SkImageInfo& info, void* storage, size_t rowBytes,
96 jbyteArray storageObj, SkColorTable* ctable);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080097
Chris Craikcd0ba712013-09-06 14:40:30 -070098 /**
99 * Creates an AndroidPixelRef that wraps (and refs) another to reuse/share
100 * the same storage and java byte array refcounting, yet have a different
101 * color table.
102 */
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500103 AndroidPixelRef(AndroidPixelRef& wrappedPixelRef, const SkImageInfo& info,
104 size_t rowBytes, SkColorTable* ctable);
Chris Craikcd0ba712013-09-06 14:40:30 -0700105
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800106 virtual ~AndroidPixelRef();
107
Chris Craikcd0ba712013-09-06 14:40:30 -0700108 jbyteArray getStorageObj();
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800109
110 void setLocalJNIRef(jbyteArray arr);
111
Patrick Dubroya2f0e2d2010-12-15 15:03:47 -0800112 /** Used to hold a ref to the pixels when the Java bitmap may be collected.
113 * If specified, 'localref' is a valid JNI local reference to the byte array
114 * containing the pixel data.
115 *
116 * 'localref' may only be NULL if setLocalJNIRef() was already called with
117 * a JNI local ref that is still valid.
118 */
119 virtual void globalRef(void* localref=NULL);
120
121 /** Release a ref that was acquired using globalRef(). */
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800122 virtual void globalUnref();
123
124private:
Chris Craikcd0ba712013-09-06 14:40:30 -0700125 AndroidPixelRef* const fWrappedPixelRef; // if set, delegate memory management calls to this
126
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800127 JavaVM* fVM;
128 bool fOnJavaHeap; // If true, the memory was allocated on the Java heap
129
130 jbyteArray fStorageObj; // The Java byte[] object used as the bitmap backing store
131 bool fHasGlobalRef; // If true, fStorageObj holds a JNI global ref
132
133 mutable int32_t fGlobalRefCnt;
134};
135
136/** A helper class for accessing Java-heap-allocated bitmaps.
137 * This should be used when calling into a JNI method that retains a
138 * reference to the bitmap longer than the lifetime of the Java Bitmap.
139 *
140 * After creating an instance of this class, a call to
141 * AndroidPixelRef::globalRef() will allocate a JNI global reference
142 * to the backing buffer object.
143 */
144class JavaHeapBitmapRef {
145public:
146
147 JavaHeapBitmapRef(JNIEnv *env, SkBitmap* nativeBitmap, jbyteArray buffer);
148 ~JavaHeapBitmapRef();
149
150private:
151 JNIEnv* fEnv;
152 SkBitmap* fNativeBitmap;
153 jbyteArray fBuffer;
154};
155
156/** Allocator which allocates the backing buffer in the Java heap.
157 * Instances can only be used to perform a single allocation, which helps
158 * ensure that the allocated buffer is properly accounted for with a
159 * reference in the heap (or a JNI global reference).
160 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161class JavaPixelAllocator : public SkBitmap::Allocator {
162public:
Carl Shapiro12d81c42010-12-17 18:36:48 -0800163 JavaPixelAllocator(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 // overrides
165 virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable);
Elliott Hughes8451b252011-04-07 19:17:57 -0700166
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800167 /** Return the Java array object created for the last allocation.
168 * This returns a local JNI reference which the caller is responsible
169 * for storing appropriately (usually by passing it to the Bitmap
170 * constructor).
171 */
172 jbyteArray getStorageObj() { return fStorageObj; }
173
174 /** Same as getStorageObj(), but also resets the allocator so that it
175 * can allocate again.
176 */
177 jbyteArray getStorageObjAndReset() {
178 jbyteArray result = fStorageObj;
179 fStorageObj = NULL;
180 fAllocCount = 0;
181 return result;
182 };
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184private:
Wei-Ta Chen291303b2010-08-18 15:40:29 +0800185 JavaVM* fVM;
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800186 bool fAllocateInJavaHeap;
187 jbyteArray fStorageObj;
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800188 int fAllocCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189};
190
Mike Reedc04851f2009-10-28 15:09:45 -0400191enum JNIAccess {
192 kRO_JNIAccess,
193 kRW_JNIAccess
194};
195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196class AutoJavaFloatArray {
197public:
Mike Reedc04851f2009-10-28 15:09:45 -0400198 AutoJavaFloatArray(JNIEnv* env, jfloatArray array,
199 int minLength = 0, JNIAccess = kRW_JNIAccess);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 ~AutoJavaFloatArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 float* ptr() const { return fPtr; }
203 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205private:
206 JNIEnv* fEnv;
207 jfloatArray fArray;
208 float* fPtr;
209 int fLen;
Mike Reedc04851f2009-10-28 15:09:45 -0400210 int fReleaseMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211};
212
213class AutoJavaIntArray {
214public:
215 AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0);
216 ~AutoJavaIntArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 jint* ptr() const { return fPtr; }
219 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221private:
222 JNIEnv* fEnv;
223 jintArray fArray;
224 jint* fPtr;
225 int fLen;
226};
227
228class AutoJavaShortArray {
229public:
Mike Reedc04851f2009-10-28 15:09:45 -0400230 AutoJavaShortArray(JNIEnv* env, jshortArray array,
231 int minLength = 0, JNIAccess = kRW_JNIAccess);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 ~AutoJavaShortArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 jshort* ptr() const { return fPtr; }
235 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237private:
238 JNIEnv* fEnv;
239 jshortArray fArray;
240 jshort* fPtr;
241 int fLen;
Mike Reedc04851f2009-10-28 15:09:45 -0400242 int fReleaseMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243};
244
245class AutoJavaByteArray {
246public:
247 AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0);
248 ~AutoJavaByteArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 jbyte* ptr() const { return fPtr; }
251 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253private:
254 JNIEnv* fEnv;
255 jbyteArray fArray;
256 jbyte* fPtr;
257 int fLen;
258};
259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260void doThrowNPE(JNIEnv* env);
261void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception
262void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument
263void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime
264void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State
265void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800266void doThrowIOE(JNIEnv* env, const char* msg = NULL); // IO Exception
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267
268#define NPE_CHECK_RETURN_ZERO(env, object) \
269 do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0)
270
271#define NPE_CHECK_RETURN_VOID(env, object) \
272 do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0)
273
274#endif