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); Rect bufferRect = application.getLayerClient().beginDrawing(originalBitmap.getWidth(), originalBitmap.getHeight(), 256, 256, metadata);
if (bufferRect == null) if (bufferRect == null) {
return false; return false;
}
int x = 0;
int y = 0;
ByteBuffer buffer = application.getLayerClient().lockBuffer();
for (Integer i = 1; i <= 9; i++) { for (Integer i = 1; i <= 9; i++) {
String imageName = "d" + i; String imageName = "d" + i;
Bitmap bitmap = application.getLayerClient().getLayerController().getDrawable(imageName); Bitmap bitmap = application.getLayerClient().getLayerController().getDrawable(imageName);
bitmap.copyPixelsToBuffer(buffer.asIntBuffer()); application.getLayerClient().addTile(bitmap, x, y);
buffer.position(buffer.position() + bitmap.getByteCount()); 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.getLayerClient().endDrawing(0, 0, originalBitmap.getWidth(), originalBitmap.getHeight());
application.runOnUiThread(new Runnable() { 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 final long MIN_VIEWPORT_CHANGE_DELAY = 25L;
private static Pattern sColorPattern; private static Pattern sColorPattern;
protected IntSize mScreenSize; protected IntSize mScreenSize;
protected IntSize mBufferSize;
protected Layer mTileLayer; protected Layer mTileLayer;
/* The viewport that Gecko is currently displaying. */ /* The viewport that Gecko is currently displaying. */
protected ViewportMetrics mGeckoViewport; protected ViewportMetrics mGeckoViewport;
@@ -94,7 +93,6 @@ public abstract class GeckoLayerClient extends LayerClient implements GeckoEvent
public GeckoLayerClient(Context context) { public GeckoLayerClient(Context context) {
mScreenSize = new IntSize(0, 0); 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 // 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() { private void adjustViewport() {
ViewportMetrics viewportMetrics = new ViewportMetrics(getLayerController().getViewportMetrics()); ViewportMetrics viewportMetrics = new ViewportMetrics(getLayerController().getViewportMetrics());
PointF viewportOffset = viewportMetrics.getOptimumViewportOffset(mBufferSize); PointF viewportOffset = viewportMetrics.getOptimumViewportOffset(getBufferSize());
viewportMetrics.setViewportOffset(viewportOffset); viewportMetrics.setViewportOffset(viewportOffset);
viewportMetrics.setViewport(viewportMetrics.getClampedViewport()); viewportMetrics.setViewport(viewportMetrics.getClampedViewport());
@@ -359,7 +357,8 @@ public abstract class GeckoLayerClient extends LayerClient implements GeckoEvent
mUpdateViewportOnEndDraw = true; mUpdateViewportOnEndDraw = true;
// Redraw everything. // 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)); LOKitShell.sendEvent(LOEvent.draw(rect));
} else if ("Viewport:UpdateLater".equals(event)) { } else if ("Viewport:UpdateLater".equals(event)) {
Log.e(LOGTAG, "### Java side Viewport:UpdateLater()!"); Log.e(LOGTAG, "### Java side Viewport:UpdateLater()!");
@@ -373,14 +372,6 @@ public abstract class GeckoLayerClient extends LayerClient implements GeckoEvent
render(); render();
} }
public int getWidth() {
return mBufferSize.width;
}
public int getHeight() {
return mBufferSize.height;
}
public ViewportMetrics getGeckoViewportMetrics() { public ViewportMetrics getGeckoViewportMetrics() {
if (mGeckoViewport != null) if (mGeckoViewport != null)
return new ViewportMetrics(mGeckoViewport); return new ViewportMetrics(mGeckoViewport);

View File

@@ -68,28 +68,18 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
private int mFormat; private int mFormat;
private IntSize mViewportSize; private IntSize mViewportSize;
private ByteBuffer mBuffer; private IntSize mBufferSize;
private CairoImage mCairoImage;
private static final IntSize TILE_SIZE = new IntSize(256, 256); private static final IntSize TILE_SIZE = new IntSize(256, 256);
public GeckoSoftwareLayerClient(Context context) { public GeckoSoftwareLayerClient(Context context) {
super(context); super(context);
mBufferSize = new IntSize(0,0);
mFormat = CairoImage.FORMAT_ARGB32; 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 { try {
if (mBuffer != null) if (mBuffer != null)
LOKitShell.freeDirectBuffer(mBuffer); LOKitShell.freeDirectBuffer(mBuffer);
@@ -97,7 +87,7 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
} finally { } finally {
super.finalize(); super.finalize();
} }
} }*/
public void setLayerController(LayerController layerController) { public void setLayerController(LayerController layerController) {
super.setLayerController(layerController); super.setLayerController(layerController);
@@ -116,7 +106,7 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
return false; return false;
Log.i(LOGTAG, "Creating MultiTileLayer"); Log.i(LOGTAG, "Creating MultiTileLayer");
mTileLayer = new MultiTileLayer(mCairoImage, TILE_SIZE); mTileLayer = new MultiTileLayer(TILE_SIZE);
getLayerController().setRoot(mTileLayer); getLayerController().setRoot(mTileLayer);
@@ -149,21 +139,6 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
// If the window size has changed, reallocate the buffer to match. // If the window size has changed, reallocate the buffer to match.
if (mBufferSize.width != width || mBufferSize.height != height) { if (mBufferSize.width != width || mBufferSize.height != height) {
mBufferSize = new IntSize(width, 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; 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); Canvas c = new Canvas(target);
ByteBuffer tileBuffer = mBuffer.slice(); ByteBuffer tileBuffer = mBuffer.slice();
int bpp = CairoUtils.bitsPerPixelForCairoFormat(mFormat) / 8; int bpp = CairoUtils.bitsPerPixelForCairoFormat(mFormat) / 8;
@@ -201,7 +176,7 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
tileBuffer = tileBuffer.slice(); tileBuffer = tileBuffer.slice();
} }
} }
} }*/
@Override @Override
protected void tileLayerUpdated() { protected void tileLayerUpdated() {
@@ -215,7 +190,7 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
// Begin a tile transaction, otherwise the buffer can be destroyed while // Begin a tile transaction, otherwise the buffer can be destroyed while
// we're reading from it. // we're reading from it.
beginTransaction(mTileLayer); /*beginTransaction(mTileLayer);
try { try {
if (mBuffer == null || mBufferSize.width <= 0 || mBufferSize.height <= 0) if (mBuffer == null || mBufferSize.width <= 0 || mBufferSize.height <= 0)
return null; return null;
@@ -223,8 +198,7 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
Bitmap b = null; Bitmap b = null;
if (mTileLayer instanceof MultiTileLayer) { if (mTileLayer instanceof MultiTileLayer) {
b = Bitmap.createBitmap(mBufferSize.width, mBufferSize.height, b = Bitmap.createBitmap(mBufferSize.width, mBufferSize.height,CairoUtils.cairoFormatTobitmapConfig(mFormat));
CairoUtils.cairoFormatTobitmapConfig(mFormat));
copyPixelsFromMultiTileLayer(b); copyPixelsFromMultiTileLayer(b);
} else { } else {
Log.w(LOGTAG, "getBitmap() called on a layer (" + mTileLayer + ") we don't know how to get a bitmap from"); 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 { } finally {
endTransaction(mTileLayer); endTransaction(mTileLayer);
} }*/
}
/** Returns the back buffer. This function is for Gecko to use. */ return null;
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 */
} }
@Override @Override
@@ -269,5 +232,11 @@ public class GeckoSoftwareLayerClient extends GeckoLayerClient {
protected IntSize getTileSize() { protected IntSize getTileSize() {
return TILE_SIZE; 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. * This function may block, so you should never call this on the main UI thread.
*/ */
public void beginTransaction(LayerView aView) { public void beginTransaction(LayerView aView) {
if (mTransactionLock.isHeldByCurrentThread()) //if (mTransactionLock.isHeldByCurrentThread())
throw new RuntimeException("Nested transactions are not supported"); // throw new RuntimeException("Nested transactions are not supported");
mTransactionLock.lock(); mTransactionLock.lock();
mView = aView; mView = aView;
mInTransaction = true; mInTransaction = true;

View File

@@ -38,11 +38,16 @@
package org.mozilla.gecko.gfx; package org.mozilla.gecko.gfx;
import android.graphics.Bitmap;
import android.graphics.Point; import android.graphics.Point;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.RectF; import android.graphics.RectF;
import android.graphics.Region; import android.graphics.Region;
import android.util.Log;
import org.libreoffice.LOKitShell;
import java.nio.Buffer;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
@@ -54,18 +59,15 @@ import java.util.ArrayList;
public class MultiTileLayer extends Layer { public class MultiTileLayer extends Layer {
private static final String LOGTAG = "GeckoMultiTileLayer"; private static final String LOGTAG = "GeckoMultiTileLayer";
private final CairoImage mImage;
private final ArrayList<SubTile> mTiles; private final ArrayList<SubTile> mTiles;
private IntSize mTileSize; private IntSize mTileSize;
private IntSize mBufferSize; private IntSize mSize;
public MultiTileLayer(CairoImage image, IntSize tileSize) { public MultiTileLayer(IntSize tileSize) {
super(); super();
mImage = image;
mTileSize = tileSize; mTileSize = tileSize;
mBufferSize = new IntSize(0, 0);
mTiles = new ArrayList<SubTile>(); mTiles = new ArrayList<SubTile>();
mSize = new IntSize(0,0);
} }
public void invalidate(Rect dirtyRect) { public void invalidate(Rect dirtyRect) {
@@ -90,67 +92,20 @@ public class MultiTileLayer extends Layer {
} }
} }
public void setSize(IntSize size) {
mSize = size;
}
@Override @Override
public IntSize getSize() { public IntSize getSize() {
return mImage.getSize(); return mSize;
} }
private void validateTiles() { private void validateTiles() {
IntSize size = getSize(); Log.i(LOGTAG, "validateTiles()");
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;
}
}
// Set tile origins and resolution // Set tile origins and resolution
refreshTileMetrics(getOrigin(), getResolution(), false); refreshTileMetrics(getOrigin(), getResolution(), false);
mBufferSize = size;
} }
@Override @Override
@@ -286,15 +241,10 @@ public class MultiTileLayer extends Layer {
return validRegion; return validRegion;
} }
class SubTile extends SingleTileLayer { public void addTile(Bitmap bitmap, int x, int y) {
public int x; SubTile tile = new SubTile(new BufferedCairoImage(bitmap), x,y);
public int y; tile.beginTransaction();
mTiles.add(tile);
public SubTile(CairoImage mImage, int mX, int mY) {
super(mImage);
x = mX;
y = mY;
}
} }
} }

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;
}
}