| 1 |
package org.mozilla.gecko.gfx; |
| 2 |
|
| 3 |
import android.content.ComponentCallbacks2; |
| 4 |
import android.content.Context; |
| 5 |
import android.content.res.Configuration; |
| 6 |
import android.graphics.RectF; |
| 7 |
import android.graphics.Region; |
| 8 |
import android.util.Log; |
| 9 |
|
| 10 |
import org.libreoffice.LOKitShell; |
| 11 |
import org.libreoffice.TileIdentifier; |
| 12 |
import org.mozilla.gecko.util.FloatUtils; |
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.Iterator; |
| 16 |
import java.util.List; |
| 17 |
import java.util.concurrent.locks.Lock; |
| 18 |
import java.util.concurrent.locks.ReadWriteLock; |
| 19 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 20 |
|
| 21 |
public abstract class ComposedTileLayer extends Layer implements ComponentCallbacks2 { |
| 22 |
private static final String LOGTAG = ComposedTileLayer.class.getSimpleName(); |
| 23 |
|
| 24 |
protected final List<SubTile> tiles = new ArrayList<SubTile>(); |
| 25 |
|
| 26 |
protected final IntSize tileSize; |
| 27 |
private final ReadWriteLock tilesReadWriteLock = new ReentrantReadWriteLock(); |
| 28 |
private final Lock tilesReadLock = tilesReadWriteLock.readLock(); |
| 29 |
private final Lock tilesWriteLock = tilesReadWriteLock.writeLock(); |
| 30 |
|
| 31 |
protected RectF currentViewport = new RectF(); |
| 32 |
protected float currentZoom = 1.0f; |
| 33 |
protected RectF currentPageRect = new RectF(); |
| 34 |
|
| 35 |
private long reevaluationNanoTime = 0; |
| 36 |
|
| 37 |
public ComposedTileLayer(Context context) { |
| 38 |
context.registerComponentCallbacks(this); |
| 39 |
this.tileSize = new IntSize(256, 256); |
| 40 |
} |
| 41 |
|
| 42 |
protected static RectF roundToTileSize(RectF input, IntSize tileSize) { |
| 43 |
float minX = ((int) (input.left / tileSize.width)) * tileSize.width; |
| 44 |
float minY = ((int) (input.top / tileSize.height)) * tileSize.height; |
| 45 |
float maxX = ((int) (input.right / tileSize.width) + 1) * tileSize.width; |
| 46 |
float maxY = ((int) (input.bottom / tileSize.height) + 1) * tileSize.height; |
| 47 |
return new RectF(minX, minY, maxX, maxY); |
| 48 |
} |
| 49 |
|
| 50 |
protected static RectF inflate(RectF rect, IntSize inflateSize) { |
| 51 |
RectF newRect = new RectF(rect); |
| 52 |
newRect.left -= inflateSize.width; |
| 53 |
newRect.left = Math.max(newRect.left, 0.0f); |
| 54 |
|
| 55 |
newRect.top -= inflateSize.height; |
| 56 |
newRect.top = Math.max(newRect.top, 0.0f); |
| 57 |
|
| 58 |
newRect.right += inflateSize.width; |
| 59 |
newRect.bottom += inflateSize.height; |
| 60 |
|
| 61 |
return newRect; |
| 62 |
} |
| 63 |
|
| 64 |
protected static RectF normalizeRect(RectF rect, float sourceFactor, float targetFactor) { |
| 65 |
return new RectF( |
| 66 |
(rect.left / sourceFactor) * targetFactor, |
| 67 |
(rect.top / sourceFactor) * targetFactor, |
| 68 |
(rect.right / sourceFactor) * targetFactor, |
| 69 |
(rect.bottom / sourceFactor) * targetFactor); |
| 70 |
} |
| 71 |
|
| 72 |
public void invalidate() { |
| 73 |
tilesReadLock.lock(); |
| 74 |
for (SubTile tile : tiles) { |
| 75 |
tile.invalidate(); |
| 76 |
} |
| 77 |
tilesReadLock.unlock(); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void beginTransaction() { |
| 82 |
super.beginTransaction(); |
| 83 |
tilesReadLock.lock(); |
| 84 |
for (SubTile tile : tiles) { |
| 85 |
tile.beginTransaction(); |
| 86 |
} |
| 87 |
tilesReadLock.unlock(); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public void endTransaction() { |
| 92 |
tilesReadLock.lock(); |
| 93 |
for (SubTile tile : tiles) { |
| 94 |
tile.endTransaction(); |
| 95 |
} |
| 96 |
tilesReadLock.unlock(); |
| 97 |
super.endTransaction(); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void draw(RenderContext context) { |
| 102 |
tilesReadLock.lock(); |
| 103 |
for (SubTile tile : tiles) { |
| 104 |
if (RectF.intersects(tile.getBounds(context), context.viewport)) { |
| 105 |
tile.draw(context); |
| 106 |
} |
| 107 |
} |
| 108 |
tilesReadLock.unlock(); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
protected void performUpdates(RenderContext context) { |
| 113 |
super.performUpdates(context); |
| 114 |
|
| 115 |
tilesReadLock.lock(); |
| 116 |
for (SubTile tile : tiles) { |
| 117 |
tile.beginTransaction(); |
| 118 |
tile.refreshTileMetrics(); |
| 119 |
tile.endTransaction(); |
| 120 |
tile.performUpdates(context); |
| 121 |
} |
| 122 |
tilesReadLock.unlock(); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public Region getValidRegion(RenderContext context) { |
| 127 |
Region validRegion = new Region(); |
| 128 |
tilesReadLock.lock(); |
| 129 |
for (SubTile tile : tiles) { |
| 130 |
validRegion.op(tile.getValidRegion(context), Region.Op.UNION); |
| 131 |
} |
| 132 |
tilesReadLock.unlock(); |
| 133 |
return validRegion; |
| 134 |
} |
| 135 |
|
| 136 |
public void reevaluateTiles(ImmutableViewportMetrics viewportMetrics) { |
| 137 |
RectF newViewPort = getViewPort(viewportMetrics); |
| 138 |
float newZoom = getZoom(viewportMetrics); |
| 139 |
|
| 140 |
// When |
| 141 |
if (newZoom <= 0.0 || Float.isNaN(newZoom)) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
if (currentViewport.equals(newViewPort) && FloatUtils.fuzzyEquals(currentZoom, newZoom)) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
long currentReevaluationNanoTime = System.nanoTime(); |
| 150 |
if ((currentReevaluationNanoTime - reevaluationNanoTime) < 25 * 1000000) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
reevaluationNanoTime = currentReevaluationNanoTime; |
| 155 |
|
| 156 |
currentViewport = newViewPort; |
| 157 |
currentZoom = newZoom; |
| 158 |
currentPageRect = viewportMetrics.getPageRect(); |
| 159 |
|
| 160 |
LOKitShell.sendTileReevaluationRequest(this); |
| 161 |
} |
| 162 |
|
| 163 |
protected abstract RectF getViewPort(ImmutableViewportMetrics viewportMetrics); |
| 164 |
|
| 165 |
protected abstract float getZoom(ImmutableViewportMetrics viewportMetrics); |
| 166 |
|
| 167 |
protected abstract int getTilePriority(); |
| 168 |
|
| 169 |
private boolean containsTilesMatching(float x, float y, float currentZoom) { |
| 170 |
tilesReadLock.lock(); |
| 171 |
try { |
| 172 |
for (SubTile tile : tiles) { |
| 173 |
if (tile.id.x == x && tile.id.y == y && tile.id.zoom == currentZoom) { |
| 174 |
return true; |
| 175 |
} |
| 176 |
} |
| 177 |
return false; |
| 178 |
} finally { |
| 179 |
tilesReadLock.unlock(); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
public void addNewTiles(List<SubTile> newTiles) { |
| 184 |
for (float y = currentViewport.top; y < currentViewport.bottom; y += tileSize.height) { |
| 185 |
if (y > currentPageRect.height()) { |
| 186 |
continue; |
| 187 |
} |
| 188 |
for (float x = currentViewport.left; x < currentViewport.right; x += tileSize.width) { |
| 189 |
if (x > currentPageRect.width()) { |
| 190 |
continue; |
| 191 |
} |
| 192 |
if (!containsTilesMatching(x, y, currentZoom)) { |
| 193 |
TileIdentifier tileId = new TileIdentifier((int) x, (int) y, currentZoom, tileSize); |
| 194 |
SubTile tile = createNewTile(tileId); |
| 195 |
newTiles.add(tile); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
public void clearMarkedTiles() { |
| 202 |
tilesWriteLock.lock(); |
| 203 |
Iterator<SubTile> iterator = tiles.iterator(); |
| 204 |
while (iterator.hasNext()) { |
| 205 |
SubTile tile = iterator.next(); |
| 206 |
if (tile.markedForRemoval) { |
| 207 |
tile.destroy(); |
| 208 |
iterator.remove(); |
| 209 |
} |
| 210 |
} |
| 211 |
tilesWriteLock.unlock(); |
| 212 |
} |
| 213 |
|
| 214 |
public void markTiles() { |
| 215 |
tilesReadLock.lock(); |
| 216 |
for (SubTile tile : tiles) { |
| 217 |
if (FloatUtils.fuzzyEquals(tile.id.zoom, currentZoom)) { |
| 218 |
RectF tileRect = tile.id.getRectF(); |
| 219 |
if (!RectF.intersects(currentViewport, tileRect)) { |
| 220 |
tile.markForRemoval(); |
| 221 |
} |
| 222 |
} else { |
| 223 |
tile.markForRemoval(); |
| 224 |
} |
| 225 |
} |
| 226 |
tilesReadLock.unlock(); |
| 227 |
} |
| 228 |
|
| 229 |
public void clearAndReset() { |
| 230 |
tilesWriteLock.lock(); |
| 231 |
tiles.clear(); |
| 232 |
tilesWriteLock.unlock(); |
| 233 |
currentViewport = new RectF(); |
| 234 |
} |
| 235 |
|
| 236 |
private SubTile createNewTile(TileIdentifier tileId) { |
| 237 |
SubTile tile = new SubTile(tileId); |
| 238 |
tile.beginTransaction(); |
| 239 |
tilesWriteLock.lock(); |
| 240 |
tiles.add(tile); |
| 241 |
tilesWriteLock.unlock(); |
| 242 |
return tile; |
| 243 |
} |
| 244 |
|
| 245 |
public boolean isStillValid(TileIdentifier tileId) { |
| 246 |
return RectF.intersects(currentViewport, tileId.getRectF()) || currentViewport.contains(tileId.getRectF()); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Invalidate tiles which intersect the input rect |
| 251 |
*/ |
| 252 |
public void invalidateTiles(List<SubTile> tilesToInvalidate, RectF cssRect) { |
| 253 |
RectF zoomedRect = RectUtils.scale(cssRect, currentZoom); |
| 254 |
tilesReadLock.lock(); |
| 255 |
for (SubTile tile : tiles) { |
| 256 |
if (!tile.markedForRemoval && RectF.intersects(zoomedRect, tile.id.getRectF())) { |
| 257 |
tilesToInvalidate.add(tile); |
| 258 |
} |
| 259 |
} |
| 260 |
tilesReadLock.unlock(); |
| 261 |
} |
| 262 |
|
| 263 |
@Override |
| 264 |
public void onConfigurationChanged(Configuration newConfig) { |
| 265 |
} |
| 266 |
|
| 267 |
@Override |
| 268 |
public void onLowMemory() { |
| 269 |
Log.i(LOGTAG, "onLowMemory"); |
| 270 |
} |
| 271 |
|
| 272 |
@Override |
| 273 |
public void onTrimMemory(int level) { |
| 274 |
if (level >= 15 /*TRIM_MEMORY_RUNNING_CRITICAL*/) { |
| 275 |
Log.i(LOGTAG, "Trimming memory - TRIM_MEMORY_RUNNING_CRITICAL"); |
| 276 |
} else if (level >= 10 /*TRIM_MEMORY_RUNNING_LOW*/) { |
| 277 |
Log.i(LOGTAG, "Trimming memory - TRIM_MEMORY_RUNNING_LOW"); |
| 278 |
} |
| 279 |
} |
| 280 |
} |