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