aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/com/jogamp/opengl/util/TileRenderer.java
blob: 887312a645f5f5f9a9f50c9cc857956f7d9ddde7 (plain)
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/**
 * 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.
 *
 * ---------------------
 *
 * Based on Brian Paul's tile rendering library, found
 * at <a href = "http://www.mesa3d.org/brianp/TR.html">http://www.mesa3d.org/brianp/TR.html</a>.
 *
 * Copyright (C) 1997-2005 Brian Paul.
 * Licensed under BSD-compatible terms with permission of the author.
 * See LICENSE.txt for license information.
 */
package com.jogamp.opengl.util;

import javax.media.nativewindow.util.Dimension;
import javax.media.nativewindow.util.DimensionImmutable;
import javax.media.opengl.GL;
import javax.media.opengl.GL2ES3;
import javax.media.opengl.GLException;

import com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes;

/**
 * A fairly direct port of Brian Paul's tile rendering library, found
 * at <a href = "http://www.mesa3d.org/brianp/TR.html">
 * http://www.mesa3d.org/brianp/TR.html </a> . I've java-fied it, but
 * the functionality is the same.
 * <p>
 * Original code Copyright (C) 1997-2005 Brian Paul. Licensed under
 * BSD-compatible terms with permission of the author. See LICENSE.txt
 * for license information.
 * </p>
 * <p>
 * Enhanced for {@link GL2ES3}.
 * </p>
 * <p>
 * See {@link TileRendererBase} for details.
 * </p>
 *
 * @author ryanm, sgothel
 */
public class TileRenderer extends TileRendererBase {
    /**
     * The width of the final clipped image. See {@link #getParam(int)}.
     */
    public static final int TR_IMAGE_CLIPPING_WIDTH = 7;
    /**
     * The height of the final clipped image. See {@link #getParam(int)}.
     */
    public static final int TR_IMAGE_CLIPPING_HEIGHT = 8;
    /**
     * The width of the tiles. See {@link #getParam(int)}.
     */
    public static final int TR_TILE_WIDTH = 9;
    /**
     * The height of the tiles. See {@link #getParam(int)}.
     */
    public static final int TR_TILE_HEIGHT = 10;
    /**
     * The width of the border around the tiles. See {@link #getParam(int)}.
     */
    public static final int TR_TILE_BORDER = 11;
    /**
     * The tiles x-offset. See {@link #getParam(int)}.
     */
    public static final int TR_TILE_X_OFFSET = 12;
    /**
     * The tiles y-offset. See {@link #getParam(int)}.
     */
    public static final int TR_TILE_Y_OFFSET = 13;
    /**
     * The number of rows of tiles. See {@link #getParam(int)}.
     */
    public static final int TR_ROWS = 14;
    /**
     * The number of columns of tiles. See {@link #getParam(int)}.
     */
    public static final int TR_COLUMNS = 15;
    /**
     * The current tile number. Has value -1 if {@link #eot()}. See {@link #getParam(int)}.
     */
    public static final int TR_CURRENT_TILE_NUM = 16;
    /**
     * The current row number. See {@link #getParam(int)}.
     */
    public static final int TR_CURRENT_ROW = 17;
    /**
     * The current column number. See {@link #getParam(int)}.
     */
    public static final int TR_CURRENT_COLUMN = 18;
    /**
     * The order that the rows are traversed. See {@link #getParam(int)}.
     */
    public static final int TR_ROW_ORDER = 19;
    /**
     * Indicates we are traversing rows from the top to the bottom. See {@link #getParam(int)}.
     */
    public static final int TR_TOP_TO_BOTTOM = 20;
    /**
     * Indicates we are traversing rows from the bottom to the top (default). See {@link #getParam(int)}.
     */
    public static final int TR_BOTTOM_TO_TOP = 21;

    private static final int DEFAULT_TILE_WIDTH = 256;
    private static final int DEFAULT_TILE_HEIGHT = 256;
    private static final int DEFAULT_TILE_BORDER = 0;

    private final Dimension tileSize = new Dimension(DEFAULT_TILE_WIDTH, DEFAULT_TILE_HEIGHT);
    private final Dimension tileSizeNB = new Dimension(DEFAULT_TILE_WIDTH - 2 * DEFAULT_TILE_BORDER, DEFAULT_TILE_HEIGHT - 2 * DEFAULT_TILE_BORDER);

    private boolean isInit = false;
    private Dimension imageClippingDim = null; // not set - default
    private int tileBorder = DEFAULT_TILE_BORDER;
    private int rowOrder = TR_BOTTOM_TO_TOP;
    private int rows;
    private int columns;
    private int currentTile = 0;
    private int currentRow;
    private int currentColumn;
    private int offsetX;
    private int offsetY;

