2828import processing .core .PConstants ;
2929import processing .opengl .PGraphicsOpenGL .GLResourceFrameBuffer ;
3030
31- import java .lang .ref .WeakReference ;
3231import java .nio .IntBuffer ;
3332
3433/**
4342 */
4443
4544public class FrameBuffer implements PConstants {
46- protected WeakReference < PGraphicsOpenGL > pg ;
45+ protected PGraphicsOpenGL pg ;
4746 protected PGL pgl ;
4847 protected int context ; // The context that created this framebuffer.
4948
@@ -73,7 +72,7 @@ public class FrameBuffer implements PConstants {
7372
7473
7574 FrameBuffer (PGraphicsOpenGL pg ) {
76- this .pg = new WeakReference < PGraphicsOpenGL >( pg ) ;
75+ this .pg = pg ;
7776 pgl = pg .pgl ;
7877 context = pgl .createEmptyContext ();
7978 }
@@ -153,17 +152,15 @@ public class FrameBuffer implements PConstants {
153152
154153
155154 public void clear () {
156- PGraphicsOpenGL g = pg .get ();
157- if (g == null ) return ;
158- g .pushFramebuffer ();
159- g .setFramebuffer (this );
155+ pg .pushFramebuffer ();
156+ pg .setFramebuffer (this );
160157 pgl .clearDepth (1 );
161158 pgl .clearStencil (0 );
162159 pgl .clearColor (0 , 0 , 0 , 0 );
163160 pgl .clear (PGL .DEPTH_BUFFER_BIT |
164161 PGL .STENCIL_BUFFER_BIT |
165162 PGL .COLOR_BUFFER_BIT );
166- g .popFramebuffer ();
163+ pg .popFramebuffer ();
167164 }
168165
169166 public void copyColor (FrameBuffer dest ) {
@@ -179,14 +176,12 @@ public void copyStencil(FrameBuffer dest) {
179176 }
180177
181178 public void copy (FrameBuffer dest , int mask ) {
182- PGraphicsOpenGL g = pg .get ();
183- if (g == null ) return ;
184179 pgl .bindFramebufferImpl (PGL .READ_FRAMEBUFFER , this .glFbo );
185180 pgl .bindFramebufferImpl (PGL .DRAW_FRAMEBUFFER , dest .glFbo );
186181 pgl .blitFramebuffer (0 , 0 , this .width , this .height ,
187182 0 , 0 , dest .width , dest .height , mask , PGL .NEAREST );
188- pgl .bindFramebufferImpl (PGL .READ_FRAMEBUFFER , g .getCurrentFB ().glFbo );
189- pgl .bindFramebufferImpl (PGL .DRAW_FRAMEBUFFER , g .getCurrentFB ().glFbo );
183+ pgl .bindFramebufferImpl (PGL .READ_FRAMEBUFFER , pg .getCurrentFB ().glFbo );
184+ pgl .bindFramebufferImpl (PGL .DRAW_FRAMEBUFFER , pg .getCurrentFB ().glFbo );
190185 }
191186
192187 public void bind () {
@@ -198,11 +193,9 @@ public void disableDepthTest() {
198193 }
199194
200195 public void finish () {
201- PGraphicsOpenGL g = pg .get ();
202- if (g == null ) return ;
203196 if (noDepth ) {
204197 // No need to clear depth buffer because depth testing was disabled.
205- if (g .getHint (ENABLE_DEPTH_TEST )) {
198+ if (pg .getHint (ENABLE_DEPTH_TEST )) {
206199 pgl .enable (PGL .DEPTH_TEST );
207200 } else {
208201 pgl .disable (PGL .DEPTH_TEST );
@@ -258,8 +251,6 @@ public void setColorBuffers(Texture[] textures) {
258251
259252
260253 public void setColorBuffers (Texture [] textures , int n ) {
261- PGraphicsOpenGL g = pg .get ();
262- if (g == null ) return ;
263254 if (screenFb ) return ;
264255
265256 if (numColorBuffers != PApplet .min (n , textures .length )) {
@@ -271,8 +262,8 @@ public void setColorBuffers(Texture[] textures, int n) {
271262 colorBufferTex [i ] = textures [i ];
272263 }
273264
274- g .pushFramebuffer ();
275- g .setFramebuffer (this );
265+ pg .pushFramebuffer ();
266+ pg .setFramebuffer (this );
276267
277268 // Making sure nothing is attached.
278269 for (int i = 0 ; i < numColorBuffers ; i ++) {
@@ -288,31 +279,28 @@ public void setColorBuffers(Texture[] textures, int n) {
288279
289280 pgl .validateFramebuffer ();
290281
291- g .popFramebuffer ();
282+ pg .popFramebuffer ();
292283 }
293284
294285
295286 public void swapColorBuffers () {
296- PGraphicsOpenGL g = pg .get ();
297- if (g == null ) return ;
298-
299287 for (int i = 0 ; i < numColorBuffers - 1 ; i ++) {
300288 int i1 = (i + 1 );
301289 Texture tmp = colorBufferTex [i ];
302290 colorBufferTex [i ] = colorBufferTex [i1 ];
303291 colorBufferTex [i1 ] = tmp ;
304292 }
305293
306- g .pushFramebuffer ();
307- g .setFramebuffer (this );
294+ pg .pushFramebuffer ();
295+ pg .setFramebuffer (this );
308296 for (int i = 0 ; i < numColorBuffers ; i ++) {
309297 pgl .framebufferTexture2D (PGL .FRAMEBUFFER , PGL .COLOR_ATTACHMENT0 + i ,
310298 colorBufferTex [i ].glTarget ,
311299 colorBufferTex [i ].glName , 0 );
312300 }
313301 pgl .validateFramebuffer ();
314302
315- g .popFramebuffer ();
303+ pg .popFramebuffer ();
316304 }
317305
318306
@@ -433,12 +421,10 @@ protected boolean contextIsOutdated() {
433421
434422
435423 protected void initColorBufferMultisample () {
436- PGraphicsOpenGL g = pg .get ();
437- if (g == null ) return ;
438424 if (screenFb ) return ;
439425
440- g .pushFramebuffer ();
441- g .setFramebuffer (this );
426+ pg .pushFramebuffer ();
427+ pg .setFramebuffer (this );
442428
443429// glMultisample = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
444430 pgl .bindRenderbuffer (PGL .RENDERBUFFER , glMultisample );
@@ -447,21 +433,19 @@ protected void initColorBufferMultisample() {
447433 pgl .framebufferRenderbuffer (PGL .FRAMEBUFFER , PGL .COLOR_ATTACHMENT0 ,
448434 PGL .RENDERBUFFER , glMultisample );
449435
450- g .popFramebuffer ();
436+ pg .popFramebuffer ();
451437 }
452438
453439
454440 protected void initPackedDepthStencilBuffer () {
455- PGraphicsOpenGL g = pg .get ();
456- if (g == null ) return ;
457441 if (screenFb ) return ;
458442
459443 if (width == 0 || height == 0 ) {
460444 throw new RuntimeException ("PFramebuffer: size undefined." );
461445 }
462446
463- g .pushFramebuffer ();
464- g .setFramebuffer (this );
447+ pg .pushFramebuffer ();
448+ pg .setFramebuffer (this );
465449
466450// glDepthStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
467451 pgl .bindRenderbuffer (PGL .RENDERBUFFER , glDepthStencil );
@@ -479,21 +463,19 @@ protected void initPackedDepthStencilBuffer() {
479463 pgl .framebufferRenderbuffer (PGL .FRAMEBUFFER , PGL .STENCIL_ATTACHMENT ,
480464 PGL .RENDERBUFFER , glDepthStencil );
481465
482- g .popFramebuffer ();
466+ pg .popFramebuffer ();
483467 }
484468
485469
486470 protected void initDepthBuffer () {
487- PGraphicsOpenGL g = pg .get ();
488- if (g == null ) return ;
489471 if (screenFb ) return ;
490472
491473 if (width == 0 || height == 0 ) {
492474 throw new RuntimeException ("PFramebuffer: size undefined." );
493475 }
494476
495- g .pushFramebuffer ();
496- g .setFramebuffer (this );
477+ pg .pushFramebuffer ();
478+ pg .setFramebuffer (this );
497479
498480// glDepth = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
499481 pgl .bindRenderbuffer (PGL .RENDERBUFFER , glDepth );
@@ -517,21 +499,19 @@ protected void initDepthBuffer() {
517499 pgl .framebufferRenderbuffer (PGL .FRAMEBUFFER , PGL .DEPTH_ATTACHMENT ,
518500 PGL .RENDERBUFFER , glDepth );
519501
520- g .popFramebuffer ();
502+ pg .popFramebuffer ();
521503 }
522504
523505
524506 protected void initStencilBuffer () {
525- PGraphicsOpenGL g = pg .get ();
526- if (g == null ) return ;
527507 if (screenFb ) return ;
528508
529509 if (width == 0 || height == 0 ) {
530510 throw new RuntimeException ("PFramebuffer: size undefined." );
531511 }
532512
533- g .pushFramebuffer ();
534- g .setFramebuffer (this );
513+ pg .pushFramebuffer ();
514+ pg .setFramebuffer (this );
535515
536516// glStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
537517 pgl .bindRenderbuffer (PGL .RENDERBUFFER , glStencil );
@@ -554,7 +534,7 @@ protected void initStencilBuffer() {
554534 pgl .framebufferRenderbuffer (PGL .FRAMEBUFFER , PGL .STENCIL_ATTACHMENT ,
555535 PGL .RENDERBUFFER , glStencil );
556536
557- g .popFramebuffer ();
537+ pg .popFramebuffer ();
558538 }
559539
560540
0 commit comments