API change: RenderLayer

- use setReady(bool) instead of public field
- no more implicit call to compile() when 'newData' is true
  -> call compile() when needed
- make RenderLayer function protected
This commit is contained in:
Hannes Janetzek 2013-09-11 00:31:28 +02:00
parent eb1e8b63c5
commit 0aa7818731
16 changed files with 69 additions and 81 deletions

View File

@ -622,7 +622,7 @@ class TextRenderLayer extends BasicRenderLayer {
layers.textureLayers = mCurLayer.l; layers.textureLayers = mCurLayer.l;
mMapPosition = mCurLayer.pos; mMapPosition = mCurLayer.pos;
this.newData = true; compile();
} }
//if (!mHolding) //if (!mHolding)

View File

@ -104,7 +104,7 @@ public abstract class ItemizedOverlay<Item extends OverlayItem> extends Overlay
if (mItems == null) { if (mItems == null) {
if (layers.textureLayers != null) { if (layers.textureLayers != null) {
layers.clear(); layers.clear();
newData = true; compile();
} }
return; return;
} }
@ -183,7 +183,8 @@ public abstract class ItemizedOverlay<Item extends OverlayItem> extends Overlay
} }
mSymbolLayer.prepare(); mSymbolLayer.prepare();
layers.textureLayers = mSymbolLayer; layers.textureLayers = mSymbolLayer;
newData = true;
compile();
} }
@Override @Override

View File

