Skip to content

Commit e92e354

Browse files
authored
Merge pull request #486 from processing/super-fast-P2D
Merging fast-P2D branch into master
2 parents 03c2efe + 79b38d1 commit e92e354

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3554
-125
lines changed

core/src/assets/shaders/P2DFrag.glsl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
varying vec4 vertColor;
7+
varying vec2 vertTexCoord;
8+
varying float vertTexFactor;
9+
10+
uniform sampler2D texture;
11+
12+
void main() {
13+
gl_FragColor = mix(vertColor, vertColor * texture2D(texture, vertTexCoord), vertTexFactor);
14+
}

core/src/assets/shaders/P2DVert.glsl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
attribute vec3 position;
2+
attribute vec4 color;
3+
attribute vec2 texCoord;
4+
attribute float texFactor;
5+
6+
varying vec4 vertColor;
7+
varying vec2 vertTexCoord;
8+
varying float vertTexFactor;
9+
10+
uniform mat4 transform;
11+
uniform vec2 texScale;
12+
13+
void main() {
14+
gl_Position = transform * vec4(position, 1);
15+
16+
//we avoid affecting the Z component by the transform
17+
//because it would mess up our depth testing
18+
gl_Position.z = position.z;
19+
20+
vertColor = color.zyxw;
21+
vertTexCoord = texCoord * texScale;
22+
vertTexFactor = texFactor;
23+
}

core/src/processing/a2d/PGraphicsAndroid2D.java

