Project

General

Profile

Download (9.53 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / plainmonalisa / RenderThread.java @ e4330c89

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.examples.plainmonalisa;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.EGL14;
25
import android.opengl.EGLSurface;
26
import android.opengl.GLES30;
27
import android.os.Looper;
28
import android.util.Log;
29
import android.view.Surface;
30
import android.view.SurfaceHolder;
31
import android.view.SurfaceView;
32

    
33
import org.distorted.library.effect.EffectName;
34
import org.distorted.library.effect.MatrixEffectMove;
35
import org.distorted.library.effect.MatrixEffectScale;
36
import org.distorted.library.effect.VertexEffectDistort;
37
import org.distorted.library.main.Distorted;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.main.DistortedScreen;
40
import org.distorted.library.main.MeshFlat;
41
import org.distorted.library.main.DistortedTexture;
42
import org.distorted.library.type.Dynamic3D;
43
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45
import org.distorted.examples.R;
46

    
47
import java.io.IOException;
48
import java.io.InputStream;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class RenderThread extends Thread
53
  {
54
  private static final String TAG = "RenderThread";
55

    
56
  // Object must be created on render thread to get correct Looper, but is used from
57
  // UI thread, so we need to declare it volatile to ensure the UI thread sees a fully
58
  // constructed object.
59
  private volatile RenderHandler mHandler;
60

    
61
  // Used to wait for the thread to start.
62
  private final Object mStartLock = new Object();
63
  private boolean mReady = false;
64
  private volatile SurfaceHolder mSurfaceHolder;  // may be updated by UI thread
65
  private EglCore eglCore;
66
  private EGLSurface eglSurface;
67

    
68
  private DistortedEffects mEffects;
69
  private DistortedTexture mTexture;
70
  private MeshFlat mMesh;
71
  private DistortedScreen mScreen;
72
  private int bmpHeight, bmpWidth;
73
  private SurfaceView mView;
74
  private static boolean resourcesCreated = false;
75
  private Static3D mMove, mScale;
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  RenderThread(SurfaceHolder holder, SurfaceView view)
80
    {
81
    mSurfaceHolder = holder;
82
    mView = view;
83

    
84
    Static3D pLeft = new Static3D( 90, 258, 0);
85
    Static3D pRight= new Static3D(176, 255, 0);
86

    
87
    Static4D rLeft = new Static4D(-10,-10,25,25);
88
    Static4D rRight= new Static4D( 10, -5,25,25);
89

    
90
    Dynamic3D dLeft = new Dynamic3D(1000,0.0f);
91
    Dynamic3D dRight= new Dynamic3D(1000,0.0f);
92

    
93
    dLeft.add( new Static3D(  0,  0,0) );
94
    dLeft.add( new Static3D(-20,-20,0) );
95

    
96
    dRight.add( new Static3D(  0,  0,0) );
97
    dRight.add( new Static3D( 20,-10,0) );
98

    
99
    mEffects = new DistortedEffects();
100
    mEffects.apply( new VertexEffectDistort(dLeft , pLeft , rLeft ) );
101
    mEffects.apply( new VertexEffectDistort(dRight, pRight, rRight) );
102

    
103
    mMove = new Static3D(0,0,0);
104
    mScale= new Static3D(1,1,1);
105
    mEffects.apply(new MatrixEffectMove(mMove));
106
    mEffects.apply(new MatrixEffectScale(mScale));
107

    
108
    mScreen = new DistortedScreen();
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  static void setResourcesCreated()
114
    {
115
    resourcesCreated = false;
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  private static void checkGlError(String op)
121
    {
122
    int error = GLES30.glGetError();
123

    
124
    if (error != GLES30.GL_NO_ERROR)
125
      {
126
      String msg = op + ": glError 0x" + Integer.toHexString(error);
127
      Log.e(TAG, msg);
128
//    throw new RuntimeException(msg);
129
      }
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  @Override
135
  public void run()
136
    {
137
    Looper.prepare();
138
    mHandler = new RenderHandler(this);
139
    eglCore = new EglCore(null, EglCore.FLAG_RECORDABLE | EglCore.FLAG_TRY_GLES3);
140

    
141
    synchronized (mStartLock)
142
      {
143
      mReady = true;
144
      mStartLock.notify();    // signal waitUntilReady()
145
      }
146

    
147
    Looper.loop();
148
    Log.d(TAG, "looper quit");
149

    
150
    checkGlError("releaseGl start");
151

    
152
    if (eglSurface != null)
153
      {
154
      eglCore.releaseSurface(eglSurface);
155
      eglSurface = EGL14.EGL_NO_SURFACE;
156
      }
157

    
158
    checkGlError("releaseGl done");
159

    
160
    eglCore.makeNothingCurrent();
161
    eglCore.release();
162

    
163
    synchronized (mStartLock)
164
      {
165
      mReady = false;
166
      }
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  void waitUntilReady()
172
    {
173
    synchronized (mStartLock)
174
      {
175
      while (!mReady)
176
        {
177
        try
178
          {
179
          mStartLock.wait();
180
          }
181
        catch (InterruptedException ie) {  }
182
        }
183
      }
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  void shutdown()
189
    {
190
    Log.d(TAG, "shutdown");
191
    Looper.myLooper().quit();
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  RenderHandler getHandler()
197
    {
198
    return mHandler;
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  void surfaceCreated()
204
    {
205
    Log.d(TAG, "surfaceCreated");
206

    
207
    Surface surface = mSurfaceHolder.getSurface();
208

    
209
    eglSurface = eglCore.createWindowSurface(surface);
210
    eglCore.makeCurrent(eglSurface);
211

    
212
    createResources();
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  void surfaceChanged(int width, int height)
218
    {
219
    Log.d(TAG, "surfaceChanged " + width + "x" + height);
220

    
221
    if( (float)bmpHeight/bmpWidth > (float)height/width )
222
      {
223
      int w = (height*bmpWidth)/bmpHeight;
224
      float factor = (float)height/bmpHeight;
225

    
226
      mMove.set((width-w)/2,0,0);
227
      mScale.set( factor,factor,factor );
228
      }
229
    else
230
      {
231
      int h = (width*bmpHeight)/bmpWidth;
232
      float factor = (float)width/bmpWidth;
233

    
234
      mMove.set(0,(height-h)/2,0);
235
      mScale.set( factor,factor,factor );
236
      }
237

    
238
    mScreen.resize(width, height);
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  private void createResources()
244
    {
245
    resourcesCreated = true;
246

    
247
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
248
    Bitmap bmp;
249

    
250
    try
251
      {
252
      bmp = BitmapFactory.decodeStream(is);
253
      }
254
    finally
255
      {
256
      try
257
        {
258
        is.close();
259
        }
260
      catch(IOException io) {}
261
      }
262

    
263
    bmpHeight = bmp.getHeight();
264
    bmpWidth  = bmp.getWidth();
265

    
266
    if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
267
    mTexture.setTexture(bmp);
268

    
269
    if( mMesh==null ) mMesh = new MeshFlat(9,9*bmpHeight/bmpWidth);
270

    
271
    mScreen.detachAll();
272
    mScreen.attach(mTexture,mEffects,mMesh);
273

    
274
    DistortedEffects.enableEffect(EffectName.DISTORT);
275

    
276
    try
277
      {
278
      Distorted.onCreate(mView.getContext());
279
      }
280
    catch(Exception ex)
281
      {
282
      Log.e("PlainMonaLisa", ex.getMessage() );
283
      }
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
  void doFrame(long frameTimeNs)
289
    {
290
    // We can still get here after onPaused - ignore such calls.
291
    if( PlainMonaLisaSurfaceView.isPaused() )
292
      {
293
      android.util.Log.e("Thread", "Got here after onPaused- ignoring frame draw call!!");
294
      return;
295
      }
296

    
297
    // This will happen if we just briefly went into the background (press 'POWER')
298
    // Then surfaceCreated/changed is not called
299
    if( !resourcesCreated )
300
      {
301
      Log.e("Thread", "resources not created!!");
302
      createResources();
303
      }
304

    
305
    eglCore.makeCurrent(eglSurface);
306
    mScreen.render( frameTimeNs/1000000 );  // 1 nanosecond = 1 millisecond / 10^-6
307
    eglCore.swapBuffers(eglSurface);
308
    }
309

    
310
  }
(5-5/5)