    @Override
    protected StringBuilder tileDetails(StringBuilder sb) {
        sb.append("# "+currentTile+": ["+currentColumn+"]["+currentRow+"] / "+columns+"x"+rows+", ")
        .append("rowOrder "+rowOrder+", offset/size "+offsetX+"/"+offsetY+" "+tileSize.getWidth()+"x"+tileSize.getHeight()+" brd "+tileBorder+", ");
        return super.tileDetails(sb);
    }

    /**
     * Creates a new TileRenderer object
     */
    public TileRenderer() {
        super();
    }

    /**
     * {@inheritDoc}
     * <p>
     * Implementation {@link #reset()} internal states.
     * </p>
     */
    @Override
    public final void setImageSize(int width, int height) {
        super.setImageSize(width, height);
        reset();
    }

    /**
     * Clips the image-size this tile-renderer iterates through,
     * which can be retrieved via {@link #getClippedImageSize()}.
     * <p>
     * Original image-size stored in this tile-renderer is unmodified.
     * </p>
     * <p>
     * Implementation {@link #reset()} internal states.
     * </p>
     *
     * @param width The image-clipping.width
     * @param height The image-clipping.height
     * @see #getClippedImageSize()
     */
    public final void clipImageSize(int width, int height) {
        if( null == imageClippingDim ) {
            imageClippingDim = new Dimension(width, height);
        } else {
            imageClippingDim.set(width, height);
        }
        reset();
    }

    /**
     * Returns the clipped image-size.
     * <p>
     * If a image-size is clipped via {@link #clipImageSize(int, int)},
     * method returns:
     * <ul>
     *   <li><code>min( image-clipping, image-size )</code>, otherwise</li>
     *   <li><code> image-size </code></li>
     * </ul>
     * </p>
     * <p>
     * The clipping width and height can be retrieved via {@link #TR_IMAGE_CLIPPING_WIDTH}
     * {@link #TR_IMAGE_CLIPPING_HEIGHT}.
     * </p>
     */
    public final DimensionImmutable getClippedImageSize() {
        if( null != imageClippingDim ) {
            return new Dimension(Math.min(imageClippingDim.getWidth(), imageSize.getWidth()),
                                 Math.min(imageClippingDim.getHeight(), imageSize.getHeight()) );
        } else {
            return imageSize;
        }
    }

    /**
     * Sets the size of the tiles to use in rendering. The actual
     * effective size of the tile depends on the border size, ie (
     * width - 2*border ) * ( height - 2 * border )
     * <p>
     * Implementation {@link #reset()} internal states.
     * </p>
     *
     * @param width
     *           The width of the tiles. Must not be larger than the GL
     *           context
     * @param height
     *           The height of the tiles. Must not be larger than the
     *           GL context
     * @param border
     *           The width of the borders on each tile. This is needed
     *           to avoid artifacts when rendering lines or points with
     *           thickness > 1.
     */
    public final void setTileSize(int width, int height, int border) {
        if( 0 > border ) {
            throw new IllegalArgumentException("Tile border must be >= 0");
        }
        if( 2 * border >= width || 2 * border >= height ) {
            throw new IllegalArgumentException("Tile size must be > 0x0 minus 2*border");
        }
        tileBorder = border;
        tileSize.set( width, height );
        tileSizeNB.set( width - 2 * border, height - 2 * border );
        reset();
    }

    /**
     * Sets an xy offset for the resulting tiles
     * {@link TileRendererBase#TR_CURRENT_TILE_X_POS x-pos} and {@link TileRendererBase#TR_CURRENT_TILE_Y_POS y-pos}.
     * @see #TR_TILE_X_OFFSET
     * @see #TR_TILE_Y_OFFSET
     **/
    public void setTileOffset(int xoff, int yoff) {
        offsetX = xoff;
        offsetY = yoff;
    }

    /**
     * {@inheritDoc}
     *
     * Reset internal states of {@link TileRenderer} are:
     * <ul>
     *  <li>{@link #TR_ROWS}</li>
     *  <li>{@link #TR_COLUMNS}</li>
     *  <li>{@link #TR_CURRENT_COLUMN}</li>
     *  <li>{@link #TR_CURRENT_ROW}</li>
     *  <li>{@link #TR_CURRENT_TILE_NUM}</li>
     *  <li>{@link #TR_CURRENT_TILE_X_POS}</li>
     *  <li>{@link #TR_CURRENT_TILE_Y_POS}</li>
     *  <li>{@link #TR_CURRENT_TILE_WIDTH}</li>
     *  <li>{@link #TR_CURRENT_TILE_HEIGHT}</li>
     *</ul>
     */
    @Override
    public final void reset() {
        final DimensionImmutable clippedImageSize = getClippedImageSize();
        columns = ( clippedImageSize.getWidth() + tileSizeNB.getWidth() - 1 ) / tileSizeNB.getWidth();
        rows = ( clippedImageSize.getHeight() + tileSizeNB.getHeight() - 1 ) / tileSizeNB.getHeight();
        currentRow = 0;
        currentColumn = 0;
        currentTile = 0;
        currentTileXPos = 0;
        currentTileYPos = 0;
        currentTileWidth = 0;
        currentTileHeight = 0;

        assert columns >= 0;
        assert rows >= 0;

        beginCalled = false;
        isInit = true;
    }

