Skip to content

Commit d064ab0

Browse files
authored
Smaller mutex scope + use new SDK android methods (Defold 1.2.188) (#31)
1 parent ede522c commit d064ab0

File tree

2 files changed

+46
-56
lines changed

2 files changed

+46
-56
lines changed

webview/src/webview_android.cpp

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#if defined(DM_PLATFORM_ANDROID)
22

3-
#include <jni.h>
3+
#include <dmsdk/dlib/android.h>
44
#include <stdlib.h>
55
#include <unistd.h>
66

@@ -37,18 +37,6 @@ struct WebViewCommand
3737
const char* m_Url;
3838
};
3939

40-
static JNIEnv* Attach()
41-
{
42-
JNIEnv* env = 0;
43-
g_AndroidApp->activity->vm->AttachCurrentThread(&env, NULL);
44-
return env;
45-
}
46-
47-
static void Detach()
48-
{
49-
g_AndroidApp->activity->vm->DetachCurrentThread();
50-
}
51-
5240
struct WebViewExtensionState
5341
{
5442
WebViewExtensionState()
@@ -111,19 +99,19 @@ int Platform_Create(lua_State* L, dmWebView::WebViewInfo* _info)
11199
g_WebView.m_Used[webview_id] = true;
112100
g_WebView.m_Info[webview_id] = *_info;
113101

114-
JNIEnv* env = Attach();
102+
dmAndroid::ThreadAttacher threadAttacher;
103+
JNIEnv* env = threadAttacher.GetEnv();
115104
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_Create, webview_id);
116-
Detach();
117105

118106
return webview_id;
119107
}
120108

121109
static int DestroyWebView(int webview_id)
122110
{
123111
CHECK_WEBVIEW_AND_RETURN();
124-
JNIEnv* env = Attach();
112+
dmAndroid::ThreadAttacher threadAttacher;
113+
JNIEnv* env = threadAttacher.GetEnv();
125114
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_Destroy, webview_id);
126-
Detach();
127115
ClearWebViewInfo(&g_WebView.m_Info[webview_id]);
128116
g_WebView.m_Used[webview_id] = false;
129117
return 0;
@@ -140,23 +128,23 @@ int Platform_Open(lua_State* L, int webview_id, const char* url, dmWebView::Requ
140128
CHECK_WEBVIEW_AND_RETURN();
141129
int request_id = ++g_WebView.m_RequestIds[webview_id];
142130

143-
JNIEnv* env = Attach();
131+
dmAndroid::ThreadAttacher threadAttacher;
132+
JNIEnv* env = threadAttacher.GetEnv();
144133
jstring jurl = env->NewStringUTF(url);
145134
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_Load, jurl, webview_id, request_id, options->m_Hidden);
146135
env->DeleteLocalRef(jurl);
147-
Detach();
148136
return request_id;
149137
}
150138

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

155-
JNIEnv* env = Attach();
143+
dmAndroid::ThreadAttacher threadAttacher;
144+
JNIEnv* env = threadAttacher.GetEnv();
156145
jstring jurl = env->NewStringUTF(url);
157146
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_ContinueLoading, jurl, webview_id, request_id);
158147
env->DeleteLocalRef(jurl);
159-
Detach();
160148
return request_id;
161149
}
162150

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

174-
JNIEnv* env = Attach();
162+
dmAndroid::ThreadAttacher threadAttacher;
163+
JNIEnv* env = threadAttacher.GetEnv();
175164
jstring jhtml = env->NewStringUTF(html);
176165
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_LoadRaw, jhtml, webview_id, request_id, options->m_Hidden);
177166
env->DeleteLocalRef(jhtml);
178-
Detach();
179167
return request_id;
180168
}
181169

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

187-
JNIEnv* env = Attach();
175+
dmAndroid::ThreadAttacher threadAttacher;
176+
JNIEnv* env = threadAttacher.GetEnv();
188177
jstring jcode = env->NewStringUTF(code);
189178
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_Eval, jcode, webview_id, request_id);
190-
Detach();
191179
return request_id;
192180
}
193181

194182
int Platform_SetVisible(lua_State* L, int webview_id, int visible)
195183
{
196184
CHECK_WEBVIEW_AND_RETURN();
197-
JNIEnv* env = Attach();
185+
dmAndroid::ThreadAttacher threadAttacher;
186+
JNIEnv* env = threadAttacher.GetEnv();
198187
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_SetVisible, webview_id, visible);
199-
Detach();
200188
return 0;
201189
}
202190

203191
int Platform_IsVisible(lua_State* L, int webview_id)
204192
{
205193
CHECK_WEBVIEW_AND_RETURN();
206-
JNIEnv* env = Attach();
194+
dmAndroid::ThreadAttacher threadAttacher;
195+
JNIEnv* env = threadAttacher.GetEnv();
207196
int visible = env->CallIntMethod(g_WebView.m_WebViewJNI, g_WebView.m_IsVisible, webview_id);
208-
Detach();
209197
return visible;
210198
}
211199

