blob: 422d3f1a66469c5eb142b1398a2c64010e8fd32b [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
Patrick Dubroye4ac2d62010-12-01 11:23:13 -08004#include "SkBitmap.h"
5#include "SkDevice.h"
6#include "SkPixelRef.h"
7#include "SkMallocPixelRef.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008#include "SkPoint.h"
9#include "SkRect.h"
Derek Sollenberger5827cb52013-07-26 14:58:06 -040010#include "SkImageDecoder.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011#include <jni.h>
12
Derek Sollenberger5827cb52013-07-26 14:58:06 -040013class SkBitmapRegionDecoder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014class SkCanvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015
Raph Levien3d528c402014-06-26 09:04:54 -070016namespace android {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040017class Paint;
Dan Albert46d84442014-11-18 16:07:51 -080018struct TypefaceImpl;
Raph Levien3d528c402014-06-26 09:04:54 -070019}
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021class GraphicsJNI {
22public:
Chris Craik1abf5d62013-08-16 12:47:03 -070023 enum BitmapCreateFlags {
24 kBitmapCreateFlag_None = 0x0,
25 kBitmapCreateFlag_Mutable = 0x1,
26 kBitmapCreateFlag_Premultiplied = 0x2,
27 };
28
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 // returns true if an exception is set (and dumps it out to the Log)
30 static bool hasException(JNIEnv*);
31
32 static void get_jrect(JNIEnv*, jobject jrect, int* L, int* T, int* R, int* B);
33 static void set_jrect(JNIEnv*, jobject jrect, int L, int T, int R, int B);
34
35 static SkIRect* jrect_to_irect(JNIEnv*, jobject jrect, SkIRect*);
36 static void irect_to_jrect(const SkIRect&, JNIEnv*, jobject jrect);
37
38 static SkRect* jrectf_to_rect(JNIEnv*, jobject jrectf, SkRect*);
39 static SkRect* jrect_to_rect(JNIEnv*, jobject jrect, SkRect*);
40 static void rect_to_jrectf(const SkRect&, JNIEnv*, jobject jrectf);
Elliott Hughes8451b252011-04-07 19:17:57 -070041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 static void set_jpoint(JNIEnv*, jobject jrect, int x, int y);
Elliott Hughes8451b252011-04-07 19:17:57 -070043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 static SkIPoint* jpoint_to_ipoint(JNIEnv*, jobject jpoint, SkIPoint* point);
45 static void ipoint_to_jpoint(const SkIPoint& point, JNIEnv*, jobject jpoint);
Elliott Hughes8451b252011-04-07 19:17:57 -070046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 static SkPoint* jpointf_to_point(JNIEnv*, jobject jpointf, SkPoint* point);
48 static void point_to_jpointf(const SkPoint& point, JNIEnv*, jobject jpointf);
Elliott Hughes8451b252011-04-07 19:17:57 -070049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 static SkCanvas* getNativeCanvas(JNIEnv*, jobject canvas);
John Reckedc22fb2015-04-20 22:06:31 +000051 static SkBitmap* getSkBitmap(JNIEnv*, jobject bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 static SkRegion* getNativeRegion(JNIEnv*, jobject region);
Elliott Hughes8451b252011-04-07 19:17:57 -070053
Mike Reed4c9355c2014-05-07 11:48:37 -040054 // Given the 'native' long held by the Rasterizer.java object, return a
55 // ref to its SkRasterizer* (or NULL).
56 static SkRasterizer* refNativeRasterizer(jlong rasterizerHandle);
57
Mike Reed1103b322014-07-08 12:36:44 -040058 /*
59 * LegacyBitmapConfig is the old enum in Skia that matched the enum int values
60 * in Bitmap.Config. Skia no longer supports this config, but has replaced it
61 * with SkColorType. These routines convert between the two.
62 */
63 static SkColorType legacyBitmapConfigToColorType(jint legacyConfig);
64 static jint colorTypeToLegacyBitmapConfig(SkColorType colorType);
65
Mike Reed42a1d082014-07-07 18:06:18 -040066 /** Return the corresponding native colorType from the java Config enum,
67 or kUnknown_SkColorType if the java object is null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 */
Mike Reed42a1d082014-07-07 18:06:18 -040069 static SkColorType getNativeBitmapColorType(JNIEnv*, jobject jconfig);
Elliott Hughes8451b252011-04-07 19:17:57 -070070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 /** Create a java Bitmap object given the native bitmap (required) and optional
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080072 storage array (may be null).
Leon Scroggins III8790be62013-12-03 16:26:51 -050073 bitmap's SkAlphaType must already be in sync with bitmapCreateFlags.
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080074 */
75 static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer,
Chris Craik47cd8e92014-07-08 17:13:08 -070076 int bitmapCreateFlags, jbyteArray ninePatch, jobject ninePatchInsets, int density = -1);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080077
Chris Craik1abf5d62013-08-16 12:47:03 -070078 static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, int bitmapCreateFlags,
Chris Craik47cd8e92014-07-08 17:13:08 -070079 jbyteArray ninePatch, int density = -1) {
80 return createBitmap(env, bitmap, NULL, bitmapCreateFlags, ninePatch, NULL, density);
81 }
Elliott Hughes8451b252011-04-07 19:17:57 -070082
Leon Scroggins III8790be62013-12-03 16:26:51 -050083 /** Reinitialize a bitmap. bitmap must already have its SkAlphaType set in
84 sync with isPremultiplied
85 */
Chris Craik1abf5d62013-08-16 12:47:03 -070086 static void reinitBitmap(JNIEnv* env, jobject javaBitmap, SkBitmap* bitmap,
87 bool isPremultiplied);
Chris Craik9f583612013-05-20 18:13:47 -070088
89 static int getBitmapAllocationByteCount(JNIEnv* env, jobject javaBitmap);
90
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 static jobject createRegion(JNIEnv* env, SkRegion* region);
92
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080093 static jobject createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap);
Joseph Wenf1f48bc2010-07-19 16:59:51 +080094
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080095 static jbyteArray allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap,
Chris Craik1abf5d62013-08-16 12:47:03 -070096 SkColorTable* ctable);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080097
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050098 /**
99 * Given a bitmap we natively allocate a memory block to store the contents
100 * of that bitmap. The memory is then attached to the bitmap via an
101 * SkPixelRef, which ensures that upon deletion the appropriate caches
102 * are notified.
103 */
104 static bool allocatePixels(JNIEnv* env, SkBitmap* bitmap, SkColorTable* ctable);
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 /** Copy the colors in colors[] to the bitmap, convert to the correct
107 format along the way.
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400108 Whether to use premultiplied pixels is determined by dstBitmap's alphaType.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 */
110 static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset,
Chris Craik1abf5d62013-08-16 12:47:03 -0700111 int srcStride, int x, int y, int width, int height,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400112 const SkBitmap& dstBitmap);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800113
114 static jbyteArray getBitmapStorageObj(SkPixelRef *pixref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115};
116
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800117class AndroidPixelRef : public SkMallocPixelRef {
118public:
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500119 AndroidPixelRef(JNIEnv* env, const SkImageInfo& info, void* storage, size_t rowBytes,
120 jbyteArray storageObj, SkColorTable* ctable);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800121
Chris Craikcd0ba712013-09-06 14:40:30 -0700122 /**
123 * Creates an AndroidPixelRef that wraps (and refs) another to reuse/share
124 * the same storage and java byte array refcounting, yet have a different
125 * color table.
126 */
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500127 AndroidPixelRef(AndroidPixelRef& wrappedPixelRef, const SkImageInfo& info,
128 size_t rowBytes, SkColorTable* ctable);
Chris Craikcd0ba712013-09-06 14:40:30 -0700129
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800130 virtual ~AndroidPixelRef();
131
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800132private:
Chris Craikcd0ba712013-09-06 14:40:30 -0700133 AndroidPixelRef* const fWrappedPixelRef; // if set, delegate memory management calls to this
134
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800135 JavaVM* fVM;
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800136 jbyteArray fStorageObj; // The Java byte[] object used as the bitmap backing store
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800137};
138
139/** Allocator which allocates the backing buffer in the Java heap.
140 * Instances can only be used to perform a single allocation, which helps
141 * ensure that the allocated buffer is properly accounted for with a
142 * reference in the heap (or a JNI global reference).
143 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144class JavaPixelAllocator : public SkBitmap::Allocator {
145public:
Carl Shapiro12d81c42010-12-17 18:36:48 -0800146 JavaPixelAllocator(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 // overrides
148 virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable);
Elliott Hughes8451b252011-04-07 19:17:57 -0700149
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800150 /** Return the Java array object created for the last allocation.
151 * This returns a local JNI reference which the caller is responsible
152 * for storing appropriately (usually by passing it to the Bitmap
153 * constructor).
154 */
155 jbyteArray getStorageObj() { return fStorageObj; }
156
157 /** Same as getStorageObj(), but also resets the allocator so that it
158 * can allocate again.
159 */
160 jbyteArray getStorageObjAndReset() {
161 jbyteArray result = fStorageObj;
162 fStorageObj = NULL;
163 fAllocCount = 0;
164 return result;
165 };
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167private:
Wei-Ta Chen291303b2010-08-18 15:40:29 +0800168 JavaVM* fVM;
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800169 jbyteArray fStorageObj;
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800170 int fAllocCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171};
172
Mike Reedc04851f2009-10-28 15:09:45 -0400173enum JNIAccess {
174 kRO_JNIAccess,
175 kRW_JNIAccess
176};
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178class AutoJavaFloatArray {
179public:
Mike Reedc04851f2009-10-28 15:09:45 -0400180 AutoJavaFloatArray(JNIEnv* env, jfloatArray array,
181 int minLength = 0, JNIAccess = kRW_JNIAccess);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 ~AutoJavaFloatArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 float* ptr() const { return fPtr; }
185 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187private:
188 JNIEnv* fEnv;
189 jfloatArray fArray;
190 float* fPtr;
191 int fLen;
Mike Reedc04851f2009-10-28 15:09:45 -0400192 int fReleaseMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193};
194
195class AutoJavaIntArray {
196public:
197 AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0);
198 ~AutoJavaIntArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 jint* ptr() const { return fPtr; }
201 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203private:
204 JNIEnv* fEnv;
205 jintArray fArray;
206 jint* fPtr;
207 int fLen;
208};
209
210class AutoJavaShortArray {
211public:
Mike Reedc04851f2009-10-28 15:09:45 -0400212 AutoJavaShortArray(JNIEnv* env, jshortArray array,
213 int minLength = 0, JNIAccess = kRW_JNIAccess);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 ~AutoJavaShortArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 jshort* ptr() const { return fPtr; }
217 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219private:
220 JNIEnv* fEnv;
221 jshortArray fArray;
222 jshort* fPtr;
223 int fLen;
Mike Reedc04851f2009-10-28 15:09:45 -0400224 int fReleaseMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225};
226
227class AutoJavaByteArray {
228public:
229 AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0);
230 ~AutoJavaByteArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 jbyte* ptr() const { return fPtr; }
233 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235private:
236 JNIEnv* fEnv;
237 jbyteArray fArray;
238 jbyte* fPtr;
239 int fLen;
240};
241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242void doThrowNPE(JNIEnv* env);
243void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception
244void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument
245void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime
246void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State
247void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800248void doThrowIOE(JNIEnv* env, const char* msg = NULL); // IO Exception
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249
250#define NPE_CHECK_RETURN_ZERO(env, object) \
251 do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0)
252
253#define NPE_CHECK_RETURN_VOID(env, object) \
254 do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0)
255
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800256#endif // _ANDROID_GRAPHICS_GRAPHICS_JNI_H_