diff options
author | Mathieu Féry <[email protected]> | 2023-11-13 10:54:47 +0100 |
---|---|---|
committer | Mathieu Féry <[email protected]> | 2023-11-13 10:55:37 +0100 |
commit | 9ac10deb2fa4e965222fb0dcd8d3e1dd136e5944 (patch) | |
tree | 459255e57db63017b1dd0c6bbc76b42ebcd50e4d | |
parent | bf09df70444f38424a84fc382498b57498bb5601 (diff) |
feat(version): Invoking JoalVersion no longer destroys the current context
-rw-r--r-- | src/java/com/jogamp/openal/JoalVersion.java | 42 | ||||
-rw-r--r-- | src/test/com/jogamp/openal/test/junit/ALVersionTest.java | 44 |
2 files changed, 77 insertions, 9 deletions
diff --git a/src/java/com/jogamp/openal/JoalVersion.java b/src/java/com/jogamp/openal/JoalVersion.java index bca175e..4ae784b 100644 --- a/src/java/com/jogamp/openal/JoalVersion.java +++ b/src/java/com/jogamp/openal/JoalVersion.java @@ -110,9 +110,18 @@ public class JoalVersion extends JogampVersion { sb.append("ALC null"); return sb; } - final ALCdevice device = alc.alcOpenDevice(null); - final ALCcontext context = alc.alcCreateContext(device, null); - alc.alcMakeContextCurrent(context); + final ALCcontext initialContext = alc.alcGetCurrentContext(); + + final ALCcontext context; + final ALCdevice device; + if( null == initialContext) { + device = alc.alcOpenDevice(null); + context = alc.alcCreateContext(device, null); + alc.alcMakeContextCurrent(context); + } else { + context = initialContext; + device = alc.alcGetContextsDevice(initialContext); + } final AL al = ALFactory.getAL(); // valid after makeContextCurrent(..) final ALVersion alv = new ALVersion(al); @@ -151,9 +160,13 @@ public class JoalVersion extends JogampVersion { sb.append(Platform.getNewline()); } } - alc.alcMakeContextCurrent(null); - alc.alcDestroyContext(context); - alc.alcCloseDevice(device); + + if( null == initialContext ) { + alc.alcMakeContextCurrent(null); + alc.alcDestroyContext(context); + alc.alcCloseDevice(device); + } + devicesToString(sb, alc, enumerationExtIsPresent, enumerateAllExtIsPresent); return sb; } @@ -178,6 +191,8 @@ public class JoalVersion extends JogampVersion { final String inOutStr = "output"; final int mixerFrequency, mixerRefresh, monoSourceCount, stereoSourceCount; final int[] val = { 0 }; + final ALCcontext initialContext = alc.alcGetCurrentContext(); + final ALCdevice initialDevice = initialContext != null ? alc.alcGetContextsDevice(initialContext) : null; final ALCdevice d = alc.alcOpenDevice(devName); if( null == d ) { System.err.println("Error: Failed to open "+defStr+inOutStr+" device "+devName); @@ -237,9 +252,18 @@ public class JoalVersion extends JogampVersion { " (min latency "+(1000f/mixerRefresh)+" ms)], sources[mono "+monoSourceCount+", stereo "+stereoSourceCount+"]"+ System.lineSeparator()); - alc.alcMakeContextCurrent(null); - alc.alcDestroyContext(c); - alc.alcCloseDevice(d); + if( null != initialContext ) { + alc.alcMakeContextCurrent(initialContext); + if( initialContext.getDirectBufferAddress() != c.getDirectBufferAddress() ) { + alc.alcDestroyContext(c); + } + } else { + alc.alcMakeContextCurrent(null); + alc.alcDestroyContext(c); + } + if( initialDevice == null || initialDevice.getDirectBufferAddress() != d.getDirectBufferAddress() ) { + alc.alcCloseDevice(d); + } } } diff --git a/src/test/com/jogamp/openal/test/junit/ALVersionTest.java b/src/test/com/jogamp/openal/test/junit/ALVersionTest.java index cb24e95..55535ed 100644 --- a/src/test/com/jogamp/openal/test/junit/ALVersionTest.java +++ b/src/test/com/jogamp/openal/test/junit/ALVersionTest.java @@ -31,10 +31,14 @@ import java.io.IOException; import org.junit.FixMethodOrder; import org.junit.Test; +import org.junit.After; import org.junit.Assert; import org.junit.runners.MethodSorters; import com.jogamp.common.util.VersionNumber; +import com.jogamp.openal.ALC; +import com.jogamp.openal.ALCcontext; +import com.jogamp.openal.ALCdevice; import com.jogamp.openal.ALFactory; import com.jogamp.openal.ALVersion; import com.jogamp.openal.JoalVersion; @@ -70,6 +74,46 @@ public class ALVersionTest extends UITestCase { System.err.println(jv.toString(ALFactory.getALC())); } + @Test + public void test03JoalVersionMustNoChangeContextAndDeviceUsed() { + final ALC alc = ALFactory.getALC(); + final ALCdevice intialDevice = alc.alcOpenDevice(null); + final ALCcontext initialContext = alc.alcCreateContext(intialDevice, null); + alc.alcMakeContextCurrent(initialContext); + final JoalVersion jv = JoalVersion.getInstance(); + System.err.println(jv.toString(alc)); + final ALCcontext currentContext = alc.alcGetCurrentContext(); + Assert.assertNotNull(currentContext); + Assert.assertEquals(initialContext.getDirectBufferAddress(), currentContext.getDirectBufferAddress()); + final ALCdevice currentDevice = alc.alcGetContextsDevice(currentContext); + Assert.assertNotNull(currentDevice); + Assert.assertEquals(intialDevice.getDirectBufferAddress(), currentDevice.getDirectBufferAddress()); + } + + @Test + public void test04JoalVersionMustNotSetAdditionalContext() { + final ALC alc = ALFactory.getALC(); + final JoalVersion jv = JoalVersion.getInstance(); + System.err.println(jv.toString(alc)); + final ALCcontext currentContext = alc.alcGetCurrentContext(); + Assert.assertNull(currentContext); + } + + @After + @Override + public void tearDown() { + super.tearDown(); + final ALC alc = ALFactory.getALC(); + final ALCcontext context = alc.alcGetCurrentContext(); + if(null != context) { + alc.alcDestroyContext(context); + final ALCdevice device = alc.alcGetContextsDevice(context); + if(null != device) { + alc.alcCloseDevice(device); + } + } + } + public static void main(final String args[]) throws IOException { org.junit.runner.JUnitCore.main(ALVersionTest.class.getName()); } |