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