Bug 732013 - Fix overdraw in LayerRenderer. r=kats

Add an optional rectangular mask to SingleTileLayer so that we can reduce the
amount of overdraw when drawing the background and checkerboard layers.
This commit is contained in:
Chris Lord 2012-03-01 19:43:14 +00:00
parent 3daa99ec38
commit e7376feda5
2 changed files with 75 additions and 32 deletions

View File

@ -574,6 +574,7 @@ public class LayerRenderer implements GLSurfaceView.Renderer {
/** This function is invoked via JNI; be careful when modifying signature. */
public void drawBackground() {
/* Draw the background. */
mBackgroundLayer.setMask(getPageRect());
mBackgroundLayer.draw(mScreenContext);
/* Draw the drop shadow, if we need to. */
@ -583,8 +584,19 @@ public class LayerRenderer implements GLSurfaceView.Renderer {
if (!untransformedPageRect.contains(mView.getController().getViewport()))
mShadowLayer.draw(mPageContext);
/* Find the area the root layer will render into, to mask the scissor rect */
Rect rootMask = null;
Layer rootLayer = mView.getController().getRoot();
if (rootLayer != null) {
RectF rootBounds = rootLayer.getBounds(mPageContext);
rootBounds.offset(-mPageContext.viewport.left, -mPageContext.viewport.top);
rootMask = new Rect();
rootBounds.roundOut(rootMask);
}
/* Draw the checkerboard. */
setScissorRect();
mCheckerboardLayer.setMask(rootMask);
mCheckerboardLayer.draw(mScreenContext);
GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
}
@ -596,9 +608,7 @@ public class LayerRenderer implements GLSurfaceView.Renderer {
return;
}
setScissorRect();
rootLayer.draw(mPageContext);
GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
}
/** This function is invoked via JNI; be careful when modifying signature. */

View File

@ -45,6 +45,8 @@ import org.mozilla.gecko.gfx.LayerController;
import org.mozilla.gecko.gfx.TileLayer;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.RegionIterator;
import android.opengl.GLES20;
import android.util.Log;
import java.nio.FloatBuffer;
@ -56,12 +58,23 @@ import javax.microedition.khronos.opengles.GL10;
* TODO: Repeating textures really should be their own type of layer.
*/
public class SingleTileLayer extends TileLayer {
private static final String LOGTAG = "GeckoSingleTileLayer";
private Rect mMask;
public SingleTileLayer(CairoImage image) { this(false, image); }
public SingleTileLayer(boolean repeat, CairoImage image) {
super(repeat, image);
}
/**
* Set an area to mask out when rendering.
*/
public void setMask(Rect aMaskRect) {
mMask = aMaskRect;
}
@Override
public void draw(RenderContext context) {
// mTextureIDs may be null here during startup if Layer.java's draw method
@ -69,8 +82,6 @@ public class SingleTileLayer extends TileLayer {
if (!initialized())
return;
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, getTextureID());
RectF bounds;
int[] cropRect;
Rect position = getPosition();
@ -86,9 +97,29 @@ public class SingleTileLayer extends TileLayer {
cropRect = new int[] { 0, 0, position.width(), position.height() };
}
float height = bounds.height();
float left = bounds.left - viewport.left;
float top = viewport.height() - (bounds.top + height - viewport.top);
Rect intBounds = new Rect();
bounds.roundOut(intBounds);
Region maskedBounds = new Region(intBounds);
if (mMask != null) {
maskedBounds.op(mMask, Region.Op.DIFFERENCE);
if (maskedBounds.isEmpty())
return;
}
// XXX Possible optimisation here, form this array so we can draw it in
// a single call.
RegionIterator i = new RegionIterator(maskedBounds);
for (Rect subRect = new Rect(); i.next(subRect);) {
// Compensate for rounding errors at the edge of the tile caused by
// the roundOut above
RectF subRectF = new RectF(Math.max(bounds.left, (float)subRect.left),
Math.max(bounds.top, (float)subRect.top),
Math.min(bounds.right, (float)subRect.right),
Math.min(bounds.bottom, (float)subRect.bottom));
float height = subRectF.height();
float left = subRectF.left - viewport.left;
float top = viewport.height() - (subRectF.top + height - viewport.top);
float[] coords = {
//x, y, z, texture_x, texture_y
@ -98,10 +129,10 @@ public class SingleTileLayer extends TileLayer {
left/viewport.width(), (top+height)/viewport.height(), 0,
cropRect[0]/(float)position.width(), cropRect[3]/(float)position.height(),
(left+bounds.width())/viewport.width(), top/viewport.height(), 0,
(left+subRectF.width())/viewport.width(), top/viewport.height(), 0,
cropRect[2]/(float)position.width(), cropRect[1]/(float)position.height(),
(left+bounds.width())/viewport.width(), (top+height)/viewport.height(), 0,
(left+subRectF.width())/viewport.width(), (top+height)/viewport.height(), 0,
cropRect[2]/(float)position.width(), cropRect[3]/(float)position.height()
};
@ -109,8 +140,9 @@ public class SingleTileLayer extends TileLayer {
int positionHandle = context.positionHandle;
int textureHandle = context.textureHandle;
// Make sure we are at position zero in the buffer in case other draw methods did not clean
// up after themselves
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, getTextureID());
// Make sure we are at position zero in the buffer
coordBuffer.position(0);
coordBuffer.put(coords);
@ -124,4 +156,5 @@ public class SingleTileLayer extends TileLayer {
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
}
}
}