Ficus Kirkpatrick | 1a9c27c | 2010-03-05 17:05:08 -0800 | [diff] [blame] | 1 | #define LOG_TAG "GraphicsJNI" |
| 2 | |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 3 | #include <unistd.h> |
| 4 | #include <sys/mman.h> |
| 5 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6 | #include "jni.h" |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 7 | #include "JNIHelp.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | #include "GraphicsJNI.h" |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 9 | #include "core_jni_helpers.h" |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 10 | |
| 11 | #include "SkCanvas.h" |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 12 | #include "SkMath.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13 | #include "SkRegion.h" |
| 14 | #include <android_runtime/AndroidRuntime.h> |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 15 | #include <cutils/ashmem.h> |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 16 | #include <hwui/Canvas.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 18 | #include <Caches.h> |
| 19 | #include <TextureCache.h> |
| 20 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 21 | using namespace android; |
| 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | void doThrowNPE(JNIEnv* env) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 24 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | void doThrowAIOOBE(JNIEnv* env) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 28 | jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void doThrowRE(JNIEnv* env, const char* msg) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 32 | jniThrowRuntimeException(env, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void doThrowIAE(JNIEnv* env, const char* msg) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 36 | jniThrowException(env, "java/lang/IllegalArgumentException", msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void doThrowISE(JNIEnv* env, const char* msg) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 40 | jniThrowException(env, "java/lang/IllegalStateException", msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void doThrowOOME(JNIEnv* env, const char* msg) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 44 | jniThrowException(env, "java/lang/OutOfMemoryError", msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 47 | void doThrowIOE(JNIEnv* env, const char* msg) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 48 | jniThrowException(env, "java/io/IOException", msg); |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 49 | } |
| 50 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | bool GraphicsJNI::hasException(JNIEnv *env) { |
| 52 | if (env->ExceptionCheck() != 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 53 | ALOGE("*** Uncaught exception returned from Java call!\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | env->ExceptionDescribe(); |
| 55 | return true; |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | /////////////////////////////////////////////////////////////////////////////// |
| 61 | |
| 62 | AutoJavaFloatArray::AutoJavaFloatArray(JNIEnv* env, jfloatArray array, |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 63 | int minLength, JNIAccess access) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | : fEnv(env), fArray(array), fPtr(NULL), fLen(0) { |
| 65 | SkASSERT(env); |
| 66 | if (array) { |
| 67 | fLen = env->GetArrayLength(array); |
| 68 | if (fLen < minLength) { |
| 69 | sk_throw(); |
| 70 | } |
| 71 | fPtr = env->GetFloatArrayElements(array, NULL); |
| 72 | } |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 73 | fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | AutoJavaFloatArray::~AutoJavaFloatArray() { |
| 77 | if (fPtr) { |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 78 | fEnv->ReleaseFloatArrayElements(fArray, fPtr, fReleaseMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | AutoJavaIntArray::AutoJavaIntArray(JNIEnv* env, jintArray array, |
| 83 | int minLength) |
| 84 | : fEnv(env), fArray(array), fPtr(NULL), fLen(0) { |
| 85 | SkASSERT(env); |
| 86 | if (array) { |
| 87 | fLen = env->GetArrayLength(array); |
| 88 | if (fLen < minLength) { |
| 89 | sk_throw(); |
| 90 | } |
| 91 | fPtr = env->GetIntArrayElements(array, NULL); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | AutoJavaIntArray::~AutoJavaIntArray() { |
| 96 | if (fPtr) { |
| 97 | fEnv->ReleaseIntArrayElements(fArray, fPtr, 0); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | AutoJavaShortArray::AutoJavaShortArray(JNIEnv* env, jshortArray array, |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 102 | int minLength, JNIAccess access) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | : fEnv(env), fArray(array), fPtr(NULL), fLen(0) { |
| 104 | SkASSERT(env); |
| 105 | if (array) { |
| 106 | fLen = env->GetArrayLength(array); |
| 107 | if (fLen < minLength) { |
| 108 | sk_throw(); |
| 109 | } |
| 110 | fPtr = env->GetShortArrayElements(array, NULL); |
| 111 | } |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 112 | fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | AutoJavaShortArray::~AutoJavaShortArray() { |
| 116 | if (fPtr) { |
Mike Reed | c04851f | 2009-10-28 15:09:45 -0400 | [diff] [blame] | 117 | fEnv->ReleaseShortArrayElements(fArray, fPtr, fReleaseMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | AutoJavaByteArray::AutoJavaByteArray(JNIEnv* env, jbyteArray array, |
| 122 | int minLength) |
| 123 | : fEnv(env), fArray(array), fPtr(NULL), fLen(0) { |
| 124 | SkASSERT(env); |
| 125 | if (array) { |
| 126 | fLen = env->GetArrayLength(array); |
| 127 | if (fLen < minLength) { |
| 128 | sk_throw(); |
| 129 | } |
| 130 | fPtr = env->GetByteArrayElements(array, NULL); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | AutoJavaByteArray::~AutoJavaByteArray() { |
| 135 | if (fPtr) { |
| 136 | fEnv->ReleaseByteArrayElements(fArray, fPtr, 0); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /////////////////////////////////////////////////////////////////////////////// |
| 141 | |
| 142 | static jclass gRect_class; |
| 143 | static jfieldID gRect_leftFieldID; |
| 144 | static jfieldID gRect_topFieldID; |
| 145 | static jfieldID gRect_rightFieldID; |
| 146 | static jfieldID gRect_bottomFieldID; |
| 147 | |
| 148 | static jclass gRectF_class; |
| 149 | static jfieldID gRectF_leftFieldID; |
| 150 | static jfieldID gRectF_topFieldID; |
| 151 | static jfieldID gRectF_rightFieldID; |
| 152 | static jfieldID gRectF_bottomFieldID; |
| 153 | |
| 154 | static jclass gPoint_class; |
| 155 | static jfieldID gPoint_xFieldID; |
| 156 | static jfieldID gPoint_yFieldID; |
| 157 | |
| 158 | static jclass gPointF_class; |
| 159 | static jfieldID gPointF_xFieldID; |
| 160 | static jfieldID gPointF_yFieldID; |
| 161 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | static jclass gBitmapConfig_class; |
| 163 | static jfieldID gBitmapConfig_nativeInstanceID; |
| 164 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 165 | static jclass gBitmapRegionDecoder_class; |
| 166 | static jmethodID gBitmapRegionDecoder_constructorMethodID; |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 167 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | static jclass gCanvas_class; |
| 169 | static jfieldID gCanvas_nativeInstanceID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | static jclass gPicture_class; |
| 172 | static jfieldID gPicture_nativeInstanceID; |
| 173 | |
| 174 | static jclass gRegion_class; |
| 175 | static jfieldID gRegion_nativeInstanceID; |
| 176 | static jmethodID gRegion_constructorMethodID; |
| 177 | |
Mathieu Chartier | 7384b42 | 2013-10-17 18:16:42 -0700 | [diff] [blame] | 178 | static jclass gByte_class; |
| 179 | static jobject gVMRuntime; |
| 180 | static jclass gVMRuntime_class; |
| 181 | static jmethodID gVMRuntime_newNonMovableArray; |
| 182 | static jmethodID gVMRuntime_addressOf; |
| 183 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 184 | static jfieldID gTransferParams_aFieldID; |
| 185 | static jfieldID gTransferParams_bFieldID; |
| 186 | static jfieldID gTransferParams_cFieldID; |
| 187 | static jfieldID gTransferParams_dFieldID; |
| 188 | static jfieldID gTransferParams_eFieldID; |
| 189 | static jfieldID gTransferParams_fFieldID; |
| 190 | static jfieldID gTransferParams_gFieldID; |
| 191 | |
| 192 | static jclass gColorSpace_class; |
| 193 | static jfieldID gColorSpace_IlluminantD50FieldID; |
| 194 | static jmethodID gColorSpace_adaptMethodID; |
| 195 | static jmethodID gColorSpace_getMethodID; |
| 196 | static jmethodID gColorSpace_matchMethodID; |
| 197 | |
| 198 | static jclass gColorSpaceRGB_class; |
| 199 | static jmethodID gColorSpaceRGB_getTransferParametersMethodID; |
| 200 | static jmethodID gColorSpaceRGB_getTransformMethodID; |
| 201 | static jmethodID gColorSpaceRGB_constructorMethodID; |
| 202 | |
| 203 | static jclass gColorSpace_Named_class; |
| 204 | static jfieldID gColorSpace_Named_sRGBFieldID; |
| 205 | static jfieldID gColorSpace_Named_LinearExtendedSRGBFieldID; |
| 206 | |
| 207 | static jclass gTransferParameters_class; |
| 208 | static jmethodID gTransferParameters_constructorMethodID; |
| 209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | /////////////////////////////////////////////////////////////////////////////// |
| 211 | |
| 212 | void GraphicsJNI::get_jrect(JNIEnv* env, jobject obj, int* L, int* T, int* R, int* B) |
| 213 | { |
| 214 | SkASSERT(env->IsInstanceOf(obj, gRect_class)); |
| 215 | |
| 216 | *L = env->GetIntField(obj, gRect_leftFieldID); |
| 217 | *T = env->GetIntField(obj, gRect_topFieldID); |
| 218 | *R = env->GetIntField(obj, gRect_rightFieldID); |
| 219 | *B = env->GetIntField(obj, gRect_bottomFieldID); |
| 220 | } |
| 221 | |
| 222 | void GraphicsJNI::set_jrect(JNIEnv* env, jobject obj, int L, int T, int R, int B) |
| 223 | { |
| 224 | SkASSERT(env->IsInstanceOf(obj, gRect_class)); |
| 225 | |
| 226 | env->SetIntField(obj, gRect_leftFieldID, L); |
| 227 | env->SetIntField(obj, gRect_topFieldID, T); |
| 228 | env->SetIntField(obj, gRect_rightFieldID, R); |
| 229 | env->SetIntField(obj, gRect_bottomFieldID, B); |
| 230 | } |
| 231 | |
| 232 | SkIRect* GraphicsJNI::jrect_to_irect(JNIEnv* env, jobject obj, SkIRect* ir) |
| 233 | { |
| 234 | SkASSERT(env->IsInstanceOf(obj, gRect_class)); |
| 235 | |
| 236 | ir->set(env->GetIntField(obj, gRect_leftFieldID), |
| 237 | env->GetIntField(obj, gRect_topFieldID), |
| 238 | env->GetIntField(obj, gRect_rightFieldID), |
| 239 | env->GetIntField(obj, gRect_bottomFieldID)); |
| 240 | return ir; |
| 241 | } |
| 242 | |
| 243 | void GraphicsJNI::irect_to_jrect(const SkIRect& ir, JNIEnv* env, jobject obj) |
| 244 | { |
| 245 | SkASSERT(env->IsInstanceOf(obj, gRect_class)); |
| 246 | |
| 247 | env->SetIntField(obj, gRect_leftFieldID, ir.fLeft); |
| 248 | env->SetIntField(obj, gRect_topFieldID, ir.fTop); |
| 249 | env->SetIntField(obj, gRect_rightFieldID, ir.fRight); |
| 250 | env->SetIntField(obj, gRect_bottomFieldID, ir.fBottom); |
| 251 | } |
| 252 | |
| 253 | SkRect* GraphicsJNI::jrectf_to_rect(JNIEnv* env, jobject obj, SkRect* r) |
| 254 | { |
| 255 | SkASSERT(env->IsInstanceOf(obj, gRectF_class)); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 256 | |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 257 | r->set(env->GetFloatField(obj, gRectF_leftFieldID), |
| 258 | env->GetFloatField(obj, gRectF_topFieldID), |
| 259 | env->GetFloatField(obj, gRectF_rightFieldID), |
| 260 | env->GetFloatField(obj, gRectF_bottomFieldID)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | return r; |
| 262 | } |
| 263 | |
| 264 | SkRect* GraphicsJNI::jrect_to_rect(JNIEnv* env, jobject obj, SkRect* r) |
| 265 | { |
| 266 | SkASSERT(env->IsInstanceOf(obj, gRect_class)); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 267 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | r->set(SkIntToScalar(env->GetIntField(obj, gRect_leftFieldID)), |
| 269 | SkIntToScalar(env->GetIntField(obj, gRect_topFieldID)), |
| 270 | SkIntToScalar(env->GetIntField(obj, gRect_rightFieldID)), |
| 271 | SkIntToScalar(env->GetIntField(obj, gRect_bottomFieldID))); |
| 272 | return r; |
| 273 | } |
| 274 | |
| 275 | void GraphicsJNI::rect_to_jrectf(const SkRect& r, JNIEnv* env, jobject obj) |
| 276 | { |
| 277 | SkASSERT(env->IsInstanceOf(obj, gRectF_class)); |
| 278 | |
| 279 | env->SetFloatField(obj, gRectF_leftFieldID, SkScalarToFloat(r.fLeft)); |
| 280 | env->SetFloatField(obj, gRectF_topFieldID, SkScalarToFloat(r.fTop)); |
| 281 | env->SetFloatField(obj, gRectF_rightFieldID, SkScalarToFloat(r.fRight)); |
| 282 | env->SetFloatField(obj, gRectF_bottomFieldID, SkScalarToFloat(r.fBottom)); |
| 283 | } |
| 284 | |
| 285 | SkIPoint* GraphicsJNI::jpoint_to_ipoint(JNIEnv* env, jobject obj, SkIPoint* point) |
| 286 | { |
| 287 | SkASSERT(env->IsInstanceOf(obj, gPoint_class)); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | point->set(env->GetIntField(obj, gPoint_xFieldID), |
| 290 | env->GetIntField(obj, gPoint_yFieldID)); |
| 291 | return point; |
| 292 | } |
| 293 | |
| 294 | void GraphicsJNI::ipoint_to_jpoint(const SkIPoint& ir, JNIEnv* env, jobject obj) |
| 295 | { |
| 296 | SkASSERT(env->IsInstanceOf(obj, gPoint_class)); |
| 297 | |
| 298 | env->SetIntField(obj, gPoint_xFieldID, ir.fX); |
| 299 | env->SetIntField(obj, gPoint_yFieldID, ir.fY); |
| 300 | } |
| 301 | |
| 302 | SkPoint* GraphicsJNI::jpointf_to_point(JNIEnv* env, jobject obj, SkPoint* point) |
| 303 | { |
| 304 | SkASSERT(env->IsInstanceOf(obj, gPointF_class)); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 305 | |
Leon Scroggins III | 2e0103e | 2014-04-04 17:05:24 -0400 | [diff] [blame] | 306 | point->set(env->GetIntField(obj, gPointF_xFieldID), |
| 307 | env->GetIntField(obj, gPointF_yFieldID)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | return point; |
| 309 | } |
| 310 | |
| 311 | void GraphicsJNI::point_to_jpointf(const SkPoint& r, JNIEnv* env, jobject obj) |
| 312 | { |
| 313 | SkASSERT(env->IsInstanceOf(obj, gPointF_class)); |
| 314 | |
| 315 | env->SetFloatField(obj, gPointF_xFieldID, SkScalarToFloat(r.fX)); |
| 316 | env->SetFloatField(obj, gPointF_yFieldID, SkScalarToFloat(r.fY)); |
| 317 | } |
| 318 | |
Romain Guy | e8d2ebb | 2017-02-09 18:38:47 -0800 | [diff] [blame] | 319 | // See enum values in GraphicsJNI.h |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 320 | jint GraphicsJNI::colorTypeToLegacyBitmapConfig(SkColorType colorType) { |
| 321 | switch (colorType) { |
Romain Guy | 9505a65 | 2016-12-14 09:43:50 -0800 | [diff] [blame] | 322 | case kRGBA_F16_SkColorType: |
| 323 | return kRGBA_16F_LegacyBitmapConfig; |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 324 | case kN32_SkColorType: |
| 325 | return kARGB_8888_LegacyBitmapConfig; |
| 326 | case kARGB_4444_SkColorType: |
| 327 | return kARGB_4444_LegacyBitmapConfig; |
| 328 | case kRGB_565_SkColorType: |
| 329 | return kRGB_565_LegacyBitmapConfig; |
| 330 | case kIndex_8_SkColorType: |
| 331 | return kIndex8_LegacyBitmapConfig; |
| 332 | case kAlpha_8_SkColorType: |
| 333 | return kA8_LegacyBitmapConfig; |
| 334 | case kUnknown_SkColorType: |
| 335 | default: |
| 336 | break; |
| 337 | } |
| 338 | return kNo_LegacyBitmapConfig; |
| 339 | } |
| 340 | |
| 341 | SkColorType GraphicsJNI::legacyBitmapConfigToColorType(jint legacyConfig) { |
| 342 | const uint8_t gConfig2ColorType[] = { |
| 343 | kUnknown_SkColorType, |
| 344 | kAlpha_8_SkColorType, |
| 345 | kIndex_8_SkColorType, |
| 346 | kRGB_565_SkColorType, |
| 347 | kARGB_4444_SkColorType, |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 348 | kN32_SkColorType, |
Romain Guy | 9505a65 | 2016-12-14 09:43:50 -0800 | [diff] [blame] | 349 | kRGBA_F16_SkColorType, |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 350 | kN32_SkColorType |
| 351 | }; |
| 352 | |
| 353 | if (legacyConfig < 0 || legacyConfig > kLastEnum_LegacyBitmapConfig) { |
| 354 | legacyConfig = kNo_LegacyBitmapConfig; |
| 355 | } |
| 356 | return static_cast<SkColorType>(gConfig2ColorType[legacyConfig]); |
| 357 | } |
| 358 | |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 359 | void GraphicsJNI::getSkBitmap(JNIEnv* env, jobject bitmap, SkBitmap* outBitmap) { |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 360 | bitmap::toBitmap(env, bitmap).getSkBitmap(outBitmap); |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 361 | } |
| 362 | |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 363 | SkPixelRef* GraphicsJNI::refSkPixelRef(JNIEnv* env, jobject jbitmap) { |
| 364 | android::Bitmap& bitmap = android::bitmap::toBitmap(env, jbitmap); |
| 365 | bitmap.ref(); |
| 366 | return &bitmap; |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 367 | } |
Mike Reed | 42a1d08 | 2014-07-07 18:06:18 -0400 | [diff] [blame] | 368 | SkColorType GraphicsJNI::getNativeBitmapColorType(JNIEnv* env, jobject jconfig) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | SkASSERT(env); |
| 370 | if (NULL == jconfig) { |
Mike Reed | 42a1d08 | 2014-07-07 18:06:18 -0400 | [diff] [blame] | 371 | return kUnknown_SkColorType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | } |
| 373 | SkASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class)); |
| 374 | int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID); |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 375 | return legacyBitmapConfigToColorType(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | } |
| 377 | |
sergeyv | da6c8ffc | 2016-11-22 18:28:54 -0800 | [diff] [blame] | 378 | bool GraphicsJNI::isHardwareConfig(JNIEnv* env, jobject jconfig) { |
| 379 | SkASSERT(env); |
| 380 | if (NULL == jconfig) { |
| 381 | return false; |
| 382 | } |
| 383 | int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID); |
| 384 | return c == kHardware_LegacyBitmapConfig; |
| 385 | } |
| 386 | |
sergeyv | 19b4b01 | 2016-12-13 16:06:00 -0800 | [diff] [blame] | 387 | jint GraphicsJNI::hardwareLegacyBitmapConfig() { |
| 388 | return kHardware_LegacyBitmapConfig; |
| 389 | } |
| 390 | |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 391 | android::Canvas* GraphicsJNI::getNativeCanvas(JNIEnv* env, jobject canvas) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | SkASSERT(env); |
| 393 | SkASSERT(canvas); |
| 394 | SkASSERT(env->IsInstanceOf(canvas, gCanvas_class)); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 395 | jlong canvasHandle = env->GetLongField(canvas, gCanvas_nativeInstanceID); |
Bo Liu | de92f4c | 2014-11-24 10:53:52 -0800 | [diff] [blame] | 396 | if (!canvasHandle) { |
| 397 | return NULL; |
| 398 | } |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 399 | return reinterpret_cast<android::Canvas*>(canvasHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | } |
| 401 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | SkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region) |
| 403 | { |
| 404 | SkASSERT(env); |
| 405 | SkASSERT(region); |
| 406 | SkASSERT(env->IsInstanceOf(region, gRegion_class)); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 407 | jlong regionHandle = env->GetLongField(region, gRegion_nativeInstanceID); |
| 408 | SkRegion* r = reinterpret_cast<SkRegion*>(regionHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | SkASSERT(r); |
| 410 | return r; |
| 411 | } |
| 412 | |
| 413 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 414 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 415 | jobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap) |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 416 | { |
| 417 | SkASSERT(bitmap != NULL); |
| 418 | |
Elliott Hughes | cf6f7a0 | 2011-04-12 17:50:45 -0700 | [diff] [blame] | 419 | jobject obj = env->NewObject(gBitmapRegionDecoder_class, |
| 420 | gBitmapRegionDecoder_constructorMethodID, |
Ashok Bhat | b091d47 | 2014-01-08 14:32:49 +0000 | [diff] [blame] | 421 | reinterpret_cast<jlong>(bitmap)); |
Elliott Hughes | cf6f7a0 | 2011-04-12 17:50:45 -0700 | [diff] [blame] | 422 | hasException(env); // For the side effect of logging. |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 423 | return obj; |
| 424 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | |
| 426 | jobject GraphicsJNI::createRegion(JNIEnv* env, SkRegion* region) |
| 427 | { |
| 428 | SkASSERT(region != NULL); |
Elliott Hughes | cf6f7a0 | 2011-04-12 17:50:45 -0700 | [diff] [blame] | 429 | jobject obj = env->NewObject(gRegion_class, gRegion_constructorMethodID, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 430 | reinterpret_cast<jlong>(region), 0); |
Elliott Hughes | cf6f7a0 | 2011-04-12 17:50:45 -0700 | [diff] [blame] | 431 | hasException(env); // For the side effect of logging. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | return obj; |
| 433 | } |
| 434 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | /////////////////////////////////////////////////////////////////////////////// |
| 436 | |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 437 | android::Bitmap* GraphicsJNI::mapAshmemBitmap(JNIEnv* env, SkBitmap* bitmap, |
John Reck | 003bdee | 2016-09-12 10:43:35 -0700 | [diff] [blame] | 438 | SkColorTable* ctable, int fd, void* addr, size_t size, bool readOnly) { |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 439 | const SkImageInfo& info = bitmap->info(); |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 440 | if (info.colorType() == kUnknown_SkColorType) { |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 441 | doThrowIAE(env, "unknown bitmap configuration"); |
| 442 | return nullptr; |
| 443 | } |
| 444 | |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 445 | if (!addr) { |
| 446 | // Map existing ashmem region if not already mapped. |
| 447 | int flags = readOnly ? (PROT_READ) : (PROT_READ | PROT_WRITE); |
John Reck | 003bdee | 2016-09-12 10:43:35 -0700 | [diff] [blame] | 448 | size = ashmem_get_size_region(fd); |
| 449 | addr = mmap(NULL, size, flags, MAP_SHARED, fd, 0); |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 450 | if (addr == MAP_FAILED) { |
| 451 | return nullptr; |
| 452 | } |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | // we must respect the rowBytes value already set on the bitmap instead of |
| 456 | // attempting to compute our own. |
| 457 | const size_t rowBytes = bitmap->rowBytes(); |
| 458 | |
Mike Reed | 0a8fb52 | 2017-04-13 15:31:32 -0400 | [diff] [blame] | 459 | auto wrapper = new android::Bitmap(addr, fd, size, info, rowBytes, sk_ref_sp(ctable)); |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 460 | wrapper->getSkBitmap(bitmap); |
Jeff Brown | a316c5d | 2015-06-05 15:14:06 -0700 | [diff] [blame] | 461 | if (readOnly) { |
| 462 | bitmap->pixelRef()->setImmutable(); |
| 463 | } |
Riley Andrews | 39d7f30 | 2014-11-13 17:43:25 -0800 | [diff] [blame] | 464 | return wrapper; |
| 465 | } |
| 466 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 467 | sk_sp<SkColorSpace> GraphicsJNI::defaultColorSpace() { |
| 468 | #ifdef ANDROID_ENABLE_LINEAR_BLENDING |
Matt Sarett | 89ddb1f | 2017-02-10 13:31:56 -0500 | [diff] [blame] | 469 | return SkColorSpace::MakeSRGB(); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 470 | #else |
| 471 | return nullptr; |
| 472 | #endif |
| 473 | } |
| 474 | |
Romain Guy | 9505a65 | 2016-12-14 09:43:50 -0800 | [diff] [blame] | 475 | sk_sp<SkColorSpace> GraphicsJNI::linearColorSpace() { |
Matt Sarett | 89ddb1f | 2017-02-10 13:31:56 -0500 | [diff] [blame] | 476 | return SkColorSpace::MakeSRGBLinear(); |
Romain Guy | 9505a65 | 2016-12-14 09:43:50 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | sk_sp<SkColorSpace> GraphicsJNI::colorSpaceForType(SkColorType type) { |
| 480 | switch (type) { |
| 481 | case kRGBA_F16_SkColorType: |
| 482 | return linearColorSpace(); |
| 483 | default: |
| 484 | return defaultColorSpace(); |
| 485 | } |
| 486 | } |
| 487 | |
Romain Guy | ce217fa | 2017-03-08 15:58:06 -0800 | [diff] [blame] | 488 | bool GraphicsJNI::isColorSpaceSRGB(SkColorSpace* colorSpace) { |
Matt Sarett | 2ecdfc2 | 2017-03-08 17:14:17 -0500 | [diff] [blame] | 489 | return colorSpace == nullptr || colorSpace->isSRGB(); |
Romain Guy | ce217fa | 2017-03-08 15:58:06 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 492 | SkColorSpaceTransferFn GraphicsJNI::getNativeTransferParameters(JNIEnv* env, jobject transferParams) { |
| 493 | SkColorSpaceTransferFn p; |
| 494 | p.fA = (float) env->GetDoubleField(transferParams, gTransferParams_aFieldID); |
| 495 | p.fB = (float) env->GetDoubleField(transferParams, gTransferParams_bFieldID); |
| 496 | p.fC = (float) env->GetDoubleField(transferParams, gTransferParams_cFieldID); |
| 497 | p.fD = (float) env->GetDoubleField(transferParams, gTransferParams_dFieldID); |
| 498 | p.fE = (float) env->GetDoubleField(transferParams, gTransferParams_eFieldID); |
| 499 | p.fF = (float) env->GetDoubleField(transferParams, gTransferParams_fFieldID); |
| 500 | p.fG = (float) env->GetDoubleField(transferParams, gTransferParams_gFieldID); |
| 501 | return p; |
| 502 | } |
| 503 | |
| 504 | SkMatrix44 GraphicsJNI::getNativeXYZMatrix(JNIEnv* env, jfloatArray xyzD50) { |
| 505 | SkMatrix44 xyzMatrix(SkMatrix44::kIdentity_Constructor); |
| 506 | jfloat* array = env->GetFloatArrayElements(xyzD50, NULL); |
| 507 | xyzMatrix.setFloat(0, 0, array[0]); |
| 508 | xyzMatrix.setFloat(1, 0, array[1]); |
| 509 | xyzMatrix.setFloat(2, 0, array[2]); |
| 510 | xyzMatrix.setFloat(0, 1, array[3]); |
| 511 | xyzMatrix.setFloat(1, 1, array[4]); |
| 512 | xyzMatrix.setFloat(2, 1, array[5]); |
| 513 | xyzMatrix.setFloat(0, 2, array[6]); |
| 514 | xyzMatrix.setFloat(1, 2, array[7]); |
| 515 | xyzMatrix.setFloat(2, 2, array[8]); |
| 516 | env->ReleaseFloatArrayElements(xyzD50, array, 0); |
| 517 | return xyzMatrix; |
| 518 | } |
| 519 | |
| 520 | sk_sp<SkColorSpace> GraphicsJNI::getNativeColorSpace(JNIEnv* env, jobject colorSpace) { |
| 521 | if (colorSpace == nullptr) return nullptr; |
| 522 | if (!env->IsInstanceOf(colorSpace, gColorSpaceRGB_class)) { |
| 523 | doThrowIAE(env, "The color space must be an RGB color space"); |
| 524 | } |
| 525 | |
| 526 | jobject transferParams = env->CallObjectMethod(colorSpace, |
| 527 | gColorSpaceRGB_getTransferParametersMethodID); |
| 528 | if (transferParams == nullptr) { |
| 529 | doThrowIAE(env, "The color space must use an ICC parametric transfer function"); |
| 530 | } |
| 531 | |
| 532 | jfloatArray illuminantD50 = (jfloatArray) env->GetStaticObjectField(gColorSpace_class, |
| 533 | gColorSpace_IlluminantD50FieldID); |
| 534 | jobject colorSpaceD50 = env->CallStaticObjectMethod(gColorSpace_class, |
| 535 | gColorSpace_adaptMethodID, colorSpace, illuminantD50); |
| 536 | |
| 537 | jfloatArray xyzD50 = (jfloatArray) env->CallObjectMethod(colorSpaceD50, |
| 538 | gColorSpaceRGB_getTransformMethodID); |
| 539 | |
| 540 | SkMatrix44 xyzMatrix = getNativeXYZMatrix(env, xyzD50); |
| 541 | SkColorSpaceTransferFn transferFunction = getNativeTransferParameters(env, transferParams); |
| 542 | |
| 543 | return SkColorSpace::MakeRGB(transferFunction, xyzMatrix); |
| 544 | } |
| 545 | |
| 546 | |
| 547 | jobject GraphicsJNI::getColorSpace(JNIEnv* env, sk_sp<SkColorSpace>& decodeColorSpace, |
| 548 | SkColorType decodeColorType) { |
| 549 | jobject colorSpace = nullptr; |
| 550 | |
| 551 | // No need to match, we know what the output color space will be |
| 552 | if (decodeColorType == kRGBA_F16_SkColorType) { |
| 553 | jobject linearExtendedSRGB = env->GetStaticObjectField( |
| 554 | gColorSpace_Named_class, gColorSpace_Named_LinearExtendedSRGBFieldID); |
| 555 | colorSpace = env->CallStaticObjectMethod(gColorSpace_class, |
| 556 | gColorSpace_getMethodID, linearExtendedSRGB); |
| 557 | } else { |
| 558 | // Same here, no need to match |
| 559 | if (decodeColorSpace->isSRGB()) { |
| 560 | jobject sRGB = env->GetStaticObjectField( |
| 561 | gColorSpace_Named_class, gColorSpace_Named_sRGBFieldID); |
| 562 | colorSpace = env->CallStaticObjectMethod(gColorSpace_class, |
| 563 | gColorSpace_getMethodID, sRGB); |
| 564 | } else if (decodeColorSpace.get() != nullptr) { |
| 565 | // Try to match against known RGB color spaces using the CIE XYZ D50 |
| 566 | // conversion matrix and numerical transfer function parameters |
| 567 | SkMatrix44 xyzMatrix(SkMatrix44::kUninitialized_Constructor); |
| 568 | LOG_ALWAYS_FATAL_IF(!decodeColorSpace->toXYZD50(&xyzMatrix)); |
| 569 | |
| 570 | SkColorSpaceTransferFn transferParams; |
| 571 | // We can only handle numerical transfer functions at the moment |
| 572 | LOG_ALWAYS_FATAL_IF(!decodeColorSpace->isNumericalTransferFn(&transferParams)); |
| 573 | |
| 574 | jobject params = env->NewObject(gTransferParameters_class, |
| 575 | gTransferParameters_constructorMethodID, |
| 576 | transferParams.fA, transferParams.fB, transferParams.fC, |
| 577 | transferParams.fD, transferParams.fE, transferParams.fF, |
| 578 | transferParams.fG); |
| 579 | |
| 580 | jfloatArray xyzArray = env->NewFloatArray(9); |
| 581 | jfloat xyz[9] = { |
| 582 | xyzMatrix.getFloat(0, 0), |
| 583 | xyzMatrix.getFloat(1, 0), |
| 584 | xyzMatrix.getFloat(2, 0), |
| 585 | xyzMatrix.getFloat(0, 1), |
| 586 | xyzMatrix.getFloat(1, 1), |
| 587 | xyzMatrix.getFloat(2, 1), |
| 588 | xyzMatrix.getFloat(0, 2), |
| 589 | xyzMatrix.getFloat(1, 2), |
| 590 | xyzMatrix.getFloat(2, 2) |
| 591 | }; |
| 592 | env->SetFloatArrayRegion(xyzArray, 0, 9, xyz); |
| 593 | |
| 594 | colorSpace = env->CallStaticObjectMethod(gColorSpace_class, |
| 595 | gColorSpace_matchMethodID, xyzArray, params); |
| 596 | |
| 597 | if (colorSpace == nullptr) { |
| 598 | // We couldn't find an exact match, let's create a new color space |
| 599 | // instance with the 3x3 conversion matrix and transfer function |
| 600 | colorSpace = env->NewObject(gColorSpaceRGB_class, |
| 601 | gColorSpaceRGB_constructorMethodID, |
| 602 | env->NewStringUTF("Unknown"), xyzArray, params); |
| 603 | } |
| 604 | |
| 605 | env->DeleteLocalRef(xyzArray); |
| 606 | } |
| 607 | } |
| 608 | return colorSpace; |
| 609 | } |
| 610 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | /////////////////////////////////////////////////////////////////////////////// |
sergeyv | 4508218 | 2016-09-29 18:25:40 -0700 | [diff] [blame] | 612 | bool HeapAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { |
Mike Reed | 0a8fb52 | 2017-04-13 15:31:32 -0400 | [diff] [blame] | 613 | mStorage = android::Bitmap::allocateHeapBitmap(bitmap, sk_ref_sp(ctable)); |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 614 | return !!mStorage; |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | //////////////////////////////////////////////////////////////////////////////// |
| 618 | |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 619 | RecyclingClippingPixelAllocator::RecyclingClippingPixelAllocator( |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 620 | android::Bitmap* recycledBitmap, size_t recycledBytes) |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 621 | : mRecycledBitmap(recycledBitmap) |
| 622 | , mRecycledBytes(recycledBytes) |
| 623 | , mSkiaBitmap(nullptr) |
| 624 | , mNeedsCopy(false) |
| 625 | {} |
| 626 | |
| 627 | RecyclingClippingPixelAllocator::~RecyclingClippingPixelAllocator() {} |
| 628 | |
| 629 | bool RecyclingClippingPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { |
| 630 | // Ensure that the caller did not pass in a NULL bitmap to the constructor or this |
| 631 | // function. |
| 632 | LOG_ALWAYS_FATAL_IF(!mRecycledBitmap); |
| 633 | LOG_ALWAYS_FATAL_IF(!bitmap); |
| 634 | mSkiaBitmap = bitmap; |
| 635 | |
| 636 | // This behaves differently than the RecyclingPixelAllocator. For backwards |
| 637 | // compatibility, the original color type of the recycled bitmap must be maintained. |
| 638 | if (mRecycledBitmap->info().colorType() != bitmap->colorType()) { |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | // The Skia bitmap specifies the width and height needed by the decoder. |
| 643 | // mRecycledBitmap specifies the width and height of the bitmap that we |
| 644 | // want to reuse. Neither can be changed. We will try to find a way |
| 645 | // to reuse the memory. |
| 646 | const int maxWidth = SkTMax(bitmap->width(), mRecycledBitmap->info().width()); |
| 647 | const int maxHeight = SkTMax(bitmap->height(), mRecycledBitmap->info().height()); |
| 648 | const SkImageInfo maxInfo = bitmap->info().makeWH(maxWidth, maxHeight); |
| 649 | const size_t rowBytes = maxInfo.minRowBytes(); |
| 650 | const size_t bytesNeeded = maxInfo.getSafeSize(rowBytes); |
| 651 | if (bytesNeeded <= mRecycledBytes) { |
| 652 | // Here we take advantage of reconfigure() to reset the rowBytes and ctable |
| 653 | // of mRecycledBitmap. It is very important that we pass in |
| 654 | // mRecycledBitmap->info() for the SkImageInfo. According to the |
| 655 | // specification for BitmapRegionDecoder, we are not allowed to change |
| 656 | // the SkImageInfo. |
Romain Guy | 5545518 | 2017-04-15 21:41:22 -0700 | [diff] [blame] | 657 | // We can (must) preserve the color space since it doesn't affect the |
| 658 | // storage needs |
| 659 | mRecycledBitmap->reconfigure( |
| 660 | mRecycledBitmap->info().makeColorSpace(bitmap->refColorSpace()), |
Romain Guy | 6be8195 | 2017-04-17 12:31:09 -0700 | [diff] [blame] | 661 | rowBytes, sk_ref_sp(ctable)); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 662 | |
Matt Sarett | db4773f | 2016-05-19 09:23:41 -0400 | [diff] [blame] | 663 | // Give the bitmap the same pixelRef as mRecycledBitmap. |
| 664 | // skbug.com/4538: We also need to make sure that the rowBytes on the pixel ref |
| 665 | // match the rowBytes on the bitmap. |
| 666 | bitmap->setInfo(bitmap->info(), rowBytes); |
Mike Reed | 826deef | 2017-04-04 15:32:04 -0400 | [diff] [blame] | 667 | bitmap->setPixelRef(sk_ref_sp(mRecycledBitmap), 0, 0); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 668 | |
| 669 | // Make sure that the recycled bitmap has the correct alpha type. |
| 670 | mRecycledBitmap->setAlphaType(bitmap->alphaType()); |
| 671 | |
Derek Sollenberger | a9471b1 | 2016-03-25 12:19:22 -0400 | [diff] [blame] | 672 | bitmap->notifyPixelsChanged(); |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 673 | mNeedsCopy = false; |
| 674 | |
| 675 | // TODO: If the dimensions of the SkBitmap are smaller than those of |
| 676 | // mRecycledBitmap, should we zero the memory in mRecycledBitmap? |
| 677 | return true; |
| 678 | } |
| 679 | |
| 680 | // In the event that mRecycledBitmap is not large enough, allocate new memory |
| 681 | // on the heap. |
| 682 | SkBitmap::HeapAllocator heapAllocator; |
| 683 | |
| 684 | // We will need to copy from heap memory to mRecycledBitmap's memory after the |
| 685 | // decode is complete. |
| 686 | mNeedsCopy = true; |
| 687 | |
| 688 | return heapAllocator.allocPixelRef(bitmap, ctable); |
| 689 | } |
| 690 | |
| 691 | void RecyclingClippingPixelAllocator::copyIfNecessary() { |
| 692 | if (mNeedsCopy) { |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 693 | mRecycledBitmap->ref(); |
| 694 | SkPixelRef* recycledPixels = mRecycledBitmap; |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 695 | void* dst = recycledPixels->pixels(); |
Matt Sarett | 1819bb5 | 2016-02-05 08:30:11 -0500 | [diff] [blame] | 696 | const size_t dstRowBytes = mRecycledBitmap->rowBytes(); |
| 697 | const size_t bytesToCopy = std::min(mRecycledBitmap->info().minRowBytes(), |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 698 | mSkiaBitmap->info().minRowBytes()); |
Matt Sarett | 1819bb5 | 2016-02-05 08:30:11 -0500 | [diff] [blame] | 699 | const int rowsToCopy = std::min(mRecycledBitmap->info().height(), |
| 700 | mSkiaBitmap->info().height()); |
| 701 | for (int y = 0; y < rowsToCopy; y++) { |
Matt Sarett | 1f97963 | 2015-10-27 10:33:20 -0400 | [diff] [blame] | 702 | memcpy(dst, mSkiaBitmap->getAddr(0, y), bytesToCopy); |
| 703 | dst = SkTAddOffset<void>(dst, dstRowBytes); |
| 704 | } |
| 705 | recycledPixels->notifyPixelsChanged(); |
| 706 | recycledPixels->unref(); |
| 707 | } |
| 708 | mRecycledBitmap = nullptr; |
| 709 | mSkiaBitmap = nullptr; |
| 710 | } |
| 711 | |
| 712 | //////////////////////////////////////////////////////////////////////////////// |
| 713 | |
Riley Andrews | 721ae5f | 2015-05-11 16:08:22 -0700 | [diff] [blame] | 714 | AshmemPixelAllocator::AshmemPixelAllocator(JNIEnv *env) { |
| 715 | LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&mJavaVM) != JNI_OK, |
| 716 | "env->GetJavaVM failed"); |
| 717 | } |
| 718 | |
Riley Andrews | 721ae5f | 2015-05-11 16:08:22 -0700 | [diff] [blame] | 719 | bool AshmemPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { |
Mike Reed | 0a8fb52 | 2017-04-13 15:31:32 -0400 | [diff] [blame] | 720 | mStorage = android::Bitmap::allocateAshmemBitmap(bitmap, sk_ref_sp(ctable)); |
sergeyv | c69853c | 2016-10-07 14:14:09 -0700 | [diff] [blame] | 721 | return !!mStorage; |
Riley Andrews | 721ae5f | 2015-05-11 16:08:22 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | //////////////////////////////////////////////////////////////////////////////// |
| 725 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | int register_android_graphics_Graphics(JNIEnv* env) |
| 727 | { |
| 728 | jmethodID m; |
| 729 | jclass c; |
| 730 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 731 | gRect_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Rect")); |
| 732 | gRect_leftFieldID = GetFieldIDOrDie(env, gRect_class, "left", "I"); |
| 733 | gRect_topFieldID = GetFieldIDOrDie(env, gRect_class, "top", "I"); |
| 734 | gRect_rightFieldID = GetFieldIDOrDie(env, gRect_class, "right", "I"); |
| 735 | gRect_bottomFieldID = GetFieldIDOrDie(env, gRect_class, "bottom", "I"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 737 | gRectF_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/RectF")); |
| 738 | gRectF_leftFieldID = GetFieldIDOrDie(env, gRectF_class, "left", "F"); |
| 739 | gRectF_topFieldID = GetFieldIDOrDie(env, gRectF_class, "top", "F"); |
| 740 | gRectF_rightFieldID = GetFieldIDOrDie(env, gRectF_class, "right", "F"); |
| 741 | gRectF_bottomFieldID = GetFieldIDOrDie(env, gRectF_class, "bottom", "F"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 743 | gPoint_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Point")); |
| 744 | gPoint_xFieldID = GetFieldIDOrDie(env, gPoint_class, "x", "I"); |
| 745 | gPoint_yFieldID = GetFieldIDOrDie(env, gPoint_class, "y", "I"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 747 | gPointF_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/PointF")); |
| 748 | gPointF_xFieldID = GetFieldIDOrDie(env, gPointF_class, "x", "F"); |
| 749 | gPointF_yFieldID = GetFieldIDOrDie(env, gPointF_class, "y", "F"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 751 | gBitmapRegionDecoder_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/BitmapRegionDecoder")); |
| 752 | gBitmapRegionDecoder_constructorMethodID = GetMethodIDOrDie(env, gBitmapRegionDecoder_class, "<init>", "(J)V"); |
Joseph Wen | f1f48bc | 2010-07-19 16:59:51 +0800 | [diff] [blame] | 753 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 754 | gBitmapConfig_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Bitmap$Config")); |
| 755 | gBitmapConfig_nativeInstanceID = GetFieldIDOrDie(env, gBitmapConfig_class, "nativeInt", "I"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 757 | gCanvas_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Canvas")); |
| 758 | gCanvas_nativeInstanceID = GetFieldIDOrDie(env, gCanvas_class, "mNativeCanvasWrapper", "J"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 759 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 760 | gPicture_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Picture")); |
| 761 | gPicture_nativeInstanceID = GetFieldIDOrDie(env, gPicture_class, "mNativePicture", "J"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 763 | gRegion_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Region")); |
| 764 | gRegion_nativeInstanceID = GetFieldIDOrDie(env, gRegion_class, "mNativeRegion", "J"); |
| 765 | gRegion_constructorMethodID = GetMethodIDOrDie(env, gRegion_class, "<init>", "(JI)V"); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 766 | |
Mathieu Chartier | 7384b42 | 2013-10-17 18:16:42 -0700 | [diff] [blame] | 767 | c = env->FindClass("java/lang/Byte"); |
Mathieu Chartier | 6ecb7a9 | 2013-10-18 11:04:11 -0700 | [diff] [blame] | 768 | gByte_class = (jclass) env->NewGlobalRef( |
Mathieu Chartier | 7384b42 | 2013-10-17 18:16:42 -0700 | [diff] [blame] | 769 | env->GetStaticObjectField(c, env->GetStaticFieldID(c, "TYPE", "Ljava/lang/Class;"))); |
| 770 | |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 771 | gVMRuntime_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "dalvik/system/VMRuntime")); |
Mathieu Chartier | 7384b42 | 2013-10-17 18:16:42 -0700 | [diff] [blame] | 772 | m = env->GetStaticMethodID(gVMRuntime_class, "getRuntime", "()Ldalvik/system/VMRuntime;"); |
| 773 | gVMRuntime = env->NewGlobalRef(env->CallStaticObjectMethod(gVMRuntime_class, m)); |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 774 | gVMRuntime_newNonMovableArray = GetMethodIDOrDie(env, gVMRuntime_class, "newNonMovableArray", |
Mathieu Chartier | 7384b42 | 2013-10-17 18:16:42 -0700 | [diff] [blame] | 775 | "(Ljava/lang/Class;I)Ljava/lang/Object;"); |
Romain Guy | 95648b8 | 2017-04-13 18:43:42 -0700 | [diff] [blame] | 776 | gVMRuntime_addressOf = GetMethodIDOrDie(env, gVMRuntime_class, "addressOf", "(Ljava/lang/Object;)J"); |
| 777 | |
| 778 | jclass transfer_params_class = FindClassOrDie(env, "android/graphics/ColorSpace$Rgb$TransferParameters"); |
| 779 | gTransferParams_aFieldID = GetFieldIDOrDie(env, transfer_params_class, "a", "D"); |
| 780 | gTransferParams_bFieldID = GetFieldIDOrDie(env, transfer_params_class, "b", "D"); |
| 781 | gTransferParams_cFieldID = GetFieldIDOrDie(env, transfer_params_class, "c", "D"); |
| 782 | gTransferParams_dFieldID = GetFieldIDOrDie(env, transfer_params_class, "d", "D"); |
| 783 | gTransferParams_eFieldID = GetFieldIDOrDie(env, transfer_params_class, "e", "D"); |
| 784 | gTransferParams_fFieldID = GetFieldIDOrDie(env, transfer_params_class, "f", "D"); |
| 785 | gTransferParams_gFieldID = GetFieldIDOrDie(env, transfer_params_class, "g", "D"); |
| 786 | |
| 787 | gColorSpace_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/ColorSpace")); |
| 788 | gColorSpace_IlluminantD50FieldID = GetStaticFieldIDOrDie(env, |
| 789 | gColorSpace_class, "ILLUMINANT_D50", "[F"); |
| 790 | gColorSpace_adaptMethodID = GetStaticMethodIDOrDie(env, gColorSpace_class, "adapt", |
| 791 | "(Landroid/graphics/ColorSpace;[F)Landroid/graphics/ColorSpace;"); |
| 792 | gColorSpace_getMethodID = GetStaticMethodIDOrDie(env, gColorSpace_class, |
| 793 | "get", "(Landroid/graphics/ColorSpace$Named;)Landroid/graphics/ColorSpace;"); |
| 794 | gColorSpace_matchMethodID = GetStaticMethodIDOrDie(env, gColorSpace_class, "match", |
| 795 | "([FLandroid/graphics/ColorSpace$Rgb$TransferParameters;)Landroid/graphics/ColorSpace;"); |
| 796 | |
| 797 | gColorSpaceRGB_class = MakeGlobalRefOrDie(env, |
| 798 | FindClassOrDie(env, "android/graphics/ColorSpace$Rgb")); |
| 799 | gColorSpaceRGB_constructorMethodID = GetMethodIDOrDie(env, gColorSpaceRGB_class, |
| 800 | "<init>", "(Ljava/lang/String;[FLandroid/graphics/ColorSpace$Rgb$TransferParameters;)V"); |
| 801 | gColorSpaceRGB_getTransferParametersMethodID = GetMethodIDOrDie(env, gColorSpaceRGB_class, |
| 802 | "getTransferParameters", "()Landroid/graphics/ColorSpace$Rgb$TransferParameters;"); |
| 803 | gColorSpaceRGB_getTransformMethodID = GetMethodIDOrDie(env, gColorSpaceRGB_class, |
| 804 | "getTransform", "()[F"); |
| 805 | |
| 806 | gColorSpace_Named_class = MakeGlobalRefOrDie(env, |
| 807 | FindClassOrDie(env, "android/graphics/ColorSpace$Named")); |
| 808 | gColorSpace_Named_sRGBFieldID = GetStaticFieldIDOrDie(env, |
| 809 | gColorSpace_Named_class, "SRGB", "Landroid/graphics/ColorSpace$Named;"); |
| 810 | gColorSpace_Named_LinearExtendedSRGBFieldID = GetStaticFieldIDOrDie(env, |
| 811 | gColorSpace_Named_class, "LINEAR_EXTENDED_SRGB", "Landroid/graphics/ColorSpace$Named;"); |
| 812 | |
| 813 | gTransferParameters_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, |
| 814 | "android/graphics/ColorSpace$Rgb$TransferParameters")); |
| 815 | gTransferParameters_constructorMethodID = GetMethodIDOrDie(env, gTransferParameters_class, |
| 816 | "<init>", "(DDDDDDD)V"); |
Mathieu Chartier | 7384b42 | 2013-10-17 18:16:42 -0700 | [diff] [blame] | 817 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | return 0; |
| 819 | } |