more work on overlay renderer:

- moved text rendering to overlay
- added grid overlay
This commit is contained in:
Hannes Janetzek
2012-10-13 04:57:27 +02:00
parent 33d8865d7b
commit 4a06553ddc
33 changed files with 2050 additions and 1089 deletions

View File

@@ -14,7 +14,7 @@
*/
package org.oscim.renderer.layer;
public class Layer {
public abstract class Layer {
public final static byte LINE = 0;
public final static byte POLYGON = 1;
public final static byte WAYTEXT = 2;
@@ -34,4 +34,7 @@ public class Layer {
VertexPoolItem pool;
protected VertexPoolItem curItem;
protected void clear() {
}
}

View File

@@ -16,8 +16,6 @@ package org.oscim.renderer.layer;
import java.nio.ShortBuffer;
import org.oscim.renderer.TextureObject;
import android.util.Log;
public class Layers {
@@ -25,8 +23,8 @@ public class Layers {
public Layer layers;
public int lineOffset;
public Layer symbolLayers;
public int symbolOffset;
public Layer textureLayers;
public int texOffset;
private Layer mCurLayer;
@@ -57,10 +55,13 @@ public class Layers {
if (ret == null) {
if (type == Layer.LINE)
ret = new LineLayer(level);
else
else if (type == Layer.POLYGON)
ret = new PolygonLayer(level);
else
return null;
if (l == null) {
// insert at start
ret.next = layers;
layers = ret;
} else {
@@ -81,7 +82,9 @@ public class Layers {
private static int TEXTURE_VERTEX_SHORTS = 6;
public int getSize() {
int size = 0;
for (Layer l = layers; l != null; l = l.next) {
if (l.type == Layer.LINE)
size += l.verticesCnt * LINE_VERTEX_SHORTS;
@@ -90,7 +93,7 @@ public class Layers {
}
for (Layer l = symbolLayers; l != null; l = l.next) {
for (Layer l = textureLayers; l != null; l = l.next) {
size += l.verticesCnt * TEXTURE_VERTEX_SHORTS;
}
@@ -109,10 +112,10 @@ public class Layers {
lineOffset = sbuf.position() * 2; // * short-bytes
addLayerItems(sbuf, layers, Layer.LINE, 0);
symbolOffset = sbuf.position() * 2; // * short-bytes
texOffset = sbuf.position() * 2; // * short-bytes
for (Layer l = symbolLayers; l != null; l = l.next) {
SymbolLayer sl = (SymbolLayer) l;
for (Layer l = textureLayers; l != null; l = l.next) {
TextureLayer sl = (TextureLayer) l;
sl.compile(sbuf);
}
}
@@ -148,19 +151,25 @@ public class Layers {
}
public void clear() {
// FIXME collect pool and add as a whole
for (Layer l = layers; l != null; l = l.next) {
while (layers != null) {
Layer l = layers;
if (l.pool != null) {
VertexPool.release(l.pool);
l.pool = null;
l.curItem = null;
}
layers = layers.next;
}
for (Layer l = symbolLayers; l != null; l = l.next) {
SymbolLayer sl = (SymbolLayer) l;
if (sl.textures != null)
TextureObject.release(sl.textures);
while (textureLayers != null) {
textureLayers.clear();
// TextureLayer sl = (TextureLayer) textureLayers;
// if (sl.textures != null)
// TextureObject.release(sl.textures);
textureLayers = textureLayers.next;
}
}
}

View File

@@ -35,20 +35,11 @@ public final class LineLayer extends Layer {
public Line line;
public float width;
// boolean isOutline;
LineLayer(int layer) {
this.layer = layer;
this.type = Layer.LINE;
}
// LineLayer(int layer, Line line, float width, boolean outline) {
// this.layer = layer;
// this.width = width;
// this.line = line;
// // this.isOutline = outline;
// }
public void addOutline(LineLayer link) {
for (LineLayer l = outlines; l != null; l = l.outlines)
if (link == l)
@@ -272,6 +263,7 @@ public final class LineLayer extends Layer {
prevY = y;
x = nextX;
y = nextY;
boolean flip = false;
for (;;) {
if (ipos < pos + length) {
@@ -314,10 +306,15 @@ public final class LineLayer extends Layer {
ux = (ux / a);
uy = (uy / a);
// hack to avoid miter going to infinity
if (ux > 2.0f || ux < -2.0f || uy > 2.0f || uy < -2.0f) {
ux = -wy;
uy = wx;
// avoid miter going to infinity...
if (ux > 4.0f || ux < -4.0f || uy > 4.0f || uy < -4.0f) {
ux = vx - wx;
uy = vy - wy;
a = -wy * ux + wx * uy;
ux = (ux / a);
uy = (uy / a);
flip = !flip;
}
}
@@ -327,6 +324,10 @@ public final class LineLayer extends Layer {
ddx = (int) (ux * DIR_SCALE);
ddy = (int) (uy * DIR_SCALE);
if (flip) {
ddx *= -1;
ddy *= -1;
}
if (opos == VertexPoolItem.SIZE) {
si = si.next = VertexPool.get();
v = si.vertices;
@@ -382,6 +383,11 @@ public final class LineLayer extends Layer {
ddx = (int) (ux * DIR_SCALE);
ddy = (int) (uy * DIR_SCALE);
if (flip) {
ddx *= -1;
ddy *= -1;
}
v[opos++] = ox;
v[opos++] = oy;
v[opos++] = (short) (0 | ddx & DIR_MASK);
@@ -410,6 +416,11 @@ public final class LineLayer extends Layer {
dx = (short) (0 | ddx & DIR_MASK);
dy = (short) (0 | ddy & DIR_MASK);
if (flip) {
ddx *= -1;
ddy *= -1;
}
v[opos++] = ox;
v[opos++] = oy;
v[opos++] = dx;
@@ -427,6 +438,11 @@ public final class LineLayer extends Layer {
dx = (short) (2 | ddx & DIR_MASK);
dy = (short) (0 | ddy & DIR_MASK);
if (flip) {
ddx *= -1;
ddy *= -1;
}
v[opos++] = ox;
v[opos++] = oy;
v[opos++] = dx;
@@ -457,7 +473,10 @@ public final class LineLayer extends Layer {
ddx = (int) ((ux - vx) * DIR_SCALE);
ddy = (int) ((uy - vy) * DIR_SCALE);
if (flip) {
ddx *= -1;
ddy *= -1;
}
v[opos++] = ox;
v[opos++] = oy;
v[opos++] = (short) (0 | ddx & DIR_MASK);
@@ -472,6 +491,10 @@ public final class LineLayer extends Layer {
// add last vertex twice
ddx = (int) (-(ux + vx) * DIR_SCALE);
ddy = (int) (-(uy + vy) * DIR_SCALE);
if (flip) {
ddx *= -1;
ddy *= -1;
}
dx = (short) (2 | ddx & DIR_MASK);
dy = (short) (1 | ddy & DIR_MASK);

View File

@@ -17,12 +17,9 @@ package org.oscim.renderer.layer;
import java.nio.ShortBuffer;
import org.oscim.renderer.TextureObject;
import org.oscim.renderer.TextureRenderer;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.RectF;
import android.opengl.GLUtils;
import android.util.Log;
// TODO share one static texture for all poi map symabols
@@ -30,28 +27,17 @@ import android.util.Log;
public final class SymbolLayer extends TextureLayer {
private static String TAG = SymbolLayer.class.getSimpleName();
private final static int TEXTURE_WIDTH = 256;
private final static int TEXTURE_HEIGHT = 256;
private final static int TEXTURE_WIDTH = TextureObject.TEXTURE_WIDTH;
private final static int TEXTURE_HEIGHT = TextureObject.TEXTURE_HEIGHT;
private final static float SCALE = 8.0f;
private static short[] mVertices;
private static Bitmap mBitmap;
private static Canvas mCanvas;
private static int mBitmapFormat;
private static int mBitmapType;
SymbolItem symbols;
public SymbolLayer() {
if (mBitmap == null) {
mBitmap = Bitmap.createBitmap(TEXTURE_WIDTH, TEXTURE_HEIGHT,
Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mBitmapFormat = GLUtils.getInternalFormat(mBitmap);
mBitmapType = GLUtils.getType(mBitmap);
//
mVertices = new short[40 * 24];
}
if (mVertices == null)
mVertices = new short[TextureRenderer.MAX_ITEMS * 24];
}
public void addSymbol(SymbolItem item) {
@@ -73,19 +59,23 @@ public final class SymbolLayer extends TextureLayer {
}
private final static int LBIT_MASK = 0xfffffffe;
private final RectF mRect = new RectF();
// TODO ... reuse texture when only symbol position changed
@Override
public void compile(ShortBuffer sbuf) {
short numIndices = 0;
short offsetIndices = 0;
int pos = 0;
short buf[] = mVertices;
int bufLen = buf.length;
int advanceY = 0;
float x = 0;
float y = 0;
mBitmap.eraseColor(Color.TRANSPARENT);
Canvas canvas = TextureObject.getCanvas();
for (SymbolItem it = symbols; it != null;) {
@@ -103,35 +93,31 @@ public final class SymbolLayer extends TextureLayer {
if (y + height > TEXTURE_HEIGHT) {
Log.d(TAG, "reached max symbols");
// need to sync bitmap upload somehow???
TextureObject to = TextureObject.get();
TextureObject.uploadTexture(to, mBitmap,
mBitmapFormat, mBitmapType,
TEXTURE_WIDTH, TEXTURE_HEIGHT);
TextureObject to = TextureObject.uploadCanvas(offsetIndices, numIndices);
offsetIndices = numIndices;
to.next = textures;
textures = to;
sbuf.put(buf, 0, pos);
pos = 0;
x = 0;
y = 0;
advanceY = (int) height;
}
}
mRect.left = x;
mRect.top = y;
mRect.right = x + width;
mRect.bottom = y + height;
// Log.d("...", "draw " + x + " " + y + " " + width + " " + height);
mCanvas.drawBitmap(it.bitmap, null, mRect, null);
// mCanvas.drawBitmap(it.bitmap, x, y, null);
canvas.drawBitmap(it.bitmap, x, y, null);
float hw = width / 2.0f;
float hh = height / 2.0f;
short x1, x2, x3, x4, y1, y2, y3, y4;
x1 = x3 = (short) (SCALE * (-hw));
x2 = x4 = (short) (SCALE * (hw));
y1 = y3 = (short) (SCALE * (hh));
y2 = y4 = (short) (SCALE * (-hh));
short x1 = (short) (SCALE * (-hw));
short x2 = (short) (SCALE * (hw));
short y1 = (short) (SCALE * (hh));
short y2 = (short) (SCALE * (-hh));
short u1 = (short) (SCALE * x);
short v1 = (short) (SCALE * y);
@@ -157,40 +143,43 @@ public final class SymbolLayer extends TextureLayer {
buf[pos++] = y1;
buf[pos++] = u1;
buf[pos++] = v2;
// top-right
buf[pos++] = tx;
buf[pos++] = ty;
buf[pos++] = x2;
buf[pos++] = y3;
buf[pos++] = y1;
buf[pos++] = u2;
buf[pos++] = v2;
// bot-right
buf[pos++] = tx;
buf[pos++] = ty;
buf[pos++] = x4;
buf[pos++] = y4;
buf[pos++] = x2;
buf[pos++] = y2;
buf[pos++] = u2;
buf[pos++] = v1;
// bot-left
buf[pos++] = tx;
buf[pos++] = ty;
buf[pos++] = x3;
buf[pos++] = x1;
buf[pos++] = y2;
buf[pos++] = u1;
buf[pos++] = v1;
x += width + 1;
// six elements used to draw the four vertices
numIndices += 6;
// FIXME this does not work, need to draw bitmap on next
// texture...
if (pos == bufLen) {
sbuf.put(buf, 0, pos);
pos = 0;
}
x += width;
}
}
TextureObject to = TextureObject.get();
TextureObject.uploadTexture(to, mBitmap,
mBitmapFormat, mBitmapType,
TEXTURE_WIDTH, TEXTURE_HEIGHT);
TextureObject to = TextureObject.uploadCanvas(offsetIndices, numIndices);
to.next = textures;
textures = to;

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2012 Hannes Janetzek
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.renderer.layer;
import org.oscim.theme.renderinstruction.Text;
public class TextItem {
private static Object lock = new Object();
private static TextItem pool;
public static TextItem get() {
synchronized (lock) {
if (pool == null)
return new TextItem();
TextItem ti = pool;
pool = pool.next;
ti.next = null;
return ti;
}
}
public static void release(TextItem ti) {
if (ti == null)
return;
synchronized (lock) {
while (ti != null) {
TextItem next = ti.next;
ti.next = pool;
pool = ti;
ti = next;
}
}
}
public TextItem set(float x, float y, String string, Text text) {
this.x = x;
this.y = y;
this.string = string;
this.text = text;
this.width = text.paint.measureText(string);
return this;
}
public TextItem move(TextItem ti, float dx, float dy) {
this.x = dx + ti.x;
this.y = dy + ti.y;
this.string = ti.string;
this.text = ti.text;
this.width = ti.width;
return this;
}
public TextItem next;
public float x, y;
public String string;
public Text text;
public float width;
public short x1, y1, x2, y2;
// public byte placement
}

View File

@@ -14,9 +14,218 @@
*/
package org.oscim.renderer.layer;
import org.oscim.renderer.TextItem;
import java.nio.ShortBuffer;
import org.oscim.renderer.TextureObject;
import org.oscim.renderer.TextureRenderer;
import android.graphics.Canvas;
import android.util.FloatMath;
public final class TextLayer extends TextureLayer {
private static String TAG = SymbolLayer.class.getSimpleName();
private final static int TEXTURE_WIDTH = TextureObject.TEXTURE_WIDTH;
private final static int TEXTURE_HEIGHT = TextureObject.TEXTURE_HEIGHT;
private final static float SCALE = 8.0f;
private final static int LBIT_MASK = 0xfffffffe;
private static short[] mVertices;
private static int mFontPadX = 1;
private static int mFontPadY = 1;
TextItem labels;
public TextItem getLabels() {
return labels;
}
public TextLayer() {
if (mVertices == null)
mVertices = new short[TextureRenderer.MAX_ITEMS * 24];
}
public void addText(TextItem item) {
verticesCnt += 4;
TextItem it = labels;
for (; it != null; it = it.next) {
if (it.text == item.text) {
item.next = it.next;
it.next = item;
return;
}
}
item.next = labels;
labels = item;
}
@Override
public void compile(ShortBuffer sbuf) {
// int numLabel = 0;
short numIndices = 0;
short offsetIndices = 0;
int pos = 0;
short buf[] = mVertices;
int bufLen = buf.length;
int advanceY = 0;
float x = 0;
float y = 0;
float yy;
Canvas canvas = TextureObject.getCanvas();
for (TextItem it = labels; it != null; it = it.next) {
// numLabel++;
float width = it.width + 2 * mFontPadX;
float height = (int) (it.text.fontHeight) + 2 * mFontPadY + 0.5f;
if (height > advanceY)
advanceY = (int) height;
if (x + width > TEXTURE_WIDTH) {
x = 0;
y += advanceY;
advanceY = (int) (height + 0.5f);
if (y + height > TEXTURE_HEIGHT) {
// Log.d(TAG, "reached max labels " + numLabel);
// need to sync bitmap upload somehow???
TextureObject to = TextureObject.uploadCanvas(offsetIndices, numIndices);
offsetIndices = numIndices;
to.next = textures;
textures = to;
sbuf.put(buf, 0, pos);
pos = 0;
x = 0;
y = 0;
advanceY = (int) height;
// clear bitmap, TODO rotate two canvas to reduce the chance
// of having upload lock draing to the canvas?
canvas = TextureObject.getCanvas();
}
}
yy = y + (height - 1) - it.text.fontDescent - mFontPadY;
if (it.text.stroke != null)
canvas.drawText(it.string, x + it.width / 2, yy, it.text.stroke);
canvas.drawText(it.string, x + it.width / 2, yy, it.text.paint);
// FIXME !!!
if (width > TEXTURE_WIDTH)
width = TEXTURE_WIDTH;
float hw = width / 2.0f;
float hh = height / 2.0f;
short x1, x2, x3, x4, y1, y3, y2, y4;
if (it.text.caption) {
x1 = x3 = (short) (SCALE * -hw);
x2 = x4 = (short) (SCALE * hw);
y1 = y2 = (short) (SCALE * hh);
y3 = y4 = (short) (SCALE * -hh);
// x1 = x3 = (short) (0);
// x2 = x4 = (short) (SCALE * width);
} else {
float vx = it.x1 - it.x2;
float vy = it.y1 - it.y2;
float a = FloatMath.sqrt(vx * vx + vy * vy);
vx = vx / a;
vy = vy / a;
float ux = -vy;
float uy = vx;
float hh2 = hh + it.text.fontDescent / 2;
hh -= it.text.fontDescent / 2;
x1 = (short) (SCALE * (vx * hw - ux * hh));
y1 = (short) (SCALE * (vy * hw - uy * hh));
x2 = (short) (SCALE * (-vx * hw - ux * hh));
y2 = (short) (SCALE * (-vy * hw - uy * hh));
x4 = (short) (SCALE * (-vx * hw + ux * hh2));
y4 = (short) (SCALE * (-vy * hw + uy * hh2));
x3 = (short) (SCALE * (vx * hw + ux * hh2));
y3 = (short) (SCALE * (vy * hw + uy * hh2));
}
short u1 = (short) (SCALE * x);
short v1 = (short) (SCALE * y);
short u2 = (short) (SCALE * (x + width));
short v2 = (short) (SCALE * (y + height));
// add vertices
int tmp = (int) (SCALE * it.x) & LBIT_MASK;
short tx = (short) (tmp | (it.text.caption ? 1 : 0));
short ty = (short) (SCALE * it.y);
// top-left
buf[pos++] = tx;
buf[pos++] = ty;
buf[pos++] = x1;
buf[pos++] = y1;
buf[pos++] = u1;
buf[pos++] = v2;
// top-right
buf[pos++] = tx;
buf[pos++] = ty;
buf[pos++] = x2;
buf[pos++] = y2;
buf[pos++] = u2;
buf[pos++] = v2;
// bot-right
buf[pos++] = tx;
buf[pos++] = ty;
buf[pos++] = x4;
buf[pos++] = y4;
buf[pos++] = u2;
buf[pos++] = v1;
// bot-left
buf[pos++] = tx;
buf[pos++] = ty;
buf[pos++] = x3;
buf[pos++] = y3;
buf[pos++] = u1;
buf[pos++] = v1;
// six indices to draw the four vertices
numIndices += 6;
// FIXME this does not work, need to draw bitmap on next
// texture...
if (pos == bufLen) {
sbuf.put(buf, 0, pos);
pos = 0;
}
x += width + 1;
}
TextureObject to = TextureObject.uploadCanvas(offsetIndices, numIndices);
to.next = textures;
textures = to;
sbuf.put(buf, 0, pos);
// Log.d(TAG, "added labels " + numLabel);
}
@Override
protected void clear() {
TextureObject.release(textures);
TextItem.release(labels);
}
}

View File

@@ -14,9 +14,18 @@
*/
package org.oscim.renderer.layer;
import java.nio.ShortBuffer;
import org.oscim.renderer.TextureObject;
public abstract class TextureLayer extends Layer {
public TextureObject textures;
/**
* @param sbuf
* buffer to add vertices
*/
void compile(ShortBuffer sbuf) {
}
}