Bug 1128045. Gut TileLayer by moving most of the functionality into ScrollbarLayer. r=kats

ScrollbarLayer is the only class using this functionality. I'll move the mImage member into ScrollbarLayer in a follow up.
This commit is contained in:
Jeff Muizelaar 2015-01-30 15:56:22 -05:00
Родитель f2806a00df
Коммит 9e603c0955
2 изменённых файлов: 132 добавлений и 139 удалений

Просмотреть файл

@ -11,15 +11,23 @@ import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.RectF;
import android.opengl.GLES20;
import android.util.Log;
import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
public class ScrollbarLayer extends TileLayer {
private static final String LOGTAG = "GeckoScrollbarLayer";
public static final long FADE_DELAY = 500; // milliseconds before fade-out starts
private static final float FADE_AMOUNT = 0.03f; // how much (as a percent) the scrollbar should fade per frame
private final boolean mVertical;
private float mOpacity;
private final Rect mDirtyRect;
private IntSize mSize;
private int[] mTextureIDs;
// To avoid excessive GC, declare some objects here that would otherwise
// be created and destroyed frequently during draw().
@ -67,6 +75,8 @@ public class ScrollbarLayer extends TileLayer {
mBarRect = new Rect();
mCoords = new float[20];
mCapRectF = new RectF();
mDirtyRect = new Rect();
mSize = new IntSize(0, 0);
mTexHeight = scrollbarImage.getHeight();
mTexWidth = scrollbarImage.getWidth();
@ -294,4 +304,125 @@ public class ScrollbarLayer extends TileLayer {
}
dest.set(barStart, viewport.height() - mBarWidth, barEnd, viewport.height());
}
private void validateTexture() {
/* Calculate the ideal texture size. This must be a power of two if
* the texture is repeated or OpenGL ES 2.0 isn't supported, as
* OpenGL ES 2.0 is required for NPOT texture support (without
* extensions), but doesn't support repeating NPOT textures.
*
* XXX Currently, we don't pick a GLES 2.0 context, so always round.
*/
IntSize textureSize = mImage.getSize().nextPowerOfTwo();
if (!textureSize.equals(mSize)) {
mSize = textureSize;
// Delete the old texture
if (mTextureIDs != null) {
TextureReaper.get().add(mTextureIDs);
mTextureIDs = null;
// Free the texture immediately, so we don't incur a
// temporarily increased memory usage.
TextureReaper.get().reap();
}
}
}
@Override
protected void performUpdates(RenderContext context) {
super.performUpdates(context);
// Reallocate the texture if the size has changed
validateTexture();
// Don't do any work if the image has an invalid size.
if (!mImage.getSize().isPositive())
return;
// If we haven't allocated a texture, assume the whole region is dirty
if (mTextureIDs == null) {
uploadFullTexture();
} else {
uploadDirtyRect(mDirtyRect);
}
mDirtyRect.setEmpty();
}
private void uploadFullTexture() {
IntSize bufferSize = mImage.getSize();
uploadDirtyRect(new Rect(0, 0, bufferSize.width, bufferSize.height));
}
private void uploadDirtyRect(Rect dirtyRect) {
// If we have nothing to upload, just return for now
if (dirtyRect.isEmpty())
return;
// It's possible that the buffer will be null, check for that and return
ByteBuffer imageBuffer = mImage.getBuffer();
if (imageBuffer == null)
return;
if (mTextureIDs == null) {
mTextureIDs = new int[1];
GLES20.glGenTextures(mTextureIDs.length, mTextureIDs, 0);
}
int imageFormat = mImage.getFormat();
BufferedImageGLInfo glInfo = new BufferedImageGLInfo(imageFormat);
bindAndSetGLParameters();
// XXX TexSubImage2D is too broken to rely on on Adreno, and very slow
// on other chipsets, so we always upload the entire buffer.
IntSize bufferSize = mImage.getSize();
if (mSize.equals(bufferSize)) {
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, glInfo.internalFormat, mSize.width,
mSize.height, 0, glInfo.format, glInfo.type, imageBuffer);
} else {
// Our texture has been expanded to the next power of two.
// XXX We probably never want to take this path, so throw an exception.
throw new RuntimeException("Buffer/image size mismatch in TileLayer!");
}
}
private void bindAndSetGLParameters() {
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureIDs[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,
GLES20.GL_LINEAR);
int repeatMode = repeats() ? GLES20.GL_REPEAT : GLES20.GL_CLAMP_TO_EDGE;
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, repeatMode);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, repeatMode);
}
@Override
public void destroy() {
try {
if (mImage != null) {
mImage.destroy();
}
} catch (Exception ex) {
Log.e(LOGTAG, "error clearing buffers: ", ex);
}
}
protected int getTextureID() { return mTextureIDs[0]; }
protected boolean initialized() { return mImage != null && mTextureIDs != null; }
@Override
protected void finalize() throws Throwable {
try {
if (mTextureIDs != null)
TextureReaper.get().add(mTextureIDs);
} finally {
super.finalize();
}
}
}

