| 1 |
#ifndef _ANDROID_GRAPHICS_GRAPHICS_JNI_H_ |
| 2 |
#define _ANDROID_GRAPHICS_GRAPHICS_JNI_H_ |
| 3 |
|
| 4 |
#include <cutils/compiler.h> |
| 5 |
|
| 6 |
#include "Bitmap.h" |
| 7 |
#include "BRDAllocator.h" |
| 8 |
#include "SkBitmap.h" |
| 9 |
#include "SkCodec.h" |
| 10 |
#include "SkPixelRef.h" |
| 11 |
#include "SkMallocPixelRef.h" |
| 12 |
#include "SkPoint.h" |
| 13 |
#include "SkRect.h" |
| 14 |
#include "SkColorSpace.h" |
| 15 |
#include <hwui/Canvas.h> |
| 16 |
#include <hwui/Bitmap.h> |
| 17 |
|
| 18 |
#include "graphics_jni_helpers.h" |
| 19 |
|
| 20 |
class SkCanvas; |
| 21 |
struct SkFontMetrics; |
| 22 |
|
| 23 |
namespace android { |
| 24 |
namespace skia { |
| 25 |
class BitmapRegionDecoder; |
| 26 |
} |
| 27 |
class Canvas; |
| 28 |
class Paint; |
| 29 |
struct Typeface; |
| 30 |
} |
| 31 |
|
| 32 |
class GraphicsJNI { |
| 33 |
public: |
| 34 |
|
| 35 |
|
| 36 |
enum LegacyBitmapConfig { |
| 37 |
kNo_LegacyBitmapConfig = 0, |
| 38 |
kA8_LegacyBitmapConfig = 1, |
| 39 |
kIndex8_LegacyBitmapConfig = 2, |
| 40 |
kRGB_565_LegacyBitmapConfig = 3, |
| 41 |
kARGB_4444_LegacyBitmapConfig = 4, |
| 42 |
kARGB_8888_LegacyBitmapConfig = 5, |
| 43 |
kRGBA_16F_LegacyBitmapConfig = 6, |
| 44 |
kHardware_LegacyBitmapConfig = 7, |
| 45 |
kRGBA_1010102_LegacyBitmapConfig = 8, |
| 46 |
|
| 47 |
kLastEnum_LegacyBitmapConfig = kRGBA_1010102_LegacyBitmapConfig |
| 48 |
}; |
| 49 |
|
| 50 |
static void setJavaVM(JavaVM* javaVM); |
| 51 |
|
| 52 |
|
| 53 |
static JavaVM* getJavaVM() { return mJavaVM; } |
| 54 |
|
| 55 |
|
| 56 |
static JNIEnv* getJNIEnv(); |
| 57 |
|
| 58 |
|
| 59 |
static JNIEnv* attachJNIEnv(const char* envName); |
| 60 |
|
| 61 |
|
| 62 |
static void detachJNIEnv(); |
| 63 |
|
| 64 |
|
| 65 |
static bool hasException(JNIEnv*); |
| 66 |
|
| 67 |
static void get_jrect(JNIEnv*, jobject jrect, int* L, int* T, int* R, int* B); |
| 68 |
static void set_jrect(JNIEnv*, jobject jrect, int L, int T, int R, int B); |
| 69 |
|
| 70 |
static SkIRect* jrect_to_irect(JNIEnv*, jobject jrect, SkIRect*); |
| 71 |
static void irect_to_jrect(const SkIRect&, JNIEnv*, jobject jrect); |
| 72 |
|
| 73 |
static SkRect* jrectf_to_rect(JNIEnv*, jobject jrectf, SkRect*); |
| 74 |
static SkRect* jrect_to_rect(JNIEnv*, jobject jrect, SkRect*); |
| 75 |
static void rect_to_jrectf(const SkRect&, JNIEnv*, jobject jrectf); |
| 76 |
|
| 77 |
static void set_jpoint(JNIEnv*, jobject jrect, int x, int y); |
| 78 |
|
| 79 |
static SkIPoint* jpoint_to_ipoint(JNIEnv*, jobject jpoint, SkIPoint* point); |
| 80 |
static void ipoint_to_jpoint(const SkIPoint& point, JNIEnv*, jobject jpoint); |
| 81 |
|
| 82 |
static SkPoint* jpointf_to_point(JNIEnv*, jobject jpointf, SkPoint* point); |
| 83 |
static void point_to_jpointf(const SkPoint& point, JNIEnv*, jobject jpointf); |
| 84 |
|
| 85 |
ANDROID_API static android::Canvas* getNativeCanvas(JNIEnv*, jobject canvas); |
| 86 |
static android::Bitmap* getNativeBitmap(JNIEnv*, jobject bitmap); |
| 87 |
static SkImageInfo getBitmapInfo(JNIEnv*, jobject bitmap, uint32_t* outRowBytes, |
| 88 |
bool* isHardware); |
| 89 |
static SkRegion* getNativeRegion(JNIEnv*, jobject region); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
static void set_metrics(JNIEnv*, jobject metrics, const SkFontMetrics& skmetrics); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
static int set_metrics_int(JNIEnv*, jobject metrics, const SkFontMetrics& skmetrics); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
static SkColorType legacyBitmapConfigToColorType(jint legacyConfig); |
| 108 |
static jint colorTypeToLegacyBitmapConfig(SkColorType colorType); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
static SkColorType getNativeBitmapColorType(JNIEnv*, jobject jconfig); |
| 114 |
static AndroidBitmapFormat getFormatFromConfig(JNIEnv* env, jobject jconfig); |
| 115 |
static jobject getConfigFromFormat(JNIEnv* env, AndroidBitmapFormat format); |
| 116 |
|
| 117 |
static bool isHardwareConfig(JNIEnv* env, jobject jconfig); |
| 118 |
static jint hardwareLegacyBitmapConfig(); |
| 119 |
|
| 120 |
static jobject createRegion(JNIEnv* env, SkRegion* region); |
| 121 |
|
| 122 |
static jobject createBitmapRegionDecoder(JNIEnv* env, |
| 123 |
android::skia::BitmapRegionDecoder* bitmap); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static bool allocatePixels(JNIEnv* env, SkBitmap* bitmap); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static bool SetPixels(JNIEnv* env, jintArray colors, int srcOffset, |
| 138 |
int srcStride, int x, int y, int width, int height, |
| 139 |
SkBitmap* dstBitmap); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
static sk_sp<SkColorSpace> getNativeColorSpace(jlong colorSpaceHandle); |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
static jobject getColorSpace(JNIEnv* env, SkColorSpace* decodeColorSpace, |
| 157 |
SkColorType decodeColorType); |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
static SkColor4f convertColorLong(jlong color); |
| 168 |
|
| 169 |
private: |
| 170 |
|
| 171 |
static JavaVM* mJavaVM; |
| 172 |
}; |
| 173 |
|
| 174 |
class HeapAllocator : public android::skia::BRDAllocator { |
| 175 |
public: |
| 176 |
HeapAllocator() { }; |
| 177 |
~HeapAllocator() { }; |
| 178 |
|
| 179 |
virtual bool allocPixelRef(SkBitmap* bitmap) override; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
android::Bitmap* getStorageObjAndReset() { |
| 185 |
return mStorage.release(); |
| 186 |
}; |
| 187 |
|
| 188 |
SkCodec::ZeroInitialized zeroInit() const override { return SkCodec::kYes_ZeroInitialized; } |
| 189 |
private: |
| 190 |
sk_sp<android::Bitmap> mStorage; |
| 191 |
}; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
class RecyclingClippingPixelAllocator : public android::skia::BRDAllocator { |
| 221 |
public: |
| 222 |
|
| 223 |
RecyclingClippingPixelAllocator(android::Bitmap* recycledBitmap, |
| 224 |
size_t recycledBytes); |
| 225 |
|
| 226 |
~RecyclingClippingPixelAllocator(); |
| 227 |
|
| 228 |
virtual bool allocPixelRef(SkBitmap* bitmap) override; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
void copyIfNecessary(); |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
SkCodec::ZeroInitialized zeroInit() const override { return SkCodec::kNo_ZeroInitialized; } |
| 246 |
|
| 247 |
private: |
| 248 |
android::Bitmap* mRecycledBitmap; |
| 249 |
const size_t mRecycledBytes; |
| 250 |
SkBitmap* mSkiaBitmap; |
| 251 |
bool mNeedsCopy; |
| 252 |
}; |
| 253 |
|
| 254 |
class AshmemPixelAllocator : public SkBitmap::Allocator { |
| 255 |
public: |
| 256 |
explicit AshmemPixelAllocator(JNIEnv* env); |
| 257 |
~AshmemPixelAllocator() { }; |
| 258 |
virtual bool allocPixelRef(SkBitmap* bitmap); |
| 259 |
android::Bitmap* getStorageObjAndReset() { |
| 260 |
return mStorage.release(); |
| 261 |
}; |
| 262 |
|
| 263 |
private: |
| 264 |
JavaVM* mJavaVM; |
| 265 |
sk_sp<android::Bitmap> mStorage; |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
enum JNIAccess { |
| 270 |
kRO_JNIAccess, |
| 271 |
kRW_JNIAccess |
| 272 |
}; |
| 273 |
|
| 274 |
class AutoJavaFloatArray { |
| 275 |
public: |
| 276 |
AutoJavaFloatArray(JNIEnv* env, jfloatArray array, |
| 277 |
int minLength = 0, JNIAccess = kRW_JNIAccess); |
| 278 |
~AutoJavaFloatArray(); |
| 279 |
|
| 280 |
float* ptr() const { return fPtr; } |
| 281 |
int length() const { return fLen; } |
| 282 |
|
| 283 |
private: |
| 284 |
JNIEnv* fEnv; |
| 285 |
jfloatArray fArray; |
| 286 |
float* fPtr; |
| 287 |
int fLen; |
| 288 |
int fReleaseMode; |
| 289 |
}; |
| 290 |
|
| 291 |
class AutoJavaIntArray { |
| 292 |
public: |
| 293 |
AutoJavaIntArray(JNIEnv* env, jintArray array, int minLength = 0); |
| 294 |
~AutoJavaIntArray(); |
| 295 |
|
| 296 |
jint* ptr() const { return fPtr; } |
| 297 |
int length() const { return fLen; } |
| 298 |
|
| 299 |
private: |
| 300 |
JNIEnv* fEnv; |
| 301 |
jintArray fArray; |
| 302 |
jint* fPtr; |
| 303 |
int fLen; |
| 304 |
}; |
| 305 |
|
| 306 |
class AutoJavaShortArray { |
| 307 |
public: |
| 308 |
AutoJavaShortArray(JNIEnv* env, jshortArray array, |
| 309 |
int minLength = 0, JNIAccess = kRW_JNIAccess); |
| 310 |
~AutoJavaShortArray(); |
| 311 |
|
| 312 |
jshort* ptr() const { return fPtr; } |
| 313 |
int length() const { return fLen; } |
| 314 |
|
| 315 |
private: |
| 316 |
JNIEnv* fEnv; |
| 317 |
jshortArray fArray; |
| 318 |
jshort* fPtr; |
| 319 |
int fLen; |
| 320 |
int fReleaseMode; |
| 321 |
}; |
| 322 |
|
| 323 |
class AutoJavaByteArray { |
| 324 |
public: |
| 325 |
AutoJavaByteArray(JNIEnv* env, jbyteArray array, int minLength = 0); |
| 326 |
~AutoJavaByteArray(); |
| 327 |
|
| 328 |
jbyte* ptr() const { return fPtr; } |
| 329 |
int length() const { return fLen; } |
| 330 |
|
| 331 |
private: |
| 332 |
JNIEnv* fEnv; |
| 333 |
jbyteArray fArray; |
| 334 |
jbyte* fPtr; |
| 335 |
int fLen; |
| 336 |
}; |
| 337 |
|
| 338 |
void doThrowNPE(JNIEnv* env); |
| 339 |
void doThrowAIOOBE(JNIEnv* env); |
| 340 |
void doThrowIAE(JNIEnv* env, const char* msg = NULL); |
| 341 |
void doThrowRE(JNIEnv* env, const char* msg = NULL); |
| 342 |
void doThrowISE(JNIEnv* env, const char* msg = NULL); |
| 343 |
void doThrowOOME(JNIEnv* env, const char* msg = NULL); |
| 344 |
void doThrowIOE(JNIEnv* env, const char* msg = NULL); |
| 345 |
|
| 346 |
#define NPE_CHECK_RETURN_ZERO(env, object) \ |
| 347 |
do { if (NULL == (object)) { doThrowNPE(env); return 0; } } while (0) |
| 348 |
|
| 349 |
#define NPE_CHECK_RETURN_VOID(env, object) \ |
| 350 |
do { if (NULL == (object)) { doThrowNPE(env); return; } } while (0) |
| 351 |
|
| 352 |
#endif |