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, int density = -1); 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 83enum JNIAccess { 84 kRO_JNIAccess, 85 kRW_JNIAccess 86}; 87 88class AutoJavaFloatArray { 89public: 90 AutoJavaFloatArray(JNIEnv* env, jfloatArray array, 91 int minLength = 0, JNIAccess = kRW_JNIAccess); 92 ~AutoJavaFloatArray(); 93 94 float* ptr() const { return fPtr; } 95 int length() const { return fLen; } 96 97private: 98 JNIEnv* fEnv; 99 jfloatArray fArray; 100 float* fPtr; 101 int fLen; 102 int fReleaseMode; 103}; 104 105class AutoJavaIntArray { 106public: 107 AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0); 108 ~AutoJavaIntArray(); 109 110 jint* ptr() const { return fPtr; } 111 int length() const { return fLen; } 112 113private: 114 JNIEnv* fEnv; 115 jintArray fArray; 116 jint* fPtr; 117 int fLen; 118}; 119 120class AutoJavaShortArray { 121public: 122 AutoJavaShortArray(JNIEnv* env, jshortArray array, 123 int minLength = 0, JNIAccess = kRW_JNIAccess); 124 ~AutoJavaShortArray(); 125 126 jshort* ptr() const { return fPtr; } 127 int length() const { return fLen; } 128 129private: 130 JNIEnv* fEnv; 131 jshortArray fArray; 132 jshort* fPtr; 133 int fLen; 134 int fReleaseMode; 135}; 136 137class AutoJavaByteArray { 138public: 139 AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0); 140 ~AutoJavaByteArray(); 141 142 jbyte* ptr() const { return fPtr; } 143 int length() const { return fLen; } 144 145private: 146 JNIEnv* fEnv; 147 jbyteArray fArray; 148 jbyte* fPtr; 149 int fLen; 150}; 151 152void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL); 153void doThrowNPE(JNIEnv* env); 154void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception 155void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument 156void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime 157void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State 158void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory 159 160#define NPE_CHECK_RETURN_ZERO(env, object) \ 161 do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0) 162 163#define NPE_CHECK_RETURN_VOID(env, object) \ 164 do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0) 165 166#endif 167 168