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