Bug 763166 - Add a AndroidBridge::GetJNIForCompositorThread function to avoid using GetJNIForThread. r=blassey

This commit is contained in:
Kartikaya Gupta 2012-07-13 09:36:43 -04:00
parent 7ad49e0b59
commit f4481f1883
4 changed files with 69 additions and 18 deletions

View File

@ -47,6 +47,21 @@ NS_IMPL_THREADSAFE_ISUPPORTS0(nsFilePickerCallback)
AndroidBridge *AndroidBridge::sBridge = 0;
AndroidBridge::AndroidBridge()
: mLayerClient(NULL)
, mJavaVM(NULL)
, mJNIEnv(NULL)
, mThread(NULL)
, mJNIForCompositorThread(NULL)
, mCompositorThread(NULL)
, mCompositorJNICreationMutex("AndroidBridge.CompositorJNICreation")
{
}
AndroidBridge::~AndroidBridge()
{
}
AndroidBridge *
AndroidBridge::ConstructBridge(JNIEnv *jEnv,
jclass jGeckoAppShellClass)
@ -76,8 +91,6 @@ AndroidBridge::Init(JNIEnv *jEnv,
AutoLocalJNIFrame jniFrame(jEnv);
mJNIEnv = nsnull;
mThread = nsnull;
mOpenedGraphicsLibraries = false;
mHasNativeBitmapAccess = false;
mHasNativeWindowAccess = false;
@ -1120,7 +1133,7 @@ AndroidBridge::CallEglCreateWindowSurface(void *dpy, void *config, AndroidGeckoS
{
ALOG_BRIDGE("AndroidBridge::CallEglCreateWindowSurface");
JNIEnv *env = GetJNIForThread(); // called on the compositor thread
JNIEnv *env = GetJNIForCompositorThread();
if (!env)
return NULL;
@ -1173,7 +1186,7 @@ void
AndroidBridge::RegisterCompositor()
{
ALOG_BRIDGE("AndroidBridge::RegisterCompositor");
JNIEnv *env = GetJNIForThread(); // called on the compositor thread
JNIEnv *env = GetJNIForCompositorThread();
if (!env)
return;
@ -2079,15 +2092,6 @@ AndroidBridge::SyncViewportInfo(const nsIntRect& aDisplayPort, float aDisplayRes
client->SyncViewportInfo(aDisplayPort, aDisplayResolution, aLayersUpdated, aScrollOffset, aScaleX, aScaleY);
}
AndroidBridge::AndroidBridge()
: mLayerClient(NULL)
{
}
AndroidBridge::~AndroidBridge()
{
}
/* Implementation file */
NS_IMPL_ISUPPORTS1(nsAndroidBridge, nsIAndroidBridge)

View File

@ -128,6 +128,48 @@ public:
}
return nsnull;
}
static JNIEnv* GetJNIForCompositorThread() {
if (NS_LIKELY(sBridge)) {
if (sBridge->mCompositorThread) {
if ((void*)pthread_self() != sBridge->mCompositorThread) {
__android_log_print(ANDROID_LOG_ERROR, "AndroidBridge", "Non-compositor thread calling GetJNIForCompositorThread!");
NS_ABORT();
return NULL;
}
return sBridge->mJNIForCompositorThread;
}
// first time this is being called, so create the JNI object for the compositor thread.
// make sure to do it in a thread-safe manner in case two different threads call this function
// at the same time during startup.
MutexAutoLock lock(sBridge->mCompositorJNICreationMutex);
if (sBridge->mCompositorThread) {
// this means that another thread executed this function between the time we started executing
// it and the time we acquired the mutex. fail.
__android_log_print(ANDROID_LOG_ERROR, "AndroidBridge", "Two threads called GetJNIForCompositorThread on startup!");
NS_ABORT();
return NULL;
}
JavaVM *jVm = mozilla::AndroidBridge::GetVM();
if (!jVm) {
__android_log_print(ANDROID_LOG_ERROR, "AndroidBridge", "Null VM in GetJNIForCompositorThread");
return NULL;
}
JNIEnv* env;
if (jVm->AttachCurrentThread(&env, NULL)) {
__android_log_print(ANDROID_LOG_ERROR, "AndroidBridge", "Unable to attach to VM in GetJNIForCompositorThread");
return NULL;
}
sBridge->mCompositorThread = (void*)pthread_self();
sBridge->mJNIForCompositorThread = env;
return env;
}
return NULL;
}
static jclass GetGeckoAppShellClass() {
return sBridge->mGeckoAppShellClass;
@ -369,6 +411,11 @@ protected:
JNIEnv *mJNIEnv;
void *mThread;
// the JNIEnv for the compositor thread and the lock used when creating it
JNIEnv *mJNIForCompositorThread;
void* mCompositorThread;
Mutex mCompositorJNICreationMutex;
// the GeckoSurfaceView
AndroidGeckoSurfaceView mSurfaceView;

View File

@ -663,7 +663,7 @@ void
AndroidGeckoLayerClient::SetFirstPaintViewport(const nsIntPoint& aOffset, float aZoom, const nsIntRect& aPageRect, const gfx::Rect& aCssPageRect)
{
NS_ASSERTION(!isNull(), "SetFirstPaintViewport called on null layer client!");
JNIEnv *env = GetJNIForThread(); // this is called on the compositor thread
JNIEnv *env = AndroidBridge::GetJNIForCompositorThread();
if (!env)
return;
@ -677,7 +677,7 @@ void
AndroidGeckoLayerClient::SetPageRect(const gfx::Rect& aCssPageRect)
{
NS_ASSERTION(!isNull(), "SetPageRect called on null layer client!");
JNIEnv *env = GetJNIForThread(); // this is called on the compositor thread
JNIEnv *env = AndroidBridge::GetJNIForCompositorThread();
if (!env)
return;
@ -691,7 +691,7 @@ AndroidGeckoLayerClient::SyncViewportInfo(const nsIntRect& aDisplayPort, float a
nsIntPoint& aScrollOffset, float& aScaleX, float& aScaleY)
{
NS_ASSERTION(!isNull(), "SyncViewportInfo called on null layer client!");
JNIEnv *env = GetJNIForThread(); // this is called on the compositor thread
JNIEnv *env = AndroidBridge::GetJNIForCompositorThread();
if (!env)
return;

View File

@ -2238,7 +2238,7 @@ nsWindow::GetIMEUpdatePreference()
void
nsWindow::DrawWindowUnderlay(LayerManager* aManager, nsIntRect aRect)
{
JNIEnv *env = GetJNIForThread();
JNIEnv *env = AndroidBridge::GetJNIForCompositorThread();
NS_ABORT_IF_FALSE(env, "No JNI environment at DrawWindowUnderlay()!");
if (!env)
return;
@ -2256,7 +2256,7 @@ nsWindow::DrawWindowUnderlay(LayerManager* aManager, nsIntRect aRect)
void
nsWindow::DrawWindowOverlay(LayerManager* aManager, nsIntRect aRect)
{
JNIEnv *env = GetJNIForThread();
JNIEnv *env = AndroidBridge::GetJNIForCompositorThread();
NS_ABORT_IF_FALSE(env, "No JNI environment at DrawWindowOverlay()!");
if (!env)
return;