mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-29 20:52:16 +09:00
[Fix] (MobileGL, android-plugin/app): release the test surface before restarting a canceled run
This commit is contained in:
1 parent
df0e1d3bdd
commit
bc7e703e23
3 files changed
+91
-39
No files matched your search
@@ -264,7 +264,16 @@ namespace {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string RunScene(const Scene& scene, ANativeWindow* window, bool vsync) {
|
struct SceneCancellation {
|
||||||
|
JNIEnv* env;
|
||||||
|
jobject flag;
|
||||||
|
jmethodID get;
|
||||||
|
|
||||||
|
bool Requested() const { return env->CallBooleanMethod(flag, get) == JNI_TRUE; }
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string RunScene(const Scene& scene, ANativeWindow* window, bool vsync, const SceneCancellation& cancellation) {
|
||||||
|
if (cancellation.Requested()) return Error("Canceled");
|
||||||
SceneWindow egl;
|
SceneWindow egl;
|
||||||
egl.Open(window, vsync);
|
egl.Open(window, vsync);
|
||||||
const auto* renderer = reinterpret_cast<const char*>(Gl::GetString(GL_RENDERER));
|
const auto* renderer = reinterpret_cast<const char*>(Gl::GetString(GL_RENDERER));
|
||||||
@@ -310,13 +319,16 @@ namespace {
|
|||||||
throw MobileGL::RuntimeError("eglSwapBuffers failed");
|
throw MobileGL::RuntimeError("eglSwapBuffers failed");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
for (int i = 0; i < 8; ++i)
|
for (int i = 0; i < 8; ++i) {
|
||||||
|
if (cancellation.Requested()) return Error("Canceled");
|
||||||
draw();
|
draw();
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<double> frameTimes;
|
std::vector<double> frameTimes;
|
||||||
const auto sampleStart = Clock::now();
|
const auto sampleStart = Clock::now();
|
||||||
auto end = sampleStart;
|
auto end = sampleStart;
|
||||||
do {
|
do {
|
||||||
|
if (cancellation.Requested()) return Error("Canceled");
|
||||||
const auto begin = end;
|
const auto begin = end;
|
||||||
draw();
|
draw();
|
||||||
end = Clock::now();
|
end = Clock::now();
|
||||||
@@ -349,10 +361,13 @@ extern "C" JNIEXPORT jstring JNICALL Java_top_mobilegl_plugin_TestActivity_nativ
|
|||||||
return env->NewStringUTF(catalog.c_str());
|
return env->NewStringUTF(catalog.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" JNIEXPORT jstring JNICALL Java_top_mobilegl_plugin_TestService_nativeRunSample(JNIEnv* env, jclass,
|
extern "C" JNIEXPORT jstring JNICALL Java_top_mobilegl_plugin_TestService_nativeRunSample(
|
||||||
jstring sample,
|
JNIEnv* env, jclass, jstring sample, jobject surface, jboolean vsync, jobject canceled) {
|
||||||
jobject surface,
|
const auto flagType = env->GetObjectClass(canceled);
|
||||||
jboolean vsync) {
|
const auto get = env->GetMethodID(flagType, "get", "()Z");
|
||||||
|
env->DeleteLocalRef(flagType);
|
||||||
|
if (!get) return nullptr;
|
||||||
|
const SceneCancellation cancellation{env, canceled, get};
|
||||||
std::unique_ptr<ANativeWindow, decltype(&ANativeWindow_release)> window(
|
std::unique_ptr<ANativeWindow, decltype(&ANativeWindow_release)> window(
|
||||||
surface ? ANativeWindow_fromSurface(env, surface) : nullptr, ANativeWindow_release);
|
surface ? ANativeWindow_fromSurface(env, surface) : nullptr, ANativeWindow_release);
|
||||||
if (!window) return env->NewStringUTF("{\"error\":\"Surface unavailable\"}");
|
if (!window) return env->NewStringUTF("{\"error\":\"Surface unavailable\"}");
|
||||||
@@ -365,7 +380,7 @@ extern "C" JNIEXPORT jstring JNICALL Java_top_mobilegl_plugin_TestService_native
|
|||||||
try {
|
try {
|
||||||
for (const auto& scene : kScenes) {
|
for (const auto& scene : kScenes) {
|
||||||
if (selected == scene.title) {
|
if (selected == scene.title) {
|
||||||
result = RunScene(scene, window.get(), vsync == JNI_TRUE);
|
result = RunScene(scene, window.get(), vsync == JNI_TRUE, cancellation);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,9 @@ public final class TestActivity extends Activity {
|
|||||||
private Button run;
|
private Button run;
|
||||||
private Button replayTrace;
|
private Button replayTrace;
|
||||||
private Switch vsync;
|
private Switch vsync;
|
||||||
|
private FrameLayout previewFrame;
|
||||||
private SurfaceView preview;
|
private SurfaceView preview;
|
||||||
|
private boolean previewReady;
|
||||||
private ImageView snapshot;
|
private ImageView snapshot;
|
||||||
private String snapshotPath;
|
private String snapshotPath;
|
||||||
private static final int FRAME_WIDTH = 1280;
|
private static final int FRAME_WIDTH = 1280;
|
||||||
@@ -105,33 +107,17 @@ public final class TestActivity extends Activity {
|
|||||||
else if (active.isTrace()) active.cancel();
|
else if (active.isTrace()) active.cancel();
|
||||||
});
|
});
|
||||||
content.addView(replayTrace);
|
content.addView(replayTrace);
|
||||||
FrameLayout frame = new FrameLayout(this);
|
previewFrame = new FrameLayout(this);
|
||||||
content.addView(frame);
|
content.addView(previewFrame);
|
||||||
preview = new SurfaceView(this) {
|
replacePreview();
|
||||||
@Override
|
|
||||||
protected void onMeasure(int widthSpec, int heightSpec) {
|
|
||||||
int width = MeasureSpec.getSize(widthSpec);
|
|
||||||
setMeasuredDimension(width, width * FRAME_HEIGHT / FRAME_WIDTH);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
preview.setContentDescription("Live sample surface");
|
|
||||||
frame.addView(preview, new FrameLayout.LayoutParams(
|
|
||||||
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
|
|
||||||
snapshot = new ImageView(this);
|
snapshot = new ImageView(this);
|
||||||
snapshot.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
snapshot.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
||||||
snapshot.setBackgroundColor(0xFF121212);
|
snapshot.setBackgroundColor(0xFF121212);
|
||||||
snapshot.setContentDescription("Trace snapshot");
|
snapshot.setContentDescription("Trace snapshot");
|
||||||
frame.addView(snapshot, new FrameLayout.LayoutParams(
|
previewFrame.addView(snapshot, new FrameLayout.LayoutParams(
|
||||||
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
|
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
|
||||||
showSnapshot(state == null ? null : state.getString("snapshotPath"));
|
showSnapshot(state == null ? null : state.getString("snapshotPath"));
|
||||||
preview.getHolder().setFixedSize(FRAME_WIDTH, FRAME_HEIGHT);
|
;
|
||||||
preview.getHolder().addCallback(new SurfaceHolder.Callback() {
|
|
||||||
@Override public void surfaceCreated(SurfaceHolder holder) {}
|
|
||||||
@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
|
|
||||||
@Override public void surfaceDestroyed(SurfaceHolder holder) {
|
|
||||||
if (active != null && !active.isTrace() && !active.received) active.cancel();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
report = text("", 14);
|
report = text("", 14);
|
||||||
report.setTextIsSelectable(true);
|
report.setTextIsSelectable(true);
|
||||||
content.addView(report);
|
content.addView(report);
|
||||||
@@ -151,7 +137,42 @@ public final class TestActivity extends Activity {
|
|||||||
} catch (LinkageError | JSONException failure) {
|
} catch (LinkageError | JSONException failure) {
|
||||||
showResult("Samples unavailable: " + failure.getMessage());
|
showResult("Samples unavailable: " + failure.getMessage());
|
||||||
}
|
}
|
||||||
run.setEnabled(!samples.isEmpty());
|
run.setEnabled(canRunScene());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void replacePreview() {
|
||||||
|
SurfaceView previous = preview;
|
||||||
|
previewReady = false;
|
||||||
|
preview = new SurfaceView(this) {
|
||||||
|
@Override
|
||||||
|
protected void onMeasure(int widthSpec, int heightSpec) {
|
||||||
|
int width = MeasureSpec.getSize(widthSpec);
|
||||||
|
setMeasuredDimension(width, width * FRAME_HEIGHT / FRAME_WIDTH);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
preview.setContentDescription("Live sample surface");
|
||||||
|
preview.getHolder().setFixedSize(FRAME_WIDTH, FRAME_HEIGHT);
|
||||||
|
preview.getHolder().addCallback(new SurfaceHolder.Callback() {
|
||||||
|
@Override public void surfaceCreated(SurfaceHolder holder) {}
|
||||||
|
@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||||
|
if (holder != preview.getHolder()) return;
|
||||||
|
previewReady = holder.getSurface().isValid();
|
||||||
|
if (active == null) run.setEnabled(canRunScene());
|
||||||
|
}
|
||||||
|
@Override public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
|
if (holder != preview.getHolder()) return;
|
||||||
|
previewReady = false;
|
||||||
|
if (active == null) run.setEnabled(false);
|
||||||
|
else if (!active.isTrace() && !active.received) active.cancel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (previous != null) previewFrame.removeView(previous);
|
||||||
|
previewFrame.addView(preview, 0, new FrameLayout.LayoutParams(
|
||||||
|
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean canRunScene() {
|
||||||
|
return previewReady && !samples.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -195,7 +216,7 @@ public final class TestActivity extends Activity {
|
|||||||
|
|
||||||
private void startRun() {
|
private void startRun() {
|
||||||
Surface surface = preview.getHolder().getSurface();
|
Surface surface = preview.getHolder().getSurface();
|
||||||
if (!surface.isValid()) {
|
if (!previewReady || !surface.isValid()) {
|
||||||
showResult("Surface unavailable");
|
showResult("Surface unavailable");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -225,7 +246,7 @@ public final class TestActivity extends Activity {
|
|||||||
vsync.setEnabled(!running);
|
vsync.setEnabled(!running);
|
||||||
boolean replaying = running && active.isTrace();
|
boolean replaying = running && active.isTrace();
|
||||||
run.setText(running && !replaying ? "Cancel" : "Run");
|
run.setText(running && !replaying ? "Cancel" : "Run");
|
||||||
run.setEnabled(replaying ? false : running || !samples.isEmpty());
|
run.setEnabled(!replaying && (running || canRunScene()));
|
||||||
replayTrace.setText(replaying ? "Cancel trace replay" : "Replay trace…");
|
replayTrace.setText(replaying ? "Cancel trace replay" : "Replay trace…");
|
||||||
replayTrace.setEnabled(!running || replaying);
|
replayTrace.setEnabled(!running || replaying);
|
||||||
}
|
}
|
||||||
@@ -290,8 +311,9 @@ public final class TestActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean onResult(Message message) {
|
private boolean onResult(Message message) {
|
||||||
if (message.what != TestService.RESULT || active != this || cancelled) return false;
|
if (message.what != TestService.RESULT || active != this) return false;
|
||||||
received = true;
|
received = true;
|
||||||
|
if (cancelled) return true;
|
||||||
String json = message.getData().getString(TestService.REPORT, "");
|
String json = message.getData().getString(TestService.REPORT, "");
|
||||||
if (isTrace()) showTraceReport(json);
|
if (isTrace()) showTraceReport(json);
|
||||||
else showResult(formatReport(name, json));
|
else showResult(formatReport(name, json));
|
||||||
@@ -302,7 +324,11 @@ public final class TestActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
public void onServiceDisconnected(ComponentName component) {
|
public void onServiceDisconnected(ComponentName component) {
|
||||||
if (active != this) return;
|
if (active != this) return;
|
||||||
if (!received && !cancelled) showResult(name + "\nTest process exited");
|
if (!received) {
|
||||||
|
if (!cancelled) showResult(name + "\nTest process exited");
|
||||||
|
// An interrupted EGL producer may still own the old buffer queue.
|
||||||
|
if (!isTrace()) replacePreview();
|
||||||
|
}
|
||||||
complete();
|
complete();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,6 +343,7 @@ public final class TestActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void cancel() {
|
void cancel() {
|
||||||
|
if (cancelled) return;
|
||||||
cancelled = true;
|
cancelled = true;
|
||||||
showResult("Canceled");
|
showResult("Canceled");
|
||||||
if (worker == null) {
|
if (worker == null) {
|
||||||
@@ -327,7 +354,7 @@ public final class TestActivity extends Activity {
|
|||||||
try {
|
try {
|
||||||
worker.send(Message.obtain(null, TestService.CANCEL));
|
worker.send(Message.obtain(null, TestService.CANCEL));
|
||||||
} catch (RemoteException ignored) {
|
} catch (RemoteException ignored) {
|
||||||
complete();
|
onServiceDisconnected(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import android.os.RemoteException;
|
|||||||
import android.system.Os;
|
import android.system.Os;
|
||||||
import android.view.Surface;
|
import android.view.Surface;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public final class TestService extends Service {
|
public final class TestService extends Service {
|
||||||
@@ -27,14 +29,17 @@ public final class TestService extends Service {
|
|||||||
static final String SURFACE = "surface";
|
static final String SURFACE = "surface";
|
||||||
static final String VSYNC = "vsync";
|
static final String VSYNC = "vsync";
|
||||||
private static final long TIMEOUT_MS = 90_000;
|
private static final long TIMEOUT_MS = 90_000;
|
||||||
|
private static final long CANCEL_TIMEOUT_MS = 6_000;
|
||||||
static final String BACKEND_ENV_PRIORITY = "MOBILEGL_TEST_BACKEND_ENV_PRIORITY";
|
static final String BACKEND_ENV_PRIORITY = "MOBILEGL_TEST_BACKEND_ENV_PRIORITY";
|
||||||
|
|
||||||
private static native String nativeRunSample(String sample, Surface surface, boolean vsync);
|
private static native String nativeRunSample(String sample, Surface surface, boolean vsync, AtomicBoolean canceled);
|
||||||
|
|
||||||
private final Handler main = new Handler(Looper.getMainLooper());
|
private final Handler main = new Handler(Looper.getMainLooper());
|
||||||
private final Messenger requests = new Messenger(new Handler(Looper.getMainLooper(), this::onRequest));
|
private final Messenger requests = new Messenger(new Handler(Looper.getMainLooper(), this::onRequest));
|
||||||
|
private final AtomicBoolean canceled = new AtomicBoolean();
|
||||||
private boolean started;
|
private boolean started;
|
||||||
private boolean finished;
|
private boolean finished;
|
||||||
|
private boolean runningTrace;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
@@ -49,7 +54,11 @@ public final class TestService extends Service {
|
|||||||
|
|
||||||
private boolean onRequest(Message message) {
|
private boolean onRequest(Message message) {
|
||||||
if (message.what == CANCEL) {
|
if (message.what == CANCEL) {
|
||||||
exit();
|
if (runningTrace) {
|
||||||
|
exit();
|
||||||
|
} else if (started && !finished && canceled.compareAndSet(false, true)) {
|
||||||
|
main.postDelayed(this::exit, CANCEL_TIMEOUT_MS);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (message.what != RUN || started || message.replyTo == null) return false;
|
if (message.what != RUN || started || message.replyTo == null) return false;
|
||||||
@@ -57,7 +66,8 @@ public final class TestService extends Service {
|
|||||||
Messenger reply = message.replyTo;
|
Messenger reply = message.replyTo;
|
||||||
Bundle args = message.getData();
|
Bundle args = message.getData();
|
||||||
Uri trace = args.getParcelable(TRACE_URI);
|
Uri trace = args.getParcelable(TRACE_URI);
|
||||||
if (trace == null) main.postDelayed(() -> finish(reply, error("Timed out")), TIMEOUT_MS);
|
runningTrace = trace != null;
|
||||||
|
if (!runningTrace) main.postDelayed(() -> finish(reply, error("Timed out")), TIMEOUT_MS);
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
String result;
|
String result;
|
||||||
Surface surface = args.getParcelable(SURFACE);
|
Surface surface = args.getParcelable(SURFACE);
|
||||||
@@ -76,7 +86,7 @@ public final class TestService extends Service {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String runSample(Bundle args, Surface surface) throws Exception {
|
private String runSample(Bundle args, Surface surface) throws Exception {
|
||||||
String sample = args.getString(SAMPLE);
|
String sample = args.getString(SAMPLE);
|
||||||
if (sample == null || sample.isEmpty()) throw new IllegalArgumentException("Select a sample");
|
if (sample == null || sample.isEmpty()) throw new IllegalArgumentException("Select a sample");
|
||||||
Bundle overrides = args.getBundle(ENVIRONMENT);
|
Bundle overrides = args.getBundle(ENVIRONMENT);
|
||||||
@@ -93,7 +103,7 @@ public final class TestService extends Service {
|
|||||||
}
|
}
|
||||||
System.loadLibrary("MobileGL");
|
System.loadLibrary("MobileGL");
|
||||||
if (surface == null || !surface.isValid()) throw new IllegalStateException("Surface unavailable");
|
if (surface == null || !surface.isValid()) throw new IllegalStateException("Surface unavailable");
|
||||||
return nativeRunSample(sample, surface, args.getBoolean(VSYNC, true));
|
return nativeRunSample(sample, surface, args.getBoolean(VSYNC, true), canceled);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String error(String detail) {
|
private static String error(String detail) {
|
||||||
|
|||||||
Reference in new issue
Block a user