212200
int Platform_SetPosition(lua_State* L, int webview_id, int x, int y, int width, int height)
213201
{
214202
CHECK_WEBVIEW_AND_RETURN();
215-
JNIEnv* env = Attach();
203+
dmAndroid::ThreadAttacher threadAttacher;
204+
JNIEnv* env = threadAttacher.GetEnv();
216205
env->CallVoidMethod(g_WebView.m_WebViewJNI, g_WebView.m_SetPosition, webview_id, x, y, width, height);
217-
Detach();
218206
return 0;
219207
}
220208

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

308-
DM_MUTEX_SCOPED_LOCK(g_WebView.m_Mutex);
309-
for (uint32_t i=0; i != g_WebView.m_CmdQueue.Size(); ++i)
298+
dmArray<WebViewCommand> tmp;
310299
{
311-
const WebViewCommand& cmd = g_WebView.m_CmdQueue[i];
300+
DM_MUTEX_SCOPED_LOCK(g_WebView.m_Mutex);
301+
tmp.Swap(g_WebView.m_CmdQueue);
302+
}
303+
304+
for (uint32_t i=0; i != tmp.Size(); ++i)
305+
{
306+
const WebViewCommand& cmd = tmp[i];
312307

313308
dmWebView::CallbackInfo cbinfo;
314309
switch (cmd.m_Type)
@@ -373,7 +368,6 @@ dmExtension::Result Platform_Update(dmExtension::Params* params)
373368
free(cmd.m_Data);
374369
}
375370
}
376-
g_WebView.m_CmdQueue.SetSize(0);
377371
return dmExtension::RESULT_OK;
378372
}
379373

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

385-
JNIEnv* env = Attach();
386-
387-
jclass activity_class = env->FindClass("android/app/NativeActivity");
388-
jmethodID get_class_loader = env->GetMethodID(activity_class,"getClassLoader", "()Ljava/lang/ClassLoader;");
389-
jobject cls = env->CallObjectMethod(g_AndroidApp->activity->clazz, get_class_loader);
390-
jclass class_loader = env->FindClass("java/lang/ClassLoader");
391-
jmethodID find_class = env->GetMethodID(class_loader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
392-
jstring str_class_name = env->NewStringUTF("com.defold.webview.WebViewJNI");
393-
jclass webview_class = (jclass)env->CallObjectMethod(cls, find_class, str_class_name);
394-
env->DeleteLocalRef(str_class_name);
379+
dmAndroid::ThreadAttacher threadAttacher;
380+
JNIEnv* env = threadAttacher.GetEnv();
381+
jclass webview_class = dmAndroid::LoadClass(env, "com.defold.webview.WebViewJNI");
395382

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

406393
jmethodID jni_constructor = env->GetMethodID(webview_class, "<init>", "(Landroid/app/Activity;I)V");
407-
g_WebView.m_WebViewJNI = env->NewGlobalRef(env->NewObject(webview_class, jni_constructor, g_AndroidApp->activity->clazz, dmWebView::MAX_NUM_WEBVIEWS));
408-
409-
Detach();
394+
g_WebView.m_WebViewJNI = env->NewGlobalRef(env->NewObject(webview_class, jni_constructor, threadAttacher.GetActivity()->clazz, dmWebView::MAX_NUM_WEBVIEWS));
410395

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

419404
dmExtension::Result Platform_AppFinalize(dmExtension::AppParams* params)
420405
{
421-
JNIEnv* env = Attach();
406+
dmAndroid::ThreadAttacher threadAttacher;
407+
JNIEnv* env = threadAttacher.GetEnv();
422408
env->DeleteGlobalRef(g_WebView.m_WebViewJNI);
423-
Detach();
424409
g_WebView.m_WebViewJNI = NULL;
425410

426411
dmMutex::Delete(g_WebView.m_Mutex);

webview/src/webview_darwin.mm

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigatio
139139

140140
static void QueueCommand(Command* cmd)
141141
{
142-
dmMutex::ScopedLock lk(g_WebView.m_Mutex);
142+
DM_MUTEX_SCOPED_LOCK(g_WebView.m_Mutex);
143143
if (g_WebView.m_CmdQueue.Full())
144144
{
145145
g_WebView.m_CmdQueue.OffsetCapacity(8);
@@ -355,7 +355,7 @@ int Platform_SetPosition(lua_State* L, int webview_id, int x, int y, int width,
355355
}
356356
}
357357

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

376-
dmMutex::ScopedLock lk(g_WebView.m_Mutex);
377-
for (uint32_t i=0; i != g_WebView.m_CmdQueue.Size(); ++i)
378+
dmArray<Command> tmp;
378379
{
379-
const Command& cmd = g_WebView.m_CmdQueue[i];
380+
DM_MUTEX_SCOPED_LOCK(g_WebView.m_Mutex);
381+
tmp.Swap(g_WebView.m_CmdQueue);
382+
}
383+
for (uint32_t i=0; i != tmp.Size(); ++i)
384+
{
385+
const Command& cmd = tmp[i];
380386

381387
dmWebView::CallbackInfo cbinfo;
382388
switch (cmd.m_Type)
@@ -411,7 +417,6 @@ int Platform_SetPosition(lua_State* L, int webview_id, int x, int y, int width,
411417
free(cmd.m_Data);
412418
}
413419
}
414-
g_WebView.m_CmdQueue.SetSize(0);
415420
return dmExtension::RESULT_OK;
416421
}
417422

0 commit comments

Comments
 (0)