blob: 8eb43f8a598976ca9d7a74bd4974681cf295bdf4 [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"
John Reckc294d122015-04-13 15:20:29 -070011#include <Canvas.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012#include <jni.h>
13
Derek Sollenberger5827cb52013-07-26 14:58:06 -040014class SkBitmapRegionDecoder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015class SkCanvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016
Raph Levien3d528c402014-06-26 09:04:54 -070017namespace android {
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -040018class Paint;
Dan Albert46d84442014-11-18 16:07:51 -080019struct TypefaceImpl;
Raph Levien3d528c402014-06-26 09:04:54 -070020}
21
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022class GraphicsJNI {
23public:
Chris Craik1abf5d62013-08-16 12:47:03 -070024 enum BitmapCreateFlags {
25 kBitmapCreateFlag_None = 0x0,
26 kBitmapCreateFlag_Mutable = 0x1,
27 kBitmapCreateFlag_Premultiplied = 0x2,
28 };
29
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030 // returns true if an exception is set (and dumps it out to the Log)
31 static bool hasException(JNIEnv*);
32
33 static void get_jrect(JNIEnv*, jobject jrect, int* L, int* T, int* R, int* B);
34 static void set_jrect(JNIEnv*, jobject jrect, int L, int T, int R, int B);
35
36 static SkIRect* jrect_to_irect(JNIEnv*, jobject jrect, SkIRect*);
37 static void irect_to_jrect(const SkIRect&, JNIEnv*, jobject jrect);
38
39 static SkRect* jrectf_to_rect(JNIEnv*, jobject jrectf, SkRect*);
40 static SkRect* jrect_to_rect(JNIEnv*, jobject jrect, SkRect*);
41 static void rect_to_jrectf(const SkRect&, JNIEnv*, jobject jrectf);
Elliott Hughes8451b252011-04-07 19:17:57 -070042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 static void set_jpoint(JNIEnv*, jobject jrect, int x, int y);
Elliott Hughes8451b252011-04-07 19:17:57 -070044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 static SkIPoint* jpoint_to_ipoint(JNIEnv*, jobject jpoint, SkIPoint* point);
46 static void ipoint_to_jpoint(const SkIPoint& point, JNIEnv*, jobject jpoint);
Elliott Hughes8451b252011-04-07 19:17:57 -070047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 static SkPoint* jpointf_to_point(JNIEnv*, jobject jpointf, SkPoint* point);
49 static void point_to_jpointf(const SkPoint& point, JNIEnv*, jobject jpointf);
Elliott Hughes8451b252011-04-07 19:17:57 -070050
John Reckc294d122015-04-13 15:20:29 -070051 static android::Canvas* getNativeCanvas(JNIEnv*, jobject canvas);
John Recka771b982015-04-10 13:52:57 -070052 static SkBitmap* getSkBitmapDeprecated(JNIEnv*, jobject bitmap);
53 static void getSkBitmap(JNIEnv*, jobject bitmap, SkBitmap* outBitmap);
54 static SkPixelRef* getSkPixelRef(JNIEnv*, jobject bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 static SkRegion* getNativeRegion(JNIEnv*, jobject region);
Elliott Hughes8451b252011-04-07 19:17:57 -070056
Mike Reed4c9355c2014-05-07 11:48:37 -040057 // Given the 'native' long held by the Rasterizer.java object, return a
58 // ref to its SkRasterizer* (or NULL).
59 static SkRasterizer* refNativeRasterizer(jlong rasterizerHandle);
60
Mike Reed1103b322014-07-08 12:36:44 -040061 /*
62 * LegacyBitmapConfig is the old enum in Skia that matched the enum int values
63 * in Bitmap.Config. Skia no longer supports this config, but has replaced it
64 * with SkColorType. These routines convert between the two.
65 */
66 static SkColorType legacyBitmapConfigToColorType(jint legacyConfig);
67 static jint colorTypeToLegacyBitmapConfig(SkColorType colorType);
68
Mike Reed42a1d082014-07-07 18:06:18 -040069 /** Return the corresponding native colorType from the java Config enum,
70 or kUnknown_SkColorType if the java object is null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 */
Mike Reed42a1d082014-07-07 18:06:18 -040072 static SkColorType getNativeBitmapColorType(JNIEnv*, jobject jconfig);
Elliott Hughes8451b252011-04-07 19:17:57 -070073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 /** Create a java Bitmap object given the native bitmap (required) and optional
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080075 storage array (may be null).
Leon Scroggins III8790be62013-12-03 16:26:51 -050076 bitmap's SkAlphaType must already be in sync with bitmapCreateFlags.
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080077 */
78 static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer,
Chris Craik47cd8e92014-07-08 17:13:08 -070079 int bitmapCreateFlags, jbyteArray ninePatch, jobject ninePatchInsets, int density = -1);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080080
Chris Craik1abf5d62013-08-16 12:47:03 -070081 static jobject createBitmap(JNIEnv* env, SkBitmap* bitmap, int bitmapCreateFlags,
Chris Craik47cd8e92014-07-08 17:13:08 -070082 jbyteArray ninePatch, int density = -1) {
83 return createBitmap(env, bitmap, NULL, bitmapCreateFlags, ninePatch, NULL, density);
84 }
Elliott Hughes8451b252011-04-07 19:17:57 -070085
Leon Scroggins III8790be62013-12-03 16:26:51 -050086 /** Reinitialize a bitmap. bitmap must already have its SkAlphaType set in
87 sync with isPremultiplied
88 */
Chris Craik1abf5d62013-08-16 12:47:03 -070089 static void reinitBitmap(JNIEnv* env, jobject javaBitmap, SkBitmap* bitmap,
90 bool isPremultiplied);
Chris Craik9f583612013-05-20 18:13:47 -070091
92 static int getBitmapAllocationByteCount(JNIEnv* env, jobject javaBitmap);
93
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 static jobject createRegion(JNIEnv* env, SkRegion* region);
95
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080096 static jobject createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap);
Joseph Wenf1f48bc2010-07-19 16:59:51 +080097
Patrick Dubroye4ac2d62010-12-01 11:23:13 -080098 static jbyteArray allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap,
Chris Craik1abf5d62013-08-16 12:47:03 -070099 SkColorTable* ctable);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800100
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500101 /**
102 * Given a bitmap we natively allocate a memory block to store the contents
103 * of that bitmap. The memory is then attached to the bitmap via an
104 * SkPixelRef, which ensures that upon deletion the appropriate caches
105 * are notified.
106 */
107 static bool allocatePixels(JNIEnv* env, SkBitmap* bitmap, SkColorTable* ctable);
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 /** Copy the colors in colors[] to the bitmap, convert to the correct
110 format along the way.
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400111 Whether to use premultiplied pixels is determined by dstBitmap's alphaType.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 */
113 static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset,
Chris Craik1abf5d62013-08-16 12:47:03 -0700114 int srcStride, int x, int y, int width, int height,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400115 const SkBitmap& dstBitmap);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800116
117 static jbyteArray getBitmapStorageObj(SkPixelRef *pixref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118};
119
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800120class AndroidPixelRef : public SkMallocPixelRef {
121public:
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500122 AndroidPixelRef(JNIEnv* env, const SkImageInfo& info, void* storage, size_t rowBytes,
123 jbyteArray storageObj, SkColorTable* ctable);
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800124
Chris Craikcd0ba712013-09-06 14:40:30 -0700125 /**
126 * Creates an AndroidPixelRef that wraps (and refs) another to reuse/share
127 * the same storage and java byte array refcounting, yet have a different
128 * color table.
129 */
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500130 AndroidPixelRef(AndroidPixelRef& wrappedPixelRef, const SkImageInfo& info,
131 size_t rowBytes, SkColorTable* ctable);
Chris Craikcd0ba712013-09-06 14:40:30 -0700132
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800133 virtual ~AndroidPixelRef();
134
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800135private:
Chris Craikcd0ba712013-09-06 14:40:30 -0700136 AndroidPixelRef* const fWrappedPixelRef; // if set, delegate memory management calls to this
137
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800138 JavaVM* fVM;
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800139 jbyteArray fStorageObj; // The Java byte[] object used as the bitmap backing store
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800140};
141
142/** Allocator which allocates the backing buffer in the Java heap.
143 * Instances can only be used to perform a single allocation, which helps
144 * ensure that the allocated buffer is properly accounted for with a
145 * reference in the heap (or a JNI global reference).
146 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147class JavaPixelAllocator : public SkBitmap::Allocator {
148public:
Carl Shapiro12d81c42010-12-17 18:36:48 -0800149 JavaPixelAllocator(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 // overrides
151 virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable);
Elliott Hughes8451b252011-04-07 19:17:57 -0700152
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800153 /** Return the Java array object created for the last allocation.
154 * This returns a local JNI reference which the caller is responsible
155 * for storing appropriately (usually by passing it to the Bitmap
156 * constructor).
157 */
158 jbyteArray getStorageObj() { return fStorageObj; }
159
160 /** Same as getStorageObj(), but also resets the allocator so that it
161 * can allocate again.
162 */
163 jbyteArray getStorageObjAndReset() {
164 jbyteArray result = fStorageObj;
165 fStorageObj = NULL;
166 fAllocCount = 0;
167 return result;
168 };
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170private:
Wei-Ta Chen291303b2010-08-18 15:40:29 +0800171 JavaVM* fVM;
Patrick Dubroye4ac2d62010-12-01 11:23:13 -0800172 jbyteArray fStorageObj;
Patrick Dubroyafde46e2010-12-15 11:52:01 -0800173 int fAllocCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174};
175
Mike Reedc04851f2009-10-28 15:09:45 -0400176enum JNIAccess {
177 kRO_JNIAccess,
178 kRW_JNIAccess
179};
180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181class AutoJavaFloatArray {
182public:
Mike Reedc04851f2009-10-28 15:09:45 -0400183 AutoJavaFloatArray(JNIEnv* env, jfloatArray array,
184 int minLength = 0, JNIAccess = kRW_JNIAccess);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 ~AutoJavaFloatArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 float* ptr() const { return fPtr; }
188 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190private:
191 JNIEnv* fEnv;
192 jfloatArray fArray;
193 float* fPtr;
194 int fLen;
Mike Reedc04851f2009-10-28 15:09:45 -0400195 int fReleaseMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196};
197
198class AutoJavaIntArray {
199public:
200 AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0);
201 ~AutoJavaIntArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 jint* ptr() const { return fPtr; }
204 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206private:
207 JNIEnv* fEnv;
208 jintArray fArray;
209 jint* fPtr;
210 int fLen;
211};
212
213class AutoJavaShortArray {
214public:
Mike Reedc04851f2009-10-28 15:09:45 -0400215 AutoJavaShortArray(JNIEnv* env, jshortArray array,
216 int minLength = 0, JNIAccess = kRW_JNIAccess);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 ~AutoJavaShortArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 jshort* ptr() const { return fPtr; }
220 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222private:
223 JNIEnv* fEnv;
224 jshortArray fArray;
225 jshort* fPtr;
226 int fLen;
Mike Reedc04851f2009-10-28 15:09:45 -0400227 int fReleaseMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228};
229
230class AutoJavaByteArray {
231public:
232 AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0);
233 ~AutoJavaByteArray();
Elliott Hughes8451b252011-04-07 19:17:57 -0700234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 jbyte* ptr() const { return fPtr; }
236 int length() const { return fLen; }
Elliott Hughes8451b252011-04-07 19:17:57 -0700237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238private:
239 JNIEnv* fEnv;
240 jbyteArray fArray;
241 jbyte* fPtr;
242 int fLen;
243};
244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245void doThrowNPE(JNIEnv* env);
246void doThrowAIOOBE(JNIEnv* env); // Array Index Out Of Bounds Exception
247void doThrowIAE(JNIEnv* env, const char* msg = NULL); // Illegal Argument
248void doThrowRE(JNIEnv* env, const char* msg = NULL); // Runtime
249void doThrowISE(JNIEnv* env, const char* msg = NULL); // Illegal State
250void doThrowOOME(JNIEnv* env, const char* msg = NULL); // Out of memory
Joseph Wenf1f48bc2010-07-19 16:59:51 +0800251void doThrowIOE(JNIEnv* env, const char* msg = NULL); // IO Exception
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253#define NPE_CHECK_RETURN_ZERO(env, object) \
254 do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0)
255
256#define NPE_CHECK_RETURN_VOID(env, object) \
257 do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0)
258
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800259#endif // _ANDROID_GRAPHICS_GRAPHICS_JNI_H_