2 * Copyright (C) 2010 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.gallery3d.ui;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Point;
22 import android.graphics.Rect;
23 import android.graphics.RectF;
24 import android.support.v4.util.LongSparseArray;
25 import android.util.DisplayMetrics;
26 import android.view.WindowManager;
28 import com.android.gallery3d.app.GalleryContext;
29 import com.android.gallery3d.common.Utils;
30 import com.android.gallery3d.data.DecodeUtils;
31 import com.android.photos.data.GalleryBitmapPool;
32 import com.android.gallery3d.glrenderer.GLCanvas;
33 import com.android.gallery3d.glrenderer.UploadedTexture;
34 import com.android.gallery3d.util.Future;
35 import com.android.gallery3d.util.ThreadPool;
36 import com.android.gallery3d.util.ThreadPool.CancelListener;
37 import com.android.gallery3d.util.ThreadPool.JobContext;
39 import java.util.concurrent.atomic.AtomicBoolean;
41 public class TileImageView extends GLView {
42 public static final int SIZE_UNKNOWN = -1;
44 @SuppressWarnings("unused")
45 private static final String TAG = "TileImageView";
46 private static final int UPLOAD_LIMIT = 1;
48 // TILE_SIZE must be 2^N
49 private static int sTileSize;
52 * This is the tile state in the CPU side.
54 * ACTIVATED (initial state)
55 * --> IN_QUEUE - by queueForDecode()
56 * --> RECYCLED - by recycleTile()
57 * IN_QUEUE --> DECODING - by decodeTile()
58 * --> RECYCLED - by recycleTile)
59 * DECODING --> RECYCLING - by recycleTile()
60 * --> DECODED - by decodeTile()
61 * --> DECODE_FAIL - by decodeTile()
62 * RECYCLING --> RECYCLED - by decodeTile()
63 * DECODED --> ACTIVATED - (after the decoded bitmap is uploaded)
64 * DECODED --> RECYCLED - by recycleTile()
65 * DECODE_FAIL -> RECYCLED - by recycleTile()
66 * RECYCLED --> ACTIVATED - by obtainTile()
68 private static final int STATE_ACTIVATED = 0x01;
69 private static final int STATE_IN_QUEUE = 0x02;
70 private static final int STATE_DECODING = 0x04;
71 private static final int STATE_DECODED = 0x08;
72 private static final int STATE_DECODE_FAIL = 0x10;
73 private static final int STATE_RECYCLING = 0x20;
74 private static final int STATE_RECYCLED = 0x40;
76 private TileSource mModel;
77 private ScreenNail mScreenNail;
78 protected int mLevelCount; // cache the value of mScaledBitmaps.length
80 // The mLevel variable indicates which level of bitmap we should use.
81 // Level 0 means the original full-sized bitmap, and a larger value means
82 // a smaller scaled bitmap (The width and height of each scaled bitmap is
83 // half size of the previous one). If the value is in [0, mLevelCount), we
84 // use the bitmap in mScaledBitmaps[mLevel] for display, otherwise the value
85 // is mLevelCount, and that means we use mScreenNail for display.
86 private int mLevel = 0;
88 // The offsets of the (left, top) of the upper-left tile to the (left, top)
93 private int mUploadQuota;
94 private boolean mRenderComplete;
96 private final RectF mSourceRect = new RectF();
97 private final RectF mTargetRect = new RectF();
99 private final LongSparseArray<Tile> mActiveTiles = new LongSparseArray<Tile>();
101 // The following three queue is guarded by TileImageView.this
102 private final TileQueue mRecycledQueue = new TileQueue();
103 private final TileQueue mUploadQueue = new TileQueue();
104 private final TileQueue mDecodeQueue = new TileQueue();
106 // The width and height of the full-sized bitmap
107 protected int mImageWidth = SIZE_UNKNOWN;
108 protected int mImageHeight = SIZE_UNKNOWN;
110 protected int mCenterX;
111 protected int mCenterY;
112 protected float mScale;
113 protected int mRotation;
115 // Temp variables to avoid memory allocation
116 private final Rect mTileRange = new Rect();
117 private final Rect mActiveRange[] = {new Rect(), new Rect()};
119 private final TileUploader mTileUploader = new TileUploader();
120 private boolean mIsTextureFreed;
121 private Future<Void> mTileDecoder;
122 private final ThreadPool mThreadPool;
123 private boolean mBackgroundTileUploaded;
125 public static interface TileSource {
126 public int getLevelCount();
127 public ScreenNail getScreenNail();
128 public int getImageWidth();
129 public int getImageHeight();
131 // The tile returned by this method can be specified this way: Assuming
132 // the image size is (width, height), first take the intersection of (0,
133 // 0) - (width, height) and (x, y) - (x + tileSize, y + tileSize). If
134 // in extending the region, we found some part of the region are outside
135 // the image, those pixels are filled with black.
137 // If level > 0, it does the same operation on a down-scaled version of
138 // the original image (down-scaled by a factor of 2^level), but (x, y)
139 // still refers to the coordinate on the original image.
141 // The method would be called in another thread.
142 public Bitmap getTile(int level, int x, int y, int tileSize);
145 public static boolean isHighResolution(Context context) {
146 DisplayMetrics metrics = new DisplayMetrics();
147 WindowManager wm = (WindowManager)
148 context.getSystemService(Context.WINDOW_SERVICE);
149 wm.getDefaultDisplay().getMetrics(metrics);
150 return metrics.heightPixels > 2048 || metrics.widthPixels > 2048;
153 public TileImageView(GalleryContext context) {
154 mThreadPool = context.getThreadPool();
155 mTileDecoder = mThreadPool.submit(new TileDecoder());
156 if (sTileSize == 0) {
157 if (isHighResolution(context.getAndroidContext())) {
165 public void setModel(TileSource model) {
167 if (model != null) notifyModelInvalidated();
170 public void setScreenNail(ScreenNail s) {
174 public void notifyModelInvalidated() {
176 if (mModel == null) {
182 setScreenNail(mModel.getScreenNail());
183 mImageWidth = mModel.getImageWidth();
184 mImageHeight = mModel.getImageHeight();
185 mLevelCount = mModel.getLevelCount();
187 layoutTiles(mCenterX, mCenterY, mScale, mRotation);
192 protected void onLayout(
193 boolean changeSize, int left, int top, int right, int bottom) {
194 super.onLayout(changeSize, left, top, right, bottom);
195 if (changeSize) layoutTiles(mCenterX, mCenterY, mScale, mRotation);
198 // Prepare the tiles we want to use for display.
200 // 1. Decide the tile level we want to use for display.
201 // 2. Decide the tile levels we want to keep as texture (in addition to
202 // the one we use for display).
203 // 3. Recycle unused tiles.
204 // 4. Activate the tiles we want.
205 private void layoutTiles(int centerX, int centerY, float scale, int rotation) {
206 // The width and height of this view.
207 int width = getWidth();
208 int height = getHeight();
210 // The tile levels we want to keep as texture is in the range
211 // [fromLevel, endLevel).
215 // We want to use a texture larger than or equal to the display size.
216 mLevel = Utils.clamp(Utils.floorLog2(1f / scale), 0, mLevelCount);
218 // We want to keep one more tile level as texture in addition to what
219 // we use for display. So it can be faster when the scale moves to the
220 // next level. We choose a level closer to the current scale.
221 if (mLevel != mLevelCount) {
222 Rect range = mTileRange;
223 getRange(range, centerX, centerY, mLevel, scale, rotation);
224 mOffsetX = Math.round(width / 2f + (range.left - centerX) * scale);
225 mOffsetY = Math.round(height / 2f + (range.top - centerY) * scale);
226 fromLevel = scale * (1 << mLevel) > 0.75f ? mLevel - 1 : mLevel;
228 // Activate the tiles of the smallest two levels.
229 fromLevel = mLevel - 2;
230 mOffsetX = Math.round(width / 2f - centerX * scale);
231 mOffsetY = Math.round(height / 2f - centerY * scale);
234 fromLevel = Math.max(0, Math.min(fromLevel, mLevelCount - 2));
235 endLevel = Math.min(fromLevel + 2, mLevelCount);
237 Rect range[] = mActiveRange;
238 for (int i = fromLevel; i < endLevel; ++i) {
239 getRange(range[i - fromLevel], centerX, centerY, i, rotation);
242 // If rotation is transient, don't update the tile.
243 if (rotation % 90 != 0) return;
245 synchronized (this) {
246 mDecodeQueue.clean();
247 mUploadQueue.clean();
248 mBackgroundTileUploaded = false;
250 // Recycle unused tiles: if the level of the active tile is outside the
251 // range [fromLevel, endLevel) or not in the visible range.
252 int n = mActiveTiles.size();
253 for (int i = 0; i < n; i++) {
254 Tile tile = mActiveTiles.valueAt(i);
255 int level = tile.mTileLevel;
256 if (level < fromLevel || level >= endLevel
257 || !range[level - fromLevel].contains(tile.mX, tile.mY)) {
258 mActiveTiles.removeAt(i);
266 for (int i = fromLevel; i < endLevel; ++i) {
267 int size = sTileSize << i;
268 Rect r = range[i - fromLevel];
269 for (int y = r.top, bottom = r.bottom; y < bottom; y += size) {
270 for (int x = r.left, right = r.right; x < right; x += size) {
271 activateTile(x, y, i);
278 protected synchronized void invalidateTiles() {
279 mDecodeQueue.clean();
280 mUploadQueue.clean();
282 // TODO disable decoder
283 int n = mActiveTiles.size();
284 for (int i = 0; i < n; i++) {
285 Tile tile = mActiveTiles.valueAt(i);
288 mActiveTiles.clear();
291 private void getRange(Rect out, int cX, int cY, int level, int rotation) {
292 getRange(out, cX, cY, level, 1f / (1 << (level + 1)), rotation);
295 // If the bitmap is scaled by the given factor "scale", return the
296 // rectangle containing visible range. The left-top coordinate returned is
297 // aligned to the tile boundary.
299 // (cX, cY) is the point on the original bitmap which will be put in the
300 // center of the ImageViewer.
301 private void getRange(Rect out,
302 int cX, int cY, int level, float scale, int rotation) {
304 double radians = Math.toRadians(-rotation);
305 double w = getWidth();
306 double h = getHeight();
308 double cos = Math.cos(radians);
309 double sin = Math.sin(radians);
310 int width = (int) Math.ceil(Math.max(
311 Math.abs(cos * w - sin * h), Math.abs(cos * w + sin * h)));
312 int height = (int) Math.ceil(Math.max(
313 Math.abs(sin * w + cos * h), Math.abs(sin * w - cos * h)));
315 int left = (int) Math.floor(cX - width / (2f * scale));
316 int top = (int) Math.floor(cY - height / (2f * scale));
317 int right = (int) Math.ceil(left + width / scale);
318 int bottom = (int) Math.ceil(top + height / scale);
320 // align the rectangle to tile boundary
321 int size = sTileSize << level;
322 left = Math.max(0, size * (left / size));
323 top = Math.max(0, size * (top / size));
324 right = Math.min(mImageWidth, right);
325 bottom = Math.min(mImageHeight, bottom);
327 out.set(left, top, right, bottom);
330 // Calculate where the center of the image is, in the view coordinates.
331 public void getImageCenter(Point center) {
332 // The width and height of this view.
333 int viewW = getWidth();
334 int viewH = getHeight();
336 // The distance between the center of the view to the center of the
337 // bitmap, in bitmap units. (mCenterX and mCenterY are the bitmap
338 // coordinates correspond to the center of view)
340 if (mRotation % 180 == 0) {
341 distW = mImageWidth / 2 - mCenterX;
342 distH = mImageHeight / 2 - mCenterY;
344 distW = mImageHeight / 2 - mCenterY;
345 distH = mImageWidth / 2 - mCenterX;
348 // Convert to view coordinates. mScale translates from bitmap units to
350 center.x = Math.round(viewW / 2f + distW * mScale);
351 center.y = Math.round(viewH / 2f + distH * mScale);
354 public boolean setPosition(int centerX, int centerY, float scale, int rotation) {
355 if (mCenterX == centerX && mCenterY == centerY
356 && mScale == scale && mRotation == rotation) return false;
360 mRotation = rotation;
361 layoutTiles(centerX, centerY, scale, rotation);
366 public void freeTextures() {
367 mIsTextureFreed = true;
369 if (mTileDecoder != null) {
370 mTileDecoder.cancel();
375 int n = mActiveTiles.size();
376 for (int i = 0; i < n; i++) {
377 Tile texture = mActiveTiles.valueAt(i);
380 mActiveTiles.clear();
381 mTileRange.set(0, 0, 0, 0);
383 synchronized (this) {
384 mUploadQueue.clean();
385 mDecodeQueue.clean();
386 Tile tile = mRecycledQueue.pop();
387 while (tile != null) {
389 tile = mRecycledQueue.pop();
395 public void prepareTextures() {
396 if (mTileDecoder == null) {
397 mTileDecoder = mThreadPool.submit(new TileDecoder());
399 if (mIsTextureFreed) {
400 layoutTiles(mCenterX, mCenterY, mScale, mRotation);
401 mIsTextureFreed = false;
402 setScreenNail(mModel == null ? null : mModel.getScreenNail());
407 protected void render(GLCanvas canvas) {
408 mUploadQuota = UPLOAD_LIMIT;
409 mRenderComplete = true;
412 int rotation = mRotation;
414 if (rotation != 0) flags |= GLCanvas.SAVE_FLAG_MATRIX;
419 int centerX = getWidth() / 2, centerY = getHeight() / 2;
420 canvas.translate(centerX, centerY);
421 canvas.rotate(rotation, 0, 0, 1);
422 canvas.translate(-centerX, -centerY);
426 if (level != mLevelCount && !isScreenNailAnimating()) {
427 if (mScreenNail != null) {
428 mScreenNail.noDraw();
431 int size = (sTileSize << level);
432 float length = size * mScale;
435 for (int ty = r.top, i = 0; ty < r.bottom; ty += size, i++) {
436 float y = mOffsetY + i * length;
437 for (int tx = r.left, j = 0; tx < r.right; tx += size, j++) {
438 float x = mOffsetX + j * length;
439 drawTile(canvas, tx, ty, level, x, y, length);
442 } else if (mScreenNail != null) {
443 mScreenNail.draw(canvas, mOffsetX, mOffsetY,
444 Math.round(mImageWidth * mScale),
445 Math.round(mImageHeight * mScale));
446 if (isScreenNailAnimating()) {
451 if (flags != 0) canvas.restore();
454 if (mRenderComplete) {
455 if (!mBackgroundTileUploaded) uploadBackgroundTiles(canvas);
461 private boolean isScreenNailAnimating() {
462 return (mScreenNail instanceof TiledScreenNail)
463 && ((TiledScreenNail) mScreenNail).isAnimating();
466 private void uploadBackgroundTiles(GLCanvas canvas) {
467 mBackgroundTileUploaded = true;
468 int n = mActiveTiles.size();
469 for (int i = 0; i < n; i++) {
470 Tile tile = mActiveTiles.valueAt(i);
471 if (!tile.isContentValid()) queueForDecode(tile);
475 void queueForUpload(Tile tile) {
476 synchronized (this) {
477 mUploadQueue.push(tile);
479 if (mTileUploader.mActive.compareAndSet(false, true)) {
480 getGLRoot().addOnGLIdleListener(mTileUploader);
484 synchronized void queueForDecode(Tile tile) {
485 if (tile.mTileState == STATE_ACTIVATED) {
486 tile.mTileState = STATE_IN_QUEUE;
487 if (mDecodeQueue.push(tile)) notifyAll();
491 boolean decodeTile(Tile tile) {
492 synchronized (this) {
493 if (tile.mTileState != STATE_IN_QUEUE) return false;
494 tile.mTileState = STATE_DECODING;
496 boolean decodeComplete = tile.decode();
497 synchronized (this) {
498 if (tile.mTileState == STATE_RECYCLING) {
499 tile.mTileState = STATE_RECYCLED;
500 if (tile.mDecodedTile != null) {
501 GalleryBitmapPool.getInstance().put(tile.mDecodedTile);
502 tile.mDecodedTile = null;
504 mRecycledQueue.push(tile);
507 tile.mTileState = decodeComplete ? STATE_DECODED : STATE_DECODE_FAIL;
508 return decodeComplete;
512 private synchronized Tile obtainTile(int x, int y, int level) {
513 Tile tile = mRecycledQueue.pop();
515 tile.mTileState = STATE_ACTIVATED;
516 tile.update(x, y, level);
519 return new Tile(x, y, level);
522 synchronized void recycleTile(Tile tile) {
523 if (tile.mTileState == STATE_DECODING) {
524 tile.mTileState = STATE_RECYCLING;
527 tile.mTileState = STATE_RECYCLED;
528 if (tile.mDecodedTile != null) {
529 GalleryBitmapPool.getInstance().put(tile.mDecodedTile);
530 tile.mDecodedTile = null;
532 mRecycledQueue.push(tile);
535 private void activateTile(int x, int y, int level) {
536 long key = makeTileKey(x, y, level);
537 Tile tile = mActiveTiles.get(key);
539 if (tile.mTileState == STATE_IN_QUEUE) {
540 tile.mTileState = STATE_ACTIVATED;
544 tile = obtainTile(x, y, level);
545 mActiveTiles.put(key, tile);
548 private Tile getTile(int x, int y, int level) {
549 return mActiveTiles.get(makeTileKey(x, y, level));
552 private static long makeTileKey(int x, int y, int level) {
554 result = (result << 16) | y;
555 result = (result << 16) | level;
559 private class TileUploader implements GLRoot.OnGLIdleListener {
560 AtomicBoolean mActive = new AtomicBoolean(false);
563 public boolean onGLIdle(GLCanvas canvas, boolean renderRequested) {
564 // Skips uploading if there is a pending rendering request.
565 // Returns true to keep uploading in next rendering loop.
566 if (renderRequested) return true;
567 int quota = UPLOAD_LIMIT;
570 synchronized (TileImageView.this) {
571 tile = mUploadQueue.pop();
573 if (tile == null) break;
574 if (!tile.isContentValid()) {
575 boolean hasBeenLoaded = tile.isLoaded();
576 Utils.assertTrue(tile.mTileState == STATE_DECODED);
577 tile.updateContent(canvas);
578 if (!hasBeenLoaded) tile.draw(canvas, 0, 0);
582 if (tile == null) mActive.set(false);
587 // Draw the tile to a square at canvas that locates at (x, y) and
588 // has a side length of length.
589 public void drawTile(GLCanvas canvas,
590 int tx, int ty, int level, float x, float y, float length) {
591 RectF source = mSourceRect;
592 RectF target = mTargetRect;
593 target.set(x, y, x + length, y + length);
594 source.set(0, 0, sTileSize, sTileSize);
596 Tile tile = getTile(tx, ty, level);
598 if (!tile.isContentValid()) {
599 if (tile.mTileState == STATE_DECODED) {
600 if (mUploadQuota > 0) {
602 tile.updateContent(canvas);
604 mRenderComplete = false;
606 } else if (tile.mTileState != STATE_DECODE_FAIL){
607 mRenderComplete = false;
608 queueForDecode(tile);
611 if (drawTile(tile, canvas, source, target)) return;
613 if (mScreenNail != null) {
614 int size = sTileSize << level;
615 float scaleX = (float) mScreenNail.getWidth() / mImageWidth;
616 float scaleY = (float) mScreenNail.getHeight() / mImageHeight;
617 source.set(tx * scaleX, ty * scaleY, (tx + size) * scaleX,
618 (ty + size) * scaleY);
619 mScreenNail.draw(canvas, source, target);
623 static boolean drawTile(
624 Tile tile, GLCanvas canvas, RectF source, RectF target) {
626 if (tile.isContentValid()) {
627 canvas.drawTexture(tile, source, target);
631 // Parent can be divided to four quads and tile is one of the four.
632 Tile parent = tile.getParentTile();
633 if (parent == null) return false;
634 if (tile.mX == parent.mX) {
638 source.left = (sTileSize + source.left) / 2f;
639 source.right = (sTileSize + source.right) / 2f;
641 if (tile.mY == parent.mY) {
645 source.top = (sTileSize + source.top) / 2f;
646 source.bottom = (sTileSize + source.bottom) / 2f;
652 private class Tile extends UploadedTexture {
655 public int mTileLevel;
657 public Bitmap mDecodedTile;
658 public volatile int mTileState = STATE_ACTIVATED;
660 public Tile(int x, int y, int level) {
667 protected void onFreeBitmap(Bitmap bitmap) {
668 GalleryBitmapPool.getInstance().put(bitmap);
672 // Get a tile from the original image. The tile is down-scaled
673 // by (1 << mTilelevel) from a region in the original image.
675 mDecodedTile = DecodeUtils.ensureGLCompatibleBitmap(mModel.getTile(
676 mTileLevel, mX, mY, sTileSize));
677 } catch (Throwable t) {
678 Log.w(TAG, "fail to decode tile", t);
680 return mDecodedTile != null;
684 protected Bitmap onGetBitmap() {
685 Utils.assertTrue(mTileState == STATE_DECODED);
687 // We need to override the width and height, so that we won't
688 // draw beyond the boundaries.
689 int rightEdge = ((mImageWidth - mX) >> mTileLevel);
690 int bottomEdge = ((mImageHeight - mY) >> mTileLevel);
691 setSize(Math.min(sTileSize, rightEdge), Math.min(sTileSize, bottomEdge));
693 Bitmap bitmap = mDecodedTile;
695 mTileState = STATE_ACTIVATED;
699 // We override getTextureWidth() and getTextureHeight() here, so the
700 // texture can be re-used for different tiles regardless of the actual
701 // size of the tile (which may be small because it is a tile at the
704 public int getTextureWidth() {
709 public int getTextureHeight() {
713 public void update(int x, int y, int level) {
720 public Tile getParentTile() {
721 if (mTileLevel + 1 == mLevelCount) return null;
722 int size = sTileSize << (mTileLevel + 1);
723 int x = size * (mX / size);
724 int y = size * (mY / size);
725 return getTile(x, y, mTileLevel + 1);
729 public String toString() {
730 return String.format("tile(%s, %s, %s / %s)",
731 mX / sTileSize, mY / sTileSize, mLevel, mLevelCount);
735 private static class TileQueue {
740 if (tile != null) mHead = tile.mNext;
744 public boolean push(Tile tile) {
745 boolean wasEmpty = mHead == null;
751 public void clean() {
756 private class TileDecoder implements ThreadPool.Job<Void> {
758 private CancelListener mNotifier = new CancelListener() {
760 public void onCancel() {
761 synchronized (TileImageView.this) {
762 TileImageView.this.notifyAll();
768 public Void run(JobContext jc) {
769 jc.setMode(ThreadPool.MODE_NONE);
770 jc.setCancelListener(mNotifier);
771 while (!jc.isCancelled()) {
773 synchronized(TileImageView.this) {
774 tile = mDecodeQueue.pop();
775 if (tile == null && !jc.isCancelled()) {
776 Utils.waitWithoutInterrupt(TileImageView.this);
779 if (tile == null) continue;
780 if (decodeTile(tile)) queueForUpload(tile);