@@ -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