Skip to content

Commit 3e3df3d

Browse files
committed
save pixels to cache file
1 parent 42f3456 commit 3e3df3d

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed

core/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,19 @@
2626

2727
import processing.android.AppComponent;
2828
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;
2935
import java.lang.ref.ReferenceQueue;
3036
import java.lang.ref.WeakReference;
3137
import java.net.URL;
3238
import java.nio.*;
3339
import java.util.*;
3440

41+
import android.content.Context;
3542
import 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

Comments
 (0)