Skip to content

Commit

Permalink
GUI: Cancel old rendering tasks on reload
Browse files Browse the repository at this point in the history
This bug probably got introduced during the last GUI rendering refactoring.
  • Loading branch information
piegamesde committed Jan 4, 2022
1 parent 5d64590 commit a114616
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -42,6 +43,7 @@ public class RenderedMap implements Runnable {
protected ReadOnlySetWrapper<Vector2ic> rendering = new ReadOnlySetWrapper<>(FXCollections.observableSet());
/** Where the mouse currently points to, in world coordinates */
protected ReadOnlyObjectProperty<Vector2dc> mouseWorldProperty;
private final AtomicBoolean cancelRendering = new AtomicBoolean(false);

public RenderedMap(RegionFolder regionFolder, ExecutorService executor, ReadOnlyObjectProperty<Vector2dc> mouseWorldProperty) {
this.regionFolder = Objects.requireNonNull(regionFolder);
Expand Down Expand Up @@ -76,10 +78,18 @@ public void draw(GraphicsContext gc, int level, AABBd frustum, double scale) {
.filter(r -> r.isVisible(frustum))
.forEach(r -> r.drawForeground(gc, frustum, scale));
}

/* Cancel existing rendering tasks and already free up some memory */
public void cancel() {
cancelRendering.set(true);
regions.clear();
}

/* Render the next region file on a worker thread */
@Override
public void run() {
if (cancelRendering.get())
return;
RenderedRegion region = nextRegion();
Platform.runLater(() -> rendering.getValue().add(region.position));
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,24 @@ public WorldRendererCanvas() {
progress.addListener(e -> repaint());

this.regionFolder.addListener((obs, prev, val) -> {
if (map != null) {
/* Cancel existing tasks */
for (Runnable task : executor.getQueue())
executor.remove(task);
/* Free up the old map */
map.cancel();
}
if (val == null || val.listRegions().isEmpty()) {
map = null;
status.set("No regions loaded");
executor.execute(() -> Platform.runLater(() -> status.set("No regions loaded")));
chunkMetadata.unbind();
chunkMetadata.clear();
} else {
executor.execute(() -> Platform.runLater(() -> status.set("Rendering")));
map = new RenderedMap(val, executor, viewport.mouseWorldProperty);
map.getCurrentlyRendering().addListener((InvalidationListener) e -> repaint());
progress.bind(map.getProgress());
chunkMetadata.bind(map.getChunkMetadata());
status.set("Rendering");
executor.execute(() -> Platform.runLater(() -> status.set("Done")));
}
repaint();
Expand Down

0 comments on commit a114616

Please sign in to comment.