mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-17 19:15:06 +00:00
86 lines
2.3 KiB
Java
86 lines
2.3 KiB
Java
package com.gtnewhorizons.angelica.rendering;
|
|
|
|
import java.util.Queue;
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
import java.util.concurrent.Executor;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.locks.LockSupport;
|
|
import java.util.function.BooleanSupplier;
|
|
|
|
import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
|
|
|
public class AngelicaRenderQueue {
|
|
private static final Queue<Runnable> TASKS = new ConcurrentLinkedQueue<>();
|
|
|
|
// Metrics
|
|
private static volatile int lastFrameTasksRan = 0;
|
|
private static volatile long lastFrameTimeNs = 0;
|
|
private static volatile long lastFrameLongestTaskNs = 0;
|
|
|
|
public static int getQueueDepth() {
|
|
return TASKS.size();
|
|
}
|
|
|
|
public static int getLastFrameTasksRan() {
|
|
return lastFrameTasksRan;
|
|
}
|
|
|
|
public static long getLastFrameTimeNs() {
|
|
return lastFrameTimeNs;
|
|
}
|
|
|
|
public static long getLastFrameLongestTaskNs() {
|
|
return lastFrameLongestTaskNs;
|
|
}
|
|
|
|
public static void recordFrameStats(int tasksRan, long timeNs, long longestTaskNs) {
|
|
lastFrameTasksRan = tasksRan;
|
|
lastFrameTimeNs = timeNs;
|
|
lastFrameLongestTaskNs = longestTaskNs;
|
|
}
|
|
|
|
private static final Executor EXECUTOR = (runnable) -> {
|
|
if(GLStateManager.isMainThread()) {
|
|
runnable.run();
|
|
} else {
|
|
TASKS.add(runnable);
|
|
LockSupport.unpark(GLStateManager.getMainThread());
|
|
}
|
|
};
|
|
|
|
public static Executor executor() {
|
|
return EXECUTOR;
|
|
}
|
|
|
|
public static void submit(Runnable runnable) {
|
|
TASKS.add(runnable);
|
|
LockSupport.unpark(GLStateManager.getMainThread());
|
|
}
|
|
|
|
public static void remove(Runnable runnable) {
|
|
TASKS.removeIf(t -> t == runnable);
|
|
}
|
|
|
|
public static int processTasks(int max) {
|
|
int tasksRun = 0;
|
|
while(tasksRun < max) {
|
|
final Runnable r = TASKS.poll();
|
|
if(r == null)
|
|
break;
|
|
r.run();
|
|
tasksRun++;
|
|
}
|
|
return tasksRun;
|
|
}
|
|
|
|
private static final long WAIT_TIME = TimeUnit.MILLISECONDS.toNanos(20);
|
|
|
|
public static void managedBlock(BooleanSupplier isDone) {
|
|
while(!isDone.getAsBoolean()) {
|
|
if(AngelicaRenderQueue.processTasks(1) == 0) {
|
|
LockSupport.parkNanos(WAIT_TIME);
|
|
}
|
|
}
|
|
}
|
|
}
|