commit c126aa8514bacf72bb2d8b36e91afc56230a2779
Author: leszek <leszek@koltunski.pl>
Date:   Wed Apr 19 00:07:29 2017 +0100

    Fix PlainMonaLisa's Activity lifecycle (it wouldn't work after a POWER button press, i.e. after a brief time in the background - surfaceCreated is not called in this case)

diff --git a/src/main/java/org/distorted/examples/plainmonalisa/PlainMonaLisaSurfaceView.java b/src/main/java/org/distorted/examples/plainmonalisa/PlainMonaLisaSurfaceView.java
index add70e7..7b9b788 100644
--- a/src/main/java/org/distorted/examples/plainmonalisa/PlainMonaLisaSurfaceView.java
+++ b/src/main/java/org/distorted/examples/plainmonalisa/PlainMonaLisaSurfaceView.java
@@ -43,6 +43,7 @@ class PlainMonaLisaSurfaceView extends SurfaceView implements SurfaceHolder.Call
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
+  @Override
   public void surfaceCreated(SurfaceHolder holder)
     {
     Log.d(TAG, "surfaceCreated holder=" + holder);
@@ -64,6 +65,7 @@ class PlainMonaLisaSurfaceView extends SurfaceView implements SurfaceHolder.Call
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
+  @Override
   public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
     {
     Log.d(TAG, "surfaceChanged fmt=" + format + " size=" + width + "x" + height +" holder=" + holder);
@@ -78,6 +80,7 @@ class PlainMonaLisaSurfaceView extends SurfaceView implements SurfaceHolder.Call
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
+  @Override
   public void surfaceDestroyed(SurfaceHolder holder)
     {
     Log.d(TAG, "surfaceDestroyed holder=" + holder);
@@ -112,6 +115,7 @@ class PlainMonaLisaSurfaceView extends SurfaceView implements SurfaceHolder.Call
   public void onPause()
     {
     mPaused = true;
+    RenderThread.setResourcesCreated();
 
     Log.d(TAG, "onPause unhooking choreographer");
     Choreographer.getInstance().removeFrameCallback(this);
diff --git a/src/main/java/org/distorted/examples/plainmonalisa/RenderThread.java b/src/main/java/org/distorted/examples/plainmonalisa/RenderThread.java
index ae377f7..843cd2b 100644
--- a/src/main/java/org/distorted/examples/plainmonalisa/RenderThread.java
+++ b/src/main/java/org/distorted/examples/plainmonalisa/RenderThread.java
@@ -64,9 +64,11 @@ class RenderThread extends Thread
   private EGLSurface eglSurface;
 
   private DistortedEffects mEffects;
+  private DistortedTexture mTexture;
   private DistortedScreen mScreen;
   private int bmpHeight, bmpWidth;
   private SurfaceView mView;
+  private static boolean resourcesCreated = false;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -97,6 +99,13 @@ class RenderThread extends Thread
     mScreen = new DistortedScreen();
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  static void setResourcesCreated()
+    {
+    resourcesCreated = false;
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private static void checkGlError(String op)
@@ -184,11 +193,50 @@ class RenderThread extends Thread
 
   void surfaceCreated()
     {
+    Log.d(TAG, "surfaceCreated");
+
     Surface surface = mSurfaceHolder.getSurface();
 
     eglSurface = eglCore.createWindowSurface(surface);
     eglCore.makeCurrent(eglSurface);
 
+    createResources();
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void surfaceChanged(int width, int height)
+    {
+    Log.d(TAG, "surfaceChanged " + width + "x" + height);
+
+    mEffects.abortEffects(EffectTypes.MATRIX);
+
+    if( (float)bmpHeight/bmpWidth > (float)height/width )
+      {
+      int w = (height*bmpWidth)/bmpHeight;
+      float factor = (float)height/bmpHeight;
+
+      mEffects.move( new Static3D((width-w)/2,0,0) );
+      mEffects.scale( factor );
+      }
+    else
+      {
+      int h = (width*bmpHeight)/bmpWidth;
+      float factor = (float)width/bmpWidth;
+
+      mEffects.move( new Static3D(0,(height-h)/2,0) );
+      mEffects.scale( factor );
+      }
+
+    mScreen.resize(width, height);
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void createResources()
+    {
+    resourcesCreated = true;
+
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
     Bitmap bmp;
 
@@ -208,12 +256,11 @@ class RenderThread extends Thread
     bmpHeight = bmp.getHeight();
     bmpWidth  = bmp.getWidth();
 
-    DistortedTexture texture = new DistortedTexture(bmpWidth,bmpHeight);
-    texture.setTexture(bmp);
-    MeshFlat mesh = new MeshFlat(9,9*bmpHeight/bmpWidth);  // more-or-less square Grid with 9 columns.
+    if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
+    mTexture.setTexture(bmp);
 
     mScreen.detachAll();
-    mScreen.attach(texture,mEffects,mesh);
+    mScreen.attach(mTexture,mEffects,new MeshFlat(9,9*bmpHeight/bmpWidth));
 
     DistortedEffects.enableEffect(EffectNames.DISTORT);
 
@@ -227,44 +274,25 @@ class RenderThread extends Thread
       }
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  void surfaceChanged(int width, int height)
-    {
-    Log.d(TAG, "surfaceChanged " + width + "x" + height);
-
-    mEffects.abortEffects(EffectTypes.MATRIX);
-
-    if( (float)bmpHeight/bmpWidth > (float)height/width )
-      {
-      int w = (height*bmpWidth)/bmpHeight;
-      float factor = (float)height/bmpHeight;
-
-      mEffects.move( new Static3D((width-w)/2,0,0) );
-      mEffects.scale( factor );
-      }
-    else
-      {
-      int h = (width*bmpHeight)/bmpWidth;
-      float factor = (float)width/bmpWidth;
-
-      mEffects.move( new Static3D(0,(height-h)/2,0) );
-      mEffects.scale( factor );
-      }
-
-    mScreen.resize(width, height);
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   void doFrame(long frameTimeNs)
     {
+    // We can still get here after onPaused - ignore such calls.
     if( PlainMonaLisaSurfaceView.isPaused() )
       {
       android.util.Log.e("Thread", "Got here after onPaused- ignoring frame draw call!!");
       return;
       }
 
+    // This will happen if we just briefly went into the background (press 'POWER')
+    // Then surfaceCreated/changed is not called
+    if( !resourcesCreated )
+      {
+      Log.e("Thread", "resources not created!!");
+      createResources();
+      }
+
     eglCore.makeCurrent(eglSurface);
     mScreen.render( frameTimeNs/1000000 );  // 1 nanosecond = 1 millisecond / 10^-6
     eglCore.swapBuffers(eglSurface);
