[DEV] update periodic call medel

This commit is contained in:
Edouard DUPIN 2021-05-31 21:55:07 +02:00
parent 2b0c623ad4
commit 86e799bdfa
2 changed files with 50 additions and 41 deletions

View File

@ -1,10 +1,12 @@
package org.atriasoft.gale; package org.atriasoft.gale;
import java.time.Clock;
import org.atriasoft.etk.Uri; import org.atriasoft.etk.Uri;
import org.atriasoft.etk.math.Vector2f; import org.atriasoft.etk.math.Vector2f;
import org.atriasoft.gale.context.ClipboardList; import org.atriasoft.gale.context.ClipboardList;
import org.atriasoft.gale.context.GaleContext;
import org.atriasoft.gale.context.Cursor; import org.atriasoft.gale.context.Cursor;
import org.atriasoft.gale.context.GaleContext;
import org.atriasoft.gale.internal.Log; import org.atriasoft.gale.internal.Log;
import org.atriasoft.gale.key.KeyKeyboard; import org.atriasoft.gale.key.KeyKeyboard;
import org.atriasoft.gale.key.KeySpecial; import org.atriasoft.gale.key.KeySpecial;
@ -185,10 +187,11 @@ public class GaleApplication {
} }
/** /**
* Call every time a draw is called (not entirely periodic, but faster at we can ... * Call when contrext finish process event in the buffer and add a latency of 10ms between calls
* @param time Current time of the call; * @param clock Current time of the call;
* @param time time of the program in nanoseconds (monotonic) ==> need restart application approximately every 292 years
*/ */
public void onPeriod(final long time) {} public void onPeriod(final Clock clock, final long time) {}
/** /**
* Get touch/mouse/... event. * Get touch/mouse/... event.
@ -313,5 +316,5 @@ public class GaleApplication {
return; return;
} }
context.setTitle(this.title); context.setTitle(this.title);
}; }
} }

View File

@ -1,5 +1,6 @@
package org.atriasoft.gale.context; package org.atriasoft.gale.context;
import java.time.Clock;
import java.util.Vector; import java.util.Vector;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
@ -26,7 +27,7 @@ interface ActionToDoInAsyncLoop {
enum ApplicationState { enum ApplicationState {
UNDEFINED, CREATE, RUNNING, DIED UNDEFINED, CREATE, RUNNING, DIED
}; }
public abstract class GaleContext { public abstract class GaleContext {
protected static final int MAX_MANAGE_INPUT = 15; protected static final int MAX_MANAGE_INPUT = 15;
@ -40,16 +41,16 @@ public abstract class GaleContext {
* @note For test create a ``` new GaleContextTest()``` ... this permit to run some test... * @note For test create a ``` new GaleContextTest()``` ... this permit to run some test...
*/ */
public static GaleContext getContext() { public static GaleContext getContext() {
return globalContext; return GaleContext.globalContext;
} }
public static void setContext(final GaleContext context) { public static void setContext(final GaleContext context) {
globalContext = context; GaleContext.globalContext = context;
} }
Lock lock = new ReentrantLock(); Lock lock = new ReentrantLock();
protected ThreadAbstract periodicThread;; protected ThreadAbstract periodicThread;
protected GaleApplication application; //!< Application handle protected GaleApplication application; //!< Application handle
protected ApplicationState applicationState = ApplicationState.UNDEFINED; // state of the application protected ApplicationState applicationState = ApplicationState.UNDEFINED; // state of the application
private final CommandLine commandLine = new CommandLine(); //!< Start command line information; private final CommandLine commandLine = new CommandLine(); //!< Start command line information;
@ -70,7 +71,7 @@ public abstract class GaleContext {
// set a basic // set a basic
this.application = application; this.application = application;
this.applicationState = ApplicationState.CREATE; this.applicationState = ApplicationState.CREATE;
setContext(this); GaleContext.setContext(this);
Thread.currentThread().setName("galeThread"); Thread.currentThread().setName("galeThread");
if (this.application == null) { if (this.application == null) {
Log.critical("Can not start context with no Application ==> rtfm ..."); Log.critical("Can not start context with no Application ==> rtfm ...");
@ -113,7 +114,7 @@ public abstract class GaleContext {
Log.info("GALE v:" + Gale.getVersion()); Log.info("GALE v:" + Gale.getVersion());
forceOrientation(Orientation.screenAuto); forceOrientation(Orientation.screenAuto);
postAction((context) -> { postAction(context -> {
final GaleApplication appl = context.getApplication(); final GaleApplication appl = context.getApplication();
if (appl == null) { if (appl == null) {
this.applicationState = ApplicationState.UNDEFINED; this.applicationState = ApplicationState.UNDEFINED;
@ -275,7 +276,7 @@ public abstract class GaleContext {
* @param clipboardID of the clipboard * @param clipboardID of the clipboard
*/ */
public void operatingSystemClipBoardArrive(final ClipboardList clipboardID) { public void operatingSystemClipBoardArrive(final ClipboardList clipboardID) {
postAction((context) -> { postAction(context -> {
final GaleApplication appl = context.getApplication(); final GaleApplication appl = context.getApplication();
if (appl != null) { if (appl != null) {
appl.onClipboardEvent(clipboardID); appl.onClipboardEvent(clipboardID);
@ -284,12 +285,11 @@ public abstract class GaleContext {
} }
public boolean operatingSystemDraw(final boolean displayEveryTime) { public boolean operatingSystemDraw(final boolean displayEveryTime) {
if (countMemeCheck++ >= 10 * 16) { if (GaleContext.countMemeCheck++ >= 10 * 16) {
countMemeCheck = 0; GaleContext.countMemeCheck = 0;
} }
Log.verbose("Call draw"); Log.verbose("Call draw");
final long currentTime = System.currentTimeMillis(); final long currentTime = System.nanoTime();
//echrono::Time currentTime2 = echrono::Time::now();
//Log.warning("Time = " << currentTime << " " << currentTime2); //Log.warning("Time = " << currentTime << " " << currentTime2);
// TODO Review this ... // TODO Review this ...
// this is to prevent the multiple display at the a high frequency ... // this is to prevent the multiple display at the a high frequency ...
@ -418,7 +418,7 @@ public abstract class GaleContext {
} }
OpenGL.threadHasNoMoreContext(); OpenGL.threadHasNoMoreContext();
return hasDisplayDone; return hasDisplayDone;
}; }
/** /**
* The current context is set in foreground (framerate is maximum speed) * The current context is set in foreground (framerate is maximum speed)
@ -439,7 +439,7 @@ public abstract class GaleContext {
* The OS inform that the Windows is now Hidden. * The OS inform that the Windows is now Hidden.
*/ */
public void operatingSystemHide() { public void operatingSystemHide() {
postAction((context) -> { postAction(context -> {
/* /*
Application> appl = context.getApplication(); Application> appl = context.getApplication();
if (appl == null) { if (appl == null) {
@ -452,7 +452,7 @@ public abstract class GaleContext {
*/ */
Log.todo("HIDE ... "); Log.todo("HIDE ... ");
}); });
}; }
/** /**
* The OS inform that the current windows has change his position. * The OS inform that the current windows has change his position.
@ -462,7 +462,7 @@ public abstract class GaleContext {
if (this.windowsPos.isEqual(pos)) { if (this.windowsPos.isEqual(pos)) {
return; return;
} }
postAction((context) -> { postAction(context -> {
Log.debug("Receive MSG : THREADMOVE : " + context.windowsPos + " ==> " + pos); Log.debug("Receive MSG : THREADMOVE : " + context.windowsPos + " ==> " + pos);
context.windowsPos = pos; context.windowsPos = pos;
final GaleApplication appl = context.getApplication(); final GaleApplication appl = context.getApplication();
@ -478,7 +478,7 @@ public abstract class GaleContext {
*/ */
public void operatingSystemOpenGlContextDestroy() { public void operatingSystemOpenGlContextDestroy() {
this.resourceManager.contextHasBeenDestroyed(); this.resourceManager.contextHasBeenDestroyed();
}; }
/** /**
* The OS inform that the current windows has change his size. * The OS inform that the current windows has change his size.
@ -491,7 +491,7 @@ public abstract class GaleContext {
} }
// TODO Better in the thread ... ==> but generate some init error ... // TODO Better in the thread ... ==> but generate some init error ...
//gale::Dimension::setPixelWindowsSize(size); //gale::Dimension::setPixelWindowsSize(size);
postAction((context) -> { postAction(context -> {
Log.error("Receive MSG : THREAD_RESIZE : " + context.windowsSize + " ==> " + size); Log.error("Receive MSG : THREAD_RESIZE : " + context.windowsSize + " ==> " + size);
context.windowsSize = size; context.windowsSize = size;
//gale::Dimension::setPixelWindowsSize(context.windowsSize); //gale::Dimension::setPixelWindowsSize(context.windowsSize);
@ -502,7 +502,7 @@ public abstract class GaleContext {
// call application inside .. // call application inside ..
context.forceRedrawAll(); context.forceRedrawAll();
}); });
}; }
/** /**
* The current context is resumed * The current context is resumed
@ -521,7 +521,7 @@ public abstract class GaleContext {
} }
public void operatingSystemSetInput(final KeySpecial special, final KeyType type, final KeyStatus status, final int pointerID, final Vector2f pos) { public void operatingSystemSetInput(final KeySpecial special, final KeyType type, final KeyStatus status, final int pointerID, final Vector2f pos) {
postAction((context) -> { postAction(context -> {
final GaleApplication appl = context.getApplication(); final GaleApplication appl = context.getApplication();
if (appl == null) { if (appl == null) {
return; return;
@ -547,7 +547,7 @@ public abstract class GaleContext {
} }
public void operatingSystemsetKeyboard2(final KeySpecial special, final KeyKeyboard type, final KeyStatus state, final Character charValue) { public void operatingSystemsetKeyboard2(final KeySpecial special, final KeyKeyboard type, final KeyStatus state, final Character charValue) {
postAction((context) -> { postAction(context -> {
final GaleApplication appl = context.getApplication(); final GaleApplication appl = context.getApplication();
if (appl == null) { if (appl == null) {
return; return;
@ -560,7 +560,7 @@ public abstract class GaleContext {
* The OS inform that the Windows is now visible. * The OS inform that the Windows is now visible.
*/ */
public void operatingSystemShow() { public void operatingSystemShow() {
postAction((context) -> { postAction(context -> {
/* /*
Application> appl = context.getApplication(); Application> appl = context.getApplication();
if (appl == null) { if (appl == null) {
@ -573,7 +573,7 @@ public abstract class GaleContext {
*/ */
Log.todo("SHOW ... "); Log.todo("SHOW ... ");
}); });
}; }
/** /**
* The OS Inform that the Window has been killed * The OS Inform that the Window has been killed
@ -613,9 +613,9 @@ public abstract class GaleContext {
} }
/** /**
* Processing all the event arrived ... (commoly called in draw function) * Processing all the event arrived ... (commonly called in draw function)
*/ */
public void processEvents() { public void processEvents(final Clock clock, final long time) {
if (!this.lock.tryLock()) { if (!this.lock.tryLock()) {
return; return;
} }
@ -673,7 +673,7 @@ public abstract class GaleContext {
// } // }
// } // }
public void requestUpdateSize() { public void requestUpdateSize() {
postAction((context) -> { postAction(context -> {
//Log.debug("Receive MSG : THREADRESIZE"); //Log.debug("Receive MSG : THREADRESIZE");
context.forceRedrawAll(); context.forceRedrawAll();
}); });
@ -711,7 +711,7 @@ public abstract class GaleContext {
* set the Icon of the program * set the Icon of the program
* @param inputFile new filename icon of the current program. * @param inputFile new filename icon of the current program.
*/ */
public void setIcon(final Uri inputFile) {}; public void setIcon(final Uri inputFile) {}
/** /**
* The Application request that the current windows will change his position. * The Application request that the current windows will change his position.
@ -719,7 +719,7 @@ public abstract class GaleContext {
*/ */
public void setPos(final Vector2f pos) { public void setPos(final Vector2f pos) {
Log.info("setPos: NOT implemented ..."); Log.info("setPos: NOT implemented ...");
}; }
/** /**
* The application request a change of his current size. * The application request a change of his current size.
@ -736,20 +736,20 @@ public abstract class GaleContext {
*/ */
public void setTitle(final String title) { public void setTitle(final String title) {
Log.info("setTitle: NOT implemented ..."); Log.info("setTitle: NOT implemented ...");
}; }
/** /**
* Enable or Disable the decoration on the Windows (availlable only on Desktop) * Enable or Disable the decoration on the Windows (availlable only on Desktop)
* @param status "true" to enable decoration / false otherwise * @param status "true" to enable decoration / false otherwise
*/ */
public void setWindowsDecoration(final boolean status) {}; public void setWindowsDecoration(final boolean status) {}
/** /**
* The Application request that the Windows will be visible. * The Application request that the Windows will be visible.
*/ */
public void show() { public void show() {
Log.info("show: NOT implemented ..."); Log.info("show: NOT implemented ...");
}; }
/** /**
* StartProcessing (2nd thread). * StartProcessing (2nd thread).
@ -767,14 +767,14 @@ public abstract class GaleContext {
} }
// release the current interface: // release the current interface:
unLockContext(); unLockContext();
}; }
/** /**
* The application request that the Window will be killed * The application request that the Window will be killed
*/ */
public void stop() { public void stop() {
Log.warning("stop: NOT implemented for this platform..."); Log.warning("stop: NOT implemented for this platform...");
}; }
protected void unLockContext() { protected void unLockContext() {
@ -817,7 +817,7 @@ class MessageSystem {
public synchronized int getSize() { public synchronized int getSize() {
return this.data.size(); return this.data.size();
} }
}; }
class PeriodicThread extends ThreadAbstract { class PeriodicThread extends ThreadAbstract {
private final GaleContext context; private final GaleContext context;
@ -839,6 +839,7 @@ class PeriodicThread extends ThreadAbstract {
@Override @Override
protected void runPeriodic() { protected void runPeriodic() {
Log.verbose("----------------------------- [START] -----------------------------------");
try { try {
Thread.sleep(10); Thread.sleep(10);
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
@ -846,13 +847,18 @@ class PeriodicThread extends ThreadAbstract {
e.printStackTrace(); e.printStackTrace();
return; return;
} }
// Keep global clock to process events
Clock clock = Clock.systemUTC();
long time = System.nanoTime();
///synchronized (this.context) { ///synchronized (this.context) {
this.context.processEvents(); this.context.processEvents(clock, time);
// call all the application for periodic request (the application manage multiple instance )... // call all the application for periodic request (the application manage multiple instance )...
final GaleApplication appl = this.context.getApplication(); final GaleApplication appl = this.context.getApplication();
//Log.verbose("Call application : " + appl);
if (appl != null) { if (appl != null) {
appl.onPeriod(System.currentTimeMillis()); appl.onPeriod(clock, time);
} }
//} Log.verbose("----------------------------- [ END ] -----------------------------------");
} }
} }