2626
2727import processing .android .AppComponent ;
2828import processing .core .*;
29+
30+ import java .io .File ;
31+ import java .io .FileInputStream ;
32+ import java .io .FileOutputStream ;
33+ import java .io .ObjectInputStream ;
34+ import java .io .ObjectOutputStream ;
2935import java .lang .ref .ReferenceQueue ;
3036import java .lang .ref .WeakReference ;
3137import java .net .URL ;
3238import java .nio .*;
3339import java .util .*;
3440
41+ import android .content .Context ;
3542import android .view .SurfaceHolder ;
3643
3744/**
@@ -509,8 +516,8 @@ public void dispose() {
509516 static protected FloatBuffer floatBuffer ;
510517
511518 /** To save the surface contents before the activity is taken to the background. */
519+ private String restorePixelFile ;
512520 private int restoreCount ;
513- private int [] restorePixels ;
514521
515522 // ........................................................
516523
@@ -5709,26 +5716,35 @@ protected void drawPixels(int[] pixBuffer, int x, int y, int w, int h) {
57095716
57105717 @ Override
57115718 protected void saveState () {
5712- /*
57135719 // Queue the pixel read operation so it is performed when the surface is ready
57145720 pgl .queueEvent (new Runnable () {
57155721 @ Override
57165722 public void run () {
5717- restorePixels = new int[pixelWidth * pixelHeight];
5718- int[] pix = new int[pixelWidth * pixelHeight];
5719- IntBuffer buf = IntBuffer.wrap(pix);
5720- buf.position(0);
5721- beginPixelsOp(OP_READ);
5722- pgl.readPixelsImpl(0, 0, pixelWidth, pixelHeight, PGL.RGBA, PGL.UNSIGNED_BYTE, buf);
5723- endPixelsOp();
5723+ Context context = parent .getContext ();
5724+ if (context == null ) return ;
57245725 try {
5725- // Convert pixels to ARGB
5726- PGL.getIntArray(buf, restorePixels);
5727- PGL.nativeToJavaARGB(restorePixels, pixelWidth, pixelHeight);
5728- } catch (ArrayIndexOutOfBoundsException e) {}
5726+ int [] restorePixels = new int [pixelWidth * pixelHeight ];
5727+ IntBuffer buf = IntBuffer .wrap (restorePixels );
5728+ buf .position (0 );
5729+ beginPixelsOp (OP_READ );
5730+ pgl .readPixelsImpl (0 , 0 , pixelWidth , pixelHeight , PGL .RGBA , PGL .UNSIGNED_BYTE , buf );
5731+ endPixelsOp ();
5732+
5733+ File cacheDir = context .getCacheDir ();
5734+ File cacheFile = File .createTempFile ("processing" , "pixels" , cacheDir );
5735+ restorePixelFile = cacheFile .getAbsolutePath ();
5736+ FileOutputStream stream = new FileOutputStream (cacheFile );
5737+ ObjectOutputStream dout = new ObjectOutputStream (stream );
5738+ dout .writeObject (restorePixels );
5739+ dout .flush ();
5740+ stream .getFD ().sync ();
5741+ stream .close ();
5742+ } catch (Exception ex ) {
5743+ PGraphics .showWarning ("Could not save screen contents to cache" );
5744+ ex .printStackTrace ();
5745+ }
57295746 }
57305747 });
5731- */
57325748 }
57335749
57345750
@@ -5739,10 +5755,9 @@ protected void restoreState() {
57395755
57405756 @ Override
57415757 protected void restoreSurface () {
5742- /*
57435758 if (changed ) {
57445759 changed = false ;
5745- if (restorePixels != null) {
5760+ if (restorePixelFile != null ) {
57465761 // Set restore count to 2 so it draws the bitmap two frames after surface change, otherwise
57475762 // the restoration does not work because the OpenGL renderer sometimes resizes the surface
57485763 // twice after restoring the app to the foreground... this may be due to broken graphics
@@ -5758,12 +5773,26 @@ protected void restoreSurface() {
57585773 } else if (restoreCount > 0 ) {
57595774 restoreCount --;
57605775 if (restoreCount == 0 ) {
5761- // Draw and dispose pixels
5762- drawPixels(restorePixels, 0, 0, pixelWidth, pixelHeight);
5763- restorePixels = null;
5776+ Context context = parent .getContext ();
5777+ if (context == null ) return ;
5778+ try {
5779+ // Load cached pixels and draw
5780+ File cacheFile = new File (restorePixelFile );
5781+ FileInputStream inStream = new FileInputStream (cacheFile );
5782+ ObjectInputStream din = new ObjectInputStream (inStream );
5783+ int [] restorePixels = (int []) din .readObject ();
5784+ if (restorePixels .length == pixelWidth * pixelHeight ) {
5785+ PGL .nativeToJavaARGB (restorePixels , pixelWidth , pixelHeight );
5786+ drawPixels (restorePixels , 0 , 0 , pixelWidth , pixelHeight );
5787+ }
5788+ inStream .close ();
5789+ cacheFile .delete ();
5790+ } catch (Exception ex ) {
5791+ PGraphics .showWarning ("Could not restore screen contents from cache" );
5792+ ex .printStackTrace ();
5793+ }
57645794 }
57655795 }
5766- */
57675796 }
57685797
57695798 //////////////////////////////////////////////////////////////
0 commit comments