From 18e1dc9f841518e73657d131d3a56649fe1fd40b Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Mon, 11 Apr 2022 00:48:12 +0200 Subject: [PATCH] Synchronize the render, but the result is not optimum... --- .../atriasoft/gale/context/GaleContext.java | 29 +++++++++++--- .../context/LWJG_AWT/ContextLWJGLAWT.java | 3 +- .../atriasoft/gale/context/MessageSystem.java | 38 ++++++++++++++++--- 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/src/org/atriasoft/gale/context/GaleContext.java b/src/org/atriasoft/gale/context/GaleContext.java index d3a503f..13e9d5a 100644 --- a/src/org/atriasoft/gale/context/GaleContext.java +++ b/src/org/atriasoft/gale/context/GaleContext.java @@ -31,6 +31,8 @@ enum ApplicationState { public abstract class GaleContext { protected static final int MAX_MANAGE_INPUT = 15; + private static final String STATIC_ID_RESIZE = "010__RESIZE"; + private static final String STATIC_ID_REDRAW_ALL = "0100__REDRAW_ALL"; private static GaleContext globalContext = null; // return true if a flush is needed private static int countMemeCheck = 0; @@ -67,6 +69,8 @@ public abstract class GaleContext { protected Vector2f windowsSize = Vector2f.ZERO; //!< current size of the system protected boolean fullscreen = false; protected Vector2f windowsPos; //!< current size of the system + // note: in the current mode, the management is not able to synchronize it good.. + private final boolean requestSynchronousProcessing = true; //!< this permit to process the event in the global GUI thread instead of the local processing thread. public GaleContext(final GaleApplication application, final String[] args) { // set a basic @@ -125,10 +129,12 @@ public abstract class GaleContext { appl.onStart(context); appl.onResume(context); this.applicationState = ApplicationState.RUNNING; + // this is done at the end to perform a full ended rendering. + context.requestUpdateSize(); }); // force a recalculation - requestUpdateSize(); + //requestUpdateSize(); Log.info(" == > Gale system init (END)"); } @@ -540,7 +546,7 @@ public abstract class GaleContext { } // TODO Better in the thread ... ==> but generate some init error ... //gale::Dimension::setPixelWindowsSize(size); - postActionAsync(context -> { + postActionAsync(GaleContext.STATIC_ID_RESIZE, context -> { Log.error("Receive MSG : THREAD_RESIZE : {} ==> {}", context.windowsSize, size); context.windowsSize = size; //gale::Dimension::setPixelWindowsSize(context.windowsSize); @@ -655,7 +661,20 @@ public abstract class GaleContext { } protected void postActionAsync(final ActionToDoInAsyncLoop data) { - this.msgSystemAsync.addElement(data); + if (this.requestSynchronousProcessing) { + this.msgSystemGui.addElement(data); + } else { + this.msgSystemAsync.addElement(data); + } + //Later, when the necessary event happens, the thread that is running it calls notify() from a block synchronized on the same object. + } + + protected void postActionAsync(final String uniqueID, final ActionToDoInAsyncLoop data) { + if (this.requestSynchronousProcessing) { + this.msgSystemGui.addElement(uniqueID, data); + } else { + this.msgSystemAsync.addElement(uniqueID, data); + } //Later, when the necessary event happens, the thread that is running it calls notify() from a block synchronized on the same object. } @@ -674,7 +693,7 @@ public abstract class GaleContext { try { int nbEvent = 0; while (this.msgSystemAsync.getSize() > 0) { - Log.error(" [" + nbEvent + "] event ..."); + Log.verbose(" [" + nbEvent + "] event ..."); nbEvent++; final ActionToDoInAsyncLoop func = this.msgSystemAsync.getElementWait(); if (func == null) { @@ -747,7 +766,7 @@ public abstract class GaleContext { // } // } public void requestUpdateSize() { - postActionAsync(context -> { + postActionAsync(this.STATIC_ID_REDRAW_ALL, context -> { //Log.debug("Receive MSG : THREADRESIZE"); context.forceRedrawAll(); }); diff --git a/src/org/atriasoft/gale/context/LWJG_AWT/ContextLWJGLAWT.java b/src/org/atriasoft/gale/context/LWJG_AWT/ContextLWJGLAWT.java index 3efede3..d9baf46 100644 --- a/src/org/atriasoft/gale/context/LWJG_AWT/ContextLWJGLAWT.java +++ b/src/org/atriasoft/gale/context/LWJG_AWT/ContextLWJGLAWT.java @@ -26,7 +26,6 @@ import javax.swing.SwingUtilities; import javax.swing.WindowConstants; import org.atriasoft.etk.Uri; -import org.atriasoft.etk.math.FMath; import org.atriasoft.etk.math.Vector2f; import org.atriasoft.gale.DisplayManagerDraw; import org.atriasoft.gale.Fps; @@ -184,6 +183,7 @@ public class ContextLWJGLAWT extends GaleContext implements MouseListener, Mouse } // Process event from the GUI (specific events... processEventsGui(); + /* final long stopRender = System.currentTimeMillis(); try { // limit at 60FPS ==> bad to do it here, but it work for now... add a minimum of 10ms to free lock... @@ -192,6 +192,7 @@ public class ContextLWJGLAWT extends GaleContext implements MouseListener, Mouse // TODO Auto-generated catch block e.printStackTrace(); } + */ } }, BorderLayout.CENTER); this.frame.pack(); diff --git a/src/org/atriasoft/gale/context/MessageSystem.java b/src/org/atriasoft/gale/context/MessageSystem.java index 46e4933..d6e0bc5 100644 --- a/src/org/atriasoft/gale/context/MessageSystem.java +++ b/src/org/atriasoft/gale/context/MessageSystem.java @@ -1,16 +1,36 @@ package org.atriasoft.gale.context; +import java.util.HashMap; +import java.util.Map; import java.util.Vector; +import org.atriasoft.gale.internal.Log; + public class MessageSystem { private final Vector data = new Vector<>(); + private final Map dataSingle = new HashMap<>(); public synchronized void addElement(final ActionToDoInAsyncLoop data2) { this.data.addElement(data2); notifyAll(); } - public synchronized ActionToDoInAsyncLoop getElement() { + public synchronized void addElement(final String uniqueID, final ActionToDoInAsyncLoop data2) { + this.dataSingle.put(uniqueID, data2); + notifyAll(); + } + + protected synchronized ActionToDoInAsyncLoop getElementSingle() { + //Log.warning("+++++++++++++++++++++++++++++++++ getElement()"); + final Map.Entry entry = this.dataSingle.entrySet().iterator().next(); + final String key = entry.getKey(); + final ActionToDoInAsyncLoop message = entry.getValue(); + this.dataSingle.remove(key); + //Log.warning("+++++++++++++++++++++++++++++++++ getElement() ===> done " + message); + return message; + } + + protected synchronized ActionToDoInAsyncLoop getElementVector() { //Log.warning("+++++++++++++++++++++++++++++++++ getElement()"); final ActionToDoInAsyncLoop message = this.data.firstElement(); this.data.removeElement(message); @@ -19,7 +39,7 @@ public class MessageSystem { } public synchronized ActionToDoInAsyncLoop getElementWait() { - if (this.data.isEmpty()) { + if (this.data.isEmpty() && this.dataSingle.isEmpty()) { try { wait(); } catch (final InterruptedException e) { @@ -28,13 +48,19 @@ public class MessageSystem { return null; } } - if (this.data.isEmpty()) { - return null; + if (!this.data.isEmpty()) { + return getElementVector(); } - return getElement(); + if (!this.dataSingle.isEmpty()) { + return getElementSingle(); + } + return null; } public synchronized int getSize() { - return this.data.size(); + Log.verbose("------------------------------------------------------------"); + Log.verbose("-- nb message: {} + {}", this.data.size(), this.dataSingle.size()); + Log.verbose("------------------------------------------------------------"); + return this.data.size() + this.dataSingle.size(); } } \ No newline at end of file