summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java
blob: e0269e9bb1e1a9bc528e4f4dcca889df6a1371d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.filtershow.pipeline;

import android.graphics.Bitmap;
import android.util.Log;
import com.android.gallery3d.filtershow.filters.FilterRepresentation;

import java.util.Vector;

public class CacheProcessing {
    private static final String LOGTAG = "CacheProcessing";
    private static final boolean DEBUG = false;
    private Vector<CacheStep> mSteps = new Vector<CacheStep>();

    static class CacheStep {
        FilterRepresentation representation;
        Bitmap cache;
    }

    public Bitmap process(Bitmap originalBitmap,
                          Vector<FilterRepresentation> filters,
                          FilterEnvironment environment) {

        if (filters.size() == 0) {
            return originalBitmap;
        }

        // New set of filters, let's clear the cache and rebuild it.
        if (filters.size() != mSteps.size()) {
            mSteps.clear();
            for (int i = 0; i < filters.size(); i++) {
                FilterRepresentation representation = filters.elementAt(i);
                CacheStep step = new CacheStep();
                step.representation = representation.copy();
                mSteps.add(step);
            }
        }

        if (DEBUG) {
            displayFilters(filters);
        }

        // First, let's find how similar we are in our cache
        // compared to the current list of filters
        int similarUpToIndex = -1;
        for (int i = 0; i < filters.size(); i++) {
            FilterRepresentation representation = filters.elementAt(i);
            CacheStep step = mSteps.elementAt(i);
            boolean similar = step.representation.equals(representation);
            if (similar) {
                similarUpToIndex = i;
            } else {
                break;
            }
        }
        if (DEBUG) {
            Log.v(LOGTAG, "similar up to index " + similarUpToIndex);
        }

        // Now, let's get the earliest cached result in our pipeline
        Bitmap cacheBitmap = null;
        int findBaseImageIndex = similarUpToIndex;
        if (findBaseImageIndex > -1) {
            while (findBaseImageIndex > 0
                    && mSteps.elementAt(findBaseImageIndex).cache == null) {
                findBaseImageIndex--;
            }
            cacheBitmap = mSteps.elementAt(findBaseImageIndex).cache;
        }
        boolean emptyStack = false;
        if (cacheBitmap == null) {
            emptyStack = true;
            // Damn, it's an empty stack, we have to start from scratch
            // TODO: use a bitmap cache + RS allocation instead of Bitmap.copy()
            cacheBitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);
            if (findBaseImageIndex > -1) {
                FilterRepresentation representation = filters.elementAt(findBaseImageIndex);
                if (representation.getFilterType() != FilterRepresentation.TYPE_GEOMETRY) {
                    cacheBitmap = environment.applyRepresentation(representation, cacheBitmap);
                }
                mSteps.elementAt(findBaseImageIndex).representation = representation.copy();
                mSteps.elementAt(findBaseImageIndex).cache = cacheBitmap;
            }
            if (DEBUG) {
                Log.v(LOGTAG, "empty stack");
            }
        }

        // Ok, so sadly the earliest cached result is before the index we want.
        // We have to rebuild a new result for this position, and then cache it.
        if (findBaseImageIndex != similarUpToIndex) {
            if (DEBUG) {
                Log.v(LOGTAG, "rebuild cacheBitmap from " + findBaseImageIndex
                        + " to " + similarUpToIndex);
            }
            // rebuild the cache image for this step
            if (!emptyStack) {
                cacheBitmap = cacheBitmap.copy(Bitmap.Config.ARGB_8888, true);
            } else {
                // if it was an empty stack, we already applied it
                findBaseImageIndex ++;
            }
            for (int i = findBaseImageIndex; i <= similarUpToIndex; i++) {
                FilterRepresentation representation = filters.elementAt(i);
                if (representation.getFilterType() != FilterRepresentation.TYPE_GEOMETRY) {
                    cacheBitmap = environment.applyRepresentation(representation, cacheBitmap);
                }
                if (DEBUG) {
                    Log.v(LOGTAG, " - " + i  + " => apply " + representation.getName());
                }
            }
            // Let's cache it!
            mSteps.elementAt(similarUpToIndex).cache = cacheBitmap;
        }

        if (DEBUG) {
            Log.v(LOGTAG, "process pipeline from " + similarUpToIndex
                    + " to " + (filters.size() - 1));
        }

        // Now we are good to go, let's use the cacheBitmap as a starting point
        for (int i = similarUpToIndex + 1; i < filters.size(); i++) {
            FilterRepresentation representation = filters.elementAt(i);
            CacheStep currentStep = mSteps.elementAt(i);
            cacheBitmap = cacheBitmap.copy(Bitmap.Config.ARGB_8888, true);
            if (representation.getFilterType() != FilterRepresentation.TYPE_GEOMETRY) {
                cacheBitmap = environment.applyRepresentation(representation, cacheBitmap);
            }
            currentStep.representation = representation.copy();
            currentStep.cache = cacheBitmap;
            if (DEBUG) {
                Log.v(LOGTAG, " - " + i  + " => apply " + representation.getName());
            }
        }

        if (DEBUG) {
            Log.v(LOGTAG, "now let's cleanup the cache...");
            displayNbBitmapsInCache();
        }

        // Let's see if we can cleanup the cache for unused bitmaps
        for (int i = 0; i < similarUpToIndex; i++) {
            CacheStep currentStep = mSteps.elementAt(i);
            currentStep.cache = null;
        }

        if (DEBUG) {
            Log.v(LOGTAG, "cleanup done...");
            displayNbBitmapsInCache();
        }
        return cacheBitmap;
    }

    private void displayFilters(Vector<FilterRepresentation> filters) {
        Log.v(LOGTAG, "------>>>");
        for (int i = 0; i < filters.size(); i++) {
            FilterRepresentation representation = filters.elementAt(i);
            CacheStep step = mSteps.elementAt(i);
            boolean similar = step.representation.equals(representation);
            Log.v(LOGTAG, "[" + i + "] - " + representation.getName()
                    + " similar rep ? " + (similar ? "YES" : "NO")
                    + " -- bitmap: " + step.cache);
        }
        Log.v(LOGTAG, "<<<------");
    }

    private void displayNbBitmapsInCache() {
        int nbBitmapsCached = 0;
        for (int i = 0; i < mSteps.size(); i++) {
            CacheStep step = mSteps.elementAt(i);
            if (step.cache != null) {
                nbBitmapsCached++;
            }
        }
        Log.v(LOGTAG, "nb bitmaps in cache: " + nbBitmapsCached + " / " + mSteps.size());
    }

}