@ -127,7 +127,7 @@ public class PathOverlay extends Layer {
if (size == 0) { if (size == 0) {
if (layers.baseLayers != null) { if (layers.baseLayers != null) {
layers.clear(); layers.clear();
newData = true; compile();
} }
return; return;
} }
@ -224,7 +224,7 @@ public class PathOverlay extends Layer {
// items are placed relative to scale 1 // items are placed relative to scale 1
mMapPosition.scale = 1 << z; mMapPosition.scale = 1 << z;
newData = true; compile();
} }
private int addPoint(float[] points, int i, int x, int y) { private int addPoint(float[] points, int i, int x, int y) {

View File

@ -49,7 +49,7 @@ public class TileRenderLayer extends RenderLayer {
} }
@Override @Override
public void update(MapPosition pos, boolean positionChanged, Matrices m) { protected void update(MapPosition pos, boolean positionChanged, Matrices m) {
if (mAlpha == 0){ if (mAlpha == 0){
mTileManager.releaseTiles(mDrawTiles); mTileManager.releaseTiles(mDrawTiles);
@ -84,12 +84,12 @@ public class TileRenderLayer extends RenderLayer {
} }
@Override @Override
public void compile() { protected void compile() {
} }
@Override @Override
public void render(MapPosition pos, Matrices m) { protected void render(MapPosition pos, Matrices m) {
} }

View File

@ -317,21 +317,10 @@ public class GLRenderer {
/* update layers */ /* update layers */
RenderLayer[] layers = mMap.getLayers().getRenderLayers(); RenderLayer[] layers = mMap.getLayers().getRenderLayers();
for (int i = 0, n = layers.length; i < n; i++)
layers[i].update(pos, changed, mMatrices);
/* compile layers */
for (int i = 0, n = layers.length; i < n; i++) { for (int i = 0, n = layers.length; i < n; i++) {
RenderLayer renderLayer = layers[i]; RenderLayer renderLayer = layers[i];
if (renderLayer.newData) { renderLayer.update(pos, changed, mMatrices);
renderLayer.compile();
renderLayer.newData = false;
}
}
for (int i = 0, n = layers.length; i < n; i++) {
RenderLayer renderLayer = layers[i];
if (renderLayer.isReady) if (renderLayer.isReady)
renderLayer.render(mMapPosition, mMatrices); renderLayer.render(mMapPosition, mMatrices);
@ -450,6 +439,10 @@ public class GLRenderer {
return mQuadIndicesID; return mQuadIndicesID;
} }
/**
* Get VBO ID for a simple quad. Handy for simple custom RenderLayers
* Vertices: { -1, -1, -1, 1, 1, -1, 1, 1 }
*/
public static int getQuadVertexVBO() { public static int getQuadVertexVBO() {
return mQuadVerticesID; return mQuadVerticesID;
} }

View File

@ -27,11 +27,16 @@ public abstract class RenderLayer {
*/ */
protected MapPosition mMapPosition; protected MapPosition mMapPosition;
/** flag to set when data is ready for (re)compilation. */
public boolean newData;
/** flag to set when layer is ready for rendering */ /** flag to set when layer is ready for rendering */
public boolean isReady; boolean isReady;
/**
* Set 'ready for render' state when layer data is ready for rendering.
* This will
* */
protected void setReady(boolean ready){
isReady = ready;
}
public RenderLayer() { public RenderLayer() {
mMapPosition = new MapPosition(); mMapPosition = new MapPosition();
@ -40,8 +45,7 @@ public abstract class RenderLayer {
/** /**
* ////////////////////// GLRender Thread /////////////////////////// * ////////////////////// GLRender Thread ///////////////////////////
* 1. Called first by GLRenderer: * 1. Called first by GLRenderer:
* Update the layer state here. Set 'this.newData = true' when * Update the layer state here.
* 'compile()' should be called before next call to 'render()'
* *
* @param position current MapPosition * @param position current MapPosition
* @param changed * @param changed
@ -49,14 +53,15 @@ public abstract class RenderLayer {
* @param matrices contains the current view- and projection-matrices * @param matrices contains the current view- and projection-matrices
* and 'mvp' matrix for temporary use. * and 'mvp' matrix for temporary use.
*/ */
public abstract void update(MapPosition position, boolean changed, protected abstract void update(MapPosition position, boolean changed,
Matrices matrices); Matrices matrices);
/** /**
* 2. Compile vertex buffers and upload textures for drawing: * 2. Compile vertex buffers and upload textures for drawing.
* Set 'this.isReady = true' when things are ready for 'render()' * Not strictly required but useful when subclassing a RenderLayer.
* Should only be called from update()
*/ */
public abstract void compile(); protected abstract void compile();
/** /**
* 3. Draw layer: * 3. Draw layer:
@ -66,7 +71,7 @@ public abstract class RenderLayer {
* 'matrices.mvp' is for temporary use to build the model- * 'matrices.mvp' is for temporary use to build the model-
* view-projection to set as uniform. * view-projection to set as uniform.
*/ */
public abstract void render(MapPosition position, Matrices matrices); protected abstract void render(MapPosition position, Matrices matrices);
/** /**
* Utility: Set matrices.mvp matrix relative to the difference of current * Utility: Set matrices.mvp matrix relative to the difference of current

View File

@ -45,7 +45,7 @@ public abstract class BasicRenderLayer extends RenderLayer {
* Render all 'layers' * Render all 'layers'
*/ */
@Override @Override
public synchronized void render(MapPosition curPos, Matrices m) { protected synchronized void render(MapPosition curPos, Matrices m) {
MapPosition pos = mMapPosition; MapPosition pos = mMapPosition;
float div = FastMath.pow(pos.zoomLevel - curPos.zoomLevel); float div = FastMath.pow(pos.zoomLevel - curPos.zoomLevel);
@ -94,12 +94,12 @@ public abstract class BasicRenderLayer extends RenderLayer {
} }
@Override @Override
public void compile() { protected void compile() {
int newSize = layers.getSize(); int newSize = layers.getSize();
if (newSize <= 0) { if (newSize <= 0) {
BufferObject.release(layers.vbo); BufferObject.release(layers.vbo);
layers.vbo = null; layers.vbo = null;
isReady = false; setReady(false);
return; return;
} }
@ -107,6 +107,6 @@ public abstract class BasicRenderLayer extends RenderLayer {
layers.vbo = BufferObject.get(GL20.GL_ARRAY_BUFFER, newSize); layers.vbo = BufferObject.get(GL20.GL_ARRAY_BUFFER, newSize);
if (GLRenderer.uploadLayers(layers, newSize, true)) if (GLRenderer.uploadLayers(layers, newSize, true))
isReady = true; setReady(true);
} }
} }

View File

@ -57,7 +57,7 @@ public class BitmapRenderLayer extends BasicRenderLayer {
} }
@Override @Override
public synchronized void update(MapPosition pos, boolean changed, Matrices m) { protected synchronized void update(MapPosition pos, boolean changed, Matrices m) {
if (!initialized) { if (!initialized) {
layers.clear(); layers.clear();
@ -65,17 +65,17 @@ public class BitmapRenderLayer extends BasicRenderLayer {
l.setBitmap(mBitmap, mWidth, mHeight); l.setBitmap(mBitmap, mWidth, mHeight);
layers.textureLayers = l; layers.textureLayers = l;
newData = true; mUpdateBitmap = true;
} }
if (mUpdateBitmap) { if (mUpdateBitmap) {
newData = true;
mUpdateBitmap = false; mUpdateBitmap = false;
compile();
} }
} }
@Override @Override
public synchronized void compile() { protected synchronized void compile() {
if (mBitmap == null) if (mBitmap == null)
return; return;
@ -85,7 +85,7 @@ public class BitmapRenderLayer extends BasicRenderLayer {
} }
@Override @Override
public synchronized void render(MapPosition pos, Matrices m) { protected synchronized void render(MapPosition pos, Matrices m) {
m.useScreenCoordinates(false, 8); m.useScreenCoordinates(false, 8);
BitmapRenderer.draw(layers.textureLayers, m, 1, 1); BitmapRenderer.draw(layers.textureLayers, m, 1, 1);
} }

View File

@ -60,37 +60,33 @@ public class CustomRenderLayer extends RenderLayer {
// ---------- everything below runs in GLRender Thread ---------- // ---------- everything below runs in GLRender Thread ----------
@Override @Override
public void update(MapPosition pos, boolean changed, Matrices matrices) { protected void update(MapPosition pos, boolean changed, Matrices matrices) {
if (!mInitialized) { if (!mInitialized) {
if (!init()) if (!init())
return; return;
mInitialized = true; mInitialized = true;
// tell GLRender to call 'compile' when data has changed
newData = true;
// fix current MapPosition // fix current MapPosition
mMapPosition.copy(pos); mMapPosition.copy(pos);
compile();
} }
} }
@Override @Override
public void compile() { protected void compile() {
// modify mVerticesData and put in FloatBuffer // modify mVerticesData and put in FloatBuffer
mVertices.clear(); mVertices.clear();
mVertices.put(mVerticesData); mVertices.put(mVerticesData);
mVertices.flip(); mVertices.flip();
newData = false; setReady(true);
// tell GLRender to call 'render'
isReady = true;
} }
@Override @Override
public void render(MapPosition pos, Matrices m) { protected void render(MapPosition pos, Matrices m) {
// Use the program object // Use the program object
GLState.useProgram(mProgramObject); GLState.useProgram(mProgramObject);

View File

@ -54,16 +54,14 @@ public class CustomRenderLayer2 extends RenderLayer {
float mCellScale = 60 * GLRenderer.COORD_SCALE; float mCellScale = 60 * GLRenderer.COORD_SCALE;
@Override @Override
public void update(MapPosition pos, boolean changed, Matrices matrices) { protected void update(MapPosition pos, boolean changed, Matrices matrices) {
if (!mInitialized) { if (!mInitialized) {
if (!init()) if (!init())
return; return;
mInitialized = true; mInitialized = true;
// tell GLRender to call 'compile' when data has changed compile();
newData = true;
// fix current MapPosition // fix current MapPosition
//mMapPosition.setPosition(53.1, 8.8); //mMapPosition.setPosition(53.1, 8.8);
@ -78,7 +76,7 @@ public class CustomRenderLayer2 extends RenderLayer {
} }
@Override @Override
public void compile() { protected void compile() {
float[] vertices = new float[12]; float[] vertices = new float[12];
@ -92,14 +90,12 @@ public class CustomRenderLayer2 extends RenderLayer {
mVBO = BufferObject.get(GL20.GL_ARRAY_BUFFER, 0); mVBO = BufferObject.get(GL20.GL_ARRAY_BUFFER, 0);
mVBO.loadBufferData(buf, 12 * 4); mVBO.loadBufferData(buf, 12 * 4);
newData = false;
// tell GLRender to call 'render' setReady(true);
isReady = true;
} }
@Override @Override
public void render(MapPosition pos, Matrices m) { protected void render(MapPosition pos, Matrices m) {
// Use the program object // Use the program object
GLState.useProgram(mProgramObject); GLState.useProgram(mProgramObject);

View File

@ -88,7 +88,7 @@ public class ExtrusionRenderLayer extends RenderLayer {
} }
@Override @Override
public void update(MapPosition pos, boolean changed, Matrices matrices) { protected void update(MapPosition pos, boolean changed, Matrices matrices) {
mMapPosition.copy(pos); mMapPosition.copy(pos);
if (!initialized && !initShader()) if (!initialized && !initShader())
@ -98,7 +98,7 @@ public class ExtrusionRenderLayer extends RenderLayer {
return; return;
if (mAlpha == 0 || pos.zoomLevel < 16) { if (mAlpha == 0 || pos.zoomLevel < 16) {
isReady = false; setReady(false);
return; return;
} }
@ -108,8 +108,7 @@ public class ExtrusionRenderLayer extends RenderLayer {
if (mTileSet.cnt == 0) { if (mTileSet.cnt == 0) {
mTileLayer.releaseTiles(mTileSet); mTileLayer.releaseTiles(mTileSet);
setReady(false);
isReady = false;
return; return;
} }
@ -155,14 +154,15 @@ public class ExtrusionRenderLayer extends RenderLayer {
} }
mTileCnt = activeTiles; mTileCnt = activeTiles;
isReady = activeTiles > 0;
if (!isReady) if (activeTiles > 0)
setReady(true);
else
mTileLayer.releaseTiles(mTileSet); mTileLayer.releaseTiles(mTileSet);
} }
@Override @Override
public void compile() { protected void compile() {
} }
@ -178,7 +178,7 @@ public class ExtrusionRenderLayer extends RenderLayer {
//private final float[] mVPMatrix = new float[16]; //private final float[] mVPMatrix = new float[16];
@Override @Override
public void render(MapPosition pos, Matrices m) { protected void render(MapPosition pos, Matrices m) {
// TODO one could render in one pass to texture and then draw the texture // TODO one could render in one pass to texture and then draw the texture
// with alpha... might be faster and would allow postprocessing outlines. // with alpha... might be faster and would allow postprocessing outlines.

View File

@ -102,7 +102,7 @@ public class GridRenderLayer extends BasicRenderLayer {
} }
@Override @Override
public void update(MapPosition pos, boolean changed, Matrices m) { protected void update(MapPosition pos, boolean changed, Matrices m) {
// scale coordinates relative to current 'zoom-level' to // scale coordinates relative to current 'zoom-level' to
// get the position as the nearest tile coordinate // get the position as the nearest tile coordinate
@ -129,7 +129,6 @@ public class GridRenderLayer extends BasicRenderLayer {
mLineLayer.clear(); mLineLayer.clear();
mLineLayer.addLine(mLines); mLineLayer.addLine(mLines);
// tell GLRender to compile new layer data. compile();
this.newData = true;
} }
} }

View File

@ -660,7 +660,7 @@ public class TextRenderLayer extends BasicRenderLayer {
mMapPosition = mTmpPos; mMapPosition = mTmpPos;
mTmpPos = tmp; mTmpPos = tmp;
this.newData = true; compile();
} }
// if (!mHolding) // if (!mHolding)

View File

@ -107,18 +107,18 @@ public class AtlasRenderLayer extends BasicRenderLayer {
tl.prepare(); tl.prepare();
TextItem.pool.releaseAll(tl.labels); TextItem.pool.releaseAll(tl.labels);
this.newData = true;
} }
boolean initial = true; boolean initial = true;
@Override @Override
public void update(MapPosition pos, boolean changed, Matrices m) { protected void update(MapPosition pos, boolean changed, Matrices m) {
if (initial) { if (initial) {
mMapPosition.copy(pos); mMapPosition.copy(pos);
this.initial = false; initial = false;
compile();
} }
} }
} }

View File

@ -40,16 +40,14 @@ public class SymbolRenderLayer extends BasicRenderLayer {
} }
l.addSymbol(it); l.addSymbol(it);
// compile layer on next frame
newData = true;
} }
@Override @Override
public void update(MapPosition position, boolean changed, Matrices matrices) { protected void update(MapPosition position, boolean changed, Matrices matrices) {
if (initialize){ if (initialize){
initialize = false; initialize = false;
mMapPosition.copy(position); mMapPosition.copy(position);
compile();
} }
} }
} }

View File

@ -115,7 +115,7 @@ public class TestRenderLayer extends BasicRenderLayer {
} }
@Override @Override
public synchronized void update(MapPosition curPos, boolean positionChanged, protected synchronized void update(MapPosition curPos, boolean positionChanged,
Matrices matrices) { Matrices matrices) {
// keep position constant (or update layer relative to new position) // keep position constant (or update layer relative to new position)
//mMapPosition.copy(curPos); //mMapPosition.copy(curPos);
@ -129,7 +129,7 @@ public class TestRenderLayer extends BasicRenderLayer {
// pass layers to be uploaded and drawn to GL Thread // pass layers to be uploaded and drawn to GL Thread
// afterwards never modify 'layers' outside of this function! // afterwards never modify 'layers' outside of this function!
newData = true; compile();
} }
} }