blob: b58a740a4c2737f8c4b44815917479341b50e978 [file] [log] [blame]
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001#ifndef _ANDROID_GRAPHICS_GRAPHICS_JNI_H_
2#define _ANDROID_GRAPHICS_GRAPHICS_JNI_H_
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003
Derek Sollenberger2173ea22020-02-19 15:37:29 -05004#include <cutils/compiler.h>
5
John Reckf29ed282015-04-07 07:32:03 -07006#include "Bitmap.h"
Patrick Dubroye4ac2d62010-12-01 11:23:13 -08007#include "SkBitmap.h"
Matt Sarett1f979632015-10-27 10:33:20 -04008#include "SkBRDAllocator.h"
9#include "SkCodec.h"
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080010#include "SkPixelRef.h"
11#include "SkMallocPixelRef.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012#include "SkPoint.h"
13#include "SkRect.h"
Romain Guy253f2c22016-09-28 17:34:42 -070014#include "SkColorSpace.h"
sergeyvdccca442016-03-21 15:38:21 -070015#include <hwui/Canvas.h>
sergeyvc1c54062016-10-19 18:47:26 -070016#include <hwui/Bitmap.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017
Derek Sollenbergerc5882c42019-10-25 11:11:32 -040018#include "graphics_jni_helpers.h"
19
Matt Sarett1f979632015-10-27 10:33:20 -040020class SkBitmapRegionDecoder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021class SkCanvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
Raph Levien3d528c402014-06-26 09:04:54 -070023namespace android {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040024class Paint;
sergeyvbad99182016-03-17 11:24:22 -070025struct Typeface;
Raph Levien3d528c402014-06-26 09:04:54 -070026}
27
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028class GraphicsJNI {
29public:
Romain Guye8d2ebb2017-02-09 18:38:47 -080030 // This enum must keep these int values, to match the int values
31 // in the java Bitmap.Config enum.
32 enum LegacyBitmapConfig {
33 kNo_LegacyBitmapConfig = 0,
34 kA8_LegacyBitmapConfig = 1,
35 kIndex8_LegacyBitmapConfig = 2,
36 kRGB_565_LegacyBitmapConfig = 3,
37 kARGB_4444_LegacyBitmapConfig = 4,
38 kARGB_8888_LegacyBitmapConfig = 5,
39 kRGBA_16F_LegacyBitmapConfig = 6,
40 kHardware_LegacyBitmapConfig = 7,
41
42 kLastEnum_LegacyBitmapConfig = kHardware_LegacyBitmapConfig
43 };
44
Derek Sollenbergerc5882c42019-10-25 11:11:32 -040045 static void setJavaVM(JavaVM* javaVM);
46
47 /** returns a pointer to the JavaVM provided when we initialized the module */
48 static JavaVM* getJavaVM() { return mJavaVM; }
49
50 /** return a pointer to the JNIEnv for this thread */
51 static JNIEnv* getJNIEnv();
52
53 /** create a JNIEnv* for this thread or assert if one already exists */
54 static JNIEnv* attachJNIEnv(const char* envName);
55
56 /** detach the current thread from the JavaVM */
57 static void detachJNIEnv();
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 // returns true if an exception is set (and dumps it out to the Log)
60 static bool hasException(JNIEnv*);
61
62 static void get_jrect(JNIEnv*, jobject jrect, int* L, int* T, int* R, int* B);
63 static void set_jrect(JNIEnv*, jobject jrect, int L, int T, int R, int B);
64
65 static SkIRect* jrect_to_irect(JNIEnv*, jobject jrect, SkIRect*);
66 static void irect_to_jrect(const SkIRect&, JNIEnv*, jobject jrect);
67
68 static SkRect* jrectf_to_rect(JNIEnv*, jobject jrectf, SkRect*);
69 static SkRect* jrect_to_rect(JNIEnv*, jobject jrect, SkRect*);
70 static void rect_to_jrectf(const SkRect&, JNIEnv*, jobject jrectf);
Elliott Hughes8451b252011-04-07 19:17:57 -070071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 static void set_jpoint(JNIEnv*, jobject jrect, int x, int y);
Elliott Hughes8451b252011-04-07 19:17:57 -070073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 static SkIPoint* jpoint_to_ipoint(JNIEnv*, jobject jpoint, SkIPoint* point);
75 static void ipoint_to_jpoint(const SkIPoint& point, JNIEnv*, jobject jpoint);
Elliott Hughes8451b252011-04-07 19:17:57 -070076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 static SkPoint* jpointf_to_point(JNIEnv*, jobject jpointf, SkPoint* point);
78 static void point_to_jpointf(const SkPoint& point, JNIEnv*, jobject jpointf);
Elliott Hughes8451b252011-04-07 19:17:57 -070079
Derek Sollenberger2173ea22020-02-19 15:37:29 -050080 ANDROID_API static android::Canvas* getNativeCanvas(JNIEnv*, jobject canvas);
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050081 static android::Bitmap* getNativeBitmap(JNIEnv*, jobject bitmap);
Leon Scroggins III84a2afc2020-01-19 19:27:16 -050082 static SkImageInfo getBitmapInfo(JNIEnv*, jobject bitmap, uint32_t* outRowBytes,
83 bool* isHardware);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 static SkRegion* getNativeRegion(JNIEnv*, jobject region);
Elliott Hughes8451b252011-04-07 19:17:57 -070085
Mike Reed1103b322014-07-08 12:36:44 -040086 /*
87 * LegacyBitmapConfig is the old enum in Skia that matched the enum int values
88 * in Bitmap.Config. Skia no longer supports this config, but has replaced it
89 * with SkColorType. These routines convert between the two.
90 */
91 static SkColorType legacyBitmapConfigToColorType(jint legacyConfig);
92 static jint colorTypeToLegacyBitmapConfig(SkColorType colorType);
93
Mike Reed42a1d082014-07-07 18:06:18 -040094 /** Return the corresponding native colorType from the java Config enum,
95 or kUnknown_SkColorType if the java object is null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 */
Mike Reed42a1d082014-07-07 18:06:18 -040097 static SkColorType getNativeBitmapColorType(JNIEnv*, jobject jconfig);
Derek Sollenberger213daca2019-10-25 14:17:32 -040098 static AndroidBitmapFormat getFormatFromConfig(JNIEnv* env, jobject jconfig);
99 static jobject getConfigFromFormat(JNIEnv* env, AndroidBitmapFormat format);
Elliott Hughes8451b252011-04-07 19:17:57 -0700100
sergeyvda6c8ffc2016-11-22 18:28:54 -0800101 static bool isHardwareConfig(JNIEnv* env, jobject jconfig);
sergeyv19b4b012016-12-13 16:06:00 -0800102 static jint hardwareLegacyBitmapConfig();
sergeyvda6c8ffc2016-11-22 18:28:54 -0800103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 static jobject createRegion(JNIEnv* env, SkRegion* region);
105
Matt Sarett1f979632015-10-27 10:33:20 -0400106 static jobject createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap);
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800107
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500108 /**
109 * Given a bitmap we natively allocate a memory block to store the contents
110 * of that bitmap. The memory is then attached to the bitmap via an
111 * SkPixelRef, which ensures that upon deletion the appropriate caches
112 * are notified.
113 */
Mike Reed81397c42017-07-18 17:04:16 -0400114 static bool allocatePixels(JNIEnv* env, SkBitmap* bitmap);
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 /** Copy the colors in colors[] to the bitmap, convert to the correct
117 format along the way.
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400118 Whether to use premultiplied pixels is determined by dstBitmap's alphaType.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 */
120 static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset,
Chris Craik1abf5d62013-08-16 12:47:03 -0700121 int srcStride, int x, int y, int width, int height,
Brian Osman91c9c282018-08-17 16:57:15 -0400122 SkBitmap* dstBitmap);
Romain Guy253f2c22016-09-28 17:34:42 -0700123
Leon Scroggins III0e443d162018-12-19 11:38:35 -0500124 /**
125 * Convert the native SkColorSpace retrieved from ColorSpace.Rgb.getNativeInstance().
126 *
127 * This will never throw an Exception. If the ColorSpace is one that Skia cannot
128 * use, ColorSpace.Rgb.getNativeInstance() would have thrown an Exception. It may,
129 * however, be nullptr, which may be acceptable.
130 */
131 static sk_sp<SkColorSpace> getNativeColorSpace(jlong colorSpaceHandle);
Romain Guy95648b82017-04-13 18:43:42 -0700132
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500133 /**
134 * Return the android.graphics.ColorSpace Java object that corresponds to decodeColorSpace
135 * and decodeColorType.
136 *
137 * This may create a new object if none of the Named ColorSpaces match.
138 */
139 static jobject getColorSpace(JNIEnv* env, SkColorSpace* decodeColorSpace,
Romain Guy95648b82017-04-13 18:43:42 -0700140 SkColorType decodeColorType);
Leon Scroggins III94ba1002019-01-17 13:34:51 -0500141
142 /**
143 * Convert from a Java @ColorLong to an SkColor4f that Skia can use directly.
144 *
145 * This ignores the encoded ColorSpace, besides checking to see if it is sRGB,
146 * which is encoded differently. The color space should be passed down separately
147 * via ColorSpace#getNativeInstance(), and converted with getNativeColorSpace(),
148 * above.
149 */
150 static SkColor4f convertColorLong(jlong color);
Derek Sollenbergerc5882c42019-10-25 11:11:32 -0400151
152private:
153 /* JNI JavaVM pointer */
154 static JavaVM* mJavaVM;
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800155};
156
sergeyv45082182016-09-29 18:25:40 -0700157class HeapAllocator : public SkBRDAllocator {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158public:
sergeyvc69853c2016-10-07 14:14:09 -0700159 HeapAllocator() { };
160 ~HeapAllocator() { };
Elliott Hughes8451b252011-04-07 19:17:57 -0700161
Mike Reed81397c42017-07-18 17:04:16 -0400162 virtual bool allocPixelRef(SkBitmap* bitmap) override;
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800163
John Reckf29ed282015-04-07 07:32:03 -0700164 /**
165 * Fetches the backing allocation object. Must be called!
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800166 */
sergeyvc1c54062016-10-19 18:47:26 -0700167 android::Bitmap* getStorageObjAndReset() {
sergeyvc69853c2016-10-07 14:14:09 -0700168 return mStorage.release();
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800169 };
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800170
Matt Sarett1f979632015-10-27 10:33:20 -0400171 SkCodec::ZeroInitialized zeroInit() const override { return SkCodec::kYes_ZeroInitialized; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172private:
sergeyvc1c54062016-10-19 18:47:26 -0700173 sk_sp<android::Bitmap> mStorage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174};
175
Matt Sarett1f979632015-10-27 10:33:20 -0400176/**
177 * Allocator to handle reusing bitmaps for BitmapRegionDecoder.
178 *
179 * The BitmapRegionDecoder documentation states that, if it is
180 * provided, the recycled bitmap will always be reused, clipping
181 * the decoded output to fit in the recycled bitmap if necessary.
182 * This allocator implements that behavior.
183 *
184 * Skia's SkBitmapRegionDecoder expects the memory that
185 * is allocated to be large enough to decode the entire region
186 * that is requested. It will decode directly into the memory
187 * that is provided.
188 *
189 * FIXME: BUG:25465958
190 * If the recycled bitmap is not large enough for the decode
191 * requested, meaning that a clip is required, we will allocate
192 * enough memory for Skia to perform the decode, and then copy
193 * from the decoded output into the recycled bitmap.
194 *
195 * If the recycled bitmap is large enough for the decode requested,
196 * we will provide that memory for Skia to decode directly into.
197 *
198 * This allocator should only be used for a single allocation.
199 * After we reuse the recycledBitmap once, it is dangerous to
200 * reuse it again, given that it still may be in use from our
201 * first allocation.
202 */
203class RecyclingClippingPixelAllocator : public SkBRDAllocator {
204public:
205
sergeyvc1c54062016-10-19 18:47:26 -0700206 RecyclingClippingPixelAllocator(android::Bitmap* recycledBitmap,
Matt Sarett1f979632015-10-27 10:33:20 -0400207 size_t recycledBytes);
208
209 ~RecyclingClippingPixelAllocator();
210
Mike Reed81397c42017-07-18 17:04:16 -0400211 virtual bool allocPixelRef(SkBitmap* bitmap) override;
Matt Sarett1f979632015-10-27 10:33:20 -0400212
213 /**
214 * Must be called!
215 *
216 * In the event that the recycled bitmap is not large enough for
217 * the allocation requested, we will allocate memory on the heap
218 * instead. As a final step, once we are done using this memory,
219 * we will copy the contents of the heap memory into the recycled
220 * bitmap's memory, clipping as necessary.
221 */
222 void copyIfNecessary();
223
224 /**
225 * Indicates that this allocator does not allocate zero initialized
226 * memory.
227 */
228 SkCodec::ZeroInitialized zeroInit() const override { return SkCodec::kNo_ZeroInitialized; }
229
230private:
sergeyvc1c54062016-10-19 18:47:26 -0700231 android::Bitmap* mRecycledBitmap;
Matt Sarett1f979632015-10-27 10:33:20 -0400232 const size_t mRecycledBytes;
233 SkBitmap* mSkiaBitmap;
234 bool mNeedsCopy;
235};
236
Riley Andrews721ae5f2015-05-11 16:08:22 -0700237class AshmemPixelAllocator : public SkBitmap::Allocator {
238public:
Chih-Hung Hsieha6543282016-08-29 14:46:35 -0700239 explicit AshmemPixelAllocator(JNIEnv* env);
sergeyvc69853c2016-10-07 14:14:09 -0700240 ~AshmemPixelAllocator() { };
Mike Reed81397c42017-07-18 17:04:16 -0400241 virtual bool allocPixelRef(SkBitmap* bitmap);
sergeyvc1c54062016-10-19 18:47:26 -0700242 android::Bitmap* getStorageObjAndReset() {
sergeyvc69853c2016-10-07 14:14:09 -0700243 return mStorage.release();
Riley Andrews721ae5f2015-05-11 16:08:22 -0700244 };
245
246private:
247 JavaVM* mJavaVM;
sergeyvc1c54062016-10-19 18:47:26 -0700248 sk_sp<android::Bitmap> mStorage;
Riley Andrews721ae5f2015-05-11 16:08:22 -0700249};
250
251
Mike Reedc04851f2009-10-28 15:09:45 -0400252enum JNIAccess {
253 kRO_JNIAccess,
254 kRW_JNIAccess
255};
256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257class AutoJavaFloatArray {
258public:
Mike Reedc04851f2009-10-28 15:09:45 -0400259 AutoJavaFloatArray(JNIEnv* env, jfloatArray array,
260 int minLength = 0, JNIAccess = kRW_JNIAccess);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 ~AutoJavaFloatArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 float* ptr() const { return fPtr; }
264 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266private:
267 JNIEnv* fEnv;
268 jfloatArray fArray;
269 float* fPtr;
270 int fLen;
Mike Reedc04851f2009-10-28 15:09:45 -0400271 int fReleaseMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272};
273
274class AutoJavaIntArray {
275public:
276 AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0);
277 ~AutoJavaIntArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 jint* ptr() const { return fPtr; }
280 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282private:
283 JNIEnv* fEnv;
284 jintArray fArray;
285 jint* fPtr;
286 int fLen;
287};
288
289class AutoJavaShortArray {
290public:
Mike Reedc04851f2009-10-28 15:09:45 -0400291 AutoJavaShortArray(JNIEnv* env, jshortArray array,
292 int minLength = 0, JNIAccess = kRW_JNIAccess);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 ~AutoJavaShortArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 jshort* ptr() const { return fPtr; }
296 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298private:
299 JNIEnv* fEnv;
300 jshortArray fArray;
301 jshort* fPtr;
302 int fLen;
Mike Reedc04851f2009-10-28 15:09:45 -0400303 int fReleaseMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304};
305
306class AutoJavaByteArray {
307public:
308 AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0);
309 ~AutoJavaByteArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 jbyte* ptr() const { return fPtr; }
312 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314private:
315 JNIEnv* fEnv;
316 jbyteArray fArray;
317 jbyte* fPtr;
318 int fLen;
319};
320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321void doThrowNPE(JNIEnv* env);
322void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception
323void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument
324void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime
325void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State
326void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800327void doThrowIOE(JNIEnv* env, const char* msg = NULL); // IO Exception
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328
329#define NPE_CHECK_RETURN_ZERO(env, object) \
330 do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0)
331
332#define NPE_CHECK_RETURN_VOID(env, object) \
333 do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0)
334
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800335#endif // _ANDROID_GRAPHICS_GRAPHICS_JNI_H_