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