28package com.jogamp.opengl.util;
30import java.nio.Buffer;
31import java.nio.ByteBuffer;
33import com.jogamp.nativewindow.util.PixelFormat;
34import com.jogamp.opengl.GL;
35import com.jogamp.opengl.GL2ES2;
36import com.jogamp.opengl.GL2ES3;
37import com.jogamp.opengl.GL2GL3;
38import com.jogamp.opengl.GLContext;
39import com.jogamp.opengl.GLException;
40import com.jogamp.opengl.GLProfile;
42import com.jogamp.common.nio.Buffers;
43import com.jogamp.opengl.util.texture.TextureData;
158 private final boolean allowRowStride;
165 this.allowRowStride = allowRowStride;
175 throw new GLException(
"Unsupported componentCount "+componentCount+
", contact maintainer to enhance");
200 final boolean pack,
final int width,
final int height,
final int depth,
final int minByteSize) {
202 if( minByteSize > 0 ) {
205 final int[] tmp = { 0 };
306 final int dFormat, dType;
307 final boolean glesReadMode =
pack && gl.
isGLES();
309 if( 1 == componentCount && !glesReadMode ) {
318 }
else if( 3 == componentCount && !glesReadMode ) {
321 }
else if( 4 == componentCount || glesReadMode ) {
325 if( _dComps == componentCount || 4 == _dComps ) {
329 if(
null != _pixFmt) {
333 System.err.println(
"GLPixelAttributes.convert("+gl.
getGLProfile()+
", comps "+componentCount+
", pack "+
pack+
334 "): GL-impl default unsupported: "+
335 "[fmt 0x"+Integer.toHexString(_dFormat)+
", type 0x"+Integer.toHexString(_dType)+
"]: Using std RGBA+UBYTE");
357 final int[] df =
new int[1];
358 final int[] dt =
new int[1];
366 final int[] dfRes,
final int[] dtRes) {
367 final boolean glesReadMode =
pack && glp.
isGLES();
372 if( !glesReadMode ) {
384 df = GL.GL_RGB; dt = GL2GL3.GL_UNSIGNED_SHORT_5_6_5_REV;
389 df = GL.GL_RGB; dt = GL.GL_UNSIGNED_SHORT_5_6_5;
394 df = GL.GL_RGBA; dt = GL2GL3.GL_UNSIGNED_SHORT_1_5_5_5_REV;
399 df = GL.GL_RGBA; dt = GL.GL_UNSIGNED_SHORT_5_5_5_1;
403 if( !glesReadMode ) {
418 df = GL.GL_RGBA; dt = GL2GL3.GL_UNSIGNED_INT_8_8_8_8;
423 df = GL.GL_BGRA; dt = GL2GL3.GL_UNSIGNED_INT_8_8_8_8;
449 int hash =
pfmt.hashCode();
450 hash = ((hash << 5) - hash) +
format;
451 return ((hash << 5) - hash) +
type;
455 public final boolean equals(
final Object obj) {
456 if(
this == obj) {
return true; }
459 return format == other.format &&
460 type == other.type &&
474 this(
null,
null, dataFormat, dataType,
true ,
true);
486 this(glp, pixFmt, 0, 0,
pack,
true);
490 final int dataFormat,
final int dataType,
final boolean pack,
final boolean checkArgs)
throws GLException {
491 if( checkArgs && ( 0 == dataFormat || 0 == dataType ) ) {
492 if(
null == pixFmt ||
null == glp ) {
493 throw new GLException(
"Zero format and/or type w/o pixFmt or glp: "+
this);
495 final int[] df =
new int[1];
496 final int[] dt =
new int[1];
498 throw new GLException(
"Could not find format and type for "+pixFmt+
" and "+glp+
", "+
this);
504 this.format = dataFormat;
505 this.type = dataType;
506 this.pfmt =
null != pixFmt ? pixFmt :
getPixelFormat(dataFormat, dataType);
507 if(
null == this.pfmt ) {
508 throw new GLException(
"Could not find PixelFormat for format and/or type: "+
this);
512 final int bytesPerPixel = GLBuffers.bytesPerPixel(this.format, this.type);
513 if( 0 == bytesPerPixel ) {
514 throw new GLException(
"Zero bytesPerPixel: "+
this);
521 return "PixelAttributes[fmt 0x"+Integer.toHexString(
format)+
", type 0x"+Integer.toHexString(
type)+
", "+
pfmt+
"]";
565 private boolean disposed =
false;
569 sb =
new StringBuilder();
572 .append(
", disposed ").append(disposed).append(
", valid ").append(
isValid())
578 return "GLPixelBuffer["+
toString(
null).toString()+
"]";
599 this.byteSize = Buffers.remainingBytes(
buffer);
600 this.bufferElemSize = Buffers.sizeOfBufferElem(
buffer);
678 if( 0 >= newByteSize ) {
679 final int[] tmp = { 0 };
Abstraction for an OpenGL rendering context.
abstract int getDefaultPixelDataFormat()
Get the default pixel data format, as required by e.g.
static final boolean DEBUG
abstract int getDefaultPixelDataType()
Get the default pixel data type, as required by e.g.
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
Specifies the the OpenGL profile.
final boolean isGL3ES3()
Indicates whether this profile is capable of GL3ES3.
final boolean isGLES()
Indicates whether this profile is capable of GLES.
final boolean isGL2GL3()
Indicates whether this profile is capable of GL2GL3.
Utility routines for dealing with direct buffers.
static final int componentCount(final int format)
Returns the number of components required for the given OpenGL format.
static final int sizeof(final GL gl, final int tmp[], final int bytesPerPixel, int width, int height, int depth, final boolean pack)
Returns the number of bytes required to read/write a memory buffer via OpenGL using the current GL pi...
GLPixelBuffer allocate(final GL gl, final PixelFormat.Composition hostPixComp, final GLPixelAttributes pixelAttributes, final boolean pack, final int width, final int height, final int depth, final int minByteSize)
Allocates a new GLPixelBuffer object.The minimum required remaining byte size equals to minByteSize,...
DefaultGLPixelBufferProvider(final boolean allowRowStride)
boolean getAllowRowStride()
Allow GL2ES3#GL_PACK_ROW_LENGTH, or GL2ES2#GL_UNPACK_ROW_LENGTH.
PixelFormat.Composition getHostPixelComp(final GLProfile glp, final int componentCount)
Returns the host PixelFormat.Composition matching GL and componentCount if required by implementation...
GLPixelAttributes getAttributes(final GL gl, final int componentCount, final boolean pack)
Returns RGB[A] GLPixelAttributes matching GL, componentCount and pack.
final boolean equals(final Object obj)
static final PixelFormat getPixelFormat(final int glFormat, final int glDataType)
Returns the matching PixelFormat for the given GL format and type if exists, otherwise returns null.
static final GLPixelAttributes convert(final GLProfile glp, final PixelFormat pixFmt, final boolean pack)
Returns the matching GLPixelAttributes for the given GLProfile, PixelFormat and pack if exists,...
final int type
The OpenGL pixel data type
final PixelFormat pfmt
PixelFormat describing the component layout
GLPixelAttributes(final GLProfile glp, final PixelFormat pixFmt, final boolean pack)
Create a new GLPixelAttributes instance based on GLProfile, PixelFormat and pack.
final int format
The OpenGL pixel data format.
GLPixelAttributes(final int dataFormat, final int dataType)
Create a new GLPixelAttributes instance based on GL format and type.
static GLPixelAttributes convert(final GL gl, final int componentCount, final boolean pack)
Returns the matching GLPixelAttributes for the given byte sized RGBA componentCount and GL if exists,...
static final GLPixelAttributes UNDEF
Undefined instance of GLPixelAttributes, having componentCount:=0, format:=0 and type:= 0.
OpenGL pixel data buffer, allowing user to provide buffers via their GLPixelBufferProvider implementa...
int position()
Returns the byte position of the buffer.
int capacity()
Returns the byte capacity of the buffer.
final int byteSize
Byte size of the buffer.
final int width
Width in pixels, representing buffer's byteSize.
int limit()
Returns the byte limit of the buffer.
Buffer flip()
See Buffer#flip().
void dispose()
Dispose resources.
static final GLPixelBufferProvider defaultProviderWithRowStride
Default GLPixelBufferProvider with GLPixelBufferProvider#getAllowRowStride() == true,...
static final GLPixelBufferProvider defaultProviderNoRowStride
Default GLPixelBufferProvider with GLPixelBufferProvider#getAllowRowStride() == false,...
final boolean allowRowStride
Allow GL2ES3#GL_PACK_ROW_LENGTH, or GL2ES2#GL_UNPACK_ROW_LENGTH.
final int depth
Depth in pixels.
final int height
Height in pixels, representing buffer's byteSize.
GLPixelBuffer(final GLPixelAttributes pixelAttributes, final boolean pack, final int width, final int height, final int depth, final Buffer buffer, final boolean allowRowStride)
StringBuilder toString(StringBuilder sb)
final int bufferElemSize
Buffer element size in bytes.
Buffer rewind()
See Buffer#rewind().
final boolean getAllowRowStride()
Allow GL2ES3#GL_PACK_ROW_LENGTH, or GL2ES2#GL_UNPACK_ROW_LENGTH.
final GLPixelAttributes pixelAttributes
The GLPixelAttributes.
Buffer clear()
See Buffer#clear().
boolean requiresNewBuffer(final GL gl, final int newWidth, final int newHeight, int newByteSize)
Returns true, if invalid or implementation requires a new buffer based on the new size due to pixel a...
boolean isValid()
Is not disposed and has byteSize > 0.
final boolean pack
Data packing direction.
Buffer position(final int bytePos)
Sets the byte position of the buffer.
final Buffer buffer
Buffer holding the pixel data.
static final int GL_RED
GL_ES_VERSION_3_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_NV_blend_equation_advanced, GL_EXT_texture_rg A...
static final int GL_UNSIGNED_SHORT_5_6_5_REV
GL_VERSION_1_2 Define "GL_UNSIGNED_SHORT_5_6_5_REV" with expression '0x8364', CType: int
static final int GL_UNSIGNED_INT_8_8_8_8
GL_VERSION_1_2, GL_EXT_packed_pixels Alias for: GL_UNSIGNED_INT_8_8_8_8_EXT Define "GL_UNSIGNED_INT_...
static final int GL_UNSIGNED_INT_8_8_8_8_REV
GL_VERSION_1_2 Define "GL_UNSIGNED_INT_8_8_8_8_REV" with expression '0x8367', CType: int
static final int GL_UNSIGNED_SHORT_1_5_5_5_REV
GL_VERSION_1_2, GL_EXT_read_format_bgra Alias for: GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT Define "GL_UNSI...
boolean isGLES()
Indicates whether this GL object conforms to one of the OpenGL ES profiles, see isGLES1(),...
boolean isGL3ES3()
Indicates whether this GL object conforms to a GL3ES3 compatible profile.
GLProfile getGLProfile()
Returns the GLProfile associated with this GL object.
GLContext getContext()
Returns the GLContext associated which this GL object.
static final int GL_BGRA
GL_VERSION_1_2, GL_IMG_read_format, GL_APPLE_texture_format_BGRA8888, GL_EXT_texture_format_BGRA8888,...
static final int GL_RGB
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_RGB" with expression ...
static final int GL_LUMINANCE
GL_ES_VERSION_2_0, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_LUMINANCE" with expression '0x1909',...
static final int GL_UNSIGNED_SHORT_5_5_5_1
GL_ES_VERSION_2_0, GL_VERSION_1_2, GL_VERSION_ES_1_0, GL_EXT_packed_pixels Alias for: GL_UNSIGNED_SHO...
static final int GL_BGR
GL_VERSION_1_2, GL_EXT_bgra Alias for: GL_BGR_EXT Define "GL_BGR" with expression '0x80E0',...
static final int GL_ALPHA
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_ALPHA" with expressio...
static final int GL_UNSIGNED_SHORT_5_6_5
GL_ES_VERSION_2_0, GL_VERSION_1_2, GL_VERSION_ES_1_0 Define "GL_UNSIGNED_SHORT_5_6_5" with expression...
static final int GL_RGBA
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_RGBA" with expression...
static final int GL_UNSIGNED_BYTE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_UNSIGNED_BYTE" with e...
Allows user to interface with another toolkit to define GLPixelAttributes and memory buffer to produc...
GLPixelBuffer allocate(GL gl, PixelFormat.Composition hostPixComp, GLPixelAttributes pixelAttributes, boolean pack, int width, int height, int depth, int minByteSize)
Allocates a new GLPixelBuffer object.
boolean getAllowRowStride()
Allow GL2ES3#GL_PACK_ROW_LENGTH, or GL2ES2#GL_UNPACK_ROW_LENGTH.
GLPixelAttributes getAttributes(GL gl, int componentCount, boolean pack)
Returns RGB[A] GLPixelAttributes matching GL, componentCount and pack.
PixelFormat.Composition getHostPixelComp(final GLProfile glp, final int componentCount)
Returns the host PixelFormat.Composition matching GL and componentCount if required by implementation...
Single GLPixelBuffer provider.
GLPixelBuffer initSingleton(GLProfile glp, int componentCount, boolean pack, int width, int height, int depth)
Initializes the single GLPixelBuffer w/ a given size, if not yet allocated.
void dispose()
Dispose all resources.
GLPixelBuffer getSingleBuffer(PixelFormat.Composition hostPixelComp, GLPixelAttributes pixelAttributes, boolean pack)
Return the last allocated GLPixelBuffer matching the given parameter.
GLPixelBuffer allocate(GL gl, PixelFormat.Composition hostPixComp, GLPixelAttributes pixelAttributes, boolean pack, int width, int height, int depth, int minByteSize)
Allocates a new GLPixelBuffer object.The minimum required remaining byte size equals to minByteSize,...