    /* pp */ final int getCurrentTile() { return currentTile; }

    @Override
    public final int getParam(int pname) {
        switch (pname) {
        case TR_IMAGE_WIDTH:
            return imageSize.getWidth();
        case TR_IMAGE_HEIGHT:
            return imageSize.getHeight();
        case TR_CURRENT_TILE_X_POS:
            return currentTileXPos;
        case TR_CURRENT_TILE_Y_POS:
            return currentTileYPos;
        case TR_CURRENT_TILE_WIDTH:
            return currentTileWidth;
        case TR_CURRENT_TILE_HEIGHT:
            return currentTileHeight;
        case TR_IMAGE_CLIPPING_WIDTH:
            return null != imageClippingDim ? imageClippingDim.getWidth() : 0;
        case TR_IMAGE_CLIPPING_HEIGHT:
            return null != imageClippingDim ? imageClippingDim.getHeight() : 0;
        case TR_TILE_WIDTH:
            return tileSize.getWidth();
        case TR_TILE_HEIGHT:
            return tileSize.getHeight();
        case TR_TILE_BORDER:
            return tileBorder;
        case TR_TILE_X_OFFSET:
            return offsetX;
        case TR_TILE_Y_OFFSET:
            return offsetY;
        case TR_ROWS:
            return rows;
        case TR_COLUMNS:
            return columns;
        case TR_CURRENT_TILE_NUM:
            return currentTile;
        case TR_CURRENT_ROW:
            return currentRow;
        case TR_CURRENT_COLUMN:
            return currentColumn;
        case TR_ROW_ORDER:
            return rowOrder;
        default:
            throw new IllegalArgumentException("Invalid pname: "+pname);
        }
    }

    /**
     * Sets the order of row traversal, default is {@link #TR_BOTTOM_TO_TOP}.
     *
     * @param order The row traversal order, must be either {@link #TR_TOP_TO_BOTTOM} or {@link #TR_BOTTOM_TO_TOP}.
     */
    public final void setRowOrder(int order) {
        if (order == TR_TOP_TO_BOTTOM || order == TR_BOTTOM_TO_TOP) {
            rowOrder = order;
        } else {
            throw new IllegalArgumentException("Must pass TR_TOP_TO_BOTTOM or TR_BOTTOM_TO_TOP");
        }
    }

    @Override
    public final boolean isSetup() {
        return 0 < imageSize.getWidth() && 0 < imageSize.getHeight();
    }

    /**
     * {@inheritDoc}
     *
     * <p>
     * <i>end of tiling</i> is reached w/ {@link TileRenderer}, if at least one of the following is true:
     * <ul>
     *   <li>all tiles have been rendered, i.e. {@link #TR_CURRENT_TILE_NUM} is -1</li>
     *   <li>no tiles to render, i.e. {@link #TR_COLUMNS} or {@link #TR_ROWS} is 0</li>
     * </ul>
     * </p>
     */
    @Override
    public final boolean eot() {
        if ( !isInit ) { // ensure at least one reset-call
            reset();
        }
        return 0 > currentTile || 0 >= columns*rows;
    }

    /**
     * {@inheritDoc}
     *
     * @throws IllegalStateException if {@link #setImageSize(int, int) image-size} has not been set or
     *         {@link #eot() end-of-tiling} has been reached.
     */
    @Override
    public final void beginTile( GL gl ) throws IllegalStateException, GLException {
        if( !isSetup() ) {
            throw new IllegalStateException("Image size has not been set: "+this);
        }
        if ( eot() ) {
            throw new IllegalStateException("EOT reached: "+this);
        }
        validateGL(gl);

        /* which tile (by row and column) we're about to render */
        if (rowOrder == TR_BOTTOM_TO_TOP) {
            currentRow = currentTile / columns;
            currentColumn = currentTile % columns;
        } else {
            currentRow = rows - ( currentTile / columns ) - 1;
            currentColumn = currentTile % columns;
        }
        assert ( currentRow < rows );
        assert ( currentColumn < columns );

        int border = tileBorder;

        final DimensionImmutable clippedImageSize = getClippedImageSize();
        int tH, tW;

        /* Compute actual size of this tile with border */
        if (currentRow < rows - 1) {
            tH = tileSize.getHeight();
        } else {
            tH = clippedImageSize.getHeight() - ( rows - 1 ) * ( tileSizeNB.getHeight() ) + 2 * border;
        }

        if (currentColumn < columns - 1) {
            tW = tileSize.getWidth();
        } else {
            tW = clippedImageSize.getWidth() - ( columns - 1 ) * ( tileSizeNB.getWidth()  ) + 2 * border;
        }

        currentTileXPos = currentColumn * tileSizeNB.getWidth() + offsetX;
        currentTileYPos = currentRow * tileSizeNB.getHeight() + offsetY;

        /* Save tile size, with border */
        currentTileWidth = tW;
        currentTileHeight = tH;

        gl.glViewport( 0, 0, tW, tH );

        if( DEBUG ) {
            System.err.println("TileRenderer.begin: "+this.toString());
        }

        // Do not forget to issue:
        //    reshape( 0, 0, tW, tH );
        // which shall reflect tile renderer tiles: currentTileXPos, currentTileYPos and imageSize
        beginCalled = true;
    }

