From d9273da292c0616cdcfb54901df3f4e7198d85ef Mon Sep 17 00:00:00 2001 From: AlexandreK38 Date: Mon, 2 Sep 2024 14:23:11 +0200 Subject: [PATCH 01/10] Thread to ask render manually --- .../java/src/org/axmol/lib/AxmolActivity.java | 17 +--- .../java/src/org/axmol/lib/AxmolRenderer.java | 86 ++++++++++++------- 2 files changed, 60 insertions(+), 43 deletions(-) diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java b/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java index dac7c7d42a60..f3767b387090 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java @@ -73,7 +73,6 @@ public abstract class AxmolActivity extends Activity implements AxmolEngineListe private boolean hasFocus = false; private boolean showVirtualButton = false; private boolean paused = true; - private boolean rendererPaused = true; public AxmolGLSurfaceView getGLSurfaceView(){ return mGLSurfaceView; @@ -225,11 +224,7 @@ public void onWindowFocusChanged(boolean hasFocus) { private void resume() { this.hideVirtualButton(); AxmolEngine.onResume(); - if (rendererPaused) { - mGLSurfaceView.onResume(); - rendererPaused = false; - } - mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); + mGLSurfaceView.onResume(); } private void resumeIfHasFocus() { @@ -248,13 +243,6 @@ protected void onPause() { paused = true; super.onPause(); AxmolEngine.onPause(); - mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); - } - - @Override - protected void onStop() { - super.onStop(); - rendererPaused = true; mGLSurfaceView.onPause(); } @@ -324,7 +312,8 @@ public void init() { //if (isAndroidEmulator()) // this.mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0); - this.mGLSurfaceView.setRenderer(new AxmolRenderer()); + this.mGLSurfaceView.setRenderer(new AxmolRenderer(this.mGLSurfaceView)); + this.mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); this.mGLSurfaceView.setEditText(edittext); // Set framelayout as the content view diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java index 5b1c900306a1..7f8e2bab9603 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java @@ -29,12 +29,43 @@ of this software and associated documentation files (the "Software"), to deal import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; public class AxmolRenderer implements GLSurfaceView.Renderer { + + private static class RendererThread extends Thread { + + private GLSurfaceView gameSurfaceView; + private AxmolRenderer renderer; + + public RendererThread(GLSurfaceView gameSurfaceView, AxmolRenderer renderer){ + this.gameSurfaceView = gameSurfaceView; + this.renderer = renderer; + } + + public void onPause(){ + pausing = true; + } + + public void onResume(){ + pausing = false; + } + + @Override + public void run(){ + + while (!Thread.interrupted()){ + + if(renderer.isReadyForRendering()){ + renderer.prepareForRendering(); + this.gameSurfaceView.requestRender(); + } + } + } + } + // =========================================================== // Constants // =========================================================== private final static long NANOSECONDSPERSECOND = 1000000000L; - private final static long NANOSECONDSPERMICROSECOND = 1000000L; // The final animation interval which is used in 'onDrawFrame' private static long sAnimationInterval = (long) (1.0f / 60f * AxmolRenderer.NANOSECONDSPERSECOND); @@ -47,12 +78,19 @@ public class AxmolRenderer implements GLSurfaceView.Renderer { private int mScreenWidth; private int mScreenHeight; private boolean mNativeInitCompleted = false; - private boolean mIsPaused = false; + private boolean isReady = true; + private static RendererThread rendererThread = null; // =========================================================== // Constructors // =========================================================== + public AxmolRenderer(GLSurfaceView surfaceView){ + + super(); + this.rendererThread = new RendererThread(surfaceView, this); + } + // =========================================================== // Getter & Setter // =========================================================== @@ -66,6 +104,15 @@ public void setScreenWidthAndHeight(final int surfaceWidth, final int surfaceHei this.mScreenHeight = surfaceHeight; } + public boolean isReadyForRendering(){ + return isReady && System.nanoTime() - mLastTickInNanoSeconds >= AxmolRenderer.sAnimationInterval; + } + + public void prepareForRendering(){ + isReady = false; + } + + // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @@ -75,6 +122,8 @@ public void onSurfaceCreated(final GL10 GL10, final EGLConfig EGLConfig) { AxmolRenderer.nativeInit(this.mScreenWidth, this.mScreenHeight); this.mLastTickInNanoSeconds = System.nanoTime(); + rendererThread.start(); + if (mNativeInitCompleted) { // This must be from an OpenGL context loss nativeOnContextLost(); @@ -90,28 +139,9 @@ public void onSurfaceChanged(final GL10 GL10, final int width, final int height) @Override public void onDrawFrame(final GL10 gl) { - /* - * Fix 60fps limiting doesn't work when high-end device is working in 120fps mode. - */ - if (AxmolRenderer.sAnimationInterval <= 1.0f / 1200.0f * AxmolRenderer.NANOSECONDSPERSECOND) { - AxmolRenderer.nativeRender(); - } else { - final long now = System.nanoTime(); - final long interval = now - this.mLastTickInNanoSeconds; - - /* - * Render time MUST be counted in, or the FPS will slower than appointed. - */ - this.mLastTickInNanoSeconds = now; - AxmolRenderer.nativeRender(); - - if (interval < AxmolRenderer.sAnimationInterval) { - try { - Thread.sleep((AxmolRenderer.sAnimationInterval - interval) / AxmolRenderer.NANOSECONDSPERMICROSECOND); - } catch (final Exception e) { - } - } - } + mLastTickInNanoSeconds = System.nanoTime(); + AxmolRenderer.nativeRender(); + isReady = true; } // =========================================================== @@ -164,15 +194,13 @@ public void handleOnPause() { if (!mNativeInitCompleted) return; + rendererThread.onPause(); AxmolRenderer.nativeOnPause(); - mIsPaused = true; } public void handleOnResume() { - if (mIsPaused) { - AxmolRenderer.nativeOnResume(); - mIsPaused = false; - } + rendererThread.onResume(); + AxmolRenderer.nativeOnResume(); } private static native void nativeInsertText(final String text); From f147907bd81865020cddb7736111d88b81982c83 Mon Sep 17 00:00:00 2001 From: AlexandreK38 Date: Mon, 2 Sep 2024 14:49:49 +0200 Subject: [PATCH 02/10] missing declaration --- core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java index 7f8e2bab9603..c50e1c098b95 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java @@ -34,6 +34,7 @@ private static class RendererThread extends Thread { private GLSurfaceView gameSurfaceView; private AxmolRenderer renderer; + private boolean pausing = false; public RendererThread(GLSurfaceView gameSurfaceView, AxmolRenderer renderer){ this.gameSurfaceView = gameSurfaceView; From 998ad7b7a381b6310488775055d999676c3db4fa Mon Sep 17 00:00:00 2001 From: AlexandreK38 Date: Tue, 3 Sep 2024 15:16:47 +0200 Subject: [PATCH 03/10] stop thread on stop --- .../java/src/org/axmol/lib/AxmolActivity.java | 11 ++++- .../java/src/org/axmol/lib/AxmolRenderer.java | 43 ++++++++++++------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java b/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java index f3767b387090..e6955ad61df4 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java @@ -224,7 +224,10 @@ public void onWindowFocusChanged(boolean hasFocus) { private void resume() { this.hideVirtualButton(); AxmolEngine.onResume(); - mGLSurfaceView.onResume(); + if (rendererPaused) { + mGLSurfaceView.onResume(); + rendererPaused = false; + } } private void resumeIfHasFocus() { @@ -243,6 +246,12 @@ protected void onPause() { paused = true; super.onPause(); AxmolEngine.onPause(); + } + + @Override + protected void onStop() { + super.onStop(); + rendererPaused = true; mGLSurfaceView.onPause(); } diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java index c50e1c098b95..ca0e57df09d0 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java @@ -35,8 +35,9 @@ private static class RendererThread extends Thread { private GLSurfaceView gameSurfaceView; private AxmolRenderer renderer; private boolean pausing = false; + private Object monitor = new Object(); - public RendererThread(GLSurfaceView gameSurfaceView, AxmolRenderer renderer){ + public RendererThread(GLSurfaceView gameSurfaceView, AxmolRenderer renderer) { this.gameSurfaceView = gameSurfaceView; this.renderer = renderer; } @@ -47,14 +48,25 @@ public void onPause(){ public void onResume(){ pausing = false; + synchronized (monitor) { + monitor.notify(); + } } @Override - public void run(){ - - while (!Thread.interrupted()){ + public void run() { + while (true) { + synchronized (monitor) { + if(pausing) { + try { + monitor.wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } - if(renderer.isReadyForRendering()){ + if (renderer.isReadyForRendering()) { renderer.prepareForRendering(); this.gameSurfaceView.requestRender(); } @@ -79,17 +91,16 @@ public void run(){ private int mScreenWidth; private int mScreenHeight; private boolean mNativeInitCompleted = false; - private boolean isReady = true; - private static RendererThread rendererThread = null; + private boolean mIsReady = true; + private RendererThread mRendererThread = null; // =========================================================== // Constructors // =========================================================== - public AxmolRenderer(GLSurfaceView surfaceView){ - + public AxmolRenderer(GLSurfaceView surfaceView) { super(); - this.rendererThread = new RendererThread(surfaceView, this); + this.mRendererThread = new RendererThread(surfaceView, this); } // =========================================================== @@ -106,11 +117,11 @@ public void setScreenWidthAndHeight(final int surfaceWidth, final int surfaceHei } public boolean isReadyForRendering(){ - return isReady && System.nanoTime() - mLastTickInNanoSeconds >= AxmolRenderer.sAnimationInterval; + return mIsReady && System.nanoTime() - mLastTickInNanoSeconds >= AxmolRenderer.sAnimationInterval; } public void prepareForRendering(){ - isReady = false; + mIsReady = false; } @@ -123,7 +134,7 @@ public void onSurfaceCreated(final GL10 GL10, final EGLConfig EGLConfig) { AxmolRenderer.nativeInit(this.mScreenWidth, this.mScreenHeight); this.mLastTickInNanoSeconds = System.nanoTime(); - rendererThread.start(); + mRendererThread.start(); if (mNativeInitCompleted) { // This must be from an OpenGL context loss @@ -142,7 +153,7 @@ public void onSurfaceChanged(final GL10 GL10, final int width, final int height) public void onDrawFrame(final GL10 gl) { mLastTickInNanoSeconds = System.nanoTime(); AxmolRenderer.nativeRender(); - isReady = true; + mIsReady = true; } // =========================================================== @@ -195,12 +206,12 @@ public void handleOnPause() { if (!mNativeInitCompleted) return; - rendererThread.onPause(); + mRendererThread.onPause(); AxmolRenderer.nativeOnPause(); } public void handleOnResume() { - rendererThread.onResume(); + mRendererThread.onResume(); AxmolRenderer.nativeOnResume(); } From 4dc5ae57c935f3ecb48632b8c38c3a51b2dc78b2 Mon Sep 17 00:00:00 2001 From: AlexandreK38 Date: Tue, 3 Sep 2024 15:22:57 +0200 Subject: [PATCH 04/10] missing declaration --- core/platform/android/java/src/org/axmol/lib/AxmolActivity.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java b/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java index e6955ad61df4..0d9171a174fb 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java @@ -73,6 +73,7 @@ public abstract class AxmolActivity extends Activity implements AxmolEngineListe private boolean hasFocus = false; private boolean showVirtualButton = false; private boolean paused = true; + private boolean rendererPaused = true; public AxmolGLSurfaceView getGLSurfaceView(){ return mGLSurfaceView; From 65f2294a9e16608d3d37796adabc7b2e3ade9f9f Mon Sep 17 00:00:00 2001 From: AlexandreK38 Date: Tue, 3 Sep 2024 15:35:53 +0200 Subject: [PATCH 05/10] use back mIsPaused in renderer --- .../android/java/src/org/axmol/lib/AxmolRenderer.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java index ca0e57df09d0..ed798c03bd78 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java @@ -91,6 +91,7 @@ public void run() { private int mScreenWidth; private int mScreenHeight; private boolean mNativeInitCompleted = false; + private boolean mIsPaused = false; private boolean mIsReady = true; private RendererThread mRendererThread = null; @@ -208,11 +209,15 @@ public void handleOnPause() { mRendererThread.onPause(); AxmolRenderer.nativeOnPause(); + mIsPaused = true; } public void handleOnResume() { mRendererThread.onResume(); - AxmolRenderer.nativeOnResume(); + if (mIsPaused) { + AxmolRenderer.nativeOnResume(); + mIsPaused = false; + } } private static native void nativeInsertText(final String text); From b71d2f1c00dfe67ec4b8f0abe4c077943298c673 Mon Sep 17 00:00:00 2001 From: AlexandreK38 Date: Tue, 3 Sep 2024 16:34:04 +0200 Subject: [PATCH 06/10] handle activity onPause to pause thread too --- .../src/org/axmol/lib/AxmolGLSurfaceView.java | 15 +++++++++++ .../java/src/org/axmol/lib/AxmolRenderer.java | 27 +++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolGLSurfaceView.java b/core/platform/android/java/src/org/axmol/lib/AxmolGLSurfaceView.java index d4d004673b6b..2cab7574e812 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolGLSurfaceView.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolGLSurfaceView.java @@ -419,6 +419,21 @@ public void run() { // Methods // =========================================================== + public void onLooseFocus() { + this.queueEvent(new Runnable() { + @Override + public void run() { + AxmolGLSurfaceView.this.mRenderer.handleOnLooseFocus(); + } + }); + } + public void onGainFocus() { + this.queueEvent(new Runnable() { + @Override + public void run() { AxmolGLSurfaceView.this.mRenderer.handleOnGainFocus(); } + }); + } + // =========================================================== // Inner and Anonymous Classes // =========================================================== diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java index ed798c03bd78..5be4463c2a2d 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java @@ -93,6 +93,7 @@ public void run() { private boolean mNativeInitCompleted = false; private boolean mIsPaused = false; private boolean mIsReady = true; + private boolean mRendererThreadIsPaused = false; private RendererThread mRendererThread = null; // =========================================================== @@ -207,19 +208,41 @@ public void handleOnPause() { if (!mNativeInitCompleted) return; - mRendererThread.onPause(); + pauseRendererThread(); AxmolRenderer.nativeOnPause(); mIsPaused = true; } public void handleOnResume() { - mRendererThread.onResume(); + resumeRendererThread(); if (mIsPaused) { AxmolRenderer.nativeOnResume(); mIsPaused = false; } } + public void handleOnLooseFocus() { + pauseRendererThread(); + } + + public void handleOnGainFocus() { + resumeRendererThread(); + } + + private void pauseRendererThread() { + if(!mRendererThreadIsPaused) { + mRendererThread.onPause(); + mRendererThreadIsPaused = true; + } + } + + private void resumeRendererThread() { + if(mRendererThreadIsPaused) { + mRendererThread.onResume(); + mRendererThreadIsPaused = false; + } + } + private static native void nativeInsertText(final String text); private static native void nativeDeleteBackward(); private static native String nativeGetContentText(); From 146a73d4bdd78fece5a04f962aab5aea6a83637f Mon Sep 17 00:00:00 2001 From: AlexandreK38 Date: Tue, 3 Sep 2024 17:15:11 +0200 Subject: [PATCH 07/10] handle activity onPause to pause thread --- .../java/src/org/axmol/lib/AxmolActivity.java | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java b/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java index 0d9171a174fb..14cef02596ed 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java @@ -229,15 +229,8 @@ private void resume() { mGLSurfaceView.onResume(); rendererPaused = false; } - } - - private void resumeIfHasFocus() { - //It is possible for the app to receive the onWindowsFocusChanged(true) event - //even though it is locked or asleep - boolean readyToPlay = !isDeviceLocked() && !isDeviceAsleep(); - - if(hasFocus && readyToPlay) { - resume(); + else { + mGLSurfaceView.onGainFocus(); } } @@ -246,6 +239,7 @@ protected void onPause() { Log.d(TAG, "onPause()"); paused = true; super.onPause(); + mGLSurfaceView.onLooseFocus(); AxmolEngine.onPause(); } From 9b1df96e680457e2912c1e8727adc294349c68ba Mon Sep 17 00:00:00 2001 From: AlexandreK38 Date: Wed, 4 Sep 2024 17:43:04 +0200 Subject: [PATCH 08/10] thread sleep --- .../java/src/org/axmol/lib/AxmolActivity.java | 2 +- .../src/org/axmol/lib/AxmolGLSurfaceView.java | 5 +- .../java/src/org/axmol/lib/AxmolRenderer.java | 74 +++++++++---------- 3 files changed, 39 insertions(+), 42 deletions(-) diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java b/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java index 14cef02596ed..04b5a394893c 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolActivity.java @@ -239,7 +239,7 @@ protected void onPause() { Log.d(TAG, "onPause()"); paused = true; super.onPause(); - mGLSurfaceView.onLooseFocus(); + mGLSurfaceView.onLoseFocus(); AxmolEngine.onPause(); } diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolGLSurfaceView.java b/core/platform/android/java/src/org/axmol/lib/AxmolGLSurfaceView.java index 2cab7574e812..10890a5c9322 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolGLSurfaceView.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolGLSurfaceView.java @@ -419,14 +419,15 @@ public void run() { // Methods // =========================================================== - public void onLooseFocus() { + public void onLoseFocus() { this.queueEvent(new Runnable() { @Override public void run() { - AxmolGLSurfaceView.this.mRenderer.handleOnLooseFocus(); + AxmolGLSurfaceView.this.mRenderer.handleOnLoseFocus(); } }); } + public void onGainFocus() { this.queueEvent(new Runnable() { @Override diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java index 5be4463c2a2d..beabe6fd5b9a 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java @@ -35,7 +35,8 @@ private static class RendererThread extends Thread { private GLSurfaceView gameSurfaceView; private AxmolRenderer renderer; private boolean pausing = false; - private Object monitor = new Object(); + private Object drawMonitor = new Object(); + private Object pauseMonitor = new Object(); public RendererThread(GLSurfaceView gameSurfaceView, AxmolRenderer renderer) { this.gameSurfaceView = gameSurfaceView; @@ -47,28 +48,42 @@ public void onPause(){ } public void onResume(){ - pausing = false; - synchronized (monitor) { - monitor.notify(); + if (pausing) { + pausing = false; + synchronized (pauseMonitor) { + pauseMonitor.notify(); + } + } + } + + public void drawFrameDone() { + synchronized (drawMonitor) { + drawMonitor.notify(); } } @Override public void run() { + while (true) { - synchronized (monitor) { - if(pausing) { - try { - monitor.wait(); - } catch (InterruptedException e) { - throw new RuntimeException(e); + try { + synchronized (pauseMonitor) { + if (pausing) { + pauseMonitor.wait(); } } - } - if (renderer.isReadyForRendering()) { - renderer.prepareForRendering(); + long now = System.nanoTime(); this.gameSurfaceView.requestRender(); + synchronized (drawMonitor) { + drawMonitor.wait(); + } + long wait = (AxmolRenderer.sAnimationInterval - (System.nanoTime()-now)) / AxmolRenderer.NANOSECONDSPERMICROSECOND; + if (wait > 0) { + sleep(wait); + } + } catch (InterruptedException e) { + throw new RuntimeException(e); } } } @@ -79,6 +94,7 @@ public void run() { // =========================================================== private final static long NANOSECONDSPERSECOND = 1000000000L; + private final static long NANOSECONDSPERMICROSECOND = 1000000L; // The final animation interval which is used in 'onDrawFrame' private static long sAnimationInterval = (long) (1.0f / 60f * AxmolRenderer.NANOSECONDSPERSECOND); @@ -87,20 +103,18 @@ public void run() { // Fields // =========================================================== - private long mLastTickInNanoSeconds; private int mScreenWidth; private int mScreenHeight; private boolean mNativeInitCompleted = false; private boolean mIsPaused = false; - private boolean mIsReady = true; - private boolean mRendererThreadIsPaused = false; private RendererThread mRendererThread = null; // =========================================================== // Constructors // =========================================================== - public AxmolRenderer(GLSurfaceView surfaceView) { + public AxmolRenderer(GLSurfaceView surfaceView){ + super(); this.mRendererThread = new RendererThread(surfaceView, this); } @@ -118,15 +132,6 @@ public void setScreenWidthAndHeight(final int surfaceWidth, final int surfaceHei this.mScreenHeight = surfaceHeight; } - public boolean isReadyForRendering(){ - return mIsReady && System.nanoTime() - mLastTickInNanoSeconds >= AxmolRenderer.sAnimationInterval; - } - - public void prepareForRendering(){ - mIsReady = false; - } - - // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @@ -134,8 +139,6 @@ public void prepareForRendering(){ @Override public void onSurfaceCreated(final GL10 GL10, final EGLConfig EGLConfig) { AxmolRenderer.nativeInit(this.mScreenWidth, this.mScreenHeight); - this.mLastTickInNanoSeconds = System.nanoTime(); - mRendererThread.start(); if (mNativeInitCompleted) { @@ -153,9 +156,8 @@ public void onSurfaceChanged(final GL10 GL10, final int width, final int height) @Override public void onDrawFrame(final GL10 gl) { - mLastTickInNanoSeconds = System.nanoTime(); AxmolRenderer.nativeRender(); - mIsReady = true; + mRendererThread.drawFrameDone(); } // =========================================================== @@ -221,7 +223,7 @@ public void handleOnResume() { } } - public void handleOnLooseFocus() { + public void handleOnLoseFocus() { pauseRendererThread(); } @@ -230,17 +232,11 @@ public void handleOnGainFocus() { } private void pauseRendererThread() { - if(!mRendererThreadIsPaused) { - mRendererThread.onPause(); - mRendererThreadIsPaused = true; - } + mRendererThread.onPause(); } private void resumeRendererThread() { - if(mRendererThreadIsPaused) { - mRendererThread.onResume(); - mRendererThreadIsPaused = false; - } + mRendererThread.onResume(); } private static native void nativeInsertText(final String text); From c592348e5e85160121a4646299593524548bf3e0 Mon Sep 17 00:00:00 2001 From: AlexandreK38 Date: Thu, 5 Sep 2024 12:32:09 +0200 Subject: [PATCH 09/10] fix thread deadlock --- core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java index beabe6fd5b9a..580404374497 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java @@ -74,8 +74,8 @@ public void run() { } long now = System.nanoTime(); - this.gameSurfaceView.requestRender(); synchronized (drawMonitor) { + this.gameSurfaceView.requestRender(); drawMonitor.wait(); } long wait = (AxmolRenderer.sAnimationInterval - (System.nanoTime()-now)) / AxmolRenderer.NANOSECONDSPERMICROSECOND; From 098d34473aac9658591c5f15fe5f5ebc791ae92f Mon Sep 17 00:00:00 2001 From: AlexandreK38 Date: Wed, 25 Sep 2024 17:14:50 +0200 Subject: [PATCH 10/10] fix start thread --- .../android/java/src/org/axmol/lib/AxmolRenderer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java index 0e07c0c5091d..6715ee974c3e 100644 --- a/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java +++ b/core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java @@ -140,13 +140,13 @@ public void setScreenWidthAndHeight(final int surfaceWidth, final int surfaceHei @Override public void onSurfaceCreated(final GL10 GL10, final EGLConfig EGLConfig) { AxmolRenderer.nativeInit(this.mScreenWidth, this.mScreenHeight); - mRendererThread.start(); - + if (mNativeInitCompleted) { // This must be from an OpenGL context loss nativeOnContextLost(); } else { mNativeInitCompleted = true; + mRendererThread.start(); } }