* Moved GLObjectManager to util, it is now NativeObjectManager and can be used to manage collection of any native object

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8422 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
sha..rd 13 years ago
parent 1b665cc38e
commit 107380afc1
  1. 4
      engine/src/android/com/jme3/renderer/android/OGLESShaderRenderer.java
  2. 16
      engine/src/core/com/jme3/scene/VertexBuffer.java
  3. 30
      engine/src/core/com/jme3/shader/Shader.java
  4. 14
      engine/src/core/com/jme3/texture/FrameBuffer.java
  5. 14
      engine/src/core/com/jme3/texture/Image.java
  6. 65
      engine/src/core/com/jme3/util/NativeObject.java
  7. 48
      engine/src/core/com/jme3/util/NativeObjectManager.java
  8. 4
      engine/src/lwjgl-ogl/com/jme3/renderer/lwjgl/LwjglGL1Renderer.java
  9. 4
      engine/src/lwjgl-ogl/com/jme3/renderer/lwjgl/LwjglRenderer.java

@ -39,7 +39,7 @@ import com.jme3.math.Quaternion;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Caps;
import com.jme3.renderer.GLObjectManager;
import com.jme3.util.NativeObjectManager;
import com.jme3.renderer.IDList;
import com.jme3.renderer.Renderer;
import com.jme3.scene.Mesh;
@ -93,7 +93,7 @@ public class OGLESShaderRenderer implements Renderer {
private final IntBuffer intBuf1 = BufferUtils.createIntBuffer(1);
private final IntBuffer intBuf16 = BufferUtils.createIntBuffer(16);
private final RenderContext context = new RenderContext();
private final GLObjectManager objManager = new GLObjectManager();
private final NativeObjectManager objManager = new NativeObjectManager();
private final EnumSet<Caps> caps = EnumSet.noneOf(Caps.class);
// current state
private Shader boundShader;

@ -38,7 +38,7 @@ import com.jme3.export.InputCapsule;
import com.jme3.export.OutputCapsule;
import com.jme3.export.Savable;
import com.jme3.math.FastMath;
import com.jme3.renderer.GLObject;
import com.jme3.util.NativeObject;
import com.jme3.renderer.Renderer;
import com.jme3.util.BufferUtils;
import java.io.IOException;
@ -62,7 +62,7 @@ import java.nio.ShortBuffer;
* For a 3D vector, a single component is one of the dimensions, X, Y or Z.</li>
* </ul>
*/
public class VertexBuffer extends GLObject implements Savable, Cloneable {
public class VertexBuffer extends NativeObject implements Savable, Cloneable {
/**
* Type of buffer. Specifies the actual attribute it defines.
@ -316,7 +316,7 @@ public class VertexBuffer extends GLObject implements Savable, Cloneable {
* Must call setupData() to initialize.
*/
public VertexBuffer(Type type){
super(GLObject.Type.VertexBuffer);
super(VertexBuffer.class);
this.bufType = type;
}
@ -324,11 +324,11 @@ public class VertexBuffer extends GLObject implements Savable, Cloneable {
* Serialization only. Do not use.
*/
public VertexBuffer(){
super(GLObject.Type.VertexBuffer);
super(VertexBuffer.class);
}
protected VertexBuffer(int id){
super(GLObject.Type.VertexBuffer, id);
super(VertexBuffer.class, id);
}
/**
@ -853,12 +853,12 @@ public class VertexBuffer extends GLObject implements Savable, Cloneable {
}
@Override
public void deleteObject(Renderer r) {
r.deleteBuffer(this);
public void deleteObject(Object rendererObject) {
((Renderer)rendererObject).deleteBuffer(this);
}
@Override
public GLObject createDestructableClone(){
public NativeObject createDestructableClone(){
return new VertexBuffer(id);
}

@ -37,7 +37,7 @@ import com.jme3.export.JmeImporter;
import com.jme3.export.InputCapsule;
import com.jme3.export.OutputCapsule;
import com.jme3.export.Savable;
import com.jme3.renderer.GLObject;
import com.jme3.util.NativeObject;
import com.jme3.renderer.Renderer;
import com.jme3.scene.VertexBuffer;
import com.jme3.util.IntMap;
@ -48,7 +48,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
public final class Shader extends GLObject implements Savable {
public final class Shader extends NativeObject implements Savable {
private String language;
@ -98,7 +98,7 @@ public final class Shader extends GLObject implements Savable {
* Shader source describes a shader object in OpenGL. Each shader source
* is assigned a certain pipeline which it controls (described by it's type).
*/
public static class ShaderSource extends GLObject implements Savable {
public static class ShaderSource extends NativeObject implements Savable {
ShaderType shaderType;
@ -108,14 +108,14 @@ public final class Shader extends GLObject implements Savable {
String defines = null;
public ShaderSource(ShaderType type){
super(Type.ShaderSource);
super(ShaderSource.class);
this.shaderType = type;
if (type == null)
throw new NullPointerException("The shader type must be specified");
}
protected ShaderSource(ShaderSource ss){
super(Type.ShaderSource, ss.id);
super(ShaderSource.class, ss.id);
this.shaderType = ss.shaderType;
usable = false;
name = ss.name;
@ -123,7 +123,7 @@ public final class Shader extends GLObject implements Savable {
}
public ShaderSource(){
super(Type.ShaderSource);
super(ShaderSource.class);
}
public void write(JmeExporter ex) throws IOException{
@ -205,11 +205,11 @@ public final class Shader extends GLObject implements Savable {
setUpdateNeeded();
}
public void deleteObject(Renderer r){
r.deleteShaderSource(ShaderSource.this);
public void deleteObject(Object rendererObject){
((Renderer)rendererObject).deleteShaderSource(ShaderSource.this);
}
public GLObject createDestructableClone(){
public NativeObject createDestructableClone(){
return new ShaderSource(ShaderSource.this);
}
}
@ -218,7 +218,7 @@ public final class Shader extends GLObject implements Savable {
* Create an empty shader.
*/
public Shader(String language){
super(Type.Shader);
super(Shader.class);
this.language = language;
shaderList = new ArrayList<ShaderSource>();
// uniforms = new HashMap<String, Uniform>();
@ -230,11 +230,11 @@ public final class Shader extends GLObject implements Savable {
* Do not use this constructor. Serialization purposes only.
*/
public Shader(){
super(Type.Shader);
super(Shader.class);
}
protected Shader(Shader s){
super(Type.Shader, s.id);
super(Shader.class, s.id);
shaderList = new ArrayList<ShaderSource>();
//uniforms = new ListMap<String, Uniform>();
//attribs = new IntMap<Attribute>();
@ -436,11 +436,11 @@ public final class Shader extends GLObject implements Savable {
}
@Override
public void deleteObject(Renderer r) {
r.deleteShader(this);
public void deleteObject(Object rendererObject) {
((Renderer)rendererObject).deleteShader(this);
}
public GLObject createDestructableClone(){
public NativeObject createDestructableClone(){
return new Shader(this);
}

@ -33,7 +33,7 @@
package com.jme3.texture;
import com.jme3.renderer.Caps;
import com.jme3.renderer.GLObject;
import com.jme3.util.NativeObject;
import com.jme3.renderer.Renderer;
import com.jme3.texture.Image.Format;
import java.util.ArrayList;
@ -72,7 +72,7 @@ import java.util.ArrayList;
*
* @author Kirill Vainer
*/
public class FrameBuffer extends GLObject {
public class FrameBuffer extends NativeObject {
private int width = 0;
private int height = 0;
@ -171,7 +171,7 @@ public class FrameBuffer extends GLObject {
* @throws IllegalArgumentException If width or height are not positive.
*/
public FrameBuffer(int width, int height, int samples){
super(Type.FrameBuffer);
super(FrameBuffer.class);
if (width <= 0 || height <= 0)
throw new IllegalArgumentException("FrameBuffer must have valid size.");
@ -181,7 +181,7 @@ public class FrameBuffer extends GLObject {
}
protected FrameBuffer(FrameBuffer src){
super(Type.FrameBuffer, src.id);
super(FrameBuffer.class, src.id);
/*
for (RenderBuffer renderBuf : src.colorBufs){
RenderBuffer clone = renderBuf.createDestructableClone();
@ -449,11 +449,11 @@ public class FrameBuffer extends GLObject {
}
@Override
public void deleteObject(Renderer r) {
r.deleteFrameBuffer(this);
public void deleteObject(Object rendererObject) {
((Renderer)rendererObject).deleteFrameBuffer(this);
}
public GLObject createDestructableClone(){
public NativeObject createDestructableClone(){
return new FrameBuffer(this);
}
}

@ -43,7 +43,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.jme3.export.Savable;
import com.jme3.renderer.GLObject;
import com.jme3.util.NativeObject;
/**
* <code>Image</code> defines a data format for a graphical image. The image
@ -55,7 +55,7 @@ import com.jme3.renderer.GLObject;
* @author Joshua Slack
* @version $Id: Image.java 4131 2009-03-19 20:15:28Z blaine.dev $
*/
public class Image extends GLObject implements Savable /*, Cloneable*/ {
public class Image extends NativeObject implements Savable /*, Cloneable*/ {
public enum Format {
/**
@ -329,12 +329,12 @@ public class Image extends GLObject implements Savable /*, Cloneable*/ {
}
@Override
public void deleteObject(Renderer r) {
r.deleteImage(this);
public void deleteObject(Object rendererObject) {
((Renderer)rendererObject).deleteImage(this);
}
@Override
public GLObject createDestructableClone() {
public NativeObject createDestructableClone() {
return new Image(id);
}
@ -355,12 +355,12 @@ public class Image extends GLObject implements Savable /*, Cloneable*/ {
* are undefined.
*/
public Image() {
super(Type.Texture);
super(Image.class);
data = new ArrayList<ByteBuffer>(1);
}
protected Image(int id){
super(GLObject.Type.Texture, id);
super(Image.class, id);
}
/**

@ -30,16 +30,19 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.renderer;
package com.jme3.util;
import com.jme3.renderer.Renderer;
/**
* Describes a GL object. An encapsulation of a certain object
* on the native side of the graphics library.
* This class is used to track when OpenGL native objects are collected
* by the garbage collector, and then invoke the proper destructor
* Describes a native object. An encapsulation of a certain object
* on the native side of the graphics or audio library.
*
* This class is used to track when OpenGL and OpenAL native objects are
* collected by the garbage collector, and then invoke the proper destructor
* on the OpenGL library to delete it from memory.
*/
public abstract class GLObject implements Cloneable {
public abstract class NativeObject implements Cloneable {
/**
* The ID of the object, usually depends on its type.
@ -63,41 +66,7 @@ public abstract class GLObject implements Cloneable {
/**
* The type of the GLObject, usually specified by a subclass.
*/
protected final Type type;
/**
* The type of the GLObject, usually specified by a subclass.
*/
public static enum Type {
/**
* A texture is an image that is applied to geometry.
*/
Texture,
/**
* Vertex buffers are used to describe geometry data and it's attributes.
*/
VertexBuffer,
/**
* ShaderSource is a shader source code that controls the output of
* a certain rendering pipeline, like vertex position or fragment color.
*/
ShaderSource,
/**
* A Shader is an aggregation of ShaderSources, collectively
* they cooperate to control the vertex and fragment processor.
*/
Shader,
/**
* FrameBuffer is an offscreen surface which can be rendered to.
* Can be used to create "Render-to-Texture" effects and
* scene post processing.
*/
FrameBuffer,
}
protected final Class<?> type;
/**
* Creates a new GLObject with the given type. Should be
@ -105,7 +74,7 @@ public abstract class GLObject implements Cloneable {
*
* @param type The type that the subclass represents.
*/
public GLObject(Type type){
public NativeObject(Class<?> type){
this.type = type;
this.handleRef = new Object();
}
@ -114,7 +83,7 @@ public abstract class GLObject implements Cloneable {
* Protected constructor that doesn't allocate handle ref.
* This is used in subclasses for the createDestructableClone().
*/
protected GLObject(Type type, int id){
protected NativeObject(Class<?> type, int id){
this.type = type;
this.id = id;
}
@ -163,7 +132,7 @@ public abstract class GLObject implements Cloneable {
@Override
public String toString(){
return type.name() + " " + Integer.toHexString(hashCode());
return "Native" + type.getSimpleName() + " " + id;
}
/**
@ -171,9 +140,9 @@ public abstract class GLObject implements Cloneable {
* createDestructableClone().
*/
@Override
protected GLObject clone(){
protected NativeObject clone(){
try{
GLObject obj = (GLObject) super.clone();
NativeObject obj = (NativeObject) super.clone();
obj.handleRef = new Object();
obj.id = -1;
obj.updateNeeded = true;
@ -195,11 +164,11 @@ public abstract class GLObject implements Cloneable {
*
* @param r The renderer to be used to delete the object
*/
public abstract void deleteObject(Renderer r);
public abstract void deleteObject(Object rendererObject);
/**
* Creates a shallow clone of this GL Object. The deleteObject method
* should be functional for this object.
*/
public abstract GLObject createDestructableClone();
public abstract NativeObject createDestructableClone();
}

@ -30,7 +30,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.renderer;
package com.jme3.util;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
@ -47,12 +47,12 @@ import java.util.logging.Logger;
* 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 GLObjectManager {
public class NativeObjectManager {
private static final Logger logger = Logger.getLogger(GLObjectManager.class.getName());
private static final Logger logger = Logger.getLogger(NativeObjectManager.class.getName());
/**
* The queue will receive notifications of GLObjects which are no longer
* The queue will receive notifications of {@link NativeObject}s which are no longer
* referenced.
*/
private ReferenceQueue<Object> refQueue = new ReferenceQueue<Object>();
@ -60,19 +60,19 @@ public class GLObjectManager {
/**
* List of currently active GLObjects.
*/
private ArrayList<GLObjectRef> refList
= new ArrayList<GLObjectRef>();
private ArrayList<NativeObjectRef> refList
= new ArrayList<NativeObjectRef>();
private class GLObjectRef extends PhantomReference<Object>{
private class NativeObjectRef extends PhantomReference<Object>{
private GLObject objClone;
private WeakReference<GLObject> realObj;
private NativeObject objClone;
private WeakReference<NativeObject> realObj;
public GLObjectRef(GLObject obj){
public NativeObjectRef(NativeObject obj){
super(obj.handleRef, refQueue);
assert obj.handleRef != null;
this.realObj = new WeakReference<GLObject>(obj);
this.realObj = new WeakReference<NativeObject>(obj);
this.objClone = obj.createDestructableClone();
}
}
@ -80,8 +80,8 @@ public class GLObjectManager {
/**
* Register a GLObject with the manager.
*/
public void registerForCleanup(GLObject obj){
GLObjectRef ref = new GLObjectRef(obj);
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()});
@ -90,14 +90,14 @@ public class GLObjectManager {
/**
* Deletes unused GLObjects
*/
public void deleteUnused(Renderer r){
public void deleteUnused(Object rendererObject){
while (true){
GLObjectRef ref = (GLObjectRef) refQueue.poll();
NativeObjectRef ref = (NativeObjectRef) refQueue.poll();
if (ref == null)
return;
refList.remove(ref);
ref.objClone.deleteObject(r);
ref.objClone.deleteObject(rendererObject);
if (logger.isLoggable(Level.FINEST))
logger.log(Level.FINEST, "Deleted: {0}", ref.objClone);
}
@ -106,11 +106,11 @@ public class GLObjectManager {
/**
* Deletes all objects. Must only be called when display is destroyed.
*/
public void deleteAllObjects(Renderer r){
deleteUnused(r);
for (GLObjectRef ref : refList){
ref.objClone.deleteObject(r);
GLObject realObj = ref.realObj.get();
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
@ -121,13 +121,13 @@ public class GLObjectManager {
}
/**
* Resets all GLObjects.
* Resets all {@link NativeObject}s.
*/
public void resetObjects(){
for (GLObjectRef ref : refList){
for (NativeObjectRef ref : refList){
// here we use the actual obj not the clone,
// otherwise its useless
GLObject realObj = ref.realObj.get();
NativeObject realObj = ref.realObj.get();
if (realObj == null)
continue;

@ -32,7 +32,7 @@ import java.util.logging.Level;
import com.jme3.material.RenderState;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.Caps;
import com.jme3.renderer.GLObjectManager;
import com.jme3.util.NativeObjectManager;
import com.jme3.renderer.RenderContext;
import com.jme3.renderer.Statistics;
import com.jme3.util.BufferUtils;
@ -56,7 +56,7 @@ public class LwjglGL1Renderer implements GL1Renderer {
private final FloatBuffer fb16 = BufferUtils.createFloatBuffer(16);
private final FloatBuffer fb4Null = BufferUtils.createFloatBuffer(4);
private final RenderContext context = new RenderContext();
private final GLObjectManager objManager = new GLObjectManager();
private final NativeObjectManager objManager = new NativeObjectManager();
private final EnumSet<Caps> caps = EnumSet.noneOf(Caps.class);
private int maxTexSize;
private int maxCubeTexSize;

@ -43,7 +43,7 @@ import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.math.Vector4f;
import com.jme3.renderer.Caps;
import com.jme3.renderer.GLObjectManager;
import com.jme3.util.NativeObjectManager;
import com.jme3.renderer.IDList;
import com.jme3.renderer.Renderer;
import com.jme3.scene.Mesh;
@ -124,7 +124,7 @@ public class LwjglRenderer implements Renderer {
private final IntBuffer intBuf1 = BufferUtils.createIntBuffer(1);
private final IntBuffer intBuf16 = BufferUtils.createIntBuffer(16);
private final RenderContext context = new RenderContext();
private final GLObjectManager objManager = new GLObjectManager();
private final NativeObjectManager objManager = new NativeObjectManager();
private final EnumSet<Caps> caps = EnumSet.noneOf(Caps.class);
// current state
private Shader boundShader;

Loading…
Cancel
Save