Skip to content

Commit

Permalink
Smaller mutex scope + use new SDK android methods (Defold 1.2.188) (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
AGulev authored Oct 31, 2021
1 parent ede522c commit d064ab0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 56 deletions.
85 changes: 35 additions & 50 deletions webview/src/webview_android.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#if defined(DM_PLATFORM_ANDROID)

#include <jni.h>
#include <dmsdk/dlib/android.h>
#include <stdlib.h>
#include <unistd.h>

Expand Down Expand Up @@ -37,18 +37,6 @@ struct WebViewCommand
const char* m_Url;
};

static JNIEnv* Attach()
{
JNIEnv* env = 0;
g_AndroidApp->activity->vm->AttachCurrentThread(&env, NULL);
return env;
}

static void Detach()
{
g_AndroidApp->activity->vm->DetachCurrentThread();
}

struct WebViewExtensionState
{
WebViewExtensionState()
Expand Down Expand Up @@ -111,19 +99,19 @@ int Platform_Create(lua_State* L, dmWebView::WebViewInfo* _info)
g_WebView.m_Used[webview_id] = true;
g_WebView.m_Info[webview_id] = *_info;

JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_Create, webview_id);
Detach();

return webview_id;
}

static int DestroyWebView(int webview_id)
{
CHECK_WEBVIEW_AND_RETURN();
JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_Destroy, webview_id);
Detach();
ClearWebViewInfo(&g_WebView.m_Info[webview_id]);
g_WebView.m_Used[webview_id] = false;
return 0;
Expand All @@ -140,23 +128,23 @@ int Platform_Open(lua_State* L, int webview_id, const char* url, dmWebView::Requ
CHECK_WEBVIEW_AND_RETURN();
int request_id = ++g_WebView.m_RequestIds[webview_id];

JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();
jstring jurl = env->NewStringUTF(url);
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_Load, jurl, webview_id, request_id, options->m_Hidden);
env->DeleteLocalRef(jurl);
Detach();
return request_id;
}

int Platform_ContinueOpen(lua_State* L, int webview_id, int request_id, const char* url)
{
CHECK_WEBVIEW_AND_RETURN();

JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();
jstring jurl = env->NewStringUTF(url);
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_ContinueLoading, jurl, webview_id, request_id);
env->DeleteLocalRef(jurl);
Detach();
return request_id;
}

Expand All @@ -171,11 +159,11 @@ int Platform_OpenRaw(lua_State* L, int webview_id, const char* html, dmWebView::
CHECK_WEBVIEW_AND_RETURN();
int request_id = ++g_WebView.m_RequestIds[webview_id];

JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();
jstring jhtml = env->NewStringUTF(html);
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_LoadRaw, jhtml, webview_id, request_id, options->m_Hidden);
env->DeleteLocalRef(jhtml);
Detach();
return request_id;
}

Expand All @@ -184,37 +172,37 @@ int Platform_Eval(lua_State* L, int webview_id, const char* code)
CHECK_WEBVIEW_AND_RETURN();
int request_id = ++g_WebView.m_RequestIds[webview_id];

JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();
jstring jcode = env->NewStringUTF(code);
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_Eval, jcode, webview_id, request_id);
Detach();
return request_id;
}

int Platform_SetVisible(lua_State* L, int webview_id, int visible)
{
CHECK_WEBVIEW_AND_RETURN();
JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_SetVisible, webview_id, visible);
Detach();
return 0;
}

int Platform_IsVisible(lua_State* L, int webview_id)
{
CHECK_WEBVIEW_AND_RETURN();
JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();
int visible = env->CallIntMethod(g_WebView.m_WebViewJNI, g_WebView.m_IsVisible, webview_id);
Detach();
return visible;
}

int Platform_SetPosition(lua_State* L, int webview_id, int x, int y, int width, int height)
{
CHECK_WEBVIEW_AND_RETURN();
JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_SetPosition, webview_id, x, y, width, height);
Detach();
return 0;
}

Expand Down Expand Up @@ -303,12 +291,19 @@ JNIEXPORT void JNICALL Java_com_defold_webview_WebViewJNI_onPageLoading(JNIEnv*
dmExtension::Result Platform_Update(dmExtension::Params* params)
{
if (g_WebView.m_CmdQueue.Empty())
{
return dmExtension::RESULT_OK; // avoid a lock (~300us on iPhone 4s)
}

DM_MUTEX_SCOPED_LOCK(g_WebView.m_Mutex);
for (uint32_t i=0; i != g_WebView.m_CmdQueue.Size(); ++i)
dmArray<WebViewCommand> tmp;
{
const WebViewCommand& cmd = g_WebView.m_CmdQueue[i];
DM_MUTEX_SCOPED_LOCK(g_WebView.m_Mutex);
tmp.Swap(g_WebView.m_CmdQueue);
}

for (uint32_t i=0; i != tmp.Size(); ++i)
{
const WebViewCommand& cmd = tmp[i];

dmWebView::CallbackInfo cbinfo;
switch (cmd.m_Type)
Expand Down Expand Up @@ -373,7 +368,6 @@ dmExtension::Result Platform_Update(dmExtension::Params* params)
free(cmd.m_Data);
}
}
g_WebView.m_CmdQueue.SetSize(0);
return dmExtension::RESULT_OK;
}

