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