-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathTexture.java
More file actions
1670 lines (1350 loc) · 45.2 KB
/
Texture.java
File metadata and controls
1670 lines (1350 loc) · 45.2 KB
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-15 The Processing Foundation
Copyright (c) 2004-12 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.1.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.opengl;
import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PGraphics;
import processing.opengl.PGraphicsOpenGL.GLResourceTexture;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.NoSuchElementException;
/**
* This class wraps an OpenGL texture.
* By Andres Colubri
*
*/
public class Texture implements PConstants {
/**
* Texture with normalized UV.
*/
protected static final int TEX2D = 0;
/**
* Texture with un-normalized UV.
*/
protected static final int TEXRECT = 1;
/** Point sampling: both magnification and minification filtering are set
* to nearest */
protected static final int POINT = 2;
/** Linear sampling: magnification filtering is nearest, minification set
* to linear */
protected static final int LINEAR = 3;
/** Bilinear sampling: both magnification filtering is set to linear and
* minification either to linear-mipmap-nearest (linear interpolation is used
* within a mipmap, but not between different mipmaps). */
protected static final int BILINEAR = 4;
/** Trilinear sampling: magnification filtering set to linear, minification to
* linear-mipmap-linear, which offers the best mipmap quality since linear
* interpolation to compute the value in each of two maps and then
* interpolates linearly between these two values. */
protected static final int TRILINEAR = 5;
// This constant controls how many times pixelBuffer and rgbaPixels can be
// accessed before they are not released anymore. The idea is that if they
// have been used only a few times, it doesn't make sense to keep them around.
protected static final int MAX_UPDATES = 10;
// The minimum amount of free JVM's memory (in MB) before pixelBuffer and
// rgbaPixels are released every time after they are used.
protected static final int MIN_MEMORY = 5;
public int width, height;
public int glName;
public int glTarget;
public int glFormat;
public int glMinFilter;
public int glMagFilter;
public int glWrapS;
public int glWrapT;
public int glWidth;
public int glHeight;
private GLResourceTexture glres;
protected PGraphicsOpenGL pg;
protected PGL pgl; // The interface between Processing and OpenGL.
protected int context; // The context that created this texture.
protected boolean colorBuffer; // true if it is the color attachment of
// FrameBuffer object.
protected boolean usingMipmaps;
protected boolean usingRepeat;
protected float maxTexcoordU;
protected float maxTexcoordV;
protected boolean bound;
protected boolean invertedX;
protected boolean invertedY;
protected int[] rgbaPixels = null;
protected IntBuffer pixelBuffer = null;
protected int[] edgePixels = null;
protected IntBuffer edgeBuffer = null;
protected FrameBuffer tempFbo = null;
protected int pixBufUpdateCount = 0;
protected int rgbaPixUpdateCount = 0;
/** Modified portion of the texture */
protected boolean modified;
protected int mx1, my1, mx2, my2;
protected Object bufferSource;
protected LinkedList<BufferData> bufferCache = null;
protected LinkedList<BufferData> usedBuffers = null;
protected Method disposeBufferMethod;
public static final int MAX_BUFFER_CACHE_SIZE = 3;
////////////////////////////////////////////////////////////
// Constructors.
public Texture(PGraphicsOpenGL pg) {
this.pg = pg;
pgl = pg.pgl;
context = pgl.createEmptyContext();
colorBuffer = false;
glName = 0;
}
/**
* Creates an instance of PTexture with size width x height. The texture is
* initialized (empty) to that size.
* @param width int
* @param height int
*/
public Texture(PGraphicsOpenGL pg, int width, int height) {
this(pg, width, height, new Parameters());
}
/**
* Creates an instance of PTexture with size width x height and with the
* specified parameters. The texture is initialized (empty) to that size.
* @param width int
* @param height int
* @param params Parameters
*/
public Texture(PGraphicsOpenGL pg, int width, int height, Object params) {
this.pg = pg;
pgl = pg.pgl;
context = pgl.createEmptyContext();
colorBuffer = false;
glName = 0;
init(width, height, (Parameters)params);
}
////////////////////////////////////////////////////////////
// Init, resize methods
/**
* Sets the size of the image and texture to width x height. If the texture is
* already initialized, it first destroys the current OpenGL texture object
* and then creates a new one with the specified size.
* @param width int
* @param height int
*/
public void init(int width, int height) {
Parameters params;
if (0 < glName) {
// Re-initializing a pre-existing texture.
// We use the current parameters as default:
params = getParameters();
} else {
// Just built-in default parameters otherwise:
params = new Parameters();
}
init(width, height, params);
}
/**
* Sets the size of the image and texture to width x height, and the
* parameters of the texture to params. If the texture is already initialized,
* it first destroys the current OpenGL texture object and then creates a new
* one with the specified size.
* @param width int
* @param height int
* @param params GLTextureParameters
*/
public void init(int width, int height, Parameters params) {
setParameters(params);
setSize(width, height);
allocate();
}
/**
* Initializes the texture using GL parameters
*/
public void init(int width, int height,
int glName, int glTarget, int glFormat,
int glWidth, int glHeight,
int glMinFilter, int glMagFilter,
int glWrapS, int glWrapT) {
this.width = width;
this.height = height;
this.glName = glName;
this.glTarget = glTarget;
this.glFormat = glFormat;
this.glWidth = glWidth;
this.glHeight = glHeight;
this.glMinFilter = glMinFilter;
this.glMagFilter = glMagFilter;
this.glWrapS = glWrapS;
this.glWrapT = glWrapT;
maxTexcoordU = (float)width / glWidth;
maxTexcoordV = (float)height / glHeight;
usingMipmaps = glMinFilter == PGL.LINEAR_MIPMAP_NEAREST ||
glMinFilter == PGL.LINEAR_MIPMAP_LINEAR;
usingRepeat = glWrapS == PGL.REPEAT || glWrapT == PGL.REPEAT;
}
public void resize(int wide, int high) {
// Disposing current resources.
dispose();
// Creating new texture with the appropriate size.
Texture tex = new Texture(pg, wide, high, getParameters());
// Copying the contents of this texture into tex.
tex.set(this);
// Now, overwriting "this" with tex.
copyObject(tex);
// Nullifying some utility objects so they are recreated with the
// appropriate size when needed.
tempFbo = null;
}
/**
* Returns true if the texture has been initialized.
* @return boolean
*/
public boolean available() {
return 0 < glName;
}
////////////////////////////////////////////////////////////
// Set methods
public void set(Texture tex) {
copyTexture(tex, 0, 0, tex.width, tex.height, true);
}
public void set(Texture tex, int x, int y, int w, int h) {
copyTexture(tex, x, y, w, h, true);
}
public void set(int texTarget, int texName, int texWidth, int texHeight,
int w, int h) {
copyTexture(texTarget, texName, texWidth, texHeight, 0, 0, w, h, true);
}
public void set(int texTarget, int texName, int texWidth, int texHeight,
int target, int tex, int x, int y, int w, int h) {
copyTexture(texTarget, texName, texWidth, texHeight, x, y, w, h, true);
}
public void set(int[] pixels) {
set(pixels, 0, 0, width, height, ARGB);
}
public void set(int[] pixels, int format) {
set(pixels, 0, 0, width, height, format);
}
public void set(int[] pixels, int x, int y, int w, int h) {
set(pixels, x, y, w, h, ARGB);
}
public void set(int[] pixels, int x, int y, int w, int h, int format) {
if (pixels == null) {
PGraphics.showWarning("The pixels array is null.");
return;
}
if (pixels.length < w * h) {
PGraphics.showWarning("The pixel array has a length of " +
pixels.length + ", but it should be at least " +
w * h);
return;
}
if (pixels.length == 0 || w == 0 || h == 0) {
return;
}
boolean enabledTex = false;
if (!pgl.texturingIsEnabled(glTarget)) {
pgl.enableTexturing(glTarget);
enabledTex = true;
}
pgl.bindTexture(glTarget, glName);
loadPixels(w * h);
convertToRGBA(pixels, format, w, h);
if (invertedX) flipArrayOnX(rgbaPixels, 1);
if (invertedY) flipArrayOnY(rgbaPixels, 1);
updatePixelBuffer(rgbaPixels);
pgl.texSubImage2D(glTarget, 0, x, y, w, h, PGL.RGBA, PGL.UNSIGNED_BYTE,
pixelBuffer);
fillEdges(x, y, w, h);
if (usingMipmaps) {
if (PGraphicsOpenGL.autoMipmapGenSupported) {
pgl.generateMipmap(glTarget);
} else {
manualMipmap();
}
}
pgl.bindTexture(glTarget, 0);
if (enabledTex) {
pgl.disableTexturing(glTarget);
}
releasePixelBuffer();
releaseRGBAPixels();
updateTexels(x, y, w, h);
}
////////////////////////////////////////////////////////////
// Native set methods
public void setNative(int[] pixels) {
setNative(pixels, 0, 0, width, height);
}
public void setNative(int[] pixels, int x, int y, int w, int h) {
updatePixelBuffer(pixels);
setNative(pixelBuffer, x, y, w, h);
releasePixelBuffer();
}
public void setNative(IntBuffer pixBuf, int x, int y, int w, int h) {
if (pixBuf == null) {
pixBuf = null;
PGraphics.showWarning("The pixel buffer is null.");
return;
}
if (pixBuf.capacity() < w * h) {
PGraphics.showWarning("The pixel bufer has a length of " +
pixBuf.capacity() + ", but it should be at least " +
w * h);
return;
}
if (pixBuf.capacity() == 0) {
// Nothing to do (means that w == h == 0) but not an erroneous situation
return;
}
boolean enabledTex = false;
if (!pgl.texturingIsEnabled(glTarget)) {
pgl.enableTexturing(glTarget);
enabledTex = true;
}
pgl.bindTexture(glTarget, glName);
pgl.texSubImage2D(glTarget, 0, x, y, w, h, PGL.RGBA, PGL.UNSIGNED_BYTE,
pixBuf);
fillEdges(x, y, w, h);
if (usingMipmaps) {
if (PGraphicsOpenGL.autoMipmapGenSupported) {
pgl.generateMipmap(glTarget);
} else {
manualMipmap();
}
}
pgl.bindTexture(glTarget, 0);
if (enabledTex) {
pgl.disableTexturing(glTarget);
}
updateTexels(x, y, w, h);
}
////////////////////////////////////////////////////////////
// Get methods
/**
* Copy texture to pixels. Involves video memory to main memory transfer (slow).
*/
public void get(int[] pixels) {
if (pixels == null) {
throw new RuntimeException("Trying to copy texture to null pixels array");
}
if (pixels.length != width * height) {
throw new RuntimeException("Trying to copy texture to pixels array of " +
"wrong size");
}
if (tempFbo == null) {
tempFbo = new FrameBuffer(pg, glWidth, glHeight);
}
// Attaching the texture to the color buffer of a FBO, binding the FBO and
// reading the pixels from the current draw buffer (which is the color
// buffer of the FBO).
tempFbo.setColorBuffer(this);
pg.pushFramebuffer();
pg.setFramebuffer(tempFbo);
tempFbo.readPixels();
pg.popFramebuffer();
tempFbo.getPixels(pixels);
convertToARGB(pixels);
if (invertedX) flipArrayOnX(pixels, 1);
if (invertedY) flipArrayOnY(pixels, 1);
}
////////////////////////////////////////////////////////////
// Put methods (the source texture is not resized to cover the entire
// destination).
public void put(Texture tex) {
copyTexture(tex, 0, 0, tex.width, tex.height, false);
}
public void put(Texture tex, int x, int y, int w, int h) {
copyTexture(tex, x, y, w, h, false);
}
public void put(int texTarget, int texName, int texWidth, int texHeight,
int w, int h) {
copyTexture(texTarget, texName, texWidth, texHeight, 0, 0, w, h, false);
}
public void put(int texTarget, int texName, int texWidth, int texHeight,
int target, int tex, int x, int y, int w, int h) {
copyTexture(texTarget, texName, texWidth, texHeight, x, y, w, h, false);
}
////////////////////////////////////////////////////////////
// Get OpenGL parameters
/**
* Returns true or false whether or not the texture is using mipmaps.
* @return boolean
*/
public boolean usingMipmaps() {
return usingMipmaps;
}
public void usingMipmaps(boolean mipmaps, int sampling) {
int glMagFilter0 = glMagFilter;
int glMinFilter0 = glMinFilter;
if (mipmaps) {
if (sampling == POINT) {
glMagFilter = PGL.NEAREST;
glMinFilter = PGL.NEAREST;
usingMipmaps = false;
} else if (sampling == LINEAR) {
glMagFilter = PGL.NEAREST;
glMinFilter =
PGL.MIPMAPS_ENABLED ? PGL.LINEAR_MIPMAP_NEAREST : PGL.LINEAR;
usingMipmaps = true;
} else if (sampling == BILINEAR) {
glMagFilter = PGL.LINEAR;
glMinFilter =
PGL.MIPMAPS_ENABLED ? PGL.LINEAR_MIPMAP_NEAREST : PGL.LINEAR;
usingMipmaps = true;
} else if (sampling == TRILINEAR) {
glMagFilter = PGL.LINEAR;
glMinFilter =
PGL.MIPMAPS_ENABLED ? PGL.LINEAR_MIPMAP_LINEAR : PGL.LINEAR;
usingMipmaps = true;
} else {
throw new RuntimeException("Unknown texture filtering mode");
}
} else {
usingMipmaps = false;
if (sampling == POINT) {
glMagFilter = PGL.NEAREST;
glMinFilter = PGL.NEAREST;
} else if (sampling == LINEAR) {
glMagFilter = PGL.NEAREST;
glMinFilter = PGL.LINEAR;
} else if (sampling == BILINEAR || sampling == TRILINEAR) {
glMagFilter = PGL.LINEAR;
glMinFilter = PGL.LINEAR;
} else {
throw new RuntimeException("Unknown texture filtering mode");
}
}
if (glMagFilter0 != glMagFilter || glMinFilter0 != glMinFilter) {
bind();
pgl.texParameteri(glTarget, PGL.TEXTURE_MIN_FILTER, glMinFilter);
pgl.texParameteri(glTarget, PGL.TEXTURE_MAG_FILTER, glMagFilter);
if (usingMipmaps) {
if (PGraphicsOpenGL.autoMipmapGenSupported) {
pgl.generateMipmap(glTarget);
} else {
manualMipmap();
}
}
unbind();
}
}
/**
* Returns true or false whether or not the texture is using repeat wrap mode
* along either U or V directions.
* @return boolean
*/
public boolean usingRepeat() {
return usingRepeat;
}
public void usingRepeat(boolean repeat) {
if (repeat) {
glWrapS = PGL.REPEAT;
glWrapT = PGL.REPEAT;
usingRepeat = true;
} else {
glWrapS = PGL.CLAMP_TO_EDGE;
glWrapT = PGL.CLAMP_TO_EDGE;
usingRepeat = false;
}
bind();
pgl.texParameteri(glTarget, PGL.TEXTURE_WRAP_S, glWrapS);
pgl.texParameteri(glTarget, PGL.TEXTURE_WRAP_T, glWrapT);
unbind();
}
/**
* Returns the maximum possible value for the texture coordinate U
* (horizontal).
* @return float
*/
public float maxTexcoordU() {
return maxTexcoordU;
}
/**
* Returns the maximum possible value for the texture coordinate V (vertical).
* @return float
*/
public float maxTexcoordV() {
return maxTexcoordV;
}
/**
* Returns true if the texture is inverted along the horizontal direction.
* @return boolean;
*/
public boolean invertedX() {
return invertedX;
}
/**
* Sets the texture as inverted or not along the horizontal direction.
* @param v boolean;
*/
public void invertedX(boolean v) {
invertedX = v;
}
/**
* Returns true if the texture is inverted along the vertical direction.
* @return boolean;
*/
public boolean invertedY() {
return invertedY;
}
/**
* Sets the texture as inverted or not along the vertical direction.
* @param v boolean;
*/
public void invertedY(boolean v) {
invertedY = v;
}
public int currentSampling() {
if (glMagFilter == PGL.NEAREST && glMinFilter == PGL.NEAREST) {
return POINT;
} else if (glMagFilter == PGL.NEAREST &&
glMinFilter == (PGL.MIPMAPS_ENABLED ? PGL.LINEAR_MIPMAP_NEAREST : PGL.LINEAR)) {
return LINEAR;
} else if (glMagFilter == PGL.LINEAR &&
glMinFilter == (PGL.MIPMAPS_ENABLED ? PGL.LINEAR_MIPMAP_NEAREST : PGL.LINEAR)) {
return BILINEAR;
} else if (glMagFilter == PGL.LINEAR &&
glMinFilter == PGL.LINEAR_MIPMAP_LINEAR) {
return TRILINEAR;
} else {
return -1;
}
}
////////////////////////////////////////////////////////////
// Bind/unbind
public void bind() {
// Binding a texture automatically enables texturing for the
// texture target from that moment onwards. Unbinding the texture
// won't disable texturing.
if (!pgl.texturingIsEnabled(glTarget)) {
pgl.enableTexturing(glTarget);
}
pgl.bindTexture(glTarget, glName);
bound = true;
}
public void unbind() {
if (pgl.textureIsBound(glTarget, glName)) {
// We don't want to unbind another texture
// that might be bound instead of this one.
if (!pgl.texturingIsEnabled(glTarget)) {
pgl.enableTexturing(glTarget);
pgl.bindTexture(glTarget, 0);
pgl.disableTexturing(glTarget);
} else {
pgl.bindTexture(glTarget, 0);
}
}
bound = false;
}
public boolean bound() {
// A true result might not necessarily mean that texturing is enabled
// (a texture can be bound to the target, but texturing is disabled).
return bound;
}
//////////////////////////////////////////////////////////////
// Modified flag
public boolean isModified() {
return modified;
}
public void setModified() {
modified = true;
}
public void setModified(boolean m) {
modified = m;
}
public int getModifiedX1() {
return mx1;
}
public int getModifiedX2() {
return mx2;
}
public int getModifiedY1() {
return my1;
}
public int getModifiedY2() {
return my2;
}
public void updateTexels() {
updateTexelsImpl(0, 0, width, height);
}
public void updateTexels(int x, int y, int w, int h) {
updateTexelsImpl(x, y, w, h);
}
protected void updateTexelsImpl(int x, int y, int w, int h) {
int x2 = x + w;
int y2 = y + h;
if (!modified) {
mx1 = PApplet.max(0, x);
mx2 = PApplet.min(width - 1, x2);
my1 = PApplet.max(0, y);
my2 = PApplet.min(height - 1, y2);
modified = true;
} else {
if (x < mx1) mx1 = PApplet.max(0, x);
if (x > mx2) mx2 = PApplet.min(width - 1, x);
if (y < my1) my1 = PApplet.max(0, y);
if (y > my2) my2 = y;
if (x2 < mx1) mx1 = PApplet.max(0, x2);
if (x2 > mx2) mx2 = PApplet.min(width - 1, x2);
if (y2 < my1) my1 = PApplet.max(0, y2);
if (y2 > my2) my2 = PApplet.min(height - 1, y2);
}
}
protected void loadPixels(int len) {
if (rgbaPixels == null || rgbaPixels.length < len) {
rgbaPixels = new int[len];
}
}
protected void updatePixelBuffer(int[] pixels) {
pixelBuffer = PGL.updateIntBuffer(pixelBuffer, pixels, true);
pixBufUpdateCount++;
}
protected void manualMipmap() {
// TODO: finish manual mipmap generation,
// https://github.com/processing/processing/issues/3335
}
////////////////////////////////////////////////////////////
// Buffer sink interface.
public void setBufferSource(Object source) {
bufferSource = source;
getSourceMethods();
}
public void copyBufferFromSource(Object natRef, ByteBuffer byteBuf,
int w, int h) {
if (bufferCache == null) {
bufferCache = new LinkedList<BufferData>();
}
if (bufferCache.size() + 1 <= MAX_BUFFER_CACHE_SIZE) {
bufferCache.add(new BufferData(natRef, byteBuf.asIntBuffer(), w, h));
} else {
// The buffer cache reached the maximum size, so we just dispose
// the new buffer by adding it to the list of used buffers.
try {
usedBuffers.add(new BufferData(natRef, byteBuf.asIntBuffer(), w, h));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void disposeSourceBuffer() {
if (usedBuffers == null) return;
while (0 < usedBuffers.size()) {
BufferData data = null;
try {
data = usedBuffers.remove(0);
} catch (NoSuchElementException ex) {
PGraphics.showWarning("Cannot remove used buffer");
}
if (data != null) {
data.dispose();
}
}
}
public void getBufferPixels(int[] pixels) {
// We get the buffer either from the used buffers or the cache, giving
// priority to the used buffers. Why? Because the used buffer was already
// transferred to the texture, so the pixels should be in sync with the
// texture.
BufferData data = null;
if (usedBuffers != null && 0 < usedBuffers.size()) {
data = usedBuffers.getLast();
} else if (bufferCache != null && 0 < bufferCache.size()) {
data = bufferCache.getLast();
}
if (data != null) {
if ((data.w != width) || (data.h != height)) {
init(data.w, data.h);
}
data.rgbBuf.rewind();
data.rgbBuf.get(pixels);
convertToARGB(pixels);
// In order to avoid a cached buffer to overwrite the texture when the
// renderer draws the texture, and hence put the pixels put of sync, we
// simply empty the cache.
if (usedBuffers == null) {
usedBuffers = new LinkedList<BufferData>();
}
while (0 < bufferCache.size()) {
data = bufferCache.remove(0);
usedBuffers.add(data);
}
}
}
public boolean hasBufferSource() {
return bufferSource != null;
}
public boolean hasBuffers() {
return bufferSource != null && bufferCache != null &&
0 < bufferCache.size();
}
protected boolean bufferUpdate() {
BufferData data = null;
try {
data = bufferCache.remove(0);
} catch (NoSuchElementException ex) {
PGraphics.showWarning("Don't have pixel data to copy to texture");
}
if (data != null) {
if ((data.w != width) || (data.h != height)) {
init(data.w, data.h);
}
data.rgbBuf.rewind();
setNative(data.rgbBuf, 0, 0, width, height);
// Putting the buffer in the used buffers list to dispose at the end of
// draw.
if (usedBuffers == null) {
usedBuffers = new LinkedList<BufferData>();
}
usedBuffers.add(data);
return true;
} else {
return false;
}
}
protected void getSourceMethods() {
try {
disposeBufferMethod = bufferSource.getClass().
getMethod("disposeBuffer", new Class[] { Object.class });
} catch (Exception e) {
throw new RuntimeException("Provided source object doesn't have a " +
"disposeBuffer method.");
}
}
////////////////////////////////////////////////////////////
// Utilities
/**
* Flips intArray along the X axis.
* @param intArray int[]
* @param mult int
*/
protected void flipArrayOnX(int[] intArray, int mult) {
int index = 0;
int xindex = mult * (width - 1);
for (int x = 0; x < width / 2; x++) {
for (int y = 0; y < height; y++) {
int i = index + mult * y * width;
int j = xindex + mult * y * width;
for (int c = 0; c < mult; c++) {
int temp = intArray[i];
intArray[i] = intArray[j];
intArray[j] = temp;
i++;
j++;
}
}
index += mult;
xindex -= mult;
}
}
/**
* Flips intArray along the Y axis.
* @param intArray int[]
* @param mult int
*/
protected void flipArrayOnY(int[] intArray, int mult) {
int index = 0;
int yindex = mult * (height - 1) * width;
for (int y = 0; y < height / 2; y++) {
for (int x = 0; x < mult * width; x++) {
int temp = intArray[index];
intArray[index] = intArray[yindex];
intArray[yindex] = temp;
index++;
yindex++;
}
yindex -= mult * width * 2;
}
}
/**
* Reorders a pixel array in the given format into the order required by
* OpenGL (RGBA) and stores it into rgbaPixels. The width and height
* parameters are used in the YUV420 to RBGBA conversion.
* @param pixels int[]
* @param format int
* @param w int
* @param h int
*/