Skip to content

Commit 4f658d1

Browse files
committed
figuring out correct order of saving/restoring state, particularly for wallpapers
1 parent fe19c3e commit 4f658d1

File tree

9 files changed

+104
-80
lines changed

9 files changed

+104
-80
lines changed

core/src/processing/a2d/PGraphicsAndroid2D.java

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import processing.core.PShapeSVG;
4545
import processing.core.PSurface;
4646
import processing.data.XML;
47-
import processing.opengl.PGL;
4847

4948
import android.annotation.SuppressLint;
5049
import android.app.Activity;
@@ -129,8 +128,9 @@ public class PGraphicsAndroid2D extends PGraphics {
129128
//////////////////////////////////////////////////////////////
130129

131130
/** To save the surface contents before the activity is taken to the background. */
131+
private String restoreFilename;
132+
private int restoreWidth, restoreHeight;
132133
private int restoreCount;
133-
private String restoreBitmapFile;
134134

135135
//////////////////////////////////////////////////////////////
136136

@@ -170,14 +170,9 @@ public void surfaceChanged() {
170170

171171
@Override
172172
public void setSize(int iwidth, int iheight) {
173-
// boolean wbool = iwidth != width;
174-
// boolean hbool = iheight != height;
175-
sized = iwidth != width || iheight != height;
176-
// sized = wbool || hbool;
177-
// if (iwidth != width) sized = true;
178-
// else if (iheight != height) sized = true;
179-
// else sized = false;
180-
// System.out.println("---------------> " + sized + " " + width + " " + iwidth + " "+ height + " " +iheight);
173+
// OR with prev value in case setSize() gets called twice before the renderer has the chance to resize
174+
sized |= iwidth != width || iheight != height;
175+
System.out.println("======================> RESIZING AT FRAME " + parent.frameCount + " FROM " + width + "x" + height + " to " + iwidth + "x" + iheight);
181176
super.setSize(iwidth, iheight);
182177
}
183178

@@ -198,22 +193,6 @@ public PSurface createSurface(AppComponent component, SurfaceHolder holder, bool
198193

199194
// FRAME
200195

201-
/*
202-
public void requestDraw() {
203-
parent.surfaceView.requestRender();
204-
}
205-
*/
206-
207-
// public boolean canDraw() {
208-
// return true;
209-
// }
210-
211-
212-
// @Override
213-
// public void requestDraw() {
214-
//parent.handleDraw();
215-
// }
216-
217196
@SuppressLint("NewApi")
218197
protected Canvas checkCanvas() {
219198
if ((canvas == null || sized) && (useBitmap || !primaryGraphics)) {
@@ -2088,16 +2067,23 @@ public void resize(int wide, int high) {
20882067

20892068
@Override
20902069
protected void saveState() {
2070+
restoreWidth = pixelWidth;
2071+
restoreHeight = pixelHeight;
2072+
if (restoreWidth == 0 || restoreHeight == 0) return; // maybe still initializing...
2073+
20912074
Context context = parent.getContext();
20922075
if (context == null || bitmap == null) return;
20932076
try {
2077+
// Saving current width and height to avoid restoring the screen after a screen rotation
2078+
System.out.println("============================> SAVING SCREEN FROM " + restoreWidth + " " + pixelHeight);
2079+
20942080
int size = bitmap.getHeight() * bitmap.getRowBytes();
20952081
ByteBuffer restoreBitmap = ByteBuffer.allocate(size);
20962082
bitmap.copyPixelsToBuffer(restoreBitmap);
20972083

20982084
File cacheDir = context.getCacheDir();
20992085
File cacheFile = File.createTempFile("processing", "pixels", cacheDir);
2100-
restoreBitmapFile = cacheFile.getAbsolutePath();
2086+
restoreFilename = cacheFile.getAbsolutePath();
21012087
FileOutputStream stream = new FileOutputStream(cacheFile);
21022088
ObjectOutputStream dout = new ObjectOutputStream(stream);
21032089
byte[] array = new byte[size];
@@ -2123,18 +2109,20 @@ protected void restoreState() {
21232109
protected void restoreSurface() {
21242110
if (changed) {
21252111
changed = false;
2126-
if (restoreBitmapFile != null) {
2112+
if (restoreFilename != null && restoreWidth == pixelWidth && restoreHeight == pixelHeight) {
21272113
// Set the counter to 1 so the restore bitmap is drawn in the next frame.
21282114
restoreCount = 1;
21292115
}
21302116
} else if (restoreCount > 0) {
21312117
restoreCount--;
21322118
if (restoreCount == 0) {
2119+
System.out.println("============================> RESTORING SCREEN TO " + restoreWidth + " " + pixelHeight);
2120+
21332121
Context context = parent.getContext();
21342122
if (context == null) return;
21352123
try {
21362124
// Load cached bitmap and draw
2137-
File cacheFile = new File(restoreBitmapFile);
2125+
File cacheFile = new File(restoreFilename);
21382126
FileInputStream inStream = new FileInputStream(cacheFile);
21392127
ObjectInputStream din = new ObjectInputStream(inStream);
21402128
byte[] array = (byte[]) din.readObject();
@@ -2145,6 +2133,9 @@ protected void restoreSurface() {
21452133
}
21462134
inStream.close();
21472135
cacheFile.delete();
2136+
restoreFilename = null;
2137+
restoreWidth = -1;
2138+
restoreHeight = -1;
21482139
} catch (Exception ex) {
21492140
PGraphics.showWarning("Could not restore screen contents from cache");
21502141
ex.printStackTrace();

core/src/processing/a2d/PSurfaceAndroid2D.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,13 @@ public void surfaceChanged(SurfaceHolder holder, int format, int iwidth, int ihe
123123
System.out.println("SketchSurfaceView.surfaceChanged() " + iwidth + " " + iheight);
124124
}
125125

126+
// sketch.surfaceChanged();
127+
// graphics.surfaceChanged();
128+
//
129+
// sketch.setSize(iwidth, iheight);
130+
// graphics.setSize(sketch.sketchWidth(), sketch.sketchHeight());
126131
sketch.surfaceChanged();
127-
graphics.surfaceChanged();
128-
129132
sketch.setSize(iwidth, iheight);
130-
graphics.setSize(sketch.sketchWidth(), sketch.sketchHeight());
131133
}
132134

133135
@Override

core/src/processing/android/PWallpaper.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,18 @@ public void onSurfaceCreated(SurfaceHolder surfaceHolder) {
154154
@Override
155155
public void onSurfaceChanged(final SurfaceHolder holder, final int format,
156156
final int width, final int height) {
157-
if (sketch != null) {
158-
sketch.g.setSize(width, height);
159-
}
157+
// if (sketch != null) {
158+
//// sketch.g.setSize(width, height);
159+
// sketch.surfaceChanged();
160+
// sketch.setSize(width, height);
161+
// }
162+
163+
// When the surface of a live wallpaper changes (eg: after a screen rotation) the same sketch
164+
// continues to run (unlike the case of regular apps, where its re-created) so we need to
165+
// force a reset of the renderer so the backing FBOs (in the case of the OpenGL renderers)
166+
// get reinitalized with the correct size.
167+
sketch.g.reset();
168+
System.out.println("===================> ON SURFACE CHANGED!!!!!!!!!!!!!!!");
160169
super.onSurfaceChanged(holder, format, width, height);
161170
}
162171

core/src/processing/android/PWatchFaceCanvas.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,12 @@ public void onVisibilityChanged(boolean visible) {
231231
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
232232
super.onSurfaceChanged(holder, format, width, height);
233233
if (sketch != null) {
234-
sketch.displayWidth = width;
235-
sketch.displayHeight = height;
236-
sketch.g.setSize(sketch.sketchWidth(), sketch.sketchHeight());
234+
// sketch.displayWidth = width;
235+
// sketch.displayHeight = height;
236+
// sketch.g.setSize(sketch.sketchWidth(), sketch.sketchHeight());
237+
// sketch.surfaceChanged();
237238
sketch.surfaceChanged();
239+
sketch.setSize(width, height);
238240
}
239241
}
240242

core/src/processing/android/PWatchFaceGLES.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,12 @@ public void onGlContextCreated() {
191191
public void onGlSurfaceCreated(int width, int height) {
192192
super.onGlSurfaceCreated(width, height);
193193
if (sketch != null) {
194-
sketch.displayWidth = width;
195-
sketch.displayHeight = height;
196-
sketch.g.setSize(sketch.sketchWidth(), sketch.sketchHeight());
194+
// sketch.displayWidth = width;
195+
// sketch.displayHeight = height;
196+
// sketch.g.setSize(sketch.sketchWidth(), sketch.sketchHeight());
197+
// sketch.surfaceChanged();
197198
sketch.surfaceChanged();
199+
sketch.setSize(width, height);
198200
}
199201
}
200202

core/src/processing/core/PApplet.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,11 @@ public void run() {
529529
// 4.4 and higher. Integer instead of constants defined in View so it can
530530
// build with SDK < 4.4
531531
visibility = 256 | // View.SYSTEM_UI_FLAG_LAYOUT_STABLE
532-
512 | // View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
533-
1024 | // View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
534-
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
535-
4 | // View.SYSTEM_UI_FLAG_FULLSCREEN
536-
4096; // View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
532+
512 | // View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
533+
1024 | // View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
534+
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
535+
4 | // View.SYSTEM_UI_FLAG_FULLSCREEN
536+
4096; // View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
537537
// However, this visibility does not fix a bug where the navigation area
538538
// turns black after resuming the app:
539539
// https://code.google.com/p/android/issues/detail?id=170752
@@ -820,6 +820,7 @@ final public int sketchPixelDensity() {
820820

821821
public void surfaceChanged() {
822822
surfaceChanged = true;
823+
g.surfaceChanged();
823824
}
824825

825826

@@ -1333,6 +1334,7 @@ public void setSize(int width, int height) {
13331334
this.height = height;
13341335
pixelWidth = width * pixelDensity;
13351336
pixelHeight = height * pixelDensity;
1337+
g.setSize(sketchWidth(), sketchHeight());
13361338
}
13371339

13381340

core/src/processing/core/PGraphics.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,12 @@ public void setFrameRate(float framerate) { // ignore
688688

689689

690690
public void surfaceChanged() { // ignore
691-
}
691+
}
692+
693+
694+
public void reset() { // ignore
695+
}
696+
692697

693698
/**
694699
* The final step in setting up a renderer, set its size of this renderer.

core/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,8 @@ public void dispose() {
516516
static protected FloatBuffer floatBuffer;
517517

518518
/** To save the surface contents before the activity is taken to the background. */
519-
private String restorePixelFile;
519+
private String restoreFilename;
520+
private int restoreWidth, restoreHeight;
520521
private int restoreCount;
521522

522523
// ........................................................
@@ -660,9 +661,18 @@ public void surfaceChanged() {
660661
}
661662

662663

664+
@Override
665+
public void reset() {
666+
pgl.resetFBOLayer();
667+
restartPGL();
668+
}
669+
670+
663671
@Override
664672
public void setSize(int iwidth, int iheight) {
665-
sized = iwidth != width || iheight != height;
673+
// OR with prev value in case setSize() gets called twice before the renderer has the chance to resize
674+
sized |= iwidth != width || iheight != height;
675+
System.out.println("======================> RESIZING AT FRAME " + parent.frameCount + " FROM " + width + "x" + height + " to " + iwidth + "x" + iheight);
666676
super.setSize(iwidth, iheight);
667677

668678
updatePixelSize();
@@ -758,27 +768,6 @@ public void setFrameRate(float frameRate) {
758768
}
759769

760770

761-
// @Override
762-
// Android only
763-
// public boolean canDraw() {
764-
// return pgl.canDraw();
765-
// }
766-
767-
768-
// @Override
769-
// Android only
770-
// public void requestDraw() {
771-
// if (primaryGraphics) {
772-
// if (initialized) {
773-
// if (sized) pgl.reinitSurface();
774-
// if (parent.canDraw()) pgl.requestDraw();
775-
// } else {
776-
// initPrimary();
777-
// }
778-
// }
779-
// }
780-
781-
782771
public boolean saveImpl(String filename) {
783772
return super.save(filename); // ASYNC save frame using PBOs not yet available on Android
784773

@@ -1447,9 +1436,7 @@ protected boolean pointBuffersContextIsOutdated() {
14471436
@Override
14481437
public void beginDraw() {
14491438
if (primaryGraphics) {
1450-
if (!initialized) {
1451-
initPrimary();
1452-
}
1439+
initPrimary();
14531440
setCurrentPG(this);
14541441
} else {
14551442
pgl.getGL(getPrimaryPGL());
@@ -5716,14 +5703,29 @@ protected void drawPixels(int[] pixBuffer, int x, int y, int w, int h) {
57165703

57175704
@Override
57185705
protected void saveState() {
5706+
// Saving current width and height to avoid restoring the screen after a screen rotation
5707+
restoreWidth = pixelWidth;
5708+
restoreHeight = pixelHeight;
5709+
if (restoreWidth == 0 || restoreHeight == 0) return; // maybe still initializing...
5710+
57195711
// Queue the pixel read operation so it is performed when the surface is ready
57205712
pgl.queueEvent(new Runnable() {
57215713
@Override
57225714
public void run() {
57235715
Context context = parent.getContext();
57245716
if (context == null) return;
57255717
try {
5726-
int[] restorePixels = new int[pixelWidth * pixelHeight];
5718+
if (restoreWidth != pixelWidth && restoreHeight != pixelHeight) {
5719+
// The screen size changed between calling saveState() and the pixel read operation,
5720+
// so it does no longer makes sense to try saving the screen's contents.
5721+
restoreWidth = -1;
5722+
restoreHeight = -1;
5723+
return;
5724+
}
5725+
5726+
System.out.println("============================> SAVING SCREEN FROM " + restoreWidth + " " + pixelHeight);
5727+
5728+
int[] restorePixels = new int[restoreWidth * restoreHeight];
57275729
IntBuffer buf = IntBuffer.wrap(restorePixels);
57285730
buf.position(0);
57295731
beginPixelsOp(OP_READ);
@@ -5732,7 +5734,7 @@ public void run() {
57325734

57335735
File cacheDir = context.getCacheDir();
57345736
File cacheFile = File.createTempFile("processing", "pixels", cacheDir);
5735-
restorePixelFile = cacheFile.getAbsolutePath();
5737+
restoreFilename = cacheFile.getAbsolutePath();
57365738
FileOutputStream stream = new FileOutputStream(cacheFile);
57375739
ObjectOutputStream dout = new ObjectOutputStream(stream);
57385740
dout.writeObject(restorePixels);
@@ -5757,7 +5759,7 @@ protected void restoreState() {
57575759
protected void restoreSurface() {
57585760
if (changed) {
57595761
changed = false;
5760-
if (restorePixelFile != null) {
5762+
if (restoreFilename != null && restoreWidth == pixelWidth && restoreHeight == pixelHeight) {
57615763
// Set restore count to 2 so it draws the bitmap two frames after surface change, otherwise
57625764
// the restoration does not work because the OpenGL renderer sometimes resizes the surface
57635765
// twice after restoring the app to the foreground... this may be due to broken graphics
@@ -5773,11 +5775,13 @@ protected void restoreSurface() {
57735775
} else if (restoreCount > 0) {
57745776
restoreCount--;
57755777
if (restoreCount == 0) {
5778+
System.out.println("============================> RESTORING SCREEN TO " + restoreWidth + " " + pixelHeight);
5779+
57765780
Context context = parent.getContext();
57775781
if (context == null) return;
57785782
try {
57795783
// Load cached pixels and draw
5780-
File cacheFile = new File(restorePixelFile);
5784+
File cacheFile = new File(restoreFilename);
57815785
FileInputStream inStream = new FileInputStream(cacheFile);
57825786
ObjectInputStream din = new ObjectInputStream(inStream);
57835787
int[] restorePixels = (int[]) din.readObject();
@@ -5787,6 +5791,9 @@ protected void restoreSurface() {
57875791
}
57885792
inStream.close();
57895793
cacheFile.delete();
5794+
restoreFilename = null;
5795+
restoreWidth = -1;
5796+
restoreHeight = -1;
57905797
} catch (Exception ex) {
57915798
PGraphics.showWarning("Could not restore screen contents from cache");
57925799
ex.printStackTrace();
@@ -6832,6 +6839,8 @@ public void resize(int wide, int high) {
68326839

68336840

68346841
protected void initPrimary() {
6842+
if (initialized) return;
6843+
System.out.println("===================> INTITALIZING PRIMARY SURFACE AT " + width + "x" + height);
68356844
pgl.initSurface(smooth);
68366845
if (texture != null) {
68376846
removeCache(this);

0 commit comments

Comments
 (0)