Auto merge of #22239 - paulrouget:shutdown2, r=jdm

shutdown synchronously

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [ ] These changes fix #__ (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/22239)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-11-23 11:19:00 -05:00 committed by GitHub
commit d7d9015518
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 21 deletions

View file

@ -12,6 +12,8 @@ import android.util.Log;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import org.freedesktop.gstreamer.GStreamer; import org.freedesktop.gstreamer.GStreamer;
import org.mozilla.servoview.JNIServo.ServoOptions; import org.mozilla.servoview.JNIServo.ServoOptions;
@ -21,6 +23,8 @@ public class Servo {
private AssetManager mAssetMgr; private AssetManager mAssetMgr;
private JNIServo mJNI = new JNIServo(); private JNIServo mJNI = new JNIServo();
private RunCallback mRunCallback; private RunCallback mRunCallback;
private boolean mShuttingDown;
private boolean mShutdownComplete;
private boolean mSuspended; private boolean mSuspended;
public Servo( public Servo(
@ -47,12 +51,33 @@ public class Servo {
} }
} }
public void requestShutdown() { public void shutdown() {
mRunCallback.inGLThread(() -> mJNI.requestShutdown()); mShuttingDown = true;
} FutureTask<Void> task = new FutureTask<Void>(new Callable<Void>() {
public Void call() throws Exception {
public void deinit() { mJNI.requestShutdown();
mRunCallback.inGLThread(() -> mJNI.deinit()); // 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() { public String version() {
@ -161,8 +186,6 @@ public class Servo {
void inGLThread(Runnable f); void inGLThread(Runnable f);
void inUIThread(Runnable f); void inUIThread(Runnable f);
void finalizeShutdown();
} }
public interface GfxCallbacks { public interface GfxCallbacks {
@ -184,7 +207,7 @@ public class Servo {
} }
public void wakeup() { public void wakeup() {
if (!mSuspended) { if (!mSuspended && !mShuttingDown) {
mRunCallback.inGLThread(() -> mJNI.performUpdates()); mRunCallback.inGLThread(() -> mJNI.performUpdates());
} }
} }
@ -200,7 +223,7 @@ public class Servo {
} }
public void onShutdownComplete() { public void onShutdownComplete() {
mRunCallback.finalizeShutdown(); mShutdownComplete = true;
} }
public void onAnimatingChanged(boolean animating) { public void onAnimatingChanged(boolean animating) {

View file

@ -70,7 +70,9 @@ public class ServoSurface {
public void shutdown() { public void shutdown() {
Log.d(LOGTAG, "shutdown"); Log.d(LOGTAG, "shutdown");
mServo.requestShutdown(); mServo.shutdown();
mServo = null;
mGLThread.shutdown();
try { try {
Log.d(LOGTAG, "Waiting for GL thread to shutdown"); Log.d(LOGTAG, "Waiting for GL thread to shutdown");
mGLThread.join(); mGLThread.join();
@ -228,9 +230,8 @@ public class ServoSurface {
mMainLooperHandler.post(r); mMainLooperHandler.post(r);
} }
public void finalizeShutdown() { public void shutdown() {
Log.d(LOGTAG, "finalizeShutdown"); Log.d(LOGTAG, "GLThread::shutdown");
mServo.deinit();
mSurface.destroy(); mSurface.destroy();
mGLLooperHandler.getLooper().quitSafely(); mGLLooperHandler.getLooper().quitSafely();
} }

View file

@ -69,8 +69,9 @@ public class ServoView extends GLSurfaceView
init(context); init(context);
} }
protected void onDetachedFromWindow() { public void onDetachedFromWindow() {
mServo.requestShutdown(); mServo.shutdown();
mServo = null;
super.onDetachedFromWindow(); super.onDetachedFromWindow();
} }
@ -140,11 +141,6 @@ public class ServoView extends GLSurfaceView
public void makeCurrent() { public void makeCurrent() {
} }
public void finalizeShutdown() {
// shutdown has been requested and completed.
mServo.deinit();
}
public void inGLThread(Runnable f) { public void inGLThread(Runnable f) {
queueEvent(f); queueEvent(f);
} }