1 #ifndef GraphicsJNI_DEFINED 2 #define GraphicsJNI_DEFINED 3 4 #include "SkBitmap.h" 5 #include "SkDevice.h" 6 #include "SkPixelRef.h" 7 #include "SkMallocPixelRef.h" 8 #include "SkPoint.h" 9 #include "SkRect.h" 10 #include "../images/SkBitmapRegionDecoder.h" 11 #include "../images/SkImageDecoder.h" 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); 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); 46 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 53 storage array (may be null). 54 */ 55 static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer, 56 bool isMutable, jbyteArray ninepatch, jintArray layoutbounds, 57 int density = -1); 58 59 static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, bool isMutable, 60 jbyteArray ninepatch, int density = -1); 61 62 static jobject createRegion(JNIEnv* env, SkRegion* region); 63 64 static jobject createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap); 65 66 static jbyteArray allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap, 67 SkColorTable* ctable); 68 69 /** Copy the colors in colors[] to the bitmap, convert to the correct 70 format along the way. 71 */ 72 static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset, 73 int srcStride, int x, int y, int width, int height, 74 const SkBitmap& dstBitmap); 75 76 static jbyteArray getBitmapStorageObj(SkPixelRef *pixref); 77 }; 78 79 class AndroidPixelRef : public SkMallocPixelRef { 80 public: 81 AndroidPixelRef(JNIEnv* env, void* storage, size_t size, jbyteArray storageObj, 82 SkColorTable* ctable); 83 84 virtual ~AndroidPixelRef(); 85 getStorageObj()86 jbyteArray getStorageObj() { return fStorageObj; } 87 88 void setLocalJNIRef(jbyteArray arr); 89 90 /** Used to hold a ref to the pixels when the Java bitmap may be collected. 91 * If specified, 'localref' is a valid JNI local reference to the byte array 92 * containing the pixel data. 93 * 94 * 'localref' may only be NULL if setLocalJNIRef() was already called with 95 * a JNI local ref that is still valid. 96 */ 97 virtual void globalRef(void* localref=NULL); 98 99 /** Release a ref that was acquired using globalRef(). */ 100 virtual void globalUnref(); 101 102 private: 103 JavaVM* fVM; 104 bool fOnJavaHeap; // If true, the memory was allocated on the Java heap 105 106 jbyteArray fStorageObj; // The Java byte[] object used as the bitmap backing store 107 bool fHasGlobalRef; // If true, fStorageObj holds a JNI global ref 108 109 mutable int32_t fGlobalRefCnt; 110 }; 111 112 /** A helper class for accessing Java-heap-allocated bitmaps. 113 * This should be used when calling into a JNI method that retains a 114 * reference to the bitmap longer than the lifetime of the Java Bitmap. 115 * 116 * After creating an instance of this class, a call to 117 * AndroidPixelRef::globalRef() will allocate a JNI global reference 118 * to the backing buffer object. 119 */ 120 class JavaHeapBitmapRef { 121 public: 122 123 JavaHeapBitmapRef(JNIEnv *env, SkBitmap* nativeBitmap, jbyteArray buffer); 124 ~JavaHeapBitmapRef(); 125 126 private: 127 JNIEnv* fEnv; 128 SkBitmap* fNativeBitmap; 129 jbyteArray fBuffer; 130 }; 131 132 /** Allocator which allocates the backing buffer in the Java heap. 133 * Instances can only be used to perform a single allocation, which helps 134 * ensure that the allocated buffer is properly accounted for with a 135 * reference in the heap (or a JNI global reference). 136 */ 137 class JavaPixelAllocator : public SkBitmap::Allocator { 138 public: 139 JavaPixelAllocator(JNIEnv* env); 140 // overrides 141 virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable); 142 143 /** Return the Java array object created for the last allocation. 144 * This returns a local JNI reference which the caller is responsible 145 * for storing appropriately (usually by passing it to the Bitmap 146 * constructor). 147 */ getStorageObj()148 jbyteArray getStorageObj() { return fStorageObj; } 149 150 /** Same as getStorageObj(), but also resets the allocator so that it 151 * can allocate again. 152 */ getStorageObjAndReset()153 jbyteArray getStorageObjAndReset() { 154 jbyteArray result = fStorageObj; 155 fStorageObj = NULL; 156 fAllocCount = 0; 157 return result; 158 }; 159 160 private: 161 JavaVM* fVM; 162 bool fAllocateInJavaHeap; 163 jbyteArray fStorageObj; 164 int fAllocCount; 165 }; 166 167 enum JNIAccess { 168 kRO_JNIAccess, 169 kRW_JNIAccess 170 }; 171 172 class AutoJavaFloatArray { 173 public: 174 AutoJavaFloatArray(JNIEnv* env, jfloatArray array, 175 int minLength = 0, JNIAccess = kRW_JNIAccess); 176 ~AutoJavaFloatArray(); 177 ptr()178 float* ptr() const { return fPtr; } length()179 int length() const { return fLen; } 180 181 private: 182 JNIEnv* fEnv; 183 jfloatArray fArray; 184 float* fPtr; 185 int fLen; 186 int fReleaseMode; 187 }; 188 189 class AutoJavaIntArray { 190 public: 191 AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0); 192 ~AutoJavaIntArray(); 193 ptr()194 jint* ptr() const { return fPtr; } length()195 int length() const { return fLen; } 196 197 private: 198 JNIEnv* fEnv; 199 jintArray fArray; 200 jint* fPtr; 201 int fLen; 202 }; 203 204 class AutoJavaShortArray { 205 public: 206 AutoJavaShortArray(JNIEnv* env, jshortArray array, 207 int minLength = 0, JNIAccess = kRW_JNIAccess); 208 ~AutoJavaShortArray(); 209 ptr()210 jshort* ptr() const { return fPtr; } length()211 int length() const { return fLen; } 212 213 private: 214 JNIEnv* fEnv; 215 jshortArray fArray; 216 jshort* fPtr; 217 int fLen; 218 int fReleaseMode; 219 }; 220 221 class AutoJavaByteArray { 222 public: 223 AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0); 224 ~AutoJavaByteArray(); 225 ptr()226 jbyte* ptr() const { return fPtr; } length()227 int length() const { return fLen; } 228 229 private: 230 JNIEnv* fEnv; 231 jbyteArray fArray; 232 jbyte* fPtr; 233 int fLen; 234 }; 235 236 void doThrowNPE(JNIEnv* env); 237 void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception 238 void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument 239 void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime 240 void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State 241 void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory 242 void doThrowIOE(JNIEnv* env, const char* msg = NULL); // IO Exception 243 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 251