From bc7e703e2387aa9f03be905f478aecdee84326b3 Mon Sep 17 00:00:00 2001 From: BZLZHH Date: Sun, 29 Nov 2026 11:28:16 +0800 Subject: [PATCH] [Fix] (MobileGL, android-plugin/app): release the test surface before restarting a canceled run --- MobileGL/AndroidTestScenes.cpp | 29 +++++-- .../top/mobilegl/plugin/TestActivity.java | 81 ++++++++++++------- .../java/top/mobilegl/plugin/TestService.java | 20 +++-- 3 files changed, 91 insertions(+), 39 deletions(-) diff --git a/MobileGL/AndroidTestScenes.cpp b/MobileGL/AndroidTestScenes.cpp index 58427fdc..336eef71 100644 --- a/MobileGL/AndroidTestScenes.cpp +++ b/MobileGL/AndroidTestScenes.cpp @@ -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; egl.Open(window, vsync); const auto* renderer = reinterpret_cast(Gl::GetString(GL_RENDERER)); @@ -310,13 +319,16 @@ namespace { 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(); + } std::vector frameTimes; const auto sampleStart = Clock::now(); auto end = sampleStart; do { + if (cancellation.Requested()) return Error("Canceled"); const auto begin = end; draw(); end = Clock::now(); @@ -349,10 +361,13 @@ extern "C" JNIEXPORT jstring JNICALL Java_top_mobilegl_plugin_TestActivity_nativ return env->NewStringUTF(catalog.c_str()); } -extern "C" JNIEXPORT jstring JNICALL Java_top_mobilegl_plugin_TestService_nativeRunSample(JNIEnv* env, jclass, - jstring sample, - jobject surface, - jboolean vsync) { +extern "C" JNIEXPORT jstring JNICALL Java_top_mobilegl_plugin_TestService_nativeRunSample( + JNIEnv* env, jclass, jstring sample, jobject surface, jboolean vsync, jobject canceled) { + const auto flagType = env->GetObjectClass(canceled); + const auto get = env->GetMethodID(flagType, "get", "()Z"); + env->DeleteLocalRef(flagType); + if (!get) return nullptr; + const SceneCancellation cancellation{env, canceled, get}; std::unique_ptr window( surface ? ANativeWindow_fromSurface(env, surface) : nullptr, ANativeWindow_release); if (!window) return env->NewStringUTF("{\"error\":\"Surface unavailable\"}"); @@ -365,7 +380,7 @@ extern "C" JNIEXPORT jstring JNICALL Java_top_mobilegl_plugin_TestService_native try { for (const auto& scene : kScenes) { if (selected == scene.title) { - result = RunScene(scene, window.get(), vsync == JNI_TRUE); + result = RunScene(scene, window.get(), vsync == JNI_TRUE, cancellation); break; } } diff --git a/android-plugin/app/src/main/java/top/mobilegl/plugin/TestActivity.java b/android-plugin/app/src/main/java/top/mobilegl/plugin/TestActivity.java index f420db6b..c1a965fc 100644 --- a/android-plugin/app/src/main/java/top/mobilegl/plugin/TestActivity.java +++ b/android-plugin/app/src/main/java/top/mobilegl/plugin/TestActivity.java @@ -50,7 +50,9 @@ public final class TestActivity extends Activity { private Button run; private Button replayTrace; private Switch vsync; + private FrameLayout previewFrame; private SurfaceView preview; + private boolean previewReady; private ImageView snapshot; private String snapshotPath; private static final int FRAME_WIDTH = 1280; @@ -105,33 +107,17 @@ public final class TestActivity extends Activity { else if (active.isTrace()) active.cancel(); }); content.addView(replayTrace); - FrameLayout frame = new FrameLayout(this); - content.addView(frame); - 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"); - frame.addView(preview, new FrameLayout.LayoutParams( - FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT)); + previewFrame = new FrameLayout(this); + content.addView(previewFrame); + replacePreview(); snapshot = new ImageView(this); snapshot.setScaleType(ImageView.ScaleType.FIT_CENTER); snapshot.setBackgroundColor(0xFF121212); snapshot.setContentDescription("Trace snapshot"); - frame.addView(snapshot, new FrameLayout.LayoutParams( + previewFrame.addView(snapshot, new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); 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.setTextIsSelectable(true); content.addView(report); @@ -151,7 +137,42 @@ public final class TestActivity extends Activity { } catch (LinkageError | JSONException failure) { 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 @@ -195,7 +216,7 @@ public final class TestActivity extends Activity { private void startRun() { Surface surface = preview.getHolder().getSurface(); - if (!surface.isValid()) { + if (!previewReady || !surface.isValid()) { showResult("Surface unavailable"); return; } @@ -225,7 +246,7 @@ public final class TestActivity extends Activity { vsync.setEnabled(!running); boolean replaying = running && active.isTrace(); 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.setEnabled(!running || replaying); } @@ -290,8 +311,9 @@ public final class TestActivity extends Activity { } 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; + if (cancelled) return true; String json = message.getData().getString(TestService.REPORT, ""); if (isTrace()) showTraceReport(json); else showResult(formatReport(name, json)); @@ -302,7 +324,11 @@ public final class TestActivity extends Activity { @Override public void onServiceDisconnected(ComponentName component) { 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(); } @@ -317,6 +343,7 @@ public final class TestActivity extends Activity { } void cancel() { + if (cancelled) return; cancelled = true; showResult("Canceled"); if (worker == null) { @@ -327,7 +354,7 @@ public final class TestActivity extends Activity { try { worker.send(Message.obtain(null, TestService.CANCEL)); } catch (RemoteException ignored) { - complete(); + onServiceDisconnected(null); } } diff --git a/android-plugin/app/src/main/java/top/mobilegl/plugin/TestService.java b/android-plugin/app/src/main/java/top/mobilegl/plugin/TestService.java index 70e0db79..52ade87d 100644 --- a/android-plugin/app/src/main/java/top/mobilegl/plugin/TestService.java +++ b/android-plugin/app/src/main/java/top/mobilegl/plugin/TestService.java @@ -14,6 +14,8 @@ import android.os.RemoteException; import android.system.Os; import android.view.Surface; +import java.util.concurrent.atomic.AtomicBoolean; + import org.json.JSONObject; public final class TestService extends Service { @@ -27,14 +29,17 @@ public final class TestService extends Service { static final String SURFACE = "surface"; static final String VSYNC = "vsync"; 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"; - 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 Messenger requests = new Messenger(new Handler(Looper.getMainLooper(), this::onRequest)); + private final AtomicBoolean canceled = new AtomicBoolean(); private boolean started; private boolean finished; + private boolean runningTrace; @Override public IBinder onBind(Intent intent) { @@ -49,7 +54,11 @@ public final class TestService extends Service { private boolean onRequest(Message message) { 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; } if (message.what != RUN || started || message.replyTo == null) return false; @@ -57,7 +66,8 @@ public final class TestService extends Service { Messenger reply = message.replyTo; Bundle args = message.getData(); 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(() -> { String result; Surface surface = args.getParcelable(SURFACE); @@ -76,7 +86,7 @@ public final class TestService extends Service { 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); if (sample == null || sample.isEmpty()) throw new IllegalArgumentException("Select a sample"); Bundle overrides = args.getBundle(ENVIRONMENT); @@ -93,7 +103,7 @@ public final class TestService extends Service { } System.loadLibrary("MobileGL"); 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) {