Просмотреть файл

@ -7,9 +7,6 @@ package org.mozilla.gecko.gfx;
import android.graphics.Rect;
import android.opengl.GLES20;
import android.util.Log;
import java.nio.ByteBuffer;
/**
* Base class for tile layers, which encapsulate the logic needed to draw textured tiles in OpenGL
@ -18,10 +15,6 @@ import java.nio.ByteBuffer;
public abstract class TileLayer extends Layer {
private static final String LOGTAG = "GeckoTileLayer";
private final Rect mDirtyRect;
private IntSize mSize;
private int[] mTextureIDs;
protected final BufferedImage mImage;
public enum PaintMode { NORMAL, REPEAT, STRETCH };
@ -32,146 +25,15 @@ public abstract class TileLayer extends Layer {
mPaintMode = paintMode;
mImage = image;
mSize = new IntSize(0, 0);
mDirtyRect = new Rect();
}
protected boolean repeats() { return mPaintMode == PaintMode.REPEAT; }
protected boolean stretches() { return mPaintMode == PaintMode.STRETCH; }
protected int getTextureID() { return mTextureIDs[0]; }
protected boolean initialized() { return mImage != null && mTextureIDs != null; }
@Override
protected void finalize() throws Throwable {
try {
if (mTextureIDs != null)
TextureReaper.get().add(mTextureIDs);
} finally {
super.finalize();
}
}
public void destroy() {
try {
if (mImage != null) {
mImage.destroy();
}
} catch (Exception ex) {
Log.e(LOGTAG, "error clearing buffers: ", ex);
}
}
public abstract void destroy();
public void setPaintMode(PaintMode mode) {
mPaintMode = mode;
}
/**
* Invalidates the entire buffer so that it will be uploaded again. Only valid inside a
* transaction.
*/
public void invalidate() {
if (!inTransaction())
throw new RuntimeException("invalidate() is only valid inside a transaction");
IntSize bufferSize = mImage.getSize();
mDirtyRect.set(0, 0, bufferSize.width, bufferSize.height);
}
private void validateTexture() {
/* Calculate the ideal texture size. This must be a power of two if
* the texture is repeated or OpenGL ES 2.0 isn't supported, as
* OpenGL ES 2.0 is required for NPOT texture support (without
* extensions), but doesn't support repeating NPOT textures.
*
* XXX Currently, we don't pick a GLES 2.0 context, so always round.
*/
IntSize textureSize = mImage.getSize().nextPowerOfTwo();
if (!textureSize.equals(mSize)) {
mSize = textureSize;
// Delete the old texture
if (mTextureIDs != null) {
TextureReaper.get().add(mTextureIDs);
mTextureIDs = null;
// Free the texture immediately, so we don't incur a
// temporarily increased memory usage.
TextureReaper.get().reap();
}
}
}
@Override
protected void performUpdates(RenderContext context) {
super.performUpdates(context);
// Reallocate the texture if the size has changed
validateTexture();
// Don't do any work if the image has an invalid size.
if (!mImage.getSize().isPositive())
return;
// If we haven't allocated a texture, assume the whole region is dirty
if (mTextureIDs == null) {
uploadFullTexture();
} else {
uploadDirtyRect(mDirtyRect);
}
mDirtyRect.setEmpty();
}
private void uploadFullTexture() {
IntSize bufferSize = mImage.getSize();
uploadDirtyRect(new Rect(0, 0, bufferSize.width, bufferSize.height));
}
private void uploadDirtyRect(Rect dirtyRect) {
// If we have nothing to upload, just return for now
if (dirtyRect.isEmpty())
return;
// It's possible that the buffer will be null, check for that and return
ByteBuffer imageBuffer = mImage.getBuffer();
if (imageBuffer == null)
return;
if (mTextureIDs == null) {
mTextureIDs = new int[1];
GLES20.glGenTextures(mTextureIDs.length, mTextureIDs, 0);
}
int imageFormat = mImage.getFormat();
BufferedImageGLInfo glInfo = new BufferedImageGLInfo(imageFormat);
bindAndSetGLParameters();
// XXX TexSubImage2D is too broken to rely on on Adreno, and very slow
// on other chipsets, so we always upload the entire buffer.
IntSize bufferSize = mImage.getSize();
if (mSize.equals(bufferSize)) {
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, glInfo.internalFormat, mSize.width,
mSize.height, 0, glInfo.format, glInfo.type, imageBuffer);
} else {
// Our texture has been expanded to the next power of two.
// XXX We probably never want to take this path, so throw an exception.
throw new RuntimeException("Buffer/image size mismatch in TileLayer!");
}
}
private void bindAndSetGLParameters() {
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureIDs[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,
GLES20.GL_LINEAR);
int repeatMode = repeats() ? GLES20.GL_REPEAT : GLES20.GL_CLAMP_TO_EDGE;
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, repeatMode);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, repeatMode);
}
}