This commit is contained in:
Victor Shcherb 2012-05-10 00:03:31 +02:00
parent 82e4307ed5
commit d44b535574
3 changed files with 84 additions and 24 deletions

View file

@ -26,6 +26,9 @@ import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
@ -199,11 +202,14 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
public static void main(String[] args) throws IOException {
showMainWindow(512, 512, null);
LatLon def = DataExtractionSettings.getSettings().getDefaultLocation();
showMainWindow(512, 512, def.getLatitude(), def.getLongitude(),
DataExtractionSettings.getSettings().getDefaultZoom(), null);
}
public static void showMainWindow(int wx, int hy, NativeSwingRendering rendering) {
public static void showMainWindow(int wx, int hy,
double latitude, double longitude, int zoom, NativeSwingRendering nativeLib) {
JFrame frame = new JFrame(Messages.getString("MapPanel.MAP.VIEW")); //$NON-NLS-1$
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
@ -212,7 +218,10 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
}
final MapPanel panel = new MapPanel(DataExtractionSettings.getSettings().getTilesDirectory());
panel.nativeRenderingImg = img;
panel.nativeLibRendering = nativeLib;
panel.longitude = longitude;
panel.latitude = latitude;
panel.zoom = zoom;
frame.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
@ -229,7 +238,9 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
bar.add(getMenuToChooseSource(panel));
frame.setJMenuBar(bar);
frame.setSize(wx, hy);
frame.setVisible(true);
NativeRendererRunnable runable = panel.new NativeRendererRunnable(512, 512);
runable.run();
// frame.setVisible(true);
}
private File tilesLocation = null;
@ -238,8 +249,12 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
private ITileSource map = TileSourceManager.getMapnikSource();
private NativeSwingRendering nativeLibRendering;
private NativeRendererRunnable lastAddedRunnable;
private Image nativeRenderingImg;
private ThreadPoolExecutor nativeRenderer = new ThreadPoolExecutor(1, 1, 30, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(1));
// zoom level
private int zoom = 1;
@ -453,7 +468,7 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
@Override
public void tileDownloaded(DownloadRequest request) {
if(request == null){
prepareImage(false);
prepareRasterImage(false);
return;
}
int tileSize = getTileSize();
@ -473,10 +488,24 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
}
public void prepareImage(){
prepareImage(DataExtractionSettings.getSettings().useInternetToLoadImages());
if(nativeLibRendering != null) {
prepareNativeImage();
} else {
prepareRasterImage(DataExtractionSettings.getSettings().useInternetToLoadImages());
}
}
public void prepareImage(boolean loadNecessaryImages){
private synchronized void prepareNativeImage() {
nativeRenderer.getQueue().clear();
NativeRendererRunnable runnable = new NativeRendererRunnable(getWidth(), getHeight());
if(lastAddedRunnable == null || !lastAddedRunnable.contains(runnable)) {
lastAddedRunnable = runnable;
nativeRenderer.execute(runnable);
}
}
private void prepareRasterImage(boolean loadNecessaryImages){
try {
int tileSize = getTileSize();
if (images != null) {
@ -824,5 +853,49 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback {
}
}
private class NativeRendererRunnable implements Runnable {
private int sleft;
private int sright;
private int stop;
private int sbottom;
private int z;
private int EXPAND_X = 100;
private int EXPAND_Y = 100;
private final int cf;
public NativeRendererRunnable(int w, int h) {
int tileSize = getTileSize();
this.z = zoom;
cf = (1 << (31 - zoom)) / tileSize;
sleft = MapUtils.get31TileNumberX(longitude) - (w / 2) * cf;
sright = MapUtils.get31TileNumberX(longitude) + (w / 2) * cf;
stop = MapUtils.get31TileNumberY(latitude) - (h / 2) * cf;
sbottom = MapUtils.get31TileNumberY(latitude) + (h / 2) * cf;
}
public boolean contains(NativeRendererRunnable r) {
if(r.sright > sright + EXPAND_X * cf ||
r.sleft < sleft - EXPAND_X * cf ||
r.stop < stop - EXPAND_Y * cf ||
r.sbottom > sbottom + EXPAND_Y * cf) {
return false;
}
return true;
}
@Override
public void run() {
if (nativeRenderer.getQueue().isEmpty()) {
try {
nativeRenderingImg = nativeLibRendering.renderImage(sleft, sright, stop, sbottom, z);
} catch (IOException e) {
e.printStackTrace();
}
repaint();
}
}
}
}

View file

@ -1,15 +1,12 @@
package net.osmand.swing;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.MemoryCacheImageInputStream;
import org.xml.sax.SAXException;
@ -106,17 +103,7 @@ public class NativeSwingRendering extends NativeLibrary {
double latTop = 22.5;
double lonLeft = -80;
float tileX = 2;
float tileY = 2;
int zoom = 11;
double latBottom = MapUtils.getLatitudeFromTile(zoom, MapUtils.getTileNumberY(zoom, latTop) + tileY);
double lonRight = MapUtils.getLongitudeFromTile(zoom, MapUtils.getTileNumberX(zoom, lonLeft) + tileX);
int sleft = MapUtils.get31TileNumberX(lonLeft);
int sright = MapUtils.get31TileNumberX(lonRight);
int stop = MapUtils.get31TileNumberY(latTop);
int sbottom = MapUtils.get31TileNumberY(latBottom);
MapPanel.showMainWindow(512, 512, lib.renderImage(sleft, sright, stop, sbottom, zoom));
MapPanel.showMainWindow(512, 512, latTop, lonLeft, zoom, lib);
}
}

View file

@ -280,8 +280,6 @@ extern "C" JNIEXPORT jobject JNICALL Java_net_osmand_NativeLibrary_generateRende
if (result != NULL) {
doRendering(result->result, canvas, req, &rc);
}
delete canvas;
delete req;
pushToJavaRenderingContext(ienv, renderingContext, &rc);
jclass resultClass = findClass(ienv, "net/osmand/NativeLibrary$RenderingGenerationResult");
@ -299,7 +297,7 @@ extern "C" JNIEXPORT jobject JNICALL Java_net_osmand_NativeLibrary_generateRende
if(encodePNG) {
SkImageEncoder* enc = SkImageEncoder::Create(SkImageEncoder::kPNG_Type);
SkDynamicMemoryWStream* stream = new SkDynamicMemoryWStream();
enc->encodeStream(stream, *bitmap, 50);
enc->encodeStream(stream, *bitmap, 80);
// clean previous data
free(bitmapData);
bitmapDataSize = stream->bytesWritten();
@ -310,6 +308,8 @@ extern "C" JNIEXPORT jobject JNICALL Java_net_osmand_NativeLibrary_generateRende
bitmapBuffer = ienv->NewDirectByteBuffer(bitmapData, bitmapDataSize);
// delete variables
delete canvas;
delete req;
delete bitmap;
/* Construct a result object */