The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | #ifndef GraphicsJNI_DEFINED |
| 2 | #define GraphicsJNI_DEFINED |
| 3 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 4 | #include "SkBitmap.h" |
| 5 | #include "SkDevice.h" |
| 6 | #include "SkPixelRef.h" |
| 7 | #include "SkMallocPixelRef.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | #include "SkPoint.h" |
| 9 | #include "SkRect.h" |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 10 | #include "../images/SkBitmapRegionDecoder.h" |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 11 | #include "../images/SkImageDecoder.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12 | #include <jni.h> |
| 13 | |
| 14 | class SkCanvas; |
| 15 | class SkPaint; |
| 16 | class SkPicture; |
| 17 | |
| 18 | class GraphicsJNI { |
| 19 | public: |
| 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); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 32 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | static void set_jpoint(JNIEnv*, jobject jrect, int x, int y); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 34 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | static SkIPoint* jpoint_to_ipoint(JNIEnv*, jobject jpoint, SkIPoint* point); |
| 36 | static void ipoint_to_jpoint(const SkIPoint& point, JNIEnv*, jobject jpoint); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 37 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | static SkPoint* jpointf_to_point(JNIEnv*, jobject jpointf, SkPoint* point); |
| 39 | static void point_to_jpointf(const SkPoint& point, JNIEnv*, jobject jpointf); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 40 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 46 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 51 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | /** Create a java Bitmap object given the native bitmap (required) and optional |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 53 | storage array (may be null). |
| 54 | */ |
| 55 | static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer, |
Amith Yamasani | ec4a504 | 2012-04-04 10:27:15 -0700 | [diff] [blame] | 56 | bool isMutable, jbyteArray ninepatch, jintArray layoutbounds, |
| 57 | int density = -1); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 58 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, bool isMutable, |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 60 | jbyteArray ninepatch, int density = -1); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 61 | |
Chris Craik | 9f58361 | 2013-05-20 18:13:47 -0700 | [diff] [blame] | 62 | static void reinitBitmap(JNIEnv* env, jobject javaBitmap); |
| 63 | |
| 64 | static int getBitmapAllocationByteCount(JNIEnv* env, jobject javaBitmap); |
| 65 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | static jobject createRegion(JNIEnv* env, SkRegion* region); |
| 67 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 68 | static jobject createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap); |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 69 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 70 | static jbyteArray allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap, |
| 71 | SkColorTable* ctable); |
| 72 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | /** Copy the colors in colors[] to the bitmap, convert to the correct |
| 74 | format along the way. |
| 75 | */ |
| 76 | static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset, |
| 77 | int srcStride, int x, int y, int width, int height, |
| 78 | const SkBitmap& dstBitmap); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 79 | |
| 80 | static jbyteArray getBitmapStorageObj(SkPixelRef *pixref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 83 | class AndroidPixelRef : public SkMallocPixelRef { |
| 84 | public: |
| 85 | AndroidPixelRef(JNIEnv* env, void* storage, size_t size, jbyteArray storageObj, |
| 86 | SkColorTable* ctable); |
| 87 | |
| 88 | virtual ~AndroidPixelRef(); |
| 89 | |
| 90 | jbyteArray getStorageObj() { return fStorageObj; } |
| 91 | |
| 92 | void setLocalJNIRef(jbyteArray arr); |
| 93 | |
Patrick Dubroy | a2f0e2d | 2010-12-15 15:03:47 -0800 | [diff] [blame] | 94 | /** Used to hold a ref to the pixels when the Java bitmap may be collected. |
| 95 | * If specified, 'localref' is a valid JNI local reference to the byte array |
| 96 | * containing the pixel data. |
| 97 | * |
| 98 | * 'localref' may only be NULL if setLocalJNIRef() was already called with |
| 99 | * a JNI local ref that is still valid. |
| 100 | */ |
| 101 | virtual void globalRef(void* localref=NULL); |
| 102 | |
| 103 | /** Release a ref that was acquired using globalRef(). */ |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 104 | virtual void globalUnref(); |
| 105 | |
| 106 | private: |
| 107 | JavaVM* fVM; |
| 108 | bool fOnJavaHeap; // If true, the memory was allocated on the Java heap |
| 109 | |
| 110 | jbyteArray fStorageObj; // The Java byte[] object used as the bitmap backing store |
| 111 | bool fHasGlobalRef; // If true, fStorageObj holds a JNI global ref |
| 112 | |
| 113 | mutable int32_t fGlobalRefCnt; |
| 114 | }; |
| 115 | |
| 116 | /** A helper class for accessing Java-heap-allocated bitmaps. |
| 117 | * This should be used when calling into a JNI method that retains a |
| 118 | * reference to the bitmap longer than the lifetime of the Java Bitmap. |
| 119 | * |
| 120 | * After creating an instance of this class, a call to |
| 121 | * AndroidPixelRef::globalRef() will allocate a JNI global reference |
| 122 | * to the backing buffer object. |
| 123 | */ |
| 124 | class JavaHeapBitmapRef { |
| 125 | public: |
| 126 | |
| 127 | JavaHeapBitmapRef(JNIEnv *env, SkBitmap* nativeBitmap, jbyteArray buffer); |
| 128 | ~JavaHeapBitmapRef(); |
| 129 | |
| 130 | private: |
| 131 | JNIEnv* fEnv; |
| 132 | SkBitmap* fNativeBitmap; |
| 133 | jbyteArray fBuffer; |
| 134 | }; |
| 135 | |
| 136 | /** Allocator which allocates the backing buffer in the Java heap. |
| 137 | * Instances can only be used to perform a single allocation, which helps |
| 138 | * ensure that the allocated buffer is properly accounted for with a |
| 139 | * reference in the heap (or a JNI global reference). |
| 140 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | class JavaPixelAllocator : public SkBitmap::Allocator { |
| 142 | public: |
Carl Shapiro | 12d81c4 | 2010-12-17 18:36:48 -0800 | [diff] [blame] | 143 | JavaPixelAllocator(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | // overrides |
| 145 | virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 146 | |
Patrick Dubroy | afde46e | 2010-12-15 11:52:01 -0800 | [diff] [blame] | 147 | /** Return the Java array object created for the last allocation. |
| 148 | * This returns a local JNI reference which the caller is responsible |
| 149 | * for storing appropriately (usually by passing it to the Bitmap |
| 150 | * constructor). |
| 151 | */ |
| 152 | jbyteArray getStorageObj() { return fStorageObj; } |
| 153 | |
| 154 | /** Same as getStorageObj(), but also resets the allocator so that it |
| 155 | * can allocate again. |
| 156 | */ |
| 157 | jbyteArray getStorageObjAndReset() { |
| 158 | jbyteArray result = fStorageObj; |
| 159 | fStorageObj = NULL; |
| 160 | fAllocCount = 0; |
| 161 | return result; |
| 162 | }; |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 163 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | private: |
Wei-Ta Chen | 291303b | 2010-08-18 15:40:29 +0800 | [diff] [blame] | 165 | JavaVM* fVM; |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 166 | bool fAllocateInJavaHeap; |
| 167 | jbyteArray fStorageObj; |
Patrick Dubroy | afde46e | 2010-12-15 11:52:01 -0800 | [diff] [blame] | 168 | int fAllocCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 171 | enum JNIAccess { |
| 172 | kRO_JNIAccess, |
| 173 | kRW_JNIAccess |
| 174 | }; |
| 175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | class AutoJavaFloatArray { |
| 177 | public: |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 178 | AutoJavaFloatArray(JNIEnv* env, jfloatArray array, |
| 179 | int minLength = 0, JNIAccess = kRW_JNIAccess); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | ~AutoJavaFloatArray(); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | float* ptr() const { return fPtr; } |
| 183 | int length() const { return fLen; } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 184 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | private: |
| 186 | JNIEnv* fEnv; |
| 187 | jfloatArray fArray; |
| 188 | float* fPtr; |
| 189 | int fLen; |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 190 | int fReleaseMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | class AutoJavaIntArray { |
| 194 | public: |
| 195 | AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0); |
| 196 | ~AutoJavaIntArray(); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | jint* ptr() const { return fPtr; } |
| 199 | int length() const { return fLen; } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 200 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | private: |
| 202 | JNIEnv* fEnv; |
| 203 | jintArray fArray; |
| 204 | jint* fPtr; |
| 205 | int fLen; |
| 206 | }; |
| 207 | |
| 208 | class AutoJavaShortArray { |
| 209 | public: |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 210 | AutoJavaShortArray(JNIEnv* env, jshortArray array, |
| 211 | int minLength = 0, JNIAccess = kRW_JNIAccess); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | ~AutoJavaShortArray(); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | jshort* ptr() const { return fPtr; } |
| 215 | int length() const { return fLen; } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 216 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | private: |
| 218 | JNIEnv* fEnv; |
| 219 | jshortArray fArray; |
| 220 | jshort* fPtr; |
| 221 | int fLen; |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 222 | int fReleaseMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | class AutoJavaByteArray { |
| 226 | public: |
| 227 | AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0); |
| 228 | ~AutoJavaByteArray(); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 229 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | jbyte* ptr() const { return fPtr; } |
| 231 | int length() const { return fLen; } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 232 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | private: |
| 234 | JNIEnv* fEnv; |
| 235 | jbyteArray fArray; |
| 236 | jbyte* fPtr; |
| 237 | int fLen; |
| 238 | }; |
| 239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | void doThrowNPE(JNIEnv* env); |
| 241 | void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception |
| 242 | void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument |
| 243 | void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime |
| 244 | void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State |
| 245 | void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 246 | void doThrowIOE(JNIEnv* env, const char* msg = NULL); // IO Exception |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | |
| 248 | #define NPE_CHECK_RETURN_ZERO(env, object) \ |
| 249 | do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0) |
| 250 | |
| 251 | #define NPE_CHECK_RETURN_VOID(env, object) \ |
| 252 | do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0) |
| 253 | |
| 254 | #endif |