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 com.sun.jsr239;
026:
027: import java.nio.*;
028: import java.lang.ref.WeakReference;
029: import java.util.Enumeration;
030: import java.util.Hashtable;
031:
032: public class BufferManager {
033:
034: // All buffers in use by the native API engine are listed in
035: // this table, along with a reference count. When a reference
036: // count drops to 0, the buffer is removed from the table.
037: //
038: // Buffer -> Integer(# implementation references)
039: static Hashtable bufferStrongReferences = new Hashtable();
040:
041: // All buffers are listed in this table. If a WeakReference
042: // becomes null, free the associated native heap data.
043: //
044: // WeakReference(Buffer) -> Integer(native heap pointer)
045: static Hashtable bufferWeakReferences = new Hashtable();
046:
047: // Record the native address of this buffer along with a
048: // weak reference.
049: public static synchronized void newBuffer(Buffer b, int pointer) {
050: bufferWeakReferences.put(new WeakReference(b), new Integer(
051: pointer));
052: }
053:
054: // GL has a new pointer to the buffer
055: public static synchronized void useBuffer(Buffer b) {
056: if (b == null) {
057: return;
058: }
059:
060: Object o = bufferStrongReferences.get(b);
061: int refs = (o == null) ? 0 : ((Integer) o).intValue();
062: bufferStrongReferences.put(b, new Integer(refs + 1));
063: }
064:
065: // GL has released a pointer to the buffer
066: public static synchronized void releaseBuffer(Buffer b) {
067: if (b == null) {
068: return;
069: }
070:
071: Object o = bufferStrongReferences.get(b);
072: int refs = (o == null) ? 0 : ((Integer) o).intValue();
073: if (refs > 1) {
074: bufferStrongReferences.put(b, new Integer(refs - 1));
075: } else {
076: bufferStrongReferences.remove(b);
077: }
078: }
079:
080: public static native void _getBytes(int address, byte[] dst,
081: int offset, int length);
082:
083: // Offsets are in bytes regardless of the buffer's datatype
084: public static void getBytes(Buffer buf, int boffset, byte[] dst,
085: int offset, int length) {
086: int address = GL10Impl._getNativeAddress(buf, 0);
087: int capacity = buf.capacity();
088:
089: if (buf instanceof ByteBuffer) {
090: ByteBuffer bbi = (ByteBuffer) buf;
091: if (!bbi.isDirect()) {
092: throw new IllegalArgumentException("!isDirect");
093: }
094: } else if (buf instanceof ShortBuffer) {
095: ShortBuffer sbi = (ShortBuffer) buf;
096: if (!sbi.isDirect()) {
097: throw new IllegalArgumentException("!isDirect");
098: }
099: capacity *= 2;
100: } else if (buf instanceof IntBuffer) {
101: IntBuffer ibi = (IntBuffer) buf;
102: if (!ibi.isDirect()) {
103: throw new IllegalArgumentException("!isDirect");
104: }
105: capacity *= 4;
106: } else if (buf instanceof FloatBuffer) {
107: FloatBuffer fbi = (FloatBuffer) buf;
108: if (!fbi.isDirect()) {
109: throw new IllegalArgumentException("!isDirect");
110: }
111: capacity *= 4;
112: } else {
113: throw new IllegalArgumentException("Unknown buffer type!");
114: }
115:
116: if (boffset < 0 || boffset + length > capacity) {
117: throw new IllegalArgumentException("boffset out of bounds");
118: }
119: if (offset < 0 || offset + length > dst.length) {
120: throw new IllegalArgumentException("offset out of bounds");
121: }
122:
123: _getBytes(address + boffset, dst, offset, length);
124: }
125: }
|