Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 721ae5fe authored by Riley Andrews's avatar Riley Andrews
Browse files

Add internal bitmap api for creating immutable ashmem backed bitmaps.

Bug 21037890
Change-Id: I827e83dd75e301e7d93ead5efdd744f0d8435ae5
parent 39d7f30e
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -719,6 +719,21 @@ static jobject Bitmap_copy(JNIEnv* env, jobject, jlong srcHandle,
            getPremulBitmapCreateFlags(isMutable));
}

static jobject Bitmap_copyAshmem(JNIEnv* env, jobject, jlong srcHandle) {
    SkBitmap src;
    reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
    SkBitmap result;

    AshmemPixelAllocator allocator(env);
    if (!src.copyTo(&result, &allocator)) {
        return NULL;
    }
    Bitmap* bitmap = allocator.getStorageObjAndReset();
    bitmap->peekAtPixelRef()->setImmutable();
    jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false));
    return ret;
}

static void Bitmap_destructor(JNIEnv* env, jobject, jlong bitmapHandle) {
    LocalScopedBitmap bitmap(bitmapHandle);
    bitmap->detachFromJava();
@@ -1267,6 +1282,8 @@ static JNINativeMethod gBitmapMethods[] = {
        (void*)Bitmap_creator },
    {   "nativeCopy",               "(JIZ)Landroid/graphics/Bitmap;",
        (void*)Bitmap_copy },
    {   "nativeCopyAshmem",         "(J)Landroid/graphics/Bitmap;",
        (void*)Bitmap_copyAshmem },
    {   "nativeDestructor",         "(J)V", (void*)Bitmap_destructor },
    {   "nativeRecycle",            "(J)Z", (void*)Bitmap_recycle },
    {   "nativeReconfigure",        "(JIIIIZ)V", (void*)Bitmap_reconfigure },
+19 −0
Original line number Diff line number Diff line
@@ -674,6 +674,25 @@ bool JavaPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) {

////////////////////////////////////////////////////////////////////////////////

AshmemPixelAllocator::AshmemPixelAllocator(JNIEnv *env) {
    LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&mJavaVM) != JNI_OK,
            "env->GetJavaVM failed");
}

AshmemPixelAllocator::~AshmemPixelAllocator() {
    if (mStorage) {
        mStorage->detachFromJava();
    }
}

bool AshmemPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) {
    JNIEnv* env = vm2env(mJavaVM);
    mStorage = GraphicsJNI::allocateAshmemPixelRef(env, bitmap, ctable);
    return mStorage != nullptr;
}

////////////////////////////////////////////////////////////////////////////////

static jclass make_globalref(JNIEnv* env, const char classname[])
{
    jclass c = env->FindClass(classname);
+17 −0
Original line number Diff line number Diff line
@@ -144,6 +144,23 @@ private:
    android::Bitmap* mStorage = nullptr;
};

class AshmemPixelAllocator : public SkBitmap::Allocator {
public:
    AshmemPixelAllocator(JNIEnv* env);
    ~AshmemPixelAllocator();
    virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable);
    android::Bitmap* getStorageObjAndReset() {
        android::Bitmap* result = mStorage;
        mStorage = NULL;
        return result;
    };

private:
    JavaVM* mJavaVM;
    android::Bitmap* mStorage = nullptr;
};


enum JNIAccess {
    kRO_JNIAccess,
    kRW_JNIAccess
+17 −0
Original line number Diff line number Diff line
@@ -557,6 +557,22 @@ public final class Bitmap implements Parcelable {
        return b;
    }

    /**
     * Creates a new immutable bitmap backed by ashmem which can efficiently
     * be passed between processes.
     *
     * @hide
     */
    public Bitmap createAshmemBitmap() {
        checkRecycled("Can't copy a recycled bitmap");
        Bitmap b = nativeCopyAshmem(mFinalizer.mNativeBitmap);
        if (b != null) {
            b.setPremultiplied(mRequestPremultiplied);
            b.mDensity = mDensity;
        }
        return b;
    }

    /**
     * Creates a new bitmap, scaled from an existing bitmap, when possible. If the
     * specified width and height are the same as the current width and height of
@@ -1636,6 +1652,7 @@ public final class Bitmap implements Parcelable {
                                              int nativeConfig, boolean mutable);
    private static native Bitmap nativeCopy(long nativeSrcBitmap, int nativeConfig,
                                            boolean isMutable);
    private static native Bitmap nativeCopyAshmem(long nativeSrcBitmap);
    private static native void nativeDestructor(long nativeBitmap);
    private static native boolean nativeRecycle(long nativeBitmap);
    private static native void nativeReconfigure(long nativeBitmap, int width, int height,