1/*
2 * Copyright (C) 2013 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.filtershow.pipeline;
18
19import android.graphics.Bitmap;
20import android.graphics.Canvas;
21import android.support.v8.renderscript.Allocation;
22import android.support.v8.renderscript.RenderScript;
23import android.util.Log;
24import com.android.gallery3d.filtershow.cache.BitmapCache;
25import com.android.gallery3d.filtershow.imageshow.MasterImage;
26
27public class Buffer {
28    private static final String LOGTAG = "Buffer";
29    private Bitmap mBitmap;
30    private Allocation mAllocation;
31    private boolean mUseAllocation = false;
32    private ImagePreset mPreset;
33
34    public Buffer(Bitmap bitmap) {
35        RenderScript rs = CachingPipeline.getRenderScriptContext();
36        if (bitmap != null) {
37            BitmapCache cache = MasterImage.getImage().getBitmapCache();
38            mBitmap = cache.getBitmapCopy(bitmap, BitmapCache.PREVIEW_CACHE);
39        }
40        if (mUseAllocation) {
41            // TODO: recreate the allocation when the RS context changes
42            mAllocation = Allocation.createFromBitmap(rs, mBitmap,
43                    Allocation.MipmapControl.MIPMAP_NONE,
44                    Allocation.USAGE_SHARED | Allocation.USAGE_SCRIPT);
45        }
46    }
47
48    public boolean isSameSize(Bitmap bitmap) {
49        if (mBitmap == null || bitmap == null) {
50            return false;
51        }
52        if (mBitmap.getWidth() == bitmap.getWidth()
53                && mBitmap.getHeight() == bitmap.getHeight()) {
54            return true;
55        }
56        return false;
57    }
58
59    public synchronized void useBitmap(Bitmap bitmap) {
60        Canvas canvas = new Canvas(mBitmap);
61        canvas.drawBitmap(bitmap, 0, 0, null);
62    }
63
64    public synchronized Bitmap getBitmap() {
65        return mBitmap;
66    }
67
68    public Allocation getAllocation() {
69        return mAllocation;
70    }
71
72    public void sync() {
73        if (mUseAllocation) {
74            mAllocation.copyTo(mBitmap);
75        }
76    }
77
78    public ImagePreset getPreset() {
79        return mPreset;
80    }
81
82    public void setPreset(ImagePreset preset) {
83        if ((mPreset == null) || (!mPreset.same(preset))) {
84            mPreset = new ImagePreset(preset);
85        } else {
86            mPreset.updateWith(preset);
87        }
88    }
89
90    public void remove() {
91        BitmapCache cache = MasterImage.getImage().getBitmapCache();
92        if (cache.cache(mBitmap)) {
93            mBitmap = null;
94        }
95    }
96}
97
98