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