Home | History | Annotate | Download | only in graphics
      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 
      9 class SkCanvas;
     10 class SkPaint;
     11 class SkPicture;
     12 
     13 class GraphicsJNI {
     14 public:
     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 
     72 class JavaPixelAllocator : public SkBitmap::Allocator {
     73 public:
     74     JavaPixelAllocator(JNIEnv* env, bool reportSizeToVM);
     75     // overrides
     76     virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable);
     77 
     78 private:
     79     JNIEnv* fEnv;
     80     bool fReportSizeToVM;
     81 };
     82 
     83 enum JNIAccess {
     84     kRO_JNIAccess,
     85     kRW_JNIAccess
     86 };
     87 
     88 class AutoJavaFloatArray {
     89 public:
     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 
     97 private:
     98     JNIEnv*     fEnv;
     99     jfloatArray fArray;
    100     float*      fPtr;
    101     int         fLen;
    102     int         fReleaseMode;
    103 };
    104 
    105 class AutoJavaIntArray {
    106 public:
    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 
    113 private:
    114     JNIEnv*     fEnv;
    115     jintArray fArray;
    116     jint*      fPtr;
    117     int         fLen;
    118 };
    119 
    120 class AutoJavaShortArray {
    121 public:
    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 
    129 private:
    130     JNIEnv*     fEnv;
    131     jshortArray fArray;
    132     jshort*      fPtr;
    133     int         fLen;
    134     int         fReleaseMode;
    135 };
    136 
    137 class AutoJavaByteArray {
    138 public:
    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 
    145 private:
    146     JNIEnv*     fEnv;
    147     jbyteArray fArray;
    148     jbyte*      fPtr;
    149     int         fLen;
    150 };
    151 
    152 void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL);
    153 void doThrowNPE(JNIEnv* env);
    154 void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception
    155 void doThrowIAE(JNIEnv* env, const char* msg = NULL);   // Illegal Argument
    156 void doThrowRE(JNIEnv* env, const char* msg = NULL);   // Runtime
    157 void doThrowISE(JNIEnv* env, const char* msg = NULL);   // Illegal State
    158 void 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