aboutsummaryrefslogtreecommitdiff
path: root/engine/src/core/com/jme3/util/NativeObjectManager.java
blob: f8d1d181d9eac6cdc3148379fc4c606ab6dc0ca4 (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
/*
 * Copyright (c) 2009-2010 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.jme3.util;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * GLObjectManager tracks all GLObjects used by the Renderer. Using a
 * <code>ReferenceQueue</code> the <code>GLObjectManager</code> can delete
 * unused objects from GPU when their counterparts on the CPU are no longer used.
 *
 * On restart, the renderer may request the objects to be reset, thus allowing
 * the GLObjects to re-initialize with the new display context.
 */
public class NativeObjectManager {

    private static final Logger logger = Logger.getLogger(NativeObjectManager.class.getName());

    /**
     * The queue will receive notifications of {@link NativeObject}s which are no longer
     * referenced.
     */
    private ReferenceQueue<Object> refQueue = new ReferenceQueue<Object>();

    /**
     * List of currently active GLObjects.
     */
    private ArrayList<NativeObjectRef> refList
            = new ArrayList<NativeObjectRef>();

    private class NativeObjectRef extends PhantomReference<Object>{
        
        private NativeObject objClone;
        private WeakReference<NativeObject> realObj;

        public NativeObjectRef(NativeObject obj){
            super(obj.handleRef, refQueue);
            assert obj.handleRef != null;

            this.realObj = new WeakReference<NativeObject>(obj);
            this.objClone = obj.createDestructableClone();       
        }
    }

    /**
     * Register a GLObject with the manager.
     */
    public void registerForCleanup(NativeObject obj){
        NativeObjectRef ref = new NativeObjectRef(obj);
        refList.add(ref);
        if (logger.isLoggable(Level.FINEST))
            logger.log(Level.FINEST, "Registered: {0}", new String[]{obj.toString()});
    }

    /**
     * Deletes unused GLObjects
     */
    public void deleteUnused(Object rendererObject){
        while (true){
            NativeObjectRef ref = (NativeObjectRef) refQueue.poll();
            if (ref == null)
                return;

            refList.remove(ref);
            ref.objClone.deleteObject(rendererObject);
            if (logger.isLoggable(Level.FINEST))
                logger.log(Level.FINEST, "Deleted: {0}", ref.objClone);
        }
    }

    /**
     * Deletes all objects. Must only be called when display is destroyed.
     */
    public void deleteAllObjects(Object rendererObject){
        deleteUnused(rendererObject);
        for (NativeObjectRef ref : refList){
            ref.objClone.deleteObject(rendererObject);
            NativeObject realObj = ref.realObj.get();
            if (realObj != null){
                // Note: make sure to reset them as well
                // They may get used in a new renderer in the future
                realObj.resetObject();
            }
        }
        refList.clear();
    }

    /**
     * Resets all {@link NativeObject}s.
     */
    public void resetObjects(){
        for (NativeObjectRef ref : refList){
            // here we use the actual obj not the clone,
            // otherwise its useless
            NativeObject realObj = ref.realObj.get();
            if (realObj == null)
                continue;
            
            realObj.resetObject();
            if (logger.isLoggable(Level.FINEST))
                logger.log(Level.FINEST, "Reset: {0}", realObj);
        }
        refList.clear();
    }

//    public void printObjects(){
//        System.out.println(" ------------------- ");
//        System.out.println(" GL Object count: "+ objectList.size());
//        for (GLObject obj : objectList){
//            System.out.println(obj);
//        }
//    }
}