Project

General

Profile

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

examples / src / main / java / org / distorted / examples / plainmonalisa / RenderThread.java @ 51554e47

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 862fcd79 Leszek Koltunski
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 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
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
import org.distorted.library.Distorted;
34 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
35 d218d64e leszek
import org.distorted.library.DistortedScreen;
36 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
37 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
38 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
39 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
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 f6d884d5 Leszek Koltunski
class RenderThread extends Thread
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 d218d64e leszek
  private DistortedScreen mScreen;
69 862fcd79 Leszek Koltunski
  private int bmpHeight, bmpWidth;
70 f6d884d5 Leszek Koltunski
  private SurfaceView mView;
71 c126aa85 leszek
  private static boolean resourcesCreated = false;
72 862fcd79 Leszek Koltunski
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74 bc0a685b Leszek Koltunski
75 f6d884d5 Leszek Koltunski
  RenderThread(SurfaceHolder holder, SurfaceView view)
76 862fcd79 Leszek Koltunski
    {
77
    mSurfaceHolder = holder;
78
    mView = view;
79
80 f6d884d5 Leszek Koltunski
    Static3D pLeft = new Static3D( 90, 258, 0);
81
    Static3D pRight= new Static3D(176, 255, 0);
82 862fcd79 Leszek Koltunski
83 f6d884d5 Leszek Koltunski
    Static4D rLeft = new Static4D(-10,-10,25,25);
84
    Static4D rRight= new Static4D( 10, -5,25,25);
85 862fcd79 Leszek Koltunski
86 f6d884d5 Leszek Koltunski
    Dynamic3D dLeft = new Dynamic3D(1000,0.0f);
87
    Dynamic3D dRight= new Dynamic3D(1000,0.0f);
88 7bf107f7 Leszek Koltunski
89
    dLeft.add( new Static3D(  0,  0,0) );
90
    dLeft.add( new Static3D(-20,-20,0) );
91
92
    dRight.add( new Static3D(  0,  0,0) );
93
    dRight.add( new Static3D( 20,-10,0) );
94 f6d884d5 Leszek Koltunski
95 d04a4886 Leszek Koltunski
    mEffects = new DistortedEffects();
96 392e16fd Leszek Koltunski
    mEffects.distort( dLeft, pLeft , rLeft );
97
    mEffects.distort(dRight, pRight, rRight);
98
99 d218d64e leszek
    mScreen = new DistortedScreen();
100 f6d884d5 Leszek Koltunski
    }
101
102 c126aa85 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104
  static void setResourcesCreated()
105
    {
106
    resourcesCreated = false;
107
    }
108
109 f6d884d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111
  private static void checkGlError(String op)
112
    {
113 41a81a14 Leszek Koltunski
    int error = GLES30.glGetError();
114 f6d884d5 Leszek Koltunski
115 41a81a14 Leszek Koltunski
    if (error != GLES30.GL_NO_ERROR)
116 f6d884d5 Leszek Koltunski
      {
117
      String msg = op + ": glError 0x" + Integer.toHexString(error);
118
      Log.e(TAG, msg);
119 6b9f9441 Leszek Koltunski
//      throw new RuntimeException(msg);
120 f6d884d5 Leszek Koltunski
      }
121 862fcd79 Leszek Koltunski
    }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124 bc0a685b Leszek Koltunski
125 862fcd79 Leszek Koltunski
  @Override
126
  public void run()
127
    {
128
    Looper.prepare();
129
    mHandler = new RenderHandler(this);
130
    eglCore = new EglCore(null, EglCore.FLAG_RECORDABLE | EglCore.FLAG_TRY_GLES3);
131
132
    synchronized (mStartLock)
133
      {
134
      mReady = true;
135
      mStartLock.notify();    // signal waitUntilReady()
136
      }
137
138
    Looper.loop();
139
    Log.d(TAG, "looper quit");
140
141
    checkGlError("releaseGl start");
142
143
    if (eglSurface != null)
144
      {
145
      eglCore.releaseSurface(eglSurface);
146
      eglSurface = EGL14.EGL_NO_SURFACE;
147
      }
148
149
    checkGlError("releaseGl done");
150
151
    eglCore.makeNothingCurrent();
152
    eglCore.release();
153
154
    synchronized (mStartLock)
155
      {
156
      mReady = false;
157
      }
158
    }
159
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161 bc0a685b Leszek Koltunski
162 f6d884d5 Leszek Koltunski
  void waitUntilReady()
