summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/kitfox/salamander/renderer/SurfaceManager.java
blob: 83cd2274ba91ba2aae75ee9bb887e1f8601afb05 (plain) (blame)
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
/*
 * SurfaceManager.java
 *
 * Created on April 12, 2007, 8:37 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.kitfox.salamander.renderer;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.image.BufferedImage;
import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.WeakHashMap;

/**
 *
 * @author kitfox
 */
public class SurfaceManager implements Runnable
{
    public class SurfaceInfo
    {
        private BufferedImage image;
        private BufImgInfo info;
        
        SurfaceInfo(BufferedImage image, BufImgInfo info)
        {
            this.image = image;
            this.info = info;
        }

        public BufferedImage getBuffer()
        {
            return image;
        }
        
        /**
         * Release resources to be recycled.  Do not use the image after calling this.
         */
        public void dispose()
        {
            image = null;
            allocatedSurfs.remove(info);
            pooledSurfs.add(info);
            info.touch();
            info = null;
        }
    }
    
    static class BufImgInfo implements Comparable<BufImgInfo>
    {
        final int width;
        final int height;
        final int area;
        WeakReference<SurfaceInfo> surfRef;
        final BufferedImage image;
        long touchTime;
        
        BufImgInfo(BufferedImage image)
        {
            this.width = image.getWidth();
            this.height = image.getHeight();
            this.area = width * height;
//            ref = new SoftReference(img);
            this.image = image;
//            this.surfRef = new WeakReference<SurfaceInfo>(null);
        }

        public void attach(SurfaceInfo surf)
        {
            surfRef = new WeakReference(surf);
//            touch();
        }
        
        public int compareTo(SurfaceManager.BufImgInfo obj)
        {
            return area < obj.area ? -1 : (area > obj.area ? 1 : 0);
        }
        
        void touch()
        {
            touchTime = System.currentTimeMillis();
        }
    }
    
    private static final SurfaceManager singleton = new SurfaceManager();
    TreeSet<BufImgInfo> pooledSurfs = new TreeSet<BufImgInfo>();
    HashSet<BufImgInfo> allocatedSurfs = new HashSet<BufImgInfo>();
//    WeakHashMap<SurfaceInfo, BufImgInfo> surfaces = new WeakHashMap<SurfaceInfo, BufImgInfo>();
    
    int SWEEP_DELAY = 10000;  //Every 10 seconds
    
    /** Creates a new instance of SurfaceManager */
    private SurfaceManager()
    {
        Thread thread = new Thread(this);
        thread.setDaemon(true);
        thread.start();
    }

    public static SurfaceManager getDefault()
    {
        return singleton;
    }
    
    /**
     * @return a new, cleared temporary surface to render upon.  Remember to call
     * dispose() on the SurfaceInfo when you're finished with it.
     */
    public SurfaceInfo getSurface(int width, int height, GraphicsConfiguration gc)
    {
        SurfaceInfo surfInfo;
        BufImgInfo bufInfo = null;
        int targetArea = width * height;
        
        //Search for exising surface
        Iterator<BufImgInfo> it = pooledSurfs.iterator();
        for (; it.hasNext();)
        {
            BufImgInfo info = it.next();
            if (info.area >= targetArea) 
            {
                if (info.width >= width && info.height >= height)
                {
                    bufInfo = info;
                }
                break;
            }
        }
        
        if (bufInfo == null)
        {
            for (; it.hasNext();)
            {
                BufImgInfo info = it.next();
                if (info.area / 2 <= targetArea) 
                {
                    //Do not use exceptionally poorly fit buffers
                    break;
                }
                if (info.width >= width && info.height >= height)
                {
                    bufInfo = info;
                    break;
                }
            }
        }
        
        if (bufInfo != null)
        {
            //Refurbish
//            BufferedImage img = bufInfo.ref.get();
            BufferedImage img = bufInfo.image;
            
            Graphics2D g = img.createGraphics();

            g.setComposite(AlphaComposite.Clear);
            g.fillRect(0, 0, width, height);

            g.dispose();

            surfInfo = new SurfaceInfo(img.getSubimage(0, 0, width, height), bufInfo);
//            surfaces.put(surfInfo, bufInfo);
            allocatedSurfs.add(bufInfo);
            pooledSurfs.remove(bufInfo);
        }
        else
        {
            //Create a new surface
            BufferedImage img;
            if (gc.getColorModel().hasAlpha())
            {
                img = gc.createCompatibleImage(width, height);
            }
            else
            {
                img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            }
            
            bufInfo = new BufImgInfo(img);
            surfInfo = new SurfaceInfo(img, bufInfo);
//            surfaces.put(surfInfo, bufInfo);
            allocatedSurfs.add(bufInfo);
        }
        
        bufInfo.attach(surfInfo);
        return surfInfo;
    }

    public void run()
    {
        while (true)
        {
            /*
            //Clean up memory leaked images
            for (Iterator<BufImgInfo> it = allocatedSurfs.iterator(); it.hasNext();)
            {
                BufImgInfo info = it.next();
                if (info.surfRef.get() == null)
                {
                    it.remove();
                    pooledSurfs.add(info);
                    info.touch();
                }
            }
             */
            
            //Kill out of date pooled surfaces
            long curTime = System.currentTimeMillis();
            for (Iterator<BufImgInfo> it = pooledSurfs.iterator(); it.hasNext();)
            {
                BufImgInfo info = it.next();
                if ((curTime - info.touchTime) > SWEEP_DELAY)
                {
                    it.remove();
                }
            }
            
            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}