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" |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 10 | #include "SkImageDecoder.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11 | #include <jni.h> |
| 12 | |
Derek Sollenberger | 5827cb5 | 2013-07-26 14:58:06 -0400 | [diff] [blame] | 13 | class SkBitmapRegionDecoder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14 | class SkCanvas; |
| 15 | class SkPaint; |
| 16 | class SkPicture; |
| 17 | |
| 18 | class GraphicsJNI { |
| 19 | public: |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 20 | enum BitmapCreateFlags { |
| 21 | kBitmapCreateFlag_None = 0x0, |
| 22 | kBitmapCreateFlag_Mutable = 0x1, |
| 23 | kBitmapCreateFlag_Premultiplied = 0x2, |
| 24 | }; |
| 25 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | // 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 Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 38 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | static void set_jpoint(JNIEnv*, jobject jrect, int x, int y); |
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 SkIPoint* jpoint_to_ipoint(JNIEnv*, jobject jpoint, SkIPoint* point); |
| 42 | static void ipoint_to_jpoint(const SkIPoint& point, JNIEnv*, jobject jpoint); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 43 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | static SkPoint* jpointf_to_point(JNIEnv*, jobject jpointf, SkPoint* point); |
| 45 | static void point_to_jpointf(const SkPoint& point, JNIEnv*, jobject jpointf); |
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 | 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 Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 52 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | /** 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 Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 57 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | /** Create a java Bitmap object given the native bitmap (required) and optional |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 59 | storage array (may be null). |
| 60 | */ |
| 61 | static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer, |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 62 | int bitmapCreateFlags, jbyteArray ninepatch, jintArray layoutbounds, int density = -1); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 63 | |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 64 | static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, int bitmapCreateFlags, |
| 65 | jbyteArray ninepatch, int density = -1); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 66 | |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 67 | static void reinitBitmap(JNIEnv* env, jobject javaBitmap, SkBitmap* bitmap, |
| 68 | bool isPremultiplied); |
Chris Craik | 9f58361 | 2013-05-20 18:13:47 -0700 | [diff] [blame] | 69 | |
| 70 | static int getBitmapAllocationByteCount(JNIEnv* env, jobject javaBitmap); |
| 71 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | static jobject createRegion(JNIEnv* env, SkRegion* region); |
| 73 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 74 | static jobject createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap); |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 75 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 76 | static jbyteArray allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap, |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 77 | SkColorTable* ctable); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | /** 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 Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 83 | int srcStride, int x, int y, int width, int height, |
| 84 | const SkBitmap& dstBitmap, bool isPremultiplied); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 85 | |
| 86 | static jbyteArray getBitmapStorageObj(SkPixelRef *pixref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 89 | class AndroidPixelRef : public SkMallocPixelRef { |
| 90 | public: |
| 91 | AndroidPixelRef(JNIEnv* env, void* storage, size_t size, jbyteArray storageObj, |
| 92 | SkColorTable* ctable); |
| 93 | |
Chris Craik | cd0ba71 | 2013-09-06 14:40:30 -0700 | [diff] [blame] | 94 | /** |
| 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 Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 101 | virtual ~AndroidPixelRef(); |
| 102 | |
Chris Craik | cd0ba71 | 2013-09-06 14:40:30 -0700 | [diff] [blame] | 103 | jbyteArray getStorageObj(); |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 104 | |
| 105 | void setLocalJNIRef(jbyteArray arr); |
| 106 | |
Patrick Dubroy | a2f0e2d | 2010-12-15 15:03:47 -0800 | [diff] [blame] | 107 | /** 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 Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 117 | virtual void globalUnref(); |
| 118 | |
| 119 | private: |
Chris Craik | cd0ba71 | 2013-09-06 14:40:30 -0700 | [diff] [blame] | 120 | AndroidPixelRef* const fWrappedPixelRef; // if set, delegate memory management calls to this |
| 121 | |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 122 | 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 | */ |
| 139 | class JavaHeapBitmapRef { |
| 140 | public: |
| 141 | |
| 142 | JavaHeapBitmapRef(JNIEnv *env, SkBitmap* nativeBitmap, jbyteArray buffer); |
| 143 | ~JavaHeapBitmapRef(); |
| 144 | |
| 145 | private: |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | class JavaPixelAllocator : public SkBitmap::Allocator { |
| 157 | public: |
Carl Shapiro | 12d81c4 | 2010-12-17 18:36:48 -0800 | [diff] [blame] | 158 | JavaPixelAllocator(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | // overrides |
| 160 | virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 161 | |
Patrick Dubroy | afde46e | 2010-12-15 11:52:01 -0800 | [diff] [blame] | 162 | /** 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 Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 178 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | private: |
Wei-Ta Chen | 291303b | 2010-08-18 15:40:29 +0800 | [diff] [blame] | 180 | JavaVM* fVM; |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 181 | bool fAllocateInJavaHeap; |
| 182 | jbyteArray fStorageObj; |
Patrick Dubroy | afde46e | 2010-12-15 11:52:01 -0800 | [diff] [blame] | 183 | int fAllocCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | }; |
| 185 | |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 186 | enum JNIAccess { |
| 187 | kRO_JNIAccess, |
| 188 | kRW_JNIAccess |
| 189 | }; |
| 190 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | class AutoJavaFloatArray { |
| 192 | public: |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 193 | AutoJavaFloatArray(JNIEnv* env, jfloatArray array, |
| 194 | int minLength = 0, JNIAccess = kRW_JNIAccess); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | ~AutoJavaFloatArray(); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 196 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | float* ptr() const { return fPtr; } |
| 198 | int length() const { return fLen; } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 199 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | private: |
| 201 | JNIEnv* fEnv; |
| 202 | jfloatArray fArray; |
| 203 | float* fPtr; |
| 204 | int fLen; |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 205 | int fReleaseMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | class AutoJavaIntArray { |
| 209 | public: |
| 210 | AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0); |
| 211 | ~AutoJavaIntArray(); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 212 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | jint* ptr() const { return fPtr; } |
| 214 | int length() const { return fLen; } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 215 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | private: |
| 217 | JNIEnv* fEnv; |
| 218 | jintArray fArray; |
| 219 | jint* fPtr; |
| 220 | int fLen; |
| 221 | }; |
| 222 | |
| 223 | class AutoJavaShortArray { |
| 224 | public: |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 225 | AutoJavaShortArray(JNIEnv* env, jshortArray array, |
| 226 | int minLength = 0, JNIAccess = kRW_JNIAccess); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | ~AutoJavaShortArray(); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 228 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | jshort* ptr() const { return fPtr; } |
| 230 | int length() const { return fLen; } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 231 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | private: |
| 233 | JNIEnv* fEnv; |
| 234 | jshortArray fArray; |
| 235 | jshort* fPtr; |
| 236 | int fLen; |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 237 | int fReleaseMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | class AutoJavaByteArray { |
| 241 | public: |
| 242 | AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0); |
| 243 | ~AutoJavaByteArray(); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 244 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | jbyte* ptr() const { return fPtr; } |
| 246 | int length() const { return fLen; } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 247 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | private: |
| 249 | JNIEnv* fEnv; |
| 250 | jbyteArray fArray; |
| 251 | jbyte* fPtr; |
| 252 | int fLen; |
| 253 | }; |
| 254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | void doThrowNPE(JNIEnv* env); |
| 256 | void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception |
| 257 | void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument |
| 258 | void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime |
| 259 | void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State |
| 260 | void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 261 | 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] | 262 | |
| 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 |