Skip to content

Commit

Permalink
shutdown synchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrouget committed Nov 22, 2018
1 parent 2b410ac commit 799833a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
Expand Up @@ -12,6 +12,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import org.freedesktop.gstreamer.GStreamer;
import org.mozilla.servoview.JNIServo.ServoOptions;
Expand All @@ -21,6 +23,8 @@ public class Servo {
private AssetManager mAssetMgr;
private JNIServo mJNI = new JNIServo();
private RunCallback mRunCallback;
private boolean mShuttingDown;
private boolean mShutdownComplete;
private boolean mSuspended;

public Servo(
Expand All @@ -47,12 +51,33 @@ public Servo(
}
}

public void requestShutdown() {
mRunCallback.inGLThread(() -> mJNI.requestShutdown());
}

public void deinit() {
mRunCallback.inGLThread(() -> mJNI.deinit());
public void shutdown() {
mShuttingDown = true;
FutureTask<Void> task = new FutureTask<Void>(new Callable<Void>() {
public Void call() throws Exception {
mJNI.requestShutdown();
// Wait until Servo gets back to us to finalize shutdown.
while (!mShutdownComplete) {
try {
Thread.sleep(10);
} catch (Exception e) {
mShutdownComplete = true;
e.printStackTrace();
return null;
}
mJNI.performUpdates();
}
mJNI.deinit();
return null;
}
});
mRunCallback.inGLThread(task);
// Block until task is complete.
try {
task.get();
} catch (Exception e) {
e.printStackTrace();
}
}

public String version() {
Expand Down Expand Up @@ -161,8 +186,6 @@ public interface RunCallback {
void inGLThread(Runnable f);

void inUIThread(Runnable f);

void finalizeShutdown();
}

public interface GfxCallbacks {
Expand All @@ -184,7 +207,7 @@ private class Callbacks implements JNIServo.Callbacks, Client {
}

public void wakeup() {
if (!mSuspended) {
if (!mSuspended && !mShuttingDown) {
mRunCallback.inGLThread(() -> mJNI.performUpdates());
}
}
Expand All @@ -200,7 +223,7 @@ public void makeCurrent() {
}

public void onShutdownComplete() {
mRunCallback.finalizeShutdown();
mShutdownComplete = true;
}

public void onAnimatingChanged(boolean animating) {
Expand Down
Expand Up @@ -70,7 +70,9 @@ public void runLoop() {

public void shutdown() {
Log.d(LOGTAG, "shutdown");
mServo.requestShutdown();
mServo.shutdown();
mServo = null;
mGLThread.shutdown();
try {
Log.d(LOGTAG, "Waiting for GL thread to shutdown");
mGLThread.join();
Expand Down Expand Up @@ -228,9 +230,8 @@ public void inUIThread(Runnable r) {
mMainLooperHandler.post(r);
}

public void finalizeShutdown() {
Log.d(LOGTAG, "finalizeShutdown");
mServo.deinit();
public void shutdown() {
Log.d(LOGTAG, "GLThread::shutdown");
mSurface.destroy();
mGLLooperHandler.getLooper().quitSafely();
}
Expand Down
Expand Up @@ -69,8 +69,9 @@ public ServoView(Context context, AttributeSet attrs) {
init(context);
}

protected void onDetachedFromWindow() {
mServo.requestShutdown();
public void onDetachedFromWindow() {
mServo.shutdown();
mServo = null;
super.onDetachedFromWindow();
}

Expand Down Expand Up @@ -140,11 +141,6 @@ public void animationStateChanged(boolean animating) {
public void makeCurrent() {
}

public void finalizeShutdown() {
// shutdown has been requested and completed.
mServo.deinit();
}

public void inGLThread(Runnable f) {
queueEvent(f);
}
Expand Down

0 comments on commit 799833a

Please sign in to comment.