summaryrefslogtreecommitdiff
path: root/tests/079-phantom/src/Bitmap.java
blob: 9d03cbd3d4d9237062c10b979fdf3720b98f5a8b (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
/*
 * Copyright (C) 2009 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.
 */

import java.lang.ref.ReferenceQueue;
import java.lang.ref.PhantomReference;
import java.util.ArrayList;

public class Bitmap {
    String mName;           /* for debugging */
    int mWidth, mHeight;
    Bitmap.NativeWrapper mNativeWrapper;

    private static int sSerial = 100;
    private static ArrayList sPhantomList = new ArrayList<PhantomWrapper>();
    private static ReferenceQueue<PhantomWrapper> sPhantomQueue =
            new ReferenceQueue<PhantomWrapper>();
    private static BitmapWatcher sWatcher = new BitmapWatcher(sPhantomQueue);
    static {
        sWatcher.start();
    };

    Bitmap(String name, int width, int height, Bitmap.NativeWrapper nativeData) {
        mName = name;
        mWidth = width;
        mHeight = height;
        mNativeWrapper = nativeData;

        System.out.println("Created " + this);
    }

    public String toString() {
        return "Bitmap " + mName + ": " + mWidth + "x" + mHeight + " (" +
                mNativeWrapper.mNativeData + ")";
    }

    public void drawAt(int x, int y) {
        System.out.println("Drawing " + this);
    }

    public static void shutDown() {
        sWatcher.shutDown();
        try {
            sWatcher.join();
        } catch (InterruptedException ie) {
            System.out.println("join intr");
        }
        System.out.println("Bitmap has shut down");
    }

    /*
     * Pretend we're allocating native storage.  Just returns a unique
     * serial number.
     */
    static Bitmap.NativeWrapper allocNativeStorage(int width, int height) {
        int nativeData;

        synchronized (Bitmap.class) {
            nativeData = sSerial++;
        }

        Bitmap.NativeWrapper wrapper = new Bitmap.NativeWrapper(nativeData);
        PhantomWrapper phan = new PhantomWrapper(wrapper, sPhantomQueue,
                nativeData);
        sPhantomList.add(phan);
        return wrapper;
    }

    static void freeNativeStorage(int nativeDataPtr) {
        System.out.println("freeNativeStorage: " + nativeDataPtr);
    }

    /*
     * Wraps a native data pointer in an object.  When this object is no
     * longer referenced, we free the native data.
     */
    static class NativeWrapper {
        public NativeWrapper(int nativeDataPtr) {
            mNativeData = nativeDataPtr;
        }
        public int mNativeData;

        /*
        @Override
        protected void finalize() throws Throwable {
            System.out.println("finalized " + mNativeData);
        }
        */
    }
}

/*
 * Keep an eye on the native data.
 *
 * We keep a copy of the native data pointer value, and set the wrapper
 * as our referent.  We need the copy because you can't get the referred-to
 * object back out of a PhantomReference.
 */
class PhantomWrapper extends PhantomReference {
    PhantomWrapper(Bitmap.NativeWrapper wrapper,
        ReferenceQueue<PhantomWrapper> queue, int nativeDataPtr)
    {
        super(wrapper, queue);
        mNativeData = nativeDataPtr;
    }

    public int mNativeData;
}

/*
 * Thread that watches for un-referenced bitmap data.
 */
class BitmapWatcher extends Thread {
    ReferenceQueue<PhantomWrapper> mQueue;
    volatile boolean mQuit = false;

    BitmapWatcher(ReferenceQueue<PhantomWrapper> queue) {
        mQueue = queue;
        setName("Bitmap Watcher");
    }

    public void run() {
        while (!mQuit) {
            try {
                PhantomWrapper ref = (PhantomWrapper) mQueue.remove();
                //System.out.println("dequeued ref " + ref.mNativeData +
                //    " - " + ref);
                Bitmap.freeNativeStorage(ref.mNativeData);
                //ref.clear();
            } catch (InterruptedException ie) {
                System.out.println("intr");
            }
        }
    }

    public void shutDown() {
        mQuit = true;
        interrupt();
    }
}