Project

General

Profile

« Previous | Next » 

Revision c126aa85

Added by Leszek Koltunski about 7 years ago

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)

View differences:

src/main/java/org/distorted/examples/plainmonalisa/RenderThread.java
64 64
  private EGLSurface eglSurface;
65 65

  
66 66
  private DistortedEffects mEffects;
67
  private DistortedTexture mTexture;
67 68
  private DistortedScreen mScreen;
68 69
  private int bmpHeight, bmpWidth;
69 70
  private SurfaceView mView;
71
  private static boolean resourcesCreated = false;
70 72

  
71 73
///////////////////////////////////////////////////////////////////////////////////////////////////
72 74

  
......
97 99
    mScreen = new DistortedScreen();
98 100
    }
99 101

  
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

  
104
  static void setResourcesCreated()
105
    {
106
    resourcesCreated = false;
107
    }
108

  
100 109
///////////////////////////////////////////////////////////////////////////////////////////////////
101 110

  
102 111
  private static void checkGlError(String op)
......
184 193

  
185 194
  void surfaceCreated()
186 195
    {
196
    Log.d(TAG, "surfaceCreated");
197

  
187 198
    Surface surface = mSurfaceHolder.getSurface();
188 199

  
189 200
    eglSurface = eglCore.createWindowSurface(surface);
190 201
    eglCore.makeCurrent(eglSurface);
191 202

  
203
    createResources();
204
    }
205

  
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

  
208
  void surfaceChanged(int width, int height)
209
    {
210
    Log.d(TAG, "surfaceChanged " + width + "x" + height);
211

  
212
    mEffects.abortEffects(EffectTypes.MATRIX);
213

  
214
    if( (float)bmpHeight/bmpWidth > (float)height/width )
215
      {
216
      int w = (height*bmpWidth)/bmpHeight;
217
      float factor = (float)height/bmpHeight;
218

  
219
      mEffects.move( new Static3D((width-w)/2,0,0) );
220
      mEffects.scale( factor );
221
      }
222
    else
223
      {
224
      int h = (width*bmpHeight)/bmpWidth;
225
      float factor = (float)width/bmpWidth;
226

  
227
      mEffects.move( new Static3D(0,(height-h)/2,0) );
228
      mEffects.scale( factor );
229
      }
230

  
231
    mScreen.resize(width, height);
232
    }
233

  
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

  
236
  void createResources()
237
    {
238
    resourcesCreated = true;
239

  
192 240
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
193 241
    Bitmap bmp;
194 242

  
......
208 256
    bmpHeight = bmp.getHeight();
209 257
    bmpWidth  = bmp.getWidth();
210 258

  
211
    DistortedTexture texture = new DistortedTexture(bmpWidth,bmpHeight);
212
    texture.setTexture(bmp);
213
    MeshFlat mesh = new MeshFlat(9,9*bmpHeight/bmpWidth);  // more-or-less square Grid with 9 columns.
259
    if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
260
    mTexture.setTexture(bmp);
214 261

  
215 262
    mScreen.detachAll();
216
    mScreen.attach(texture,mEffects,mesh);
263
    mScreen.attach(mTexture,mEffects,new MeshFlat(9,9*bmpHeight/bmpWidth));
217 264

  
218 265
    DistortedEffects.enableEffect(EffectNames.DISTORT);
219 266

  
......
227 274
      }
228 275
    }
229 276

  
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

  
232
  void surfaceChanged(int width, int height)
233
    {
234
    Log.d(TAG, "surfaceChanged " + width + "x" + height);
235

  
236
    mEffects.abortEffects(EffectTypes.MATRIX);
237

  
238
    if( (float)bmpHeight/bmpWidth > (float)height/width )
239
      {
240
      int w = (height*bmpWidth)/bmpHeight;
241
      float factor = (float)height/bmpHeight;
242

  
243
      mEffects.move( new Static3D((width-w)/2,0,0) );
244
      mEffects.scale( factor );
245
      }
246
    else
247
      {
248
      int h = (width*bmpHeight)/bmpWidth;
249
      float factor = (float)width/bmpWidth;
250

  
251
      mEffects.move( new Static3D(0,(height-h)/2,0) );
252
      mEffects.scale( factor );
253
      }
254

  
255
    mScreen.resize(width, height);
256
    }
257

  
258 277
///////////////////////////////////////////////////////////////////////////////////////////////////
259 278

  
260 279
  void doFrame(long frameTimeNs)
261 280
    {
281
    // We can still get here after onPaused - ignore such calls.
262 282
    if( PlainMonaLisaSurfaceView.isPaused() )
263 283
      {
264 284
      android.util.Log.e("Thread", "Got here after onPaused- ignoring frame draw call!!");
265 285
      return;
266 286
      }
267 287

  
288
    // This will happen if we just briefly went into the background (press 'POWER')
289
    // Then surfaceCreated/changed is not called
290
    if( !resourcesCreated )
291
      {
292
      Log.e("Thread", "resources not created!!");
293
      createResources();
294
      }
295

  
268 296
    eglCore.makeCurrent(eglSurface);
269 297
    mScreen.render( frameTimeNs/1000000 );  // 1 nanosecond = 1 millisecond / 10^-6
270 298
    eglCore.swapBuffers(eglSurface);

Also available in: Unified diff