mirror of
https://github.com/webbukkit/dynmap.git
synced 2024-09-24 20:02:35 +02:00
Shift all imageIO and file I/O to async thread
Minus the version change in plugin.yml.
This commit is contained in:
parent
619485212d
commit
0a8f2a182a
@ -1,6 +1,8 @@
|
|||||||
package org.dynmap;
|
package org.dynmap;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -12,6 +14,8 @@ import java.util.logging.Level;
|
|||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import org.dynmap.kzedmap.KzedMapTile;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.entity.Entity;
|
||||||
@ -20,12 +24,15 @@ import org.bukkit.util.config.ConfigurationNode;
|
|||||||
import org.dynmap.DynmapWorld;
|
import org.dynmap.DynmapWorld;
|
||||||
import org.dynmap.MapTile;
|
import org.dynmap.MapTile;
|
||||||
import org.dynmap.debug.Debug;
|
import org.dynmap.debug.Debug;
|
||||||
|
import org.dynmap.kzedmap.KzedMap;
|
||||||
|
import org.dynmap.kzedmap.KzedZoomedMapTile;
|
||||||
import org.bukkit.Chunk;
|
import org.bukkit.Chunk;
|
||||||
|
|
||||||
public class MapManager {
|
public class MapManager {
|
||||||
protected static final Logger log = Logger.getLogger("Minecraft");
|
protected static final Logger log = Logger.getLogger("Minecraft");
|
||||||
|
|
||||||
public AsynchronousQueue<MapTile> tileQueue;
|
public AsynchronousQueue<MapTile> tileQueue;
|
||||||
|
public AsynchronousQueue<ImageWriter> writeQueue;
|
||||||
|
|
||||||
public Map<String, DynmapWorld> worlds = new HashMap<String, DynmapWorld>();
|
public Map<String, DynmapWorld> worlds = new HashMap<String, DynmapWorld>();
|
||||||
public Map<String, DynmapWorld> inactiveworlds = new HashMap<String, DynmapWorld>();
|
public Map<String, DynmapWorld> inactiveworlds = new HashMap<String, DynmapWorld>();
|
||||||
@ -40,6 +47,12 @@ public class MapManager {
|
|||||||
/* lock for our data structures */
|
/* lock for our data structures */
|
||||||
public static final Object lock = new Object();
|
public static final Object lock = new Object();
|
||||||
|
|
||||||
|
public static MapManager mapman; /* Our singleton */
|
||||||
|
|
||||||
|
private static class ImageWriter {
|
||||||
|
Runnable run;
|
||||||
|
}
|
||||||
|
|
||||||
private class FullWorldRenderState implements Runnable {
|
private class FullWorldRenderState implements Runnable {
|
||||||
DynmapWorld world; /* Which world are we rendering */
|
DynmapWorld world; /* Which world are we rendering */
|
||||||
Location loc; /* Start location */
|
Location loc; /* Start location */
|
||||||
@ -146,6 +159,9 @@ public class MapManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public MapManager(DynmapPlugin plugin, ConfigurationNode configuration) {
|
public MapManager(DynmapPlugin plugin, ConfigurationNode configuration) {
|
||||||
|
|
||||||
|
mapman = this;
|
||||||
|
|
||||||
this.tileQueue = new AsynchronousQueue<MapTile>(new Handler<MapTile>() {
|
this.tileQueue = new AsynchronousQueue<MapTile>(new Handler<MapTile>() {
|
||||||
@Override
|
@Override
|
||||||
public void handle(MapTile t) {
|
public void handle(MapTile t) {
|
||||||
@ -157,6 +173,14 @@ public class MapManager {
|
|||||||
}
|
}
|
||||||
}, (int) (configuration.getDouble("renderinterval", 0.5) * 1000));
|
}, (int) (configuration.getDouble("renderinterval", 0.5) * 1000));
|
||||||
|
|
||||||
|
this.writeQueue = new AsynchronousQueue<ImageWriter>(
|
||||||
|
new Handler<ImageWriter>() {
|
||||||
|
@Override
|
||||||
|
public void handle(ImageWriter w) {
|
||||||
|
w.run.run();
|
||||||
|
}
|
||||||
|
}, 10);
|
||||||
|
|
||||||
for(Object worldConfigurationObj : (List<?>)configuration.getProperty("worlds")) {
|
for(Object worldConfigurationObj : (List<?>)configuration.getProperty("worlds")) {
|
||||||
Map<?, ?> worldConfiguration = (Map<?, ?>)worldConfigurationObj;
|
Map<?, ?> worldConfiguration = (Map<?, ?>)worldConfigurationObj;
|
||||||
String worldName = (String)worldConfiguration.get("name");
|
String worldName = (String)worldConfiguration.get("name");
|
||||||
@ -180,6 +204,7 @@ public class MapManager {
|
|||||||
plug_in = plugin;
|
plug_in = plugin;
|
||||||
|
|
||||||
tileQueue.start();
|
tileQueue.start();
|
||||||
|
writeQueue.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -329,19 +354,21 @@ public class MapManager {
|
|||||||
|
|
||||||
public void startRendering() {
|
public void startRendering() {
|
||||||
tileQueue.start();
|
tileQueue.start();
|
||||||
|
writeQueue.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopRendering() {
|
public void stopRendering() {
|
||||||
tileQueue.stop();
|
tileQueue.stop();
|
||||||
|
writeQueue.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean render(MapTile tile) {
|
public boolean render(MapTile tile) {
|
||||||
boolean result = tile.getMap().render(tile, getTileFile(tile));
|
boolean result = tile.getMap().render(tile, getTileFile(tile));
|
||||||
pushUpdate(tile.getWorld(), new Client.Tile(tile.getFilename()));
|
//Do update after async file write
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private HashMap<World, File> worldTileDirectories = new HashMap<World, File>();
|
private HashMap<World, File> worldTileDirectories = new HashMap<World, File>();
|
||||||
private File getTileFile(MapTile tile) {
|
private File getTileFile(MapTile tile) {
|
||||||
World world = tile.getWorld();
|
World world = tile.getWorld();
|
||||||
@ -377,4 +404,10 @@ public class MapManager {
|
|||||||
return new Object[0];
|
return new Object[0];
|
||||||
return world.updates.getUpdatedObjects(since);
|
return world.updates.getUpdatedObjects(since);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void enqueueImageWrite(Runnable run) {
|
||||||
|
ImageWriter handler = new ImageWriter();
|
||||||
|
handler.run = run;
|
||||||
|
writeQueue.push(handler);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,10 @@ import javax.imageio.ImageIO;
|
|||||||
|
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
|
import org.dynmap.Client;
|
||||||
import org.dynmap.ColorScheme;
|
import org.dynmap.ColorScheme;
|
||||||
import org.dynmap.DynmapChunk;
|
import org.dynmap.DynmapChunk;
|
||||||
|
import org.dynmap.MapManager;
|
||||||
import org.dynmap.MapTile;
|
import org.dynmap.MapTile;
|
||||||
import org.dynmap.MapType;
|
import org.dynmap.MapType;
|
||||||
import org.dynmap.debug.Debug;
|
import org.dynmap.debug.Debug;
|
||||||
@ -120,15 +122,26 @@ public class FlatMap extends MapType {
|
|||||||
raster.setPixel(t.size-y-1, x, pixel);
|
raster.setPixel(t.size-y-1, x, pixel);
|
||||||
rendered = true;
|
rendered = true;
|
||||||
}
|
}
|
||||||
|
/* Hand encoding and writing file off to MapManager */
|
||||||
|
final File fname = outputFile;
|
||||||
|
final MapTile mtile = tile;
|
||||||
|
final BufferedImage img = im;
|
||||||
|
MapManager.mapman.enqueueImageWrite(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
Debug.debug("saving image " + fname.getPath());
|
||||||
|
try {
|
||||||
|
ImageIO.write(img, "png", fname);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Debug.error("Failed to save image: " + fname.getPath(), e);
|
||||||
|
} catch (java.lang.NullPointerException e) {
|
||||||
|
Debug.error("Failed to save image (NullPointerException): " + fname.getPath(), e);
|
||||||
|
}
|
||||||
|
img.flush();
|
||||||
|
MapManager.mapman.pushUpdate(mtile.getWorld(),
|
||||||
|
new Client.Tile(mtile.getFilename()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
|
||||||
ImageIO.write(im, "png", outputFile);
|
|
||||||
} catch (IOException e) {
|
|
||||||
Debug.error("Failed to save image: " + outputFile.getPath(), e);
|
|
||||||
} catch (java.lang.NullPointerException e) {
|
|
||||||
Debug.error("Failed to save image (NullPointerException): " + outputFile.getPath(), e);
|
|
||||||
}
|
|
||||||
im.flush();
|
|
||||||
return rendered;
|
return rendered;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,10 @@ import java.util.Map;
|
|||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
|
import org.dynmap.Client;
|
||||||
import org.dynmap.ColorScheme;
|
import org.dynmap.ColorScheme;
|
||||||
|
import org.dynmap.MapManager;
|
||||||
|
import org.dynmap.MapTile;
|
||||||
import org.dynmap.debug.Debug;
|
import org.dynmap.debug.Debug;
|
||||||
|
|
||||||
public class DefaultTileRenderer implements MapTileRenderer {
|
public class DefaultTileRenderer implements MapTileRenderer {
|
||||||
@ -104,12 +107,28 @@ public class DefaultTileRenderer implements MapTileRenderer {
|
|||||||
iz--;
|
iz--;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* save the generated tile */
|
/* Hand encoding and writing file off to MapManager */
|
||||||
saveImage(im, outputFile);
|
final File fname = outputFile;
|
||||||
im.flush();
|
final KzedMapTile mtile = tile;
|
||||||
|
final BufferedImage img = im;
|
||||||
tile.file = outputFile;
|
MapManager.mapman.enqueueImageWrite(new Runnable() {
|
||||||
((KzedMap) tile.getMap()).invalidateTile(new KzedZoomedMapTile(world, (KzedMap) tile.getMap(), tile));
|
public void run() {
|
||||||
|
Debug.debug("saving image " + fname.getPath());
|
||||||
|
try {
|
||||||
|
ImageIO.write(img, "png", fname);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Debug.error("Failed to save image: " + fname.getPath(), e);
|
||||||
|
} catch (java.lang.NullPointerException e) {
|
||||||
|
Debug.error("Failed to save image (NullPointerException): " + fname.getPath(), e);
|
||||||
|
}
|
||||||
|
img.flush();
|
||||||
|
mtile.file = fname;
|
||||||
|
((KzedMap)mtile.getMap()).invalidateTile(
|
||||||
|
new KzedZoomedMapTile(mtile.getWorld(), (KzedMap) mtile.getMap(), mtile));
|
||||||
|
MapManager.mapman.pushUpdate(mtile.getWorld(),
|
||||||
|
new Client.Tile(mtile.getFilename()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return !isempty;
|
return !isempty;
|
||||||
}
|
}
|
||||||
@ -170,17 +189,4 @@ public class DefaultTileRenderer implements MapTileRenderer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* save rendered tile, update zoom-out tile */
|
|
||||||
public void saveImage(BufferedImage im, File outputFile) {
|
|
||||||
Debug.debug("saving image " + outputFile.getPath());
|
|
||||||
/* save image */
|
|
||||||
try {
|
|
||||||
ImageIO.write(im, "png", outputFile);
|
|
||||||
} catch (IOException e) {
|
|
||||||
Debug.error("Failed to save image: " + outputFile.getPath(), e);
|
|
||||||
} catch (java.lang.NullPointerException e) {
|
|
||||||
Debug.error("Failed to save image (NullPointerException): " + outputFile.getPath(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -9,79 +9,89 @@ import java.util.Map;
|
|||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import org.dynmap.Client;
|
||||||
|
import org.dynmap.MapManager;
|
||||||
import org.dynmap.debug.Debug;
|
import org.dynmap.debug.Debug;
|
||||||
|
|
||||||
public class ZoomedTileRenderer {
|
public class ZoomedTileRenderer {
|
||||||
public ZoomedTileRenderer(Map<String, Object> configuration) {
|
public ZoomedTileRenderer(Map<String, Object> configuration) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render(KzedZoomedMapTile zt, File outputPath) {
|
public void render(final KzedZoomedMapTile zt, final File outputPath) {
|
||||||
KzedMapTile originalTile = zt.originalTile;
|
/* Hand it all to map manager write thread */
|
||||||
int px = originalTile.px;
|
MapManager.mapman.enqueueImageWrite(new Runnable() {
|
||||||
int py = originalTile.py;
|
public void run() {
|
||||||
int zpx = zt.getTileX();
|
KzedMapTile originalTile = zt.originalTile;
|
||||||
int zpy = zt.getTileY();
|
int px = originalTile.px;
|
||||||
|
int py = originalTile.py;
|
||||||
|
int zpx = zt.getTileX();
|
||||||
|
int zpy = zt.getTileY();
|
||||||
|
|
||||||
BufferedImage image = null;
|
BufferedImage image = null;
|
||||||
try {
|
try {
|
||||||
image = ImageIO.read(originalTile.file);
|
image = ImageIO.read(originalTile.file);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
} catch (IndexOutOfBoundsException e) {
|
} catch (IndexOutOfBoundsException e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (image == null) {
|
if (image == null) {
|
||||||
Debug.debug("Could not load original tile, won't render zoom-out tile.");
|
Debug.debug("Could not load original tile, won't render zoom-out tile.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferedImage zIm = null;
|
BufferedImage zIm = null;
|
||||||
File zoomFile = outputPath;
|
File zoomFile = outputPath;
|
||||||
try {
|
try {
|
||||||
zIm = ImageIO.read(zoomFile);
|
zIm = ImageIO.read(zoomFile);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
} catch (IndexOutOfBoundsException e) {
|
} catch (IndexOutOfBoundsException e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (zIm == null) {
|
if (zIm == null) {
|
||||||
/* create new one */
|
/* create new one */
|
||||||
zIm = new BufferedImage(KzedMap.tileWidth, KzedMap.tileHeight, BufferedImage.TYPE_INT_RGB);
|
zIm = new BufferedImage(KzedMap.tileWidth, KzedMap.tileHeight, BufferedImage.TYPE_INT_RGB);
|
||||||
Debug.debug("New zoom-out tile created " + zt.getFilename());
|
Debug.debug("New zoom-out tile created " + zt.getFilename());
|
||||||
} else {
|
} else {
|
||||||
Debug.debug("Loaded zoom-out tile from " + zt.getFilename());
|
Debug.debug("Loaded zoom-out tile from " + zt.getFilename());
|
||||||
}
|
}
|
||||||
|
/* update zoom-out tile */
|
||||||
|
|
||||||
/* update zoom-out tile */
|
/* scaled size */
|
||||||
|
int scw = KzedMap.tileWidth / 2;
|
||||||
|
int sch = KzedMap.tileHeight / 2;
|
||||||
|
|
||||||
/* scaled size */
|
/* origin in zoomed-out tile */
|
||||||
int scw = KzedMap.tileWidth / 2;
|
int ox = 0;
|
||||||
int sch = KzedMap.tileHeight / 2;
|
int oy = 0;
|
||||||
|
|
||||||
/* origin in zoomed-out tile */
|
if (zpx != px)
|
||||||
int ox = 0;
|
ox = scw;
|
||||||
int oy = 0;
|
if (zpy != py)
|
||||||
|
oy = sch;
|
||||||
|
|
||||||
if (zpx != px)
|
/* blit scaled rendered tile onto zoom-out tile */
|
||||||
ox = scw;
|
// WritableRaster zr = zIm.getRaster();
|
||||||
if (zpy != py)
|
Graphics2D g2 = zIm.createGraphics();
|
||||||
oy = sch;
|
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||||
|
g2.drawImage(image, ox, oy, scw, sch, null);
|
||||||
|
|
||||||
/* blit scaled rendered tile onto zoom-out tile */
|
image.flush();
|
||||||
// WritableRaster zr = zIm.getRaster();
|
|
||||||
Graphics2D g2 = zIm.createGraphics();
|
|
||||||
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
||||||
g2.drawImage(image, ox, oy, scw, sch, null);
|
|
||||||
|
|
||||||
image.flush();
|
/* save zoom-out tile */
|
||||||
|
try {
|
||||||
|
ImageIO.write(zIm, "png", zoomFile);
|
||||||
|
Debug.debug("Saved zoom-out tile at " + zoomFile.getName());
|
||||||
|
} catch (IOException e) {
|
||||||
|
Debug.error("Failed to save zoom-out tile: " + zoomFile.getName(), e);
|
||||||
|
} catch (java.lang.NullPointerException e) {
|
||||||
|
Debug.error("Failed to save zoom-out tile (NullPointerException): " + zoomFile.getName(), e);
|
||||||
|
}
|
||||||
|
zIm.flush();
|
||||||
|
|
||||||
/* save zoom-out tile */
|
MapManager.mapman.pushUpdate(zt.getWorld(),
|
||||||
try {
|
new Client.Tile(zt.getFilename()));
|
||||||
ImageIO.write(zIm, "png", zoomFile);
|
|
||||||
Debug.debug("Saved zoom-out tile at " + zoomFile.getName());
|
}
|
||||||
} catch (IOException e) {
|
});
|
||||||
Debug.error("Failed to save zoom-out tile: " + zoomFile.getName(), e);
|
|
||||||
} catch (java.lang.NullPointerException e) {
|
|
||||||
Debug.error("Failed to save zoom-out tile (NullPointerException): " + zoomFile.getName(), e);
|
|
||||||
}
|
|
||||||
zIm.flush();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user