LODroid: Make SubTile hold the buffer for a tile

Previously there was only one buffer which contained all the
tiles currently visible. This is inflexible - we need to control
each tile individually. This commit introduces SubTile object,
which is the holder of the buffer for each individual tile.

Change-Id: I511f13dc7fad7c3c04f3d7f23b3abc97a3cc2268
This commit is contained in:
Tomaž Vajngerl
2014-06-30 09:02:22 +02:00
parent 7164161de9
commit 3799a6f3a8
6 changed files with 66 additions and 137 deletions

View File

@@ -40,19 +40,23 @@ public class LOKitThread extends Thread {
}
Rect bufferRect = application.getLayerClient().beginDrawing(originalBitmap.getWidth(), originalBitmap.getHeight(), 256, 256, metadata);
if (bufferRect == null)
if (bufferRect == null) {
return false;
}
int x = 0;
int y = 0;
ByteBuffer buffer = application.getLayerClient().lockBuffer();
for (Integer i = 1; i <= 9; i++) {
String imageName = "d" + i;
Bitmap bitmap = application.getLayerClient().getLayerController().getDrawable(imageName);
bitmap.copyPixelsToBuffer(buffer.asIntBuffer());
buffer.position(buffer.position() + bitmap.getByteCount());
application.getLayerClient().addTile(bitmap, x, y);
x += TILE_SIZE;
if (x > originalBitmap.getWidth()) {
x = 0;
y += TILE_SIZE;
}
}
buffer.position(0);
application.getLayerClient().unlockBuffer();
application.getLayerClient().endDrawing(0, 0, originalBitmap.getWidth(), originalBitmap.getHeight());
application.runOnUiThread(new Runnable() {

View File

@@ -72,7 +72,6 @@ public abstract class GeckoLayerClient extends LayerClient implements GeckoEvent
private static final long MIN_VIEWPORT_CHANGE_DELAY = 25L;
private static Pattern sColorPattern;
protected IntSize mScreenSize;
protected IntSize mBufferSize;
protected Layer mTileLayer;
/* The viewport that Gecko is currently displaying. */
protected ViewportMetrics mGeckoViewport;
@@ -94,7 +93,6 @@ public abstract class GeckoLayerClient extends LayerClient implements GeckoEvent
public GeckoLayerClient(Context context) {
mScreenSize = new IntSize(0, 0);
mBufferSize = new IntSize(0, 0);
}
// Parses a color from an RGB triple of the form "rgb([0-9]+, [0-9]+, [0-9]+)". If the color
@@ -340,7 +338,7 @@ public abstract class GeckoLayerClient extends LayerClient implements GeckoEvent
private void adjustViewport() {
ViewportMetrics viewportMetrics = new ViewportMetrics(getLayerController().getViewportMetrics());
PointF viewportOffset = viewportMetrics.getOptimumViewportOffset(mBufferSize);
PointF viewportOffset = viewportMetrics.getOptimumViewportOffset(getBufferSize());
viewportMetrics.setViewportOffset(viewportOffset);
viewportMetrics.setViewport(viewportMetrics.getClampedViewport());
@@ -359,7 +357,8 @@ public abstract class GeckoLayerClient extends LayerClient implements GeckoEvent
mUpdateViewportOnEndDraw = true;
// Redraw everything.
Rect rect = new Rect(0, 0, mBufferSize.width, mBufferSize.height);
IntSize bufferSize = getBufferSize();
Rect rect = new Rect(0, 0, bufferSize.width, bufferSize.height);
LOKitShell.sendEvent(LOEvent.draw(rect));
} else if ("Viewport:UpdateLater".equals(event)) {
Log.e(LOGTAG, "### Java side Viewport:UpdateLater()!");
@@ -373,14 +372,6 @@ public abstract class GeckoLayerClient extends LayerClient implements GeckoEvent
render();
}
public int getWidth() {
return mBufferSize.width;
}
public int getHeight() {
return mBufferSize.height;
}
public ViewportMetrics getGeckoViewportMetrics() {
if (mGeckoViewport != null)
return new ViewportMetrics(mGeckoViewport);

View File

@@ -68,28 +68,18 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
private int mFormat;
private IntSize mViewportSize;
private ByteBuffer mBuffer;
private CairoImage mCairoImage;
private IntSize mBufferSize;
private static final IntSize TILE_SIZE = new IntSize(256, 256);
public GeckoSoftwareLayerClient(Context context) {
super(context);
mBufferSize = new IntSize(0,0);
mFormat = CairoImage.FORMAT_ARGB32;
mCairoImage = new CairoImage() {
@Override
public ByteBuffer getBuffer() { return mBuffer; }
@Override
public IntSize getSize() { return mBufferSize; }
@Override
public int getFormat() { return mFormat; }
};
}
protected void finalize() throws Throwable {
/*protected void finalize() throws Throwable {
try {
if (mBuffer != null)
LOKitShell.freeDirectBuffer(mBuffer);
@@ -97,7 +87,7 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
} finally {
super.finalize();
}
}
}*/
public void setLayerController(LayerController layerController) {
super.setLayerController(layerController);
@@ -116,7 +106,7 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
return false;
Log.i(LOGTAG, "Creating MultiTileLayer");
mTileLayer = new MultiTileLayer(mCairoImage, TILE_SIZE);
mTileLayer = new MultiTileLayer(TILE_SIZE);
getLayerController().setRoot(mTileLayer);
@@ -149,21 +139,6 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
// If the window size has changed, reallocate the buffer to match.
if (mBufferSize.width != width || mBufferSize.height != height) {
mBufferSize = new IntSize(width, height);
// Reallocate the buffer if necessary
if (mTileLayer instanceof MultiTileLayer) {
int bpp = CairoUtils.bitsPerPixelForCairoFormat(mFormat) / 8;
int size = mBufferSize.getArea() * bpp;
if (mBuffer == null || mBuffer.capacity() != size) {
// Free the old buffer
if (mBuffer != null) {
LOKitShell.freeDirectBuffer(mBuffer);
mBuffer = null;
}
mBuffer = LOKitShell.allocateDirectBuffer(size);
}
}
}
return bufferRect;
@@ -176,7 +151,7 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
}
}
private void copyPixelsFromMultiTileLayer(Bitmap target) {
/*private void copyPixelsFromMultiTileLayer(Bitmap target) {
Canvas c = new Canvas(target);
ByteBuffer tileBuffer = mBuffer.slice();
int bpp = CairoUtils.bitsPerPixelForCairoFormat(mFormat) / 8;
@@ -201,7 +176,7 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
tileBuffer = tileBuffer.slice();
}
}
}
}*/
@Override
protected void tileLayerUpdated() {
@@ -215,7 +190,7 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
// Begin a tile transaction, otherwise the buffer can be destroyed while
// we're reading from it.
beginTransaction(mTileLayer);
/*beginTransaction(mTileLayer);
try {
if (mBuffer == null || mBufferSize.width <= 0 || mBufferSize.height <= 0)
return null;
@@ -223,8 +198,7 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
Bitmap b = null;
if (mTileLayer instanceof MultiTileLayer) {
b = Bitmap.createBitmap(mBufferSize.width, mBufferSize.height,
CairoUtils.cairoFormatTobitmapConfig(mFormat));
b = Bitmap.createBitmap(mBufferSize.width, mBufferSize.height,CairoUtils.cairoFormatTobitmapConfig(mFormat));
copyPixelsFromMultiTileLayer(b);
} else {
Log.w(LOGTAG, "getBitmap() called on a layer (" + mTileLayer + ") we don't know how to get a bitmap from");
@@ -237,20 +211,9 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
}
} finally {
endTransaction(mTileLayer);
}
}
}*/
/** Returns the back buffer. This function is for Gecko to use. */
public ByteBuffer lockBuffer() {
return mBuffer;
}
/**
* Gecko calls this function to signal that it is done with the back buffer. After this call,
* it is forbidden for Gecko to touch the buffer.
*/
public void unlockBuffer() {
/* no-op */
return null;
}
@Override
@@ -269,5 +232,11 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
protected IntSize getTileSize() {
return TILE_SIZE;
}
public void addTile(Bitmap bitmap, int x, int y) {
if (mTileLayer instanceof MultiTileLayer) {
((MultiTileLayer)mTileLayer).addTile(bitmap, x, y);
}
}
}

View File

@@ -121,8 +121,8 @@ public abstract class Layer {
* This function may block, so you should never call this on the main UI thread.
*/
public void beginTransaction(LayerView aView) {
if (mTransactionLock.isHeldByCurrentThread())
throw new RuntimeException("Nested transactions are not supported");
//if (mTransactionLock.isHeldByCurrentThread())
// throw new RuntimeException("Nested transactions are not supported");
mTransactionLock.lock();
mView = aView;
mInTransaction = true;

View File

@@ -38,11 +38,16 @@
package org.mozilla.gecko.gfx;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.Log;
import org.libreoffice.LOKitShell;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
@@ -54,18 +59,15 @@ import java.util.ArrayList;
public class MultiTileLayer extends Layer {
private static final String LOGTAG = "GeckoMultiTileLayer";
private final CairoImage mImage;
private final ArrayList<SubTile> mTiles;
private IntSize mTileSize;
private IntSize mBufferSize;
private IntSize mSize;
public MultiTileLayer(CairoImage image, IntSize tileSize) {
public MultiTileLayer(IntSize tileSize) {
super();
mImage = image;
mTileSize = tileSize;
mBufferSize = new IntSize(0, 0);
mTiles = new ArrayList<SubTile>();
mSize = new IntSize(0,0);
}
public void invalidate(Rect dirtyRect) {
@@ -90,67 +92,20 @@ public class MultiTileLayer extends Layer {
}
}
public void setSize(IntSize size) {
mSize = size;
}
@Override
public IntSize getSize() {
return mImage.getSize();
return mSize;
}
private void validateTiles() {
IntSize size = getSize();
if (size.equals(mBufferSize)) {
return;
}
// Regenerate tiles
mTiles.clear();
int offset = 0;
final int format = mImage.getFormat();
final ByteBuffer buffer = mImage.getBuffer().slice();
final int bpp = CairoUtils.bitsPerPixelForCairoFormat(format) / 8;
for (int y = 0; y < size.height; y += mTileSize.height) {
for (int x = 0; x < size.width; x += mTileSize.width) {
// Create a CairoImage implementation that returns a
// tile from the parent CairoImage. It's assumed that
// the tiles are stored in series.
final IntSize layerSize =
new IntSize(Math.min(mTileSize.width, size.width - x),
Math.min(mTileSize.height, size.height - y));
final int tileOffset = offset;
CairoImage subImage = new CairoImage() {
@Override
public ByteBuffer getBuffer() {
// Create a ByteBuffer that shares the data of the original
// buffer, but is positioned and limited so that only the
// tile data is accessible.
buffer.position(tileOffset);
ByteBuffer tileBuffer = buffer.slice();
tileBuffer.limit(layerSize.getArea() * bpp);
return tileBuffer;
}
@Override
public IntSize getSize() {
return layerSize;
}
@Override
public int getFormat() {
return format;
}
};
mTiles.add(new SubTile(subImage, x, y));
offset += layerSize.getArea() * bpp;
}
}
Log.i(LOGTAG, "validateTiles()");
// Set tile origins and resolution
refreshTileMetrics(getOrigin(), getResolution(), false);
mBufferSize = size;
}
@Override
@@ -286,15 +241,10 @@ public class MultiTileLayer extends Layer {
return validRegion;
}
class SubTile extends SingleTileLayer {
public int x;
public int y;
public SubTile(CairoImage mImage, int mX, int mY) {
super(mImage);
x = mX;
y = mY;
}
public void addTile(Bitmap bitmap, int x, int y) {
SubTile tile = new SubTile(new BufferedCairoImage(bitmap), x,y);
tile.beginTransaction();
mTiles.add(tile);
}
}

View File

@@ -0,0 +1,15 @@
package org.mozilla.gecko.gfx;
/**
* Created by quikee on 29.6.2014.
*/
public class SubTile extends SingleTileLayer {
public int x;
public int y;
public SubTile(CairoImage mImage, int mX, int mY) {
super(mImage);
x = mX;
y = mY;
}
}