Skip to content

Commit fe19c3e

Browse files
committed
save screen bitmap in Android2D renderer to cache
1 parent 3e3df3d commit fe19c3e

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

core/src/processing/a2d/PGraphicsAndroid2D.java

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323

2424
package processing.a2d;
2525

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;
2631
import java.nio.ByteBuffer;
2732
import java.io.InputStream;
2833
import java.util.zip.GZIPInputStream;
@@ -39,11 +44,13 @@
3944
import processing.core.PShapeSVG;
4045
import processing.core.PSurface;
4146
import processing.data.XML;
47+
import processing.opengl.PGL;
4248

4349
import android.annotation.SuppressLint;
4450
import android.app.Activity;
4551
import android.app.ActivityManager;
4652
import android.app.ActivityManager.MemoryInfo;
53+
import android.content.Context;
4754
import android.graphics.*;
4855
import android.graphics.Bitmap.Config;
4956
import android.graphics.Paint.Style;
@@ -123,7 +130,7 @@ public class PGraphicsAndroid2D extends PGraphics {
123130

124131
/** To save the surface contents before the activity is taken to the background. */
125132
private int restoreCount;
126-
private ByteBuffer restoreBitmap;
133+
private String restoreBitmapFile;
127134

128135
//////////////////////////////////////////////////////////////
129136

@@ -163,7 +170,14 @@ public void surfaceChanged() {
163170

164171
@Override
165172
public void setSize(int iwidth, int iheight) {
173+
// boolean wbool = iwidth != width;
174+
// boolean hbool = iheight != height;
166175
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);
167181
super.setSize(iwidth, iheight);
168182
}
169183

@@ -253,6 +267,7 @@ public void endDraw() {
253267
try {
254268
holder.unlockCanvasAndPost(screen);
255269
} catch (IllegalStateException ex) {
270+
} catch (IllegalArgumentException ex) {
256271
}
257272
}
258273
}
@@ -2073,10 +2088,28 @@ public void resize(int wide, int high) {
20732088

20742089
@Override
20752090
protected void saveState() {
2076-
if (bitmap != null) {
2091+
Context context = parent.getContext();
2092+
if (context == null || bitmap == null) return;
2093+
try {
20772094
int size = bitmap.getHeight() * bitmap.getRowBytes();
2078-
restoreBitmap = ByteBuffer.allocate(size);
2095+
ByteBuffer restoreBitmap = ByteBuffer.allocate(size);
20792096
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();
20802113
}
20812114
}
20822115

@@ -2090,17 +2123,32 @@ protected void restoreState() {
20902123
protected void restoreSurface() {
20912124
if (changed) {
20922125
changed = false;
2093-
if (restoreBitmap != null) {
2126+
if (restoreBitmapFile != null) {
20942127
// Set the counter to 1 so the restore bitmap is drawn in the next frame.
20952128
restoreCount = 1;
20962129
}
20972130
} else if (restoreCount > 0) {
20982131
restoreCount--;
20992132
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+
}
21042152
}
21052153
}
21062154
}

0 commit comments

Comments
 (0)