ImageFilterRS.java revision 0f8047fe826a84829ab37bc7cd24c8a4dea0db64
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.filtershow.filters;
18
19import android.app.Activity;
20import android.graphics.Bitmap;
21import android.support.v8.renderscript.*;
22import android.util.Log;
23
24public abstract class ImageFilterRS extends ImageFilter {
25    private final String LOGTAG = "ImageFilterRS";
26
27    private static RenderScript mRS = null;
28    protected static Allocation mInPixelsAllocation;
29    protected static Allocation mOutPixelsAllocation;
30    private static android.content.res.Resources mResources = null;
31    private static Bitmap sOldBitmap = null;
32    private Bitmap mOldBitmap = null;
33
34    private final Bitmap.Config mBitmapConfig = Bitmap.Config.ARGB_8888;
35
36    public void resetBitmap() {
37        mOldBitmap = null;
38    }
39
40    public void prepare(Bitmap bitmap, float scaleFactor, int quality) {
41        if (sOldBitmap == null
42                || (bitmap.getWidth() != sOldBitmap.getWidth())
43                || (bitmap.getHeight() != sOldBitmap.getHeight())) {
44            if (mInPixelsAllocation != null) {
45                mInPixelsAllocation.destroy();
46                mInPixelsAllocation = null;
47            }
48            if (mOutPixelsAllocation != null) {
49                mOutPixelsAllocation.destroy();
50                mOutPixelsAllocation = null;
51            }
52            Bitmap bitmapBuffer = bitmap.copy(mBitmapConfig, true);
53            mOutPixelsAllocation = Allocation.createFromBitmap(mRS, bitmapBuffer,
54                    Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
55            mInPixelsAllocation = Allocation.createTyped(mRS,
56                    mOutPixelsAllocation.getType());
57            sOldBitmap = bitmap;
58        }
59        mInPixelsAllocation.copyFrom(bitmap);
60        if (mOldBitmap != sOldBitmap) {
61            createFilter(mResources, scaleFactor, quality);
62            mOldBitmap = sOldBitmap;
63        }
64    }
65
66    public void createFilter(android.content.res.Resources res,
67            float scaleFactor, int quality) {
68        // Stub
69    }
70
71    public void runFilter() {
72        // Stub
73    }
74
75    public void update(Bitmap bitmap) {
76        mOutPixelsAllocation.copyTo(bitmap);
77    }
78
79    @Override
80    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
81        if (bitmap == null) {
82            return bitmap;
83        }
84        try {
85            prepare(bitmap, scaleFactor, quality);
86            runFilter();
87            update(bitmap);
88        } catch (android.renderscript.RSIllegalArgumentException e) {
89            Log.e(LOGTAG, "Illegal argument? " + e);
90        } catch (android.renderscript.RSRuntimeException e) {
91            Log.e(LOGTAG, "RS runtime exception ? " + e);
92        }
93        return bitmap;
94    }
95
96    public static RenderScript getRenderScriptContext() {
97        return mRS;
98    }
99
100    public static void setRenderScriptContext(Activity context) {
101        if (mRS == null) {
102            mRS = RenderScript.create(context);
103        }
104        mResources = context.getResources();
105        if (mInPixelsAllocation != null) {
106            mInPixelsAllocation.destroy();
107            mInPixelsAllocation = null;
108        }
109        if (mOutPixelsAllocation != null) {
110            mOutPixelsAllocation.destroy();
111            mOutPixelsAllocation = null;
112        }
113        sOldBitmap = null;
114    }
115
116}
117