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.util.FloatMath;
27 import android.view.WindowManager;
29 import com.android.gallery3d.app.GalleryContext;
30 import com.android.gallery3d.common.ApiHelper;
31 import com.android.gallery3d.common.Utils;
32 import com.android.gallery3d.data.BitmapPool;
33 import com.android.gallery3d.data.DecodeUtils;
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;
51 private static BitmapPool sTilePool;
54 * This is the tile state in the CPU side.
56 * ACTIVATED (initial state)
57 * --> IN_QUEUE - by queueForDecode()
58 * --> RECYCLED - by recycleTile()
59 * IN_QUEUE --> DECODING - by decodeTile()
60 * --> RECYCLED - by recycleTile)
61 * DECODING --> RECYCLING - by recycleTile()
62 * --> DECODED - by decodeTile()
63 * --> DECODE_FAIL - by decodeTile()
64 * RECYCLING --> RECYCLED - by decodeTile()
65 * DECODED --> ACTIVATED - (after the decoded bitmap is uploaded)
66 * DECODED --> RECYCLED - by recycleTile()
67 * DECODE_FAIL -> RECYCLED - by recycleTile()
68 * RECYCLED --> ACTIVATED - by obtainTile()
70 private static final int STATE_ACTIVATED = 0x01;
71 private static final int STATE_IN_QUEUE = 0x02;
72 private static final int STATE_DECODING = 0x04;
73 private static final int STATE_DECODED = 0x08;
74 private static final int STATE_DECODE_FAIL = 0x10;
75 private static final int STATE_RECYCLING = 0x20;
76 private static final int STATE_RECYCLED = 0x40;
78 private TileSource mModel;
79 private ScreenNail mScreenNail;
80 protected int mLevelCount; // cache the value of mScaledBitmaps.length
82 // The mLevel variable indicates which level of bitmap we should use.
83 // Level 0 means the original full-sized bitmap, and a larger value means
84 // a smaller scaled bitmap (The width and height of each scaled bitmap is
85 // half size of the previous one). If the value is in [0, mLevelCount), we
86 // use the bitmap in mScaledBitmaps[mLevel] for display, otherwise the value
87 // is mLevelCount, and that means we use mScreenNail for display.
88 private int mLevel = 0;
90 // The offsets of the (left, top) of the upper-left tile to the (left, top)
95 private int mUploadQuota;
96 private boolean mRenderComplete;
98 private final RectF mSourceRect = new RectF();
99 private final RectF mTargetRect = new RectF();
101 private final LongSparseArray<Tile> mActiveTiles = new LongSparseArray<Tile>();
103 // The following three queue is guarded by TileImageView.this
104 private final TileQueue mRecycledQueue = new TileQueue();
105 private final TileQueue mUploadQueue = new TileQueue();
106 private final TileQueue mDecodeQueue = new TileQueue();
108 // The width and height of the full-sized bitmap
109 protected int mImageWidth = SIZE_UNKNOWN;
110 protected int mImageHeight = SIZE_UNKNOWN;
112 protected int mCenterX;
113 protected int mCenterY;
114 protected float mScale;
115 protected int mRotation;
117 // Temp variables to avoid memory allocation
118 private final Rect mTileRange = new Rect();
119 private final Rect mActiveRange[] = {new Rect(), new Rect()};
121 private final TileUploader mTileUploader = new TileUploader();
122 private boolean mIsTextureFreed;
123 private Future<Void> mTileDecoder;
124 private final ThreadPool mThreadPool;
125 private boolean mBackgroundTileUploaded;
127 public static interface TileSource {
128 public int getLevelCount();
129 public ScreenNail getScreenNail();
130 public int getImageWidth();
131 public int getImageHeight();
133 // The tile returned by this method can be specified this way: Assuming
134 // the image size is (width, height), first take the intersection of (0,
135 // 0) - (width, height) and (x, y) - (x + tileSize, y + tileSize). If
136 // in extending the region, we found some part of the region are outside
137 // the image, those pixels are filled with black.
139 // If level > 0, it does the same operation on a down-scaled version of
140 // the original image (down-scaled by a factor of 2^level), but (x, y)
141 // still refers to the coordinate on the original image.
143 // The method would be called in another thread.
144 public Bitmap getTile(int level, int x, int y, int tileSize,
148 public static boolean isHighResolution(Context context) {
149 DisplayMetrics metrics = new DisplayMetrics();
150 WindowManager wm = (WindowManager)
151 context.getSystemService(Context.WINDOW_SERVICE);
152 wm.getDefaultDisplay().getMetrics(metrics);
153 return metrics.heightPixels > 2048 || metrics.widthPixels > 2048;
156 public TileImageView(GalleryContext context) {
157 mThreadPool = context.getThreadPool();
158 mTileDecoder = mThreadPool.submit(new TileDecoder());
159 if (sTileSize == 0) {
160 if (isHighResolution(context.getAndroidContext())) {
166 ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_REGION_DECODER
167 ? new BitmapPool(sTileSize, sTileSize, 128)
172 public void setModel(TileSource model) {
174 if (model != null) notifyModelInvalidated();
177 public void setScreenNail(ScreenNail s) {
181 public void notifyModelInvalidated() {
183 if (mModel == null) {
189 setScreenNail(mModel.getScreenNail());
190 mImageWidth = mModel.getImageWidth();
191 mImageHeight = mModel.getImageHeight();
192 mLevelCount = mModel.getLevelCount();
194 layoutTiles(mCenterX, mCenterY, mScale, mRotation);
199 protected void onLayout(
200 boolean changeSize, int left, int top, int right, int bottom) {
201 super.onLayout(changeSize, left, top, right, bottom);
202 if (changeSize) layoutTiles(mCenterX, mCenterY, mScale, mRotation);
205 // Prepare the tiles we want to use for display.
207 // 1. Decide the tile level we want to use for display.
208 // 2. Decide the tile levels we want to keep as texture (in addition to
209 // the one we use for display).
210 // 3. Recycle unused tiles.
211 // 4. Activate the tiles we want.
212 private void layoutTiles(int centerX, int centerY, float scale, int rotation) {
213 // The width and height of this view.
214 int width = getWidth();
215 int height = getHeight();
217 // The tile levels we want to keep as texture is in the range
218 // [fromLevel, endLevel).
222 // We want to use a texture larger than or equal to the display size.
223 mLevel = Utils.clamp(Utils.floorLog2(1f / scale), 0, mLevelCount);
225 // We want to keep one more tile level as texture in addition to what
226 // we use for display. So it can be faster when the scale moves to the
227 // next level. We choose a level closer to the current scale.
228 if (mLevel != mLevelCount) {
229 Rect range = mTileRange;
230 getRange(range, centerX, centerY, mLevel, scale, rotation);
231 mOffsetX = Math.round(width / 2f + (range.left - centerX) * scale);
232 mOffsetY = Math.round(height / 2f + (range.top - centerY) * scale);
233 fromLevel = scale * (1 << mLevel) > 0.75f ? mLevel - 1 : mLevel;
235 // Activate the tiles of the smallest two levels.
236 fromLevel = mLevel - 2;
237 mOffsetX = Math.round(width / 2f - centerX * scale);
238 mOffsetY = Math.round(height / 2f - centerY * scale);
241 fromLevel = Math.max(0, Math.min(fromLevel, mLevelCount - 2));
242 endLevel = Math.min(fromLevel + 2, mLevelCount);
244 Rect range[] = mActiveRange;
245 for (int i = fromLevel; i < endLevel; ++i) {
246 getRange(range[i - fromLevel], centerX, centerY, i, rotation);
249 // If rotation is transient, don't update the tile.
250 if (rotation % 90 != 0) return;
252 synchronized (this) {
253 mDecodeQueue.clean();
254 mUploadQueue.clean();
255 mBackgroundTileUploaded = false;
257 // Recycle unused tiles: if the level of the active tile is outside the
258 // range [fromLevel, endLevel) or not in the visible range.
259 int n = mActiveTiles.size();
260 for (int i = 0; i < n; i++) {
261 Tile tile = mActiveTiles.valueAt(i);
262 int level = tile.mTileLevel;
263 if (level < fromLevel || level >= endLevel
264 || !range[level - fromLevel].contains(tile.mX, tile.mY)) {
265 mActiveTiles.removeAt(i);
273 for (int i = fromLevel; i < endLevel; ++i) {
274 int size = sTileSize << i;
275 Rect r = range[i - fromLevel];
276 for (int y = r.top, bottom = r.bottom; y < bottom; y += size) {
277 for (int x = r.left, right = r.right; x < right; x += size) {
278 activateTile(x, y, i);
285 protected synchronized void invalidateTiles() {
286 mDecodeQueue.clean();
287 mUploadQueue.clean();
289 // TODO disable decoder
290 int n = mActiveTiles.size();
291 for (int i = 0; i < n; i++) {
292 Tile tile = mActiveTiles.valueAt(i);
295 mActiveTiles.clear();
298 private void getRange(Rect out, int cX, int cY, int level, int rotation) {
299 getRange(out, cX, cY, level, 1f / (1 << (level + 1)), rotation);
302 // If the bitmap is scaled by the given factor "scale", return the
303 // rectangle containing visible range. The left-top coordinate returned is
304 // aligned to the tile boundary.
306 // (cX, cY) is the point on the original bitmap which will be put in the
307 // center of the ImageViewer.
308 private void getRange(Rect out,
309 int cX, int cY, int level, float scale, int rotation) {
311 double radians = Math.toRadians(-rotation);
312 double w = getWidth();
313 double h = getHeight();
315 double cos = Math.cos(radians);
316 double sin = Math.sin(radians);
317 int width = (int) Math.ceil(Math.max(
318 Math.abs(cos * w - sin * h), Math.abs(cos * w + sin * h)));
319 int height = (int) Math.ceil(Math.max(
320 Math.abs(sin * w + cos * h), Math.abs(sin * w - cos * h)));
322 int left = (int) FloatMath.floor(cX - width / (2f * scale));
323 int top = (int) FloatMath.floor(cY - height / (2f * scale));
324 int right = (int) FloatMath.ceil(left + width / scale);
325 int bottom = (int) FloatMath.ceil(top + height / scale);
327 // align the rectangle to tile boundary
328 int size = sTileSize << level;
329 left = Math.max(0, size * (left / size));
330 top = Math.max(0, size * (top / size));
331 right = Math.min(mImageWidth, right);
332 bottom = Math.min(mImageHeight, bottom);
334 out.set(left, top, right, bottom);
337 // Calculate where the center of the image is, in the view coordinates.
338 public void getImageCenter(Point center) {
339 // The width and height of this view.
340 int viewW = getWidth();
341 int viewH = getHeight();
343 // The distance between the center of the view to the center of the
344 // bitmap, in bitmap units. (mCenterX and mCenterY are the bitmap
345 // coordinates correspond to the center of view)
347 if (mRotation % 180 == 0) {
348 distW = mImageWidth / 2 - mCenterX;
349 distH = mImageHeight / 2 - mCenterY;
351 distW = mImageHeight / 2 - mCenterY;
352 distH = mImageWidth / 2 - mCenterX;
355 // Convert to view coordinates. mScale translates from bitmap units to
357 center.x = Math.round(viewW / 2f + distW * mScale);
358 center.y = Math.round(viewH / 2f + distH * mScale);
361 public boolean setPosition(int centerX, int centerY, float scale, int rotation) {
362 if (mCenterX == centerX && mCenterY == centerY
363 && mScale == scale && mRotation == rotation) return false;
367 mRotation = rotation;
368 layoutTiles(centerX, centerY, scale, rotation);
373 public void freeTextures() {
374 mIsTextureFreed = true;
376 if (mTileDecoder != null) {
377 mTileDecoder.cancel();
382 int n = mActiveTiles.size();
383 for (int i = 0; i < n; i++) {
384 Tile texture = mActiveTiles.valueAt(i);
387 mActiveTiles.clear();
388 mTileRange.set(0, 0, 0, 0);
390 synchronized (this) {
391 mUploadQueue.clean();
392 mDecodeQueue.clean();
393 Tile tile = mRecycledQueue.pop();
394 while (tile != null) {
396 tile = mRecycledQueue.pop();
400 if (sTilePool != null) sTilePool.clear();
403 public void prepareTextures() {
404 if (mTileDecoder == null) {
405 mTileDecoder = mThreadPool.submit(new TileDecoder());
407 if (mIsTextureFreed) {
408 layoutTiles(mCenterX, mCenterY, mScale, mRotation);
409 mIsTextureFreed = false;
410 setScreenNail(mModel == null ? null : mModel.getScreenNail());
415 protected void render(GLCanvas canvas) {
416 mUploadQuota = UPLOAD_LIMIT;
417 mRenderComplete = true;
420 int rotation = mRotation;
422 if (rotation != 0) flags |= GLCanvas.SAVE_FLAG_MATRIX;
427 int centerX = getWidth() / 2, centerY = getHeight() / 2;
428 canvas.translate(centerX, centerY);
429 canvas.rotate(rotation, 0, 0, 1);
430 canvas.translate(-centerX, -centerY);
434 if (level != mLevelCount && !isScreenNailAnimating()) {
435 if (mScreenNail != null) {
436 mScreenNail.noDraw();
439 int size = (sTileSize << level);
440 float length = size * mScale;
443 for (int ty = r.top, i = 0; ty < r.bottom; ty += size, i++) {
444 float y = mOffsetY + i * length;
445 for (int tx = r.left, j = 0; tx < r.right; tx += size, j++) {
446 float x = mOffsetX + j * length;
447 drawTile(canvas, tx, ty, level, x, y, length);
450 } else if (mScreenNail != null) {
451 mScreenNail.draw(canvas, mOffsetX, mOffsetY,
452 Math.round(mImageWidth * mScale),
453 Math.round(mImageHeight * mScale));
454 if (isScreenNailAnimating()) {
459 if (flags != 0) canvas.restore();
462 if (mRenderComplete) {
463 if (!mBackgroundTileUploaded) uploadBackgroundTiles(canvas);
469 private boolean isScreenNailAnimating() {
470 return (mScreenNail instanceof TiledScreenNail)
471 && ((TiledScreenNail) mScreenNail).isAnimating();
474 private void uploadBackgroundTiles(GLCanvas canvas) {
475 mBackgroundTileUploaded = true;
476 int n = mActiveTiles.size();
477 for (int i = 0; i < n; i++) {
478 Tile tile = mActiveTiles.valueAt(i);
479 if (!tile.isContentValid()) queueForDecode(tile);
483 void queueForUpload(Tile tile) {
484 synchronized (this) {
485 mUploadQueue.push(tile);
487 if (mTileUploader.mActive.compareAndSet(false, true)) {
488 getGLRoot().addOnGLIdleListener(mTileUploader);
492 synchronized void queueForDecode(Tile tile) {
493 if (tile.mTileState == STATE_ACTIVATED) {
494 tile.mTileState = STATE_IN_QUEUE;
495 if (mDecodeQueue.push(tile)) notifyAll();
499 boolean decodeTile(Tile tile) {
500 synchronized (this) {
501 if (tile.mTileState != STATE_IN_QUEUE) return false;
502 tile.mTileState = STATE_DECODING;
504 boolean decodeComplete = tile.decode();
505 synchronized (this) {
506 if (tile.mTileState == STATE_RECYCLING) {
507 tile.mTileState = STATE_RECYCLED;
508 if (tile.mDecodedTile != null) {
509 if (sTilePool != null) sTilePool.recycle(tile.mDecodedTile);
510 tile.mDecodedTile = null;
512 mRecycledQueue.push(tile);
515 tile.mTileState = decodeComplete ? STATE_DECODED : STATE_DECODE_FAIL;
516 return decodeComplete;
520 private synchronized Tile obtainTile(int x, int y, int level) {
521 Tile tile = mRecycledQueue.pop();
523 tile.mTileState = STATE_ACTIVATED;
524 tile.update(x, y, level);
527 return new Tile(x, y, level);
530 synchronized void recycleTile(Tile tile) {
531 if (tile.mTileState == STATE_DECODING) {
532 tile.mTileState = STATE_RECYCLING;
535 tile.mTileState = STATE_RECYCLED;
536 if (tile.mDecodedTile != null) {
537 if (sTilePool != null) sTilePool.recycle(tile.mDecodedTile);
538 tile.mDecodedTile = null;
540 mRecycledQueue.push(tile);
543 private void activateTile(int x, int y, int level) {
544 long key = makeTileKey(x, y, level);
545 Tile tile = mActiveTiles.get(key);
547 if (tile.mTileState == STATE_IN_QUEUE) {
548 tile.mTileState = STATE_ACTIVATED;
552 tile = obtainTile(x, y, level);
553 mActiveTiles.put(key, tile);
556 private Tile getTile(int x, int y, int level) {
557 return mActiveTiles.get(makeTileKey(x, y, level));
560 private static long makeTileKey(int x, int y, int level) {
562 result = (result << 16) | y;
563 result = (result << 16) | level;
567 private class TileUploader implements GLRoot.OnGLIdleListener {
568 AtomicBoolean mActive = new AtomicBoolean(false);
571 public boolean onGLIdle(GLCanvas canvas, boolean renderRequested) {
572 // Skips uploading if there is a pending rendering request.
573 // Returns true to keep uploading in next rendering loop.
574 if (renderRequested) return true;
575 int quota = UPLOAD_LIMIT;
578 synchronized (TileImageView.this) {
579 tile = mUploadQueue.pop();
581 if (tile == null) break;
582 if (!tile.isContentValid()) {
583 boolean hasBeenLoaded = tile.isLoaded();
584 Utils.assertTrue(tile.mTileState == STATE_DECODED);
585 tile.updateContent(canvas);
586 if (!hasBeenLoaded) tile.draw(canvas, 0, 0);
590 if (tile == null) mActive.set(false);
595 // Draw the tile to a square at canvas that locates at (x, y) and
596 // has a side length of length.
597 public void drawTile(GLCanvas canvas,
598 int tx, int ty, int level, float x, float y, float length) {
599 RectF source = mSourceRect;
600 RectF target = mTargetRect;
601 target.set(x, y, x + length, y + length);
602 source.set(0, 0, sTileSize, sTileSize);
604 Tile tile = getTile(tx, ty, level);
606 if (!tile.isContentValid()) {
607 if (tile.mTileState == STATE_DECODED) {
608 if (mUploadQuota > 0) {
610 tile.updateContent(canvas);
612 mRenderComplete = false;
614 } else if (tile.mTileState != STATE_DECODE_FAIL){
615 mRenderComplete = false;
616 queueForDecode(tile);
619 if (drawTile(tile, canvas, source, target)) return;
621 if (mScreenNail != null) {
622 int size = sTileSize << level;
623 float scaleX = (float) mScreenNail.getWidth() / mImageWidth;
624 float scaleY = (float) mScreenNail.getHeight() / mImageHeight;
625 source.set(tx * scaleX, ty * scaleY, (tx + size) * scaleX,
626 (ty + size) * scaleY);
627 mScreenNail.draw(canvas, source, target);
631 static boolean drawTile(
632 Tile tile, GLCanvas canvas, RectF source, RectF target) {
634 if (tile.isContentValid()) {
635 canvas.drawTexture(tile, source, target);
639 // Parent can be divided to four quads and tile is one of the four.
640 Tile parent = tile.getParentTile();
641 if (parent == null) return false;
642 if (tile.mX == parent.mX) {
646 source.left = (sTileSize + source.left) / 2f;
647 source.right = (sTileSize + source.right) / 2f;
649 if (tile.mY == parent.mY) {
653 source.top = (sTileSize + source.top) / 2f;
654 source.bottom = (sTileSize + source.bottom) / 2f;
660 private class Tile extends UploadedTexture {
663 public int mTileLevel;
665 public Bitmap mDecodedTile;
666 public volatile int mTileState = STATE_ACTIVATED;
668 public Tile(int x, int y, int level) {
675 protected void onFreeBitmap(Bitmap bitmap) {
676 if (sTilePool != null) sTilePool.recycle(bitmap);
680 // Get a tile from the original image. The tile is down-scaled
681 // by (1 << mTilelevel) from a region in the original image.
683 mDecodedTile = DecodeUtils.ensureGLCompatibleBitmap(mModel.getTile(
684 mTileLevel, mX, mY, sTileSize, sTilePool));
685 } catch (Throwable t) {
686 Log.w(TAG, "fail to decode tile", t);
688 return mDecodedTile != null;
692 protected Bitmap onGetBitmap() {
693 Utils.assertTrue(mTileState == STATE_DECODED);
695 // We need to override the width and height, so that we won't
696 // draw beyond the boundaries.
697 int rightEdge = ((mImageWidth - mX) >> mTileLevel);
698 int bottomEdge = ((mImageHeight - mY) >> mTileLevel);
699 setSize(Math.min(sTileSize, rightEdge), Math.min(sTileSize, bottomEdge));
701 Bitmap bitmap = mDecodedTile;
703 mTileState = STATE_ACTIVATED;
707 // We override getTextureWidth() and getTextureHeight() here, so the
708 // texture can be re-used for different tiles regardless of the actual
709 // size of the tile (which may be small because it is a tile at the
712 public int getTextureWidth() {
717 public int getTextureHeight() {
721 public void update(int x, int y, int level) {
728 public Tile getParentTile() {
729 if (mTileLevel + 1 == mLevelCount) return null;
730 int size = sTileSize << (mTileLevel + 1);
731 int x = size * (mX / size);
732 int y = size * (mY / size);
733 return getTile(x, y, mTileLevel + 1);
737 public String toString() {
738 return String.format("tile(%s, %s, %s / %s)",
739 mX / sTileSize, mY / sTileSize, mLevel, mLevelCount);
743 private static class TileQueue {
748 if (tile != null) mHead = tile.mNext;
752 public boolean push(Tile tile) {
753 boolean wasEmpty = mHead == null;
759 public void clean() {
764 private class TileDecoder implements ThreadPool.Job<Void> {
766 private CancelListener mNotifier = new CancelListener() {
768 public void onCancel() {
769 synchronized (TileImageView.this) {
770 TileImageView.this.notifyAll();
776 public Void run(JobContext jc) {
777 jc.setMode(ThreadPool.MODE_NONE);
778 jc.setCancelListener(mNotifier);
779 while (!jc.isCancelled()) {
781 synchronized(TileImageView.this) {
782 tile = mDecodeQueue.pop();
783 if (tile == null && !jc.isCancelled()) {
784 Utils.waitWithoutInterrupt(TileImageView.this);
787 if (tile == null) continue;
788 if (decodeTile(tile)) queueForUpload(tile);