11a9c27c312ba20b2ceafcde18ce451724782d2a5Ficus Kirkpatrick#define LOG_TAG "GraphicsJNI"
21a9c27c312ba20b2ceafcde18ce451724782d2a5Ficus Kirkpatrick
339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews#include <unistd.h>
439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews#include <sys/mman.h>
539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "jni.h"
78451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes#include "JNIHelp.h"
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "GraphicsJNI.h"
9e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
10e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy#include "SkCanvas.h"
11e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy#include "SkDevice.h"
1246cb9bdbf56b27cbf2ab878b41d21e30896a4feaLeon Scroggins#include "SkMath.h"
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkRegion.h"
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <android_runtime/AndroidRuntime.h>
1539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews#include <cutils/ashmem.h>
16dccca44ffda4836b56a21da95a046c9708ffd49csergeyv#include <hwui/Canvas.h>
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
183d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger#include <Caches.h>
193d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger#include <TextureCache.h>
203d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid doThrowNPE(JNIEnv* env) {
2269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes    jniThrowNullPointerException(env, NULL);
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid doThrowAIOOBE(JNIEnv* env) {
268451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes    jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid doThrowRE(JNIEnv* env, const char* msg) {
3069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes    jniThrowRuntimeException(env, msg);
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid doThrowIAE(JNIEnv* env, const char* msg) {
348451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes    jniThrowException(env, "java/lang/IllegalArgumentException", msg);
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid doThrowISE(JNIEnv* env, const char* msg) {
388451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes    jniThrowException(env, "java/lang/IllegalStateException", msg);
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid doThrowOOME(JNIEnv* env, const char* msg) {
428451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes    jniThrowException(env, "java/lang/OutOfMemoryError", msg);
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
45f1f48bc7f200f54c76b22d845d8ba8419879b375Joseph Wenvoid doThrowIOE(JNIEnv* env, const char* msg) {
468451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes    jniThrowException(env, "java/io/IOException", msg);
47f1f48bc7f200f54c76b22d845d8ba8419879b375Joseph Wen}
48f1f48bc7f200f54c76b22d845d8ba8419879b375Joseph Wen
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectbool GraphicsJNI::hasException(JNIEnv *env) {
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (env->ExceptionCheck() != 0) {
513762c311729fe9f3af085c14c5c1fb471d994c03Steve Block        ALOGE("*** Uncaught exception returned from Java call!\n");
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ExceptionDescribe();
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return true;
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return false;
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectAutoJavaFloatArray::AutoJavaFloatArray(JNIEnv* env, jfloatArray array,
61c04851fd0af87f44a7d7351e0c17442fa1d3fc28Mike Reed                                       int minLength, JNIAccess access)
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project: fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env);
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (array) {
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fLen = env->GetArrayLength(array);
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (fLen < minLength) {
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            sk_throw();
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fPtr = env->GetFloatArrayElements(array, NULL);
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
71c04851fd0af87f44a7d7351e0c17442fa1d3fc28Mike Reed    fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0;
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectAutoJavaFloatArray::~AutoJavaFloatArray() {
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fPtr) {
76c04851fd0af87f44a7d7351e0c17442fa1d3fc28Mike Reed        fEnv->ReleaseFloatArrayElements(fArray, fPtr, fReleaseMode);
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectAutoJavaIntArray::AutoJavaIntArray(JNIEnv* env, jintArray array,
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                       int minLength)
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project: fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env);
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (array) {
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fLen = env->GetArrayLength(array);
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (fLen < minLength) {
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            sk_throw();
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fPtr = env->GetIntArrayElements(array, NULL);
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectAutoJavaIntArray::~AutoJavaIntArray() {
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fPtr) {
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fEnv->ReleaseIntArrayElements(fArray, fPtr, 0);
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectAutoJavaShortArray::AutoJavaShortArray(JNIEnv* env, jshortArray array,
100c04851fd0af87f44a7d7351e0c17442fa1d3fc28Mike Reed                                       int minLength, JNIAccess access)
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project: fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env);
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (array) {
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fLen = env->GetArrayLength(array);
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (fLen < minLength) {
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            sk_throw();
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fPtr = env->GetShortArrayElements(array, NULL);
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
110c04851fd0af87f44a7d7351e0c17442fa1d3fc28Mike Reed    fReleaseMode = (access == kRO_JNIAccess) ? JNI_ABORT : 0;
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectAutoJavaShortArray::~AutoJavaShortArray() {
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fPtr) {
115c04851fd0af87f44a7d7351e0c17442fa1d3fc28Mike Reed        fEnv->ReleaseShortArrayElements(fArray, fPtr, fReleaseMode);
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectAutoJavaByteArray::AutoJavaByteArray(JNIEnv* env, jbyteArray array,
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                       int minLength)
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project: fEnv(env), fArray(array), fPtr(NULL), fLen(0) {
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env);
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (array) {
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fLen = env->GetArrayLength(array);
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (fLen < minLength) {
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            sk_throw();
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fPtr = env->GetByteArrayElements(array, NULL);
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectAutoJavaByteArray::~AutoJavaByteArray() {
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fPtr) {
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fEnv->ReleaseByteArrayElements(fArray, fPtr, 0);
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jclass   gRect_class;
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gRect_leftFieldID;
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gRect_topFieldID;
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gRect_rightFieldID;
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gRect_bottomFieldID;
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jclass   gRectF_class;
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gRectF_leftFieldID;
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gRectF_topFieldID;
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gRectF_rightFieldID;
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gRectF_bottomFieldID;
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jclass   gPoint_class;
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gPoint_xFieldID;
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gPoint_yFieldID;
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jclass   gPointF_class;
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gPointF_xFieldID;
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gPointF_yFieldID;
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jclass   gBitmap_class;
161f29ed28c7b878ef28058bc730715d0d32445bc57John Reckstatic jfieldID gBitmap_nativePtr;
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jmethodID gBitmap_constructorMethodID;
1639f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craikstatic jmethodID gBitmap_reinitMethodID;
1649f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craikstatic jmethodID gBitmap_getAllocationByteCountMethodID;
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jclass   gBitmapConfig_class;
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gBitmapConfig_nativeInstanceID;
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1696b849e2123be98eb2a1a25b8abf0b13a279ce952Wei-Ta Chenstatic jclass   gBitmapRegionDecoder_class;
1706b849e2123be98eb2a1a25b8abf0b13a279ce952Wei-Ta Chenstatic jmethodID gBitmapRegionDecoder_constructorMethodID;
171f1f48bc7f200f54c76b22d845d8ba8419879b375Joseph Wen
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jclass   gCanvas_class;
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gCanvas_nativeInstanceID;
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jclass   gPicture_class;
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gPicture_nativeInstanceID;
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jclass   gRegion_class;
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jfieldID gRegion_nativeInstanceID;
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jmethodID gRegion_constructorMethodID;
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1827384b428c445f07540fd6a80a21727d03be65d96Mathieu Chartierstatic jclass    gByte_class;
1837384b428c445f07540fd6a80a21727d03be65d96Mathieu Chartierstatic jobject   gVMRuntime;
1847384b428c445f07540fd6a80a21727d03be65d96Mathieu Chartierstatic jclass    gVMRuntime_class;
1857384b428c445f07540fd6a80a21727d03be65d96Mathieu Chartierstatic jmethodID gVMRuntime_newNonMovableArray;
1867384b428c445f07540fd6a80a21727d03be65d96Mathieu Chartierstatic jmethodID gVMRuntime_addressOf;
1877384b428c445f07540fd6a80a21727d03be65d96Mathieu Chartier
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid GraphicsJNI::get_jrect(JNIEnv* env, jobject obj, int* L, int* T, int* R, int* B)
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(obj, gRect_class));
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    *L = env->GetIntField(obj, gRect_leftFieldID);
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    *T = env->GetIntField(obj, gRect_topFieldID);
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    *R = env->GetIntField(obj, gRect_rightFieldID);
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    *B = env->GetIntField(obj, gRect_bottomFieldID);
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid GraphicsJNI::set_jrect(JNIEnv* env, jobject obj, int L, int T, int R, int B)
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(obj, gRect_class));
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(obj, gRect_leftFieldID, L);
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(obj, gRect_topFieldID, T);
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(obj, gRect_rightFieldID, R);
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(obj, gRect_bottomFieldID, B);
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectSkIRect* GraphicsJNI::jrect_to_irect(JNIEnv* env, jobject obj, SkIRect* ir)
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(obj, gRect_class));
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ir->set(env->GetIntField(obj, gRect_leftFieldID),
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->GetIntField(obj, gRect_topFieldID),
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->GetIntField(obj, gRect_rightFieldID),
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->GetIntField(obj, gRect_bottomFieldID));
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return ir;
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid GraphicsJNI::irect_to_jrect(const SkIRect& ir, JNIEnv* env, jobject obj)
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(obj, gRect_class));
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(obj, gRect_leftFieldID, ir.fLeft);
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(obj, gRect_topFieldID, ir.fTop);
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(obj, gRect_rightFieldID, ir.fRight);
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(obj, gRect_bottomFieldID, ir.fBottom);
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectSkRect* GraphicsJNI::jrectf_to_rect(JNIEnv* env, jobject obj, SkRect* r)
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(obj, gRectF_class));
2348451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2352e0103eb340822f9d580c1aa8492bae8394b8243Leon Scroggins III    r->set(env->GetFloatField(obj, gRectF_leftFieldID),
2362e0103eb340822f9d580c1aa8492bae8394b8243Leon Scroggins III           env->GetFloatField(obj, gRectF_topFieldID),
2372e0103eb340822f9d580c1aa8492bae8394b8243Leon Scroggins III           env->GetFloatField(obj, gRectF_rightFieldID),
2382e0103eb340822f9d580c1aa8492bae8394b8243Leon Scroggins III           env->GetFloatField(obj, gRectF_bottomFieldID));
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return r;
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectSkRect* GraphicsJNI::jrect_to_rect(JNIEnv* env, jobject obj, SkRect* r)
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(obj, gRect_class));
2458451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    r->set(SkIntToScalar(env->GetIntField(obj, gRect_leftFieldID)),
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project           SkIntToScalar(env->GetIntField(obj, gRect_topFieldID)),
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project           SkIntToScalar(env->GetIntField(obj, gRect_rightFieldID)),
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project           SkIntToScalar(env->GetIntField(obj, gRect_bottomFieldID)));
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return r;
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid GraphicsJNI::rect_to_jrectf(const SkRect& r, JNIEnv* env, jobject obj)
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(obj, gRectF_class));
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetFloatField(obj, gRectF_leftFieldID, SkScalarToFloat(r.fLeft));
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetFloatField(obj, gRectF_topFieldID, SkScalarToFloat(r.fTop));
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetFloatField(obj, gRectF_rightFieldID, SkScalarToFloat(r.fRight));
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetFloatField(obj, gRectF_bottomFieldID, SkScalarToFloat(r.fBottom));
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectSkIPoint* GraphicsJNI::jpoint_to_ipoint(JNIEnv* env, jobject obj, SkIPoint* point)
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(obj, gPoint_class));
2668451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    point->set(env->GetIntField(obj, gPoint_xFieldID),
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project               env->GetIntField(obj, gPoint_yFieldID));
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return point;
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid GraphicsJNI::ipoint_to_jpoint(const SkIPoint& ir, JNIEnv* env, jobject obj)
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(obj, gPoint_class));
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(obj, gPoint_xFieldID, ir.fX);
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(obj, gPoint_yFieldID, ir.fY);
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectSkPoint* GraphicsJNI::jpointf_to_point(JNIEnv* env, jobject obj, SkPoint* point)
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(obj, gPointF_class));
2838451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
2842e0103eb340822f9d580c1aa8492bae8394b8243Leon Scroggins III    point->set(env->GetIntField(obj, gPointF_xFieldID),
2852e0103eb340822f9d580c1aa8492bae8394b8243Leon Scroggins III               env->GetIntField(obj, gPointF_yFieldID));
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return point;
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid GraphicsJNI::point_to_jpointf(const SkPoint& r, JNIEnv* env, jobject obj)
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(obj, gPointF_class));
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetFloatField(obj, gPointF_xFieldID, SkScalarToFloat(r.fX));
2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetFloatField(obj, gPointF_yFieldID, SkScalarToFloat(r.fY));
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2971103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed// This enum must keep these int values, to match the int values
2981103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed// in the java Bitmap.Config enum.
2991103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reedenum LegacyBitmapConfig {
3001103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    kNo_LegacyBitmapConfig          = 0,
3011103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    kA8_LegacyBitmapConfig          = 1,
3021103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    kIndex8_LegacyBitmapConfig      = 2,
3031103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    kRGB_565_LegacyBitmapConfig     = 3,
3041103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    kARGB_4444_LegacyBitmapConfig   = 4,
3051103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    kARGB_8888_LegacyBitmapConfig   = 5,
3061103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed
3071103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    kLastEnum_LegacyBitmapConfig = kARGB_8888_LegacyBitmapConfig
3081103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed};
3091103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed
3101103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reedjint GraphicsJNI::colorTypeToLegacyBitmapConfig(SkColorType colorType) {
3111103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    switch (colorType) {
3121103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        case kN32_SkColorType:
3131103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed            return kARGB_8888_LegacyBitmapConfig;
3141103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        case kARGB_4444_SkColorType:
3151103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed            return kARGB_4444_LegacyBitmapConfig;
3161103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        case kRGB_565_SkColorType:
3171103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed            return kRGB_565_LegacyBitmapConfig;
3181103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        case kIndex_8_SkColorType:
3191103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed            return kIndex8_LegacyBitmapConfig;
3201103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        case kAlpha_8_SkColorType:
3211103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed            return kA8_LegacyBitmapConfig;
3221103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        case kUnknown_SkColorType:
3231103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        default:
3241103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed            break;
3251103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    }
3261103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    return kNo_LegacyBitmapConfig;
3271103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed}
3281103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed
3291103b3255945d2eb2fa9c191e84e2270b343cca9Mike ReedSkColorType GraphicsJNI::legacyBitmapConfigToColorType(jint legacyConfig) {
3301103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    const uint8_t gConfig2ColorType[] = {
3311103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        kUnknown_SkColorType,
3321103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        kAlpha_8_SkColorType,
3331103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        kIndex_8_SkColorType,
3341103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        kRGB_565_SkColorType,
3351103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        kARGB_4444_SkColorType,
3361103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        kN32_SkColorType
3371103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    };
3381103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed
3391103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    if (legacyConfig < 0 || legacyConfig > kLastEnum_LegacyBitmapConfig) {
3401103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        legacyConfig = kNo_LegacyBitmapConfig;
3411103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    }
3421103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    return static_cast<SkColorType>(gConfig2ColorType[legacyConfig]);
3431103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed}
3441103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed
345f29ed28c7b878ef28058bc730715d0d32445bc57John Reckandroid::Bitmap* GraphicsJNI::getBitmap(JNIEnv* env, jobject bitmap) {
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env);
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(bitmap);
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(bitmap, gBitmap_class));
349f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    jlong bitmapHandle = env->GetLongField(bitmap, gBitmap_nativePtr);
350f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    android::Bitmap* b = reinterpret_cast<android::Bitmap*>(bitmapHandle);
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(b);
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return b;
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
355ed207b92747234eac88dd3664ecfb535e45d8ed1John Reckvoid GraphicsJNI::getSkBitmap(JNIEnv* env, jobject bitmap, SkBitmap* outBitmap) {
356f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    getBitmap(env, bitmap)->getSkBitmap(outBitmap);
357ed207b92747234eac88dd3664ecfb535e45d8ed1John Reck}
358ed207b92747234eac88dd3664ecfb535e45d8ed1John Reck
359ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn ReckSkPixelRef* GraphicsJNI::refSkPixelRef(JNIEnv* env, jobject bitmap) {
360ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    return getBitmap(env, bitmap)->refPixelRef();
361ed207b92747234eac88dd3664ecfb535e45d8ed1John Reck}
362ed207b92747234eac88dd3664ecfb535e45d8ed1John Reck
36342a1d08df7d417fd4e67eabc91ff05ee77fd9995Mike ReedSkColorType GraphicsJNI::getNativeBitmapColorType(JNIEnv* env, jobject jconfig) {
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env);
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == jconfig) {
36642a1d08df7d417fd4e67eabc91ff05ee77fd9995Mike Reed        return kUnknown_SkColorType;
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(jconfig, gBitmapConfig_class));
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int c = env->GetIntField(jconfig, gBitmapConfig_nativeInstanceID);
3701103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed    return legacyBitmapConfigToColorType(c);
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
373c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reckandroid::Canvas* GraphicsJNI::getNativeCanvas(JNIEnv* env, jobject canvas) {
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env);
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(canvas);
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(canvas, gCanvas_class));
37736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    jlong canvasHandle = env->GetLongField(canvas, gCanvas_nativeInstanceID);
378de92f4c72be8537d2efff6024390b9ab706286e1Bo Liu    if (!canvasHandle) {
379de92f4c72be8537d2efff6024390b9ab706286e1Bo Liu        return NULL;
380de92f4c72be8537d2efff6024390b9ab706286e1Bo Liu    }
381c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reck    return reinterpret_cast<android::Canvas*>(canvasHandle);
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectSkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region)
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env);
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(region);
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(env->IsInstanceOf(region, gRegion_class));
38936bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    jlong regionHandle = env->GetLongField(region, gRegion_nativeInstanceID);
39036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    SkRegion* r = reinterpret_cast<SkRegion*>(regionHandle);
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(r);
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return r;
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////////////////
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3978790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III// Assert that bitmap's SkAlphaType is consistent with isPremultiplied.
398f29ed28c7b878ef28058bc730715d0d32445bc57John Reckstatic void assert_premultiplied(const SkImageInfo& info, bool isPremultiplied) {
3998790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III    // kOpaque_SkAlphaType and kIgnore_SkAlphaType mean that isPremultiplied is
4008790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III    // irrelevant. This just tests to ensure that the SkAlphaType is not
4018790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III    // opposite of isPremultiplied.
4028790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III    if (isPremultiplied) {
403f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        SkASSERT(info.alphaType() != kUnpremul_SkAlphaType);
4048790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III    } else {
405f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        SkASSERT(info.alphaType() != kPremul_SkAlphaType);
4068790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III    }
4078790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III}
4088790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III
409f29ed28c7b878ef28058bc730715d0d32445bc57John Reckjobject GraphicsJNI::createBitmap(JNIEnv* env, android::Bitmap* bitmap,
410f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        int bitmapCreateFlags, jbyteArray ninePatchChunk, jobject ninePatchInsets,
411f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        int density) {
4121abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    bool isMutable = bitmapCreateFlags & kBitmapCreateFlag_Mutable;
4131abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    bool isPremultiplied = bitmapCreateFlags & kBitmapCreateFlag_Premultiplied;
4148790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III    // The caller needs to have already set the alpha type properly, so the
4158790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III    // native SkBitmap stays in sync with the Java Bitmap.
416f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    assert_premultiplied(bitmap->info(), isPremultiplied);
4178790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III
418cf6f7a0f006c0fcf59bb634cbe79f2a8500fd92aElliott Hughes    jobject obj = env->NewObject(gBitmap_class, gBitmap_constructorMethodID,
419f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            reinterpret_cast<jlong>(bitmap), bitmap->javaByteArray(),
4201abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik            bitmap->width(), bitmap->height(), density, isMutable, isPremultiplied,
42147cd8e921db73e894f94ec4729ade90da50996f5Chris Craik            ninePatchChunk, ninePatchInsets);
422cf6f7a0f006c0fcf59bb634cbe79f2a8500fd92aElliott Hughes    hasException(env); // For the side effect of logging.
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return obj;
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4256b849e2123be98eb2a1a25b8abf0b13a279ce952Wei-Ta Chen
426f29ed28c7b878ef28058bc730715d0d32445bc57John Reckvoid GraphicsJNI::reinitBitmap(JNIEnv* env, jobject javaBitmap, const SkImageInfo& info,
4271abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        bool isPremultiplied)
4289f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik{
4298790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III    // The caller needs to have already set the alpha type properly, so the
4308790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III    // native SkBitmap stays in sync with the Java Bitmap.
431f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    assert_premultiplied(info, isPremultiplied);
4328790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III
4331abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    env->CallVoidMethod(javaBitmap, gBitmap_reinitMethodID,
434f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            info.width(), info.height(), isPremultiplied);
4359f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik}
4369f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik
4379f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craikint GraphicsJNI::getBitmapAllocationByteCount(JNIEnv* env, jobject javaBitmap)
4389f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik{
4399f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    return env->CallIntMethod(javaBitmap, gBitmap_getAllocationByteCountMethodID);
4409f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik}
441e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
4421f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarettjobject GraphicsJNI::createBitmapRegionDecoder(JNIEnv* env, SkBitmapRegionDecoder* bitmap)
443f1f48bc7f200f54c76b22d845d8ba8419879b375Joseph Wen{
444f1f48bc7f200f54c76b22d845d8ba8419879b375Joseph Wen    SkASSERT(bitmap != NULL);
445f1f48bc7f200f54c76b22d845d8ba8419879b375Joseph Wen
446cf6f7a0f006c0fcf59bb634cbe79f2a8500fd92aElliott Hughes    jobject obj = env->NewObject(gBitmapRegionDecoder_class,
447cf6f7a0f006c0fcf59bb634cbe79f2a8500fd92aElliott Hughes            gBitmapRegionDecoder_constructorMethodID,
448b091d47a2e31a30581aa210419ff09bcc8715cdfAshok Bhat            reinterpret_cast<jlong>(bitmap));
449cf6f7a0f006c0fcf59bb634cbe79f2a8500fd92aElliott Hughes    hasException(env); // For the side effect of logging.
450f1f48bc7f200f54c76b22d845d8ba8419879b375Joseph Wen    return obj;
451f1f48bc7f200f54c76b22d845d8ba8419879b375Joseph Wen}
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjobject GraphicsJNI::createRegion(JNIEnv* env, SkRegion* region)
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(region != NULL);
456cf6f7a0f006c0fcf59bb634cbe79f2a8500fd92aElliott Hughes    jobject obj = env->NewObject(gRegion_class, gRegion_constructorMethodID,
45736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat                                 reinterpret_cast<jlong>(region), 0);
458cf6f7a0f006c0fcf59bb634cbe79f2a8500fd92aElliott Hughes    hasException(env); // For the side effect of logging.
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return obj;
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNIEnv* vm2env(JavaVM* vm)
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    JNIEnv* env = NULL;
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK || NULL == env)
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkDebugf("------- [%p] vm->GetEnv() failed\n", vm);
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        sk_throw();
4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return env;
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
475d37095b74eac610a719826311693078a670e9f71Derek Sollenbergerstatic bool computeAllocationSize(const SkBitmap& bitmap, size_t* size) {
476d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    int32_t rowBytes32 = SkToS32(bitmap.rowBytes());
477d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    int64_t bigSize = (int64_t)bitmap.height() * rowBytes32;
478d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    if (rowBytes32 < 0 || !sk_64_isS32(bigSize)) {
479d37095b74eac610a719826311693078a670e9f71Derek Sollenberger        return false; // allocation will be too large
480d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    }
4813d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
482d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    *size = sk_64_asS32(bigSize);
483d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    return true;
4843d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger}
4853d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
486f29ed28c7b878ef28058bc730715d0d32445bc57John Reckandroid::Bitmap* GraphicsJNI::allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap,
487e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy                                             SkColorTable* ctable) {
48846cb9bdbf56b27cbf2ab878b41d21e30896a4feaLeon Scroggins    const SkImageInfo& info = bitmap->info();
489f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    if (info.colorType() == kUnknown_SkColorType) {
49046cb9bdbf56b27cbf2ab878b41d21e30896a4feaLeon Scroggins        doThrowIAE(env, "unknown bitmap configuration");
491e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        return NULL;
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
493b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger
494d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    size_t size;
495d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    if (!computeAllocationSize(*bitmap, &size)) {
4963d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger        return NULL;
4973d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    }
4983d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
499d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    // we must respect the rowBytes value already set on the bitmap instead of
500d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    // attempting to compute our own.
501d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    const size_t rowBytes = bitmap->rowBytes();
502d37095b74eac610a719826311693078a670e9f71Derek Sollenberger
5037384b428c445f07540fd6a80a21727d03be65d96Mathieu Chartier    jbyteArray arrayObj = (jbyteArray) env->CallObjectMethod(gVMRuntime,
5047384b428c445f07540fd6a80a21727d03be65d96Mathieu Chartier                                                             gVMRuntime_newNonMovableArray,
5057384b428c445f07540fd6a80a21727d03be65d96Mathieu Chartier                                                             gByte_class, size);
506a1a19d28d0e432c2d90e4fd73146891c57d01479Mathieu Chartier    if (env->ExceptionCheck() != 0) {
507a1a19d28d0e432c2d90e4fd73146891c57d01479Mathieu Chartier        return NULL;
508a1a19d28d0e432c2d90e4fd73146891c57d01479Mathieu Chartier    }
509a1a19d28d0e432c2d90e4fd73146891c57d01479Mathieu Chartier    SkASSERT(arrayObj);
510a1a19d28d0e432c2d90e4fd73146891c57d01479Mathieu Chartier    jbyte* addr = (jbyte*) env->CallLongMethod(gVMRuntime, gVMRuntime_addressOf, arrayObj);
511a1a19d28d0e432c2d90e4fd73146891c57d01479Mathieu Chartier    if (env->ExceptionCheck() != 0) {
512a1a19d28d0e432c2d90e4fd73146891c57d01479Mathieu Chartier        return NULL;
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
514a1a19d28d0e432c2d90e4fd73146891c57d01479Mathieu Chartier    SkASSERT(addr);
515f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    android::Bitmap* wrapper = new android::Bitmap(env, arrayObj, (void*) addr,
516f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            info, rowBytes, ctable);
517f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    wrapper->getSkBitmap(bitmap);
518a1a19d28d0e432c2d90e4fd73146891c57d01479Mathieu Chartier    // since we're already allocated, we lockPixels right away
519a1a19d28d0e432c2d90e4fd73146891c57d01479Mathieu Chartier    // HeapAllocator behaves this way too
520a1a19d28d0e432c2d90e4fd73146891c57d01479Mathieu Chartier    bitmap->lockPixels();
521e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
522f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return wrapper;
523e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy}
524e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
5253d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenbergerstruct AndroidPixelRefContext {
5263d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    int32_t stableID;
5273d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger};
5283d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
5293d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenbergerstatic void allocatePixelsReleaseProc(void* ptr, void* ctx) {
5303d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    AndroidPixelRefContext* context = (AndroidPixelRefContext*)ctx;
5313d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    if (android::uirenderer::Caches::hasInstance()) {
5323d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger         android::uirenderer::Caches::getInstance().textureCache.releaseTexture(context->stableID);
5333d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    }
5343d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
5353d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    sk_free(ptr);
5363d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    delete context;
5373d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger}
5383d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
5393d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenbergerbool GraphicsJNI::allocatePixels(JNIEnv* env, SkBitmap* bitmap, SkColorTable* ctable) {
5403d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    const SkImageInfo& info = bitmap->info();
541f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    if (info.colorType() == kUnknown_SkColorType) {
5423d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger        doThrowIAE(env, "unknown bitmap configuration");
5433d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger        return NULL;
5443d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    }
5453d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
546d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    size_t size;
547d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    if (!computeAllocationSize(*bitmap, &size)) {
5483d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger        return false;
5493d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    }
5503d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
551d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    // we must respect the rowBytes value already set on the bitmap instead of
552d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    // attempting to compute our own.
553d37095b74eac610a719826311693078a670e9f71Derek Sollenberger    const size_t rowBytes = bitmap->rowBytes();
554d37095b74eac610a719826311693078a670e9f71Derek Sollenberger
5553d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    void* addr = sk_malloc_flags(size, 0);
5563d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    if (NULL == addr) {
5573d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger        return false;
5583d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    }
5593d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
5603d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    AndroidPixelRefContext* context = new AndroidPixelRefContext;
5613d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    SkMallocPixelRef* pr = SkMallocPixelRef::NewWithProc(info, rowBytes, ctable, addr,
5623d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger                                                         &allocatePixelsReleaseProc, context);
5633d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    if (!pr) {
5643d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger        delete context;
5653d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger        return false;
5663d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    }
5673d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
5683d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    // set the stableID in the context so that it can be used later in
5693d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    // allocatePixelsReleaseProc to remove the texture from the cache.
5703d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    context->stableID = pr->getStableID();
5713d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
5723d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    bitmap->setPixelRef(pr)->unref();
5733d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    // since we're already allocated, we can lockPixels right away
5743d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    bitmap->lockPixels();
5753d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
5763d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    return true;
5773d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger}
5783d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger
57939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrewsandroid::Bitmap* GraphicsJNI::allocateAshmemPixelRef(JNIEnv* env, SkBitmap* bitmap,
58039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews                                                     SkColorTable* ctable) {
58139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    int fd;
58239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
58339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    const SkImageInfo& info = bitmap->info();
584f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    if (info.colorType() == kUnknown_SkColorType) {
58539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        doThrowIAE(env, "unknown bitmap configuration");
58639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        return nullptr;
58739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    }
58839d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
58939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    size_t size;
59039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    if (!computeAllocationSize(*bitmap, &size)) {
59139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        return nullptr;
59239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    }
59339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
59439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    // we must respect the rowBytes value already set on the bitmap instead of
59539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    // attempting to compute our own.
59639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    const size_t rowBytes = bitmap->rowBytes();
59739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
59839d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    // Create new ashmem region with read/write priv
59939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    fd = ashmem_create_region("bitmap", size);
60039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    if (fd < 0) {
60139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        return nullptr;
60239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    }
60339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
60439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    void* addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
60539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    if (addr == MAP_FAILED) {
60639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        close(fd);
60739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        return nullptr;
60839d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    }
60939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
61039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    if (ashmem_set_prot_region(fd, PROT_READ) < 0) {
61139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        munmap(addr, size);
61239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        close(fd);
61339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        return nullptr;
61439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    }
61539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
61639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    android::Bitmap* wrapper = new android::Bitmap(addr, fd, info, rowBytes, ctable);
61739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    wrapper->getSkBitmap(bitmap);
61839d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    // since we're already allocated, we lockPixels right away
61939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    // HeapAllocator behaves this way too
62039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    bitmap->lockPixels();
62139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
62239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    return wrapper;
62339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews}
62439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
62539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrewsandroid::Bitmap* GraphicsJNI::mapAshmemPixelRef(JNIEnv* env, SkBitmap* bitmap,
626a316c5dfbc6355f536d765959cacb06bbfed76adJeff Brown        SkColorTable* ctable, int fd, void* addr, bool readOnly) {
62739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    const SkImageInfo& info = bitmap->info();
628f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    if (info.colorType() == kUnknown_SkColorType) {
62939d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        doThrowIAE(env, "unknown bitmap configuration");
63039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews        return nullptr;
63139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    }
63239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
633a316c5dfbc6355f536d765959cacb06bbfed76adJeff Brown    if (!addr) {
634a316c5dfbc6355f536d765959cacb06bbfed76adJeff Brown        // Map existing ashmem region if not already mapped.
635a316c5dfbc6355f536d765959cacb06bbfed76adJeff Brown        int flags = readOnly ? (PROT_READ) : (PROT_READ | PROT_WRITE);
636a316c5dfbc6355f536d765959cacb06bbfed76adJeff Brown        addr = mmap(NULL, ashmem_get_size_region(fd), flags, MAP_SHARED, fd, 0);
637a316c5dfbc6355f536d765959cacb06bbfed76adJeff Brown        if (addr == MAP_FAILED) {
638a316c5dfbc6355f536d765959cacb06bbfed76adJeff Brown            return nullptr;
639a316c5dfbc6355f536d765959cacb06bbfed76adJeff Brown        }
64039d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    }
64139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
64239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    // we must respect the rowBytes value already set on the bitmap instead of
64339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    // attempting to compute our own.
64439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    const size_t rowBytes = bitmap->rowBytes();
64539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
64639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    android::Bitmap* wrapper = new android::Bitmap(addr, fd, info, rowBytes, ctable);
64739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    wrapper->getSkBitmap(bitmap);
648a316c5dfbc6355f536d765959cacb06bbfed76adJeff Brown    if (readOnly) {
649a316c5dfbc6355f536d765959cacb06bbfed76adJeff Brown        bitmap->pixelRef()->setImmutable();
650a316c5dfbc6355f536d765959cacb06bbfed76adJeff Brown    }
65139d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    // since we're already allocated, we lockPixels right away
65239d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    // HeapAllocator behaves this way too
65339d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    bitmap->lockPixels();
65439d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
65539d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews    return wrapper;
65639d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews}
65739d7f30ebe490c1d6aee76b0b61e3e67dec13e34Riley Andrews
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
660f29ed28c7b878ef28058bc730715d0d32445bc57John ReckJavaPixelAllocator::JavaPixelAllocator(JNIEnv* env) {
661f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&mJavaVM) != JNI_OK,
662f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            "env->GetJavaVM failed");
663f29ed28c7b878ef28058bc730715d0d32445bc57John Reck}
664f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
665f29ed28c7b878ef28058bc730715d0d32445bc57John ReckJavaPixelAllocator::~JavaPixelAllocator() {
666f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    if (mStorage) {
667f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        mStorage->detachFromJava();
668291303ba3dbb3a0173bcc82ded595ca75df7b50eWei-Ta Chen    }
669291303ba3dbb3a0173bcc82ded595ca75df7b50eWei-Ta Chen}
6708451b25a4422656bbd6657a5855e69c0f4d53c74Elliott Hughes
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectbool JavaPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) {
672f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    JNIEnv* env = vm2env(mJavaVM);
673e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
674f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    mStorage = GraphicsJNI::allocateJavaPixelRef(env, bitmap, ctable);
675f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    return mStorage != nullptr;
676f1f48bc7f200f54c76b22d845d8ba8419879b375Joseph Wen}
677f1f48bc7f200f54c76b22d845d8ba8419879b375Joseph Wen
678f1f48bc7f200f54c76b22d845d8ba8419879b375Joseph Wen////////////////////////////////////////////////////////////////////////////////
679f1f48bc7f200f54c76b22d845d8ba8419879b375Joseph Wen
6801f979639c168ebdf77ad8d7771786fc321ce8234Matt SarettRecyclingClippingPixelAllocator::RecyclingClippingPixelAllocator(
6811f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett        android::Bitmap* recycledBitmap, size_t recycledBytes)
6821f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    : mRecycledBitmap(recycledBitmap)
6831f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    , mRecycledBytes(recycledBytes)
6841f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    , mSkiaBitmap(nullptr)
6851f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    , mNeedsCopy(false)
6861f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett{}
6871f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett
6881f979639c168ebdf77ad8d7771786fc321ce8234Matt SarettRecyclingClippingPixelAllocator::~RecyclingClippingPixelAllocator() {}
6891f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett
6901f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarettbool RecyclingClippingPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) {
6911f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    // Ensure that the caller did not pass in a NULL bitmap to the constructor or this
6921f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    // function.
6931f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    LOG_ALWAYS_FATAL_IF(!mRecycledBitmap);
6941f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    LOG_ALWAYS_FATAL_IF(!bitmap);
6951f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    mSkiaBitmap = bitmap;
6961f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett
6971f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    // This behaves differently than the RecyclingPixelAllocator.  For backwards
6981f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    // compatibility, the original color type of the recycled bitmap must be maintained.
6991f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    if (mRecycledBitmap->info().colorType() != bitmap->colorType()) {
7001f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett        return false;
7011f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    }
7021f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett
7031f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    // The Skia bitmap specifies the width and height needed by the decoder.
7041f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    // mRecycledBitmap specifies the width and height of the bitmap that we
7051f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    // want to reuse.  Neither can be changed.  We will try to find a way
7061f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    // to reuse the memory.
7071f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    const int maxWidth = SkTMax(bitmap->width(), mRecycledBitmap->info().width());
7081f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    const int maxHeight = SkTMax(bitmap->height(), mRecycledBitmap->info().height());
7091f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    const SkImageInfo maxInfo = bitmap->info().makeWH(maxWidth, maxHeight);
7101f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    const size_t rowBytes = maxInfo.minRowBytes();
7111f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    const size_t bytesNeeded = maxInfo.getSafeSize(rowBytes);
7121f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett    if (bytesNeeded <= mRecycledBytes) {
7131f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett        // Here we take advantage of reconfigure() to reset the rowBytes and ctable
7141f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett        // of mRecycledBitmap.  It is very important that we pass in
7151f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett        // mRecycledBitmap->info() for the SkImageInfo.  According to the
7161f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett        // specification for BitmapRegionDecoder, we are not allowed to change
7171f979639c168ebdf77ad8d7771786fc321ce8234Matt Sarett        // the SkImageInfo.
718