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