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