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