Lines changed: 56 additions & 25 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;
@@ -44,6 +49,7 @@
4449
import android.app.Activity;
4550
import android.app.ActivityManager;
4651
import android.app.ActivityManager.MemoryInfo;
52+
import android.content.Context;
4753
import android.graphics.*;
4854
import android.graphics.Bitmap.Config;
4955
import android.graphics.Paint.Style;
@@ -122,8 +128,9 @@ public class PGraphicsAndroid2D extends PGraphics {
122128
//////////////////////////////////////////////////////////////
123129

124130
/** To save the surface contents before the activity is taken to the background. */
131+
private String restoreFilename;
132+
private int restoreWidth, restoreHeight;
125133
private int restoreCount;
126-
private ByteBuffer restoreBitmap;
127134

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

@@ -184,22 +191,6 @@ public PSurface createSurface(AppComponent component, SurfaceHolder holder, bool
184191

185192
// FRAME
186193

187-
/*
188-
public void requestDraw() {
189-
parent.surfaceView.requestRender();
190-
}
191-
*/
192-
193-
// public boolean canDraw() {
194-
// return true;
195-
// }
196-
197-
198-
// @Override
199-
// public void requestDraw() {
200-
//parent.handleDraw();
201-
// }
202-
203194
@SuppressLint("NewApi")
204195
protected Canvas checkCanvas() {
205196
if ((canvas == null || sized) && (useBitmap || !primaryGraphics)) {
@@ -221,7 +212,6 @@ protected Canvas checkCanvas() {
221212

222213
@Override
223214
public void beginDraw() {
224-
225215
canvas = checkCanvas();
226216

227217
checkSettings();
@@ -253,6 +243,7 @@ public void endDraw() {
253243
try {
254244
holder.unlockCanvasAndPost(screen);
255245
} catch (IllegalStateException ex) {
246+
} catch (IllegalArgumentException ex) {
256247
}
257248
}
258249
}
@@ -2073,10 +2064,32 @@ public void resize(int wide, int high) {
20732064

20742065
@Override
20752066
protected void saveState() {
2076-
if (bitmap != null) {
2067+
Context context = parent.getContext();
2068+
if (context == null || bitmap == null || parent.getSurface().getComponent().isService()) return;
2069+
try {
2070+
// Saving current width and height to avoid restoring the screen after a screen rotation
2071+
restoreWidth = pixelWidth;
2072+
restoreHeight = pixelHeight;
2073+
20772074
int size = bitmap.getHeight() * bitmap.getRowBytes();
2078-
restoreBitmap = ByteBuffer.allocate(size);
2075+
ByteBuffer restoreBitmap = ByteBuffer.allocate(size);
20792076
bitmap.copyPixelsToBuffer(restoreBitmap);
2077+
2078+
File cacheDir = context.getCacheDir();
2079+
File cacheFile = File.createTempFile("processing", "pixels", cacheDir);
2080+
restoreFilename = cacheFile.getAbsolutePath();
2081+
FileOutputStream stream = new FileOutputStream(cacheFile);
2082+
ObjectOutputStream dout = new ObjectOutputStream(stream);
2083+
byte[] array = new byte[size];
2084+
restoreBitmap.rewind();
2085+
restoreBitmap.get(array);
2086+
dout.writeObject(array);
2087+
dout.flush();
2088+
stream.getFD().sync();
2089+
stream.close();
2090+
} catch (Exception ex) {
2091+
PGraphics.showWarning("Could not save screen contents to cache");
2092+
ex.printStackTrace();
20802093
}
20812094
}
20822095

@@ -2090,17 +2103,35 @@ protected void restoreState() {
20902103
protected void restoreSurface() {
20912104
if (changed) {
20922105
changed = false;
2093-
if (restoreBitmap != null) {
2106+
if (restoreFilename != null && restoreWidth == pixelWidth && restoreHeight == pixelHeight) {
20942107
// Set the counter to 1 so the restore bitmap is drawn in the next frame.
20952108
restoreCount = 1;
20962109
}
20972110
} else if (restoreCount > 0) {
20982111
restoreCount--;
20992112
if (restoreCount == 0) {
2100-
// Draw and dispose bitmap
2101-
restoreBitmap.rewind();
2102-
bitmap.copyPixelsFromBuffer(restoreBitmap);
2103-
restoreBitmap = null;
2113+
Context context = parent.getContext();
2114+
if (context == null) return;
2115+
try {
2116+
// Load cached bitmap and draw
2117+
File cacheFile = new File(restoreFilename);
2118+
FileInputStream inStream = new FileInputStream(cacheFile);
2119+
ObjectInputStream din = new ObjectInputStream(inStream);
2120+
byte[] array = (byte[]) din.readObject();
2121+
ByteBuffer restoreBitmap = ByteBuffer.wrap(array);
2122+
if (restoreBitmap.capacity() == bitmap.getHeight() * bitmap.getRowBytes()) {
2123+
restoreBitmap.rewind();
2124+
bitmap.copyPixelsFromBuffer(restoreBitmap);
2125+
}
2126+
inStream.close();
2127+
cacheFile.delete();
2128+
restoreFilename = null;
2129+
restoreWidth = -1;
2130+
restoreHeight = -1;
2131+
} catch (Exception ex) {
2132+
PGraphics.showWarning("Could not restore screen contents from cache");
2133+
ex.printStackTrace();
2134+
}
21042135
}
21052136
}
21062137
}

core/src/processing/a2d/PSurfaceAndroid2D.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,7 @@ public void surfaceChanged(SurfaceHolder holder, int format, int iwidth, int ihe
124124
}
125125

126126
sketch.surfaceChanged();
127-
graphics.surfaceChanged();
128-
129127
sketch.setSize(iwidth, iheight);
130-
graphics.setSize(sketch.sketchWidth(), sketch.sketchHeight());
131128
}
132129

133130
@Override

core/src/processing/android/PWallpaper.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import android.graphics.Point;
3333
import android.graphics.Rect;
3434

35+
3536
public class PWallpaper extends WallpaperService implements AppComponent {
3637
private Point size;
3738
private DisplayMetrics metrics;
@@ -119,12 +120,16 @@ public Engine onCreateEngine() {
119120
@Override
120121
public void onDestroy() {
121122
super.onDestroy();
122-
if (engine != null) engine.onDestroy();
123+
124+
if (engine != null){
125+
//engine.sketch = null;
126+
engine.onDestroy();
127+
}
123128
}
124129

125130

126131
public class WallpaperEngine extends Engine implements ServiceEngine {
127-
private PApplet sketch;
132+
PApplet sketch;
128133
private float xOffset, xOffsetStep;
129134
private float yOffset, yOffsetStep;
130135
private int xPixelOffset, yPixelOffset;
@@ -149,9 +154,11 @@ public void onSurfaceCreated(SurfaceHolder surfaceHolder) {
149154
@Override
150155
public void onSurfaceChanged(final SurfaceHolder holder, final int format,
151156
final int width, final int height) {
152-
if (sketch != null) {
153-
sketch.g.setSize(width, height);
154-
}
157+
// When the surface of a live wallpaper changes (eg: after a screen rotation) the same sketch
158+
// continues to run (unlike the case of regular apps, where its re-created) so we need to
159+
// force a reset of the renderer so the backing FBOs (in the case of the OpenGL renderers)
160+
// get reinitalized with the correct size.
161+
sketch.g.reset();
155162
super.onSurfaceChanged(holder, format, width, height);
156163
}
157164

@@ -205,7 +212,7 @@ public void onSurfaceDestroyed(SurfaceHolder holder) {
205212
// surface. If you have a rendering thread that directly accesses the
206213
// surface, you must ensure that thread is no longer touching the Surface
207214
// before returning from this function.
208-
super.onSurfaceDestroyed(holder);
215+
super.onSurfaceDestroyed(holder);
209216
}
210217

211218

core/src/processing/android/PWatchFaceCanvas.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public class PWatchFaceCanvas extends CanvasWatchFaceService implements AppCompo
4747
private DisplayMetrics metrics;
4848
private CanvasEngine engine;
4949

50-
5150
public void initDimensions() {
5251
metrics = new DisplayMetrics();
5352
size = new Point();
@@ -231,10 +230,8 @@ public void onVisibilityChanged(boolean visible) {
231230
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
232231
super.onSurfaceChanged(holder, format, width, height);
233232
if (sketch != null) {
234-
sketch.displayWidth = width;
235-
sketch.displayHeight = height;
236-
sketch.g.setSize(sketch.sketchWidth(), sketch.sketchHeight());
237233
sketch.surfaceChanged();
234+
sketch.setSize(width, height);
238235
}
239236
}
240237

core/src/processing/android/PWatchFaceGLES.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,8 @@ public void onGlContextCreated() {
191191
public void onGlSurfaceCreated(int width, int height) {
192192
super.onGlSurfaceCreated(width, height);
193193
if (sketch != null) {
194-
sketch.displayWidth = width;
195-
sketch.displayHeight = height;
196-
sketch.g.setSize(sketch.sketchWidth(), sketch.sketchHeight());
197194
sketch.surfaceChanged();
195+
sketch.setSize(width, height);
198196
}
199197
}
200198

0 commit comments

Comments
 (0)