com.jogamp.newt.Display Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jogl-all Show documentation
Show all versions of jogl-all Show documentation
Java™ Binding for the OpenGL® API
/**
* Copyright 2010 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``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 JogAmp Community 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.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/
package com.jogamp.newt;
import com.jogamp.newt.util.EDTUtil;
import jogamp.newt.Debug;
import java.lang.ref.WeakReference;
import java.util.*;
import javax.media.nativewindow.AbstractGraphicsDevice;
import javax.media.nativewindow.NativeWindowException;
public abstract class Display {
public static final boolean DEBUG = Debug.debug("Display");
/** return precomputed hashCode from FQN {@link #getFQName()} */
public abstract int hashCode();
/** return true if obj is of type Display and both FQN {@link #getFQName()} equals */
public boolean equals(Object obj) {
if (this == obj) { return true; }
if (obj instanceof Display) {
Display d = (Display)obj;
return d.getFQName().equals(getFQName());
}
return false;
}
/**
* Manual trigger the native creation, if it is not done yet.
* This is useful to be able to request the {@link javax.media.nativewindow.AbstractGraphicsDevice}, via
* {@link #getGraphicsDevice()}.
* Otherwise the abstract device won't be available before the dependent components (Screen and Window) are realized.
*
* This method is usually invoke by {@link #addReference()}
*
* @throws NativeWindowException if the native creation failed.
*/
public abstract void createNative() throws NativeWindowException;
/**
* Manually trigger the destruction, incl. native destruction.
*
* This method is usually invoke by {@link #removeReference()}
*
*/
public abstract void destroy();
/**
* Validate EDT running state.
* Stop the running EDT in case this display is destroyed already.
* @return true if EDT has been stopped (destroyed but running), otherwise false.
*/
public abstract boolean validateEDT();
/**
* @return true if the native display handle is valid and ready to operate,
* otherwise false.
*
* @see #destroy()
*/
public abstract boolean isNativeValid();
/**
* @return number of references
*/
public abstract int getReferenceCount();
/**
* The 1st call will initiate native creation,
* since we follow the lazy creation pattern.
*
* @return number of references post operation
* @throws NativeWindowException if the native creation failed.
* @see #removeReference()
*/
public abstract int addReference() throws NativeWindowException ;
/**
* The last call may destroy this instance,
* if {@link #getDestroyWhenUnused()} returns true
.
*
* @return number of references post operation
* @see #addReference()
* @see #getDestroyWhenUnused()
* @see #setDestroyWhenUnused(boolean)
*/
public abstract int removeReference();
/**
* Return the {@link AbstractGraphicsDevice} used for depending resources lifecycle,
* i.e. {@link Screen} and {@link Window}, as well as the event dispatching (EDT). */
public abstract AbstractGraphicsDevice getGraphicsDevice();
/**
* Return the handle of the {@link AbstractGraphicsDevice} as returned by {@link #getGraphicsDevice()}.
*/
public abstract long getHandle();
/**
* @return The fully qualified Display name,
* which is a key of {@link #getType()} + {@link #getName()} + {@link #getId()}.
*/
public abstract String getFQName();
/**
* @return this display internal serial id
*/
public abstract int getId();
/**
* @return This display connection name as defined at creation time.
* The display connection name is a technical platform specific detail, see {@link AbstractGraphicsDevice#getConnection()}.
*
* @see AbstractGraphicsDevice#getConnection()
*/
public abstract String getName();
/**
* @return the native display type, ie {@link javax.media.nativewindow.NativeWindowFactory#getNativeWindowType(boolean)}
*/
public abstract String getType();
/** Return true if this instance is exclusive, i.e. will not be shared. */
public abstract boolean isExclusive();
/**
* Sets a new {@link EDTUtil} and returns the previous one.
*
* If usrEDTUtil
is null
,
* the device's default EDTUtil is created and used.
*
*
* If a previous one exists and it differs from usrEDTUtil
,
* it's being stopped, wait-until-idle.
*
*
* If usrEDTUtil
is not null and equals the previous one,
* no change is being made.
*
*/
public abstract EDTUtil setEDTUtil(EDTUtil usrEDTUtil);
public abstract EDTUtil getEDTUtil();
/**
* @return true if EDT is running and not subject to be stopped, otherwise false.
*/
public abstract boolean isEDTRunning();
public abstract void dispatchMessages();
// Global Displays
protected static final ArrayList> displayList = new ArrayList>();
protected static int displaysActive = 0;
public static void dumpDisplayList(String prefix) {
synchronized(displayList) {
System.err.println(prefix+" DisplayList[] entries: "+displayList.size()+" - "+getThreadName());
final Iterator> ri = displayList.iterator();
for(int j=0; ri.hasNext(); j++) {
final Display d = ri.next().get();
System.err.println(" ["+j+"] : "+d+", GC'ed "+(null==d));
}
}
}
/**
*
* @param type
* @param name
* @param fromIndex start index, then increasing until found or end of list
* @paran shared if true, only shared instances are found, otherwise also exclusive
* @return
*/
public static Display getFirstDisplayOf(String type, String name, int fromIndex, boolean shared) {
return getDisplayOfImpl(type, name, fromIndex, 1, shared);
}
/**
*
* @param type
* @param name
* @param fromIndex start index, then decreasing until found or end of list. -1 is interpreted as size - 1.
* @paran shared if true, only shared instances are found, otherwise also exclusive
* @return
*/
public static Display getLastDisplayOf(String type, String name, int fromIndex, boolean shared) {
return getDisplayOfImpl(type, name, fromIndex, -1, shared);
}
private static Display getDisplayOfImpl(String type, String name, final int fromIndex, final int incr, boolean shared) {
synchronized(displayList) {
int i = fromIndex >= 0 ? fromIndex : displayList.size() - 1 ;
while( ( incr > 0 ) ? i < displayList.size() : i >= 0 ) {
final Display display = (Display) displayList.get(i).get();
if( null == display ) {
// Clear GC'ed dead reference entry!
displayList.remove(i);
if( incr < 0 ) {
// decrease
i+=incr;
} // else nop - remove shifted subsequent elements to the left
} else {
if( display.getType().equals(type) &&
display.getName().equals(name) &&
( !shared || shared && !display.isExclusive() )
) {
return display;
}
i+=incr;
}
}
}
return null;
}
protected static void addDisplay2List(Display display) {
synchronized(displayList) {
// GC before add
int i=0;
while( i < displayList.size() ) {
if( null == displayList.get(i).get() ) {
displayList.remove(i);
} else {
i++;
}
}
displayList.add(new WeakReference(display));
}
}
/** Returns the global display collection */
public static Collection getAllDisplays() {
ArrayList list;
synchronized(displayList) {
list = new ArrayList();
int i = 0;
while( i < displayList.size() ) {
final Display d = displayList.get(i).get();
if( null == d ) {
displayList.remove(i);
} else {
list.add( displayList.get(i).get() );
i++;
}
}
}
return list;
}
public static int getActiveDisplayNumber() {
synchronized(displayList) {
return displaysActive;
}
}
public static String getThreadName() {
return Thread.currentThread().getName();
}
public static String toHexString(int hex) {
return "0x" + Integer.toHexString(hex);
}
public static String toHexString(long hex) {
return "0x" + Long.toHexString(hex);
}
public static int hashCodeNullSafe(Object o) {
return ( null != o ) ? o.hashCode() : 0;
}
}