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.glrenderer;
18
19import android.graphics.Bitmap;
20import android.graphics.Bitmap.Config;
21import android.graphics.Canvas;
22
23// CanvasTexture is a texture whose content is the drawing on a Canvas.
24// The subclasses should override onDraw() to draw on the bitmap.
25// By default CanvasTexture is not opaque.
26abstract class CanvasTexture extends UploadedTexture {
27    protected Canvas mCanvas;
28    private final Config mConfig;
29
30    public CanvasTexture(int width, int height) {
31        mConfig = Config.ARGB_8888;
32        setSize(width, height);
33        setOpaque(false);
34    }
35
36    @Override
37    protected Bitmap onGetBitmap() {
38        Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, mConfig);
39        mCanvas = new Canvas(bitmap);
40        onDraw(mCanvas, bitmap);
41        return bitmap;
42    }
43
44    @Override
45    protected void onFreeBitmap(Bitmap bitmap) {
46        if (!inFinalizer()) {
47            bitmap.recycle();
48        }
49    }
50
51    abstract protected void onDraw(Canvas canvas, Bitmap backing);
52}
53