/* * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution 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 Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed or intended for use * in the design, construction, operation or maintenance of any nuclear * facility. * * Sun gratefully acknowledges that this software was originally authored * and developed by Kenneth Bradley Russell and Christopher John Kline. */ package jogamp.opengl.gl2; import jogamp.opengl.*; import java.nio.*; import com.jogamp.opengl.*; /** * Tracks the creation of server-side OpenGL objects which can be * shared between contexts. Ordinarily, when an OpenGL context is * deleted and no other contexts are sharing server-side objects with * it, all of the server-side objects are automatically deleted by the * OpenGL implementation. It is not necessary for the end user to * explicitly delete these objects. However, when the Java2D/OpenGL * pipeline is active and frame buffer objects are being used for * rendering, it is necessary for all OpenGL contexts created by JOGL * to share server-side objects with the Java2D OpenGL context. This * means that these objects "leak" into the namespace used by Java2D. * In order to prevent memory leaks and to present the same * programming model to the end user, it is necessary to track the * creation and destruction of all of these server-side OpenGL objects * and to explicitly release them when all of the JOGL-created * contexts which can see them have been released.

* * The {@link #ref ref} and {@link #unref unref} methods should be * used during the creation and destruction of OpenGL contexts by JOGL * in order to update the liveness of the objects being tracked. The * various other methods should be called by the OpenGL binding in the * various named methods. */ public class GLObjectTracker { private static final boolean DEBUG = Debug.debug("GLStatusTracker"); //---------------------------------------------------------------------- // Adders // // glGenBuffers public synchronized void addBuffers(int n, IntBuffer ids) { add(getList(BUFFERS), n, ids); } // glGenBuffers public synchronized void addBuffers(int n, int[] ids, int ids_offset) { add(getList(BUFFERS), n, ids, ids_offset); } // glGenBuffersARB public synchronized void addBuffersARB(int n, IntBuffer ids) { add(getList(BUFFERS_ARB), n, ids); } // glGenBuffersARB public synchronized void addBuffersARB(int n, int[] ids, int ids_offset) { add(getList(BUFFERS_ARB), n, ids, ids_offset); } // glGenFencesAPPLE public synchronized void addFencesAPPLE(int n, IntBuffer ids) { add(getList(FENCES_APPLE), n, ids); } // glGenFencesAPPLE public synchronized void addFencesAPPLE(int n, int[] ids, int ids_offset) { add(getList(FENCES_APPLE), n, ids, ids_offset); } // glGenFencesNV public synchronized void addFencesNV(int n, IntBuffer ids) { add(getList(FENCES_NV), n, ids); } // glGenFencesNV public synchronized void addFencesNV(int n, int[] ids, int ids_offset) { add(getList(FENCES_NV), n, ids, ids_offset); } // glGenFragmentShadersATI public synchronized void addFragmentShadersATI(int start, int n) { add(getList(FRAGMENT_SHADERS_ATI), start, n); } // glGenFramebuffersEXT public synchronized void addFramebuffersEXT(int n, IntBuffer ids) { add(getList(FRAMEBUFFERS_EXT), n, ids); } // glGenFramebuffersEXT public synchronized void addFramebuffersEXT(int n, int[] ids, int ids_offset) { add(getList(FRAMEBUFFERS_EXT), n, ids, ids_offset); } // glGenLists public synchronized void addLists(int start, int n) { add(getList(LISTS), start, n); } // glGenOcclusionQueriesNV public synchronized void addOcclusionQueriesNV(int n, IntBuffer ids) { add(getList(OCCLUSION_QUERIES_NV), n, ids); } // glGenOcclusionQueriesNV public synchronized void addOcclusionQueriesNV(int n, int[] ids, int ids_offset) { add(getList(OCCLUSION_QUERIES_NV), n, ids, ids_offset); } // glCreateProgram public synchronized void addProgramObject(int obj) { add(getList(PROGRAM_OBJECTS), obj, 1); } // glCreateProgramObjectARB public synchronized void addProgramObjectARB(int obj) { add(getList(PROGRAM_AND_SHADER_OBJECTS_ARB), obj, 1); } // glGenProgramsARB public synchronized void addProgramsARB(int n, IntBuffer ids) { add(getList(PROGRAMS_ARB), n, ids); } // glGenProgramsARB public synchronized void addProgramsARB(int n, int[] ids, int ids_offset) { add(getList(PROGRAMS_ARB), n, ids, ids_offset); } // glGenProgramsNV public synchronized void addProgramsNV(int n, IntBuffer ids) { add(getList(PROGRAMS_NV), n, ids); } // glGenProgramsNV public synchronized void addProgramsNV(int n, int[] ids, int ids_offset) { add(getList(PROGRAMS_NV), n, ids, ids_offset); } // glGenQueries public synchronized void addQueries(int n, IntBuffer ids) { add(getList(QUERIES), n, ids); } // glGenQueries public synchronized void addQueries(int n, int[] ids, int ids_offset) { add(getList(QUERIES), n, ids, ids_offset); } // glGenQueriesARB public synchronized void addQueriesARB(int n, IntBuffer ids) { add(getList(QUERIES_ARB), n, ids); } // glGenQueriesARB public synchronized void addQueriesARB(int n, int[] ids, int ids_offset) { add(getList(QUERIES_ARB), n, ids, ids_offset); } // glGenRenderbuffersEXT public synchronized void addRenderbuffersEXT(int n, IntBuffer ids) { add(getList(RENDERBUFFERS_EXT), n, ids); } // glGenRenderbuffersEXT public synchronized void addRenderbuffersEXT(int n, int[] ids, int ids_offset) { add(getList(RENDERBUFFERS_EXT), n, ids, ids_offset); } // glCreateShader public synchronized void addShaderObject(int obj) { add(getList(SHADER_OBJECTS), obj, 1); } // glCreateShaderObjectARB public synchronized void addShaderObjectARB(int obj) { add(getList(PROGRAM_AND_SHADER_OBJECTS_ARB), obj, 1); } // glGenTextures public synchronized void addTextures(int n, IntBuffer ids) { add(getList(TEXTURES), n, ids); } // glGenTextures public synchronized void addTextures(int n, int[] ids, int ids_offset) { add(getList(TEXTURES), n, ids, ids_offset); } // glGenVertexArraysAPPLE public synchronized void addVertexArraysAPPLE(int n, IntBuffer ids) { add(getList(VERTEX_ARRAYS_APPLE), n, ids); } // glGenVertexArraysAPPLE public synchronized void addVertexArraysAPPLE(int n, int[] ids, int ids_offset) { add(getList(VERTEX_ARRAYS_APPLE), n, ids, ids_offset); } // glGenVertexShadersEXT public synchronized void addVertexShadersEXT(int start, int n) { add(getList(VERTEX_SHADERS_EXT), start, n); } //---------------------------------------------------------------------- // Removers // // glDeleteBuffers public synchronized void removeBuffers(int n, IntBuffer ids) { remove(getList(BUFFERS), n, ids); } // glDeleteBuffers public synchronized void removeBuffers(int n, int[] ids, int ids_offset) { remove(getList(BUFFERS), n, ids, ids_offset); } // glDeleteBuffersARB public synchronized void removeBuffersARB(int n, IntBuffer ids) { remove(getList(BUFFERS_ARB), n, ids); } // glDeleteBuffersARB public synchronized void removeBuffersARB(int n, int[] ids, int ids_offset) { remove(getList(BUFFERS_ARB), n, ids, ids_offset); } // glDeleteFencesAPPLE public synchronized void removeFencesAPPLE(int n, IntBuffer ids) { remove(getList(FENCES_APPLE), n, ids); } // glDeleteFencesAPPLE public synchronized void removeFencesAPPLE(int n, int[] ids, int ids_offset) { remove(getList(FENCES_APPLE), n, ids, ids_offset); } // glDeleteFencesNV public synchronized void removeFencesNV(int n, IntBuffer ids) { remove(getList(FENCES_NV), n, ids); } // glDeleteFencesNV public synchronized void removeFencesNV(int n, int[] ids, int ids_offset) { remove(getList(FENCES_NV), n, ids, ids_offset); } // glDeleteFragmentShaderATI public synchronized void removeFragmentShaderATI(int obj) { remove(getList(FRAGMENT_SHADERS_ATI), obj, 1); } // glDeleteFramebuffersEXT public synchronized void removeFramebuffersEXT(int n, IntBuffer ids) { remove(getList(FRAMEBUFFERS_EXT), n, ids); } // glDeleteFramebuffersEXT public synchronized void removeFramebuffersEXT(int n, int[] ids, int ids_offset) { remove(getList(FRAMEBUFFERS_EXT), n, ids, ids_offset); } // glDeleteLists public synchronized void removeLists(int start, int n) { remove(getList(LISTS), start, n); } // glDeleteOcclusionQueriesNV public synchronized void removeOcclusionQueriesNV(int n, IntBuffer ids) { remove(getList(OCCLUSION_QUERIES_NV), n, ids); } // glDeleteOcclusionQueriesNV public synchronized void removeOcclusionQueriesNV(int n, int[] ids, int ids_offset) { remove(getList(OCCLUSION_QUERIES_NV), n, ids, ids_offset); } // glDeleteProgram public synchronized void removeProgramObject(int obj) { remove(getList(PROGRAM_OBJECTS), obj, 1); } // glDeleteObjectARB public synchronized void removeProgramOrShaderObjectARB(int obj) { remove(getList(PROGRAM_AND_SHADER_OBJECTS_ARB), obj, 1); } // glDeleteProgramsARB public synchronized void removeProgramsARB(int n, IntBuffer ids) { remove(getList(PROGRAMS_ARB), n, ids); } // glDeleteProgramsARB public synchronized void removeProgramsARB(int n, int[] ids, int ids_offset) { remove(getList(PROGRAMS_ARB), n, ids, ids_offset); } // glDeleteProgramsNV public synchronized void removeProgramsNV(int n, IntBuffer ids) { remove(getList(PROGRAMS_NV), n, ids); } // glDeleteProgramsNV public synchronized void removeProgramsNV(int n, int[] ids, int ids_offset) { remove(getList(PROGRAMS_NV), n, ids, ids_offset); } // glDeleteQueries public synchronized void removeQueries(int n, IntBuffer ids) { remove(getList(QUERIES), n, ids); } // glDeleteQueries public synchronized void removeQueries(int n, int[] ids, int ids_offset) { remove(getList(QUERIES), n, ids, ids_offset); } // glDeleteQueriesARB public synchronized void removeQueriesARB(int n, IntBuffer ids) { remove(getList(QUERIES_ARB), n, ids); } // glDeleteQueriesARB public synchronized void removeQueriesARB(int n, int[] ids, int ids_offset) { remove(getList(QUERIES_ARB), n, ids, ids_offset); } // glDeleteRenderbuffersEXT public synchronized void removeRenderbuffersEXT(int n, IntBuffer ids) { remove(getList(RENDERBUFFERS_EXT), n, ids); } // glDeleteRenderbuffersEXT public synchronized void removeRenderbuffersEXT(int n, int[] ids, int ids_offset) { remove(getList(RENDERBUFFERS_EXT), n, ids, ids_offset); } // glDeleteShader public synchronized void removeShaderObject(int obj) { remove(getList(SHADER_OBJECTS), obj, 1); } // glDeleteTextures public synchronized void removeTextures(int n, IntBuffer ids) { remove(getList(TEXTURES), n, ids); } // glDeleteTextures public synchronized void removeTextures(int n, int[] ids, int ids_offset) { remove(getList(TEXTURES), n, ids, ids_offset); } // glDeleteVertexArraysAPPLE public synchronized void removeVertexArraysAPPLE(int n, IntBuffer ids) { remove(getList(VERTEX_ARRAYS_APPLE), n, ids); } // glDeleteVertexArraysAPPLE public synchronized void removeVertexArraysAPPLE(int n, int[] ids, int ids_offset) { remove(getList(VERTEX_ARRAYS_APPLE), n, ids, ids_offset); } // glDeleteVertexShaderEXT public synchronized void removeVertexShaderEXT(int obj) { remove(getList(VERTEX_SHADERS_EXT), obj, 1); } //---------------------------------------------------------------------- // Reference count maintenance and manual deletion // public synchronized void transferAll(GLObjectTracker other) { for (int i = 0; i < lists.length; i++) { getList(i).addAll(other.lists[i]); if (other.lists[i] != null) { other.lists[i].clear(); * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * -Redistribution of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * -Redistribution 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 Sun Microsystems, Inc. or the names of contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS * LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT * OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed or intended for use in the * design, construction, operation or maintenance of any nuclear facility. * */ package demos.joal; import java.io.*; import java.nio.ByteBuffer; import com.jogamp.openal.*; import com.jogamp.openal.util.*; // For the GUI import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * Adapted from <a href="http://www.devmaster.net/">DevMaster</a> * <a href="http://www.devmaster.net/articles/openal-tutorials/lesson1.php">SingleStaticSource Tutorial</a> * by Jesse Maurais. * * @author Athomas Goldberg * @author Kenneth Russell */ public class SingleStaticSource { static final String audioFile = "demos/data/sound/FancyPants.wav"; public SingleStaticSource(boolean gui) { this(gui, null, true); } public SingleStaticSource(boolean gui, Container parent, boolean showQuitButton) { if (gui) { JFrame frame = null; if (parent == null) { frame = new JFrame("Single Static Source - DevMaster OpenAL Lesson 1"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); parent = frame.getContentPane(); } JPanel container = new JPanel(); container.setLayout(new GridLayout((showQuitButton ? 4 : 3), 1)); JButton button = new JButton("Play sound"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!initialize()) System.exit(1); al.alSourcePlay(source[0]); } }); container.add(button); button = new JButton("Stop playing"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!initialize()) System.exit(1); al.alSourceStop(source[0]); } }); container.add(button); button = new JButton("Pause sound"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!initialize()) System.exit(1); al.alSourcePause(source[0]); } }); container.add(button); if (showQuitButton) { button = new JButton("Quit"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!initialize()) System.exit(1); killAllData(); System.exit(0); } }); container.add(button); } parent.add(container); if (frame != null) { frame.