2 * Copyright (C) 2013 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.photos.views;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Rect;
22 import android.graphics.RectF;
23 import android.support.v4.util.LongSparseArray;
24 import android.util.DisplayMetrics;
25 import android.util.Log;
26 import android.view.View;
27 import android.view.WindowManager;
29 import com.android.gallery3d.common.Utils;
30 import com.android.gallery3d.glrenderer.GLCanvas;
31 import com.android.gallery3d.glrenderer.UploadedTexture;
32 import com.android.photos.data.GalleryBitmapPool;
34 public class TiledImageRenderer {
35 public static final int SIZE_UNKNOWN = -1;
37 private static final String TAG = "TiledImageRenderer";
38 private static final int UPLOAD_LIMIT = 1;
41 * This is the tile state in the CPU side.
43 * ACTIVATED (initial state)
44 * --> IN_QUEUE - by queueForDecode()
45 * --> RECYCLED - by recycleTile()
46 * IN_QUEUE --> DECODING - by decodeTile()
47 * --> RECYCLED - by recycleTile)
48 * DECODING --> RECYCLING - by recycleTile()
49 * --> DECODED - by decodeTile()
50 * --> DECODE_FAIL - by decodeTile()
51 * RECYCLING --> RECYCLED - by decodeTile()
52 * DECODED --> ACTIVATED - (after the decoded bitmap is uploaded)
53 * DECODED --> RECYCLED - by recycleTile()
54 * DECODE_FAIL -> RECYCLED - by recycleTile()
55 * RECYCLED --> ACTIVATED - by obtainTile()
57 private static final int STATE_ACTIVATED = 0x01;
58 private static final int STATE_IN_QUEUE = 0x02;
59 private static final int STATE_DECODING = 0x04;
60 private static final int STATE_DECODED = 0x08;
61 private static final int STATE_DECODE_FAIL = 0x10;
62 private static final int STATE_RECYCLING = 0x20;
63 private static final int STATE_RECYCLED = 0x40;
65 private static GalleryBitmapPool sTilePool = GalleryBitmapPool.getInstance();
67 // TILE_SIZE must be 2^N
68 private int mTileSize;
70 private TileSource mModel;
71 protected int mLevelCount; // cache the value of mScaledBitmaps.length
73 // The mLevel variable indicates which level of bitmap we should use.
74 // Level 0 means the original full-sized bitmap, and a larger value means
75 // a smaller scaled bitmap (The width and height of each scaled bitmap is
76 // half size of the previous one). If the value is in [0, mLevelCount), we
77 // use the bitmap in mScaledBitmaps[mLevel] for display, otherwise the value
79 private int mLevel = 0;
84 private int mUploadQuota;
85 private boolean mRenderComplete;
87 private final RectF mSourceRect = new RectF();
88 private final RectF mTargetRect = new RectF();
90 private final LongSparseArray<Tile> mActiveTiles = new LongSparseArray<Tile>();
92 // The following three queue are guarded by mQueueLock
93 private final Object mQueueLock = new Object();
94 private final TileQueue mRecycledQueue = new TileQueue();
95 private final TileQueue mUploadQueue = new TileQueue();
96 private final TileQueue mDecodeQueue = new TileQueue();
98 // The width and height of the full-sized bitmap
99 protected int mImageWidth = SIZE_UNKNOWN;
100 protected int mImageHeight = SIZE_UNKNOWN;
102 protected int mCenterX;
103 protected int mCenterY;
104 protected float mScale;
105 protected int mRotation;
107 private boolean mLayoutTiles;
109 // Temp variables to avoid memory allocation
110 private final Rect mTileRange = new Rect();
111 private final Rect mActiveRange[] = {new Rect(), new Rect()};
113 private TileDecoder mTileDecoder;
114 private boolean mBackgroundTileUploaded;
116 private int mViewWidth, mViewHeight;
117 private View mParent;
119 public static interface TileSource {
120 public int getTileSize();
121 public int getImageWidth();
122 public int getImageHeight();
124 // The tile returned by this method can be specified this way: Assuming
125 // the image size is (width, height), first take the intersection of (0,
126 // 0) - (width, height) and (x, y) - (x + tileSize, y + tileSize). If
127 // in extending the region, we found some part of the region is outside
128 // the image, those pixels are filled with black.
130 // If level > 0, it does the same operation on a down-scaled version of
131 // the original image (down-scaled by a factor of 2^level), but (x, y)
132 // still refers to the coordinate on the original image.
134 // The method would be called by the decoder thread.
135 public Bitmap getTile(int level, int x, int y, Bitmap reuse);
138 public static int suggestedTileSize(Context context) {
139 return isHighResolution(context) ? 512 : 256;
142 private static boolean isHighResolution(Context context) {
143 DisplayMetrics metrics = new DisplayMetrics();
144 WindowManager wm = (WindowManager)
145 context.getSystemService(Context.WINDOW_SERVICE);
146 wm.getDefaultDisplay().getMetrics(metrics);
147 return metrics.heightPixels > 2048 || metrics.widthPixels > 2048;
150 public TiledImageRenderer(View parent) {
152 mTileDecoder = new TileDecoder();
153 mTileDecoder.start();
156 public int getViewWidth() {
160 public int getViewHeight() {
164 private void invalidate() {
165 mParent.postInvalidate();
168 public void setModel(TileSource model, int rotation) {
169 if (mModel != model) {
171 notifyModelInvalidated();
173 if (mRotation != rotation) {
174 mRotation = rotation;
180 private static int calulateLevelCount(TileSource source) {
182 int maxDim = Math.max(source.getImageWidth(), source.getImageHeight());
183 int t = source.getTileSize();
191 public void notifyModelInvalidated() {
193 if (mModel == null) {
198 mImageWidth = mModel.getImageWidth();
199 mImageHeight = mModel.getImageHeight();
200 mLevelCount = calulateLevelCount(mModel);
201 mTileSize = mModel.getTileSize();
207 public void setViewSize(int width, int height) {
209 mViewHeight = height;
212 public void setPosition(int centerX, int centerY, float scale) {
213 if (mCenterX == centerX && mCenterY == centerY
214 && mScale == scale) return;
222 // Prepare the tiles we want to use for display.
224 // 1. Decide the tile level we want to use for display.
225 // 2. Decide the tile levels we want to keep as texture (in addition to
226 // the one we use for display).
227 // 3. Recycle unused tiles.
228 // 4. Activate the tiles we want.
229 private void layoutTiles() {
230 if (mViewWidth == 0 || mViewHeight == 0 || !mLayoutTiles) {
233 mLayoutTiles = false;
235 // The tile levels we want to keep as texture is in the range
236 // [fromLevel, endLevel).
240 // We want to use a texture larger than or equal to the display size.
241 mLevel = Utils.clamp(Utils.floorLog2(1f / mScale), 0, mLevelCount);
243 // We want to keep one more tile level as texture in addition to what
244 // we use for display. So it can be faster when the scale moves to the
245 // next level. We choose the level closest to the current scale.
246 if (mLevel != mLevelCount) {
247 Rect range = mTileRange;
248 getRange(range, mCenterX, mCenterY, mLevel, mScale, mRotation);
249 mOffsetX = Math.round(mViewWidth / 2f + (range.left - mCenterX) * mScale);
250 mOffsetY = Math.round(mViewHeight / 2f + (range.top - mCenterY) * mScale);
251 fromLevel = mScale * (1 << mLevel) > 0.75f ? mLevel - 1 : mLevel;
253 // Activate the tiles of the smallest two levels.
254 fromLevel = mLevel - 2;
255 mOffsetX = Math.round(mViewWidth / 2f - mCenterX * mScale);
256 mOffsetY = Math.round(mViewHeight / 2f - mCenterY * mScale);
259 fromLevel = Math.max(0, Math.min(fromLevel, mLevelCount - 2));
260 endLevel = Math.min(fromLevel + 2, mLevelCount);
262 Rect range[] = mActiveRange;
263 for (int i = fromLevel; i < endLevel; ++i) {
264 getRange(range[i - fromLevel], mCenterX, mCenterY, i, mRotation);
267 // If rotation is transient, don't update the tile.
268 if (mRotation % 90 != 0) return;
270 synchronized (mQueueLock) {
271 mDecodeQueue.clean();
272 mUploadQueue.clean();
273 mBackgroundTileUploaded = false;
275 // Recycle unused tiles: if the level of the active tile is outside the
276 // range [fromLevel, endLevel) or not in the visible range.
277 int n = mActiveTiles.size();
278 for (int i = 0; i < n; i++) {
279 Tile tile = mActiveTiles.valueAt(i);
280 int level = tile.mTileLevel;
281 if (level < fromLevel || level >= endLevel
282 || !range[level - fromLevel].contains(tile.mX, tile.mY)) {
283 mActiveTiles.removeAt(i);
291 for (int i = fromLevel; i < endLevel; ++i) {
292 int size = mTileSize << i;
293 Rect r = range[i - fromLevel];
294 for (int y = r.top, bottom = r.bottom; y < bottom; y += size) {
295 for (int x = r.left, right = r.right; x < right; x += size) {
296 activateTile(x, y, i);
303 private void invalidateTiles() {
304 synchronized (mQueueLock) {
305 mDecodeQueue.clean();
306 mUploadQueue.clean();
308 // TODO disable decoder
309 int n = mActiveTiles.size();
310 for (int i = 0; i < n; i++) {
311 Tile tile = mActiveTiles.valueAt(i);
314 mActiveTiles.clear();
318 private void getRange(Rect out, int cX, int cY, int level, int rotation) {
319 getRange(out, cX, cY, level, 1f / (1 << (level + 1)), rotation);
322 // If the bitmap is scaled by the given factor "scale", return the
323 // rectangle containing visible range. The left-top coordinate returned is
324 // aligned to the tile boundary.
326 // (cX, cY) is the point on the original bitmap which will be put in the
327 // center of the ImageViewer.
328 private void getRange(Rect out,
329 int cX, int cY, int level, float scale, int rotation) {
331 double radians = Math.toRadians(-rotation);
332 double w = mViewWidth;
333 double h = mViewHeight;
335 double cos = Math.cos(radians);
336 double sin = Math.sin(radians);
337 int width = (int) Math.ceil(Math.max(
338 Math.abs(cos * w - sin * h), Math.abs(cos * w + sin * h)));
339 int height = (int) Math.ceil(Math.max(
340 Math.abs(sin * w + cos * h), Math.abs(sin * w - cos * h)));
342 int left = (int) Math.floor(cX - width / (2f * scale));
343 int top = (int) Math.floor(cY - height / (2f * scale));
344 int right = (int) Math.ceil(left + width / scale);
345 int bottom = (int) Math.ceil(top + height / scale);
347 // align the rectangle to tile boundary
348 int size = mTileSize << level;
349 left = Math.max(0, size * (left / size));
350 top = Math.max(0, size * (top / size));
351 right = Math.min(mImageWidth, right);
352 bottom = Math.min(mImageHeight, bottom);
354 out.set(left, top, right, bottom);
357 public void freeTextures() {
360 synchronized (mQueueLock) {
361 mUploadQueue.clean();
362 mDecodeQueue.clean();
363 Tile tile = mRecycledQueue.pop();
364 while (tile != null) {
366 tile = mRecycledQueue.pop();
370 int n = mActiveTiles.size();
371 for (int i = 0; i < n; i++) {
372 Tile texture = mActiveTiles.valueAt(i);
375 mActiveTiles.clear();
376 mTileRange.set(0, 0, 0, 0);
378 if (sTilePool != null) sTilePool.clear();
381 public void draw(GLCanvas canvas) {
385 mUploadQuota = UPLOAD_LIMIT;
386 mRenderComplete = true;
389 int rotation = mRotation;
391 if (rotation != 0) flags |= GLCanvas.SAVE_FLAG_MATRIX;
396 int centerX = mViewWidth / 2, centerY = mViewHeight / 2;
397 canvas.translate(centerX, centerY);
398 canvas.rotate(rotation, 0, 0, 1);
399 canvas.translate(-centerX, -centerY);
403 if (level != mLevelCount) {
404 int size = (mTileSize << level);
405 float length = size * mScale;
408 for (int ty = r.top, i = 0; ty < r.bottom; ty += size, i++) {
409 float y = mOffsetY + i * length;
410 for (int tx = r.left, j = 0; tx < r.right; tx += size, j++) {
411 float x = mOffsetX + j * length;
412 drawTile(canvas, tx, ty, level, x, y, length);
417 if (flags != 0) canvas.restore();
420 if (mRenderComplete) {
421 if (!mBackgroundTileUploaded) {
422 uploadBackgroundTiles(canvas);
429 private void uploadBackgroundTiles(GLCanvas canvas) {
430 mBackgroundTileUploaded = true;
431 int n = mActiveTiles.size();
432 for (int i = 0; i < n; i++) {
433 Tile tile = mActiveTiles.valueAt(i);
434 if (!tile.isContentValid()) {
435 queueForDecode(tile);
440 private void queueForUpload(Tile tile) {
441 synchronized (mQueueLock) {
442 mUploadQueue.push(tile);
446 // if (mTileUploader.mActive.compareAndSet(false, true)) {
447 // getGLRoot().addOnGLIdleListener(mTileUploader);
451 private void queueForDecode(Tile tile) {
452 synchronized (mQueueLock) {
453 if (tile.mTileState == STATE_ACTIVATED) {
454 tile.mTileState = STATE_IN_QUEUE;
455 if (mDecodeQueue.push(tile)) {
456 mQueueLock.notifyAll();
462 private boolean decodeTile(Tile tile) {
463 synchronized (mQueueLock) {
464 if (tile.mTileState != STATE_IN_QUEUE) return false;
465 tile.mTileState = STATE_DECODING;
467 boolean decodeComplete = tile.decode();
468 synchronized (mQueueLock) {
469 if (tile.mTileState == STATE_RECYCLING) {
470 tile.mTileState = STATE_RECYCLED;
471 if (tile.mDecodedTile != null) {
472 if (sTilePool != null) sTilePool.put(tile.mDecodedTile);
473 tile.mDecodedTile = null;
475 mRecycledQueue.push(tile);
478 tile.mTileState = decodeComplete ? STATE_DECODED : STATE_DECODE_FAIL;
479 return decodeComplete;
483 private Tile obtainTile(int x, int y, int level) {
484 synchronized (mQueueLock) {
485 Tile tile = mRecycledQueue.pop();
487 tile.mTileState = STATE_ACTIVATED;
488 tile.update(x, y, level);
491 return new Tile(x, y, level);
495 private void recycleTile(Tile tile) {
496 synchronized (mQueueLock) {
497 if (tile.mTileState == STATE_DECODING) {
498 tile.mTileState = STATE_RECYCLING;
501 tile.mTileState = STATE_RECYCLED;
502 if (tile.mDecodedTile != null) {
503 if (sTilePool != null) sTilePool.put(tile.mDecodedTile);
504 tile.mDecodedTile = null;
506 mRecycledQueue.push(tile);
510 private void activateTile(int x, int y, int level) {
511 long key = makeTileKey(x, y, level);
512 Tile tile = mActiveTiles.get(key);
514 if (tile.mTileState == STATE_IN_QUEUE) {
515 tile.mTileState = STATE_ACTIVATED;
519 tile = obtainTile(x, y, level);
520 mActiveTiles.put(key, tile);
523 private Tile getTile(int x, int y, int level) {
524 return mActiveTiles.get(makeTileKey(x, y, level));
527 private static long makeTileKey(int x, int y, int level) {
529 result = (result << 16) | y;
530 result = (result << 16) | level;
534 private void uploadTiles(GLCanvas canvas) {
535 int quota = UPLOAD_LIMIT;
538 synchronized (mQueueLock) {
539 tile = mUploadQueue.pop();
541 if (tile == null) break;
542 if (!tile.isContentValid()) {
543 Utils.assertTrue(tile.mTileState == STATE_DECODED);
544 tile.updateContent(canvas);
553 // Draw the tile to a square at canvas that locates at (x, y) and
554 // has a side length of length.
555 private void drawTile(GLCanvas canvas,
556 int tx, int ty, int level, float x, float y, float length) {
557 RectF source = mSourceRect;
558 RectF target = mTargetRect;
559 target.set(x, y, x + length, y + length);
560 source.set(0, 0, mTileSize, mTileSize);
562 Tile tile = getTile(tx, ty, level);
564 if (!tile.isContentValid()) {
565 if (tile.mTileState == STATE_DECODED) {
566 if (mUploadQuota > 0) {
568 tile.updateContent(canvas);
570 mRenderComplete = false;
572 } else if (tile.mTileState != STATE_DECODE_FAIL){
573 mRenderComplete = false;
574 queueForDecode(tile);
577 drawTile(tile, canvas, source, target);
581 private boolean drawTile(
582 Tile tile, GLCanvas canvas, RectF source, RectF target) {
584 if (tile.isContentValid()) {
585 canvas.drawTexture(tile, source, target);
589 // Parent can be divided to four quads and tile is one of the four.
590 Tile parent = tile.getParentTile();
591 if (parent == null) return false;
592 if (tile.mX == parent.mX) {
596 source.left = (mTileSize + source.left) / 2f;
597 source.right = (mTileSize + source.right) / 2f;
599 if (tile.mY == parent.mY) {
603 source.top = (mTileSize + source.top) / 2f;
604 source.bottom = (mTileSize + source.bottom) / 2f;
610 private class Tile extends UploadedTexture {
613 public int mTileLevel;
615 public Bitmap mDecodedTile;
616 public volatile int mTileState = STATE_ACTIVATED;
618 public Tile(int x, int y, int level) {
625 protected void onFreeBitmap(Bitmap bitmap) {
626 if (sTilePool != null) sTilePool.put(bitmap);
630 // Get a tile from the original image. The tile is down-scaled
631 // by (1 << mTilelevel) from a region in the original image.
633 Bitmap reuse = sTilePool.get(mTileSize, mTileSize);
634 mDecodedTile = mModel.getTile(mTileLevel, mX, mY, reuse);
635 } catch (Throwable t) {
636 Log.w(TAG, "fail to decode tile", t);
638 return mDecodedTile != null;
642 protected Bitmap onGetBitmap() {
643 Utils.assertTrue(mTileState == STATE_DECODED);
645 // We need to override the width and height, so that we won't
646 // draw beyond the boundaries.
647 int rightEdge = ((mImageWidth - mX) >> mTileLevel);
648 int bottomEdge = ((mImageHeight - mY) >> mTileLevel);
649 setSize(Math.min(mTileSize, rightEdge), Math.min(mTileSize, bottomEdge));
651 Bitmap bitmap = mDecodedTile;
653 mTileState = STATE_ACTIVATED;
657 // We override getTextureWidth() and getTextureHeight() here, so the
658 // texture can be re-used for different tiles regardless of the actual
659 // size of the tile (which may be small because it is a tile at the
662 public int getTextureWidth() {
667 public int getTextureHeight() {
671 public void update(int x, int y, int level) {
678 public Tile getParentTile() {
679 if (mTileLevel + 1 == mLevelCount) return null;
680 int size = mTileSize << (mTileLevel + 1);
681 int x = size * (mX / size);
682 int y = size * (mY / size);
683 return getTile(x, y, mTileLevel + 1);
687 public String toString() {
688 return String.format("tile(%s, %s, %s / %s)",
689 mX / mTileSize, mY / mTileSize, mLevel, mLevelCount);
693 private static class TileQueue {
698 if (tile != null) mHead = tile.mNext;
702 public boolean push(Tile tile) {
703 boolean wasEmpty = mHead == null;
709 public void clean() {
714 private class TileDecoder extends Thread {
716 public void finishAndWait() {
720 } catch (InterruptedException e) {
721 Log.w(TAG, "Interrupted while waiting for TileDecoder thread to finish!");
725 private Tile waitForTile() throws InterruptedException {
726 synchronized(mQueueLock) {
728 Tile tile = mDecodeQueue.pop();
740 while (!isInterrupted()) {
741 Tile tile = waitForTile();
742 if (decodeTile(tile)) {
743 queueForUpload(tile);
746 } catch (InterruptedException ex) {