23
23
24
24
package processing .a2d ;
25
25
26
+ import java .io .File ;
27
+ import java .io .FileInputStream ;
28
+ import java .io .FileOutputStream ;
29
+ import java .io .ObjectInputStream ;
30
+ import java .io .ObjectOutputStream ;
26
31
import java .nio .ByteBuffer ;
27
32
import java .io .InputStream ;
28
33
import java .util .zip .GZIPInputStream ;
39
44
import processing .core .PShapeSVG ;
40
45
import processing .core .PSurface ;
41
46
import processing .data .XML ;
47
+ import processing .opengl .PGL ;
42
48
43
49
import android .annotation .SuppressLint ;
44
50
import android .app .Activity ;
45
51
import android .app .ActivityManager ;
46
52
import android .app .ActivityManager .MemoryInfo ;
53
+ import android .content .Context ;
47
54
import android .graphics .*;
48
55
import android .graphics .Bitmap .Config ;
49
56
import android .graphics .Paint .Style ;
@@ -123,7 +130,7 @@ public class PGraphicsAndroid2D extends PGraphics {
123
130
124
131
/** To save the surface contents before the activity is taken to the background. */
125
132
private int restoreCount ;
126
- private ByteBuffer restoreBitmap ;
133
+ private String restoreBitmapFile ;
127
134
128
135
//////////////////////////////////////////////////////////////
129
136
@@ -163,7 +170,14 @@ public void surfaceChanged() {
163
170
164
171
@ Override
165
172
public void setSize (int iwidth , int iheight ) {
173
+ // boolean wbool = iwidth != width;
174
+ // boolean hbool = iheight != height;
166
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);
167
181
super .setSize (iwidth , iheight );
168
182
}
169
183
@@ -253,6 +267,7 @@ public void endDraw() {
253
267
try {
254
268
holder .unlockCanvasAndPost (screen );
255
269
} catch (IllegalStateException ex ) {
270
+ } catch (IllegalArgumentException ex ) {
256
271
}
257
272
}
258
273
}
@@ -2073,10 +2088,28 @@ public void resize(int wide, int high) {
2073
2088
2074
2089
@ Override
2075
2090
protected void saveState () {
2076
- if (bitmap != null ) {
2091
+ Context context = parent .getContext ();
2092
+ if (context == null || bitmap == null ) return ;
2093
+ try {
2077
2094
int size = bitmap .getHeight () * bitmap .getRowBytes ();
2078
- restoreBitmap = ByteBuffer .allocate (size );
2095
+ ByteBuffer restoreBitmap = ByteBuffer .allocate (size );
2079
2096
bitmap .copyPixelsToBuffer (restoreBitmap );
2097
+
2098
+ File cacheDir = context .getCacheDir ();
2099
+ File cacheFile = File .createTempFile ("processing" , "pixels" , cacheDir );
2100
+ restoreBitmapFile = cacheFile .getAbsolutePath ();
2101
+ FileOutputStream stream = new FileOutputStream (cacheFile );
2102
+ ObjectOutputStream dout = new ObjectOutputStream (stream );
2103
+ byte [] array = new byte [size ];
2104
+ restoreBitmap .rewind ();
2105
+ restoreBitmap .get (array );
2106
+ dout .writeObject (array );
2107
+ dout .flush ();
2108
+ stream .getFD ().sync ();
2109
+ stream .close ();
2110
+ } catch (Exception ex ) {
2111
+ PGraphics .showWarning ("Could not save screen contents to cache" );
2112
+ ex .printStackTrace ();
2080
2113
}
2081
2114
}
2082
2115
@@ -2090,17 +2123,32 @@ protected void restoreState() {
2090
2123
protected void restoreSurface () {
2091
2124
if (changed ) {
2092
2125
changed = false ;
2093
- if (restoreBitmap != null ) {
2126
+ if (restoreBitmapFile != null ) {
2094
2127
// Set the counter to 1 so the restore bitmap is drawn in the next frame.
2095
2128
restoreCount = 1 ;
2096
2129
}
2097
2130
} else if (restoreCount > 0 ) {
2098
2131
restoreCount --;
2099
2132
if (restoreCount == 0 ) {
2100
- // Draw and dispose bitmap
2101
- restoreBitmap .rewind ();
2102
- bitmap .copyPixelsFromBuffer (restoreBitmap );
2103
- restoreBitmap = null ;
2133
+ Context context = parent .getContext ();
2134
+ if (context == null ) return ;
2135
+ try {
2136
+ // Load cached bitmap and draw
2137
+ File cacheFile = new File (restoreBitmapFile );
2138
+ FileInputStream inStream = new FileInputStream (cacheFile );
2139
+ ObjectInputStream din = new ObjectInputStream (inStream );
2140
+ byte [] array = (byte []) din .readObject ();
2141
+ ByteBuffer restoreBitmap = ByteBuffer .wrap (array );
2142
+ if (restoreBitmap .capacity () == bitmap .getHeight () * bitmap .getRowBytes ()) {
2143
+ restoreBitmap .rewind ();
2144
+ bitmap .copyPixelsFromBuffer (restoreBitmap );
2145
+ }
2146
+ inStream .close ();
2147
+ cacheFile .delete ();
2148
+ } catch (Exception ex ) {
2149
+ PGraphics .showWarning ("Could not restore screen contents from cache" );
2150
+ ex .printStackTrace ();
2151
+ }
2104
2152
}
2105
2153
}
2106
2154
}
0 commit comments