Skip to content

Commit 55f56aa

Browse files
committed
handle resuming in no-loop state
1 parent b64acb2 commit 55f56aa

File tree

4 files changed

+100
-56
lines changed

4 files changed

+100
-56
lines changed

core/src/processing/a2d/PGraphicsAndroid2D.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,6 @@ public class PGraphicsAndroid2D extends PGraphics {
127127

128128
//////////////////////////////////////////////////////////////
129129

130-
/** To save the surface contents before the activity is taken to the background. */
131-
private String restoreFilename;
132-
private int restoreWidth, restoreHeight;
133-
private int restoreCount;
134-
135-
//////////////////////////////////////////////////////////////
136-
137130
// INTERNAL
138131

139132

@@ -2064,6 +2057,8 @@ public void resize(int wide, int high) {
20642057

20652058
@Override
20662059
protected void saveState() {
2060+
super.saveState();
2061+
20672062
Context context = parent.getContext();
20682063
if (context == null || bitmap == null || parent.getSurface().getComponent().isService()) return;
20692064
try {
@@ -2094,11 +2089,6 @@ protected void saveState() {
20942089
}
20952090

20962091

2097-
@Override
2098-
protected void restoreState() {
2099-
}
2100-
2101-
21022092
@Override
21032093
protected void restoreSurface() {
21042094
if (changed) {
@@ -2125,15 +2115,18 @@ protected void restoreSurface() {
21252115
}
21262116
inStream.close();
21272117
cacheFile.delete();
2128-
restoreFilename = null;
2129-
restoreWidth = -1;
2130-
restoreHeight = -1;
21312118
} catch (Exception ex) {
21322119
PGraphics.showWarning("Could not restore screen contents from cache");
21332120
ex.printStackTrace();
2121+
} finally {
2122+
restoreFilename = null;
2123+
restoreWidth = -1;
2124+
restoreHeight = -1;
2125+
restoredSurface = true;
21342126
}
21352127
}
21362128
}
2129+
super.restoreSurface();
21372130
}
21382131

21392132

core/src/processing/core/PApplet.java

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -558,27 +558,25 @@ public void onResume() {
558558
setFullScreenVisibility();
559559
}
560560

561-
if (g != null) {
562-
g.restoreState();
563-
}
564-
565561
handleMethods("resume");
566562

563+
// Don't call resume() when the app is starting and setup() has not been called yet:
564+
// https://github.com/processing/processing-android/issues/274
565+
// Also, there is no need to call resume() from anywhere else (for example, from
566+
// onStart) since onResume() is always called in the activity lifecyle:
567+
// https://developer.android.com/guide/components/activities/activity-lifecycle.html
567568
if (0 < frameCount) {
568-
// Don't call resume() when the app is starting and setup() has not been
569-
// called yet
570-
// https://github.com/processing/processing-android/issues/274
571-
572-
// Also, no need to call resume() from anywhere else (for example, from
573-
// onStart) since onResume() is always called in the activity lifecyle:
574-
// https://developer.android.com/guide/components/activities/activity-lifecycle.html
575569
resume();
576570
}
577571

578-
// Set the default to true to handle the situation where a fragment is popping back
579-
// after pressing back (app does not exit)
572+
// Set the handledBackPressed to true to handle the situation where a fragment is popping
573+
// right back after pressing the back button (the sketch does not exit).
580574
handledBackPressed = true;
581575

576+
if (g != null) {
577+
g.restoreState();
578+
}
579+
582580
surface.resumeThread();
583581
}
584582

@@ -1838,22 +1836,10 @@ public void handleDraw() {
18381836
// recorder.beginDraw();
18391837
// }
18401838

1841-
if (requestedNoLoop) {
1842-
// noLoop() was called sometime in the previous frame with a GL renderer, but only now
1843-
// we are sure that the frame is properly displayed.
1844-
looping = false;
1845-
// Perform a full frame draw, to ensure that the previous frame is properly displayed (see
1846-
// comment in the declaration of requestedNoLoop).
1847-
g.beginDraw();
1848-
g.endDraw();
1849-
requestedNoLoop = false;
1850-
insideDraw = false;
1851-
return;
1852-
}
1839+
if (handleSpecialDraw()) return;
18531840

18541841
g.beginDraw();
18551842

1856-
18571843
long now = System.nanoTime();
18581844

18591845
if (frameCount == 0) {
@@ -1909,6 +1895,41 @@ public void handleDraw() {
19091895
}
19101896

19111897

1898+
// This method handles some special situations on Android where beginDraw/endDraw are needed,
1899+
// but not to render the actualy contents of draw(). In general, this situations arise from
1900+
// having to refresh/restore the screen after requesting no loop, or resuming the sketch in
1901+
// no-loop state.
1902+
protected boolean handleSpecialDraw() {
1903+
boolean handled = false;
1904+
1905+
if (g.restoringState()) {
1906+
// The sketch is restoring, so begin/end the frame properly and quit drawing.
1907+
g.beginDraw();
1908+
g.endDraw();
1909+
1910+
handled = true;
1911+
} else if (requestedNoLoop) {
1912+
// noLoop() was called sometime in the previous frame with a GL renderer, but only now
1913+
// we are sure that the frame is properly displayed.
1914+
looping = false;
1915+
1916+
// Perform a full frame draw, to ensure that the previous frame is properly displayed (see
1917+
// comment in the declaration of requestedNoLoop).
1918+
g.beginDraw();
1919+
g.endDraw();
1920+
1921+
requestedNoLoop = false;
1922+
handled = true;
1923+
}
1924+
1925+
if (handled) {
1926+
insideDraw = false;
1927+
return true;
1928+
} else {
1929+
return false;
1930+
}
1931+
}
1932+
19121933
//////////////////////////////////////////////////////////////
19131934

19141935

core/src/processing/core/PGraphics.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,14 @@ public class PGraphics extends PImage implements PConstants {
640640
/// Number of V steps (aka "phi") along latitudinally top-to-bottom spanning pi
641641
public int sphereDetailV = 0;
642642

643+
// ........................................................
644+
645+
// Variables used to save the surface contents before the activity is taken to the background.
646+
protected String restoreFilename;
647+
protected int restoreWidth, restoreHeight;
648+
protected int restoreCount;
649+
protected boolean restartedLoopingAfterResume = false;
650+
protected boolean restoredSurface = false;
643651

644652
//////////////////////////////////////////////////////////////
645653

@@ -969,17 +977,44 @@ protected void reapplySettings() { // ignore
969977
reapplySettings = false;
970978
}
971979

980+
//////////////////////////////////////////////////////////////
981+
982+
// RENDERER STATE
972983

973-
protected void saveState() { // ignore
974984

985+
protected void saveState() { // ignore
986+
// Nothing to do here, it depends on the renderer's implementation.
975987
}
976988

977989
protected void restoreState() { // ignore
990+
// This method probably does not need to be re-implemented in the subclasses. All we need to
991+
// do is to check for the resume in no-loop state situation:
992+
restoredSurface = false;
993+
if (!parent.isLooping()) {
994+
// The sketch needs to draw a few frames after resuming so it has the chance to restore the
995+
// screen contents:
996+
// https://github.com/processing/processing-android/issues/492
997+
// so we restart looping:
998+
parent.loop();
999+
// and flag this situation when the surface has been restored:
1000+
restartedLoopingAfterResume = true;
1001+
}
1002+
}
1003+
9781004

1005+
protected boolean restoringState() { // ignore
1006+
return !restoredSurface && restartedLoopingAfterResume;
9791007
}
9801008

981-
protected void restoreSurface() { // ignore
9821009

1010+
protected void restoreSurface() { // ignore
1011+
// When implementing this method in a subclass of PGraphics, it should add a call to the super
1012+
// implementation, to make sure that the looping is stopped in the case where the sketch was
1013+
// resumed in no-loop state (see comment in restoreState() method above).
1014+
if (restoredSurface && restartedLoopingAfterResume) {
1015+
restartedLoopingAfterResume = false;
1016+
parent.noLoop();
1017+
}
9831018
}
9841019

9851020
//////////////////////////////////////////////////////////////

core/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -515,11 +515,6 @@ public void dispose() {
515515
static protected IntBuffer intBuffer;
516516
static protected FloatBuffer floatBuffer;
517517

518-
/** To save the surface contents before the activity is taken to the background. */
519-
private String restoreFilename;
520-
private int restoreWidth, restoreHeight;
521-
private int restoreCount;
522-
523518
// ........................................................
524519

525520
// Error strings:
@@ -5701,6 +5696,8 @@ protected void drawPixels(int[] pixBuffer, int x, int y, int w, int h) {
57015696

57025697
@Override
57035698
protected void saveState() {
5699+
super.saveState();
5700+
57045701
// Queue the pixel read operation so it is performed when the surface is ready
57055702
pgl.queueEvent(new Runnable() {
57065703
@Override
@@ -5736,11 +5733,6 @@ public void run() {
57365733
}
57375734

57385735

5739-
@Override
5740-
protected void restoreState() {
5741-
}
5742-
5743-
57445736
@Override
57455737
protected void restoreSurface() {
57465738
if (changed) {
@@ -5775,15 +5767,18 @@ protected void restoreSurface() {
57755767
}
57765768
inStream.close();
57775769
cacheFile.delete();
5778-
restoreFilename = null;
5779-
restoreWidth = -1;
5780-
restoreHeight = -1;
57815770
} catch (Exception ex) {
57825771
PGraphics.showWarning("Could not restore screen contents from cache");
57835772
ex.printStackTrace();
5773+
} finally {
5774+
restoreFilename = null;
5775+
restoreWidth = -1;
5776+
restoreHeight = -1;
5777+
restoredSurface = true;
57845778
}
57855779
}
57865780
}
5781+
super.restoreSurface();
57875782
}
57885783

57895784
//////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)