2011-11-18 10:28:17 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko.gfx;
|
|
|
|
|
2012-07-27 21:57:47 -07:00
|
|
|
import org.mozilla.gecko.util.FloatUtils;
|
2012-07-27 17:53:54 -07:00
|
|
|
|
2012-02-26 07:47:47 -08:00
|
|
|
import android.graphics.Rect;
|
2011-12-01 14:05:41 -08:00
|
|
|
import android.graphics.RectF;
|
2012-02-02 01:02:32 -08:00
|
|
|
import android.graphics.Region;
|
2012-07-27 17:53:54 -07:00
|
|
|
|
2012-02-08 21:13:08 -08:00
|
|
|
import java.nio.FloatBuffer;
|
2011-11-18 18:07:14 -08:00
|
|
|
import java.util.concurrent.locks.ReentrantLock;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
public abstract class Layer {
|
2011-11-18 18:07:14 -08:00
|
|
|
private final ReentrantLock mTransactionLock;
|
|
|
|
private boolean mInTransaction;
|
2012-02-26 07:47:47 -08:00
|
|
|
private Rect mNewPosition;
|
2011-11-23 11:07:47 -08:00
|
|
|
private float mNewResolution;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-02-26 07:47:47 -08:00
|
|
|
protected Rect mPosition;
|
2012-01-31 06:40:58 -08:00
|
|
|
protected float mResolution;
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
public Layer() {
|
2012-02-26 07:47:47 -08:00
|
|
|
this(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Layer(IntSize size) {
|
2011-11-18 18:07:14 -08:00
|
|
|
mTransactionLock = new ReentrantLock();
|
2012-02-26 07:47:47 -08:00
|
|
|
if (size == null) {
|
2012-03-12 13:20:19 -07:00
|
|
|
mPosition = new Rect();
|
2012-02-26 07:47:47 -08:00
|
|
|
} else {
|
|
|
|
mPosition = new Rect(0, 0, size.width, size.height);
|
|
|
|
}
|
2011-11-23 11:07:47 -08:00
|
|
|
mResolution = 1.0f;
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-01-06 03:22:52 -08:00
|
|
|
/**
|
|
|
|
* Updates the layer. This returns false if there is still work to be done
|
2012-02-04 15:49:59 -08:00
|
|
|
* after this update.
|
2012-01-06 03:22:52 -08:00
|
|
|
*/
|
2012-02-08 21:13:08 -08:00
|
|
|
public final boolean update(RenderContext context) {
|
2011-11-18 18:07:14 -08:00
|
|
|
if (mTransactionLock.isHeldByCurrentThread()) {
|
2012-02-04 15:49:59 -08:00
|
|
|
throw new RuntimeException("draw() called while transaction lock held by this " +
|
|
|
|
"thread?!");
|
2012-01-24 07:39:53 -08:00
|
|
|
}
|
|
|
|
|
2012-02-04 15:49:59 -08:00
|
|
|
if (mTransactionLock.tryLock()) {
|
|
|
|
try {
|
2012-04-05 08:32:16 -07:00
|
|
|
performUpdates(context);
|
|
|
|
return true;
|
2012-02-04 15:49:59 -08:00
|
|
|
} finally {
|
2011-11-18 18:07:14 -08:00
|
|
|
mTransactionLock.unlock();
|
|
|
|
}
|
|
|
|
}
|
2012-02-04 15:49:59 -08:00
|
|
|
|
|
|
|
return false;
|
2011-11-23 11:07:47 -08:00
|
|
|
}
|
2011-11-18 18:07:14 -08:00
|
|
|
|
2011-12-01 14:05:41 -08:00
|
|
|
/** Subclasses override this function to draw the layer. */
|
|
|
|
public abstract void draw(RenderContext context);
|
2011-11-18 18:07:14 -08:00
|
|
|
|
2011-12-01 14:05:41 -08:00
|
|
|
/** Given the intrinsic size of the layer, returns the pixel boundaries of the layer rect. */
|
2012-02-26 07:47:47 -08:00
|
|
|
protected RectF getBounds(RenderContext context) {
|
|
|
|
return RectUtils.scale(new RectF(mPosition), context.zoomFactor / mResolution);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2011-11-18 18:07:14 -08:00
|
|
|
/**
|
|
|
|
* Call this before modifying the layer. Note that, for TileLayers, "modifying the layer"
|
|
|
|
* includes altering the underlying CairoImage in any way. Thus you must call this function
|
|
|
|
* before modifying the byte buffer associated with this layer.
|
|
|
|
*
|
|
|
|
* This function may block, so you should never call this on the main UI thread.
|
|
|
|
*/
|
2012-02-21 08:05:09 -08:00
|
|
|
public void beginTransaction() {
|
2011-11-18 18:07:14 -08:00
|
|
|
if (mTransactionLock.isHeldByCurrentThread())
|
|
|
|
throw new RuntimeException("Nested transactions are not supported");
|
|
|
|
mTransactionLock.lock();
|
|
|
|
mInTransaction = true;
|
2012-02-04 15:49:59 -08:00
|
|
|
mNewResolution = mResolution;
|
2011-11-18 18:07:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Call this when you're done modifying the layer. */
|
|
|
|
public void endTransaction() {
|
|
|
|
if (!mInTransaction)
|
|
|
|
throw new RuntimeException("endTransaction() called outside a transaction");
|
|
|
|
mInTransaction = false;
|
|
|
|
mTransactionLock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns true if the layer is currently in a transaction and false otherwise. */
|
|
|
|
protected boolean inTransaction() {
|
|
|
|
return mInTransaction;
|
|
|
|
}
|
|
|
|
|
2012-02-26 07:47:47 -08:00
|
|
|
/** Returns the current layer position. */
|
|
|
|
public Rect getPosition() {
|
|
|
|
return mPosition;
|
2011-11-18 18:07:14 -08:00
|
|
|
}
|
|
|
|
|
2012-02-26 07:47:47 -08:00
|
|
|
/** Sets the position. Only valid inside a transaction. */
|
|
|
|
public void setPosition(Rect newPosition) {
|
2011-11-18 18:07:14 -08:00
|
|
|
if (!mInTransaction)
|
2012-02-26 07:47:47 -08:00
|
|
|
throw new RuntimeException("setPosition() is only valid inside a transaction");
|
|
|
|
mNewPosition = newPosition;
|
2011-11-18 18:07:14 -08:00
|
|
|
}
|
|
|
|
|
2011-11-23 11:07:47 -08:00
|
|
|
/** Returns the current layer's resolution. */
|
|
|
|
public float getResolution() {
|
|
|
|
return mResolution;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the layer resolution. This value is used to determine how many pixels per
|
|
|
|
* device pixel this layer was rendered at. This will be reflected by scaling by
|
|
|
|
* the reciprocal of the resolution in the layer's transform() function.
|
|
|
|
* Only valid inside a transaction. */
|
|
|
|
public void setResolution(float newResolution) {
|
|
|
|
if (!mInTransaction)
|
|
|
|
throw new RuntimeException("setResolution() is only valid inside a transaction");
|
|
|
|
mNewResolution = newResolution;
|
|
|
|
}
|
|
|
|
|
2011-11-18 18:07:14 -08:00
|
|
|
/**
|
|
|
|
* Subclasses may override this method to perform custom layer updates. This will be called
|
|
|
|
* with the transaction lock held. Subclass implementations of this method must call the
|
2012-01-06 03:22:52 -08:00
|
|
|
* superclass implementation. Returns false if there is still work to be done after this
|
|
|
|
* update is complete.
|
2011-11-18 18:07:14 -08:00
|
|
|
*/
|
2012-04-05 08:32:16 -07:00
|
|
|
protected void performUpdates(RenderContext context) {
|
2012-02-26 07:47:47 -08:00
|
|
|
if (mNewPosition != null) {
|
|
|
|
mPosition = mNewPosition;
|
|
|
|
mNewPosition = null;
|
2011-11-18 18:07:14 -08:00
|
|
|
}
|
2011-12-01 14:05:41 -08:00
|
|
|
if (mNewResolution != 0.0f) {
|
|
|
|
mResolution = mNewResolution;
|
|
|
|
mNewResolution = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-06 11:34:38 -07:00
|
|
|
/**
|
|
|
|
* This function fills in the provided <tt>dest</tt> array with values to render a texture.
|
|
|
|
* The array is filled with 4 sets of {x, y, z, texture_x, texture_y} values (so 20 values
|
|
|
|
* in total) corresponding to the corners of the rect.
|
|
|
|
*/
|
|
|
|
protected final void fillRectCoordBuffer(float[] dest, RectF rect, float viewWidth, float viewHeight,
|
|
|
|
Rect cropRect, float texWidth, float texHeight) {
|
|
|
|
//x, y, z, texture_x, texture_y
|
|
|
|
dest[0] = rect.left / viewWidth;
|
|
|
|
dest[1] = rect.bottom / viewHeight;
|
|
|
|
dest[2] = 0;
|
|
|
|
dest[3] = cropRect.left / texWidth;
|
|
|
|
dest[4] = cropRect.top / texHeight;
|
|
|
|
|
|
|
|
dest[5] = rect.left / viewWidth;
|
|
|
|
dest[6] = rect.top / viewHeight;
|
|
|
|
dest[7] = 0;
|
|
|
|
dest[8] = cropRect.left / texWidth;
|
|
|
|
dest[9] = cropRect.bottom / texHeight;
|
|
|
|
|
|
|
|
dest[10] = rect.right / viewWidth;
|
|
|
|
dest[11] = rect.bottom / viewHeight;
|
|
|
|
dest[12] = 0;
|
|
|
|
dest[13] = cropRect.right / texWidth;
|
|
|
|
dest[14] = cropRect.top / texHeight;
|
|
|
|
|
|
|
|
dest[15] = rect.right / viewWidth;
|
|
|
|
dest[16] = rect.top / viewHeight;
|
|
|
|
dest[17] = 0;
|
|
|
|
dest[18] = cropRect.right / texWidth;
|
|
|
|
dest[19] = cropRect.bottom / texHeight;
|
|
|
|
}
|
|
|
|
|
2012-07-17 17:54:54 -07:00
|
|
|
public static class RenderContext {
|
2011-12-01 14:05:41 -08:00
|
|
|
public final RectF viewport;
|
2012-05-23 07:49:52 -07:00
|
|
|
public final RectF pageRect;
|
2011-12-01 14:05:41 -08:00
|
|
|
public final float zoomFactor;
|
2012-02-08 21:13:08 -08:00
|
|
|
public final int positionHandle;
|
|
|
|
public final int textureHandle;
|
|
|
|
public final FloatBuffer coordBuffer;
|
2011-12-01 14:05:41 -08:00
|
|
|
|
2012-05-24 08:02:49 -07:00
|
|
|
public RenderContext(RectF aViewport, RectF aPageRect, float aZoomFactor,
|
2012-02-08 21:13:08 -08:00
|
|
|
int aPositionHandle, int aTextureHandle, FloatBuffer aCoordBuffer) {
|
2011-12-01 14:05:41 -08:00
|
|
|
viewport = aViewport;
|
2012-05-23 07:49:52 -07:00
|
|
|
pageRect = aPageRect;
|
2011-12-01 14:05:41 -08:00
|
|
|
zoomFactor = aZoomFactor;
|
2012-02-08 21:13:08 -08:00
|
|
|
positionHandle = aPositionHandle;
|
|
|
|
textureHandle = aTextureHandle;
|
|
|
|
coordBuffer = aCoordBuffer;
|
2011-12-01 14:05:41 -08:00
|
|
|
}
|
2011-12-09 13:05:24 -08:00
|
|
|
|
|
|
|
public boolean fuzzyEquals(RenderContext other) {
|
|
|
|
if (other == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return RectUtils.fuzzyEquals(viewport, other.viewport)
|
2012-05-23 07:49:52 -07:00
|
|
|
&& RectUtils.fuzzyEquals(pageRect, other.pageRect)
|
2011-12-09 13:05:24 -08:00
|
|
|
&& FloatUtils.fuzzyEquals(zoomFactor, other.zoomFactor);
|
|
|
|
}
|
2011-11-18 18:07:14 -08:00
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
2012-07-17 17:54:54 -07:00
|
|
|
|