BitmapScreenNail.java revision b8be1e0ad76b6abc0da7ead39f7a9811195d001e
1/*
2 * Copyright (C) 2012 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.RectF;
21import android.util.Log;
22
23import com.android.gallery3d.data.MediaItem;
24
25// This is a ScreenNail wraps a Bitmap. It also includes the rotation
26// information. The getWidth() and getHeight() methods return the width/height
27// before rotation.
28public class BitmapScreenNail implements ScreenNail {
29    private static final String TAG = "BitmapScreenNail";
30    private final int mWidth;
31    private final int mHeight;
32    private final int mRotation;
33    private Bitmap mBitmap;
34    private BitmapTexture mTexture;
35
36    public BitmapScreenNail(Bitmap bitmap, int rotation) {
37        mWidth = bitmap.getWidth();
38        mHeight = bitmap.getHeight();
39        mRotation = rotation;
40        mBitmap = bitmap;
41        // We create mTexture lazily, so we don't incur the cost if we don't
42        // actually need it.
43    }
44
45    @Override
46    public int getWidth() {
47        return mWidth;
48    }
49
50    @Override
51    public int getHeight() {
52        return mHeight;
53    }
54
55    @Override
56    public int getRotation() {
57        return mRotation;
58    }
59
60    @Override
61    public void noDraw() {
62    }
63
64    @Override
65    public void recycle() {
66        if (mTexture != null) {
67            mTexture.recycle();
68            mTexture = null;
69        }
70        if (mBitmap != null) {
71            MediaItem.getThumbPool().recycle(mBitmap);
72            mBitmap = null;
73        }
74    }
75
76    @Override
77    public void draw(GLCanvas canvas, int x, int y, int width, int height) {
78        if (mTexture == null) {
79            mTexture = new BitmapTexture(mBitmap);
80        }
81        mTexture.draw(canvas, x, y, width, height);
82    }
83
84    @Override
85    public void draw(GLCanvas canvas, RectF source, RectF dest) {
86        if (mTexture == null) {
87            mTexture = new BitmapTexture(mBitmap);
88        }
89        canvas.drawTexture(mTexture, source, dest);
90    }
91}
92