Skip to content

Commit

Permalink
Reduce preview thread lock contention
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdanggg2 committed Jun 24, 2024
1 parent e9470f5 commit 398351d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 36 deletions.
65 changes: 31 additions & 34 deletions src/org/jmc/gui/PreviewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class PreviewPanel extends JPanel implements MouseMotionListener, MouseWh
/**
* Back buffers used for drawing the preview.
*/
private BufferedImage main_img,height_img;
private final BufferedImage main_img, height_img;

/**
* Font used in the preview.
Expand Down Expand Up @@ -104,8 +104,7 @@ public class PreviewPanel extends JPanel implements MouseMotionListener, MouseWh
* @author danijel
*
*/
class ChunkImage
{
static class ChunkImage {
public BufferedImage image;
public BufferedImage height_map;
public int x, y;
Expand All @@ -117,20 +116,20 @@ class ChunkImage
* @author danijel
*
*/
class MapMarker
{
static class MapMarker {
int x, z;
Color color;
}

/**
* Collection of chunks.
*/
private Vector<ChunkImage> chunks;
private final Vector<ChunkImage> chunks;
public volatile boolean chunksDirty = true;
/**
* Collection of markers.
*/
private Vector<MapMarker> markers;
private final Vector<MapMarker> markers;

/**
* Main constructor.
Expand All @@ -156,7 +155,7 @@ public PreviewPanel() {
gui_font=new Font("Verdana",Font.BOLD,10);
gui_color=Color.white;
gui_bg_color=Color.black;
gui_bg_alpha=0.3f;
gui_bg_alpha=0.3f;
gui_text=new Vector<String>();

}
Expand Down Expand Up @@ -274,6 +273,12 @@ void redraw(boolean fast)
int win_w=getWidth();
int win_h=getHeight();

Vector<ChunkImage> chunksSnapshot;
synchronized (chunks) {
chunksSnapshot = (Vector<ChunkImage>)chunks.clone();
chunksDirty = false;
}

synchronized (main_img) {
Graphics2D mg=main_img.createGraphics();
if(!fast)
Expand All @@ -288,23 +293,19 @@ void redraw(boolean fast)
hg.setColor(Color.black);
hg.clearRect(0, 0, win_w, win_h);
}

BufferedImage ckln=new BufferedImage(MAX_WIDTH, MAX_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D cklng = ckln.createGraphics();

for (ChunkImage chunk : chunksSnapshot) {
redrawChunk(chunk, fast, cklng);
}

synchronized (chunks) {
for(ChunkImage chunk:chunks)
{
redrawChunk(chunk, fast, cklng);
}
}

if(!fast)
{
if(!fast) {
WritableRaster height_raster = height_img.getRaster();
int h,oh;
for(int x=0; x<win_w; x++)
for(int y=0; y<win_h; y++)
{
for(int x=0; x<win_w; x++) {
for(int y=0; y<win_h; y++) {
h=height_raster.getSample(x, y, 0);
if(x<(win_w-1) && y<(win_h-1)) oh=height_raster.getSample(x+1, y+1, 0);
else oh=h;
Expand All @@ -315,10 +316,11 @@ void redraw(boolean fast)

height_raster.setSample(x, y, 0, h);
}
}
mg.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
mg.drawImage(height_img,0,0,null);
}
if(showchunks)
if (showchunks)
mg.drawImage(ckln, 0, 0, null);

}
Expand All @@ -327,8 +329,7 @@ void redraw(boolean fast)
/**
* Draws a single chunk.
*/
private void redrawChunk(ChunkImage chunk, boolean fast, Graphics2D cklng)
{
private void redrawChunk(ChunkImage chunk, boolean fast, Graphics2D cklng) {
int win_w=getWidth();
int win_h=getHeight();

Expand Down Expand Up @@ -360,9 +361,8 @@ private void redrawChunk(ChunkImage chunk, boolean fast, Graphics2D cklng)
* @param x x location of the chunk on the screen
* @param y y location of the chunk on the screen
*/
public void addImage(BufferedImage img, BufferedImage height, int x, int y, BlockDataPos[] topBlocks)
{
ChunkImage chunk=new ChunkImage();
public void addImage(BufferedImage img, BufferedImage height, int x, int y, BlockDataPos[] topBlocks) {
ChunkImage chunk= new ChunkImage();
chunk.image=img;
chunk.height_map=height;
chunk.x=x;
Expand All @@ -371,7 +371,7 @@ public void addImage(BufferedImage img, BufferedImage height, int x, int y, Bloc
synchronized (chunks) {
chunks.add(chunk);
}
redrawChunk(chunk, true, null);
chunksDirty = true;
}

/**
Expand Down Expand Up @@ -424,7 +424,7 @@ public void setPosition(int x, int z)
*/
public void addMarker(int x, int z, Color color)
{
MapMarker marker=new MapMarker();
MapMarker marker= new MapMarker();
marker.x=x;
marker.z=z;
marker.color=color;
Expand Down Expand Up @@ -471,12 +471,9 @@ public void mouseWheelMoved(MouseWheelEvent e) {
float ratio=zoom_level/old_zoom_level;

shift_x-=(x-x/ratio)/old_zoom_level;
shift_y-=(y-y/ratio)/old_zoom_level;

if(!fastrendermode)
redraw(false);
else
redraw(true);
shift_y-=(y-y/ratio)/old_zoom_level;

redraw(fastrendermode);
repaint();

}
Expand Down
4 changes: 2 additions & 2 deletions src/org/jmc/gui/ViewChunkLoaderRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void run() {
if (Thread.interrupted()) {
return;
}
if (chunksToDo.get() == 0 && !drawn) {
if (chunksToDo.get() == 0 && !drawn || preview.chunksDirty) {
preview.redraw(preview.fastrendermode);
drawn = true;
}
Expand Down Expand Up @@ -331,8 +331,8 @@ private BlockDataPos[] renderImages(Chunk.Blocks bd, Graphics2D block_graphics,
for(int z = 0; z < 16; z++) {
for(int x = 0; x < 16; x++) {
for(int y = chunkCeiling; y >= chunkFloor; y--) {
NamespaceID blockBiome = bd.getBiome(x, y, z);
BlockData blockData = bd.getBlockData(x, y, z);
NamespaceID blockBiome = bd.getBiome(x, y, z);

if(blockData != null && !blockData.getInfo().getOcclusion().equals(BlockInfo.Occlusion.NONE)) {
topBlocks[z*16+x] = new BlockDataPos(new BlockPos(x, y, z), blockData, blockBiome);
Expand Down

0 comments on commit 398351d

Please sign in to comment.