Bug 713803 - Rationalize JNI access accross android widget. r=blassey

This commit is contained in:
Doug Turner 2012-01-28 22:38:02 -08:00
Родитель 3928acc847
Коммит dc1b4c0544
12 изменённых файлов: 726 добавлений и 395 удалений

Просмотреть файл

@ -64,17 +64,20 @@ nsresult
PluginPRLibrary::NP_Initialize(NPNetscapeFuncs* bFuncs,
NPPluginFuncs* pFuncs, NPError* error)
{
JNIEnv* env = GetJNIForThread();
if (!env)
return NS_ERROR_FAILURE;
if (mNP_Initialize) {
*error = mNP_Initialize(bFuncs, pFuncs, GetJNIForThread());
*error = mNP_Initialize(bFuncs, pFuncs, env);
} else {
NP_InitializeFunc pfNP_Initialize = (NP_InitializeFunc)
PR_FindFunctionSymbol(mLibrary, "NP_Initialize");
if (!pfNP_Initialize)
return NS_ERROR_FAILURE;
*error = pfNP_Initialize(bFuncs, pFuncs, GetJNIForThread());
*error = pfNP_Initialize(bFuncs, pFuncs, env);
}
// Save pointers to functions that get called through PluginLibrary itself.
mNPP_New = pFuncs->newp;
mNPP_GetValue = pFuncs->getvalue;

Просмотреть файл

@ -140,6 +140,8 @@ NS_IMETHODIMP
AudioRunnable::Run()
{
JNIEnv* jenv = GetJNIForThread();
if (!jenv)
return NS_ERROR_FAILURE;
if (jenv->PushLocalFrame(128)) {
return NS_ERROR_FAILURE;
@ -309,6 +311,9 @@ anp_audio_start(ANPAudioTrack* s)
}
JNIEnv *jenv = GetJNIForThread();
if (!jenv)
return;
jenv->CallVoidMethod(s->output_unit, at.play);
s->isStopped = false;
@ -329,6 +334,8 @@ anp_audio_pause(ANPAudioTrack* s)
}
JNIEnv *jenv = GetJNIForThread();
if (!jenv)
return;
jenv->CallVoidMethod(s->output_unit, at.pause);
}
@ -341,6 +348,8 @@ anp_audio_stop(ANPAudioTrack* s)
s->isStopped = true;
JNIEnv *jenv = GetJNIForThread();
if (!jenv)
return;
jenv->CallVoidMethod(s->output_unit, at.stop);
}

Просмотреть файл

@ -66,14 +66,21 @@ void
anp_event_postEvent(NPP inst, const ANPEvent* event)
{
LOG("%s", __PRETTY_FUNCTION__);
JNIEnv* env = GetJNIForThread();
if (!env)
return;
if (!mozilla::AndroidBridge::Bridge()) {
LOG("no bridge in %s!!!!", __PRETTY_FUNCTION__);
return;
}
nsNPAPIPluginInstance* pinst = static_cast<nsNPAPIPluginInstance*>(inst->ndata);
NPPluginFuncs* pluginFunctions = pinst->GetPlugin()->PluginFuncs();
mozilla::AndroidBridge::Bridge()->PostToJavaThread(
new PluginEventRunnable(inst, const_cast<ANPEvent*>(event), pluginFunctions), true);
mozilla::AndroidBridge::Bridge()->PostToJavaThread(env,
new PluginEventRunnable(inst, const_cast<ANPEvent*>(event), pluginFunctions),
true);
LOG("returning from %s", __PRETTY_FUNCTION__);
}

Просмотреть файл

