OSDN Git Service

Merge "Import translations. DO NOT MERGE" into klp-dev
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / pipeline / Buffer.java
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
17 package com.android.gallery3d.filtershow.pipeline;
18
19 import android.graphics.Bitmap;
20 import android.support.v8.renderscript.Allocation;
21 import android.support.v8.renderscript.RenderScript;
22 import android.util.Log;
23 import com.android.gallery3d.filtershow.cache.BitmapCache;
24 import com.android.gallery3d.filtershow.imageshow.MasterImage;
25
26 public class Buffer {
27     private static final String LOGTAG = "Buffer";
28     private Bitmap mBitmap;
29     private Allocation mAllocation;
30     private boolean mUseAllocation = false;
31     private ImagePreset mPreset;
32
33     public Buffer(Bitmap bitmap) {
34         RenderScript rs = CachingPipeline.getRenderScriptContext();
35         if (bitmap != null) {
36             BitmapCache cache = MasterImage.getImage().getBitmapCache();
37             cache.cache(mBitmap);
38             mBitmap = cache.getBitmapCopy(bitmap);
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 Bitmap getBitmap() {
49         return mBitmap;
50     }
51
52     public Allocation getAllocation() {
53         return mAllocation;
54     }
55
56     public void sync() {
57         if (mUseAllocation) {
58             mAllocation.copyTo(mBitmap);
59         }
60     }
61
62     public ImagePreset getPreset() {
63         return mPreset;
64     }
65
66     public void setPreset(ImagePreset preset) {
67         if ((mPreset == null) || (!mPreset.same(preset))) {
68             mPreset = new ImagePreset(preset);
69         } else {
70             mPreset.updateWith(preset);
71         }
72     }
73
74     public void remove() {
75         BitmapCache cache = MasterImage.getImage().getBitmapCache();
76         cache.cache(mBitmap);
77         mBitmap = null;
78     }
79 }
80