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