28
28
import processing .core .PConstants ;
29
29
import processing .opengl .PGraphicsOpenGL .GLResourceFrameBuffer ;
30
30
31
+ import java .lang .ref .WeakReference ;
31
32
import java .nio .IntBuffer ;
32
33
33
34
/**
42
43
*/
43
44
44
45
public class FrameBuffer implements PConstants {
45
- protected PGraphicsOpenGL pg ;
46
+ protected WeakReference < PGraphicsOpenGL > pg ;
46
47
protected PGL pgl ;
47
48
protected int context ; // The context that created this framebuffer.
48
49
@@ -72,7 +73,7 @@ public class FrameBuffer implements PConstants {
72
73
73
74
74
75
FrameBuffer (PGraphicsOpenGL pg ) {
75
- this .pg = pg ;
76
+ this .pg = new WeakReference < PGraphicsOpenGL >( pg ) ;
76
77
pgl = pg .pgl ;
77
78
context = pgl .createEmptyContext ();
78
79
}
@@ -152,15 +153,17 @@ public class FrameBuffer implements PConstants {
152
153
153
154
154
155
public void clear () {
155
- pg .pushFramebuffer ();
156
- pg .setFramebuffer (this );
156
+ PGraphicsOpenGL g = pg .get ();
157
+ if (g == null ) return ;
158
+ g .pushFramebuffer ();
159
+ g .setFramebuffer (this );
157
160
pgl .clearDepth (1 );
158
161
pgl .clearStencil (0 );
159
162
pgl .clearColor (0 , 0 , 0 , 0 );
160
163
pgl .clear (PGL .DEPTH_BUFFER_BIT |
161
164
PGL .STENCIL_BUFFER_BIT |
162
165
PGL .COLOR_BUFFER_BIT );
163
- pg .popFramebuffer ();
166
+ g .popFramebuffer ();
164
167
}
165
168
166
169
public void copyColor (FrameBuffer dest ) {
@@ -176,12 +179,14 @@ public void copyStencil(FrameBuffer dest) {
176
179
}
177
180
178
181
public void copy (FrameBuffer dest , int mask ) {
182
+ PGraphicsOpenGL g = pg .get ();
183
+ if (g == null ) return ;
179
184
pgl .bindFramebufferImpl (PGL .READ_FRAMEBUFFER , this .glFbo );
180
185
pgl .bindFramebufferImpl (PGL .DRAW_FRAMEBUFFER , dest .glFbo );
181
186
pgl .blitFramebuffer (0 , 0 , this .width , this .height ,
182
187
0 , 0 , dest .width , dest .height , mask , PGL .NEAREST );
183
- pgl .bindFramebufferImpl (PGL .READ_FRAMEBUFFER , pg .getCurrentFB ().glFbo );
184
- pgl .bindFramebufferImpl (PGL .DRAW_FRAMEBUFFER , pg .getCurrentFB ().glFbo );
188
+ pgl .bindFramebufferImpl (PGL .READ_FRAMEBUFFER , g .getCurrentFB ().glFbo );
189
+ pgl .bindFramebufferImpl (PGL .DRAW_FRAMEBUFFER , g .getCurrentFB ().glFbo );
185
190
}
186
191
187
192
public void bind () {
@@ -193,9 +198,11 @@ public void disableDepthTest() {
193
198
}
194
199
195
200
public void finish () {
201
+ PGraphicsOpenGL g = pg .get ();
202
+ if (g == null ) return ;
196
203
if (noDepth ) {
197
204
// No need to clear depth buffer because depth testing was disabled.
198
- if (pg .getHint (ENABLE_DEPTH_TEST )) {
205
+ if (g .getHint (ENABLE_DEPTH_TEST )) {
199
206
pgl .enable (PGL .DEPTH_TEST );
200
207
} else {
201
208
pgl .disable (PGL .DEPTH_TEST );
@@ -251,6 +258,8 @@ public void setColorBuffers(Texture[] textures) {
251
258
252
259
253
260
public void setColorBuffers (Texture [] textures , int n ) {
261
+ PGraphicsOpenGL g = pg .get ();
262
+ if (g == null ) return ;
254
263
if (screenFb ) return ;
255
264
256
265
if (numColorBuffers != PApplet .min (n , textures .length )) {
@@ -262,8 +271,8 @@ public void setColorBuffers(Texture[] textures, int n) {
262
271
colorBufferTex [i ] = textures [i ];
263
272
}
264
273
265
- pg .pushFramebuffer ();
266
- pg .setFramebuffer (this );
274
+ g .pushFramebuffer ();
275
+ g .setFramebuffer (this );
267
276
268
277
// Making sure nothing is attached.
269
278
for (int i = 0 ; i < numColorBuffers ; i ++) {
@@ -279,28 +288,31 @@ public void setColorBuffers(Texture[] textures, int n) {
279
288
280
289
pgl .validateFramebuffer ();
281
290
282
- pg .popFramebuffer ();
291
+ g .popFramebuffer ();
283
292
}
284
293
285
294
286
295
public void swapColorBuffers () {
296
+ PGraphicsOpenGL g = pg .get ();
297
+ if (g == null ) return ;
298
+
287
299
for (int i = 0 ; i < numColorBuffers - 1 ; i ++) {
288
300
int i1 = (i + 1 );
289
301
Texture tmp = colorBufferTex [i ];
290
302
colorBufferTex [i ] = colorBufferTex [i1 ];
291
303
colorBufferTex [i1 ] = tmp ;
292
304
}
293
305
294
- pg .pushFramebuffer ();
295
- pg .setFramebuffer (this );
306
+ g .pushFramebuffer ();
307
+ g .setFramebuffer (this );
296
308
for (int i = 0 ; i < numColorBuffers ; i ++) {
297
309
pgl .framebufferTexture2D (PGL .FRAMEBUFFER , PGL .COLOR_ATTACHMENT0 + i ,
298
310
colorBufferTex [i ].glTarget ,
299
311
colorBufferTex [i ].glName , 0 );
300
312
}
301
313
pgl .validateFramebuffer ();
302
314
303
- pg .popFramebuffer ();
315
+ g .popFramebuffer ();
304
316
}
305
317
306
318
@@ -421,10 +433,12 @@ protected boolean contextIsOutdated() {
421
433
422
434
423
435
protected void initColorBufferMultisample () {
436
+ PGraphicsOpenGL g = pg .get ();
437
+ if (g == null ) return ;
424
438
if (screenFb ) return ;
425
439
426
- pg .pushFramebuffer ();
427
- pg .setFramebuffer (this );
440
+ g .pushFramebuffer ();
441
+ g .setFramebuffer (this );
428
442
429
443
// glMultisample = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
430
444
pgl .bindRenderbuffer (PGL .RENDERBUFFER , glMultisample );
@@ -433,19 +447,21 @@ protected void initColorBufferMultisample() {
433
447
pgl .framebufferRenderbuffer (PGL .FRAMEBUFFER , PGL .COLOR_ATTACHMENT0 ,
434
448
PGL .RENDERBUFFER , glMultisample );
435
449
436
- pg .popFramebuffer ();
450
+ g .popFramebuffer ();
437
451
}
438
452
439
453
440
454
protected void initPackedDepthStencilBuffer () {
455
+ PGraphicsOpenGL g = pg .get ();
456
+ if (g == null ) return ;
441
457
if (screenFb ) return ;
442
458
443
459
if (width == 0 || height == 0 ) {
444
460
throw new RuntimeException ("PFramebuffer: size undefined." );
445
461
}
446
462
447
- pg .pushFramebuffer ();
448
- pg .setFramebuffer (this );
463
+ g .pushFramebuffer ();
464
+ g .setFramebuffer (this );
449
465
450
466
// glDepthStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
451
467
pgl .bindRenderbuffer (PGL .RENDERBUFFER , glDepthStencil );
@@ -463,19 +479,21 @@ protected void initPackedDepthStencilBuffer() {
463
479
pgl .framebufferRenderbuffer (PGL .FRAMEBUFFER , PGL .STENCIL_ATTACHMENT ,
464
480
PGL .RENDERBUFFER , glDepthStencil );
465
481
466
- pg .popFramebuffer ();
482
+ g .popFramebuffer ();
467
483
}
468
484
469
485
470
486
protected void initDepthBuffer () {
487
+ PGraphicsOpenGL g = pg .get ();
488
+ if (g == null ) return ;
471
489
if (screenFb ) return ;
472
490
473
491
if (width == 0 || height == 0 ) {
474
492
throw new RuntimeException ("PFramebuffer: size undefined." );
475
493
}
476
494
477
- pg .pushFramebuffer ();
478
- pg .setFramebuffer (this );
495
+ g .pushFramebuffer ();
496
+ g .setFramebuffer (this );
479
497
480
498
// glDepth = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
481
499
pgl .bindRenderbuffer (PGL .RENDERBUFFER , glDepth );
@@ -499,19 +517,21 @@ protected void initDepthBuffer() {
499
517
pgl .framebufferRenderbuffer (PGL .FRAMEBUFFER , PGL .DEPTH_ATTACHMENT ,
500
518
PGL .RENDERBUFFER , glDepth );
501
519
502
- pg .popFramebuffer ();
520
+ g .popFramebuffer ();
503
521
}
504
522
505
523
506
524
protected void initStencilBuffer () {
525
+ PGraphicsOpenGL g = pg .get ();
526
+ if (g == null ) return ;
507
527
if (screenFb ) return ;
508
528
509
529
if (width == 0 || height == 0 ) {
510
530
throw new RuntimeException ("PFramebuffer: size undefined." );
511
531
}
512
532
513
- pg .pushFramebuffer ();
514
- pg .setFramebuffer (this );
533
+ g .pushFramebuffer ();
534
+ g .setFramebuffer (this );
515
535
516
536
// glStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
517
537
pgl .bindRenderbuffer (PGL .RENDERBUFFER , glStencil );
@@ -534,7 +554,7 @@ protected void initStencilBuffer() {
534
554
pgl .framebufferRenderbuffer (PGL .FRAMEBUFFER , PGL .STENCIL_ATTACHMENT ,
535
555
PGL .RENDERBUFFER , glStencil );
536
556
537
- pg .popFramebuffer ();
557
+ g .popFramebuffer ();
538
558
}
539
559
540
560
0 commit comments