Update the logic used to start / stop the render thread

Currently the render thread is started / stopped when the activity is respectively resumed / paused. However, according to the `GLSurfaceView` documentation, this should be done instead when the activity is started / stopped, so this change updates the start / stop logic for the render thread to match the documentation.
This commit is contained in:
Fredia Huya-Kouadio 2023-12-20 20:37:16 -08:00
parent 3a8524dd92
commit f537cdefcf
7 changed files with 98 additions and 57 deletions

View file

@ -484,6 +484,14 @@ class Godot(private val context: Context) : SensorEventListener {
return containerLayout return containerLayout
} }
fun onStart(host: GodotHost) {
if (host != primaryHost) {
return
}
renderView!!.onActivityStarted()
}
fun onResume(host: GodotHost) { fun onResume(host: GodotHost) {
if (host != primaryHost) { if (host != primaryHost) {
return return
@ -528,6 +536,14 @@ class Godot(private val context: Context) : SensorEventListener {
} }
} }
fun onStop(host: GodotHost) {
if (host != primaryHost) {
return
}
renderView!!.onActivityStopped()
}
fun onDestroy(primaryHost: GodotHost) { fun onDestroy(primaryHost: GodotHost) {
if (this.primaryHost != primaryHost) { if (this.primaryHost != primaryHost) {
return return

View file

@ -270,6 +270,32 @@ public class GodotFragment extends Fragment implements IDownloaderClient, GodotH
godot.onPause(this); godot.onPause(this);
} }
@Override
public void onStop() {
super.onStop();
if (!godot.isInitialized()) {
if (null != mDownloaderClientStub) {
mDownloaderClientStub.disconnect(getActivity());
}
return;
}
godot.onStop(this);
}
@Override
public void onStart() {
super.onStart();
if (!godot.isInitialized()) {
if (null != mDownloaderClientStub) {
mDownloaderClientStub.connect(getActivity());
}
return;
}
godot.onStart(this);
}
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();

View file

@ -114,12 +114,30 @@ public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView
@Override @Override
public void onActivityPaused() { public void onActivityPaused() {
onPause(); queueEvent(() -> {
GodotLib.focusout();
// Pause the renderer
godotRenderer.onActivityPaused();
});
}
@Override
public void onActivityStopped() {
pauseGLThread();
} }
@Override @Override
public void onActivityResumed() { public void onActivityResumed() {
onResume(); queueEvent(() -> {
// Resume the renderer
godotRenderer.onActivityResumed();
GodotLib.focusin();
});
}
@Override
public void onActivityStarted() {
resumeGLThread();
} }
@Override @Override
@ -283,26 +301,4 @@ public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView
/* Set the renderer responsible for frame rendering */ /* Set the renderer responsible for frame rendering */
setRenderer(godotRenderer); setRenderer(godotRenderer);
} }
@Override
public void onResume() {
super.onResume();
queueEvent(() -> {
// Resume the renderer
godotRenderer.onActivityResumed();
GodotLib.focusin();
});
}
@Override
public void onPause() {
super.onPause();
queueEvent(() -> {
GodotLib.focusout();
// Pause the renderer
godotRenderer.onActivityPaused();
});
}
} }

View file

@ -47,8 +47,13 @@ public interface GodotRenderView {
void queueOnRenderThread(Runnable event); void queueOnRenderThread(Runnable event);
void onActivityPaused(); void onActivityPaused();
void onActivityStopped();
void onActivityResumed(); void onActivityResumed();
void onActivityStarted();
void onBackPressed(); void onBackPressed();
GodotInputHandler getInputHandler(); GodotInputHandler getInputHandler();

View file

@ -92,12 +92,30 @@ public class GodotVulkanRenderView extends VkSurfaceView implements GodotRenderV
@Override @Override
public void onActivityPaused() { public void onActivityPaused() {
onPause(); queueOnVkThread(() -> {
GodotLib.focusout();
// Pause the renderer
mRenderer.onVkPause();
});
}
@Override
public void onActivityStopped() {
pauseRenderThread();
}
@Override
public void onActivityStarted() {
resumeRenderThread();
} }
@Override @Override
public void onActivityResumed() { public void onActivityResumed() {
onResume(); queueOnVkThread(() -> {
// Resume the renderer
mRenderer.onVkResume();
GodotLib.focusin();
});
} }
@Override @Override
@ -211,26 +229,4 @@ public class GodotVulkanRenderView extends VkSurfaceView implements GodotRenderV
} }
return super.onResolvePointerIcon(me, pointerIndex); return super.onResolvePointerIcon(me, pointerIndex);
} }
@Override
public void onResume() {
super.onResume();
queueOnVkThread(() -> {
// Resume the renderer
mRenderer.onVkResume();
GodotLib.focusin();
});
}
@Override
public void onPause() {
super.onPause();
queueOnVkThread(() -> {
GodotLib.focusout();
// Pause the renderer
mRenderer.onVkPause();
});
}
} }

