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
17package com.android.gallery3d.ui;
18
19import android.graphics.Bitmap;
20import android.graphics.Bitmap.Config;
21import android.graphics.Canvas;
22
23import com.android.gallery3d.common.BitmapUtils;
24import com.android.photos.data.GalleryBitmapPool;
25
26import java.util.ArrayList;
27
28public class BitmapTileProvider implements TileImageView.TileSource {
29    private final ScreenNail mScreenNail;
30    private final Bitmap[] mMipmaps;
31    private final Config mConfig;
32    private final int mImageWidth;
33    private final int mImageHeight;
34
35    private boolean mRecycled = false;
36
37    public BitmapTileProvider(Bitmap bitmap, int maxBackupSize) {
38        mImageWidth = bitmap.getWidth();
39        mImageHeight = bitmap.getHeight();
40        ArrayList<Bitmap> list = new ArrayList<Bitmap>();
41        list.add(bitmap);
42        while (bitmap.getWidth() > maxBackupSize
43                || bitmap.getHeight() > maxBackupSize) {
44            bitmap = BitmapUtils.resizeBitmapByScale(bitmap, 0.5f, false);
45            list.add(bitmap);
46        }
47
48        mScreenNail = new BitmapScreenNail(list.remove(list.size() - 1));
49        mMipmaps = list.toArray(new Bitmap[list.size()]);
50        mConfig = Config.ARGB_8888;
51    }
52
53    @Override
54    public ScreenNail getScreenNail() {
55        return mScreenNail;
56    }
57
58    @Override
59    public int getImageHeight() {
60        return mImageHeight;
61    }
62
63    @Override
64    public int getImageWidth() {
65        return mImageWidth;
66    }
67
68    @Override
69    public int getLevelCount() {
70        return mMipmaps.length;
71    }
72
73    @Override
74    public Bitmap getTile(int level, int x, int y, int tileSize) {
75        x >>= level;
76        y >>= level;
77
78        Bitmap result = GalleryBitmapPool.getInstance().get(tileSize, tileSize);
79        if (result == null) {
80            result = Bitmap.createBitmap(tileSize, tileSize, mConfig);
81        } else {
82            result.eraseColor(0);
83        }
84
85        Bitmap mipmap = mMipmaps[level];
86        Canvas canvas = new Canvas(result);
87        int offsetX = -x;
88        int offsetY = -y;
89        canvas.drawBitmap(mipmap, offsetX, offsetY, null);
90        return result;
91    }
92
93    public void recycle() {
94        if (mRecycled) return;
95        mRecycled = true;
96        for (Bitmap bitmap : mMipmaps) {
97            BitmapUtils.recycleSilently(bitmap);
98        }
99        if (mScreenNail != null) {
100            mScreenNail.recycle();
101        }
102    }
103}
104