163 862fcd79 Leszek Koltunski
    {
164
    synchronized (mStartLock)
165
      {
166
      while (!mReady)
167
        {
168
        try
169
          {
170
          mStartLock.wait();
171
          }
172 bc0a685b Leszek Koltunski
        catch (InterruptedException ie) {  }
173 862fcd79 Leszek Koltunski
        }
174
      }
175
    }
176
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178 bc0a685b Leszek Koltunski
179 862fcd79 Leszek Koltunski
  void shutdown()
180
    {
181
    Log.d(TAG, "shutdown");
182
    Looper.myLooper().quit();
183
    }
184
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186 bc0a685b Leszek Koltunski
187 f6d884d5 Leszek Koltunski
  RenderHandler getHandler()
188 bc0a685b Leszek Koltunski
    {
189
    return mHandler;
190
    }
191 862fcd79 Leszek Koltunski
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193 bc0a685b Leszek Koltunski
194 862fcd79 Leszek Koltunski
  void surfaceCreated()
195
    {
196 c126aa85 leszek
    Log.d(TAG, "surfaceCreated");
197
198 862fcd79 Leszek Koltunski
    Surface surface = mSurfaceHolder.getSurface();
199
200
    eglSurface = eglCore.createWindowSurface(surface);
201
    eglCore.makeCurrent(eglSurface);
202
203 c126aa85 leszek
    createResources();
204
    }
205
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208
  void surfaceChanged(int width, int height)
209
    {
210
    Log.d(TAG, "surfaceChanged " + width + "x" + height);
211
212 51554e47 leszek
    float qx = (float)width /bmpWidth;
213
    float qy = (float)height/bmpHeight;
214 c126aa85 leszek
215 51554e47 leszek
    mEffects.abortEffects(EffectTypes.MATRIX);
216
    mEffects.scale(  qx<qy ? (new Static3D(1,qx/qy,1)) : (new Static3D(qy/qx,1,1)) );
217 c126aa85 leszek
218
    mScreen.resize(width, height);
219
    }
220
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
223
  void createResources()
224
    {
225
    resourcesCreated = true;
226
227 862fcd79 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
228
    Bitmap bmp;
229
230
    try
231
      {
232
      bmp = BitmapFactory.decodeStream(is);
233
      }
234
    finally
235
      {
236
      try
237
        {
238
        is.close();
239
        }
240
      catch(IOException io) {}
241
      }
242
243 e8b6aa95 Leszek Koltunski
    bmpHeight = bmp.getHeight();
244
    bmpWidth  = bmp.getWidth();
245
246 c126aa85 leszek
    if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
247
    mTexture.setTexture(bmp);
248 862fcd79 Leszek Koltunski
249 fe59d375 Leszek Koltunski
    mScreen.detachAll();
250 c126aa85 leszek
    mScreen.attach(mTexture,mEffects,new MeshFlat(9,9*bmpHeight/bmpWidth));
251 862fcd79 Leszek Koltunski
252 6637d0f2 Leszek Koltunski
    DistortedEffects.enableEffect(EffectNames.DISTORT);
253
254 862fcd79 Leszek Koltunski
    try
255
      {
256 76f9798b Leszek Koltunski
      Distorted.onCreate(mView.getContext());
257 862fcd79 Leszek Koltunski
      }
258
    catch(Exception ex)
259
      {
260 ee76cb8f Leszek Koltunski
      Log.e("PlainMonaLisa", ex.getMessage() );
261 862fcd79 Leszek Koltunski
      }
262
    }
263
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
266 f6d884d5 Leszek Koltunski
  void doFrame(long frameTimeNs)
267 862fcd79 Leszek Koltunski
    {
268 c126aa85 leszek
    // We can still get here after onPaused - ignore such calls.
269 e52f9d96 Leszek Koltunski
    if( PlainMonaLisaSurfaceView.isPaused() )
270
      {
271
      android.util.Log.e("Thread", "Got here after onPaused- ignoring frame draw call!!");
272
      return;
273
      }
274
275 c126aa85 leszek
    // This will happen if we just briefly went into the background (press 'POWER')
276
    // Then surfaceCreated/changed is not called
277
    if( !resourcesCreated )
278
      {
279
      Log.e("Thread", "resources not created!!");
280
      createResources();
281
      }
282
283 862fcd79 Leszek Koltunski
    eglCore.makeCurrent(eglSurface);
284 855ba24e leszek
    mScreen.render( frameTimeNs/1000000 );  // 1 nanosecond = 1 millisecond / 10^-6
285 862fcd79 Leszek Koltunski
    eglCore.swapBuffers(eglSurface);
286
    }
287
288
  }