Skip to content

Commit bb81e52

Browse files
committed
update from java mode
1 parent 5b33bbb commit bb81e52

File tree

8 files changed

+617
-446
lines changed

8 files changed

+617
-446
lines changed

core/src/processing/opengl/FontTexture.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ class FontTexture implements PConstants {
6464
protected TextureInfo[] glyphTexinfos;
6565
protected HashMap<PFont.Glyph, TextureInfo> texinfoMap;
6666

67-
6867
public FontTexture(PGraphicsOpenGL pg, PFont font, boolean is3D) {
6968
pgl = pg.pgl;
7069
this.is3D = is3D;
@@ -156,10 +155,13 @@ public boolean addTexture(PGraphicsOpenGL pg) {
156155
// REPLACE to preserve color of transparent pixels.
157156
Texture tex0 = textures[currentTex];
158157

159-
tex.pg.pushStyle();
160-
tex.pg.blendMode(REPLACE);
161-
tex.put(tex0);
162-
tex.pg.popStyle();
158+
PGraphicsOpenGL g = tex.pg.get();
159+
if (g != null) {
160+
g.pushStyle();
161+
g.blendMode(REPLACE);
162+
tex.put(tex0);
163+
g.popStyle();
164+
}
163165

164166
textures[currentTex] = tex;
165167

core/src/processing/opengl/FrameBuffer.java

+46-26
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import processing.core.PConstants;
2929
import processing.opengl.PGraphicsOpenGL.GLResourceFrameBuffer;
3030

31+
import java.lang.ref.WeakReference;
3132
import java.nio.IntBuffer;
3233

3334
/**
@@ -42,7 +43,7 @@
4243
*/
4344

4445
public class FrameBuffer implements PConstants {
45-
protected PGraphicsOpenGL pg;
46+
protected WeakReference<PGraphicsOpenGL> pg;
4647
protected PGL pgl;
4748
protected int context; // The context that created this framebuffer.
4849

@@ -72,7 +73,7 @@ public class FrameBuffer implements PConstants {
7273

7374

7475
FrameBuffer(PGraphicsOpenGL pg) {
75-
this.pg = pg;
76+
this.pg = new WeakReference<PGraphicsOpenGL>(pg);
7677
pgl = pg.pgl;
7778
context = pgl.createEmptyContext();
7879
}
@@ -152,15 +153,17 @@ public class FrameBuffer implements PConstants {
152153

153154

154155
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);
157160
pgl.clearDepth(1);
158161
pgl.clearStencil(0);
159162
pgl.clearColor(0, 0, 0, 0);
160163
pgl.clear(PGL.DEPTH_BUFFER_BIT |
161164
PGL.STENCIL_BUFFER_BIT |
162165
PGL.COLOR_BUFFER_BIT);
163-
pg.popFramebuffer();
166+
g.popFramebuffer();
164167
}
165168

166169
public void copyColor(FrameBuffer dest) {
@@ -176,12 +179,14 @@ public void copyStencil(FrameBuffer dest) {
176179
}
177180

178181
public void copy(FrameBuffer dest, int mask) {
182+
PGraphicsOpenGL g = pg.get();
183+
if (g == null) return;
179184
pgl.bindFramebufferImpl(PGL.READ_FRAMEBUFFER, this.glFbo);
180185
pgl.bindFramebufferImpl(PGL.DRAW_FRAMEBUFFER, dest.glFbo);
181186
pgl.blitFramebuffer(0, 0, this.width, this.height,
182187
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);
185190
}
186191

187192
public void bind() {
@@ -193,9 +198,11 @@ public void disableDepthTest() {
193198
}
194199

195200
public void finish() {
201+
PGraphicsOpenGL g = pg.get();
202+
if (g == null) return;
196203
if (noDepth) {
197204
// 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)) {
199206
pgl.enable(PGL.DEPTH_TEST);
200207
} else {
201208
pgl.disable(PGL.DEPTH_TEST);
@@ -251,6 +258,8 @@ public void setColorBuffers(Texture[] textures) {
251258

252259

253260
public void setColorBuffers(Texture[] textures, int n) {
261+
PGraphicsOpenGL g = pg.get();
262+
if (g == null) return;
254263
if (screenFb) return;
255264

256265
if (numColorBuffers != PApplet.min(n, textures.length)) {
@@ -262,8 +271,8 @@ public void setColorBuffers(Texture[] textures, int n) {
262271
colorBufferTex[i] = textures[i];
263272
}
264273

265-
pg.pushFramebuffer();
266-
pg.setFramebuffer(this);
274+
g.pushFramebuffer();
275+
g.setFramebuffer(this);
267276

268277
// Making sure nothing is attached.
269278
for (int i = 0; i < numColorBuffers; i++) {
@@ -279,28 +288,31 @@ public void setColorBuffers(Texture[] textures, int n) {
279288

280289
pgl.validateFramebuffer();
281290

282-
pg.popFramebuffer();
291+
g.popFramebuffer();
283292
}
284293

285294

286295
public void swapColorBuffers() {
296+
PGraphicsOpenGL g = pg.get();
297+
if (g == null) return;
298+
287299
for (int i = 0; i < numColorBuffers - 1; i++) {
288300
int i1 = (i + 1);
289301
Texture tmp = colorBufferTex[i];
290302
colorBufferTex[i] = colorBufferTex[i1];
291303
colorBufferTex[i1] = tmp;
292304
}
293305

294-
pg.pushFramebuffer();
295-
pg.setFramebuffer(this);
306+
g.pushFramebuffer();
307+
g.setFramebuffer(this);
296308
for (int i = 0; i < numColorBuffers; i++) {
297309
pgl.framebufferTexture2D(PGL.FRAMEBUFFER, PGL.COLOR_ATTACHMENT0 + i,
298310
colorBufferTex[i].glTarget,
299311
colorBufferTex[i].glName, 0);
300312
}
301313
pgl.validateFramebuffer();
302314

303-
pg.popFramebuffer();
315+
g.popFramebuffer();
304316
}
305317

306318

@@ -421,10 +433,12 @@ protected boolean contextIsOutdated() {
421433

422434

423435
protected void initColorBufferMultisample() {
436+
PGraphicsOpenGL g = pg.get();
437+
if (g == null) return;
424438
if (screenFb) return;
425439

426-
pg.pushFramebuffer();
427-
pg.setFramebuffer(this);
440+
g.pushFramebuffer();
441+
g.setFramebuffer(this);
428442

429443
// glMultisample = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
430444
pgl.bindRenderbuffer(PGL.RENDERBUFFER, glMultisample);
@@ -433,19 +447,21 @@ protected void initColorBufferMultisample() {
433447
pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.COLOR_ATTACHMENT0,
434448
PGL.RENDERBUFFER, glMultisample);
435449

436-
pg.popFramebuffer();
450+
g.popFramebuffer();
437451
}
438452

439453

440454
protected void initPackedDepthStencilBuffer() {
455+
PGraphicsOpenGL g = pg.get();
456+
if (g == null) return;
441457
if (screenFb) return;
442458

443459
if (width == 0 || height == 0) {
444460
throw new RuntimeException("PFramebuffer: size undefined.");
445461
}
446462

447-
pg.pushFramebuffer();
448-
pg.setFramebuffer(this);
463+
g.pushFramebuffer();
464+
g.setFramebuffer(this);
449465

450466
// glDepthStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
451467
pgl.bindRenderbuffer(PGL.RENDERBUFFER, glDepthStencil);
@@ -463,19 +479,21 @@ protected void initPackedDepthStencilBuffer() {
463479
pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.STENCIL_ATTACHMENT,
464480
PGL.RENDERBUFFER, glDepthStencil);
465481

466-
pg.popFramebuffer();
482+
g.popFramebuffer();
467483
}
468484

469485

470486
protected void initDepthBuffer() {
487+
PGraphicsOpenGL g = pg.get();
488+
if (g == null) return;
471489
if (screenFb) return;
472490

473491
if (width == 0 || height == 0) {
474492
throw new RuntimeException("PFramebuffer: size undefined.");
475493
}
476494

477-
pg.pushFramebuffer();
478-
pg.setFramebuffer(this);
495+
g.pushFramebuffer();
496+
g.setFramebuffer(this);
479497

480498
// glDepth = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
481499
pgl.bindRenderbuffer(PGL.RENDERBUFFER, glDepth);
@@ -499,19 +517,21 @@ protected void initDepthBuffer() {
499517
pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.DEPTH_ATTACHMENT,
500518
PGL.RENDERBUFFER, glDepth);
501519

502-
pg.popFramebuffer();
520+
g.popFramebuffer();
503521
}
504522

505523

506524
protected void initStencilBuffer() {
525+
PGraphicsOpenGL g = pg.get();
526+
if (g == null) return;
507527
if (screenFb) return;
508528

509529
if (width == 0 || height == 0) {
510530
throw new RuntimeException("PFramebuffer: size undefined.");
511531
}
512532

513-
pg.pushFramebuffer();
514-
pg.setFramebuffer(this);
533+
g.pushFramebuffer();
534+
g.setFramebuffer(this);
515535

516536
// glStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
517537
pgl.bindRenderbuffer(PGL.RENDERBUFFER, glStencil);
@@ -534,7 +554,7 @@ protected void initStencilBuffer() {
534554
pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.STENCIL_ATTACHMENT,
535555
PGL.RENDERBUFFER, glStencil);
536556

537-
pg.popFramebuffer();
557+
g.popFramebuffer();
538558
}
539559

540560

0 commit comments

Comments
 (0)