Project

General

Profile

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

examples / src / main / java / org / distorted / examples / surfaceview / RenderThread.java @ 061449ed

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 0de83d97 Leszek Koltunski
package org.distorted.examples.surfaceview;
21 862fcd79 Leszek Koltunski
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.EGL14;
25
import android.opengl.EGLSurface;
26 41a85bb7 Leszek Koltunski
import android.opengl.GLES31;
27 862fcd79 Leszek Koltunski
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 1f218177 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectDistort;
35 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
36 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedScreen;
38 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
39 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
40 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
41 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43 862fcd79 Leszek Koltunski
import org.distorted.examples.R;
44
45
import java.io.IOException;
46
import java.io.InputStream;
47
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49 bc0a685b Leszek Koltunski
50 061449ed Leszek Koltunski
class RenderThread extends Thread implements DistortedLibrary.ExceptionListener
51 862fcd79 Leszek Koltunski
  {
52
  private static final String TAG = "RenderThread";
53
54
  // Object must be created on render thread to get correct Looper, but is used from
55
  // UI thread, so we need to declare it volatile to ensure the UI thread sees a fully
56
  // constructed object.
57
  private volatile RenderHandler mHandler;
58
59
  // Used to wait for the thread to start.
60 ee76cb8f Leszek Koltunski
  private final Object mStartLock = new Object();
61 862fcd79 Leszek Koltunski
  private boolean mReady = false;
62
  private volatile SurfaceHolder mSurfaceHolder;  // may be updated by UI thread
63
  private EglCore eglCore;
64
  private EGLSurface eglSurface;
65
66 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
67 c126aa85 leszek
  private DistortedTexture mTexture;
68 f3ca895f Leszek Koltunski
  private MeshRectangles mMesh;
69 d218d64e leszek
  private DistortedScreen mScreen;
70 f6d884d5 Leszek Koltunski
  private SurfaceView mView;
71 c126aa85 leszek
  private static boolean resourcesCreated = false;
72 0de83d97 Leszek Koltunski
  private Static3D mScale;
73 5d7e316e Leszek Koltunski
  private float mBmpRatio;
74 862fcd79 Leszek Koltunski
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76 bc0a685b Leszek Koltunski
77 f6d884d5 Leszek Koltunski
  RenderThread(SurfaceHolder holder, SurfaceView view)
78 862fcd79 Leszek Koltunski
    {
79
    mSurfaceHolder = holder;
80
    mView = view;
81
82 5d7e316e Leszek Koltunski
    Static3D pLeft = new Static3D( ( 90-320*0.5f)/320.0f, (108-366*0.5f)/366.0f, 0);
83
    Static3D pRight= new Static3D( (176-320*0.5f)/320.0f, (111-366*0.5f)/366.0f, 0);
84 862fcd79 Leszek Koltunski
85 5d7e316e Leszek Koltunski
    Static4D rLeft = new Static4D( -10/320.0f, 10/366.0f, 0, 25/320.0f);
86
    Static4D rRight= new Static4D(  10/320.0f,  5/366.0f, 0, 25/320.0f);
87 862fcd79 Leszek Koltunski
88 f6d884d5 Leszek Koltunski
    Dynamic3D dLeft = new Dynamic3D(1000,0.0f);
89
    Dynamic3D dRight= new Dynamic3D(1000,0.0f);
90 7bf107f7 Leszek Koltunski
91 5d7e316e Leszek Koltunski
    dLeft.add( new Static3D(         0,         0, 0) );
92
    dLeft.add( new Static3D(-20/320.0f, 20/366.0f, 0) );
93 7bf107f7 Leszek Koltunski
94 5d7e316e Leszek Koltunski
    dRight.add( new Static3D(         0,         0, 0) );
95
    dRight.add( new Static3D( 20/320.0f, 10/366.0f, 0) );
96 f6d884d5 Leszek Koltunski
97 698ad0a8 Leszek Koltunski
    mEffects = new DistortedEffects();
98 1f218177 Leszek Koltunski
    mEffects.apply( new VertexEffectDistort(dLeft , pLeft , rLeft ) );
99
    mEffects.apply( new VertexEffectDistort(dRight, pRight, rRight) );
100
101
    mScale= new Static3D(1,1,1);
102
    mEffects.apply(new MatrixEffectScale(mScale));
103 392e16fd Leszek Koltunski
104 e4330c89 Leszek Koltunski
    mScreen = new DistortedScreen();
105 f6d884d5 Leszek Koltunski
    }
106
107 c126aa85 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
108
109
  static void setResourcesCreated()
110
    {
111
    resourcesCreated = false;
112
    }
113
114 f6d884d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
115
116
  private static void checkGlError(String op)
117
    {
118 41a85bb7 Leszek Koltunski
    int error = GLES31.glGetError();
119 f6d884d5 Leszek Koltunski
120 41a85bb7 Leszek Koltunski
    if (error != GLES31.GL_NO_ERROR)
121 f6d884d5 Leszek Koltunski
      {
122
      String msg = op + ": glError 0x" + Integer.toHexString(error);
123
      Log.e(TAG, msg);
124 1f218177 Leszek Koltunski
//    throw new RuntimeException(msg);
125 f6d884d5 Leszek Koltunski
      }
126 862fcd79 Leszek Koltunski
    }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129 bc0a685b Leszek Koltunski
130 862fcd79 Leszek Koltunski
  @Override
131
  public void run()
132
    {
133
    Looper.prepare();
134
    mHandler = new RenderHandler(this);
135
    eglCore = new EglCore(null, EglCore.FLAG_RECORDABLE | EglCore.FLAG_TRY_GLES3);
136
137
    synchronized (mStartLock)
138
      {
139
      mReady = true;
140
      mStartLock.notify();    // signal waitUntilReady()
141
      }
142
143
    Looper.loop();
144
    Log.d(TAG, "looper quit");
145
146
    checkGlError("releaseGl start");
147
148
    if (eglSurface != null)
149
      {
150
      eglCore.releaseSurface(eglSurface);
151
      eglSurface = EGL14.EGL_NO_SURFACE;
152
      }
153
154
    checkGlError("releaseGl done");
155
156
    eglCore.makeNothingCurrent();
157
    eglCore.release();
158
159
    synchronized (mStartLock)
160
      {
161
      mReady = false;
162
      }
163
    }
164
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166 bc0a685b Leszek Koltunski
167 f6d884d5 Leszek Koltunski
  void waitUntilReady()
168 862fcd79 Leszek Koltunski
    {
169
    synchronized (mStartLock)
170
      {
171
      while (!mReady)
172
        {
173
        try
174
          {
175
          mStartLock.wait();
176
          }
177 bc0a685b Leszek Koltunski
        catch (InterruptedException ie) {  }
178 862fcd79 Leszek Koltunski
        }
179
      }
180
    }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183 bc0a685b Leszek Koltunski
184 862fcd79 Leszek Koltunski
  void shutdown()
185
    {
186
    Log.d(TAG, "shutdown");
187
    Looper.myLooper().quit();
188
    }
189
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191 bc0a685b Leszek Koltunski
192 f6d884d5 Leszek Koltunski
  RenderHandler getHandler()
193 bc0a685b Leszek Koltunski
    {
194
    return mHandler;
195
    }
196 862fcd79 Leszek Koltunski
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198 bc0a685b Leszek Koltunski
199 862fcd79 Leszek Koltunski
  void surfaceCreated()
200
    {
201 c126aa85 leszek
    Log.d(TAG, "surfaceCreated");
202
203 862fcd79 Leszek Koltunski
    Surface surface = mSurfaceHolder.getSurface();
204
205
    eglSurface = eglCore.createWindowSurface(surface);
206
    eglCore.makeCurrent(eglSurface);
207
208 c126aa85 leszek
    createResources();
209
    }
210
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
213
  void surfaceChanged(int width, int height)
214
    {
215
    Log.d(TAG, "surfaceChanged " + width + "x" + height);
216
217 5d7e316e Leszek Koltunski
    if( width<height ) mScale.set( width,   width*mBmpRatio, 1 );
218
    else               mScale.set( height/mBmpRatio, height, 1 );
219 c126aa85 leszek
220
    mScreen.resize(width, height);
221
    }
222
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224
225 1f218177 Leszek Koltunski
  private void createResources()
226 c126aa85 leszek
    {
227
    resourcesCreated = true;
228
229 862fcd79 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
230
    Bitmap bmp;
231
232
    try
233
      {
234
      bmp = BitmapFactory.decodeStream(is);
235
      }
236
    finally
237
      {
238
      try
239
        {
240
        is.close();
241
        }
242
      catch(IOException io) {}
243
      }
244
245 5d7e316e Leszek Koltunski
    mBmpRatio = (float)bmp.getHeight()/bmp.getWidth();
246 e8b6aa95 Leszek Koltunski
247 687263cc Leszek Koltunski
    if( mTexture==null ) mTexture = new DistortedTexture();
248 c126aa85 leszek
    mTexture.setTexture(bmp);
249 862fcd79 Leszek Koltunski
250 5d7e316e Leszek Koltunski
    if( mMesh==null ) mMesh = new MeshRectangles(9,(int)(9*mBmpRatio));
251 5f2a53b6 leszek
252 fe59d375 Leszek Koltunski
    mScreen.detachAll();
253 5f2a53b6 leszek
    mScreen.attach(mTexture,mEffects,mMesh);
254 862fcd79 Leszek Koltunski
255 885b9cca leszek
    VertexEffectDistort.enable();
256 061449ed Leszek Koltunski
    DistortedLibrary.onCreate(mView.getContext(), this);
257
    }
258 6637d0f2 Leszek Koltunski
259 061449ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
260
261
  public void distortedException(Exception ex)
262
    {
263
    android.util.Log.e("SurfaceView", ex.getMessage() );
264 862fcd79 Leszek Koltunski
    }
265
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268 f6d884d5 Leszek Koltunski
  void doFrame(long frameTimeNs)
269 862fcd79 Leszek Koltunski
    {
270 c126aa85 leszek
    // We can still get here after onPaused - ignore such calls.
271 0de83d97 Leszek Koltunski
    if( SurfaceViewSurfaceView.isPaused() )
272 e52f9d96 Leszek Koltunski
      {
273
      android.util.Log.e("Thread", "Got here after onPaused- ignoring frame draw call!!");
274
      return;
275
      }
276
277 c126aa85 leszek
    // This will happen if we just briefly went into the background (press 'POWER')
278
    // Then surfaceCreated/changed is not called
279
    if( !resourcesCreated )
280
      {
281
      Log.e("Thread", "resources not created!!");
282
      createResources();
283
      }
284
285 862fcd79 Leszek Koltunski
    eglCore.makeCurrent(eglSurface);
286 855ba24e leszek
    mScreen.render( frameTimeNs/1000000 );  // 1 nanosecond = 1 millisecond / 10^-6
287 862fcd79 Leszek Koltunski
    eglCore.swapBuffers(eglSurface);
288
    }
289
290
  }