Skip to content

Commit

Permalink
perf: limit max framerate for paint requests
Browse files Browse the repository at this point in the history
  • Loading branch information
cmhulbert committed Sep 19, 2023
1 parent c793e47 commit 6d1b2e1
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/main/java/bdv/fx/viewer/render/PainterThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ public final class PainterThread extends Thread {

private boolean isRunning;

private long lastUpdate = -1;
private long targetFrameRateMs = 1000 / 60;

public PainterThread(PainterThread.Paintable paintable) {

this((ThreadGroup)null, "PainterThread", paintable);
this(null, "PainterThread", paintable);
}

public PainterThread(ThreadGroup group, PainterThread.Paintable paintable) {
Expand All @@ -35,9 +38,18 @@ public PainterThread(ThreadGroup group, String name, PainterThread.Paintable pai
this.setDaemon(true);
}

@Override public void run() {
@Override
public void run() {


while (this.isRunning) {
final long msSinceLastUpdate = System.currentTimeMillis() - this.lastUpdate;
if (lastUpdate > 0 && msSinceLastUpdate < targetFrameRateMs) {
try {
Thread.sleep(msSinceLastUpdate);
} catch (InterruptedException e) {
}
}
if (this.isRunning && !this.isInterrupted()) {
boolean b;
synchronized (this) {
Expand All @@ -47,6 +59,7 @@ public PainterThread(ThreadGroup group, String name, PainterThread.Paintable pai

if (b) {
try {
lastUpdate = System.currentTimeMillis();
this.paintable.paint();
} catch (RejectedExecutionException var5) {
}
Expand Down

0 comments on commit 6d1b2e1

Please sign in to comment.