blob: ae2c688603d56ceac8c5a84e68f72932a47e5e3f [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
2 * Copyright (C) 2009 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
Igor Murashkin2b592ab2017-07-06 13:16:34 -070017import java.util.concurrent.CountDownLatch;
18
jeffhao5d1ac922011-09-29 17:41:15 -070019public class Main {
20 Bitmap mBitmap1, mBitmap2, mBitmap3, mBitmap4;
Igor Murashkin2b592ab2017-07-06 13:16:34 -070021 CountDownLatch mFreeSignalA, mFreeSignalB;
jeffhao5d1ac922011-09-29 17:41:15 -070022
23 public static void sleep(int ms) {
24 try {
25 Thread.sleep(ms);
26 } catch (InterruptedException ie) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +000027 System.out.println("sleep interrupted");
jeffhao5d1ac922011-09-29 17:41:15 -070028 }
29 }
30
31 public static void main(String args[]) {
32 System.out.println("start");
33
34 Main main = new Main();
35 main.run();
36
jeffhao5d1ac922011-09-29 17:41:15 -070037 System.out.println("done");
38 }
39
40 public void run() {
41 createBitmaps();
42
Mathieu Chartier7befd0e2014-02-03 17:48:41 -080043 Runtime.getRuntime().gc();
jeffhao5d1ac922011-09-29 17:41:15 -070044 sleep(250);
45
46 mBitmap2.drawAt(0, 0);
47
48 System.out.println("nulling 1");
49 mBitmap1 = null;
Mathieu Chartier7befd0e2014-02-03 17:48:41 -080050 Runtime.getRuntime().gc();
Igor Murashkin2b592ab2017-07-06 13:16:34 -070051 try {
52 mFreeSignalA.await(); // Block until dataA is definitely freed.
53 } catch (InterruptedException e) {
54 System.out.println("got unexpected InterruptedException e: " + e);
55 }
jeffhao5d1ac922011-09-29 17:41:15 -070056
57 System.out.println("nulling 2");
58 mBitmap2 = null;
Mathieu Chartier7befd0e2014-02-03 17:48:41 -080059 Runtime.getRuntime().gc();
Igor Murashkin2b592ab2017-07-06 13:16:34 -070060 sleep(200);
jeffhao5d1ac922011-09-29 17:41:15 -070061
62 System.out.println("nulling 3");
63 mBitmap3 = null;
Mathieu Chartier7befd0e2014-02-03 17:48:41 -080064 Runtime.getRuntime().gc();
Igor Murashkin2b592ab2017-07-06 13:16:34 -070065 sleep(200);
jeffhao5d1ac922011-09-29 17:41:15 -070066
67 System.out.println("nulling 4");
68 mBitmap4 = null;
Mathieu Chartier7befd0e2014-02-03 17:48:41 -080069 Runtime.getRuntime().gc();
Igor Murashkin2b592ab2017-07-06 13:16:34 -070070 try {
71 mFreeSignalB.await(); // Block until dataB is definitely freed.
72 } catch (InterruptedException e) {
73 System.out.println("got unexpected InterruptedException e: " + e);
74 }
jeffhao5d1ac922011-09-29 17:41:15 -070075
76 Bitmap.shutDown();
77 }
78
79 /*
80 * Create bitmaps.
81 *
82 * bitmap1 is 10x10 and unique
83 * bitmap2 and bitmap3 are 20x20 and share the same storage.
84 * bitmap4 is just another reference to bitmap3
85 *
86 * When we return there should be no local refs lurking on the stack.
87 */
88 public void createBitmaps() {
89 Bitmap.NativeWrapper dataA = Bitmap.allocNativeStorage(10, 10);
Igor Murashkin2b592ab2017-07-06 13:16:34 -070090 mFreeSignalA = dataA.mPhantomWrapper.mFreeSignal;
jeffhao5d1ac922011-09-29 17:41:15 -070091 Bitmap.NativeWrapper dataB = Bitmap.allocNativeStorage(20, 20);
Igor Murashkin2b592ab2017-07-06 13:16:34 -070092 mFreeSignalB = dataB.mPhantomWrapper.mFreeSignal;
93
jeffhao5d1ac922011-09-29 17:41:15 -070094 mBitmap1 = new Bitmap("one", 10, 10, dataA);
95 mBitmap2 = new Bitmap("two", 20, 20, dataB);
96 mBitmap3 = mBitmap4 = new Bitmap("three/four", 20, 20, dataB);
97 }
98}