JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
StereoDeviceFactory.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.opengl.util.stereo;
29
30import java.lang.ref.WeakReference;
31import java.util.ArrayList;
32
33import com.jogamp.common.util.ReflectionUtil;
34import com.jogamp.nativewindow.NativeWindowFactory;
35
36/**
37 * Platform agnostic {@link StereoDevice} factory.
38 * <p>
39 * To implement a new {@link StereoDevice}, the following interfaces/classes must be implemented:
40 * <ul>
41 * <li>{@link StereoDeviceFactory}</li>
42 * <li>{@link StereoDevice}</li>
43 * <li>{@link StereoDeviceRenderer}</li>
44 * </ul>
45 * </p>
46 */
47public abstract class StereoDeviceFactory {
48 private static final String OVRStereoDeviceClazzName = "jogamp.opengl.oculusvr.OVRStereoDeviceFactory";
49 private static final String GenericStereoDeviceClazzName = com.jogamp.opengl.util.stereo.generic.GenericStereoDeviceFactory.class.getName();
50 private static final String isAvailableMethodName = "isAvailable";
51 static {
52 NativeWindowFactory.addCustomShutdownHook(false /* head */, new Runnable() {
53 @Override
54 public void run() {
55 shutdownAll();
56 }
57 });
58 }
59
60 /** {@link StereoDevice} type used for {@link StereoDeviceFactory#createFactory(DeviceType) createFactory(type)}. */
61 public static enum DeviceType {
62 /**
63 * Auto selection of device in the following order:
64 * <ol>
65 * <li>{@link DeviceType#OculusVR}</li>
66 * <li>{@link DeviceType#Generic}</li>
67 * </ol>
68 */
70 /**
71 * Generic software implementation.
72 */
74 /**
75 * OculusVR DK1 implementation.
76 */
78 /**
79 * OculusVR DK2 implementation.
80 */
81 OculusVR_DK2
82 };
83
85 final ClassLoader cl = StereoDeviceFactory.class.getClassLoader();
86 StereoDeviceFactory sink = createFactory(cl, OVRStereoDeviceClazzName);
87 if( null == sink ) {
88 sink = createFactory(cl, GenericStereoDeviceClazzName);
89 }
90 return sink;
91 }
92
93 public static StereoDeviceFactory createFactory(final DeviceType type) {
94 final String className;
95 switch( type ) {
96 case Default: return createDefaultFactory();
97 case Generic: className = GenericStereoDeviceClazzName; break;
98 case OculusVR: className = OVRStereoDeviceClazzName; break;
99 default: throw new InternalError("Unsupported type "+type);
100 }
101 final ClassLoader cl = StereoDeviceFactory.class.getClassLoader();
102 return createFactory(cl, className);
103 }
104
105 public static StereoDeviceFactory createFactory(final ClassLoader cl, final String implName) {
106 StereoDeviceFactory res = null;
107 try {
108 if(((Boolean)ReflectionUtil.callStaticMethod(implName, isAvailableMethodName, null, null, cl)).booleanValue()) {
109 res = (StereoDeviceFactory) ReflectionUtil.createInstance(implName, cl);
110 }
111 } catch (final Throwable t) { if(StereoDevice.DEBUG) { System.err.println("Caught "+t.getClass().getName()+": "+t.getMessage()); t.printStackTrace(); } }
112 if( null != res ) {
113 addFactory2List(res);
114 }
115 return res;
116 }
117
118 /**
119 *
120 * @param deviceIndex
121 * @param config optional custom configuration, matching the implementation, i.e. {@link StereoDeviceConfig.GenericStereoDeviceConfig}.
122 * @param verbose
123 * @return
124 */
125 public final StereoDevice createDevice(final int deviceIndex, final StereoDeviceConfig config, final boolean verbose) {
126 final StereoDevice device = createDeviceImpl(deviceIndex, config, verbose);
127 if( null != device ) {
128 addDevice2List(device);
129 }
130 return device;
131 }
132 protected abstract StereoDevice createDeviceImpl(final int deviceIndex, final StereoDeviceConfig config, final boolean verbose);
133
134 /**
135 * Returns {@code true}, if instance is created and not {@link #shutdown()}
136 * otherwise returns {@code false}.
137 */
138 public abstract boolean isValid();
139
140 /**
141 * Shutdown factory if {@link #isValid() valid}.
142 */
143 public abstract void shutdown();
144
145 private static final ArrayList<WeakReference<StereoDeviceFactory>> factoryList = new ArrayList<WeakReference<StereoDeviceFactory>>();
146 private static void addFactory2List(final StereoDeviceFactory factory) {
147 synchronized(factoryList) {
148 // GC before add
149 int i=0;
150 while( i < factoryList.size() ) {
151 if( null == factoryList.get(i).get() ) {
152 factoryList.remove(i);
153 } else {
154 i++;
155 }
156 }
157 factoryList.add(new WeakReference<StereoDeviceFactory>(factory));
158 }
159 }
160 private static final ArrayList<WeakReference<StereoDevice>> deviceList = new ArrayList<WeakReference<StereoDevice>>();
161 private static void addDevice2List(final StereoDevice device) {
162 synchronized(deviceList) {
163 // GC before add
164 int i=0;
165 while( i < deviceList.size() ) {
166 if( null == deviceList.get(i).get() ) {
167 deviceList.remove(i);
168 } else {
169 i++;
170 }
171 }
172 deviceList.add(new WeakReference<StereoDevice>(device));
173 }
174 }
175
176 private final static void shutdownAll() {
177 shutdownDevices();
178 shutdownFactories();
179 }
180 private final static void shutdownFactories() {
181 while( 0 < factoryList.size() ) {
182 final StereoDeviceFactory f = factoryList.remove(0).get();
183 if( null != f && f.isValid() ) {
184 f.shutdown();
185 }
186 }
187 }
188 private final static void shutdownDevices() {
189 while( 0 < deviceList.size() ) {
190 final StereoDevice d = deviceList.remove(0).get();
191 if( null != d && d.isValid() ) {
192 d.dispose();
193 }
194 }
195 }
196}
Provides a pluggable mechanism for arbitrary window toolkits to adapt their components to the NativeW...
static void addCustomShutdownHook(final boolean head, final Runnable runnable)
Add a custom shutdown hook to be performed at JVM shutdown before shutting down NativeWindowFactory i...
Merely a class providing a type-tag for extended configuration.
Platform agnostic StereoDevice factory.
abstract void shutdown()
Shutdown factory if valid.
static StereoDeviceFactory createFactory(final ClassLoader cl, final String implName)
abstract StereoDevice createDeviceImpl(final int deviceIndex, final StereoDeviceConfig config, final boolean verbose)
static StereoDeviceFactory createFactory(final DeviceType type)
final StereoDevice createDevice(final int deviceIndex, final StereoDeviceConfig config, final boolean verbose)
abstract boolean isValid()
Returns true, if instance is created and not shutdown() otherwise returns false.
StereoDevice type used for createFactory(type).
Default
Auto selection of device in the following order:
Interface describing a native stereoscopic device.