Project

General

Profile

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

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

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 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
36 d218d64e leszek
import org.distorted.library.DistortedScreen;
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 f6d884d5 Leszek Koltunski
  private DistortedTexture mTexture;
67 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
68 d218d64e leszek
  private DistortedScreen mScreen;
69 b01acdaf Leszek Koltunski
  private MeshFlat mMesh;
70 862fcd79 Leszek Koltunski
  private int bmpHeight, bmpWidth;
71 f6d884d5 Leszek Koltunski
  private SurfaceView mView;
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
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104
  private static void checkGlError(String op)
105
    {
106 41a81a14 Leszek Koltunski
    int error = GLES30.glGetError();
107 f6d884d5 Leszek Koltunski
108 41a81a14 Leszek Koltunski
    if (error != GLES30.GL_NO_ERROR)
109 f6d884d5 Leszek Koltunski
      {
110
      String msg = op + ": glError 0x" + Integer.toHexString(error);
111
      Log.e(TAG, msg);
112 6b9f9441 Leszek Koltunski
//      throw new RuntimeException(msg);
113 f6d884d5 Leszek Koltunski
      }
114 862fcd79 Leszek Koltunski
    }
115
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117 bc0a685b Leszek Koltunski
118 862fcd79 Leszek Koltunski
  @Override
119
  public void run()
120
    {
121
    Looper.prepare();
122
    mHandler = new RenderHandler(this);
123
    eglCore = new EglCore(null, EglCore.FLAG_RECORDABLE | EglCore.FLAG_TRY_GLES3);
124
125
    synchronized (mStartLock)
126
      {
127
      mReady = true;
128
      mStartLock.notify();    // signal waitUntilReady()
129
      }
130
131
    Looper.loop();
132
    Log.d(TAG, "looper quit");
133
134
    checkGlError("releaseGl start");
135
136
    if (eglSurface != null)
137
      {
138
      eglCore.releaseSurface(eglSurface);
139
      eglSurface = EGL14.EGL_NO_SURFACE;
140
      }
141
142
    checkGlError("releaseGl done");
143
144
    eglCore.makeNothingCurrent();
145
    eglCore.release();
146
147
    synchronized (mStartLock)
148
      {
149
      mReady = false;
150
      }
151
    }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154 bc0a685b Leszek Koltunski
155 f6d884d5 Leszek Koltunski
  void waitUntilReady()
156 862fcd79 Leszek Koltunski
    {
157
    synchronized (mStartLock)
158
      {
159
      while (!mReady)
160
        {
161
        try
162
          {
163
          mStartLock.wait();
164
          }
165 bc0a685b Leszek Koltunski
        catch (InterruptedException ie) {  }
166 862fcd79 Leszek Koltunski
        }
167
      }
168
    }
169
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171 bc0a685b Leszek Koltunski
172 862fcd79 Leszek Koltunski
  void shutdown()
173
    {
174
    Log.d(TAG, "shutdown");
175
    Looper.myLooper().quit();
176
    }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179 bc0a685b Leszek Koltunski
180 f6d884d5 Leszek Koltunski
  RenderHandler getHandler()
181 bc0a685b Leszek Koltunski
    {
182
    return mHandler;
183
    }
184 862fcd79 Leszek Koltunski
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186 bc0a685b Leszek Koltunski
187 862fcd79 Leszek Koltunski
  void surfaceCreated()
188
    {
189
    Surface surface = mSurfaceHolder.getSurface();
190
191
    eglSurface = eglCore.createWindowSurface(surface);
192
    eglCore.makeCurrent(eglSurface);
193
194 41a81a14 Leszek Koltunski
    GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
195 e7a4ef16 Leszek Koltunski
196 862fcd79 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
197
    Bitmap bmp;
198
199
    try
200
      {
201
      bmp = BitmapFactory.decodeStream(is);
202
      }
203
    finally
204
      {
205
      try
206
        {
207
        is.close();
208
        }
209
      catch(IOException io) {}
210
      }
211
212 e8b6aa95 Leszek Koltunski
    bmpHeight = bmp.getHeight();
213
    bmpWidth  = bmp.getWidth();
214
215 7451c98a Leszek Koltunski
    mTexture = new DistortedTexture(bmpWidth,bmpHeight);
216 f6d884d5 Leszek Koltunski
    mTexture.setTexture(bmp);
217 862fcd79 Leszek Koltunski
218 b01acdaf Leszek Koltunski
    mMesh = new MeshFlat(9,9*bmpHeight/bmpWidth);  // more-or-less square Grid with 9 columns.
219 862fcd79 Leszek Koltunski
220
    try
221
      {
222 76f9798b Leszek Koltunski
      Distorted.onCreate(mView.getContext());
223 862fcd79 Leszek Koltunski
      }
224
    catch(Exception ex)
225
      {
226 ee76cb8f Leszek Koltunski
      Log.e("PlainMonaLisa", ex.getMessage() );
227 862fcd79 Leszek Koltunski
      }
228
    }
229
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231
232
  void surfaceChanged(int width, int height)
233
    {
234
    Log.d(TAG, "surfaceChanged " + width + "x" + height);
235
236 392e16fd Leszek Koltunski
    mEffects.abortEffects(EffectTypes.MATRIX);
237 862fcd79 Leszek Koltunski
238 5055b5d4 Leszek Koltunski
    if( (float)bmpHeight/bmpWidth > (float)height/width )
239 862fcd79 Leszek Koltunski
      {
240
      int w = (height*bmpWidth)/bmpHeight;
241 7589635e Leszek Koltunski
      float factor = (float)height/bmpHeight;
242
243 392e16fd Leszek Koltunski
      mEffects.move( new Static3D((width-w)/2,0,0) );
244
      mEffects.scale( factor );
245 862fcd79 Leszek Koltunski
      }
246
    else
247
      {
248
      int h = (width*bmpHeight)/bmpWidth;
249 7589635e Leszek Koltunski
      float factor = (float)width/bmpWidth;
250
251 392e16fd Leszek Koltunski
      mEffects.move( new Static3D(0,(height-h)/2,0) );
252
      mEffects.scale( factor );
253 862fcd79 Leszek Koltunski
      }
254
255 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
256 862fcd79 Leszek Koltunski
    }
257
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
260 f6d884d5 Leszek Koltunski
  void doFrame(long frameTimeNs)
261 862fcd79 Leszek Koltunski
    {
262 e52f9d96 Leszek Koltunski
    if( PlainMonaLisaSurfaceView.isPaused() )
263
      {
264
      android.util.Log.e("Thread", "Got here after onPaused- ignoring frame draw call!!");
265
      return;
266
      }
267
268 862fcd79 Leszek Koltunski
    eglCore.makeCurrent(eglSurface);
269
270 41a81a14 Leszek Koltunski
    GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
271 b01acdaf Leszek Koltunski
    mScreen.renderTo(mTexture, mMesh, mEffects, System.currentTimeMillis() );
272 862fcd79 Leszek Koltunski
273
    eglCore.swapBuffers(eglSurface);
274
    }
275
276
  }