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.graphics.Bitmap;
20 import android.graphics.Point;
21 import android.graphics.Rect;
22 import android.graphics.RectF;
23 import android.util.FloatMath;
25 import com.android.gallery3d.app.GalleryContext;
26 import com.android.gallery3d.common.ApiHelper;
27 import com.android.gallery3d.common.LongSparseArray;
28 import com.android.gallery3d.common.Utils;
29 import com.android.gallery3d.data.BitmapPool;
30 import com.android.gallery3d.data.DecodeUtils;
31 import com.android.gallery3d.util.Future;
32 import com.android.gallery3d.util.ThreadPool;
33 import com.android.gallery3d.util.ThreadPool.CancelListener;
34 import com.android.gallery3d.util.ThreadPool.JobContext;
36 import java.util.concurrent.atomic.AtomicBoolean;
38 public class TileImageView extends GLView {
39 public static final int SIZE_UNKNOWN = -1;
41 @SuppressWarnings("unused")
42 private static final String TAG = "TileImageView";
44 // TILE_SIZE must be 2^N - 2. We put one pixel border in each side of the
45 // texture to avoid seams between tiles.
46 private static final int TILE_SIZE = 254;
47 private static final int TILE_BORDER = 1;
48 private static final int BITMAP_SIZE = TILE_SIZE + TILE_BORDER * 2;
49 private static final int UPLOAD_LIMIT = 1;
51 private static final BitmapPool sTilePool =
52 ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_REGION_DECODER
53 ? new BitmapPool(BITMAP_SIZE, BITMAP_SIZE, 128)
57 * This is the tile state in the CPU side.
59 * ACTIVATED (initial state)
60 * --> IN_QUEUE - by queueForDecode()
61 * --> RECYCLED - by recycleTile()
62 * IN_QUEUE --> DECODING - by decodeTile()
63 * --> RECYCLED - by recycleTile)
64 * DECODING --> RECYCLING - by recycleTile()
65 * --> DECODED - by decodeTile()
66 * --> DECODE_FAIL - by decodeTile()
67 * RECYCLING --> RECYCLED - by decodeTile()
68 * DECODED --> ACTIVATED - (after the decoded bitmap is uploaded)
69 * DECODED --> RECYCLED - by recycleTile()
70 * DECODE_FAIL -> RECYCLED - by recycleTile()
71 * RECYCLED --> ACTIVATED - by obtainTile()
73 private static final int STATE_ACTIVATED = 0x01;
74 private static final int STATE_IN_QUEUE = 0x02;
75 private static final int STATE_DECODING = 0x04;
76 private static final int STATE_DECODED = 0x08;
77 private static final int STATE_DECODE_FAIL = 0x10;
78 private static final int STATE_RECYCLING = 0x20;
79 private static final int STATE_RECYCLED = 0x40;
82 private ScreenNail mScreenNail;
83 protected int mLevelCount; // cache the value of mScaledBitmaps.length
85 // The mLevel variable indicates which level of bitmap we should use.
86 // Level 0 means the original full-sized bitmap, and a larger value means
87 // a smaller scaled bitmap (The width and height of each scaled bitmap is
88 // half size of the previous one). If the value is in [0, mLevelCount), we
89 // use the bitmap in mScaledBitmaps[mLevel] for display, otherwise the value
90 // is mLevelCount, and that means we use mScreenNail for display.
91 private int mLevel = 0;
93 // The offsets of the (left, top) of the upper-left tile to the (left, top)
98 private int mUploadQuota;
99 private boolean mRenderComplete;
101 private final RectF mSourceRect = new RectF();
102 private final RectF mTargetRect = new RectF();
104 private final LongSparseArray<Tile> mActiveTiles = new LongSparseArray<Tile>();
106 // The following three queue is guarded by TileImageView.this
107 private final TileQueue mRecycledQueue = new TileQueue();
108 private final TileQueue mUploadQueue = new TileQueue();
109 private final TileQueue mDecodeQueue = new TileQueue();
111 // The width and height of the full-sized bitmap
112 protected int mImageWidth = SIZE_UNKNOWN;
113 protected int mImageHeight = SIZE_UNKNOWN;
115 protected int mCenterX;
116 protected int mCenterY;
117 protected float mScale;
118 protected int mRotation;
120 // Temp variables to avoid memory allocation
121 private final Rect mTileRange = new Rect();
122 private final Rect mActiveRange[] = {new Rect(), new Rect()};
124 private final TileUploader mTileUploader = new TileUploader();
125 private boolean mIsTextureFreed;
126 private Future<Void> mTileDecoder;
127 private final ThreadPool mThreadPool;
128 private boolean mBackgroundTileUploaded;
130 public static interface Model {
131 public int getLevelCount();
132 public ScreenNail getScreenNail();
133 public int getImageWidth();
134 public int getImageHeight();
136 // The tile returned by this method can be specified this way: Assuming
137 // the image size is (width, height), first take the intersection of (0,
138 // 0) - (width, height) and (x, y) - (x + tileSize, y + tileSize). Then
139 // extend this intersection region by borderSize pixels on each side. If
140 // in extending the region, we found some part of the region are outside
141 // the image, those pixels are filled with black.
143 // If level > 0, it does the same operation on a down-scaled version of
144 // the original image (down-scaled by a factor of 2^level), but (x, y)
145 // still refers to the coordinate on the original image.
147 // The method would be called in another thread.
148 public Bitmap getTile(int level, int x, int y, int tileSize,
149 int borderSize, BitmapPool pool);
152 public TileImageView(GalleryContext context) {
153 mThreadPool = context.getThreadPool();
154 mTileDecoder = mThreadPool.submit(new TileDecoder());
157 public void setModel(Model model) {
159 if (model != null) notifyModelInvalidated();
162 public void setScreenNail(ScreenNail s) {
166 public void notifyModelInvalidated() {
168 if (mModel == null) {
174 setScreenNail(mModel.getScreenNail());
175 mImageWidth = mModel.getImageWidth();
176 mImageHeight = mModel.getImageHeight();
177 mLevelCount = mModel.getLevelCount();
179 layoutTiles(mCenterX, mCenterY, mScale, mRotation);
184 protected void onLayout(
185 boolean changeSize, int left, int top, int right, int bottom) {
186 super.onLayout(changeSize, left, top, right, bottom);
187 if (changeSize) layoutTiles(mCenterX, mCenterY, mScale, mRotation);
190 // Prepare the tiles we want to use for display.
192 // 1. Decide the tile level we want to use for display.
193 // 2. Decide the tile levels we want to keep as texture (in addition to
194 // the one we use for display).
195 // 3. Recycle unused tiles.
196 // 4. Activate the tiles we want.
197 private void layoutTiles(int centerX, int centerY, float scale, int rotation) {
198 // The width and height of this view.
199 int width = getWidth();
200 int height = getHeight();
202 // The tile levels we want to keep as texture is in the range
203 // [fromLevel, endLevel).
207 // We want to use a texture larger than or equal to the display size.
208 mLevel = Utils.clamp(Utils.floorLog2(1f / scale), 0, mLevelCount);
210 // We want to keep one more tile level as texture in addition to what
211 // we use for display. So it can be faster when the scale moves to the
212 // next level. We choose a level closer to the current scale.
213 if (mLevel != mLevelCount) {
214 Rect range = mTileRange;
215 getRange(range, centerX, centerY, mLevel, scale, rotation);
216 mOffsetX = Math.round(width / 2f + (range.left - centerX) * scale);
217 mOffsetY = Math.round(height / 2f + (range.top - centerY) * scale);
218 fromLevel = scale * (1 << mLevel) > 0.75f ? mLevel - 1 : mLevel;
220 // Activate the tiles of the smallest two levels.
221 fromLevel = mLevel - 2;
222 mOffsetX = Math.round(width / 2f - centerX * scale);
223 mOffsetY = Math.round(height / 2f - centerY * scale);
226 fromLevel = Math.max(0, Math.min(fromLevel, mLevelCount - 2));
227 endLevel = Math.min(fromLevel + 2, mLevelCount);
229 Rect range[] = mActiveRange;
230 for (int i = fromLevel; i < endLevel; ++i) {
231 getRange(range[i - fromLevel], centerX, centerY, i, rotation);
234 // If rotation is transient, don't update the tile.
235 if (rotation % 90 != 0) return;
237 synchronized (this) {
238 mDecodeQueue.clean();
239 mUploadQueue.clean();
240 mBackgroundTileUploaded = false;
242 // Recycle unused tiles: if the level of the active tile is outside the
243 // range [fromLevel, endLevel) or not in the visible range.
244 int n = mActiveTiles.size();
245 for (int i = 0; i < n; i++) {
246 Tile tile = mActiveTiles.valueAt(i);
247 int level = tile.mTileLevel;
248 if (level < fromLevel || level >= endLevel
249 || !range[level - fromLevel].contains(tile.mX, tile.mY)) {
250 mActiveTiles.removeAt(i);
258 for (int i = fromLevel; i < endLevel; ++i) {
259 int size = TILE_SIZE << i;
260 Rect r = range[i - fromLevel];
261 for (int y = r.top, bottom = r.bottom; y < bottom; y += size) {
262 for (int x = r.left, right = r.right; x < right; x += size) {
263 activateTile(x, y, i);
270 protected synchronized void invalidateTiles() {
271 mDecodeQueue.clean();
272 mUploadQueue.clean();
273 // TODO disable decoder
274 int n = mActiveTiles.size();
275 for (int i = 0; i < n; i++) {
276 Tile tile = mActiveTiles.valueAt(i);
279 mActiveTiles.clear();
282 private void getRange(Rect out, int cX, int cY, int level, int rotation) {
283 getRange(out, cX, cY, level, 1f / (1 << (level + 1)), rotation);
286 // If the bitmap is scaled by the given factor "scale", return the
287 // rectangle containing visible range. The left-top coordinate returned is
288 // aligned to the tile boundary.
290 // (cX, cY) is the point on the original bitmap which will be put in the
291 // center of the ImageViewer.
292 private void getRange(Rect out,
293 int cX, int cY, int level, float scale, int rotation) {
295 double radians = Math.toRadians(-rotation);
296 double w = getWidth();
297 double h = getHeight();
299 double cos = Math.cos(radians);
300 double sin = Math.sin(radians);
301 int width = (int) Math.ceil(Math.max(
302 Math.abs(cos * w - sin * h), Math.abs(cos * w + sin * h)));
303 int height = (int) Math.ceil(Math.max(
304 Math.abs(sin * w + cos * h), Math.abs(sin * w - cos * h)));
306 int left = (int) FloatMath.floor(cX - width / (2f * scale));
307 int top = (int) FloatMath.floor(cY - height / (2f * scale));
308 int right = (int) FloatMath.ceil(left + width / scale);
309 int bottom = (int) FloatMath.ceil(top + height / scale);
311 // align the rectangle to tile boundary
312 int size = TILE_SIZE << level;
313 left = Math.max(0, size * (left / size));
314 top = Math.max(0, size * (top / size));
315 right = Math.min(mImageWidth, right);
316 bottom = Math.min(mImageHeight, bottom);
318 out.set(left, top, right, bottom);
321 // Calculate where the center of the image is, in the view coordinates.
322 public void getImageCenter(Point center) {
323 // The width and height of this view.
324 int viewW = getWidth();
325 int viewH = getHeight();
327 // The distance between the center of the view to the center of the
328 // bitmap, in bitmap units. (mCenterX and mCenterY are the bitmap
329 // coordinates correspond to the center of view)
331 if (mRotation % 180 == 0) {
332 distW = mImageWidth / 2 - mCenterX;
333 distH = mImageHeight / 2 - mCenterY;
335 distW = mImageHeight / 2 - mCenterY;
336 distH = mImageWidth / 2 - mCenterX;
339 // Convert to view coordinates. mScale translates from bitmap units to
341 center.x = Math.round(viewW / 2f + distW * mScale);
342 center.y = Math.round(viewH / 2f + distH * mScale);
345 public boolean setPosition(int centerX, int centerY, float scale, int rotation) {
346 if (mCenterX == centerX && mCenterY == centerY
347 && mScale == scale && mRotation == rotation) return false;
351 mRotation = rotation;
352 layoutTiles(centerX, centerY, scale, rotation);
357 public void freeTextures() {
358 mIsTextureFreed = true;
360 if (mTileDecoder != null) {
361 mTileDecoder.cancel();
366 int n = mActiveTiles.size();
367 for (int i = 0; i < n; i++) {
368 Tile texture = mActiveTiles.valueAt(i);
371 mActiveTiles.clear();
372 mTileRange.set(0, 0, 0, 0);
374 synchronized (this) {
375 mUploadQueue.clean();
376 mDecodeQueue.clean();
377 Tile tile = mRecycledQueue.pop();
378 while (tile != null) {
380 tile = mRecycledQueue.pop();
384 if (sTilePool != null) sTilePool.clear();
387 public void prepareTextures() {
388 if (mTileDecoder == null) {
389 mTileDecoder = mThreadPool.submit(new TileDecoder());
391 if (mIsTextureFreed) {
392 layoutTiles(mCenterX, mCenterY, mScale, mRotation);
393 mIsTextureFreed = false;
394 setScreenNail(mModel == null ? null : mModel.getScreenNail());
399 protected void render(GLCanvas canvas) {
400 mUploadQuota = UPLOAD_LIMIT;
401 mRenderComplete = true;
404 int rotation = mRotation;
406 if (rotation != 0) flags |= GLCanvas.SAVE_FLAG_MATRIX;
411 int centerX = getWidth() / 2, centerY = getHeight() / 2;
412 canvas.translate(centerX, centerY);
413 canvas.rotate(rotation, 0, 0, 1);
414 canvas.translate(-centerX, -centerY);
418 if (level != mLevelCount && !isScreenNailAnimating()) {
419 if (mScreenNail != null) {
420 mScreenNail.noDraw();
423 int size = (TILE_SIZE << level);
424 float length = size * mScale;
427 for (int ty = r.top, i = 0; ty < r.bottom; ty += size, i++) {
428 float y = mOffsetY + i * length;
429 for (int tx = r.left, j = 0; tx < r.right; tx += size, j++) {
430 float x = mOffsetX + j * length;
431 drawTile(canvas, tx, ty, level, x, y, length);
434 } else if (mScreenNail != null) {
435 mScreenNail.draw(canvas, mOffsetX, mOffsetY,
436 Math.round(mImageWidth * mScale),
437 Math.round(mImageHeight * mScale));
438 if (isScreenNailAnimating()) {
443 if (flags != 0) canvas.restore();
446 if (mRenderComplete) {
447 if (!mBackgroundTileUploaded) uploadBackgroundTiles(canvas);
453 private boolean isScreenNailAnimating() {
454 return (mScreenNail instanceof BitmapScreenNail)
455 && ((BitmapScreenNail) mScreenNail).isAnimating();
458 private void uploadBackgroundTiles(GLCanvas canvas) {
459 mBackgroundTileUploaded = true;
460 int n = mActiveTiles.size();
461 for (int i = 0; i < n; i++) {
462 Tile tile = mActiveTiles.valueAt(i);
463 if (!tile.isContentValid()) queueForDecode(tile);
467 void queueForUpload(Tile tile) {
468 synchronized (this) {
469 mUploadQueue.push(tile);
471 if (mTileUploader.mActive.compareAndSet(false, true)) {
472 getGLRoot().addOnGLIdleListener(mTileUploader);
476 synchronized void queueForDecode(Tile tile) {
477 if (tile.mTileState == STATE_ACTIVATED) {
478 tile.mTileState = STATE_IN_QUEUE;
479 if (mDecodeQueue.push(tile)) notifyAll();
483 boolean decodeTile(Tile tile) {
484 synchronized (this) {
485 if (tile.mTileState != STATE_IN_QUEUE) return false;
486 tile.mTileState = STATE_DECODING;
488 boolean decodeComplete = tile.decode();
489 synchronized (this) {
490 if (tile.mTileState == STATE_RECYCLING) {
491 tile.mTileState = STATE_RECYCLED;
492 if (tile.mDecodedTile != null) {
493 if (sTilePool != null) sTilePool.recycle(tile.mDecodedTile);
494 tile.mDecodedTile = null;
496 mRecycledQueue.push(tile);
499 tile.mTileState = decodeComplete ? STATE_DECODED : STATE_DECODE_FAIL;
500 return decodeComplete;
504 private synchronized Tile obtainTile(int x, int y, int level) {
505 Tile tile = mRecycledQueue.pop();
507 tile.mTileState = STATE_ACTIVATED;
508 tile.update(x, y, level);
511 return new Tile(x, y, level);
514 synchronized void recycleTile(Tile tile) {
515 if (tile.mTileState == STATE_DECODING) {
516 tile.mTileState = STATE_RECYCLING;
519 tile.mTileState = STATE_RECYCLED;
520 if (tile.mDecodedTile != null) {
521 if (sTilePool != null) sTilePool.recycle(tile.mDecodedTile);
522 tile.mDecodedTile = null;
524 mRecycledQueue.push(tile);
527 private void activateTile(int x, int y, int level) {
528 long key = makeTileKey(x, y, level);
529 Tile tile = mActiveTiles.get(key);
531 if (tile.mTileState == STATE_IN_QUEUE) {
532 tile.mTileState = STATE_ACTIVATED;
536 tile = obtainTile(x, y, level);
537 mActiveTiles.put(key, tile);
540 private Tile getTile(int x, int y, int level) {
541 return mActiveTiles.get(makeTileKey(x, y, level));
544 private static long makeTileKey(int x, int y, int level) {
546 result = (result << 16) | y;
547 result = (result << 16) | level;
551 private class TileUploader implements GLRoot.OnGLIdleListener {
552 AtomicBoolean mActive = new AtomicBoolean(false);
555 public boolean onGLIdle(GLCanvas canvas, boolean renderRequested) {
556 if (renderRequested) return false;
557 int quota = UPLOAD_LIMIT;
560 synchronized (TileImageView.this) {
561 tile = mUploadQueue.pop();
563 if (tile == null || quota <= 0) break;
564 if (!tile.isContentValid()) {
565 Utils.assertTrue(tile.mTileState == STATE_DECODED);
566 tile.updateContent(canvas);
570 mActive.set(tile != null);
575 // Draw the tile to a square at canvas that locates at (x, y) and
576 // has a side length of length.
577 public void drawTile(GLCanvas canvas,
578 int tx, int ty, int level, float x, float y, float length) {
579 RectF source = mSourceRect;
580 RectF target = mTargetRect;
581 target.set(x, y, x + length, y + length);
582 source.set(0, 0, TILE_SIZE, TILE_SIZE);
584 Tile tile = getTile(tx, ty, level);
586 if (!tile.isContentValid()) {
587 if (tile.mTileState == STATE_DECODED) {
588 if (mUploadQuota > 0) {
590 tile.updateContent(canvas);
592 mRenderComplete = false;
594 } else if (tile.mTileState != STATE_DECODE_FAIL){
595 mRenderComplete = false;
596 queueForDecode(tile);
599 if (drawTile(tile, canvas, source, target)) return;
601 if (mScreenNail != null) {
602 int size = TILE_SIZE << level;
603 float scaleX = (float) mScreenNail.getWidth() / mImageWidth;
604 float scaleY = (float) mScreenNail.getHeight() / mImageHeight;
605 source.set(tx * scaleX, ty * scaleY, (tx + size) * scaleX,
606 (ty + size) * scaleY);
607 mScreenNail.draw(canvas, source, target);
611 // TODO: avoid drawing the unused part of the textures.
612 static boolean drawTile(
613 Tile tile, GLCanvas canvas, RectF source, RectF target) {
615 if (tile.isContentValid()) {
616 // offset source rectangle for the texture border.
617 source.offset(TILE_BORDER, TILE_BORDER);
618 canvas.drawTexture(tile, source, target);
622 // Parent can be divided to four quads and tile is one of the four.
623 Tile parent = tile.getParentTile();
624 if (parent == null) return false;
625 if (tile.mX == parent.mX) {
629 source.left = (TILE_SIZE + source.left) / 2f;
630 source.right = (TILE_SIZE + source.right) / 2f;
632 if (tile.mY == parent.mY) {
636 source.top = (TILE_SIZE + source.top) / 2f;
637 source.bottom = (TILE_SIZE + source.bottom) / 2f;
643 private class Tile extends UploadedTexture {
646 public int mTileLevel;
648 public Bitmap mDecodedTile;
649 public volatile int mTileState = STATE_ACTIVATED;
651 public Tile(int x, int y, int level) {
658 protected void onFreeBitmap(Bitmap bitmap) {
659 if (sTilePool != null) sTilePool.recycle(bitmap);
663 // Get a tile from the original image. The tile is down-scaled
664 // by (1 << mTilelevel) from a region in the original image.
666 mDecodedTile = DecodeUtils.ensureGLCompatibleBitmap(mModel.getTile(
667 mTileLevel, mX, mY, TILE_SIZE, TILE_BORDER, sTilePool));
668 } catch (Throwable t) {
669 Log.w(TAG, "fail to decode tile", t);
671 return mDecodedTile != null;
675 protected Bitmap onGetBitmap() {
676 Utils.assertTrue(mTileState == STATE_DECODED);
678 // We need to override the width and height, so that we won't
679 // draw beyond the boundaries.
680 int rightEdge = ((mImageWidth - mX) >> mTileLevel) + TILE_BORDER;
681 int bottomEdge = ((mImageHeight - mY) >> mTileLevel) + TILE_BORDER;
682 setSize(Math.min(BITMAP_SIZE, rightEdge), Math.min(BITMAP_SIZE, bottomEdge));
684 Bitmap bitmap = mDecodedTile;
686 mTileState = STATE_ACTIVATED;
690 // We override getTextureWidth() and getTextureHeight() here, so the
691 // texture can be re-used for different tiles regardless of the actual
692 // size of the tile (which may be small because it is a tile at the
695 public int getTextureWidth() {
696 return TILE_SIZE + TILE_BORDER * 2;
700 public int getTextureHeight() {
701 return TILE_SIZE + TILE_BORDER * 2;
704 public void update(int x, int y, int level) {
711 public Tile getParentTile() {
712 if (mTileLevel + 1 == mLevelCount) return null;
713 int size = TILE_SIZE << (mTileLevel + 1);
714 int x = size * (mX / size);
715 int y = size * (mY / size);
716 return getTile(x, y, mTileLevel + 1);
720 public String toString() {
721 return String.format("tile(%s, %s, %s / %s)",
722 mX / TILE_SIZE, mY / TILE_SIZE, mLevel, mLevelCount);
726 private static class TileQueue {
731 if (tile != null) mHead = tile.mNext;
735 public boolean push(Tile tile) {
736 boolean wasEmpty = mHead == null;
742 public void clean() {
747 private class TileDecoder implements ThreadPool.Job<Void> {
749 private CancelListener mNotifier = new CancelListener() {
751 public void onCancel() {
752 synchronized (TileImageView.this) {
753 TileImageView.this.notifyAll();
759 public Void run(JobContext jc) {
760 jc.setMode(ThreadPool.MODE_NONE);
761 jc.setCancelListener(mNotifier);
762 while (!jc.isCancelled()) {
764 synchronized(TileImageView.this) {
765 tile = mDecodeQueue.pop();
766 if (tile == null && !jc.isCancelled()) {
767 Utils.waitWithoutInterrupt(TileImageView.this);
770 if (tile == null) continue;
771 if (decodeTile(tile)) queueForUpload(tile);