11#ifndef GAMP_RENDER_GL_PIXEL_GLPIXELBUFFER_HPP_
12#define GAMP_RENDER_GL_PIXEL_GLPIXELBUFFER_HPP_
29 class GLPixelAttributes;
36 virtual bool getAllowRowStride() = 0;
46 virtual const GLPixelAttributes& getAttributes(
const GL&
gl,
int componentCount,
bool pack)
const noexcept = 0;
55 virtual PixelFormat getHostPixelComp(
const GLProfile& glp,
int componentCount)
const noexcept = 0;
81 bool pack,
int width,
int height,
int depth,
int minByteSize);
110 case GL.GL_LUMINANCE:
116 case GL2GL3.GL_UNSIGNED_SHORT_5_6_5_REV:
119 case GL.GL_UNSIGNED_SHORT_5_6_5:
122 case GL.GL_UNSIGNED_BYTE:
129 case GL2GL3.GL_UNSIGNED_SHORT_1_5_5_5_REV:
132 case GL.GL_UNSIGNED_SHORT_5_5_5_1:
135 case GL2GL3.GL_UNSIGNED_INT_8_8_8_8_REV:
137 case GL.GL_UNSIGNED_BYTE:
140 case GL2GL3.GL_UNSIGNED_INT_8_8_8_8:
146 if(
GL.GL_UNSIGNED_BYTE == glDataType ) {
152 case GL2GL3.GL_UNSIGNED_INT_8_8_8_8:
155 case GL2GL3.GL_UNSIGNED_INT_8_8_8_8_REV:
157 case GL.GL_UNSIGNED_BYTE:
176 final int dFormat, dType;
177 final boolean glesReadMode =
pack &&
gl.isGLES();
179 if( 1 == componentCount && !glesReadMode ) {
180 if(
gl.isGL3ES3() ) {
182 dFormat = GL2ES2.GL_RED;
185 dFormat =
GL.GL_ALPHA;
187 dType =
GL.GL_UNSIGNED_BYTE;
188 }
else if( 3 == componentCount && !glesReadMode ) {
190 dType =
GL.GL_UNSIGNED_BYTE;
191 }
else if( 4 == componentCount || glesReadMode ) {
193 final int _dFormat = ctx.getDefaultPixelDataFormat();
194 final int _dComps = GLBuffers.componentCount(_dFormat);
195 if( _dComps == componentCount || 4 == _dComps ) {
197 final int _dType = ctx.getDefaultPixelDataType();
199 if( null != _pixFmt) {
203 System.err.println(
"GLPixelAttributes.convert("+
gl.getGLProfile()+
", comps "+componentCount+
", pack "+
pack+
204 "): GL-impl default unsupported: "+
205 "[fmt 0x"+
Integer.toHexString(_dFormat)+
", type 0x"+
Integer.toHexString(_dType)+
"]: Using std RGBA+UBYTE");
210 dFormat =
GL.GL_RGBA;
211 dType =
GL.GL_UNSIGNED_BYTE;
227 final int[] df =
new int[1];
228 final int[] dt =
new int[1];
236 final int[] dfRes,
final int[] dtRes) {
237 final boolean glesReadMode =
pack && glp.
isGLES();
239 int dt =
GL.GL_UNSIGNED_BYTE;
242 if( !glesReadMode ) {
248 df =
GL.GL_LUMINANCE;
254 df =
GL.GL_RGB; dt = GL2GL3.GL_UNSIGNED_SHORT_5_6_5_REV;
259 df =
GL.GL_RGB; dt =
GL.GL_UNSIGNED_SHORT_5_6_5;
264 df =
GL.GL_RGBA; dt = GL2GL3.GL_UNSIGNED_SHORT_1_5_5_5_REV;
269 df =
GL.GL_RGBA; dt =
GL.GL_UNSIGNED_SHORT_5_5_5_1;
273 if( !glesReadMode ) {
288 df =
GL.GL_RGBA; dt = GL2GL3.GL_UNSIGNED_INT_8_8_8_8;
293 df =
GL.GL_BGRA; dt = GL2GL3.GL_UNSIGNED_INT_8_8_8_8;
319 int hash =
pfmt.hashCode();
320 hash = ((hash << 5) - hash) +
format;
321 return ((hash << 5) - hash) +
type;
325 public final boolean equals(
final Object obj) {
326 if(
this == obj) {
return true; }
344 this(null, null, dataFormat, dataType,
true ,
true);
356 this(glp, pixFmt, 0, 0,
pack,
true);
360 final int dataFormat,
final int dataType,
final boolean pack,
final boolean checkArgs)
throws GLException {
361 if( checkArgs && ( 0 == dataFormat || 0 == dataType ) ) {
362 if( null == pixFmt || null == glp ) {
363 throw new GLException(
"Zero format and/or type w/o pixFmt or glp: "+
this);
365 final int[] df =
new int[1];
366 final int[] dt =
new int[1];
368 throw new GLException(
"Could not find format and type for "+pixFmt+
" and "+glp+
", "+
this);
374 this.format = dataFormat;
375 this.type = dataType;
376 this.pfmt = null != pixFmt ? pixFmt :
getPixelFormat(dataFormat, dataType);
377 if( null == this.pfmt ) {
378 throw new GLException(
"Could not find PixelFormat for format and/or type: "+
this);
382 final int bytesPerPixel = GLBuffers.bytesPerPixel(this.format, this.type);
383 if( 0 == bytesPerPixel ) {
384 throw new GLException(
"Zero bytesPerPixel: "+
this);
435 private boolean disposed =
false;
439 sb =
new StringBuilder();
442 .append(
", disposed ").append(disposed).append(
", valid ").append(
isValid())
448 return "GLPixelBuffer["+
toString(null).toString()+
"]";
469 this.byteSize = Buffers.remainingBytes(
buffer);
470 this.bufferElemSize = Buffers.sizeOfBufferElem(
buffer);
548 if( 0 >= newByteSize ) {
549 final int[] tmp = { 0 };
550 newByteSize = GLBuffers.sizeof(
gl, tmp,
pixelAttributes.pfmt.comp.bytesPerPixel(), newWidth, newHeight, 1,
true);
567 bool m_allowRowStride;
575 : m_allowRowStride(allowRowStride) {}
580 public GLPixelAttributes
getAttributes(
final GL gl,
final int componentCount,
final boolean pack) {
581 final GLPixelAttributes res = GLPixelAttributes.convert(
gl, componentCount, pack);
583 throw new GLException(
"Unsupported componentCount "+componentCount+
", contact maintainer to enhance");
608 final boolean pack,
final int width,
final int height,
final int depth,
final int minByteSize) {
610 if( minByteSize > 0 ) {
613 final int[] tmp = { 0 };
614 final int byteSize = GLBuffers.sizeof(
gl, tmp, pixelAttributes.pfmt.comp.bytesPerPixel(), width, height, depth, pack);
OpenGL Rendering Context.
Specifies the OpenGL profile.
constexpr bool isGLES() const noexcept
Indicates whether this profile is capable of GLES.
constexpr bool isGL2GL3() const noexcept
Indicates whether this profile is capable of GL2GL3.
constexpr bool isGL3ES3() const noexcept
Indicates whether this profile is capable of GL3ES3.
Allows user to interface with another toolkit to define GLPixelAttributes and memory buffer to produc...
OpenGL pixel data buffer, allowing user to provide buffers via their GLPixelBufferProvider implementa...
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...
int capacity()
Returns the byte capacity of the buffer.
final int byteSize
Byte size of the buffer.
final int bufferElemSize
Buffer element size in bytes.
final int height
Height in pixels, representing buffer's byteSize.
final boolean pack
Data packing direction.
GLPixelAttributes getAttributes(final GL gl, final int componentCount, final boolean pack)
int position()
Returns the byte position of the buffer.
void dispose()
Dispose resources.
gamp::render::gl::pixel::DefaultGLPixelBufferProvider defaultProviderNoRowStride
Default GLPixelBufferProvider with GLPixelBufferProvider#getAllowRowStride() == false,...
GLPixelAttributes(final int dataFormat, final int dataType)
Create a new GLPixelAttributes instance based on GL format and type.
GLPixelBuffer(final GLPixelAttributes pixelAttributes, final boolean pack, final int width, final int height, final int depth, final Buffer buffer, final boolean allowRowStride)
final int type
The OpenGL pixel data type.
StringBuilder toString(StringBuilder sb)
bool getAllowRowStride() const noexcept override
final PixelFormat pfmt
PixelFormat describing the component layout
boolean isValid()
Is not disposed and has byteSize > 0.
std::shared_ptr< GLPixelBuffer > GLPixelBufferRef
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,...
Buffer rewind()
See Buffer#rewind().
static final GLPixelBufferProvider defaultProviderWithRowStride
Default GLPixelBufferProvider with GLPixelBufferProvider#getAllowRowStride() == true,...
static final GLPixelAttributes UNDEF
Undefined instance of GLPixelAttributes, having componentCount:=0, format:=0 and type:= 0.
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)
final int format
The OpenGL pixel data format.
static class gamp::render::gl::pixel::GLPixelBuffer::GLPixelAttributes pixelAttributes
The GLPixelAttributes.
final int width
Width in pixels, representing buffer's byteSize.
int limit()
Returns the byte limit of the buffer.
GLPixelAttributes(final GLProfile glp, final PixelFormat pixFmt, final boolean pack)
Create a new GLPixelAttributes instance based on GLProfile, PixelFormat and pack.
final int depth
Depth in pixels.
Buffer position(final int bytePos)
Sets the byte position of the buffer.
Buffer flip()
See Buffer#flip().
final Buffer buffer
Buffer holding the pixel data.
final boolean getAllowRowStride()
Allow GL2ES3#GL_PACK_ROW_LENGTH, or GL2ES2#GL_UNPACK_ROW_LENGTH.
final boolean allowRowStride
Allow GL2ES3#GL_PACK_ROW_LENGTH, or GL2ES2#GL_UNPACK_ROW_LENGTH.
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,...
PixelFormat Composition getHostPixelComp(final GLProfile glp, final int componentCount)
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.
DefaultGLPixelBufferProvider(bool allowRowStride)
final boolean equals(final Object obj)
Buffer clear()
See Buffer#clear().