Skip to content

Commit e772fd5

Browse files
committed
refining surface restart changes
1 parent afbc5d3 commit e772fd5

File tree

5 files changed

+56
-48
lines changed

5 files changed

+56
-48
lines changed

core/src/processing/android/PWallpaper.java

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,24 @@ public class PEngine extends Engine {
133133
@Override
134134
public void onCreate(SurfaceHolder surfaceHolder) {
135135
super.onCreate(surfaceHolder);
136-
if (sketch == null) {
136+
if (sketch == null || isPreview()) {
137+
// Creating the sketch for the first time, this means that we are
138+
// in preview mode
137139
sketch = createSketch();
138140
sketch.initSurface(PWallpaper.this, getSurfaceHolder());
139141
sketch.startSurface();
140142
// By default we don't get touch events, so enable them.
141143
setTouchEventsEnabled(true);
142144
} else {
143-
// sketch.onPause();
145+
// Sketch already exists, so we are launching the "real" wallpaper.
146+
// Since the containing process is still the same, we don't want to
147+
// recreate the sketch and all associated resources (renderer, etc) not
148+
// only to keep resource usage/starting times low, but also because
149+
// otherwise strange things might happen (e.g.: static variables in places
150+
// like PConstants are zeroed when the preview instance is disposed, but
151+
// still accessed by the new instance).
144152
sketch.resetSurface(PWallpaper.this, getSurfaceHolder());
145-
sketch.frameCount = 0;
146153
sketch.startSurface();
147-
PApplet.println("Restarting sketch", sketch.isLooping());
148154
}
149155
if (sketch != null) {
150156
sketch.preview = isPreview();
@@ -155,13 +161,11 @@ public void onCreate(SurfaceHolder surfaceHolder) {
155161
@Override
156162
public void onSurfaceCreated(SurfaceHolder surfaceHolder) {
157163
super.onSurfaceCreated(surfaceHolder);
158-
// Log.d(TAG, "onSurfaceCreated()");
159164
}
160165

161166
@Override
162167
public void onSurfaceChanged(final SurfaceHolder holder, final int format,
163168
final int width, final int height) {
164-
// Log.d(TAG, "onSurfaceChanged()");
165169
if (sketch != null) {
166170
sketch.g.setSize(width, height);
167171
}
@@ -170,19 +174,11 @@ public void onSurfaceChanged(final SurfaceHolder holder, final int format,
170174

171175
@Override
172176
public void onVisibilityChanged(boolean visible) {
173-
// if (LoggerConfig.ON) {
174-
// Log.d(TAG, "onVisibilityChanged(" + visible + ")");
175-
// }
176-
//
177-
if (!isPreview()) {
178-
if (sketch != null) {
179-
if (visible) {
180-
PApplet.println("resuming sketch", sketch.frameCount);
181-
sketch.onResume();
182-
} else {
183-
PApplet.println("pausing sketch", sketch.frameCount);
184-
sketch.onPause();
185-
}
177+
if (!isPreview() && sketch != null) {
178+
if (visible) {
179+
sketch.onResume();
180+
} else {
181+
sketch.onPause();
186182
}
187183
}
188184
super.onVisibilityChanged(visible);
@@ -217,20 +213,9 @@ public void onSurfaceDestroyed(SurfaceHolder holder) {
217213

218214
@Override
219215
public void onDestroy() {
220-
// Called right before the engine is going away.
221-
// if (LoggerConfig.ON) {
222-
// Log.d(TAG, "onDestroy()");
223-
// }
224-
//
225216
super.onDestroy();
226-
if (sketch != null) {
227-
if (isPreview()) {
228-
// PApplet.println("Pausing sketch");
229-
// sketch.onPause();
230-
} else {
231-
PApplet.println("Destroying sketch");
232-
sketch.onDestroy();
233-
}
217+
if (!isPreview() && sketch != null) {
218+
sketch.onDestroy();
234219
}
235220
}
236221

core/src/processing/core/PApplet.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,6 @@ public void initSurface(LayoutInflater inflater, ViewGroup container,
504504

505505
if (parentLayout == -1) {
506506
setFullScreenVisibility();
507-
// Now we now the right width/height size for the renderer
508-
// g.setSize(width, height); // do need this?
509-
// Finalize surface initialization.
510507
surface.initView(width, height);
511508
} else {
512509
surface.initView(inflater, container, savedInstanceState,
@@ -524,18 +521,37 @@ public void initSurface(LayoutInflater inflater, ViewGroup container,
524521
}
525522

526523
public void resetSurface(AppComponent component, SurfaceHolder holder) {
527-
if (surface != null) surface.dispose();
524+
parentLayout = -1;
525+
resetSurface(null, null, null, component, holder);
526+
}
527+
528+
public void resetSurface(LayoutInflater inflater, ViewGroup container,
529+
Bundle savedInstanceState,
530+
AppComponent component, SurfaceHolder holder) {
531+
if (surface != null) {
532+
// Don't kill the process (activity or service) after stopping the
533+
// animation thread, otherwise the app will just quit. This in particular
534+
// is needed by live wallpapers (since the preview and real wallpaper are
535+
// the same service process).
536+
surface.stopThread(false);
537+
surface.dispose();
538+
}
528539
surface = g.createSurface(component, holder);
540+
529541
if (parentLayout == -1) {
530542
setFullScreenVisibility();
531-
// Now we now the right width/height size for the renderer
532-
// g.setSize(width, height); // do need this?
533-
// Finalize surface initialization.
534543
surface.initView(width, height);
535544
} else {
536-
// surface.initView(inflater, container, savedInstanceState,
537-
// fullScreen, width, height);
545+
surface.initView(inflater, container, savedInstanceState,
546+
fullScreen, width, height);
538547
}
548+
549+
// Reset frame count to start from setup() again
550+
frameCount = 0;
551+
552+
finished = false;
553+
looping = true;
554+
redraw = true;
539555
}
540556

541557

core/src/processing/core/PSurface.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public void initView(LayoutInflater inflater, ViewGroup container,
8787

8888
public boolean stopThread();
8989

90+
public boolean stopThread(boolean killProc);
91+
9092
public boolean isStopped();
9193

9294
public void finish();

core/src/processing/core/PSurfaceNone.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public class PSurfaceNone implements PSurface, PConstants {
6666
protected float frameRateTarget = 60;
6767
protected long frameRatePeriod = 1000000000L / 60L;
6868

69+
protected boolean killProcAfterStop = true;
70+
6971
@Override
7072
public Context getContext() {
7173
if (component.getKind() == AppComponent.FRAGMENT) {
@@ -299,7 +301,7 @@ public void setSystemUiVisibility(int visibility) {
299301

300302
@Override
301303
public void finish() {
302-
if (sketch == null || component == null) return;
304+
if (component == null) return;
303305

304306
if (component.getKind() == AppComponent.FRAGMENT) {
305307
activity.finish();
@@ -349,6 +351,12 @@ public void resumeThread() {
349351

350352
@Override
351353
public boolean stopThread() {
354+
return stopThread(true);
355+
}
356+
357+
@Override
358+
public boolean stopThread(boolean killProc) {
359+
killProcAfterStop = killProc;
352360
if (thread == null) {
353361
return false;
354362
}
@@ -409,7 +417,8 @@ public void run() { // not good to make this synchronized, locks things up
409417
// un-pause the sketch and get rolling
410418
sketch.start();
411419

412-
while ((Thread.currentThread() == thread) && (sketch != null && !sketch.finished)) {
420+
while ((Thread.currentThread() == thread) &&
421+
(sketch != null && !sketch.finished)) {
413422
checkPause();
414423
callDraw();
415424

@@ -444,8 +453,7 @@ public void run() { // not good to make this synchronized, locks things up
444453
beforeTime = System.nanoTime();
445454
}
446455

447-
// PApplet.println("Sketch is finished", sketch.frameCount);
448-
finish();
456+
if (killProcAfterStop) finish();
449457
}
450458
}
451459
}

core/src/processing/opengl/PSurfaceGLES.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public PSurfaceGLES(PGraphics graphics, AppComponent component, SurfaceHolder ho
8080
public void dispose() {
8181
super.dispose();
8282
if (glsurf != null) {
83-
PApplet.println("dispose surface");
8483
glsurf.dispose();
8584
glsurf = null;
8685
}
@@ -152,8 +151,6 @@ public SurfaceHolder getHolder() {
152151
public void dispose() {
153152
super.destroyDrawingCache();
154153
super.onDetachedFromWindow();
155-
// don't think i want to call stop() from here, since it might be swapping renderers
156-
// stop();
157154
}
158155

159156

0 commit comments

Comments
 (0)