GraphicsJNI.h revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1#ifndef GraphicsJNI_DEFINED 2#define GraphicsJNI_DEFINED 3 4#include "SkPoint.h" 5#include "SkRect.h" 6#include "SkBitmap.h" 7#include <jni.h> 8 9class SkCanvas; 10class SkPaint; 11class SkPicture; 12 13class GraphicsJNI { 14public: 15 // returns true if an exception is set (and dumps it out to the Log) 16 static bool hasException(JNIEnv*); 17 18 static void get_jrect(JNIEnv*, jobject jrect, int* L, int* T, int* R, int* B); 19 static void set_jrect(JNIEnv*, jobject jrect, int L, int T, int R, int B); 20 21 static SkIRect* jrect_to_irect(JNIEnv*, jobject jrect, SkIRect*); 22 static void irect_to_jrect(const SkIRect&, JNIEnv*, jobject jrect); 23 24 static SkRect* jrectf_to_rect(JNIEnv*, jobject jrectf, SkRect*); 25 static SkRect* jrect_to_rect(JNIEnv*, jobject jrect, SkRect*); 26 static void rect_to_jrectf(const SkRect&, JNIEnv*, jobject jrectf); 27 28 static void set_jpoint(JNIEnv*, jobject jrect, int x, int y); 29 30 static SkIPoint* jpoint_to_ipoint(JNIEnv*, jobject jpoint, SkIPoint* point); 31 static void ipoint_to_jpoint(const SkIPoint& point, JNIEnv*, jobject jpoint); 32 33 static SkPoint* jpointf_to_point(JNIEnv*, jobject jpointf, SkPoint* point); 34 static void point_to_jpointf(const SkPoint& point, JNIEnv*, jobject jpointf); 35 36 static SkCanvas* getNativeCanvas(JNIEnv*, jobject canvas); 37 static SkPaint* getNativePaint(JNIEnv*, jobject paint); 38 static SkBitmap* getNativeBitmap(JNIEnv*, jobject bitmap); 39 static SkPicture* getNativePicture(JNIEnv*, jobject picture); 40 static SkRegion* getNativeRegion(JNIEnv*, jobject region); 41 42 /** Return the corresponding native config from the java Config enum, 43 or kNo_Config if the java object is null. 44 */ 45 static SkBitmap::Config getNativeBitmapConfig(JNIEnv*, jobject jconfig); 46 47 /** Create a java Bitmap object given the native bitmap (required) and optional 48 storage array (may be null). If storage is specified, then it must already be 49 locked, and its native address set as the bitmap's pixels. If storage is null, 50 then the bitmap must be an owner of its natively allocated pixels (via allocPixels). 51 */ 52 static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, bool isMutable, 53 jbyteArray ninePatch); 54 55 static jobject createRegion(JNIEnv* env, SkRegion* region); 56 57 /** Set a pixelref for the bitmap (needs setConfig to already be called) 58 Returns true on success. If it returns false, then it failed, and the 59 appropriate exception will have been raised. 60 */ 61 static bool setJavaPixelRef(JNIEnv*, SkBitmap*, SkColorTable* ctable); 62 63 /** Copy the colors in colors[] to the bitmap, convert to the correct 64 format along the way. 65 */ 66 static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset, 67 int srcStride, int x, int y, int width, int height, 68 const SkBitmap& dstBitmap); 69}; 70 71class JavaPixelAllocator : public SkBitmap::Allocator { 72public: 73 JavaPixelAllocator(JNIEnv* env); 74 // overrides 75 virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable); 76 77private: 78 JNIEnv* fEnv; 79}; 80 81class AutoJavaFloatArray { 82public: 83 AutoJavaFloatArray(JNIEnv* env, jfloatArray array, int minLength = 0); 84 ~AutoJavaFloatArray(); 85 86 float* ptr() const { return fPtr; } 87 int length() const { return fLen; } 88 89private: 90 JNIEnv* fEnv; 91 jfloatArray fArray; 92 float* fPtr; 93 int fLen; 94}; 95 96class AutoJavaIntArray { 97public: 98 AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0); 99 ~AutoJavaIntArray(); 100 101 jint* ptr() const { return fPtr; } 102 int length() const { return fLen; } 103 104private: 105 JNIEnv* fEnv; 106 jintArray fArray; 107 jint* fPtr; 108 int fLen; 109}; 110 111class AutoJavaShortArray { 112public: 113 AutoJavaShortArray(JNIEnv* env, jshortArray array, int minLength = 0); 114 ~AutoJavaShortArray(); 115 116 jshort* ptr() const { return fPtr; } 117 int length() const { return fLen; } 118 119private: 120 JNIEnv* fEnv; 121 jshortArray fArray; 122 jshort* fPtr; 123 int fLen; 124}; 125 126class AutoJavaByteArray { 127public: 128 AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0); 129 ~AutoJavaByteArray(); 130 131 jbyte* ptr() const { return fPtr; } 132 int length() const { return fLen; } 133 134private: 135 JNIEnv* fEnv; 136 jbyteArray fArray; 137 jbyte* fPtr; 138 int fLen; 139}; 140 141void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL); 142void doThrowNPE(JNIEnv* env); 143void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception 144void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument 145void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime 146void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State 147void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory 148 149#define NPE_CHECK_RETURN_ZERO(env, object) \ 150 do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0) 151 152#define NPE_CHECK_RETURN_VOID(env, object) \ 153 do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0) 154 155#endif 156 157