@ -67,6 +67,9 @@ jclass anp_system_loadJavaClass(NPP instance, const char* className)
LOG("%s", __PRETTY_FUNCTION__);
JNIEnv* env = GetJNIForThread();
if (!env)
return nsnull;
jclass cls = env->FindClass("org/mozilla/gecko/GeckoAppShell");
jmethodID method = env->GetStaticMethodID(cls,
"loadPluginClass",

Просмотреть файл

@ -2383,7 +2383,10 @@ _getvalue(NPP npp, NPNVariable variable, void *result)
case kJavaContext_ANPGetValue: {
LOG("get context");
JNIEnv* env = GetJNIForThread();
JNIEnv* env = GetJNIForThread();
if (!env)
return NPERR_GENERIC_ERROR;
jclass cls = env->FindClass("org/mozilla/gecko/GeckoApp");
jfieldID field = env->GetStaticFieldID(cls, "mAppContext",
"Lorg/mozilla/gecko/GeckoApp;");

Просмотреть файл

@ -750,7 +750,15 @@ public:
return NS_OK;
}
void RequestSurface() {
mozilla::AndroidBridge::Bridge()->PostToJavaThread(this);
JNIEnv* env = GetJNIForThread();
if (!env)
return;
if (!mozilla::AndroidBridge::Bridge()) {
PLUGIN_LOG(PLUGIN_LOG_BASIC, ("nsNPAPIPluginInstance null AndroidBridge"));
return;
}
mozilla::AndroidBridge::Bridge()->PostToJavaThread(env, this);
}
private:
nsNPAPIPluginInstance* mInstance;

Просмотреть файл

@ -1736,33 +1736,32 @@ bool nsPluginInstanceOwner::AddPluginView(const gfxRect& aRect)
void nsPluginInstanceOwner::RemovePluginView()
{
if (mInstance && mObjectFrame && mPluginViewAdded) {
mPluginViewAdded = false;
if (!mInstance || !mObjectFrame | !mPluginViewAdded)
return;
void* surface = mInstance->GetJavaSurface();
if (!surface)
return;
mPluginViewAdded = false;
JNIEnv* env = GetJNIForThread();
if (!env)
return;
void* surface = mInstance->GetJavaSurface();
if (!surface)
return;
AndroidBridge::AutoLocalJNIFrame frame(env, 1);
JNIEnv* env = GetJNIForThread();
if (!env)
return;
jclass cls = env->FindClass("org/mozilla/gecko/GeckoAppShell");
jmethodID method = env->GetStaticMethodID(cls,
"removePluginView",
"(Landroid/view/View;)V");
env->CallStaticVoidMethod(cls, method, surface);
AndroidBridge::AutoLocalJNIFrame frame(env, 1);
{
ANPEvent event;
event.inSize = sizeof(ANPEvent);
event.eventType = kLifecycle_ANPEventType;
event.data.lifecycle.action = kOffScreen_ANPLifecycleAction;
mInstance->HandleEvent(&event, nsnull);
}
}
jclass cls = env->FindClass("org/mozilla/gecko/GeckoAppShell");
jmethodID method = env->GetStaticMethodID(cls,
"removePluginView",
"(Landroid/view/View;)V");
env->CallStaticVoidMethod(cls, method, surface);
ANPEvent event;
event.inSize = sizeof(ANPEvent);
event.eventType = kLifecycle_ANPEventType;
event.data.lifecycle.action = kOffScreen_ANPLifecycleAction;
mInstance->HandleEvent(&event, nsnull);
}
#endif

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -41,11 +41,13 @@
#include <jni.h>
#include <android/log.h>
#include <cstdlib>
#include <pthread.h>
#include "nsCOMPtr.h"
#include "nsCOMArray.h"
#include "nsIRunnable.h"
#include "nsIObserver.h"
#include "nsThreadUtils.h"
#include "AndroidJavaWrappers.h"
@ -62,6 +64,12 @@
class nsWindow;
class nsIDOMMozSmsMessage;
/* See the comment in AndroidBridge about this function before using it */
extern "C" JNIEnv * GetJNIForThread();
extern bool mozilla_AndroidBridge_SetMainThread(void *);
extern jclass GetGeckoAppShellClass();
namespace mozilla {
namespace hal {
@ -108,18 +116,23 @@ public:
return sBridge;
}
static JavaVM *VM() {
return sBridge->mJavaVM;
}
static JNIEnv *JNI() {
sBridge->EnsureJNIThread();
return sBridge->mJNIEnv;
}
static JNIEnv *JNIForThread() {
static JavaVM *GetVM() {
if (NS_LIKELY(sBridge))
return sBridge->AttachThread();
return sBridge->mJavaVM;
return nsnull;
}
static JNIEnv *GetJNIEnv() {
if (NS_LIKELY(sBridge)) {
if ((void*)pthread_self() != sBridge->mThread) {
__android_log_print(ANDROID_LOG_INFO, "AndroidBridge",
"###!!!!!!! Something's grabbing the JNIEnv from the wrong thread! (thr %p should be %p)",
(void*)pthread_self(), (void*)sBridge->mThread);
return nsnull;
}
return sBridge->mJNIEnv;
}
return nsnull;
}
@ -134,8 +147,6 @@ public:
// SetMainThread.
bool SetMainThread(void *thr);
JNIEnv* AttachThread(bool asDaemon = true);
/* These are all implemented in Java */
static void NotifyIME(int aType, int aState);
@ -242,15 +253,16 @@ public:
public:
AutoLocalJNIFrame(int nEntries = 128)
: mEntries(nEntries)
, mJNIEnv(JNI())
{
mJNIEnv = AndroidBridge::GetJNIEnv();
Push();
}
AutoLocalJNIFrame(JNIEnv* aJNIEnv, int nEntries = 128)
: mEntries(nEntries)
, mJNIEnv(aJNIEnv ? aJNIEnv : JNI())
{
mJNIEnv = aJNIEnv ? aJNIEnv : AndroidBridge::GetJNIEnv();
Push();
}
@ -258,11 +270,16 @@ public:
// the AutoLocalJNIFrame's scope INVALID; be sure that you locked down
// any local refs that you need to keep around in global refs!
void Purge() {
mJNIEnv->PopLocalFrame(NULL);
Push();
if (mJNIEnv) {
mJNIEnv->PopLocalFrame(NULL);
Push();
}
}
~AutoLocalJNIFrame() {
if (!mJNIEnv)
return;
jthrowable exception = mJNIEnv->ExceptionOccurred();
if (exception) {
mJNIEnv->ExceptionDescribe();
@ -274,6 +291,9 @@ public:
private:
void Push() {
if (!mJNIEnv)
return;
// Make sure there is enough space to store a local ref to the
// exception. I am not completely sure this is needed, but does
// not hurt.
@ -306,9 +326,9 @@ public:
void UnlockBitmap(jobject bitmap);
void PostToJavaThread(nsIRunnable* aRunnable, bool aMainThread = false);
void PostToJavaThread(JNIEnv *aEnv, nsIRunnable* aRunnable, bool aMainThread = false);
void ExecuteNextRunnable();
void ExecuteNextRunnable(JNIEnv *aEnv);
/* Copied from Android's native_window.h in newer (platform 9) NDK */
enum {
@ -378,8 +398,6 @@ protected:
AndroidBridge() { }
bool Init(JNIEnv *jEnv, jclass jGeckoApp);
void EnsureJNIThread();
bool mOpenedGraphicsLibraries;
void OpenGraphicsLibraries();
@ -498,8 +516,5 @@ private:
protected:
};
extern "C" JNIEnv * GetJNIForThread();
extern bool mozilla_AndroidBridge_SetMainThread(void *);
extern jclass GetGeckoAppShellClass();
#endif /* AndroidBridge_h__ */

Просмотреть файл

@ -218,14 +218,14 @@ Java_org_mozilla_gecko_GeckoAppShell_reportJavaCrash(JNIEnv *jenv, jclass, jstri
}
NS_EXPORT void JNICALL
Java_org_mozilla_gecko_GeckoAppShell_executeNextRunnable(JNIEnv *, jclass)
Java_org_mozilla_gecko_GeckoAppShell_executeNextRunnable(JNIEnv *jenv, jclass)
{
__android_log_print(ANDROID_LOG_INFO, "GeckoJNI", "%s", __PRETTY_FUNCTION__);
if (!AndroidBridge::Bridge()) {
__android_log_print(ANDROID_LOG_INFO, "GeckoJNI", "no bridge in %s!!!!", __PRETTY_FUNCTION__);
return;
}
AndroidBridge::Bridge()->ExecuteNextRunnable();
AndroidBridge::Bridge()->ExecuteNextRunnable(jenv);
__android_log_print(ANDROID_LOG_INFO, "GeckoJNI", "leaving %s", __PRETTY_FUNCTION__);
}

Просмотреть файл

@ -124,8 +124,6 @@ jmethodID AndroidGeckoSurfaceView::jGetSoftwareDrawBufferMethod = 0;
jmethodID AndroidGeckoSurfaceView::jGetSurfaceMethod = 0;
jmethodID AndroidGeckoSurfaceView::jGetHolderMethod = 0;
#define JNI() (AndroidBridge::JNI())
#define initInit() jclass jClass
// note that this also sets jClass
@ -608,43 +606,68 @@ AndroidGeckoSurfaceView::BeginDrawing()
{
NS_ASSERTION(!isNull(), "BeginDrawing called on null surfaceview!");
return JNI()->CallIntMethod(wrapped_obj, jBeginDrawingMethod);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return 0;
return env->CallIntMethod(wrapped_obj, jBeginDrawingMethod);
}
void
AndroidGeckoSurfaceView::EndDrawing()
{
JNI()->CallVoidMethod(wrapped_obj, jEndDrawingMethod);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return;
env->CallVoidMethod(wrapped_obj, jEndDrawingMethod);
}
void
AndroidGeckoSurfaceView::Draw2D(jobject bitmap, int width, int height)
{
JNI()->CallVoidMethod(wrapped_obj, jDraw2DBitmapMethod, bitmap, width, height);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return;
env->CallVoidMethod(wrapped_obj, jDraw2DBitmapMethod, bitmap, width, height);
}
void
AndroidGeckoSurfaceView::Draw2D(jobject buffer, int stride)
{
JNI()->CallVoidMethod(wrapped_obj, jDraw2DBufferMethod, buffer, stride);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return;
env->CallVoidMethod(wrapped_obj, jDraw2DBufferMethod, buffer, stride);
}
jobject
AndroidGeckoSoftwareLayerClient::LockBuffer()
{
NS_ASSERTION(!isNull(), "LockBuffer() called on null software layer client!");
AndroidBridge::AutoLocalJNIFrame(1);
return JNI()->CallObjectMethod(wrapped_obj, jLockBufferMethod);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return nsnull;
AndroidBridge::AutoLocalJNIFrame(env, 1);
return env->CallObjectMethod(wrapped_obj, jLockBufferMethod);
}
unsigned char *
AndroidGeckoSoftwareLayerClient::LockBufferBits()
{
AndroidBridge::AutoLocalJNIFrame(1);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return nsnull;
AndroidBridge::AutoLocalJNIFrame(env, 1);
jobject bufferObject = LockBuffer();
if (bufferObject != nsnull)
return reinterpret_cast<unsigned char *>(JNI()->GetDirectBufferAddress(bufferObject));
return reinterpret_cast<unsigned char *>(env->GetDirectBufferAddress(bufferObject));
return nsnull;
}
@ -653,14 +676,22 @@ void
AndroidGeckoSoftwareLayerClient::UnlockBuffer()
{
NS_ASSERTION(!isNull(), "UnlockBuffer() called on null software layer client!");
AndroidBridge::AutoLocalJNIFrame(1);
JNI()->CallVoidMethod(wrapped_obj, jUnlockBufferMethod);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return;
AndroidBridge::AutoLocalJNIFrame(env, 1);
env->CallVoidMethod(wrapped_obj, jUnlockBufferMethod);
}
void
AndroidGeckoSoftwareLayerClient::GetRenderOffset(nsIntPoint &aOffset)
{
AndroidPoint offset(JNI(), JNI()->CallObjectMethod(wrapped_obj, jGetRenderOffsetMethod));
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return;
AndroidPoint offset(env, env->CallObjectMethod(wrapped_obj, jGetRenderOffsetMethod));
aOffset.x = offset.X();
aOffset.y = offset.Y();
}
@ -669,41 +700,65 @@ bool
AndroidGeckoSoftwareLayerClient::BeginDrawing(int aWidth, int aHeight, int aTileWidth, int aTileHeight, const nsAString &aMetadata, bool aHasDirectTexture)
{
NS_ASSERTION(!isNull(), "BeginDrawing() called on null software layer client!");
AndroidBridge::AutoLocalJNIFrame(1);
jstring jMetadata = JNI()->NewString(nsPromiseFlatString(aMetadata).get(), aMetadata.Length());
return JNI()->CallBooleanMethod(wrapped_obj, jBeginDrawingMethod, aWidth, aHeight, aTileWidth, aTileHeight, jMetadata, aHasDirectTexture);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return false;
AndroidBridge::AutoLocalJNIFrame(env, 1);
jstring jMetadata = env->NewString(nsPromiseFlatString(aMetadata).get(), aMetadata.Length());
return env->CallBooleanMethod(wrapped_obj, jBeginDrawingMethod, aWidth, aHeight, aTileWidth, aTileHeight, jMetadata, aHasDirectTexture);
}
void
AndroidGeckoSoftwareLayerClient::EndDrawing(const nsIntRect &aRect)
{
NS_ASSERTION(!isNull(), "EndDrawing() called on null software layer client!");
AndroidBridge::AutoLocalJNIFrame(1);
return JNI()->CallVoidMethod(wrapped_obj, jEndDrawingMethod, aRect.x, aRect.y, aRect.width, aRect.height);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return;
AndroidBridge::AutoLocalJNIFrame(env, 1);
return env->CallVoidMethod(wrapped_obj, jEndDrawingMethod, aRect.x, aRect.y, aRect.width, aRect.height);
}
jobject
AndroidGeckoSurfaceView::GetSoftwareDrawBitmap()
{
return JNI()->CallObjectMethod(wrapped_obj, jGetSoftwareDrawBitmapMethod);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return nsnull;
return env->CallObjectMethod(wrapped_obj, jGetSoftwareDrawBitmapMethod);
}
jobject
AndroidGeckoSurfaceView::GetSoftwareDrawBuffer()
{
return JNI()->CallObjectMethod(wrapped_obj, jGetSoftwareDrawBufferMethod);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return nsnull;
return env->CallObjectMethod(wrapped_obj, jGetSoftwareDrawBufferMethod);
}
jobject
AndroidGeckoSurfaceView::GetSurface()
{
return JNI()->CallObjectMethod(wrapped_obj, jGetSurfaceMethod);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return nsnull;
return env->CallObjectMethod(wrapped_obj, jGetSurfaceMethod);
}
jobject
AndroidGeckoSurfaceView::GetSurfaceHolder()
{
return JNI()->CallObjectMethod(wrapped_obj, jGetHolderMethod);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return nsnull;
return env->CallObjectMethod(wrapped_obj, jGetHolderMethod);
}
void
@ -734,7 +789,7 @@ nsJNIString::nsJNIString(jstring jstr, JNIEnv *jenv)
}
JNIEnv *jni = jenv;
if (!jni)
jni = JNI();
jni = AndroidBridge::GetJNIEnv();
const jchar* jCharPtr = jni->GetStringChars(jstr, NULL);
if (!jCharPtr) {

Просмотреть файл

@ -1352,8 +1352,12 @@ nsWindow::OnDraw(AndroidGeckoEvent *ae)
return;
}
void *buf = AndroidBridge::JNI()->GetDirectBufferAddress(bytebuf);
int cap = AndroidBridge::JNI()->GetDirectBufferCapacity(bytebuf);
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (!env)
return;
void *buf = env->GetDirectBufferAddress(bytebuf);
int cap = env->GetDirectBufferCapacity(bytebuf);
if (!buf || cap != (mBounds.width * mBounds.height * 2)) {
ALOG("### Software drawing, but unexpected buffer size %d expected %d (or no buffer %p)!", cap, mBounds.width * mBounds.height * 2, buf);
return;