1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
/**
* Copyright 2013 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``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 JogAmp Community OR
* CONTRIBUTORS 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.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/
package com.jogamp.opengl.util;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GL2ES2;
import javax.media.opengl.GL2ES3;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLException;
import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.util.texture.TextureData;
/**
* OpenGL pixel data buffer, allowing user to provide buffers via their {@link GLPixelBufferProvider} implementation.
* <p>
* {@link GLPixelBufferProvider} produces a {@link GLPixelBuffer}.
* </p>
* <p>
* You may use {@link #defaultProviderNoRowStride}.
* </p>
*/
public class GLPixelBuffer {
/** Allows user to interface with another toolkit to define {@link GLPixelAttributes} and memory buffer to produce {@link TextureData}. */
public static interface GLPixelBufferProvider {
/** Allow {@link GL2ES3#GL_PACK_ROW_LENGTH}, or {@link GL2ES2#GL_UNPACK_ROW_LENGTH}. */
boolean getAllowRowStride();
/** Called first to determine {@link GLPixelAttributes}. */
GLPixelAttributes getAttributes(GL gl, int componentCount);
/**
* Allocates a new {@link GLPixelBuffer} object.
* <p>
* Being called to gather the initial {@link GLPixelBuffer},
* or a new replacement {@link GLPixelBuffer} if {@link GLPixelBuffer#requiresNewBuffer(GL, int, int, int)}.
* </p>
* <p>
* The minimum required {@link Buffer#remaining() remaining} byte size equals to <code>minByteSize</code>, if > 0,
* otherwise utilize {@link GLBuffers#sizeof(GL, int[], int, int, int, int, int, boolean)}
* to calculate it.
* </p>
*
* @param gl the corresponding current GL context object
* @param pixelAttributes the desired {@link GLPixelAttributes}
* @param width in pixels
* @param height in pixels
* @param depth in pixels
* @param pack true for read mode GPU -> CPU, otherwise false for write mode CPU -> GPU
* @param minByteSize if > 0, the pre-calculated minimum byte-size for the resulting buffer, otherwise ignore.
*/
GLPixelBuffer allocate(GL gl, GLPixelAttributes pixelAttributes, int width, int height, int depth, boolean pack, int minByteSize);
}
/** Single {@link GLPixelBuffer} provider. */
public static interface SingletonGLPixelBufferProvider extends GLPixelBufferProvider {
/** Return the last {@link #allocate(GL, GLPixelAttributes, int, int, int, boolean, int) allocated} {@link GLPixelBuffer} w/ {@link GLPixelAttributes#componentCount}. */
GLPixelBuffer getSingleBuffer(GLPixelAttributes pixelAttributes);
/**
* Initializes the single {@link GLPixelBuffer} w/ a given size, if not yet {@link #allocate(GL, GLPixelAttributes, int, int, int, boolean, int) allocated}.
* @return the newly initialized single {@link GLPixelBuffer}, or null if already allocated.
*/
GLPixelBuffer initSingleton(int componentCount, int width, int height, int depth, boolean pack);
}
public static class DefaultGLPixelBufferProvider implements GLPixelBufferProvider {
private final boolean allowRowStride;
/**
* @param allowRowStride If <code>true</code>, allow row-stride, otherwise not.
* See {@link #getAllowRowStride()} and {@link GLPixelBuffer#requiresNewBuffer(GL, int, int, int)}.
*/
public DefaultGLPixelBufferProvider(boolean allowRowStride) {
this.allowRowStride = allowRowStride;
}
@Override
public boolean getAllowRowStride() { return allowRowStride; }
@Override
public GLPixelAttributes getAttributes(GL gl, int componentCount) {
final GLContext ctx = gl.getContext();
final int dFormat, dType;
if( 1 == componentCount ) {
if( gl.isGL3ES3() ) {
// RED is supported on ES3 and >= GL3 [core]; ALPHA is deprecated on core
dFormat = GL2ES2.GL_RED;
} else {
// ALPHA is supported on ES2 and GL2, i.e. <= GL3 [core] or compatibility
dFormat = GL2ES2.GL_ALPHA;
}
dType = GL.GL_UNSIGNED_BYTE;
} else if( 3 == componentCount ) {
dFormat = GL.GL_RGB;
dType = GL.GL_UNSIGNED_BYTE;
} else if( 4 == componentCount ) {
int _dFormat = ctx.getDefaultPixelDataFormat();
final int dComps = GLBuffers.componentCount(_dFormat);
if( dComps == componentCount ) {
dFormat = _dFormat;
dType = ctx.getDefaultPixelDataType();
} else {
dFormat = GL.GL_RGBA;
dType = GL.GL_UNSIGNED_BYTE;
}
} else {
throw new GLException("Unsupported componentCount "+componentCount+", contact maintainer to enhance");
}
return new GLPixelAttributes(componentCount, dFormat, dType);
}
/**
* {@inheritDoc}
* <p>
* Returns an NIO {@link ByteBuffer}.
* </p>
*/
@Override
public GLPixelBuffer allocate(GL gl, GLPixelAttributes pixelAttributes, int width, int height, int depth, boolean pack, int minByteSize) {
if( minByteSize > 0 ) {
return new GLPixelBuffer(pixelAttributes, width, height, depth, pack, Buffers.newDirectByteBuffer(minByteSize), getAllowRowStride());
} else {
int[] tmp = { 0 };
final int byteSize = GLBuffers.sizeof(gl, tmp, pixelAttributes.bytesPerPixel, width, height, depth, pack);
return new GLPixelBuffer(pixelAttributes, width, height, depth, pack, Buffers.newDirectByteBuffer(byteSize), getAllowRowStride());
}
}
}
/**
* Default {@link GLPixelBufferProvider} with {@link GLPixelBufferProvider#getAllowRowStride()} == <code>false</code>,
* utilizing best match for {@link GLPixelAttributes}
* and {@link GLPixelBufferProvider#allocate(GL, GLPixelAttributes, int, int, int, boolean, int) allocating} a {@link ByteBuffer}.
*/
public static GLPixelBufferProvider defaultProviderNoRowStride = new DefaultGLPixelBufferProvider(false);
/**
* Default {@link GLPixelBufferProvider} with {@link GLPixelBufferProvider#getAllowRowStride()} == <code>true</code>,
* utilizing best match for {@link GLPixelAttributes}
* and {@link GLPixelBufferProvider#allocate(GL, GLPixelAttributes, int, int, int, boolean, int) allocating} a {@link ByteBuffer}.
*/
public static GLPixelBufferProvider defaultProviderWithRowStride = new DefaultGLPixelBufferProvider(true);
/** Pixel attributes. */
public static class GLPixelAttributes {
/** Undefined instance of {@link GLPixelAttributes}, having componentCount:=0, format:=0 and type:= 0. */
public static final GLPixelAttributes UNDEF = new GLPixelAttributes(0, 0, 0, false);
/** Pixel <i>source</i> component count, i.e. number of meaningful components. */
public final int componentCount;
/** The OpenGL pixel data format */
public final int format;
/** The OpenGL pixel data type */
public final int type;
/** The OpenGL pixel size in bytes */
public final int bytesPerPixel;
/**
* Deriving {@link #componentCount} via GL <code>dataFormat</code>, i.e. {@link GLBuffers#componentCount(int)} if > 0.
* @param dataFormat GL data format
* @param dataType GL data type
*/
public GLPixelAttributes(int dataFormat, int dataType) {
this(0 < dataFormat ? GLBuffers.componentCount(dataFormat) : 0, dataFormat, dataType);
}
/**
* Using user specified source {@link #componentCount}.
* @param componentCount source component count
* @param dataFormat GL data format
* @param dataType GL data type
*/
public GLPixelAttributes(int componentCount, int dataFormat, int dataType) {
this(componentCount, dataFormat, dataType, true);
}
private GLPixelAttributes(int componentCount, int dataFormat, int dataType, boolean checkArgs) {
this.componentCount = componentCount;
this.format = dataFormat;
this.type = dataType;
this.bytesPerPixel = ( 0 < dataFormat && 0 < dataType ) ? GLBuffers.bytesPerPixel(dataFormat, dataType) : 0;
if( checkArgs ) {
if( 0 == componentCount || 0 == format || 0 == type ) {
throw new GLException("Zero components, format and/or type: "+this);
}
if( 0 == bytesPerPixel ) {
throw new GLException("Zero bytesPerPixel: "+this);
}
}
}
public String toString() {
return "PixelAttributes[comp "+componentCount+", fmt 0x"+Integer.toHexString(format)+", type 0x"+Integer.toHexString(type)+", bytesPerPixel "+bytesPerPixel+"]";
}
}
/** The {@link GLPixelAttributes}. */
public final GLPixelAttributes pixelAttributes;
/** Width in pixels. */
public final int width;
/** Height in pixels. */
public final int height;
/** Depth in pixels. */
public final int depth;
/** Data packing direction. If <code>true</code> for read mode GPU -> CPU, <code>false</code> for write mode CPU -> GPU. */
public final boolean pack;
/** Byte size of the buffer. Actually the number of {@link Buffer#remaining()} bytes when passed in ctor. */
public final int byteSize;
/**
* Buffer holding the pixel data. If {@link #rewind()}, it holds <code>byteSize</code> {@link Buffer#remaining()} bytes.
* <p>
* By default the {@link Buffer} is a {@link ByteBuffer}, due to {@link DefProvider#allocate(GL, GLPixelAttributes, int, int, int, boolean, int)}.
* However, other {@link GLPixelBufferProvider} may utilize different {@link Buffer} types.
* </p>
*/
public final Buffer buffer;
/** Buffer element size in bytes. */
public final int bufferElemSize;
/** Allow {@link GL2ES3#GL_PACK_ROW_LENGTH}, or {@link GL2ES2#GL_UNPACK_ROW_LENGTH}. See {@link #requiresNewBuffer(GL, int, int, int)}. */
public final boolean allowRowStride;
private boolean disposed = false;
public StringBuilder toString(StringBuilder sb) {
if(null == sb) {
sb = new StringBuilder();
}
sb.append(pixelAttributes).append(", dim ").append(width).append("x").append(height).append("x").append(depth).append(", pack ").append(pack)
.append(", disposed ").append(disposed).append(", valid ").append(isValid()).append(", buffer[sz [bytes ").append(byteSize).append(", elemSize ").append(bufferElemSize).append(", ").append(buffer).append("]");
return sb;
}
public String toString() {
return "GLPixelBuffer["+toString(null).toString()+"]";
}
/**
* @param pixelAttributes the desired {@link GLPixelAttributes}
* @param width in pixels
* @param height in pixels
* @param depth in pixels
* @param pack true for read mode GPU -> CPU, otherwise false for write mode CPU -> GPU
* @param buffer the backing array
* @param allowRowStride If <code>true</code>, allow row-stride, otherwise not. See {@link #requiresNewBuffer(GL, int, int, int)}.
*/
public GLPixelBuffer(GLPixelAttributes pixelAttributes, int width, int height, int depth, boolean pack, Buffer buffer, boolean allowRowStride) {
this.pixelAttributes = pixelAttributes;
this.width = width;
this.height = height;
this.depth = depth;
this.pack = pack;
this.buffer = buffer;
this.byteSize = Buffers.remainingBytes(buffer);
this.bufferElemSize = Buffers.sizeOfBufferElem(buffer);
this.allowRowStride = allowRowStride;
}
/** Allow {@link GL2ES3#GL_PACK_ROW_LENGTH}, or {@link GL2ES2#GL_UNPACK_ROW_LENGTH}. */
public final boolean getAllowRowStride() { return allowRowStride; }
/** Is not {@link #dispose() disposed} and has {@link #byteSize} > 0. */
public boolean isValid() {
return !disposed && 0 < byteSize;
}
/** See {@link Buffer#rewind()}. */
public Buffer rewind() {
return buffer.rewind();
}
/** Returns the byte position of the {@link #buffer}. */
public int position() {
return buffer.position() * bufferElemSize;
}
/** Sets the byte position of the {@link #buffer}. */
public Buffer position(int bytePos) {
return buffer.position( bytePos / bufferElemSize );
}
/** Returns the byte capacity of the {@link #buffer}. */
public int capacity() {
return buffer.capacity() * bufferElemSize;
}
/** Returns the byte limit of the {@link #buffer}. */
public int limit() {
return buffer.limit() * bufferElemSize;
}
/** See {@link Buffer#flip()}. */
public Buffer flip() {
return buffer.flip();
}
/** See {@link Buffer#clear()}. */
public Buffer clear() {
return buffer.clear();
}
/**
* Returns true, if {@link #isValid() invalid} or implementation requires a new buffer based on the new size
* due to pixel alignment or byte size, otherwise false.
* <p>
* It is assumed that <code>pixelAttributes</code>, <code>depth</code> and <code>pack</code> stays the same!
* </p>
* <p>
* The minimum required byte size equals to <code>minByteSize</code>, if > 0,
* otherwise {@link GLBuffers#sizeof(GL, int[], int, int, int, int, int, boolean) GLBuffers.sizeof(..)}
* is being used to calculate it. This value is referred to <i>newByteSize</i>.
* </p>
* <p>
* If <code>{@link #allowRowStride} = false</code>,
* method returns <code>true</code> if the <i>newByteSize</i> > <i>currentByteSize</i>
* or the <code>newWidth</code> != <code>currentWidth</code>.
* </p>
* <p>
* If <code>{@link #allowRowStride} = true</code>, see {@link GLPixelBufferProvider#getAllowRowStride()},
* method returns <code>true</code> only if the <i>newByteSize</i> > <i>currentByteSize</i>.
* Assuming user utilizes the row-stride when dealing w/ the data, i.e. {@link GL2ES3#GL_PACK_ROW_LENGTH}.
* </p>
* @param gl the corresponding current GL context object
* @param newWidth new width in pixels
* @param newHeight new height in pixels
* @param newByteSize if > 0, the pre-calculated minimum byte-size for the resulting buffer, otherwise ignore.
* @see GLPixelBufferProvider#allocate(GL, GLPixelAttributes, int, int, int, boolean, int)
*/
public boolean requiresNewBuffer(GL gl, int newWidth, int newHeight, int newByteSize) {
if( !isValid() ) {
return true;
}
if( 0 >= newByteSize ) {
final int[] tmp = { 0 };
newByteSize = GLBuffers.sizeof(gl, tmp, pixelAttributes.bytesPerPixel, newWidth, newHeight, 1, true);
}
if( allowRowStride ) {
return byteSize < newByteSize;
}
return byteSize < newByteSize || width != newWidth;
}
/** Dispose resources. See {@link #isValid()}. */
public void dispose() {
disposed = true;
buffer.clear();
}
}
|