    @Override
    public void endTile( GL gl ) throws IllegalStateException, GLException {
        if( !beginCalled ) {
            throw new IllegalStateException("beginTile(..) has not been called");
        }
        validateGL(gl);

        // be sure OpenGL rendering is finished
        gl.glFlush();

        // implicitly save current glPixelStore values
        psm.setPackAlignment(gl, 1);
        final GL2ES3 gl2es3;
        final int readBuffer;
        if( gl.isGL2ES3() ) {
            gl2es3 = gl.getGL2ES3();
            readBuffer = gl2es3.getDefaultReadBuffer();
            gl2es3.glReadBuffer(readBuffer);
        } else {
            gl2es3 = null;
            readBuffer = 0; // undef. probably default: GL_FRONT (single buffering) GL_BACK (double buffering)
        }
        if( DEBUG ) {
            System.err.println("TileRenderer.end.0: readBuffer 0x"+Integer.toHexString(readBuffer)+", "+this.toString());
        }

        final int tmp[] = new int[1];

        if( tileBuffer != null ) {
            final GLPixelAttributes pixelAttribs = tileBuffer.pixelAttributes;
            final int srcX = tileBorder;
            final int srcY = tileBorder;
            final int srcWidth = tileSizeNB.getWidth();
            final int srcHeight = tileSizeNB.getHeight();
            final int readPixelSize = GLBuffers.sizeof(gl, tmp, pixelAttribs.bytesPerPixel, srcWidth, srcHeight, 1, true);
            tileBuffer.clear();
            if( tileBuffer.requiresNewBuffer(gl, srcWidth, srcHeight, readPixelSize) ) {
                throw new IndexOutOfBoundsException("Required " + readPixelSize + " bytes of buffer, only had " + tileBuffer);
            }
            gl.glReadPixels( srcX, srcY, srcWidth, srcHeight, pixelAttribs.format, pixelAttribs.type, tileBuffer.buffer);
            // be sure OpenGL rendering is finished
            gl.glFlush();
            tileBuffer.position( readPixelSize );
            tileBuffer.flip();
        }

        if( imageBuffer != null ) {
            final GLPixelAttributes pixelAttribs = imageBuffer.pixelAttributes;
            final int srcX = tileBorder;
            final int srcY = tileBorder;
            final int srcWidth = currentTileWidth - 2 * tileBorder;
            final int srcHeight = currentTileHeight - 2 * tileBorder;

            /* setup pixel store for glReadPixels */
            final int rowLength = imageSize.getWidth();
            psm.setPackRowLength(gl2es3, rowLength);

            /* read the tile into the final image */
            final int readPixelSize = GLBuffers.sizeof(gl, tmp, pixelAttribs.bytesPerPixel, srcWidth, srcHeight, 1, true);

            final int skipPixels = currentColumn * tileSizeNB.getWidth();
            final int skipRows = currentRow * tileSizeNB.getHeight();
            final int ibPos = ( skipPixels + ( skipRows * rowLength ) ) * pixelAttribs.bytesPerPixel;
            final int ibLim = ibPos + readPixelSize;
            imageBuffer.clear();
            if( imageBuffer.requiresNewBuffer(gl, srcWidth, srcHeight, readPixelSize) ) {
                throw new IndexOutOfBoundsException("Required " + ibLim + " bytes of buffer, only had " + imageBuffer);
            }
            imageBuffer.position(ibPos);

            gl.glReadPixels( srcX, srcY, srcWidth, srcHeight, pixelAttribs.format, pixelAttribs.type, imageBuffer.buffer);
            // be sure OpenGL rendering is finished
            gl.glFlush();
            imageBuffer.position( ibLim );
            imageBuffer.flip();
        }

        /* restore previous glPixelStore values */
        psm.restore(gl);

        beginCalled = false;

        /* increment tile counter, return 1 if more tiles left to render */
        currentTile++;
        if( currentTile >= rows * columns ) {
            currentTile = -1; /* all done */
        }
    }
}