View file

@ -122,8 +122,8 @@ import javax.microedition.khronos.opengles.GL10;
* <p> * <p>
* <h3>Activity Life-cycle</h3> * <h3>Activity Life-cycle</h3>
* A GLSurfaceView must be notified when to pause and resume rendering. GLSurfaceView clients * A GLSurfaceView must be notified when to pause and resume rendering. GLSurfaceView clients
* are required to call {@link #onPause()} when the activity stops and * are required to call {@link #pauseGLThread()} when the activity stops and
* {@link #onResume()} when the activity starts. These calls allow GLSurfaceView to * {@link #resumeGLThread()} when the activity starts. These calls allow GLSurfaceView to
* pause and resume the rendering thread, and also allow GLSurfaceView to release and recreate * pause and resume the rendering thread, and also allow GLSurfaceView to release and recreate
* the OpenGL display. * the OpenGL display.
* <p> * <p>
@ -339,8 +339,8 @@ public class GLSurfaceView extends SurfaceView implements SurfaceHolder.Callback
* setRenderer is called: * setRenderer is called:
* <ul> * <ul>
* <li>{@link #getRenderMode()} * <li>{@link #getRenderMode()}
* <li>{@link #onPause()} * <li>{@link #pauseGLThread()}
* <li>{@link #onResume()} * <li>{@link #resumeGLThread()}
* <li>{@link #queueEvent(Runnable)} * <li>{@link #queueEvent(Runnable)}
* <li>{@link #requestRender()} * <li>{@link #requestRender()}
* <li>{@link #setRenderMode(int)} * <li>{@link #setRenderMode(int)}
@ -568,6 +568,7 @@ public class GLSurfaceView extends SurfaceView implements SurfaceHolder.Callback
} }
// -- GODOT start --
/** /**
* Pause the rendering thread, optionally tearing down the EGL context * Pause the rendering thread, optionally tearing down the EGL context
* depending upon the value of {@link #setPreserveEGLContextOnPause(boolean)}. * depending upon the value of {@link #setPreserveEGLContextOnPause(boolean)}.
@ -578,22 +579,23 @@ public class GLSurfaceView extends SurfaceView implements SurfaceHolder.Callback
* *
* Must not be called before a renderer has been set. * Must not be called before a renderer has been set.
*/ */
public void onPause() { protected final void pauseGLThread() {
mGLThread.onPause(); mGLThread.onPause();
} }
/** /**
* Resumes the rendering thread, re-creating the OpenGL context if necessary. It * Resumes the rendering thread, re-creating the OpenGL context if necessary. It
* is the counterpart to {@link #onPause()}. * is the counterpart to {@link #pauseGLThread()}.
* *
* This method should typically be called in * This method should typically be called in
* {@link android.app.Activity#onStart Activity.onStart}. * {@link android.app.Activity#onStart Activity.onStart}.
* *
* Must not be called before a renderer has been set. * Must not be called before a renderer has been set.
*/ */
public void onResume() { protected final void resumeGLThread() {
mGLThread.onResume(); mGLThread.onResume();
} }
// -- GODOT end --
/** /**
* Queue a runnable to be run on the GL rendering thread. This can be used * Queue a runnable to be run on the GL rendering thread. This can be used

View file

@ -99,7 +99,7 @@ open internal class VkSurfaceView(context: Context) : SurfaceView(context), Surf
* *
* Must not be called before a [VkRenderer] has been set. * Must not be called before a [VkRenderer] has been set.
*/ */
open fun onResume() { protected fun resumeRenderThread() {
vkThread.onResume() vkThread.onResume()
} }
@ -108,7 +108,7 @@ open internal class VkSurfaceView(context: Context) : SurfaceView(context), Surf
* *
* Must not be called before a [VkRenderer] has been set. * Must not be called before a [VkRenderer] has been set.
*/ */
open fun onPause() { protected fun pauseRenderThread() {
vkThread.onPause() vkThread.onPause()
} }