diff options
Diffstat (limited to 'src/java/com/jogamp/common/nio')
-rw-r--r-- | src/java/com/jogamp/common/nio/AbstractBuffer.java | 114 | ||||
-rw-r--r-- | src/java/com/jogamp/common/nio/AbstractLongBuffer.java | 144 | ||||
-rwxr-xr-x | src/java/com/jogamp/common/nio/Buffers.java | 707 | ||||
-rw-r--r-- | src/java/com/jogamp/common/nio/Int64Buffer.java | 83 | ||||
-rwxr-xr-x | src/java/com/jogamp/common/nio/Int64BufferME_CDC_FP.java | 89 | ||||
-rwxr-xr-x | src/java/com/jogamp/common/nio/Int64BufferSE.java | 69 | ||||
-rw-r--r-- | src/java/com/jogamp/common/nio/NativeBuffer.java | 54 | ||||
-rw-r--r-- | src/java/com/jogamp/common/nio/PointerBuffer.java | 156 | ||||
-rwxr-xr-x | src/java/com/jogamp/common/nio/PointerBufferME_CDC_FP.java | 92 | ||||
-rwxr-xr-x | src/java/com/jogamp/common/nio/PointerBufferSE.java | 83 | ||||
-rw-r--r-- | src/java/com/jogamp/common/nio/StructAccessor.java | 327 |
11 files changed, 1918 insertions, 0 deletions
diff --git a/src/java/com/jogamp/common/nio/AbstractBuffer.java b/src/java/com/jogamp/common/nio/AbstractBuffer.java new file mode 100644 index 0000000..d76574b --- /dev/null +++ b/src/java/com/jogamp/common/nio/AbstractBuffer.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2010, Sven Gothel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of Sven Gothel nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 Sven Gothel 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. + */ + +/* + * Created on Saturday, March 27 2010 11:55 + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.*; + +import java.nio.ByteBuffer; +import java.nio.Buffer; +import java.util.HashMap; + +/** + * @author Michael Bien + * @author Sven Gothel + */ +public abstract class AbstractBuffer implements NativeBuffer { + + protected final ByteBuffer bb; + protected int capacity; + protected int position; + + static { + NativeLibrary.ensureNativeLibLoaded(); + } + + protected AbstractBuffer(ByteBuffer bb, int elementSize) { + this.bb = bb; + + capacity = bb.capacity() / elementSize; + position = 0; + } + + public final int limit() { + return capacity; + } + + public final int capacity() { + return capacity; + } + + public final int position() { + return position; + } + + public final NativeBuffer position(int newPos) { + if (0 > newPos || newPos >= capacity) { + throw new IndexOutOfBoundsException("Sorry to interrupt, but the position "+newPos+" was out of bounds. " + + "My capacity is "+capacity()+"."); + } + position = newPos; + return this; + } + + public final int remaining() { + return capacity - position; + } + + public final boolean hasRemaining() { + return position < capacity; + } + + public final NativeBuffer rewind() { + position = 0; + return this; + } + + public boolean hasArray() { + return false; + } + + public int arrayOffset() { + return 0; + } + + public final ByteBuffer getBuffer() { + return bb; + } + + public final boolean isDirect() { + return bb.isDirect(); + } + + public String toString() { + return "AbstractBuffer[capacity "+capacity+", position "+position+", elementSize "+(bb.capacity()/capacity)+", ByteBuffer.capacity "+bb.capacity()+"]"; + } + +} diff --git a/src/java/com/jogamp/common/nio/AbstractLongBuffer.java b/src/java/com/jogamp/common/nio/AbstractLongBuffer.java new file mode 100644 index 0000000..ad90ffc --- /dev/null +++ b/src/java/com/jogamp/common/nio/AbstractLongBuffer.java @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2010, Michael Bien + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of Michael Bien nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 Michael Bien 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. + */ + +/* + * Created on Saturday, March 27 2010 11:55 + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.*; +import java.nio.ByteBuffer; +import java.nio.Buffer; +import java.util.HashMap; + +/** + * Hardware independent container for native pointer arrays. + * + * The native values (NIO direct ByteBuffer) might be 32bit or 64bit wide, + * depending of the CPU pointer width. + * + * @author Michael Bien + * @author Sven Gothel + */ +public abstract class AbstractLongBuffer extends AbstractBuffer { + + protected long[] backup; + + protected HashMap/*<aptr, buffer>*/ dataMap = new HashMap(); + + static { + NativeLibrary.ensureNativeLibLoaded(); + } + + protected AbstractLongBuffer(ByteBuffer bb, int elementSize) { + super(bb, elementSize); + + backup = new long[capacity]; + } + + final void updateBackup() { + for (int i = 0; i < capacity; i++) { + backup[i] = get(i); + } + } + + public final boolean hasArray() { + return true; + } + + public final long[] array() { + return backup; + } + + /** Absolute get method. Get the pointer value at the given index */ + public abstract long get(int idx); + + /** Relative get method. Get the pointer value at the current position and increment the position by one. */ + public final long get() { + long r = get(position); + position++; + return r; + } + + /** + * Relative bulk get method. Copy the pointer values <code> [ position .. position+length [</code> + * to the destination array <code> [ dest[offset] .. dest[offset+length] [ </code> + * and increment the position by <code>length</code>. */ + public final AbstractLongBuffer get(long[] dest, int offset, int length) { + if (dest.length<offset+length) { + throw new IndexOutOfBoundsException(); + } + if (remaining() < length) { + throw new IndexOutOfBoundsException(); + } + while(length>0) { + dest[offset++] = get(position++); + length--; + } + return this; + } + + /** Absolute put method. Put the pointer value at the given index */ + public abstract AbstractLongBuffer put(int index, long value); + + /** Relative put method. Put the pointer value at the current position and increment the position by one. */ + public final AbstractLongBuffer put(long value) { + put(position, value); + position++; + return this; + } + + /** + * Relative bulk put method. Put the pointer values <code> [ src[offset] .. src[offset+length] [</code> + * at the current position and increment the position by <code>length</code>. */ + public final AbstractLongBuffer put(long[] src, int offset, int length) { + if (src.length<offset+length) { + throw new IndexOutOfBoundsException(); + } + if (remaining() < length) { + throw new IndexOutOfBoundsException(); + } + while(length>0) { + put(position++, src[offset++]); + length--; + } + return this; + } + + /** + * Relative bulk get method. Copy the source values <code> src[position .. capacity] [</code> + * to this buffer and increment the position by <code>capacity-position</code>. */ + public AbstractLongBuffer put(AbstractLongBuffer src) { + if (remaining() < src.remaining()) { + throw new IndexOutOfBoundsException(); + } + while (src.hasRemaining()) { + put(src.get()); + } + return this; + } +} diff --git a/src/java/com/jogamp/common/nio/Buffers.java b/src/java/com/jogamp/common/nio/Buffers.java new file mode 100755 index 0000000..e3bea17 --- /dev/null +++ b/src/java/com/jogamp/common/nio/Buffers.java @@ -0,0 +1,707 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution 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. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.Platform; +import java.nio.*; + +/** + * @author Kenneth Russel + * @author Sven Gothel + * @author Michael Bien + */ +public class Buffers { + + public static final int SIZEOF_BYTE = 1; + public static final int SIZEOF_SHORT = 2; + public static final int SIZEOF_CHAR = 2; + public static final int SIZEOF_INT = 4; + public static final int SIZEOF_FLOAT = 4; + public static final int SIZEOF_LONG = 8; + public static final int SIZEOF_DOUBLE = 8; + + protected Buffers() {} + + /** + * Allocates a new direct ByteBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static ByteBuffer newDirectByteBuffer(int numElements) { + return nativeOrder(ByteBuffer.allocateDirect(numElements)); + } + + public static ByteBuffer newDirectByteBuffer(byte[] values, int offset, int lenght) { + return (ByteBuffer)newDirectByteBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static ByteBuffer newDirectByteBuffer(byte[] values, int offset) { + return newDirectByteBuffer(values, offset, values.length-offset); + } + + public static ByteBuffer newDirectByteBuffer(byte[] values) { + return newDirectByteBuffer(values, 0); + } + + /** + * Allocates a new direct DoubleBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static DoubleBuffer newDirectDoubleBuffer(int numElements) { + return newDirectByteBuffer(numElements * SIZEOF_DOUBLE).asDoubleBuffer(); + } + + public static DoubleBuffer newDirectDoubleBuffer(double[] values, int offset, int lenght) { + return (DoubleBuffer)newDirectDoubleBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static DoubleBuffer newDirectDoubleBuffer(double[] values, int offset) { + return newDirectDoubleBuffer(values, offset, values.length - offset); + } + + public static DoubleBuffer newDirectDoubleBuffer(double[] values) { + return newDirectDoubleBuffer(values, 0); + } + + /** + * Allocates a new direct FloatBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static FloatBuffer newDirectFloatBuffer(int numElements) { + return newDirectByteBuffer(numElements * SIZEOF_FLOAT).asFloatBuffer(); + } + + public static FloatBuffer newDirectFloatBuffer(float[] values, int offset, int lenght) { + return (FloatBuffer)newDirectFloatBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static FloatBuffer newDirectFloatBuffer(float[] values, int offset) { + return newDirectFloatBuffer(values, offset, values.length - offset); + } + + public static FloatBuffer newDirectFloatBuffer(float[] values) { + return newDirectFloatBuffer(values, 0); + } + + /** + * Allocates a new direct IntBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static IntBuffer newDirectIntBuffer(int numElements) { + return newDirectByteBuffer(numElements * SIZEOF_INT).asIntBuffer(); + } + + public static IntBuffer newDirectIntBuffer(int[] values, int offset, int lenght) { + return (IntBuffer)newDirectIntBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static IntBuffer newDirectIntBuffer(int[] values, int offset) { + return newDirectIntBuffer(values, offset, values.length - offset); + } + + public static IntBuffer newDirectIntBuffer(int[] values) { + return newDirectIntBuffer(values, 0); + } + + /** + * Allocates a new direct LongBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static LongBuffer newDirectLongBuffer(int numElements) { + return newDirectByteBuffer(numElements * SIZEOF_LONG).asLongBuffer(); + } + + public static LongBuffer newDirectLongBuffer(long[] values, int offset, int lenght) { + return (LongBuffer)newDirectLongBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static LongBuffer newDirectLongBuffer(long[] values, int offset) { + return newDirectLongBuffer(values, offset, values.length - offset); + } + + public static LongBuffer newDirectLongBuffer(long[] values) { + return newDirectLongBuffer(values, 0); + } + + /** + * Allocates a new direct ShortBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static ShortBuffer newDirectShortBuffer(int numElements) { + return newDirectByteBuffer(numElements * SIZEOF_SHORT).asShortBuffer(); + } + + public static ShortBuffer newDirectShortBuffer(short[] values, int offset, int lenght) { + return (ShortBuffer)newDirectShortBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static ShortBuffer newDirectShortBuffer(short[] values, int offset) { + return newDirectShortBuffer(values, offset, values.length - offset); + } + + public static ShortBuffer newDirectShortBuffer(short[] values) { + return newDirectShortBuffer(values, 0); + } + + /** + * Allocates a new direct CharBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static CharBuffer newDirectCharBuffer(int numElements) { + return newDirectByteBuffer(numElements * SIZEOF_SHORT).asCharBuffer(); + } + + public static CharBuffer newDirectCharBuffer(char[] values, int offset, int lenght) { + return (CharBuffer)newDirectCharBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static CharBuffer newDirectCharBuffer(char[] values, int offset) { + return newDirectCharBuffer(values, offset, values.length - offset); + } + + public static CharBuffer newDirectCharBuffer(char[] values) { + return newDirectCharBuffer(values, 0); + } + + /** + * Helper routine to set a ByteBuffer to the native byte order, if + * that operation is supported by the underlying NIO + * implementation. + */ + public static ByteBuffer nativeOrder(ByteBuffer buf) { + if (Platform.isJavaSE()) { + return buf.order(ByteOrder.nativeOrder()); + } else { + // JSR 239 does not support the ByteOrder class or the order methods. + // The initial order of a byte buffer is the platform byte order. + return buf; + } + } + + /** + * Returns the size of a single element of this buffer in bytes. + */ + public static final int sizeOfBufferElem(Buffer buffer) { + if (buffer == null) { + return 0; + } + if (buffer instanceof ByteBuffer) { + return SIZEOF_BYTE; + } else if (buffer instanceof IntBuffer) { + return SIZEOF_INT; + } else if (buffer instanceof ShortBuffer) { + return SIZEOF_SHORT; + } else if (buffer instanceof FloatBuffer) { + return SIZEOF_FLOAT; + } else if (Platform.isJavaSE()) { + if (buffer instanceof DoubleBuffer) { + return SIZEOF_DOUBLE; + } else if (buffer instanceof LongBuffer) { + return SIZEOF_LONG; + } else if (buffer instanceof CharBuffer) { + return SIZEOF_CHAR; + } + } + throw new RuntimeException("Unexpected buffer type " + buffer.getClass().getName()); + } + + /** + * Helper routine to tell whether a buffer is direct or not. Null + * pointers are considered NOT direct. isDirect() should really be + * public in Buffer and not replicated in all subclasses. + */ + public static boolean isDirect(Object buf) { + if (buf == null) { + return true; + } + if (buf instanceof ByteBuffer) { + return ((ByteBuffer) buf).isDirect(); + } else if (buf instanceof FloatBuffer) { + return ((FloatBuffer) buf).isDirect(); + } else if (buf instanceof IntBuffer) { + return ((IntBuffer) buf).isDirect(); + } else if (buf instanceof ShortBuffer) { + return ((ShortBuffer) buf).isDirect(); + } else if (buf instanceof Int64Buffer) { + return ((Int64Buffer) buf).isDirect(); + } else if (buf instanceof PointerBuffer) { + return ((PointerBuffer) buf).isDirect(); + } else if (Platform.isJavaSE()) { + if (buf instanceof DoubleBuffer) { + return ((DoubleBuffer) buf).isDirect(); + } else if (buf instanceof LongBuffer) { + return ((LongBuffer) buf).isDirect(); + }else if (buf instanceof CharBuffer) { + return ((CharBuffer) buf).isDirect(); + } + } + throw new RuntimeException("Unexpected buffer type " + buf.getClass().getName()); + } + + /** + * Helper routine to get the Buffer byte offset by taking into + * account the Buffer position and the underlying type. This is + * the total offset for Direct Buffers. + */ + public static int getDirectBufferByteOffset(Object buf) { + if (buf == null) { + return 0; + } + if (buf instanceof Buffer) { + int pos = ((Buffer) buf).position(); + if (buf instanceof ByteBuffer) { + return pos; + } else if (buf instanceof FloatBuffer) { + return pos * SIZEOF_FLOAT; + } else if (buf instanceof IntBuffer) { + return pos * SIZEOF_INT; + } else if (buf instanceof ShortBuffer) { + return pos * SIZEOF_SHORT; + }else if(Platform.isJavaSE()) { + if (buf instanceof DoubleBuffer) { + return pos * SIZEOF_DOUBLE; + } else if (buf instanceof LongBuffer) { + return pos * SIZEOF_LONG; + } else if (buf instanceof CharBuffer) { + return pos * SIZEOF_CHAR; + } + } + } else if (buf instanceof Int64Buffer) { + Int64Buffer int64Buffer = (Int64Buffer) buf; + return int64Buffer.position() * Int64Buffer.elementSize(); + } else if (buf instanceof PointerBuffer) { + PointerBuffer pointerBuffer = (PointerBuffer) buf; + return pointerBuffer.position() * PointerBuffer.elementSize(); + } + + throw new RuntimeException("Disallowed array backing store type in buffer " + buf.getClass().getName()); + } + + /** + * Helper routine to return the array backing store reference from + * a Buffer object. + */ + public static Object getArray(Object buf) { + if (buf == null) { + return null; + } + if (buf instanceof ByteBuffer) { + return ((ByteBuffer) buf).array(); + } else if (buf instanceof FloatBuffer) { + return ((FloatBuffer) buf).array(); + } else if (buf instanceof IntBuffer) { + return ((IntBuffer) buf).array(); + } else if (buf instanceof ShortBuffer) { + return ((ShortBuffer) buf).array(); + } else if (buf instanceof Int64Buffer) { + return ((Int64Buffer) buf).array(); + } else if (buf instanceof PointerBuffer) { + return ((PointerBuffer) buf).array(); + }else if(Platform.isJavaSE()) { + if (buf instanceof DoubleBuffer) { + return ((DoubleBuffer) buf).array(); + } else if (buf instanceof LongBuffer) { + return ((LongBuffer) buf).array(); + } else if (buf instanceof CharBuffer) { + return ((CharBuffer) buf).array(); + } + } + + throw new RuntimeException("Disallowed array backing store type in buffer " + buf.getClass().getName()); + } + + /** + * Helper routine to get the full byte offset from the beginning of + * the array that is the storage for the indirect Buffer + * object. The array offset also includes the position offset + * within the buffer, in addition to any array offset. + */ + public static int getIndirectBufferByteOffset(Object buf) { + if (buf == null) { + return 0; + } + if (buf instanceof Buffer) { + int pos = ((Buffer) buf).position(); + if (buf instanceof ByteBuffer) { + return (((ByteBuffer) buf).arrayOffset() + pos); + } else if (buf instanceof FloatBuffer) { + return (SIZEOF_FLOAT * (((FloatBuffer) buf).arrayOffset() + pos)); + } else if (buf instanceof IntBuffer) { + return (SIZEOF_INT * (((IntBuffer) buf).arrayOffset() + pos)); + } else if (buf instanceof ShortBuffer) { + return (SIZEOF_SHORT * (((ShortBuffer) buf).arrayOffset() + pos)); + }else if(Platform.isJavaSE()) { + if (buf instanceof DoubleBuffer) { + return (SIZEOF_DOUBLE * (((DoubleBuffer) buf).arrayOffset() + pos)); + } else if (buf instanceof LongBuffer) { + return (SIZEOF_LONG * (((LongBuffer) buf).arrayOffset() + pos)); + } else if (buf instanceof CharBuffer) { + return (SIZEOF_CHAR * (((CharBuffer) buf).arrayOffset() + pos)); + } + } + } else if (buf instanceof Int64Buffer) { + Int64Buffer int64Buffer = (Int64Buffer) buf; + return Int64Buffer.elementSize() * (int64Buffer.arrayOffset() + int64Buffer.position()); + } else if (buf instanceof PointerBuffer) { + PointerBuffer pointerBuffer = (PointerBuffer) buf; + return PointerBuffer.elementSize() * (pointerBuffer.arrayOffset() + pointerBuffer.position()); + } + + throw new RuntimeException("Unknown buffer type " + buf.getClass().getName()); + } + + + //---------------------------------------------------------------------- + // Copy routines (type-to-type) + // + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed ByteBuffer into + * a newly-allocated direct ByteBuffer. The returned buffer will + * have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static ByteBuffer copyByteBuffer(ByteBuffer orig) { + ByteBuffer dest = newDirectByteBuffer(orig.remaining()); + dest.put(orig); + dest.rewind(); + return dest; + } + + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed FloatBuffer + * into a newly-allocated direct FloatBuffer. The returned buffer + * will have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static FloatBuffer copyFloatBuffer(FloatBuffer orig) { + return copyFloatBufferAsByteBuffer(orig).asFloatBuffer(); + } + + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed IntBuffer + * into a newly-allocated direct IntBuffer. The returned buffer + * will have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static IntBuffer copyIntBuffer(IntBuffer orig) { + return copyIntBufferAsByteBuffer(orig).asIntBuffer(); + } + + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed ShortBuffer + * into a newly-allocated direct ShortBuffer. The returned buffer + * will have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static ShortBuffer copyShortBuffer(ShortBuffer orig) { + return copyShortBufferAsByteBuffer(orig).asShortBuffer(); + } + + //---------------------------------------------------------------------- + // Copy routines (type-to-ByteBuffer) + // + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed FloatBuffer + * into a newly-allocated direct ByteBuffer. The returned buffer + * will have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static ByteBuffer copyFloatBufferAsByteBuffer(FloatBuffer orig) { + ByteBuffer dest = newDirectByteBuffer(orig.remaining() * SIZEOF_FLOAT); + dest.asFloatBuffer().put(orig); + dest.rewind(); + return dest; + } + + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed IntBuffer into + * a newly-allocated direct ByteBuffer. The returned buffer will + * have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static ByteBuffer copyIntBufferAsByteBuffer(IntBuffer orig) { + ByteBuffer dest = newDirectByteBuffer(orig.remaining() * SIZEOF_INT); + dest.asIntBuffer().put(orig); + dest.rewind(); + return dest; + } + + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed ShortBuffer + * into a newly-allocated direct ByteBuffer. The returned buffer + * will have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static ByteBuffer copyShortBufferAsByteBuffer(ShortBuffer orig) { + ByteBuffer dest = newDirectByteBuffer(orig.remaining() * SIZEOF_SHORT); + dest.asShortBuffer().put(orig); + dest.rewind(); + return dest; + } + + //---------------------------------------------------------------------- + // Conversion routines + // + public final static FloatBuffer getFloatBuffer(DoubleBuffer source) { + source.rewind(); + FloatBuffer dest = newDirectFloatBuffer(source.limit()); + while (source.hasRemaining()) { + dest.put((float) source.get()); + } + return dest; + } + + //---------------------------------------------------------------------- + // Convenient put methods with generic target Buffer + // + public static Buffer put(Buffer dest, Buffer src) { + if ((dest instanceof ByteBuffer) && (src instanceof ByteBuffer)) { + return ((ByteBuffer) dest).put((ByteBuffer) src); + } else if ((dest instanceof ShortBuffer) && (src instanceof ShortBuffer)) { + return ((ShortBuffer) dest).put((ShortBuffer) src); + } else if ((dest instanceof IntBuffer) && (src instanceof IntBuffer)) { + return ((IntBuffer) dest).put((IntBuffer) src); + } else if ((dest instanceof FloatBuffer) && (src instanceof FloatBuffer)) { + return ((FloatBuffer) dest).put((FloatBuffer) src); + } else if (Platform.isJavaSE()) { + if ((dest instanceof LongBuffer) && (src instanceof LongBuffer)) { + return ((LongBuffer) dest).put((LongBuffer) src); + } else if ((dest instanceof DoubleBuffer) && (src instanceof DoubleBuffer)) { + return ((DoubleBuffer) dest).put((DoubleBuffer) src); + } else if ((dest instanceof CharBuffer) && (src instanceof CharBuffer)) { + return ((CharBuffer) dest).put((CharBuffer) src); + } + } + throw new RuntimeException("Incompatible Buffer classes: dest = " + dest.getClass().getName() + ", src = " + src.getClass().getName()); + } + + public static Buffer putb(Buffer dest, byte v) { + if (dest instanceof ByteBuffer) { + return ((ByteBuffer) dest).put(v); + } else if (dest instanceof ShortBuffer) { + return ((ShortBuffer) dest).put((short) v); + } else if (dest instanceof IntBuffer) { + return ((IntBuffer) dest).put((int) v); + } else { + throw new RuntimeException("Byte doesn't match Buffer Class: " + dest); + } + } + + public static Buffer puts(Buffer dest, short v) { + if (dest instanceof ShortBuffer) { + return ((ShortBuffer) dest).put(v); + } else if (dest instanceof IntBuffer) { + return ((IntBuffer) dest).put((int) v); + } else { + throw new RuntimeException("Short doesn't match Buffer Class: " + dest); + } + } + + public static void puti(Buffer dest, int v) { + if (dest instanceof IntBuffer) { + ((IntBuffer) dest).put(v); + } else { + throw new RuntimeException("Integer doesn't match Buffer Class: " + dest); + } + } + + public static void putf(Buffer dest, float v) { + if (dest instanceof FloatBuffer) { + ((FloatBuffer) dest).put(v); +/* TODO FixedPoint required + } else if (dest instanceof IntBuffer) { + ((IntBuffer) dest).put(FixedPoint.toFixed(v)); +*/ + } else { + throw new RuntimeException("Float doesn't match Buffer Class: " + dest); + } + } + public static void putd(Buffer dest, double v) { + if (dest instanceof FloatBuffer) { + ((FloatBuffer) dest).put((float) v); + } else { + throw new RuntimeException("Double doesn't match Buffer Class: " + dest); + } + } + + public static void rangeCheck(byte[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(char[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(short[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(int[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(long[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(float[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(double[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(Buffer buffer, int minElementsRemaining) { + if (buffer == null) { + return; + } + + if (buffer.remaining() < minElementsRemaining) { + throw new IndexOutOfBoundsException("Required " + minElementsRemaining + " remaining elements in buffer, only had " + buffer.remaining()); + } + } + + public static void rangeCheckBytes(Object buffer, int minBytesRemaining) { + if (buffer == null) { + return; + } + + int bytesRemaining = 0; + if (buffer instanceof Buffer) { + int elementsRemaining = ((Buffer) buffer).remaining(); + if (buffer instanceof ByteBuffer) { + bytesRemaining = elementsRemaining; + } else if (buffer instanceof FloatBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_FLOAT; + } else if (buffer instanceof IntBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_INT; + } else if (buffer instanceof ShortBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_SHORT; + }else if(Platform.isJavaSE()) { + if (buffer instanceof DoubleBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_DOUBLE; + } else if (buffer instanceof LongBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_LONG; + } else if (buffer instanceof CharBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_CHAR; + } + } + } else if (buffer instanceof Int64Buffer) { + Int64Buffer int64Buffer = (Int64Buffer) buffer; + bytesRemaining = int64Buffer.remaining() * Int64Buffer.elementSize(); + } else if (buffer instanceof PointerBuffer) { + PointerBuffer pointerBuffer = (PointerBuffer) buffer; + bytesRemaining = pointerBuffer.remaining() * PointerBuffer.elementSize(); + } + if (bytesRemaining < minBytesRemaining) { + throw new IndexOutOfBoundsException("Required " + minBytesRemaining + " remaining bytes in buffer, only had " + bytesRemaining); + } + } + +} diff --git a/src/java/com/jogamp/common/nio/Int64Buffer.java b/src/java/com/jogamp/common/nio/Int64Buffer.java new file mode 100644 index 0000000..6f0b7a3 --- /dev/null +++ b/src/java/com/jogamp/common/nio/Int64Buffer.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2010, Michael Bien + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of Michael Bien nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 Michael Bien 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. + */ + +package com.jogamp.common.nio; + +import com.jogamp.common.os.*; +import java.nio.ByteBuffer; + +/** + * Hardware independent container for native int64_t arrays. + * + * The native values (NIO direct ByteBuffer) are always 64bit wide. + * + * @author Michael Bien + * @author Sven Gothel + */ +public abstract class Int64Buffer extends AbstractLongBuffer { + + protected Int64Buffer(ByteBuffer bb) { + super(bb, elementSize()); + } + + public static Int64Buffer allocate(int size) { + if (Platform.isJavaSE()) { + return new Int64BufferSE(ByteBuffer.wrap(new byte[elementSize() * size])); + } else { + return new Int64BufferME_CDC_FP(ByteBuffer.wrap(new byte[elementSize() * size])); + } + } + + public static Int64Buffer allocateDirect(int size) { + if (Platform.isJavaSE()) { + return new Int64BufferSE(Buffers.newDirectByteBuffer(elementSize() * size)); + } else { + return new Int64BufferME_CDC_FP(Buffers.newDirectByteBuffer(elementSize() * size)); + } + } + + public static Int64Buffer wrap(ByteBuffer src) { + Int64Buffer res; + if (Platform.isJavaSE()) { + res = new Int64BufferSE(src); + } else { + res = new Int64BufferME_CDC_FP(src); + } + res.updateBackup(); + return res; + + } + + public static int elementSize() { + return Buffers.SIZEOF_LONG; + } + + public String toString() { + return "Int64Buffer:"+super.toString(); + } + +} diff --git a/src/java/com/jogamp/common/nio/Int64BufferME_CDC_FP.java b/src/java/com/jogamp/common/nio/Int64BufferME_CDC_FP.java new file mode 100755 index 0000000..9621677 --- /dev/null +++ b/src/java/com/jogamp/common/nio/Int64BufferME_CDC_FP.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution 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. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.Platform; +import java.nio.*; + +/** + * @author Sven Gothel + * @author Michael Bien + */ +final class Int64BufferME_CDC_FP extends Int64Buffer { + + private IntBuffer pb; + + Int64BufferME_CDC_FP(ByteBuffer bb) { + super(bb); + this.pb = bb.asIntBuffer(); + + capacity = bb.capacity() / elementSize(); + + position = 0; + backup = new long[capacity]; + } + + public final long get(int idx) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + idx = idx << 1; // 8-byte to 4-byte offset + long lo = 0x00000000FFFFFFFFL & ((long) pb.get(idx)); + long hi = 0x00000000FFFFFFFFL & ((long) pb.get(idx + 1)); + if (Platform.isLittleEndian()) { + return hi << 32 | lo; + } + return lo << 32 | hi; + } + + public final AbstractLongBuffer put(int idx, long v) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + backup[idx] = v; + idx = idx << 1; // 8-byte to 4-byte offset + int lo = (int) ((v) & 0x00000000FFFFFFFFL); + int hi = (int) ((v >> 32) & 0x00000000FFFFFFFFL); + if (Platform.isLittleEndian()) { + pb.put(idx, lo); + pb.put(idx + 1, hi); + } else { + pb.put(idx, hi); + pb.put(idx + 1, lo); + } + return this; + } +} diff --git a/src/java/com/jogamp/common/nio/Int64BufferSE.java b/src/java/com/jogamp/common/nio/Int64BufferSE.java new file mode 100755 index 0000000..fc262fd --- /dev/null +++ b/src/java/com/jogamp/common/nio/Int64BufferSE.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution 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. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + */ +package com.jogamp.common.nio; + +import java.nio.*; + +/** + * @author Sven Gothel + * @author Michael Bien + */ +final class Int64BufferSE extends Int64Buffer { + + private LongBuffer pb; + + Int64BufferSE(ByteBuffer bb) { + super(bb); + + this.pb = bb.asLongBuffer(); + } + + public final long get(int idx) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + return pb.get(idx); + } + + public final AbstractLongBuffer put(int idx, long v) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + backup[idx] = v; + pb.put(idx, v); + return this; + } +} diff --git a/src/java/com/jogamp/common/nio/NativeBuffer.java b/src/java/com/jogamp/common/nio/NativeBuffer.java new file mode 100644 index 0000000..99a5cbc --- /dev/null +++ b/src/java/com/jogamp/common/nio/NativeBuffer.java @@ -0,0 +1,54 @@ +/* + * Created on Tuesday, March 30 2010 18:22 + */ +package com.jogamp.common.nio; + +import java.nio.ByteBuffer; + +/** + * Hardware independent container for various kinds of buffers. + * + * @author Michael Bien + * @author Sven Gothel + */ +public interface NativeBuffer/*<B extends NativeBuffer>*/ { + + public int limit(); + + public int capacity(); + + public int position(); + + public NativeBuffer position(int newPos); + + public int remaining(); + + public boolean hasRemaining(); + + public NativeBuffer rewind(); + + public boolean hasArray(); + + public int arrayOffset(); + + public ByteBuffer getBuffer(); + + public boolean isDirect(); + +/* + public long[] array(); + + public B rewind(); + + public B put(int index, long value); + + public B put(long value); + + public B put(B src); + + public long get(); + + public long get(int idx); +*/ + +} diff --git a/src/java/com/jogamp/common/nio/PointerBuffer.java b/src/java/com/jogamp/common/nio/PointerBuffer.java new file mode 100644 index 0000000..26d2e7b --- /dev/null +++ b/src/java/com/jogamp/common/nio/PointerBuffer.java @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2010, Michael Bien + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of Michael Bien nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 Michael Bien 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. + */ + +/* + * Created on Saturday, March 27 2010 11:55 + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.*; +import java.nio.ByteBuffer; +import java.nio.Buffer; +import java.util.HashMap; + +/** + * Hardware independent container for native pointer arrays. + * + * The native values (NIO direct ByteBuffer) might be 32bit or 64bit wide, + * depending of the CPU pointer width. + * + * @author Michael Bien + * @author Sven Gothel + */ +public abstract class PointerBuffer extends AbstractLongBuffer { + + protected HashMap/*<aptr, buffer>*/ dataMap = new HashMap(); + + static { + NativeLibrary.ensureNativeLibLoaded(); + } + + protected PointerBuffer(ByteBuffer bb) { + super(bb, elementSize()); + } + + public static PointerBuffer allocate(int size) { + if (Platform.isJavaSE()) { + return new PointerBufferSE(ByteBuffer.wrap(new byte[elementSize() * size])); + } else { + return new PointerBufferME_CDC_FP(ByteBuffer.wrap(new byte[elementSize() * size])); + } + } + + public static PointerBuffer allocateDirect(int size) { + if (Platform.isJavaSE()) { + return new PointerBufferSE(Buffers.newDirectByteBuffer(elementSize() * size)); + } else { + return new PointerBufferME_CDC_FP(Buffers.newDirectByteBuffer(elementSize() * size)); + } + } + + public static PointerBuffer wrap(ByteBuffer src) { + PointerBuffer res; + if (Platform.isJavaSE()) { + res = new PointerBufferSE(src); + } else { + res = new PointerBufferME_CDC_FP(src); + } + res.updateBackup(); + return res; + + } + + public static int elementSize() { + return Platform.is32Bit() ? Buffers.SIZEOF_INT : Buffers.SIZEOF_LONG; + } + + public final PointerBuffer put(PointerBuffer src) { + if (remaining() < src.remaining()) { + throw new IndexOutOfBoundsException(); + } + long addr; + while (src.hasRemaining()) { + addr = src.get(); + put(addr); + Long addrL = new Long(addr); + Buffer bb = (Buffer) dataMap.get(addrL); + if(null!=bb) { + dataMap.put(addrL, bb); + } else { + dataMap.remove(addrL); + } + } + return this; + } + + /** Put the address of the given direct Buffer at the given position + of this pointer array. + Adding a reference of the given direct Buffer to this object. */ + public final PointerBuffer referenceBuffer(int index, Buffer bb) { + if(null==bb) { + throw new RuntimeException("Buffer is null"); + } + if(!bb.isDirect()) { + throw new RuntimeException("Buffer is not direct"); + } + long bbAddr = getDirectBufferAddressImpl(bb); + if(0==bbAddr) { + throw new RuntimeException("Couldn't determine native address of given Buffer: "+bb); + } + + put(index, bbAddr); + dataMap.put(new Long(bbAddr), bb); + return this; + } + + /** Put the address of the given direct Buffer at the end + of this pointer array. + Adding a reference of the given direct Buffer to this object. */ + public final PointerBuffer referenceBuffer(Buffer bb) { + referenceBuffer(position, bb); + position++; + return this; + } + + public final Buffer getReferencedBuffer(int index) { + long addr = get(index); + return (Buffer) dataMap.get(new Long(addr)); + } + + public final Buffer getReferencedBuffer() { + Buffer bb = getReferencedBuffer(position); + position++; + return bb; + } + + private native long getDirectBufferAddressImpl(Object directBuffer); + + public String toString() { + return "PointerBuffer:"+super.toString(); + } + +} diff --git a/src/java/com/jogamp/common/nio/PointerBufferME_CDC_FP.java b/src/java/com/jogamp/common/nio/PointerBufferME_CDC_FP.java new file mode 100755 index 0000000..6e2c7d9 --- /dev/null +++ b/src/java/com/jogamp/common/nio/PointerBufferME_CDC_FP.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution 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. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.Platform; +import java.nio.*; + +/** + * @author Sven Gothel + * @author Michael Bien + */ +final class PointerBufferME_CDC_FP extends PointerBuffer { + + private IntBuffer pb; + + PointerBufferME_CDC_FP(ByteBuffer bb) { + super(bb); + this.pb = bb.asIntBuffer(); + } + + public final long get(int idx) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + if (Platform.is32Bit()) { + return pb.get(idx); + } else { + idx = idx << 1; // 8-byte to 4-byte offset + long lo = 0x00000000FFFFFFFFL & ((long) pb.get(idx)); + long hi = 0x00000000FFFFFFFFL & ((long) pb.get(idx + 1)); + if (Platform.isLittleEndian()) { + return hi << 32 | lo; + } + return lo << 32 | hi; + } + } + + public final AbstractLongBuffer put(int idx, long v) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + backup[idx] = v; + if (Platform.is32Bit()) { + pb.put(idx, (int) v); + } else { + idx = idx << 1; // 8-byte to 4-byte offset + int lo = (int) ((v) & 0x00000000FFFFFFFFL); + int hi = (int) ((v >> 32) & 0x00000000FFFFFFFFL); + if (Platform.isLittleEndian()) { + pb.put(idx, lo); + pb.put(idx + 1, hi); + } else { + pb.put(idx, hi); + pb.put(idx + 1, lo); + } + } + return this; + } +} diff --git a/src/java/com/jogamp/common/nio/PointerBufferSE.java b/src/java/com/jogamp/common/nio/PointerBufferSE.java new file mode 100755 index 0000000..11dc629 --- /dev/null +++ b/src/java/com/jogamp/common/nio/PointerBufferSE.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution 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. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.Platform; +import java.nio.*; + +/** + * @author Sven Gothel + * @author Michael Bien + */ +final class PointerBufferSE extends PointerBuffer { + + private Buffer pb; + + PointerBufferSE(ByteBuffer bb) { + super(bb); + + if (Platform.is32Bit()) { + this.pb = bb.asIntBuffer(); + } else { + this.pb = bb.asLongBuffer(); + } + } + + public final long get(int idx) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + if (Platform.is32Bit()) { + return ((IntBuffer) pb).get(idx); + } else { + return ((LongBuffer) pb).get(idx); + } + } + + public final AbstractLongBuffer put(int idx, long v) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + backup[idx] = v; + if (Platform.is32Bit()) { + ((IntBuffer) pb).put(idx, (int) v); + } else { + ((LongBuffer) pb).put(idx, v); + } + return this; + } + +} diff --git a/src/java/com/jogamp/common/nio/StructAccessor.java b/src/java/com/jogamp/common/nio/StructAccessor.java new file mode 100644 index 0000000..117e8de --- /dev/null +++ b/src/java/com/jogamp/common/nio/StructAccessor.java @@ -0,0 +1,327 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution 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. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.Platform; +import java.nio.*; + +/** + * @author Kenneth Russel + * @author Michael Bien + */ +public class StructAccessor { + + private ByteBuffer bb; + private FloatBuffer fb; + private IntBuffer ib; + private ShortBuffer sb; + + //Java SE only + private CharBuffer cb; + private DoubleBuffer db; + private LongBuffer lb; + + public StructAccessor(ByteBuffer bb) { + // Setting of byte order is concession to native code which needs + // to instantiate these + if(Platform.isJavaSE()) { + this.bb = bb.order(ByteOrder.nativeOrder()); + }else{ + // JSR 239 does not support the ByteOrder class or the order methods. + // The initial order of a byte buffer is the platform byte order. + this.bb = bb; + } + } + + public ByteBuffer getBuffer() { + return bb; + } + + /** + * Returns a slice of the current ByteBuffer starting at the + * specified byte offset and extending the specified number of + * bytes. Note that this method is not thread-safe with respect to + * the other methods in this class. + */ + public ByteBuffer slice(int byteOffset, int byteLength) { + bb.position(byteOffset); + bb.limit(byteOffset + byteLength); + ByteBuffer newBuf = bb.slice(); + bb.position(0); + bb.limit(bb.capacity()); + return newBuf; + } + + /** Retrieves the byte at the specified slot (byte offset). */ + public byte getByteAt(int slot) { + return bb.get(slot); + } + + /** Puts a byte at the specified slot (byte offset). */ + public void setByteAt(int slot, byte v) { + bb.put(slot, v); + } + + /** Retrieves the char at the specified slot (2-byte offset). */ + public char getCharAt(int slot) { + return charBuffer().get(slot); + } + + /** Puts a char at the specified slot (2-byte offset). */ + public void setCharAt(int slot, char v) { + charBuffer().put(slot, v); + } + + /** Retrieves the double at the specified slot (8-byte offset). */ + public double getDoubleAt(int slot) { + return doubleBuffer().get(slot); + } + + /** Puts a double at the specified slot (8-byte offset). */ + public void setDoubleAt(int slot, double v) { + doubleBuffer().put(slot, v); + } + + /** Retrieves the float at the specified slot (4-byte offset). */ + public float getFloatAt(int slot) { + return floatBuffer().get(slot); + } + + /** Puts a float at the specified slot (4-byte offset). */ + public void setFloatAt(int slot, float v) { + floatBuffer().put(slot, v); + } + + /** Retrieves the int at the specified slot (4-byte offset). */ + public int getIntAt(int slot) { + return intBuffer().get(slot); + } + + /** Puts a int at the specified slot (4-byte offset). */ + public void setIntAt(int slot, int v) { + intBuffer().put(slot, v); + } + + /** Retrieves the short at the specified slot (2-byte offset). */ + public short getShortAt(int slot) { + return shortBuffer().get(slot); + } + + /** Puts a short at the specified slot (2-byte offset). */ + public void setShortAt(int slot, short v) { + shortBuffer().put(slot, v); + } + + public void setBytesAt(int slot, byte[] v) { + for (int i = 0; i < v.length; i++) { + bb.put(slot++, v[i]); + } + } + + public byte[] getBytesAt(int slot, byte[] v) { + for (int i = 0; i < v.length; i++) { + v[i] = bb.get(slot++); + } + return v; + } + + public void setCharsAt(int slot, char[] v) { + for (int i = 0; i < v.length; i++) { + charBuffer().put(slot++, v[i]); + } + } + + public char[] getCharsAt(int slot, char[] v) { + for (int i = 0; i < v.length; i++) { + v[i] = charBuffer().get(slot++); + } + return v; + } + + public void setIntsAt(int slot, int[] v) { + for (int i = 0; i < v.length; i++) { + intBuffer().put(slot++, v[i]); + } + } + + public int[] getIntsAt(int slot, int[] v) { + for (int i = 0; i < v.length; i++) { + v[i] = intBuffer().get(slot++); + } + return v; + } + + public void setFloatsAt(int slot, float[] v) { + for (int i = 0; i < v.length; i++) { + floatBuffer().put(slot++, v[i]); + } + } + + public float[] getFloatsAt(int slot, float[] v) { + for (int i = 0; i < v.length; i++) { + v[i] = floatBuffer().get(slot++); + } + return v; + } + + /** + * Puts a double at the specified slot (8-byte offset). + * May throw an {@link UnsupportedOperationException} + */ + public void setDoublesAt(int slot, double[] v) { + for (int i = 0; i < v.length; i++) { + doubleBuffer().put(slot++, v[i]); + } + } + + /** + * Retrieves the long at the specified slot (8-byte offset). + * May throw an {@link UnsupportedOperationException} + */ + public double[] getDoublesAt(int slot, double[] v) { + for (int i = 0; i < v.length; i++) { + v[i] = doubleBuffer().get(slot++); + } + return v; + } + + /** + * Retrieves the long at the specified slot (8-byte offset). + */ + public long getLongAt(int slot) { + if(Platform.isJavaSE()){ + return longBuffer().get(slot); + }else{ + return getLongCDCAt(slot); + } + } + + /** + * Puts a long at the specified slot (8-byte offset). + */ + public void setLongAt(int slot, long v) { + if(Platform.isJavaSE()){ + longBuffer().put(slot, v); + }else{ + setLongCDCAt(slot, v); + } + } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + private final long getLongCDCAt(int slot) { + slot = slot << 1; // 8-byte to 4-byte offset + IntBuffer intBuffer = intBuffer(); + long lo = 0x00000000FFFFFFFFL & ((long) intBuffer.get(slot)); + long hi = 0x00000000FFFFFFFFL & ((long) intBuffer.get(slot + 1)); + if (Platform.isLittleEndian()) { + return hi << 32 | lo; + } + return lo << 32 | hi; + } + + private final void setLongCDCAt(int slot, long v) { + slot = slot << 1; // 8-byte to 4-byte offset + IntBuffer intBuffer = intBuffer(); + int lo = (int) ((v) & 0x00000000FFFFFFFFL); + int hi = (int) ((v >> 32) & 0x00000000FFFFFFFFL); + if (Platform.isLittleEndian()) { + intBuffer.put(slot, lo); + intBuffer.put(slot + 1, hi); + } else { + intBuffer.put(slot, hi); + intBuffer.put(slot + 1, lo); + } + } + + private final FloatBuffer floatBuffer() { + if (fb == null) { + fb = bb.asFloatBuffer(); + } + return fb; + } + + private final IntBuffer intBuffer() { + if (ib == null) { + ib = bb.asIntBuffer(); + } + return ib; + } + + private final ShortBuffer shortBuffer() { + if (sb == null) { + sb = bb.asShortBuffer(); + } + return sb; + } + + // - - Java SE only - - + + private final LongBuffer longBuffer() { + checkSE(); + if (lb == null) { + lb = bb.asLongBuffer(); + } + return lb; + } + + private final DoubleBuffer doubleBuffer() { + checkSE(); + if (db == null) { + db = bb.asDoubleBuffer(); + } + return db; + } + + private final CharBuffer charBuffer() { + checkSE(); + if (cb == null) { + cb = bb.asCharBuffer(); + } + return cb; + } + + private static void checkSE() { + if (!Platform.isJavaSE()) { + throw new UnsupportedOperationException("I am affraid, this Operation is not supportet on this platform."); + } + } +} |