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.lcdui.Graphics;
028: import java.util.Hashtable;
029: import java.lang.ref.WeakReference;
030:
031: /**
032: * A class encapsulating an EGL surface.
033: */
034: final class EGLSurfaceImpl extends EGLSurface {
035:
036: static final Hashtable byId = new Hashtable();
037: private int nativeId;
038: private int width, height;
039:
040: /**
041: * A native pointer (cast to int) to the JSR239_Pixmap object
042: * backing the surface, or 0 for null.
043: */
044: private int pixmapPointer = 0;
045:
046: /**
047: * An LCDUI Graphics object referencing the surface.
048: */
049: private Graphics target;
050:
051: public EGLSurfaceImpl(int nativeId, int width, int height) {
052: synchronized (byId) {
053: this .nativeId = nativeId;
054: this .width = width;
055: this .height = height;
056: byId.put(new Integer(nativeId), new WeakReference(this ));
057: }
058: }
059:
060: private native void finalize();
061:
062: public int nativeId() {
063: return nativeId;
064: }
065:
066: public static EGLSurfaceImpl getInstance(int nativeId, int width,
067: int height) {
068: synchronized (byId) {
069: WeakReference ref = (WeakReference) byId.get(new Integer(
070: nativeId));
071: EGLSurfaceImpl surface = ref != null ? (EGLSurfaceImpl) ref
072: .get() : null;
073: if (surface == null) {
074: return new EGLSurfaceImpl(nativeId, width, height);
075: } else {
076: return surface;
077: }
078: }
079: }
080:
081: public static EGLSurfaceImpl getInstance(int nativeId) {
082: return getInstance(nativeId, -1, -1);
083: }
084:
085: public String toString() {
086: return "EGLSurfaceImpl[" + nativeId + "]";
087: }
088:
089: public void setPixmapPointer(int pixmapPointer) {
090: this .pixmapPointer = pixmapPointer;
091: }
092:
093: public int getPixmapPointer() {
094: return this .pixmapPointer;
095: }
096:
097: public void setTarget(Graphics target) {
098: this .target = target;
099: }
100:
101: public Graphics getTarget() {
102: return this .target;
103: }
104:
105: public int getWidth() {
106: return this .width;
107: }
108:
109: public int getHeight() {
110: return this .height;
111: }
112:
113: public void dispose() {
114: synchronized (byId) {
115: byId.remove(new Integer(nativeId));
116: this .nativeId = 0;
117: }
118: }
119: }
|