Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1 | #ifndef _ANDROID_GRAPHICS_GRAPHICS_JNI_H_ |
| 2 | #define _ANDROID_GRAPHICS_GRAPHICS_JNI_H_ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | |
Derek Sollenberger | 2173ea2 | 2020-02-19 15:37:29 -0500 | [diff] [blame] | 4 | #include <cutils/compiler.h> |
| 5 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 6 | #include "Bitmap.h" |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 7 | #include "BRDAllocator.h" |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 8 | #include "SkBitmap.h" |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 9 | #include "SkCodec.h" |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 10 | #include "SkPixelRef.h" |
| 11 | #include "SkMallocPixelRef.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12 | #include "SkPoint.h" |
| 13 | #include "SkRect.h" |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 14 | #include "SkColorSpace.h" |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 15 | #include <hwui/Canvas.h> |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 16 | #include <hwui/Bitmap.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 18 | #include "graphics_jni_helpers.h" |
| 19 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | class SkCanvas; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
Raph Levien | 3d528c40 | 2014-06-26 09:04:54 -0700 | [diff] [blame] | 22 | namespace android { |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 23 | namespace skia { |
| 24 | class BitmapRegionDecoder; |
| 25 | } |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 26 | class Paint; |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 27 | struct Typeface; |
Raph Levien | 3d528c40 | 2014-06-26 09:04:54 -0700 | [diff] [blame] | 28 | } |
| 29 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | class GraphicsJNI { |
| 31 | public: |
Romain Guy | e8d2ebb | 2017-02-09 18:38:47 -0800 | [diff] [blame] | 32 | // This enum must keep these int values, to match the int values |
| 33 | // in the java Bitmap.Config enum. |
| 34 | enum LegacyBitmapConfig { |
| 35 | kNo_LegacyBitmapConfig = 0, |
| 36 | kA8_LegacyBitmapConfig = 1, |
| 37 | kIndex8_LegacyBitmapConfig = 2, |
| 38 | kRGB_565_LegacyBitmapConfig = 3, |
| 39 | kARGB_4444_LegacyBitmapConfig = 4, |
| 40 | kARGB_8888_LegacyBitmapConfig = 5, |
| 41 | kRGBA_16F_LegacyBitmapConfig = 6, |
| 42 | kHardware_LegacyBitmapConfig = 7, |
| 43 | |
| 44 | kLastEnum_LegacyBitmapConfig = kHardware_LegacyBitmapConfig |
| 45 | }; |
| 46 | |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 47 | static void setJavaVM(JavaVM* javaVM); |
| 48 | |
| 49 | /** returns a pointer to the JavaVM provided when we initialized the module */ |
| 50 | static JavaVM* getJavaVM() { return mJavaVM; } |
| 51 | |
| 52 | /** return a pointer to the JNIEnv for this thread */ |
| 53 | static JNIEnv* getJNIEnv(); |
| 54 | |
| 55 | /** create a JNIEnv* for this thread or assert if one already exists */ |
| 56 | static JNIEnv* attachJNIEnv(const char* envName); |
| 57 | |
| 58 | /** detach the current thread from the JavaVM */ |
| 59 | static void detachJNIEnv(); |
| 60 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | // returns true if an exception is set (and dumps it out to the Log) |
| 62 | static bool hasException(JNIEnv*); |
| 63 | |
| 64 | static void get_jrect(JNIEnv*, jobject jrect, int* L, int* T, int* R, int* B); |
| 65 | static void set_jrect(JNIEnv*, jobject jrect, int L, int T, int R, int B); |
| 66 | |
| 67 | static SkIRect* jrect_to_irect(JNIEnv*, jobject jrect, SkIRect*); |
| 68 | static void irect_to_jrect(const SkIRect&, JNIEnv*, jobject jrect); |
| 69 | |
| 70 | static SkRect* jrectf_to_rect(JNIEnv*, jobject jrectf, SkRect*); |
| 71 | static SkRect* jrect_to_rect(JNIEnv*, jobject jrect, SkRect*); |
| 72 | static void rect_to_jrectf(const SkRect&, JNIEnv*, jobject jrectf); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 73 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | static void set_jpoint(JNIEnv*, jobject jrect, int x, int y); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 75 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | static SkIPoint* jpoint_to_ipoint(JNIEnv*, jobject jpoint, SkIPoint* point); |
| 77 | static void ipoint_to_jpoint(const SkIPoint& point, JNIEnv*, jobject jpoint); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | static SkPoint* jpointf_to_point(JNIEnv*, jobject jpointf, SkPoint* point); |
| 80 | static void point_to_jpointf(const SkPoint& point, JNIEnv*, jobject jpointf); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 81 | |
Derek Sollenberger | 2173ea2 | 2020-02-19 15:37:29 -0500 | [diff] [blame] | 82 | ANDROID_API static android::Canvas* getNativeCanvas(JNIEnv*, jobject canvas); |
Derek Sollenberger | 6c41ab1 | 2019-11-08 08:50:58 -0500 | [diff] [blame] | 83 | static android::Bitmap* getNativeBitmap(JNIEnv*, jobject bitmap); |
Leon Scroggins III | 84a2afc | 2020-01-19 19:27:16 -0500 | [diff] [blame] | 84 | static SkImageInfo getBitmapInfo(JNIEnv*, jobject bitmap, uint32_t* outRowBytes, |
| 85 | bool* isHardware); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | static SkRegion* getNativeRegion(JNIEnv*, jobject region); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 87 | |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 88 | /* |
| 89 | * LegacyBitmapConfig is the old enum in Skia that matched the enum int values |
| 90 | * in Bitmap.Config. Skia no longer supports this config, but has replaced it |
| 91 | * with SkColorType. These routines convert between the two. |
| 92 | */ |
| 93 | static SkColorType legacyBitmapConfigToColorType(jint legacyConfig); |
| 94 | static jint colorTypeToLegacyBitmapConfig(SkColorType colorType); |
| 95 | |
Mike Reed | 42a1d08 | 2014-07-07 18:06:18 -0400 | [diff] [blame] | 96 | /** Return the corresponding native colorType from the java Config enum, |
| 97 | or kUnknown_SkColorType if the java object is null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | */ |
Mike Reed | 42a1d08 | 2014-07-07 18:06:18 -0400 | [diff] [blame] | 99 | static SkColorType getNativeBitmapColorType(JNIEnv*, jobject jconfig); |
Derek Sollenberger | 213daca | 2019-10-25 14:17:32 -0400 | [diff] [blame] | 100 | static AndroidBitmapFormat getFormatFromConfig(JNIEnv* env, jobject jconfig); |
| 101 | static jobject getConfigFromFormat(JNIEnv* env, AndroidBitmapFormat format); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 102 | |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 103 | static bool isHardwareConfig(JNIEnv* env, jobject jconfig); |
sergeyv | 19b4b01 | 2016-12-13 16:06:00 -0800 | [diff] [blame] | 104 | static jint hardwareLegacyBitmapConfig(); |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 105 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | static jobject createRegion(JNIEnv* env, SkRegion* region); |
| 107 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 108 | static jobject createBitmapRegionDecoder(JNIEnv* env, |
| 109 | android::skia::BitmapRegionDecoder* bitmap); |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 110 | |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 111 | /** |
| 112 | * Given a bitmap we natively allocate a memory block to store the contents |
| 113 | * of that bitmap. The memory is then attached to the bitmap via an |
| 114 | * SkPixelRef, which ensures that upon deletion the appropriate caches |
| 115 | * are notified. |
| 116 | */ |
Mike Reed | 81397c4 | 2017-07-18 17:04:16 -0400 | [diff] [blame] | 117 | static bool allocatePixels(JNIEnv* env, SkBitmap* bitmap); |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 118 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | /** Copy the colors in colors[] to the bitmap, convert to the correct |
| 120 | format along the way. |
Leon Scroggins III | 57ee620 | 2014-06-04 18:51:07 -0400 | [diff] [blame] | 121 | Whether to use premultiplied pixels is determined by dstBitmap's alphaType. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | */ |
| 123 | static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset, |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 124 | int srcStride, int x, int y, int width, int height, |
Brian Osman | 91c9c28 | 2018-08-17 16:57:15 -0400 | [diff] [blame] | 125 | SkBitmap* dstBitmap); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 126 | |
Leon Scroggins III | 0e443d16 | 2018-12-19 11:38:35 -0500 | [diff] [blame] | 127 | /** |
| 128 | * Convert the native SkColorSpace retrieved from ColorSpace.Rgb.getNativeInstance(). |
| 129 | * |
| 130 | * This will never throw an Exception. If the ColorSpace is one that Skia cannot |
| 131 | * use, ColorSpace.Rgb.getNativeInstance() would have thrown an Exception. It may, |
| 132 | * however, be nullptr, which may be acceptable. |
| 133 | */ |
| 134 | static sk_sp<SkColorSpace> getNativeColorSpace(jlong colorSpaceHandle); |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 135 | |
Derek Sollenberger | bf3e464 | 2019-01-30 11:28:27 -0500 | [diff] [blame] | 136 | /** |
| 137 | * Return the android.graphics.ColorSpace Java object that corresponds to decodeColorSpace |
| 138 | * and decodeColorType. |
| 139 | * |
| 140 | * This may create a new object if none of the Named ColorSpaces match. |
| 141 | */ |
| 142 | static jobject getColorSpace(JNIEnv* env, SkColorSpace* decodeColorSpace, |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 143 | SkColorType decodeColorType); |
Leon Scroggins III | 94ba100 | 2019-01-17 13:34:51 -0500 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Convert from a Java @ColorLong to an SkColor4f that Skia can use directly. |
| 147 | * |
| 148 | * This ignores the encoded ColorSpace, besides checking to see if it is sRGB, |
| 149 | * which is encoded differently. The color space should be passed down separately |
| 150 | * via ColorSpace#getNativeInstance(), and converted with getNativeColorSpace(), |
| 151 | * above. |
| 152 | */ |
| 153 | static SkColor4f convertColorLong(jlong color); |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 154 | |
| 155 | private: |
| 156 | /* JNI JavaVM pointer */ |
| 157 | static JavaVM* mJavaVM; |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 158 | }; |
| 159 | |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 160 | class HeapAllocator : public android::skia::BRDAllocator { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | public: |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 162 | HeapAllocator() { }; |
| 163 | ~HeapAllocator() { }; |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 164 | |
Mike Reed | 81397c4 | 2017-07-18 17:04:16 -0400 | [diff] [blame] | 165 | virtual bool allocPixelRef(SkBitmap* bitmap) override; |
Patrick Dubroy | afde46e | 2010-12-15 11:52:01 -0800 | [diff] [blame] | 166 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Fetches the backing allocation object. Must be called! |
Patrick Dubroy | afde46e | 2010-12-15 11:52:01 -0800 | [diff] [blame] | 169 | */ |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 170 | android::Bitmap* getStorageObjAndReset() { |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 171 | return mStorage.release(); |
Patrick Dubroy | afde46e | 2010-12-15 11:52:01 -0800 | [diff] [blame] | 172 | }; |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 173 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 174 | SkCodec::ZeroInitialized zeroInit() const override { return SkCodec::kYes_ZeroInitialized; } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | private: |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 176 | sk_sp<android::Bitmap> mStorage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | }; |
| 178 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 179 | /** |
| 180 | * Allocator to handle reusing bitmaps for BitmapRegionDecoder. |
| 181 | * |
| 182 | * The BitmapRegionDecoder documentation states that, if it is |
| 183 | * provided, the recycled bitmap will always be reused, clipping |
| 184 | * the decoded output to fit in the recycled bitmap if necessary. |
| 185 | * This allocator implements that behavior. |
| 186 | * |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 187 | * Skia's BitmapRegionDecoder expects the memory that |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 188 | * is allocated to be large enough to decode the entire region |
| 189 | * that is requested. It will decode directly into the memory |
| 190 | * that is provided. |
| 191 | * |
| 192 | * FIXME: BUG:25465958 |
| 193 | * If the recycled bitmap is not large enough for the decode |
| 194 | * requested, meaning that a clip is required, we will allocate |
| 195 | * enough memory for Skia to perform the decode, and then copy |
| 196 | * from the decoded output into the recycled bitmap. |
| 197 | * |
| 198 | * If the recycled bitmap is large enough for the decode requested, |
| 199 | * we will provide that memory for Skia to decode directly into. |
| 200 | * |
| 201 | * This allocator should only be used for a single allocation. |
| 202 | * After we reuse the recycledBitmap once, it is dangerous to |
| 203 | * reuse it again, given that it still may be in use from our |
| 204 | * first allocation. |
| 205 | */ |
Leon Scroggins III | 23ac036 | 2020-05-04 15:38:58 -0400 | [diff] [blame] | 206 | class RecyclingClippingPixelAllocator : public android::skia::BRDAllocator { |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 207 | public: |
| 208 | |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 209 | RecyclingClippingPixelAllocator(android::Bitmap* recycledBitmap, |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 210 | size_t recycledBytes); |
| 211 | |
| 212 | ~RecyclingClippingPixelAllocator(); |
| 213 | |
Mike Reed | 81397c4 | 2017-07-18 17:04:16 -0400 | [diff] [blame] | 214 | virtual bool allocPixelRef(SkBitmap* bitmap) override; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * Must be called! |
| 218 | * |
| 219 | * In the event that the recycled bitmap is not large enough for |
| 220 | * the allocation requested, we will allocate memory on the heap |
| 221 | * instead. As a final step, once we are done using this memory, |
| 222 | * we will copy the contents of the heap memory into the recycled |
| 223 | * bitmap's memory, clipping as necessary. |
| 224 | */ |
| 225 | void copyIfNecessary(); |
| 226 | |
| 227 | /** |
| 228 | * Indicates that this allocator does not allocate zero initialized |
| 229 | * memory. |
| 230 | */ |
| 231 | SkCodec::ZeroInitialized zeroInit() const override { return SkCodec::kNo_ZeroInitialized; } |
| 232 | |
| 233 | private: |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 234 | android::Bitmap* mRecycledBitmap; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 235 | const size_t mRecycledBytes; |
| 236 | SkBitmap* mSkiaBitmap; |
| 237 | bool mNeedsCopy; |
| 238 | }; |
| 239 | |
Riley Andrews | 721ae5f | 2015-05-11 16:08:22 -0700 | [diff] [blame] | 240 | class AshmemPixelAllocator : public SkBitmap::Allocator { |
| 241 | public: |
Chih-Hung Hsieh | a654328 | 2016-08-29 14:46:35 -0700 | [diff] [blame] | 242 | explicit AshmemPixelAllocator(JNIEnv* env); |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 243 | ~AshmemPixelAllocator() { }; |
Mike Reed | 81397c4 | 2017-07-18 17:04:16 -0400 | [diff] [blame] | 244 | virtual bool allocPixelRef(SkBitmap* bitmap); |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 245 | android::Bitmap* getStorageObjAndReset() { |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 246 | return mStorage.release(); |
Riley Andrews | 721ae5f | 2015-05-11 16:08:22 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | private: |
| 250 | JavaVM* mJavaVM; |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 251 | sk_sp<android::Bitmap> mStorage; |
Riley Andrews | 721ae5f | 2015-05-11 16:08:22 -0700 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 255 | enum JNIAccess { |
| 256 | kRO_JNIAccess, |
| 257 | kRW_JNIAccess |
| 258 | }; |
| 259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | class AutoJavaFloatArray { |
| 261 | public: |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 262 | AutoJavaFloatArray(JNIEnv* env, jfloatArray array, |
| 263 | int minLength = 0, JNIAccess = kRW_JNIAccess); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | ~AutoJavaFloatArray(); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 265 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | float* ptr() const { return fPtr; } |
| 267 | int length() const { return fLen; } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 268 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | private: |
| 270 | JNIEnv* fEnv; |
| 271 | jfloatArray fArray; |
| 272 | float* fPtr; |
| 273 | int fLen; |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 274 | int fReleaseMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | class AutoJavaIntArray { |
| 278 | public: |
| 279 | AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0); |
| 280 | ~AutoJavaIntArray(); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 281 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | jint* ptr() const { return fPtr; } |
| 283 | int length() const { return fLen; } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 284 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | private: |
| 286 | JNIEnv* fEnv; |
| 287 | jintArray fArray; |
| 288 | jint* fPtr; |
| 289 | int fLen; |
| 290 | }; |
| 291 | |
| 292 | class AutoJavaShortArray { |
| 293 | public: |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 294 | AutoJavaShortArray(JNIEnv* env, jshortArray array, |
| 295 | int minLength = 0, JNIAccess = kRW_JNIAccess); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | ~AutoJavaShortArray(); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 297 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | jshort* ptr() const { return fPtr; } |
| 299 | int length() const { return fLen; } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 300 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | private: |
| 302 | JNIEnv* fEnv; |
| 303 | jshortArray fArray; |
| 304 | jshort* fPtr; |
| 305 | int fLen; |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 306 | int fReleaseMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | }; |
| 308 | |
| 309 | class AutoJavaByteArray { |
| 310 | public: |
| 311 | AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0); |
| 312 | ~AutoJavaByteArray(); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 313 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | jbyte* ptr() const { return fPtr; } |
| 315 | int length() const { return fLen; } |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 316 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | private: |
| 318 | JNIEnv* fEnv; |
| 319 | jbyteArray fArray; |
| 320 | jbyte* fPtr; |
| 321 | int fLen; |
| 322 | }; |
| 323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | void doThrowNPE(JNIEnv* env); |
| 325 | void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception |
| 326 | void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument |
| 327 | void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime |
| 328 | void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State |
| 329 | void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 330 | void doThrowIOE(JNIEnv* env, const char* msg = NULL); // IO Exception |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | |
| 332 | #define NPE_CHECK_RETURN_ZERO(env, object) \ |
| 333 | do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0) |
| 334 | |
| 335 | #define NPE_CHECK_RETURN_VOID(env, object) \ |
| 336 | do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0) |
| 337 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 338 | #endif // _ANDROID_GRAPHICS_GRAPHICS_JNI_H_ |