GraphicsJNI.h revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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 static SkScalar getCanvasDensityScale(JNIEnv*, jobject canvas); 42 43 /** Return the corresponding native config from the java Config enum, 44 or kNo_Config if the java object is null. 45 */ 46 static SkBitmap::Config getNativeBitmapConfig(JNIEnv*, jobject jconfig); 47 48 /** Create a java Bitmap object given the native bitmap (required) and optional 49 storage array (may be null). If storage is specified, then it must already be 50 locked, and its native address set as the bitmap's pixels. If storage is null, 51 then the bitmap must be an owner of its natively allocated pixels (via allocPixels). 52 */ 53 static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, bool isMutable, 54 jbyteArray ninePatch); 55 56 static jobject createRegion(JNIEnv* env, SkRegion* region); 57 58 /** Set a pixelref for the bitmap (needs setConfig to already be called) 59 Returns true on success. If it returns false, then it failed, and the 60 appropriate exception will have been raised. 61 */ 62 static bool setJavaPixelRef(JNIEnv*, SkBitmap*, SkColorTable* ctable); 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); 75 // overrides 76 virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable); 77 78private: 79 JNIEnv* fEnv; 80}; 81 82class AutoJavaFloatArray { 83public: 84 AutoJavaFloatArray(JNIEnv* env, jfloatArray array, int minLength = 0); 85 ~AutoJavaFloatArray(); 86 87 float* ptr() const { return fPtr; } 88 int length() const { return fLen; } 89 90private: 91 JNIEnv* fEnv; 92 jfloatArray fArray; 93 float* fPtr; 94 int fLen; 95}; 96 97class AutoJavaIntArray { 98public: 99 AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0); 100 ~AutoJavaIntArray(); 101 102 jint* ptr() const { return fPtr; } 103 int length() const { return fLen; } 104 105private: 106 JNIEnv* fEnv; 107 jintArray fArray; 108 jint* fPtr; 109 int fLen; 110}; 111 112class AutoJavaShortArray { 113public: 114 AutoJavaShortArray(JNIEnv* env, jshortArray array, int minLength = 0); 115 ~AutoJavaShortArray(); 116 117 jshort* ptr() const { return fPtr; } 118 int length() const { return fLen; } 119 120private: 121 JNIEnv* fEnv; 122 jshortArray fArray; 123 jshort* fPtr; 124 int fLen; 125}; 126 127class AutoJavaByteArray { 128public: 129 AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0); 130 ~AutoJavaByteArray(); 131 132 jbyte* ptr() const { return fPtr; } 133 int length() const { return fLen; } 134 135private: 136 JNIEnv* fEnv; 137 jbyteArray fArray; 138 jbyte* fPtr; 139 int fLen; 140}; 141 142void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL); 143void doThrowNPE(JNIEnv* env); 144void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception 145void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument 146void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime 147void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State 148void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory 149 150#define NPE_CHECK_RETURN_ZERO(env, object) \ 151 do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0) 152 153#define NPE_CHECK_RETURN_VOID(env, object) \ 154 do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0) 155 156#endif 157 158