26
26
27
27
import processing .android .AppComponent ;
28
28
import 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 ;
29
35
import java .lang .ref .ReferenceQueue ;
30
36
import java .lang .ref .WeakReference ;
31
37
import java .net .URL ;
32
38
import java .nio .*;
33
39
import java .util .*;
34
40
41
+ import android .content .Context ;
35
42
import android .view .SurfaceHolder ;
36
43
37
44
/**
@@ -509,8 +516,8 @@ public void dispose() {
509
516
static protected FloatBuffer floatBuffer ;
510
517
511
518
/** To save the surface contents before the activity is taken to the background. */
519
+ private String restorePixelFile ;
512
520
private int restoreCount ;
513
- private int [] restorePixels ;
514
521
515
522
// ........................................................
516
523
@@ -5709,26 +5716,35 @@ protected void drawPixels(int[] pixBuffer, int x, int y, int w, int h) {
5709
5716
5710
5717
@ Override
5711
5718
protected void saveState () {
5712
- /*
5713
5719
// Queue the pixel read operation so it is performed when the surface is ready
5714
5720
pgl .queueEvent (new Runnable () {
5715
5721
@ Override
5716
5722
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 ;
5724
5725
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
+ }
5729
5746
}
5730
5747
});
5731
- */
5732
5748
}
5733
5749
5734
5750
@@ -5739,10 +5755,9 @@ protected void restoreState() {
5739
5755
5740
5756
@ Override
5741
5757
protected void restoreSurface () {
5742
- /*
5743
5758
if (changed ) {
5744
5759
changed = false ;
5745
- if (restorePixels != null) {
5760
+ if (restorePixelFile != null ) {
5746
5761
// Set restore count to 2 so it draws the bitmap two frames after surface change, otherwise
5747
5762
// the restoration does not work because the OpenGL renderer sometimes resizes the surface
5748
5763
// twice after restoring the app to the foreground... this may be due to broken graphics
@@ -5758,12 +5773,26 @@ protected void restoreSurface() {
5758
5773
} else if (restoreCount > 0 ) {
5759
5774
restoreCount --;
5760
5775
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
+ }
5764
5794
}
5765
5795
}
5766
- */
5767
5796
}
5768
5797
5769
5798
//////////////////////////////////////////////////////////////
0 commit comments