001: /*
002: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package javax.microedition.khronos.egl;
026:
027: import javax.microedition.khronos.opengles.GL;
028: import java.util.Hashtable;
029: import java.lang.ref.WeakReference;
030:
031: import com.sun.jsr239.ContextAccess;
032: import com.sun.jsr239.GLConfiguration;
033: import com.sun.jsr239.GL10Impl;
034: import com.sun.jsr239.GL11Impl;
035:
036: final class EGLContextImpl extends EGLContext implements ContextAccess {
037:
038: private static final Hashtable byId = new Hashtable();
039:
040: private GL gl = null;
041: private int nativeId;
042:
043: private Thread boundThread = null;
044: private EGLDisplayImpl display = null;
045: private EGLSurface drawSurface = null;
046: private EGLSurface readSurface = null;
047: private boolean destroyed = false;
048:
049: public EGLContextImpl(int nativeId) {
050: synchronized (byId) {
051: this .nativeId = nativeId;
052: byId.put(new Integer(nativeId), new WeakReference(this ));
053: }
054: }
055:
056: public int nativeId() {
057: return nativeId;
058: }
059:
060: public Thread getBoundThread() {
061: return boundThread;
062: }
063:
064: public void setBoundThread(Thread boundThread) {
065: this .boundThread = boundThread;
066: }
067:
068: public EGLDisplay getDisplay() {
069: return display;
070: }
071:
072: public void setDisplay(EGLDisplayImpl display) {
073: this .display = display;
074: }
075:
076: public EGLSurface getDrawSurface() {
077: return drawSurface;
078: }
079:
080: public void setDrawSurface(EGLSurfaceImpl drawSurface) {
081: this .drawSurface = drawSurface;
082: }
083:
084: public EGLSurface getReadSurface() {
085: return readSurface;
086: }
087:
088: EGLSurfaceImpl getDrawSurfaceImpl() {
089: return (EGLSurfaceImpl) getDrawSurface();
090: }
091:
092: public void setReadSurface(EGLSurfaceImpl readSurface) {
093: this .readSurface = readSurface;
094: }
095:
096: public void setDestroyed(boolean destroyed) {
097: this .destroyed = destroyed;
098: }
099:
100: public boolean isDestroyed() {
101: return destroyed;
102: }
103:
104: public static EGLContextImpl getInstance(int nativeId) {
105: synchronized (byId) {
106: WeakReference ref = (WeakReference) byId.get(new Integer(
107: nativeId));
108: EGLContextImpl context = ref != null ? (EGLContextImpl) ref
109: .get() : null;
110: if (context == null) {
111: return new EGLContextImpl(nativeId);
112: } else {
113: return context;
114: }
115: }
116: }
117:
118: public GL getGL() {
119: synchronized (this ) {
120: if (gl == null) {
121: if (!GLConfiguration.supportsGL11) {
122: gl = new GL10Impl(this );
123: } else {
124: gl = new GL11Impl(this );
125: }
126: }
127: return gl;
128: }
129: }
130:
131: /**
132: * For debugging purposes, prints the native context ID.
133: */
134: public String toString() {
135: return "EGLContextImpl[" + nativeId + "]";
136: }
137:
138: public void dispose() {
139: synchronized (byId) {
140: byId.remove(new Integer(nativeId));
141: this .nativeId = 0;
142: }
143: }
144: }
|