Expand All @@ -382,16 +376,9 @@ dmExtension::Result Platform_AppInitialize(dmExtension::AppParams* params)
g_WebView.m_Mutex = dmMutex::New();
g_WebView.m_CmdQueue.SetCapacity(8);

JNIEnv* env = Attach();

jclass activity_class = env->FindClass("android/app/NativeActivity");
jmethodID get_class_loader = env->GetMethodID(activity_class,"getClassLoader", "()Ljava/lang/ClassLoader;");
jobject cls = env->CallObjectMethod(g_AndroidApp->activity->clazz, get_class_loader);
jclass class_loader = env->FindClass("java/lang/ClassLoader");
jmethodID find_class = env->GetMethodID(class_loader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
jstring str_class_name = env->NewStringUTF("com.defold.webview.WebViewJNI");
jclass webview_class = (jclass)env->CallObjectMethod(cls, find_class, str_class_name);
env->DeleteLocalRef(str_class_name);
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();
jclass webview_class = dmAndroid::LoadClass(env, "com.defold.webview.WebViewJNI");

g_WebView.m_Create = env->GetMethodID(webview_class, "create", "(I)V");
g_WebView.m_Destroy = env->GetMethodID(webview_class, "destroy", "(I)V");
Expand All @@ -404,9 +391,7 @@ dmExtension::Result Platform_AppInitialize(dmExtension::AppParams* params)
g_WebView.m_SetPosition = env->GetMethodID(webview_class, "setPosition", "(IIIII)V");

jmethodID jni_constructor = env->GetMethodID(webview_class, "<init>", "(Landroid/app/Activity;I)V");
g_WebView.m_WebViewJNI = env->NewGlobalRef(env->NewObject(webview_class, jni_constructor, g_AndroidApp->activity->clazz, dmWebView::MAX_NUM_WEBVIEWS));

Detach();
g_WebView.m_WebViewJNI = env->NewGlobalRef(env->NewObject(webview_class, jni_constructor, threadAttacher.GetActivity()->clazz, dmWebView::MAX_NUM_WEBVIEWS));

return dmExtension::RESULT_OK;
}
Expand All @@ -418,9 +403,9 @@ dmExtension::Result Platform_Initialize(dmExtension::Params* params)

dmExtension::Result Platform_AppFinalize(dmExtension::AppParams* params)
{
JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();
env->DeleteGlobalRef(g_WebView.m_WebViewJNI);
Detach();
g_WebView.m_WebViewJNI = NULL;

dmMutex::Delete(g_WebView.m_Mutex);
Expand Down
17 changes: 11 additions & 6 deletions webview/src/webview_darwin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigatio

static void QueueCommand(Command* cmd)
{
dmMutex::ScopedLock lk(g_WebView.m_Mutex);
DM_MUTEX_SCOPED_LOCK(g_WebView.m_Mutex);
if (g_WebView.m_CmdQueue.Full())
{
g_WebView.m_CmdQueue.OffsetCapacity(8);
Expand Down Expand Up @@ -355,7 +355,7 @@ int Platform_SetPosition(lua_State* L, int webview_id, int x, int y, int width,
}
}

dmMutex::ScopedLock lk(g_WebView.m_Mutex);
DM_MUTEX_SCOPED_LOCK(g_WebView.m_Mutex);
for (uint32_t i=0; i != g_WebView.m_CmdQueue.Size(); ++i)
{
const Command& cmd = g_WebView.m_CmdQueue[i];
Expand All @@ -371,12 +371,18 @@ int Platform_SetPosition(lua_State* L, int webview_id, int x, int y, int width,
dmExtension::Result Platform_Update(dmExtension::Params* params)
{
if (g_WebView.m_CmdQueue.Empty())
{
return dmExtension::RESULT_OK; // avoid a lock (~300us on iPhone 4s)
}

dmMutex::ScopedLock lk(g_WebView.m_Mutex);
for (uint32_t i=0; i != g_WebView.m_CmdQueue.Size(); ++i)
dmArray<Command> tmp;
{
const Command& cmd = g_WebView.m_CmdQueue[i];
DM_MUTEX_SCOPED_LOCK(g_WebView.m_Mutex);
tmp.Swap(g_WebView.m_CmdQueue);
}
for (uint32_t i=0; i != tmp.Size(); ++i)
{
const Command& cmd = tmp[i];

dmWebView::CallbackInfo cbinfo;
switch (cmd.m_Type)
Expand Down Expand Up @@ -411,7 +417,6 @@ int Platform_SetPosition(lua_State* L, int webview_id, int x, int y, int width,
free(cmd.m_Data);
}
}
g_WebView.m_CmdQueue.SetSize(0);
return dmExtension::RESULT_OK;
}

Expand Down

0 comments on commit d064ab0

Please sign in to comment.