GraphicsJNI.h revision 11ea33471e1a14a8594f0b2cd012d86340dd3bd8
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 bool reportSizeToVM); 63 64 /** Copy the colors in colors[] to the bitmap, convert to the correct 65 format along the way. 66 */ 67 static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset, 68 int srcStride, int x, int y, int width, int height, 69 const SkBitmap& dstBitmap); 70}; 71 72class JavaPixelAllocator : public SkBitmap::Allocator { 73public: 74 JavaPixelAllocator(JNIEnv* env, bool reportSizeToVM); 75 // overrides 76 virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable); 77 78private: 79 JNIEnv* fEnv; 80 bool fReportSizeToVM; 81}; 82 83class AutoJavaFloatArray { 84public: 85 AutoJavaFloatArray(JNIEnv* env, jfloatArray array, int minLength = 0); 86 ~AutoJavaFloatArray(); 87 88 float* ptr() const { return fPtr; } 89 int length() const { return fLen; } 90 91private: 92 JNIEnv* fEnv; 93 jfloatArray fArray; 94 float* fPtr; 95 int fLen; 96}; 97 98class AutoJavaIntArray { 99public: 100 AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0); 101 ~AutoJavaIntArray(); 102 103 jint* ptr() const { return fPtr; } 104 int length() const { return fLen; } 105 106private: 107 JNIEnv* fEnv; 108 jintArray fArray; 109 jint* fPtr; 110 int fLen; 111}; 112 113class AutoJavaShortArray { 114public: 115 AutoJavaShortArray(JNIEnv* env, jshortArray array, int minLength = 0); 116 ~AutoJavaShortArray(); 117 118 jshort* ptr() const { return fPtr; } 119 int length() const { return fLen; } 120 121private: 122 JNIEnv* fEnv; 123 jshortArray fArray; 124 jshort* fPtr; 125 int fLen; 126}; 127 128class AutoJavaByteArray { 129public: 130 AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0); 131 ~AutoJavaByteArray(); 132 133 jbyte* ptr() const { return fPtr; } 134 int length() const { return fLen; } 135 136private: 137 JNIEnv* fEnv; 138 jbyteArray fArray; 139 jbyte* fPtr; 140 int fLen; 141}; 142 143void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL); 144void doThrowNPE(JNIEnv* env); 145void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception 146void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument 147void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime 148void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State 149void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory 150 151#define NPE_CHECK_RETURN_ZERO(env, object) \ 152 do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0) 153 154#define NPE_CHECK_RETURN_VOID(env, object) \ 155 do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0) 156 157#endif 158 159