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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/*
* Copyright (C) 2007 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.camera;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.util.Config;
import android.util.Log;
import java.io.FileDescriptor;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;
/**
* This class provides several utilities to cancel/prevent potential heavy
* loading of CPU in bitmap decoding.
*
* To use this class, be sure to call setAllowDecoding(true) in onResume
* and turn it off by cancelAllDecoding() in onPause, and replace all
* BitmapFactory.decodeFileDescriptor with BitmapManager.decodeFileDescriptor.
*
* Individual thread can be controlled by {allow/cancel}ThreadDecoding.
*/
public class BitmapManager {
private static final String TAG = "BitmapManager";
private static final boolean VERBOSE =
Config.LOGD && (false || Config.LOGV);
private static enum State {RUNNING, CANCEL, WAIT}
private static class ThreadStatus {
public State state = State.WAIT;
public BitmapFactory.Options options;
public String toString() {
String s;
if (state == State.RUNNING) {
s = "Running";
} else if (state == State.CANCEL) {
s = "Cancel";
} else {
s = "Wait";
}
s = "thread state = " + s + ", options = " + options;
return s;
}
}
private WeakHashMap<Thread, ThreadStatus> mThreadStatus =
new WeakHashMap<Thread, ThreadStatus>();
private boolean mAllowDecoding = false;
private boolean mLocked = false;
private boolean mCheckResourceLock = true;
private static BitmapManager sManager;
public static BitmapManager instance() {
if (sManager == null) {
sManager = new BitmapManager();
}
return sManager;
}
private BitmapManager() {
}
public synchronized void setCheckResourceLock(boolean b) {
mCheckResourceLock = b;
}
private synchronized boolean checkResourceLock() {
return mCheckResourceLock;
}
/**
* Get thread status and create one if specified.
*/
private synchronized ThreadStatus getThreadStatus(Thread t,
boolean createNew) {
ThreadStatus status = mThreadStatus.get(t);
if (status == null && createNew) {
status = new ThreadStatus();
mThreadStatus.put(t, status);
}
return status;
}
/**
* Since Bitmap related operations are resource-intensive (CPU/Memory),
* we can't affort too many threads running at the same time, so only
* thread that acquire the lock can proceed, others have to wait in the
* queue. It can also be canceled and removed by other thread to avoid
* starvation.
*/
public synchronized boolean acquireResourceLock() {
Thread t = Thread.currentThread();
ThreadStatus status = getThreadStatus(t, true);
if (VERBOSE) {
Log.v(TAG, "lock... thread " + t + "(" + t.getId() + ")");
}
while (mLocked) {
try {
if (VERBOSE) {
Log.v(TAG, "waiting... thread " + t.getId());
}
wait();
// remove canceled thread
if (status.state == State.CANCEL) {
if (VERBOSE) {
Log.v(TAG, "[" + t + "] someone cancels me!!");
}
return false;
}
} catch (InterruptedException ex) {
Log.e(TAG, ex.toString());
}
}
if (VERBOSE) {
Log.v(TAG, "locked... thread " + t + "(" + t.getId() + ")");
}
status.state = State.RUNNING;
mLocked = true;
return true;
}
/**
* Make sure "acquire/release" are pairing correctly
*/
public synchronized void releaseResourceLock() {
Thread t = Thread.currentThread();
if (VERBOSE) {
Log.v(TAG, "unlocking... thread " + t + "(" + t.getId() + ")");
}
mLocked = false;
notifyAll();
}
/**
* The following three methods are used to keep track of
* BitmapFaction.Options used for decoding and cancelling.
*/
private synchronized void setDecodingOptions(Thread t,
BitmapFactory.Options options) {
if (VERBOSE) {
Log.v(TAG, "setDecodingOptions for thread " + t.getId()
+ ", options=" + options);
}
getThreadStatus(t, true).options = options;
}
synchronized BitmapFactory.Options getDecodingOptions(Thread t) {
ThreadStatus status = mThreadStatus.get(t);
return status != null ? status.options : null;
}
synchronized void removeDecodingOptions(Thread t) {
if (VERBOSE) {
Log.v(TAG, "removeDecodingOptions for thread " + t.getId());
}
ThreadStatus status = mThreadStatus.get(t);
status.options = null;
}
/**
* The following three methods are used to keep track of which thread
* is being disabled for bitmap decoding.
*/
public synchronized boolean canThreadDecoding(Thread t) {
ThreadStatus status = mThreadStatus.get(t);
if (status == null) {
Log.v(TAG, "can't find status for thread " + t);
return false;
}
boolean result = (status.state == State.RUNNING) ||
(status.state != State.CANCEL && !mCheckResourceLock);
if (VERBOSE) {
Log.v(TAG, "canThread " + t + " allow to decode "
+ result);
}
return result;
}
public synchronized void allowThreadDecoding(Thread t) {
if (VERBOSE) {
Log.v(TAG, "allowThreadDecoding: " + t + "(" + t.getId() + ")");
}
getThreadStatus(t, true).state = State.WAIT;
}
public synchronized void cancelThreadDecoding(Thread t) {
if (VERBOSE) {
Log.v(TAG, "[Cancel Thread] cancelThreadDecode: "
+ t + "(" + t.getId() + ")");
}
ThreadStatus status = getThreadStatus(t, true);
status.state = State.CANCEL;
if (status.options != null) {
if (VERBOSE) {
Log.v(TAG, "[Cancel Decoding] options: " + status.options);
}
status.options.requestCancelDecode();
}
// Wake up threads in waiting list
notifyAll();
}
/**
* The following four methods are used to control global switch of
* bitmap decoding.
*/
public synchronized void cancelAllDecoding() {
if (VERBOSE) {
Log.v(TAG, ">>>>>>>> cancelAllDecoding <<<<<<<");
}
allowAllDecoding(false);
for (ThreadStatus status : mThreadStatus.values()) {
status.state = State.CANCEL;
if (status.options != null) {
if (VERBOSE) {
Log.v(TAG, "cancelDecode: " + status.options);
}
status.options.requestCancelDecode();
}
}
// Wake up all threads in the waiting list
notifyAll();
}
public synchronized void allowAllDecoding() {
allowAllDecoding(true);
}
public synchronized void allowAllDecoding(boolean reset) {
if (VERBOSE) {
Log.v(TAG, ">>>>>>>> allowAllDecoding <<<<<<<");
}
mAllowDecoding = true;
if (reset) {
mThreadStatus.clear();
}
}
public synchronized boolean canDecode() {
return mAllowDecoding;
}
/**
* A debugging routine.
*/
public synchronized void dump() {
Iterator<Map.Entry<Thread, ThreadStatus>> i =
mThreadStatus.entrySet().iterator();
while (i.hasNext()) {
Map.Entry<Thread, ThreadStatus> entry = i.next();
Log.v(TAG, "[Dump] Thread " + entry.getKey() + " ("
+ entry.getKey().getId()
+ ")'s status is " + entry.getValue());
}
}
/**
* The real place to delegate bitmap decoding to BitmapFactory.
*/
public Bitmap decodeFileDescriptor(FileDescriptor fd,
Rect outPadding,
BitmapFactory.Options options) {
// Does the global switch turn on?
if (!canDecode() || options.mCancel) {
if (VERBOSE) {
Log.v(TAG, "Not allowed to decode.");
}
return null;
}
// Can current thread decode?
Thread thread = Thread.currentThread();
if (!canThreadDecoding(thread)) {
if (VERBOSE) {
Log.v(TAG, "Thread " + thread + "(" + thread.getId()
+ ") is not allowed to decode");
}
return null;
}
setDecodingOptions(thread, options);
if (VERBOSE) {
Log.v(TAG, "decodeFileDescriptor: " + options + ", cancel="
+ options.mCancel);
}
long t = System.currentTimeMillis();
Bitmap b = BitmapFactory.decodeFileDescriptor(fd, null, options);
// In case legacy code cancel it in traditional way
if (options.mCancel) {
cancelThreadDecoding(thread);
}
if (VERBOSE) {
Log.v(TAG, "decodeFileDescriptor done: options=" + options
+ ", cancel=" + options.mCancel + ", it takes "
+ (System.currentTimeMillis() - t) + " ms.");
}
removeDecodingOptions(thread);
return b;
}
}
|