001: /*
002: * $RCSfile: J3DBuffer.java,v $
003: *
004: * Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.7 $
028: * $Date: 2008/02/28 20:17:25 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import com.sun.j3d.internal.ByteBufferWrapper;
035: import com.sun.j3d.internal.BufferWrapper;
036: import com.sun.j3d.internal.FloatBufferWrapper;
037: import com.sun.j3d.internal.DoubleBufferWrapper;
038: import com.sun.j3d.internal.ByteOrderWrapper;
039:
040: /**
041: * Java 3D wrapper class for java.nio.Buffer objects.
042: * When used to wrap a non-null NIO buffer object, this class will
043: * create a read-only view of the wrapped NIO buffer, and will call
044: * <code>rewind</code> on the read-only view, so that elements 0
045: * through <code>buffer.limit()-1</code> will be available internally.
046: *
047: * @see GeometryArray#setCoordRefBuffer(J3DBuffer)
048: * @see GeometryArray#setColorRefBuffer(J3DBuffer)
049: * @see GeometryArray#setNormalRefBuffer(J3DBuffer)
050: * @see GeometryArray#setTexCoordRefBuffer(int,J3DBuffer)
051: * @see GeometryArray#setVertexAttrRefBuffer(int,J3DBuffer)
052: * @see GeometryArray#setInterleavedVertexBuffer(J3DBuffer)
053: * @see CompressedGeometry#CompressedGeometry(CompressedGeometryHeader,J3DBuffer)
054: *
055: * @since Java 3D 1.3
056: */
057:
058: public class J3DBuffer {
059: static final int TYPE_NULL = 0;
060: static final int TYPE_UNKNOWN = 1;
061: static final int TYPE_BYTE = 2;
062: static final int TYPE_CHAR = 3;
063: static final int TYPE_SHORT = 4;
064: static final int TYPE_INT = 5;
065: static final int TYPE_LONG = 6;
066: static final int TYPE_FLOAT = 7;
067: static final int TYPE_DOUBLE = 8;
068:
069: static boolean unsupportedOperation = false;
070:
071: private java.nio.Buffer originalBuffer = null;
072: private BufferWrapper bufferImpl = null;
073: private int bufferType = TYPE_NULL;
074:
075: /**
076: * Constructs a J3DBuffer object and initializes it with
077: * a null NIO buffer object. The NIO buffer object
078: * must be set to a non-null value before using this J3DBuffer
079: * object in a Java 3D node component.
080: *
081: * @exception UnsupportedOperationException if the JVM does not
082: * support native access to direct NIO buffers
083: */
084: public J3DBuffer() {
085: this (null);
086: }
087:
088: /**
089: * Constructs a J3DBuffer object and initializes it with
090: * the specified NIO buffer object.
091: *
092: * @param buffer the NIO buffer wrapped by this J3DBuffer
093: *
094: * @exception UnsupportedOperationException if the JVM does not
095: * support native access to direct NIO buffers
096: *
097: * @exception IllegalArgumentException if the specified buffer is
098: * not a direct buffer, or if the byte order of the specified
099: * buffer does not match the native byte order of the underlying
100: * platform.
101: */
102: public J3DBuffer(java.nio.Buffer buffer) {
103: if (unsupportedOperation)
104: throw new UnsupportedOperationException(J3dI18N
105: .getString("J3DBuffer0"));
106:
107: setBuffer(buffer);
108: }
109:
110: /**
111: * Sets the NIO buffer object in this J3DBuffer to
112: * the specified object.
113: *
114: * @param buffer the NIO buffer wrapped by this J3DBuffer
115: *
116: * @exception IllegalArgumentException if the specified buffer is
117: * not a direct buffer, or if the byte order of the specified
118: * buffer does not match the native byte order of the underlying
119: * platform.
120: */
121: public void setBuffer(java.nio.Buffer buffer) {
122: int bType = TYPE_NULL;
123: boolean direct = false;
124: java.nio.ByteOrder order = java.nio.ByteOrder.BIG_ENDIAN;
125:
126: if (buffer == null) {
127: bType = TYPE_NULL;
128: } else if (buffer instanceof java.nio.ByteBuffer) {
129: bType = TYPE_BYTE;
130: direct = ((java.nio.ByteBuffer) buffer).isDirect();
131: order = ((java.nio.ByteBuffer) buffer).order();
132: } else if (buffer instanceof java.nio.CharBuffer) {
133: bType = TYPE_CHAR;
134: direct = ((java.nio.CharBuffer) buffer).isDirect();
135: order = ((java.nio.CharBuffer) buffer).order();
136: } else if (buffer instanceof java.nio.ShortBuffer) {
137: bType = TYPE_SHORT;
138: direct = ((java.nio.ShortBuffer) buffer).isDirect();
139: order = ((java.nio.ShortBuffer) buffer).order();
140: } else if (buffer instanceof java.nio.IntBuffer) {
141: bType = TYPE_INT;
142: direct = ((java.nio.IntBuffer) buffer).isDirect();
143: order = ((java.nio.IntBuffer) buffer).order();
144: } else if (buffer instanceof java.nio.LongBuffer) {
145: bType = TYPE_LONG;
146: direct = ((java.nio.LongBuffer) buffer).isDirect();
147: order = ((java.nio.LongBuffer) buffer).order();
148: } else if (buffer instanceof java.nio.FloatBuffer) {
149: bType = TYPE_FLOAT;
150: direct = ((java.nio.FloatBuffer) buffer).isDirect();
151: order = ((java.nio.FloatBuffer) buffer).order();
152: } else if (buffer instanceof java.nio.DoubleBuffer) {
153: bType = TYPE_DOUBLE;
154: direct = ((java.nio.DoubleBuffer) buffer).isDirect();
155: order = ((java.nio.DoubleBuffer) buffer).order();
156: } else {
157: bType = TYPE_UNKNOWN;
158: }
159:
160: // Verify that the buffer is direct and has the correct byte order
161: if (buffer != null) {
162: if (!direct) {
163: throw new IllegalArgumentException(J3dI18N
164: .getString("J3DBuffer1"));
165: }
166:
167: if (order != java.nio.ByteOrder.nativeOrder()) {
168: throw new IllegalArgumentException(J3dI18N
169: .getString("J3DBuffer2"));
170: }
171: }
172:
173: bufferType = bType;
174: originalBuffer = buffer;
175:
176: // Make a read-only view of the buffer if the type is one
177: // of the internally supported types: byte, float, or double
178: switch (bufferType) {
179: case TYPE_BYTE:
180: java.nio.ByteBuffer byteBuffer = ((java.nio.ByteBuffer) buffer)
181: .asReadOnlyBuffer();
182: byteBuffer.rewind();
183: bufferImpl = new ByteBufferWrapper(byteBuffer);
184: break;
185: case TYPE_FLOAT:
186: java.nio.FloatBuffer floatBuffer = ((java.nio.FloatBuffer) buffer)
187: .asReadOnlyBuffer();
188: floatBuffer.rewind();
189: bufferImpl = new FloatBufferWrapper(floatBuffer);
190: break;
191: case TYPE_DOUBLE:
192: java.nio.DoubleBuffer doubleBuffer = ((java.nio.DoubleBuffer) buffer)
193: .asReadOnlyBuffer();
194: doubleBuffer.rewind();
195: bufferImpl = new DoubleBufferWrapper(doubleBuffer);
196: break;
197: default:
198: bufferImpl = null;
199: }
200: }
201:
202: /**
203: * Retrieves the NIO buffer object from this J3DBuffer.
204: *
205: * @return the current NIO buffer wrapped by this J3DBuffer
206: */
207: public java.nio.Buffer getBuffer() {
208: return originalBuffer;
209: }
210:
211: int getBufferType() {
212: return bufferType;
213: }
214:
215: BufferWrapper getBufferImpl() {
216: return bufferImpl;
217: }
218:
219: private static boolean checkNativeBufferAccess(
220: java.nio.Buffer buffer) {
221: if (buffer == null /*|| !Pipeline.getPipeline().checkNativeBufferAccess(buffer)*/) {
222: return false;
223: } else {
224: return true;
225: }
226: }
227:
228: static {
229: // Allocate a direct byte buffer and verify that we can
230: // access the data pointer from native code
231: java.nio.ByteBuffer buffer = java.nio.ByteBuffer
232: .allocateDirect(8);
233:
234: if (!checkNativeBufferAccess(buffer)) {
235: unsupportedOperation = true;
236: }
237: }
238: }
|