Skip to content

Commit 51ee9a3

Browse files
committed
calling glFinish() only if the current framerate falls below 50% of the
target
1 parent fb1e322 commit 51ee9a3

File tree

2 files changed

+53
-15
lines changed
  • core/src/processing/opengl
  • java/libraries/lwjgl/src/processing/lwjgl

2 files changed

+53
-15
lines changed

core/src/processing/opengl/PGL.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,11 @@ public class PGL {
416416
protected static PGLListener listener;
417417

418418
/** Desired target framerate */
419-
protected float targetFramerate = 60;
420-
protected boolean setFramerate = false;
419+
protected float targetFps = 60;
420+
protected float currentFps = 60;
421+
protected boolean setFps = false;
422+
protected int fcount, lastm;
423+
protected int fint = 3;
421424

422425
/** Which texturing targets are enabled */
423426
protected static boolean[] texturingTargets = { false, false };
@@ -557,7 +560,7 @@ public PGL(PGraphicsOpenGL pg) {
557560

558561

559562
protected void setFrameRate(float framerate) {
560-
if (targetFramerate != framerate) {
563+
if (targetFps != framerate) {
561564
if (60 < framerate) {
562565
// Disables v-sync
563566
gl.setSwapInterval(0);
@@ -566,15 +569,15 @@ protected void setFrameRate(float framerate) {
566569
} else {
567570
gl.setSwapInterval(2);
568571
}
569-
if ((60 < framerate && targetFramerate <= 60) ||
570-
(framerate <= 60 && 60 < targetFramerate)) {
572+
if ((60 < framerate && targetFps <= 60) ||
573+
(framerate <= 60 && 60 < targetFps)) {
571574
// Enabling/disabling v-sync, we force a
572575
// surface reinitialization to avoid screen
573576
// no-paint issue observed on MacOSX.
574577
pg.initialized = false;
575578
}
576-
targetFramerate = framerate;
577-
setFramerate = true;
579+
targetFps = currentFps = framerate;
580+
setFps = true;
578581
}
579582
}
580583

@@ -593,7 +596,7 @@ protected void initSurface(int antialias) {
593596
pg.parent.remove(canvasNEWT);
594597
}
595598
sinkFBO = backFBO = frontFBO = null;
596-
setFramerate = false;
599+
setFps = false;
597600
}
598601

599602
// Setting up the desired GL capabilities;
@@ -617,6 +620,9 @@ protected void initSurface(int antialias) {
617620

618621
if (toolkit == AWT) {
619622
canvasAWT = new GLCanvas(caps);
623+
624+
//canvas = new GLCanvas(caps, context);
625+
620626
canvasAWT.setBounds(0, 0, pg.width, pg.height);
621627
canvasAWT.setBackground(new Color(pg.backgroundColor, true));
622628
canvasAWT.setFocusable(true);
@@ -694,8 +700,8 @@ protected void deleteSurface() {
694700

695701

696702
protected void update() {
697-
if (!setFramerate) {
698-
setFrameRate(targetFramerate);
703+
if (!setFps) {
704+
setFrameRate(targetFps);
699705
}
700706
if (USE_JOGL_FBOLAYER) return;
701707

@@ -1138,7 +1144,6 @@ protected void beginDraw(boolean clear0) {
11381144
}
11391145
}
11401146

1141-
11421147
protected void endDraw(boolean clear0) {
11431148
if (USE_JOGL_FBOLAYER) {
11441149
if (!clear0 && isFBOBacked() && !isMultisampled()) {
@@ -1175,8 +1180,19 @@ protected void endDraw(boolean clear0) {
11751180
backTex = temp;
11761181
}
11771182
}
1178-
// flush();
1179-
finish();
1183+
1184+
// call (gl)finish() only if the rendering of each frame is taking too long,
1185+
// to make sure that commands are not accumulating in the GL command queue.
1186+
fcount += 1;
1187+
int m = pg.parent.millis();
1188+
if (m - lastm > 1000 * fint) {
1189+
currentFps = (float)(fcount) / fint;
1190+
fcount = 0;
1191+
lastm = m;
1192+
}
1193+
if (currentFps < 0.5f * targetFps) {
1194+
finish();
1195+
}
11801196
}
11811197

11821198

java/libraries/lwjgl/src/processing/lwjgl/PGL.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,13 @@ public class PGL extends processing.opengl.PGL {
359359
protected static KeyPoller keyPoller;
360360
protected static MousePoller mousePoller;
361361

362+
/** Desired target framerate */
363+
protected float targetFps = 60;
364+
protected float currentFps = 60;
365+
protected boolean setFps = false;
366+
protected int fcount, lastm;
367+
protected int fint = 3;
368+
362369
/** Which texturing targets are enabled */
363370
protected static boolean[] texturingTargets = { false, false };
364371

@@ -483,6 +490,10 @@ public PGL(PGraphicsOpenGL pg) {
483490

484491

485492
protected void setFrameRate(float framerate) {
493+
if (targetFps != framerate) {
494+
targetFps = currentFps = framerate;
495+
setFps = true;
496+
}
486497
}
487498

488499

@@ -957,8 +968,19 @@ protected void endDraw(boolean clear0) {
957968
frontTex = backTex;
958969
backTex = temp;
959970
}
960-
// flush();
961-
finish();
971+
972+
// call (gl)finish() only if the rendering of each frame is taking too long,
973+
// to make sure that commands are not accumulating in the GL command queue.
974+
fcount += 1;
975+
int m = pg.parent.millis();
976+
if (m - lastm > 1000 * fint) {
977+
currentFps = (float)(fcount) / fint;
978+
fcount = 0;
979+
lastm = m;
980+
}
981+
if (currentFps < 0.5f * targetFps) {
982+
finish();
983+
}
962984
}
963985

964986

0 commit comments

Comments
 (0)