From 130ae2cfe1e65cc262313aab0b35794fc95e1ebd Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 9 Apr 2012 07:38:30 +0200 Subject: Buffers: Add generic slice2Float(..) method from JOGL's ProjectFloat/FloatUtil --- src/java/com/jogamp/common/nio/Buffers.java | 50 ++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'src/java/com/jogamp/common/nio/Buffers.java') diff --git a/src/java/com/jogamp/common/nio/Buffers.java b/src/java/com/jogamp/common/nio/Buffers.java index 5aace6e..1558887 100755 --- a/src/java/com/jogamp/common/nio/Buffers.java +++ b/src/java/com/jogamp/common/nio/Buffers.java @@ -253,6 +253,48 @@ public class Buffers { return slice; } + /** + * Slices a ByteBuffer or a primitive float array to a FloatBuffer at the given position with the given size + * in float-space. + *

+ * Using a ByteBuffer as the source guarantees + * keeping the source native order programmatically. + * This works around Honeycomb / Android 3.0 Issue 16434. + * This bug is resolved at least in Android 3.2. + *

+ * + * @param buf source Buffer, maybe ByteBuffer (recommended) or FloatBuffer or null. + * Buffer's position is ignored and floatPos is being used. + * @param backing source float array or null + * @param floatPos {@link Buffers#SIZEOF_FLOAT} position + * @param floatSize {@link Buffers#SIZEOF_FLOAT} size + * @return FloatBuffer w/ native byte order as given ByteBuffer + */ + public static final FloatBuffer slice2Float(Buffer buf, float[] backing, int floatPos, int floatSize) { + if(buf instanceof ByteBuffer) { + ByteBuffer bb = (ByteBuffer) buf; + bb.position( floatPos * Buffers.SIZEOF_FLOAT ); + bb.limit( (floatPos + floatSize) * Buffers.SIZEOF_FLOAT ); + FloatBuffer fb = bb.slice().order(bb.order()).asFloatBuffer(); // slice and duplicate may change byte order + fb.mark(); + return fb; + } else if(null != backing) { + FloatBuffer fb = FloatBuffer.wrap(backing, floatPos, floatSize); + fb.mark(); + return fb; + } else if(buf instanceof FloatBuffer) { + FloatBuffer fb = (FloatBuffer) buf; + fb.position( floatPos ); + fb.limit( floatPos + floatSize ); + FloatBuffer fb0 = fb.slice(); // slice and duplicate may change byte order + fb0.mark(); + return fb0; + } else { + throw new InternalError("XXX"); + } + } + + /** * Helper routine to set a ByteBuffer to the native byte order, if * that operation is supported by the underlying NIO @@ -330,7 +372,7 @@ public class Buffers { return pos * SIZEOF_CHAR; } } else if (buf instanceof NativeBuffer) { - final NativeBuffer nb = (NativeBuffer) buf; + final NativeBuffer nb = (NativeBuffer) buf; return nb.position() * nb.elementSize() ; } @@ -350,7 +392,7 @@ public class Buffers { if (buf instanceof Buffer) { return ((Buffer) buf).array(); } else if (buf instanceof NativeBuffer) { - return ((NativeBuffer) buf).array(); + return ((NativeBuffer) buf).array(); } throw new IllegalArgumentException("Disallowed array backing store type in buffer " + buf.getClass().getName()); @@ -384,7 +426,7 @@ public class Buffers { return (SIZEOF_CHAR * (((CharBuffer) buf).arrayOffset() + pos)); } } else if (buf instanceof NativeBuffer) { - final NativeBuffer nb = (NativeBuffer) buf; + final NativeBuffer nb = (NativeBuffer) buf; return nb.elementSize() * ( nb.arrayOffset() + nb.position() ); } @@ -804,7 +846,7 @@ public class Buffers { bytesRemaining = elementsRemaining * SIZEOF_CHAR; } } else if (buffer instanceof NativeBuffer) { - final NativeBuffer nb = (NativeBuffer) buffer; + final NativeBuffer nb = (NativeBuffer) buffer; bytesRemaining = nb.remaining() * nb.elementSize(); } if (bytesRemaining < minBytesRemaining) { -- cgit v1.2.3