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

Commit 291303ba authored by Wei-Ta Chen's avatar Wei-Ta Chen
Browse files

Fix a bug, where one thread is using JNIEnv associated with another thread.

We see abort messages like this when using JavaPixelAllocator and JavaMemoryUsageReporter.
W/dalvikvm(  680): JNI WARNING: threadid=2 using env from threadid=10
W/dalvikvm(  680):              in Landroid/graphics/LargeBitmap;.nativeClean (I)V (CallVoidMethodV)

To fix it, we keep JavaVM, rather than JNIEnv, in JavaPixelAllocator and JavaMemoryUsageReporter,
because JavaVM allows us to get the JNIEnv corresponds to the current thread.

Change-Id: Ibd4b394a53dd3fdccc5a442eeb0dedf836479575
parent 6ab94608
Loading
Loading
Loading
Loading
+20 −7
Original line number Original line Diff line number Diff line
@@ -518,35 +518,48 @@ bool GraphicsJNI::setJavaPixelRef(JNIEnv* env, SkBitmap* bitmap,
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////


JavaPixelAllocator::JavaPixelAllocator(JNIEnv* env, bool reportSizeToVM)
JavaPixelAllocator::JavaPixelAllocator(JNIEnv* env, bool reportSizeToVM)
    : fEnv(env), fReportSizeToVM(reportSizeToVM) {}
    : fReportSizeToVM(reportSizeToVM) {
    if (env->GetJavaVM(&fVM) != JNI_OK) {
        SkDebugf("------ [%p] env->GetJavaVM failed\n", env);
        sk_throw();
    }
}
    
    
bool JavaPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) {
bool JavaPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) {
    return GraphicsJNI::setJavaPixelRef(fEnv, bitmap, ctable, fReportSizeToVM);
    JNIEnv* env = vm2env(fVM);
    return GraphicsJNI::setJavaPixelRef(env, bitmap, ctable, fReportSizeToVM);
}
}


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


JavaMemoryUsageReporter::JavaMemoryUsageReporter(JNIEnv* env)
JavaMemoryUsageReporter::JavaMemoryUsageReporter(JNIEnv* env)
    : fEnv(env), fTotalSize(0) {}
    : fTotalSize(0) {
    if (env->GetJavaVM(&fVM) != JNI_OK) {
        SkDebugf("------ [%p] env->GetJavaVM failed\n", env);
        sk_throw();
    }
}


JavaMemoryUsageReporter::~JavaMemoryUsageReporter() {
JavaMemoryUsageReporter::~JavaMemoryUsageReporter() {
    JNIEnv* env = vm2env(fVM);
    jlong jtotalSize = fTotalSize;
    jlong jtotalSize = fTotalSize;
    fEnv->CallVoidMethod(gVMRuntime_singleton,
    env->CallVoidMethod(gVMRuntime_singleton,
            gVMRuntime_trackExternalFreeMethodID,
            gVMRuntime_trackExternalFreeMethodID,
            jtotalSize);
            jtotalSize);
}
}


bool JavaMemoryUsageReporter::reportMemory(size_t memorySize) {
bool JavaMemoryUsageReporter::reportMemory(size_t memorySize) {
    jlong jsize = memorySize;  // the VM wants longs for the size
    jlong jsize = memorySize;  // the VM wants longs for the size
    bool r = fEnv->CallBooleanMethod(gVMRuntime_singleton,
    JNIEnv* env = vm2env(fVM);
    bool r = env->CallBooleanMethod(gVMRuntime_singleton,
            gVMRuntime_trackExternalAllocationMethodID,
            gVMRuntime_trackExternalAllocationMethodID,
            jsize);
            jsize);
    if (GraphicsJNI::hasException(fEnv)) {
    if (GraphicsJNI::hasException(env)) {
        return false;
        return false;
    }
    }
    if (!r) {
    if (!r) {
        LOGE("VM won't let us allocate %zd bytes\n", memorySize);
        LOGE("VM won't let us allocate %zd bytes\n", memorySize);
        doThrowOOME(fEnv, "bitmap size exceeds VM budget");
        doThrowOOME(env, "bitmap size exceeds VM budget");
        return false;
        return false;
    }
    }
    fTotalSize += memorySize;
    fTotalSize += memorySize;
+2 −2
Original line number Original line Diff line number Diff line
@@ -80,7 +80,7 @@ public:
    virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable);
    virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable);
    
    
private:
private:
    JNIEnv* fEnv;
    JavaVM* fVM;
    bool fReportSizeToVM;
    bool fReportSizeToVM;
};
};


@@ -92,7 +92,7 @@ public:
    virtual bool reportMemory(size_t memorySize);
    virtual bool reportMemory(size_t memorySize);


private:
private:
    JNIEnv* fEnv;
    JavaVM* fVM;
    size_t fTotalSize;
    size_t fTotalSize;
};
};