diff options
Diffstat (limited to 'core/jni/android/graphics/Graphics.cpp')
| -rw-r--r-- | core/jni/android/graphics/Graphics.cpp | 120 |
1 files changed, 34 insertions, 86 deletions
diff --git a/core/jni/android/graphics/Graphics.cpp b/core/jni/android/graphics/Graphics.cpp index f793df1..0deb8cc 100644 --- a/core/jni/android/graphics/Graphics.cpp +++ b/core/jni/android/graphics/Graphics.cpp @@ -154,7 +154,7 @@ static jfieldID gPointF_xFieldID; static jfieldID gPointF_yFieldID; static jclass gBitmap_class; -static jfieldID gBitmap_skBitmapPtr; +static jfieldID gBitmap_nativePtr; static jmethodID gBitmap_constructorMethodID; static jmethodID gBitmap_reinitMethodID; static jmethodID gBitmap_getAllocationByteCountMethodID; @@ -338,27 +338,22 @@ SkColorType GraphicsJNI::legacyBitmapConfigToColorType(jint legacyConfig) { return static_cast<SkColorType>(gConfig2ColorType[legacyConfig]); } -SkBitmap* GraphicsJNI::getSkBitmapDeprecated(JNIEnv* env, jobject bitmap) { +android::Bitmap* GraphicsJNI::getBitmap(JNIEnv* env, jobject bitmap) { SkASSERT(env); SkASSERT(bitmap); SkASSERT(env->IsInstanceOf(bitmap, gBitmap_class)); - jlong bitmapHandle = env->GetLongField(bitmap, gBitmap_skBitmapPtr); - SkBitmap* b = reinterpret_cast<SkBitmap*>(bitmapHandle); + jlong bitmapHandle = env->GetLongField(bitmap, gBitmap_nativePtr); + android::Bitmap* b = reinterpret_cast<android::Bitmap*>(bitmapHandle); SkASSERT(b); return b; } void GraphicsJNI::getSkBitmap(JNIEnv* env, jobject bitmap, SkBitmap* outBitmap) { - // TODO: We have to copy from the existing bitmap due to rowBytes not - // being updated on the SkPixelRef at reconfigure time. This is a short term - // problem that will be fixed with the specialized wrapper - *outBitmap = *getSkBitmapDeprecated(env, bitmap); + getBitmap(env, bitmap)->getSkBitmap(outBitmap); } SkPixelRef* GraphicsJNI::getSkPixelRef(JNIEnv* env, jobject bitmap) { - jlong bitmapHandle = env->GetLongField(bitmap, gBitmap_skBitmapPtr); - SkBitmap* b = reinterpret_cast<SkBitmap*>(bitmapHandle); - return b->pixelRef(); + return getBitmap(env, bitmap)->pixelRef(); } SkColorType GraphicsJNI::getNativeBitmapColorType(JNIEnv* env, jobject jconfig) { @@ -396,47 +391,43 @@ SkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region) /////////////////////////////////////////////////////////////////////////////////////////// // Assert that bitmap's SkAlphaType is consistent with isPremultiplied. -static void assert_premultiplied(const SkBitmap& bitmap, bool isPremultiplied) { +static void assert_premultiplied(const SkImageInfo& info, bool isPremultiplied) { // kOpaque_SkAlphaType and kIgnore_SkAlphaType mean that isPremultiplied is // irrelevant. This just tests to ensure that the SkAlphaType is not // opposite of isPremultiplied. if (isPremultiplied) { - SkASSERT(bitmap.alphaType() != kUnpremul_SkAlphaType); + SkASSERT(info.alphaType() != kUnpremul_SkAlphaType); } else { - SkASSERT(bitmap.alphaType() != kPremul_SkAlphaType); + SkASSERT(info.alphaType() != kPremul_SkAlphaType); } } -jobject GraphicsJNI::createBitmap(JNIEnv* env, SkBitmap* bitmap, jbyteArray buffer, - int bitmapCreateFlags, jbyteArray ninePatchChunk, jobject ninePatchInsets, int density) -{ - SkASSERT(bitmap); - SkASSERT(bitmap->pixelRef()); - SkASSERT(!env->ExceptionCheck()); +jobject GraphicsJNI::createBitmap(JNIEnv* env, android::Bitmap* bitmap, + int bitmapCreateFlags, jbyteArray ninePatchChunk, jobject ninePatchInsets, + int density) { bool isMutable = bitmapCreateFlags & kBitmapCreateFlag_Mutable; bool isPremultiplied = bitmapCreateFlags & kBitmapCreateFlag_Premultiplied; - // The caller needs to have already set the alpha type properly, so the // native SkBitmap stays in sync with the Java Bitmap. - assert_premultiplied(*bitmap, isPremultiplied); + assert_premultiplied(bitmap->info(), isPremultiplied); jobject obj = env->NewObject(gBitmap_class, gBitmap_constructorMethodID, - reinterpret_cast<jlong>(bitmap), buffer, + reinterpret_cast<jlong>(bitmap), bitmap->javaByteArray(), bitmap->width(), bitmap->height(), density, isMutable, isPremultiplied, ninePatchChunk, ninePatchInsets); hasException(env); // For the side effect of logging. return obj; } -void GraphicsJNI::reinitBitmap(JNIEnv* env, jobject javaBitmap, SkBitmap* bitmap, +void GraphicsJNI::reinitBitmap(JNIEnv* env, jobject javaBitmap, const SkImageInfo& info, bool isPremultiplied) { // The caller needs to have already set the alpha type properly, so the // native SkBitmap stays in sync with the Java Bitmap. - assert_premultiplied(*bitmap, isPremultiplied); + assert_premultiplied(info, isPremultiplied); env->CallVoidMethod(javaBitmap, gBitmap_reinitMethodID, - bitmap->width(), bitmap->height(), isPremultiplied); + info.width(), info.height(), isPremultiplied); } int GraphicsJNI::getBitmapAllocationByteCount(JNIEnv* env, jobject javaBitmap) @@ -477,51 +468,6 @@ static JNIEnv* vm2env(JavaVM* vm) /////////////////////////////////////////////////////////////////////////////// -AndroidPixelRef::AndroidPixelRef(JNIEnv* env, const SkImageInfo& info, void* storage, - size_t rowBytes, jbyteArray storageObj, SkColorTable* ctable) : - SkMallocPixelRef(info, storage, rowBytes, ctable, (storageObj == NULL)), - fWrappedPixelRef(NULL) { - SkASSERT(storage); - SkASSERT(storageObj); - SkASSERT(env); - - if (env->GetJavaVM(&fVM) != JNI_OK) { - SkDebugf("------ [%p] env->GetJavaVM failed\n", env); - sk_throw(); - } - - fStorageObj = (jbyteArray) env->NewGlobalRef(storageObj); -} - -AndroidPixelRef::AndroidPixelRef(AndroidPixelRef& wrappedPixelRef, const SkImageInfo& info, - size_t rowBytes, SkColorTable* ctable) : - SkMallocPixelRef(info, wrappedPixelRef.getAddr(), rowBytes, ctable, false), - fWrappedPixelRef(wrappedPixelRef.fWrappedPixelRef ? - wrappedPixelRef.fWrappedPixelRef : &wrappedPixelRef) -{ - SkASSERT(fWrappedPixelRef); - SkSafeRef(fWrappedPixelRef); - - // don't need to initialize this, as all the relevant logic delegates to the wrapped ref - fStorageObj = NULL; -} - -AndroidPixelRef::~AndroidPixelRef() { - if (fWrappedPixelRef) { - SkSafeUnref(fWrappedPixelRef); - } else { - SkASSERT(fStorageObj); - JNIEnv* env = vm2env(fVM); - env->DeleteGlobalRef(fStorageObj); - } - - if (android::uirenderer::Caches::hasInstance()) { - android::uirenderer::Caches::getInstance().textureCache.releaseTexture(getStableID()); - } -} - -/////////////////////////////////////////////////////////////////////////////// - static bool computeAllocationSize(const SkBitmap& bitmap, size_t* size) { int32_t rowBytes32 = SkToS32(bitmap.rowBytes()); int64_t bigSize = (int64_t)bitmap.height() * rowBytes32; @@ -533,7 +479,7 @@ static bool computeAllocationSize(const SkBitmap& bitmap, size_t* size) { return true; } -jbyteArray GraphicsJNI::allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap, +android::Bitmap* GraphicsJNI::allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap, SkColorTable* ctable) { const SkImageInfo& info = bitmap->info(); if (info.fColorType == kUnknown_SkColorType) { @@ -562,13 +508,14 @@ jbyteArray GraphicsJNI::allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap, return NULL; } SkASSERT(addr); - SkPixelRef* pr = new AndroidPixelRef(env, info, (void*) addr, rowBytes, arrayObj, ctable); - bitmap->setPixelRef(pr)->unref(); + android::Bitmap* wrapper = new android::Bitmap(env, arrayObj, (void*) addr, + info, rowBytes, ctable); + wrapper->getSkBitmap(bitmap); // since we're already allocated, we lockPixels right away // HeapAllocator behaves this way too bitmap->lockPixels(); - return arrayObj; + return wrapper; } struct AndroidPixelRefContext { @@ -627,21 +574,22 @@ bool GraphicsJNI::allocatePixels(JNIEnv* env, SkBitmap* bitmap, SkColorTable* ct /////////////////////////////////////////////////////////////////////////////// -JavaPixelAllocator::JavaPixelAllocator(JNIEnv* env) - : fStorageObj(NULL), - fAllocCount(0) { - if (env->GetJavaVM(&fVM) != JNI_OK) { - SkDebugf("------ [%p] env->GetJavaVM failed\n", env); - sk_throw(); +JavaPixelAllocator::JavaPixelAllocator(JNIEnv* env) { + LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&mJavaVM) != JNI_OK, + "env->GetJavaVM failed"); +} + +JavaPixelAllocator::~JavaPixelAllocator() { + if (mStorage) { + mStorage->detachFromJava(); } } bool JavaPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { - JNIEnv* env = vm2env(fVM); + JNIEnv* env = vm2env(mJavaVM); - fStorageObj = GraphicsJNI::allocateJavaPixelRef(env, bitmap, ctable); - fAllocCount += 1; - return fStorageObj != NULL; + mStorage = GraphicsJNI::allocateJavaPixelRef(env, bitmap, ctable); + return mStorage != nullptr; } //////////////////////////////////////////////////////////////////////////////// @@ -687,7 +635,7 @@ int register_android_graphics_Graphics(JNIEnv* env) gPointF_yFieldID = getFieldIDCheck(env, gPointF_class, "y", "F"); gBitmap_class = make_globalref(env, "android/graphics/Bitmap"); - gBitmap_skBitmapPtr = getFieldIDCheck(env, gBitmap_class, "mSkBitmapPtr", "J"); + gBitmap_nativePtr = getFieldIDCheck(env, gBitmap_class, "mNativePtr", "J"); gBitmap_constructorMethodID = env->GetMethodID(gBitmap_class, "<init>", "(J[BIIIZZ[BLandroid/graphics/NinePatch$InsetStruct;)V"); gBitmap_reinitMethodID = env->GetMethodID(gBitmap_class, "reinit", "(IIZ)V"); gBitmap_getAllocationByteCountMethodID = env->GetMethodID(gBitmap_class, "getAllocationByteCount", "()I"); |
