Skip to content

Commit

Permalink
widgetplugin: reduce some of the locking, simplify and also fix
Browse files Browse the repository at this point in the history
the deadlock on s8

doResume must be called before a widget is initialized; the code though
was written in such a way that doResume may end up waiting for widget
initialization
  • Loading branch information
liaxim committed Apr 13, 2017
1 parent 9afd405 commit 652501d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,31 @@

import com.badlogic.gdx.ApplicationAdapter;

import java.util.concurrent.CountDownLatch;

/**
* GVRWidget is a wrapper for LibGDX widget. base class for application widget
* implementation
*/

public abstract class GVRWidget extends ApplicationAdapter {

private boolean mIsInitialised = false;
private int mTexid = 0;
private Object mSync;

@Override
public void create() {
}

@Override
public void resize(int width, int height) {
}

@Override
public void render() {
}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void dispose() {
}

public boolean isInitialised() {
return mIsInitialised;
}
private int mTexid;
private CountDownLatch mInitializedLatch = new CountDownLatch(1);

@Override
public void notifyCreation(int id) {
mIsInitialised = true;
mTexid = id;
synchronized (mSync) {
mSync.notifyAll();
}

mInitializedLatch.countDown();
}

public int getTexId() {
try {
mInitializedLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return mTexid;
}

public void setSyncObject(Object obj) {
mSync = obj;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@
import org.gearvrf.GVREventListeners;
import org.gearvrf.GVRMain;
import org.gearvrf.GVRPicker;
import org.gearvrf.GVRSceneObject;
import org.gearvrf.IActivityEvents;
import org.gearvrf.IScriptEvents;
import org.gearvrf.utility.Threads;

import java.lang.reflect.Method;
import java.util.concurrent.CountDownLatch;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLContext;
Expand Down Expand Up @@ -89,37 +90,36 @@ public class GVRWidgetPlugin implements AndroidApplicationBase {
protected boolean mHideStatusBar = false;
private int mWasFocusChanged = -1;
private boolean mIsWaitingForAudio = false;
protected int mFBOTextureId = 0;
private Object mSync = new Object();
private EGLContext mEGLContext;
private final CountDownLatch mEglContextLatch = new CountDownLatch(1);
private GVRActivity mActivity;

private IActivityEvents mActivityEventsListener = new GVREventListeners.ActivityEvents() {
@Override
public void onPause() {
if(mGraphics != null){
boolean isContinuous = mGraphics.isContinuousRendering();
boolean isContinuousEnforced = AndroidGraphics.enforceContinuousRendering;

// from here we don't want non continuous rendering
AndroidGraphics.enforceContinuousRendering = true;
mGraphics.setContinuousRendering(true);
// calls to setContinuousRendering(false) from other thread (ex:
// GLThread)
// will be ignored at this point...
mGraphics.pause();

mInputDispatcher.getInput().onPause();

if (mActivity.isFinishing()) {
mGraphics.clearManagedCaches();
mGraphics.destroy();
}
if (mGraphics != null) {
boolean isContinuous = mGraphics.isContinuousRendering();
boolean isContinuousEnforced = AndroidGraphics.enforceContinuousRendering;

// from here we don't want non continuous rendering
AndroidGraphics.enforceContinuousRendering = true;
mGraphics.setContinuousRendering(true);
// calls to setContinuousRendering(false) from other thread (ex:
// GLThread)
// will be ignored at this point...
mGraphics.pause();

mInputDispatcher.getInput().onPause();

if (mActivity.isFinishing()) {
mGraphics.clearManagedCaches();
mGraphics.destroy();
}

AndroidGraphics.enforceContinuousRendering = isContinuousEnforced;
mGraphics.setContinuousRendering(isContinuous);
AndroidGraphics.enforceContinuousRendering = isContinuousEnforced;
mGraphics.setContinuousRendering(isContinuous);

mGraphics.onPauseGLSurfaceView();
mGraphics.onPauseGLSurfaceView();
}
}

Expand Down Expand Up @@ -160,7 +160,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
resultCode, data);
}
}
}
}
};

private IScriptEvents mScriptEventsListener = new GVREventListeners.ScriptEvents() {
Expand Down Expand Up @@ -476,10 +476,7 @@ protected void initWidget() {
@Override
public void run() {
mEGLContext = ((EGL10) EGLContext.getEGL()).eglGetCurrentContext();
syncNotify();
while (!isInitialised()) {
syncWait();
}
mEglContextLatch.countDown();
}
});
}
Expand All @@ -496,10 +493,6 @@ public int getHeight() {
return mViewHeight;
}

protected boolean isInitialised() {
return mWidget.isInitialised();
}

public int getTextureId() {
return mWidget.getTexId();
}
Expand All @@ -514,29 +507,25 @@ public void setPickedObject(GVRPicker.GVRPickedObject pickInfo) {

public void initializeWidget(GVRWidget widget) {
mWidget = widget;
mWidget.setSyncObject(mSync);
Thread thread = new Thread() {

Threads.spawn(new Runnable() {
@Override
public void run() {
synchronized (mSync) {
try {
while (mEGLContext == null) {
mSync.wait();
try {
mEglContextLatch.await();

runOnUiThread(new Runnable() {
public void run() {
doResume(mWidget);
}
runOnUiThread(new Runnable() {
public void run() {
doResume(mWidget);
}
});

} catch (InterruptedException e) {
e.printStackTrace();
}
});

} catch (InterruptedException e) {
e.printStackTrace();
mActivity.finish();
}
}
};

thread.start();
});
}

private void doResume(GVRWidget widget) {
Expand Down Expand Up @@ -569,22 +558,6 @@ private void doResume(GVRWidget widget) {
}
}

protected void syncNotify() {
synchronized (mSync) {
mSync.notifyAll();
}
}

protected void syncWait() {
synchronized (mSync) {
try {
mSync.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@Override
public void debug(String arg0, String arg1) {
}
Expand Down

0 comments on commit 652501d

Please sign in to comment.