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 "../images/SkImageDecoder.h"
      8 #include <jni.h>
      9 
     10 class SkCanvas;
     11 class SkPaint;
     12 class SkPicture;
     13 
     14 class GraphicsJNI {
     15 public:
     16     // returns true if an exception is set (and dumps it out to the Log)
     17     static bool hasException(JNIEnv*);
     18 
     19     static void get_jrect(JNIEnv*, jobject jrect, int* L, int* T, int* R, int* B);
     20     static void set_jrect(JNIEnv*, jobject jrect, int L, int T, int R, int B);
     21 
     22     static SkIRect* jrect_to_irect(JNIEnv*, jobject jrect, SkIRect*);
     23     static void irect_to_jrect(const SkIRect&, JNIEnv*, jobject jrect);
     24 
     25     static SkRect* jrectf_to_rect(JNIEnv*, jobject jrectf, SkRect*);
     26     static SkRect* jrect_to_rect(JNIEnv*, jobject jrect, SkRect*);
     27     static void rect_to_jrectf(const SkRect&, JNIEnv*, jobject jrectf);
     28 
     29     static void set_jpoint(JNIEnv*, jobject jrect, int x, int y);
     30 
     31     static SkIPoint* jpoint_to_ipoint(JNIEnv*, jobject jpoint, SkIPoint* point);
     32     static void ipoint_to_jpoint(const SkIPoint& point, JNIEnv*, jobject jpoint);
     33 
     34     static SkPoint* jpointf_to_point(JNIEnv*, jobject jpointf, SkPoint* point);
     35     static void point_to_jpointf(const SkPoint& point, JNIEnv*, jobject jpointf);
     36 
     37     static SkCanvas* getNativeCanvas(JNIEnv*, jobject canvas);
     38     static SkPaint*  getNativePaint(JNIEnv*, jobject paint);
     39     static SkBitmap* getNativeBitmap(JNIEnv*, jobject bitmap);
     40     static SkPicture* getNativePicture(JNIEnv*, jobject picture);
     41     static SkRegion* getNativeRegion(JNIEnv*, jobject region);
     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, int density = -1);
     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                                 bool reportSizeToVM);
     64 
     65     /** Copy the colors in colors[] to the bitmap, convert to the correct
     66         format along the way.
     67     */
     68     static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset,
     69                           int srcStride, int x, int y, int width, int height,
     70                           const SkBitmap& dstBitmap);
     71 };
     72 
     73 class JavaPixelAllocator : public SkBitmap::Allocator {
     74 public:
     75     JavaPixelAllocator(JNIEnv* env, bool reportSizeToVM);
     76     // overrides
     77     virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable);
     78 
     79 private:
     80     JavaVM* fVM;
     81     bool fReportSizeToVM;
     82 };
     83 
     84 class JavaMemoryUsageReporter : public SkVMMemoryReporter {
     85 public:
     86     JavaMemoryUsageReporter(JNIEnv* env);
     87     virtual ~JavaMemoryUsageReporter();
     88     // overrides
     89     virtual bool reportMemory(size_t memorySize);
     90 
     91 private:
     92     JavaVM* fVM;
     93     size_t fTotalSize;
     94 };
     95 
     96 enum JNIAccess {
     97     kRO_JNIAccess,
     98     kRW_JNIAccess
     99 };
    100 
    101 class AutoJavaFloatArray {
    102 public:
    103     AutoJavaFloatArray(JNIEnv* env, jfloatArray array,
    104                        int minLength = 0, JNIAccess = kRW_JNIAccess);
    105     ~AutoJavaFloatArray();
    106 
    107     float* ptr() const { return fPtr; }
    108     int    length() const { return fLen; }
    109 
    110 private:
    111     JNIEnv*     fEnv;
    112     jfloatArray fArray;
    113     float*      fPtr;
    114     int         fLen;
    115     int         fReleaseMode;
    116 };
    117 
    118 class AutoJavaIntArray {
    119 public:
    120     AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0);
    121     ~AutoJavaIntArray();
    122 
    123     jint* ptr() const { return fPtr; }
    124     int    length() const { return fLen; }
    125 
    126 private:
    127     JNIEnv*     fEnv;
    128     jintArray fArray;
    129     jint*      fPtr;
    130     int         fLen;
    131 };
    132 
    133 class AutoJavaShortArray {
    134 public:
    135     AutoJavaShortArray(JNIEnv* env, jshortArray array,
    136                        int minLength = 0, JNIAccess = kRW_JNIAccess);
    137     ~AutoJavaShortArray();
    138 
    139     jshort* ptr() const { return fPtr; }
    140     int    length() const { return fLen; }
    141 
    142 private:
    143     JNIEnv*     fEnv;
    144     jshortArray fArray;
    145     jshort*      fPtr;
    146     int         fLen;
    147     int         fReleaseMode;
    148 };
    149 
    150 class AutoJavaByteArray {
    151 public:
    152     AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0);
    153     ~AutoJavaByteArray();
    154 
    155     jbyte* ptr() const { return fPtr; }
    156     int    length() const { return fLen; }
    157 
    158 private:
    159     JNIEnv*     fEnv;
    160     jbyteArray fArray;
    161     jbyte*      fPtr;
    162     int         fLen;
    163 };
    164 
    165 void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL);
    166 void doThrowNPE(JNIEnv* env);
    167 void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception
    168 void doThrowIAE(JNIEnv* env, const char* msg = NULL);   // Illegal Argument
    169 void doThrowRE(JNIEnv* env, const char* msg = NULL);   // Runtime
    170 void doThrowISE(JNIEnv* env, const char* msg = NULL);   // Illegal State
    171 void doThrowOOME(JNIEnv* env, const char* msg = NULL);   // Out of memory
    172 void doThrowIOE(JNIEnv* env, const char* msg = NULL);   // IO Exception
    173 
    174 #define NPE_CHECK_RETURN_ZERO(env, object)    \
    175     do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0)
    176 
    177 #define NPE_CHECK_RETURN_VOID(env, object)    \
    178     do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0)
    179 
    180 #endif
    181