blob: b9fff36d372efcc69effbfac7285bac10863e755 [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>
Nader Jawada3521852023-01-30 20:23:46 -08005#include <hwui/Bitmap.h>
6#include <hwui/Canvas.h>
Derek Sollenberger2173ea22020-02-19 15:37:29 -05007
John Reckcca989f2023-01-30 20:46:20 +00008#include "BRDAllocator.h"
Nader Jawada3521852023-01-30 20:23:46 -08009#include "Bitmap.h"
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080010#include "SkBitmap.h"
Matt Sarett1f979632015-10-27 10:33:20 -040011#include "SkCodec.h"
Nader Jawada3521852023-01-30 20:23:46 -080012#include "SkColorSpace.h"
John Reckcca989f2023-01-30 20:46:20 +000013#include "SkMallocPixelRef.h"
Nader Jawada3521852023-01-30 20:23:46 -080014#include "SkPixelRef.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015#include "SkPoint.h"
16#include "SkRect.h"
Derek Sollenbergerc5882c42019-10-25 11:11:32 -040017#include "graphics_jni_helpers.h"
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019class SkCanvas;
Seigo Nonaka1ed4f642020-09-10 17:19:34 -070020struct SkFontMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
Raph Levien3d528c402014-06-26 09:04:54 -070022namespace android {
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +000023class BitmapRegionDecoderWrapper;
John Reckbe671952021-01-13 22:39:32 -050024class Canvas;
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040025class Paint;
sergeyvbad99182016-03-17 11:24:22 -070026struct Typeface;
Raph Levien3d528c402014-06-26 09:04:54 -070027}
28
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029class GraphicsJNI {
30public:
Romain Guye8d2ebb2017-02-09 18:38:47 -080031 // This enum must keep these int values, to match the int values
32 // in the java Bitmap.Config enum.
33 enum LegacyBitmapConfig {
Alec Mouri1efd0a52022-01-20 13:58:23 -080034 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 Guye8d2ebb2017-02-09 18:38:47 -080043
Alec Mouri1efd0a52022-01-20 13:58:23 -080044 kLastEnum_LegacyBitmapConfig = kRGBA_1010102_LegacyBitmapConfig
Romain Guye8d2ebb2017-02-09 18:38:47 -080045 };
46
Derek Sollenbergerc5882c42019-10-25 11:11:32 -040047 static void setJavaVM(JavaVM* javaVM);
48
John Recka6b177b2023-02-24 17:25:18 -050049 /**
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 Sollenbergerc5882c42019-10-25 11:11:32 -040053 static JavaVM* getJavaVM() { return mJavaVM; }
54
John Recka6b177b2023-02-24 17:25:18 -050055 /**
56 * return a pointer to the JNIEnv for this thread
57 * DEPRECATED: Objects should know the JavaVM that created them
58 */
Derek Sollenbergerc5882c42019-10-25 11:11:32 -040059 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 Project9066cfe2009-03-03 19:31:44 -080067 // 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 Hughes8451b252011-04-07 19:17:57 -070079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 static void set_jpoint(JNIEnv*, jobject jrect, int x, int y);
Elliott Hughes8451b252011-04-07 19:17:57 -070081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 static SkIPoint* jpoint_to_ipoint(JNIEnv*, jobject jpoint, SkIPoint* point);
83 static void ipoint_to_jpoint(const SkIPoint& point, JNIEnv*, jobject jpoint);
Elliott Hughes8451b252011-04-07 19:17:57 -070084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 static SkPoint* jpointf_to_point(JNIEnv*, jobject jpointf, SkPoint* point);
86 static void point_to_jpointf(const SkPoint& point, JNIEnv*, jobject jpointf);
Elliott Hughes8451b252011-04-07 19:17:57 -070087
Derek Sollenberger2173ea22020-02-19 15:37:29 -050088 ANDROID_API static android::Canvas* getNativeCanvas(JNIEnv*, jobject canvas);
Derek Sollenberger6c41ab12019-11-08 08:50:58 -050089 static android::Bitmap* getNativeBitmap(JNIEnv*, jobject bitmap);
Leon Scroggins III84a2afc2020-01-19 19:27:16 -050090 static SkImageInfo getBitmapInfo(JNIEnv*, jobject bitmap, uint32_t* outRowBytes,
91 bool* isHardware);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 static SkRegion* getNativeRegion(JNIEnv*, jobject region);
Elliott Hughes8451b252011-04-07 19:17:57 -070093
Seigo Nonaka1ed4f642020-09-10 17:19:34 -070094 /**
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 Reed1103b322014-07-08 12:36:44 -0400105 /*
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 Reed42a1d082014-07-07 18:06:18 -0400113 /** Return the corresponding native colorType from the java Config enum,
114 or kUnknown_SkColorType if the java object is null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 */
Mike Reed42a1d082014-07-07 18:06:18 -0400116 static SkColorType getNativeBitmapColorType(JNIEnv*, jobject jconfig);
Derek Sollenberger213daca2019-10-25 14:17:32 -0400117 static AndroidBitmapFormat getFormatFromConfig(JNIEnv* env, jobject jconfig);
118 static jobject getConfigFromFormat(JNIEnv* env, AndroidBitmapFormat format);
Elliott Hughes8451b252011-04-07 19:17:57 -0700119
sergeyvda6c8ffc2016-11-22 18:28:54 -0800120 static bool isHardwareConfig(JNIEnv* env, jobject jconfig);
sergeyv19b4b012016-12-13 16:06:00 -0800121 static jint hardwareLegacyBitmapConfig();
sergeyvda6c8ffc2016-11-22 18:28:54 -0800122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 static jobject createRegion(JNIEnv* env, SkRegion* region);
124
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400125 static jobject createBitmapRegionDecoder(JNIEnv* env,
Fyodor Kyslovb0da4a52023-01-26 18:39:33 +0000126 android::BitmapRegionDecoderWrapper* bitmap);
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 /** Copy the colors in colors[] to the bitmap, convert to the correct
129 format along the way.
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400130 Whether to use premultiplied pixels is determined by dstBitmap's alphaType.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 */
132 static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset,
Chris Craik1abf5d62013-08-16 12:47:03 -0700133 int srcStride, int x, int y, int width, int height,
Brian Osman91c9c282018-08-17 16:57:15 -0400134 SkBitmap* dstBitmap);
Romain Guy253f2c22016-09-28 17:34:42 -0700135
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500136 /**
137 * Convert the native SkColorSpace retrieved from ColorSpace.Rgb.getNativeInstance().
138 *
139 * This will never throw an Exception. If the ColorSpace is one that Skia cannot
140 * use, ColorSpace.Rgb.getNativeInstance() would have thrown an Exception. It may,
141 * however, be nullptr, which may be acceptable.
142 */
143 static sk_sp<SkColorSpace> getNativeColorSpace(jlong colorSpaceHandle);
Romain Guy95648b82017-04-13 18:43:42 -0700144
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500145 /**
146 * Return the android.graphics.ColorSpace Java object that corresponds to decodeColorSpace
147 * and decodeColorType.
148 *
149 * This may create a new object if none of the Named ColorSpaces match.
150 */
151 static jobject getColorSpace(JNIEnv* env, SkColorSpace* decodeColorSpace,
Romain Guy95648b82017-04-13 18:43:42 -0700152 SkColorType decodeColorType);
Leon Scroggins III94ba1002019-01-17 13:34:51 -0500153
154 /**
155 * Convert from a Java @ColorLong to an SkColor4f that Skia can use directly.
156 *
157 * This ignores the encoded ColorSpace, besides checking to see if it is sRGB,
158 * which is encoded differently. The color space should be passed down separately
159 * via ColorSpace#getNativeInstance(), and converted with getNativeColorSpace(),
160 * above.
161 */
162 static SkColor4f convertColorLong(jlong color);
Derek Sollenbergerc5882c42019-10-25 11:11:32 -0400163
164private:
165 /* JNI JavaVM pointer */
166 static JavaVM* mJavaVM;
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800167};
168
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400169class HeapAllocator : public android::skia::BRDAllocator {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170public:
sergeyvc69853c2016-10-07 14:14:09 -0700171 HeapAllocator() { };
172 ~HeapAllocator() { };
Elliott Hughes8451b252011-04-07 19:17:57 -0700173
Mike Reed81397c42017-07-18 17:04:16 -0400174 virtual bool allocPixelRef(SkBitmap* bitmap) override;
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800175
John Reckf29ed282015-04-07 07:32:03 -0700176 /**
177 * Fetches the backing allocation object. Must be called!
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800178 */
sergeyvc1c54062016-10-19 18:47:26 -0700179 android::Bitmap* getStorageObjAndReset() {
sergeyvc69853c2016-10-07 14:14:09 -0700180 return mStorage.release();
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800181 };
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800182
Matt Sarett1f979632015-10-27 10:33:20 -0400183 SkCodec::ZeroInitialized zeroInit() const override { return SkCodec::kYes_ZeroInitialized; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184private:
sergeyvc1c54062016-10-19 18:47:26 -0700185 sk_sp<android::Bitmap> mStorage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186};
187
Matt Sarett1f979632015-10-27 10:33:20 -0400188/**
189 * Allocator to handle reusing bitmaps for BitmapRegionDecoder.
190 *
191 * The BitmapRegionDecoder documentation states that, if it is
192 * provided, the recycled bitmap will always be reused, clipping
193 * the decoded output to fit in the recycled bitmap if necessary.
194 * This allocator implements that behavior.
195 *
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400196 * Skia's BitmapRegionDecoder expects the memory that
Matt Sarett1f979632015-10-27 10:33:20 -0400197 * is allocated to be large enough to decode the entire region
198 * that is requested. It will decode directly into the memory
199 * that is provided.
200 *
201 * FIXME: BUG:25465958
202 * If the recycled bitmap is not large enough for the decode
203 * requested, meaning that a clip is required, we will allocate
204 * enough memory for Skia to perform the decode, and then copy
205 * from the decoded output into the recycled bitmap.
206 *
207 * If the recycled bitmap is large enough for the decode requested,
208 * we will provide that memory for Skia to decode directly into.
209 *
210 * This allocator should only be used for a single allocation.
211 * After we reuse the recycledBitmap once, it is dangerous to
212 * reuse it again, given that it still may be in use from our
213 * first allocation.
214 */
Leon Scroggins III23ac0362020-05-04 15:38:58 -0400215class RecyclingClippingPixelAllocator : public android::skia::BRDAllocator {
Matt Sarett1f979632015-10-27 10:33:20 -0400216public:
sergeyvc1c54062016-10-19 18:47:26 -0700217 RecyclingClippingPixelAllocator(android::Bitmap* recycledBitmap,
John Reck40ffc1d2023-06-20 17:26:09 -0400218 bool mustMatchColorType = true);
Matt Sarett1f979632015-10-27 10:33:20 -0400219
220 ~RecyclingClippingPixelAllocator();
221
Mike Reed81397c42017-07-18 17:04:16 -0400222 virtual bool allocPixelRef(SkBitmap* bitmap) override;
Matt Sarett1f979632015-10-27 10:33:20 -0400223
224 /**
225 * Must be called!
226 *
227 * In the event that the recycled bitmap is not large enough for
228 * the allocation requested, we will allocate memory on the heap
229 * instead. As a final step, once we are done using this memory,
230 * we will copy the contents of the heap memory into the recycled
231 * bitmap's memory, clipping as necessary.
232 */
233 void copyIfNecessary();
234
235 /**
236 * Indicates that this allocator does not allocate zero initialized
237 * memory.
238 */
239 SkCodec::ZeroInitialized zeroInit() const override { return SkCodec::kNo_ZeroInitialized; }
240
241private:
sergeyvc1c54062016-10-19 18:47:26 -0700242 android::Bitmap* mRecycledBitmap;
Matt Sarett1f979632015-10-27 10:33:20 -0400243 const size_t mRecycledBytes;
244 SkBitmap* mSkiaBitmap;
245 bool mNeedsCopy;
John Reck40ffc1d2023-06-20 17:26:09 -0400246 const bool mMustMatchColorType;
Matt Sarett1f979632015-10-27 10:33:20 -0400247};
248
Riley Andrews721ae5f2015-05-11 16:08:22 -0700249class AshmemPixelAllocator : public SkBitmap::Allocator {
250public:
Chih-Hung Hsieha6543282016-08-29 14:46:35 -0700251 explicit AshmemPixelAllocator(JNIEnv* env);
sergeyvc69853c2016-10-07 14:14:09 -0700252 ~AshmemPixelAllocator() { };
Mike Reed81397c42017-07-18 17:04:16 -0400253 virtual bool allocPixelRef(SkBitmap* bitmap);
sergeyvc1c54062016-10-19 18:47:26 -0700254 android::Bitmap* getStorageObjAndReset() {
sergeyvc69853c2016-10-07 14:14:09 -0700255 return mStorage.release();
Riley Andrews721ae5f2015-05-11 16:08:22 -0700256 };
257
258private:
259 JavaVM* mJavaVM;
sergeyvc1c54062016-10-19 18:47:26 -0700260 sk_sp<android::Bitmap> mStorage;
Riley Andrews721ae5f2015-05-11 16:08:22 -0700261};
262
263
Mike Reedc04851f2009-10-28 15:09:45 -0400264enum JNIAccess {
265 kRO_JNIAccess,
266 kRW_JNIAccess
267};
268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269class AutoJavaFloatArray {
270public:
Mike Reedc04851f2009-10-28 15:09:45 -0400271 AutoJavaFloatArray(JNIEnv* env, jfloatArray array,
272 int minLength = 0, JNIAccess = kRW_JNIAccess);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 ~AutoJavaFloatArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 float* ptr() const { return fPtr; }
276 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278private:
279 JNIEnv* fEnv;
280 jfloatArray fArray;
281 float* fPtr;
282 int fLen;
Mike Reedc04851f2009-10-28 15:09:45 -0400283 int fReleaseMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284};
285
286class AutoJavaIntArray {
287public:
288 AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0);
289 ~AutoJavaIntArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 jint* ptr() const { return fPtr; }
292 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294private:
295 JNIEnv* fEnv;
296 jintArray fArray;
297 jint* fPtr;
298 int fLen;
299};
300
301class AutoJavaShortArray {
302public:
Mike Reedc04851f2009-10-28 15:09:45 -0400303 AutoJavaShortArray(JNIEnv* env, jshortArray array,
304 int minLength = 0, JNIAccess = kRW_JNIAccess);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 ~AutoJavaShortArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 jshort* ptr() const { return fPtr; }
308 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310private:
311 JNIEnv* fEnv;
312 jshortArray fArray;
313 jshort* fPtr;
314 int fLen;
Mike Reedc04851f2009-10-28 15:09:45 -0400315 int fReleaseMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316};
317
318class AutoJavaByteArray {
319public:
320 AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0);
321 ~AutoJavaByteArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 jbyte* ptr() const { return fPtr; }
324 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326private:
327 JNIEnv* fEnv;
328 jbyteArray fArray;
329 jbyte* fPtr;
330 int fLen;
331};
332
Nader Jawada3521852023-01-30 20:23:46 -0800333class JGlobalRefHolder {
334public:
335 JGlobalRefHolder(JavaVM* vm, jobject object) : mVm(vm), mObject(object) {}
336
337 virtual ~JGlobalRefHolder() {
John Recka6b177b2023-02-24 17:25:18 -0500338 env()->DeleteGlobalRef(mObject);
Nader Jawada3521852023-01-30 20:23:46 -0800339 mObject = nullptr;
340 }
341
342 jobject object() { return mObject; }
343 JavaVM* vm() { return mVm; }
344
John Recka6b177b2023-02-24 17:25:18 -0500345 JNIEnv* env() {
346 JNIEnv* env;
347 if (mVm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
348 LOG_ALWAYS_FATAL("Failed to get JNIEnv for JavaVM: %p", mVm);
349 }
350 return env;
351 }
352
Nader Jawada3521852023-01-30 20:23:46 -0800353private:
354 JGlobalRefHolder(const JGlobalRefHolder&) = delete;
355 void operator=(const JGlobalRefHolder&) = delete;
356
357 JavaVM* mVm;
358 jobject mObject;
359};
360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361void doThrowNPE(JNIEnv* env);
362void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception
363void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument
364void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime
365void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State
366void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800367void doThrowIOE(JNIEnv* env, const char* msg = NULL); // IO Exception
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368
369#define NPE_CHECK_RETURN_ZERO(env, object) \
370 do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0)
371
372#define NPE_CHECK_RETURN_VOID(env, object) \
373 do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0)
374
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800375#endif // _ANDROID_GRAPHICS_GRAPHICS_JNI_H_