From bee52e378daab250ae0ad7aa8d56e53832935cc4 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 23 Sep 2015 11:22:36 +0800 Subject: [PATCH] fix ui::WebView callback operate OpenGL issue. All the OpenGL operations should be in OpenGL thread. This PR wraps the callbacks in OpenGL thread. --- .../src/org/cocos2dx/lib/Cocos2dxWebView.java | 30 ++++++++++++++++--- cocos/ui/UIWebView.h | 1 + .../UIWebViewTest/UIWebViewTest.cpp | 5 +++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java index f10485ee0b..b81ede796c 100755 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebView.java @@ -58,29 +58,51 @@ public class Cocos2dxWebView extends WebView { class Cocos2dxWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String urlString) { + Cocos2dxActivity activity = (Cocos2dxActivity)getContext(); + final String finalizedUrlString = urlString; + try { URI uri = URI.create(urlString); if (uri != null && uri.getScheme().equals(mJSScheme)) { - Cocos2dxWebViewHelper._onJsCallback(mViewTag, urlString); + activity.runOnGLThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebViewHelper._onJsCallback(mViewTag, finalizedUrlString); + } + }); return true; } } catch (Exception e) { Log.d(TAG, "Failed to create URI from url"); } - return Cocos2dxWebViewHelper._shouldStartLoading(mViewTag, urlString); + return Cocos2dxWebViewHelper._shouldStartLoading(mViewTag, finalizedUrlString); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); - Cocos2dxWebViewHelper._didFinishLoading(mViewTag, url); + Cocos2dxActivity activity = (Cocos2dxActivity)getContext(); + final String finalizedUrl = url; + activity.runOnGLThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebViewHelper._didFinishLoading(mViewTag, finalizedUrl); + } + }); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); - Cocos2dxWebViewHelper._didFailLoading(mViewTag, failingUrl); + Cocos2dxActivity activity = (Cocos2dxActivity)getContext(); + final String finalizedUrl = failingUrl; + activity.runOnGLThread(new Runnable() { + @Override + public void run() { + Cocos2dxWebViewHelper._didFailLoading(mViewTag, finalizedUrl); + } + }); } } diff --git a/cocos/ui/UIWebView.h b/cocos/ui/UIWebView.h index 303dcd366b..6b4e6a11d0 100644 --- a/cocos/ui/UIWebView.h +++ b/cocos/ui/UIWebView.h @@ -147,6 +147,7 @@ public: /** * Call before a web view begins loading. + * Note: Any OpenGL related operations are forbidden in this callback. * * @param callback The web view that is about to load new content. * @return YES if the web view should begin loading content; otherwise, NO . diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp index 58e8ba707c..4e947d0745 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp @@ -141,6 +141,7 @@ bool WebViewTest::init() evalJsBtn->addClickEventListener([=](Ref*){ _webView->evaluateJS("alert(\"hello\")"); }); + evalJsBtn->setName("evalJs"); this->addChild(evalJsBtn); return true; @@ -151,12 +152,14 @@ bool WebViewTest::init() bool WebViewTest::onWebViewShouldStartLoading(experimental::ui::WebView *sender, const std::string &url) { CCLOG("onWebViewShouldStartLoading, url is %s", url.c_str()); - + //don't do any OpenGL operation here!! It's forbidden! return true; } void WebViewTest::onWebViewDidFinishLoading(experimental::ui::WebView *sender, const std::string &url) { + auto node = (ui::Button*)this->getChildByName("evalJs"); + node->setTitleText("start loading..."); CCLOG("onWebViewDidFinishLoading, url is %s", url.c_str()); }