OSDN Git Service

merge in jb-mr1-release history after reset to master
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / ui / TileImageView.java
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.gallery3d.ui;
18
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;
24
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;
35
36 import java.util.concurrent.atomic.AtomicBoolean;
37
38 public class TileImageView extends GLView {
39     public static final int SIZE_UNKNOWN = -1;
40
41     @SuppressWarnings("unused")
42     private static final String TAG = "TileImageView";
43
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;
50
51     private static final BitmapPool sTilePool =
52             ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_REGION_DECODER
53             ? new BitmapPool(BITMAP_SIZE, BITMAP_SIZE, 128)
54             : null;
55
56     /*
57      *  This is the tile state in the CPU side.
58      *  Life of a Tile:
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()
72      */
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;
80
81     private Model mModel;
82     private ScreenNail mScreenNail;
83     protected int mLevelCount;  // cache the value of mScaledBitmaps.length
84
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;
92
93     // The offsets of the (left, top) of the upper-left tile to the (left, top)
94     // of the view.
95     private int mOffsetX;
96     private int mOffsetY;
97
98     private int mUploadQuota;
99     private boolean mRenderComplete;
100
101     private final RectF mSourceRect = new RectF();
102     private final RectF mTargetRect = new RectF();
103
104     private final LongSparseArray<Tile> mActiveTiles = new LongSparseArray<Tile>();
105
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();
110
111     // The width and height of the full-sized bitmap
112     protected int mImageWidth = SIZE_UNKNOWN;
113     protected int mImageHeight = SIZE_UNKNOWN;
114
115     protected int mCenterX;
116     protected int mCenterY;
117     protected float mScale;
118     protected int mRotation;
119
120     // Temp variables to avoid memory allocation
121     private final Rect mTileRange = new Rect();
122     private final Rect mActiveRange[] = {new Rect(), new Rect()};
123
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;
129
130     public static interface Model {
131         public int getLevelCount();
132         public ScreenNail getScreenNail();
133         public int getImageWidth();
134         public int getImageHeight();
135
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.
142         //
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.
146         //
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);
150     }
151
152     public TileImageView(GalleryContext context) {
153         mThreadPool = context.getThreadPool();
154         mTileDecoder = mThreadPool.submit(new TileDecoder());
155     }
156
157     public void setModel(Model model) {
158         mModel = model;
159         if (model != null) notifyModelInvalidated();
160     }
161
162     public void setScreenNail(ScreenNail s) {
163         mScreenNail = s;
164     }
165
166     public void notifyModelInvalidated() {
167         invalidateTiles();
168         if (mModel == null) {
169             mScreenNail = null;
170             mImageWidth = 0;
171             mImageHeight = 0;
172             mLevelCount = 0;
173         } else {
174             setScreenNail(mModel.getScreenNail());
175             mImageWidth = mModel.getImageWidth();
176             mImageHeight = mModel.getImageHeight();
177             mLevelCount = mModel.getLevelCount();
178         }
179         layoutTiles(mCenterX, mCenterY, mScale, mRotation);
180         invalidate();
181     }
182
183     @Override
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);
188     }
189
190     // Prepare the tiles we want to use for display.
191     //
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();
201
202         // The tile levels we want to keep as texture is in the range
203         // [fromLevel, endLevel).
204         int fromLevel;
205         int endLevel;
206
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);
209
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;
219         } else {
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);
224         }
225
226         fromLevel = Math.max(0, Math.min(fromLevel, mLevelCount - 2));
227         endLevel = Math.min(fromLevel + 2, mLevelCount);
228
229         Rect range[] = mActiveRange;
230         for (int i = fromLevel; i < endLevel; ++i) {
231             getRange(range[i - fromLevel], centerX, centerY, i, rotation);
232         }
233
234         // If rotation is transient, don't update the tile.
235         if (rotation % 90 != 0) return;
236
237         synchronized (this) {
238             mDecodeQueue.clean();
239             mUploadQueue.clean();
240             mBackgroundTileUploaded = false;
241
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);
251                     i--;
252                     n--;
253                     recycleTile(tile);
254                 }
255             }
256         }
257
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);
264                 }
265             }
266         }
267         invalidate();
268     }
269
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);
277             recycleTile(tile);
278         }
279         mActiveTiles.clear();
280     }
281
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);
284     }
285
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.
289     //
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) {
294
295         double radians = Math.toRadians(-rotation);
296         double w = getWidth();
297         double h = getHeight();
298
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)));
305
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);
310
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);
317
318         out.set(left, top, right, bottom);
319     }
320
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();
326
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)
330         int distW, distH;
331         if (mRotation % 180 == 0) {
332             distW = mImageWidth / 2 - mCenterX;
333             distH = mImageHeight / 2 - mCenterY;
334         } else {
335             distW = mImageHeight / 2 - mCenterY;
336             distH = mImageWidth / 2 - mCenterX;
337         }
338
339         // Convert to view coordinates. mScale translates from bitmap units to
340         // view units.
341         center.x = Math.round(viewW / 2f + distW * mScale);
342         center.y = Math.round(viewH / 2f + distH * mScale);
343     }
344
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;
348         mCenterX = centerX;
349         mCenterY = centerY;
350         mScale = scale;
351         mRotation = rotation;
352         layoutTiles(centerX, centerY, scale, rotation);
353         invalidate();
354         return true;
355     }
356
357     public void freeTextures() {
358         mIsTextureFreed = true;
359
360         if (mTileDecoder != null) {
361             mTileDecoder.cancel();
362             mTileDecoder.get();
363             mTileDecoder = null;
364         }
365
366         int n = mActiveTiles.size();
367         for (int i = 0; i < n; i++) {
368             Tile texture = mActiveTiles.valueAt(i);
369             texture.recycle();
370         }
371         mActiveTiles.clear();
372         mTileRange.set(0, 0, 0, 0);
373
374         synchronized (this) {
375             mUploadQueue.clean();
376             mDecodeQueue.clean();
377             Tile tile = mRecycledQueue.pop();
378             while (tile != null) {
379                 tile.recycle();
380                 tile = mRecycledQueue.pop();
381             }
382         }
383         setScreenNail(null);
384         if (sTilePool != null) sTilePool.clear();
385     }
386
387     public void prepareTextures() {
388         if (mTileDecoder == null) {
389             mTileDecoder = mThreadPool.submit(new TileDecoder());
390         }
391         if (mIsTextureFreed) {
392             layoutTiles(mCenterX, mCenterY, mScale, mRotation);
393             mIsTextureFreed = false;
394             setScreenNail(mModel == null ? null : mModel.getScreenNail());
395         }
396     }
397
398     @Override
399     protected void render(GLCanvas canvas) {
400         mUploadQuota = UPLOAD_LIMIT;
401         mRenderComplete = true;
402
403         int level = mLevel;
404         int rotation = mRotation;
405         int flags = 0;
406         if (rotation != 0) flags |= GLCanvas.SAVE_FLAG_MATRIX;
407
408         if (flags != 0) {
409             canvas.save(flags);
410             if (rotation != 0) {
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);
415             }
416         }
417         try {
418             if (level != mLevelCount && !isScreenNailAnimating()) {
419                 if (mScreenNail != null) {
420                     mScreenNail.noDraw();
421                 }
422
423                 int size = (TILE_SIZE << level);
424                 float length = size * mScale;
425                 Rect r = mTileRange;
426
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);
432                     }
433                 }
434             } else if (mScreenNail != null) {
435                 mScreenNail.draw(canvas, mOffsetX, mOffsetY,
436                         Math.round(mImageWidth * mScale),
437                         Math.round(mImageHeight * mScale));
438                 if (isScreenNailAnimating()) {
439                     invalidate();
440                 }
441             }
442         } finally {
443             if (flags != 0) canvas.restore();
444         }
445
446         if (mRenderComplete) {
447             if (!mBackgroundTileUploaded) uploadBackgroundTiles(canvas);
448         } else {
449             invalidate();
450         }
451     }
452
453     private boolean isScreenNailAnimating() {
454         return (mScreenNail instanceof BitmapScreenNail)
455                 && ((BitmapScreenNail) mScreenNail).isAnimating();
456     }
457
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);
464         }
465     }
466
467     void queueForUpload(Tile tile) {
468         synchronized (this) {
469             mUploadQueue.push(tile);
470         }
471         if (mTileUploader.mActive.compareAndSet(false, true)) {
472             getGLRoot().addOnGLIdleListener(mTileUploader);
473         }
474     }
475
476     synchronized void queueForDecode(Tile tile) {
477         if (tile.mTileState == STATE_ACTIVATED) {
478             tile.mTileState = STATE_IN_QUEUE;
479             if (mDecodeQueue.push(tile)) notifyAll();
480         }
481     }
482
483     boolean decodeTile(Tile tile) {
484         synchronized (this) {
485             if (tile.mTileState != STATE_IN_QUEUE) return false;
486             tile.mTileState = STATE_DECODING;
487         }
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;
495                 }
496                 mRecycledQueue.push(tile);
497                 return false;
498             }
499             tile.mTileState = decodeComplete ? STATE_DECODED : STATE_DECODE_FAIL;
500             return decodeComplete;
501         }
502     }
503
504     private synchronized Tile obtainTile(int x, int y, int level) {
505         Tile tile = mRecycledQueue.pop();
506         if (tile != null) {
507             tile.mTileState = STATE_ACTIVATED;
508             tile.update(x, y, level);
509             return tile;
510         }
511         return new Tile(x, y, level);
512     }
513
514     synchronized void recycleTile(Tile tile) {
515         if (tile.mTileState == STATE_DECODING) {
516             tile.mTileState = STATE_RECYCLING;
517             return;
518         }
519         tile.mTileState = STATE_RECYCLED;
520         if (tile.mDecodedTile != null) {
521             if (sTilePool != null) sTilePool.recycle(tile.mDecodedTile);
522             tile.mDecodedTile = null;
523         }
524         mRecycledQueue.push(tile);
525     }
526
527     private void activateTile(int x, int y, int level) {
528         long key = makeTileKey(x, y, level);
529         Tile tile = mActiveTiles.get(key);
530         if (tile != null) {
531             if (tile.mTileState == STATE_IN_QUEUE) {
532                 tile.mTileState = STATE_ACTIVATED;
533             }
534             return;
535         }
536         tile = obtainTile(x, y, level);
537         mActiveTiles.put(key, tile);
538     }
539
540     private Tile getTile(int x, int y, int level) {
541         return mActiveTiles.get(makeTileKey(x, y, level));
542     }
543
544     private static long makeTileKey(int x, int y, int level) {
545         long result = x;
546         result = (result << 16) | y;
547         result = (result << 16) | level;
548         return result;
549     }
550
551     private class TileUploader implements GLRoot.OnGLIdleListener {
552         AtomicBoolean mActive = new AtomicBoolean(false);
553
554         @Override
555         public boolean onGLIdle(GLCanvas canvas, boolean renderRequested) {
556             if (renderRequested) return false;
557             int quota = UPLOAD_LIMIT;
558             Tile tile;
559             while (true) {
560                 synchronized (TileImageView.this) {
561                     tile = mUploadQueue.pop();
562                 }
563                 if (tile == null || quota <= 0) break;
564                 if (!tile.isContentValid()) {
565                     Utils.assertTrue(tile.mTileState == STATE_DECODED);
566                     tile.updateContent(canvas);
567                     --quota;
568                 }
569             }
570             mActive.set(tile != null);
571             return tile != null;
572         }
573     }
574
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);
583
584         Tile tile = getTile(tx, ty, level);
585         if (tile != null) {
586             if (!tile.isContentValid()) {
587                 if (tile.mTileState == STATE_DECODED) {
588                     if (mUploadQuota > 0) {
589                         --mUploadQuota;
590                         tile.updateContent(canvas);
591                     } else {
592                         mRenderComplete = false;
593                     }
594                 } else if (tile.mTileState != STATE_DECODE_FAIL){
595                     mRenderComplete = false;
596                     queueForDecode(tile);
597                 }
598             }
599             if (drawTile(tile, canvas, source, target)) return;
600         }
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);
608         }
609     }
610
611     // TODO: avoid drawing the unused part of the textures.
612     static boolean drawTile(
613             Tile tile, GLCanvas canvas, RectF source, RectF target) {
614         while (true) {
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);
619                 return true;
620             }
621
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) {
626                 source.left /= 2f;
627                 source.right /= 2f;
628             } else {
629                 source.left = (TILE_SIZE + source.left) / 2f;
630                 source.right = (TILE_SIZE + source.right) / 2f;
631             }
632             if (tile.mY == parent.mY) {
633                 source.top /= 2f;
634                 source.bottom /= 2f;
635             } else {
636                 source.top = (TILE_SIZE + source.top) / 2f;
637                 source.bottom = (TILE_SIZE + source.bottom) / 2f;
638             }
639             tile = parent;
640         }
641     }
642
643     private class Tile extends UploadedTexture {
644         public int mX;
645         public int mY;
646         public int mTileLevel;
647         public Tile mNext;
648         public Bitmap mDecodedTile;
649         public volatile int mTileState = STATE_ACTIVATED;
650
651         public Tile(int x, int y, int level) {
652             mX = x;
653             mY = y;
654             mTileLevel = level;
655         }
656
657         @Override
658         protected void onFreeBitmap(Bitmap bitmap) {
659             if (sTilePool != null) sTilePool.recycle(bitmap);
660         }
661
662         boolean decode() {
663             // Get a tile from the original image. The tile is down-scaled
664             // by (1 << mTilelevel) from a region in the original image.
665             try {
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);
670             }
671             return mDecodedTile != null;
672         }
673
674         @Override
675         protected Bitmap onGetBitmap() {
676             Utils.assertTrue(mTileState == STATE_DECODED);
677
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));
683
684             Bitmap bitmap = mDecodedTile;
685             mDecodedTile = null;
686             mTileState = STATE_ACTIVATED;
687             return bitmap;
688         }
689
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
693         // boundary).
694         @Override
695         public int getTextureWidth() {
696             return TILE_SIZE + TILE_BORDER * 2;
697         }
698
699         @Override
700         public int getTextureHeight() {
701             return TILE_SIZE + TILE_BORDER * 2;
702         }
703
704         public void update(int x, int y, int level) {
705             mX = x;
706             mY = y;
707             mTileLevel = level;
708             invalidateContent();
709         }
710
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);
717         }
718
719         @Override
720         public String toString() {
721             return String.format("tile(%s, %s, %s / %s)",
722                     mX / TILE_SIZE, mY / TILE_SIZE, mLevel, mLevelCount);
723         }
724     }
725
726     private static class TileQueue {
727         private Tile mHead;
728
729         public Tile pop() {
730             Tile tile = mHead;
731             if (tile != null) mHead = tile.mNext;
732             return tile;
733         }
734
735         public boolean push(Tile tile) {
736             boolean wasEmpty = mHead == null;
737             tile.mNext = mHead;
738             mHead = tile;
739             return wasEmpty;
740         }
741
742         public void clean() {
743             mHead = null;
744         }
745     }
746
747     private class TileDecoder implements ThreadPool.Job<Void> {
748
749         private CancelListener mNotifier = new CancelListener() {
750             @Override
751             public void onCancel() {
752                 synchronized (TileImageView.this) {
753                     TileImageView.this.notifyAll();
754                 }
755             }
756         };
757
758         @Override
759         public Void run(JobContext jc) {
760             jc.setMode(ThreadPool.MODE_NONE);
761             jc.setCancelListener(mNotifier);
762             while (!jc.isCancelled()) {
763                 Tile tile = null;
764                 synchronized(TileImageView.this) {
765                     tile = mDecodeQueue.pop();
766                     if (tile == null && !jc.isCancelled()) {
767                         Utils.waitWithoutInterrupt(TileImageView.this);
768                     }
769                 }
770                 if (tile == null) continue;
771                 if (decodeTile(tile)) queueForUpload(tile);
772             }
773             return null;
774         }
775     }
776 }