Project

General

Profile

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

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

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
import android.opengl.GLES20;
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.Distorted;
34 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
35 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
36 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
37 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
38 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
39 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
40 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42 862fcd79 Leszek Koltunski
import org.distorted.examples.R;
43
44
import java.io.IOException;
45
import java.io.InputStream;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48 bc0a685b Leszek Koltunski
49 f6d884d5 Leszek Koltunski
class RenderThread extends Thread
50 862fcd79 Leszek Koltunski
  {
51
  private static final String TAG = "RenderThread";
52
53
  // Object must be created on render thread to get correct Looper, but is used from
54
  // UI thread, so we need to declare it volatile to ensure the UI thread sees a fully
55
  // constructed object.
56
  private volatile RenderHandler mHandler;
57
58
  // Used to wait for the thread to start.
59 ee76cb8f Leszek Koltunski
  private final Object mStartLock = new Object();
60 862fcd79 Leszek Koltunski
  private boolean mReady = false;
61
  private volatile SurfaceHolder mSurfaceHolder;  // may be updated by UI thread
62
  private EglCore eglCore;
63
  private EGLSurface eglSurface;
64
65 f6d884d5 Leszek Koltunski
  private DistortedTexture mTexture;
66 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
67 392e16fd Leszek Koltunski
  private DistortedFramebuffer mScreen;
68 b01acdaf Leszek Koltunski
  private MeshFlat mMesh;
69 862fcd79 Leszek Koltunski
  private int bmpHeight, bmpWidth;
70 f6d884d5 Leszek Koltunski
  private SurfaceView mView;
71 862fcd79 Leszek Koltunski
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73 bc0a685b Leszek Koltunski
74 f6d884d5 Leszek Koltunski
  RenderThread(SurfaceHolder holder, SurfaceView view)
75 862fcd79 Leszek Koltunski
    {
76
    mSurfaceHolder = holder;
77
    mView = view;
78
79 f6d884d5 Leszek Koltunski
    Static3D pLeft = new Static3D( 90, 258, 0);
80
    Static3D pRight= new Static3D(176, 255, 0);
81 862fcd79 Leszek Koltunski
82 f6d884d5 Leszek Koltunski
    Static4D rLeft = new Static4D(-10,-10,25,25);
83
    Static4D rRight= new Static4D( 10, -5,25,25);
84 862fcd79 Leszek Koltunski
85 f6d884d5 Leszek Koltunski
    Dynamic3D dLeft = new Dynamic3D(1000,0.0f);
86
    Dynamic3D dRight= new Dynamic3D(1000,0.0f);
87 7bf107f7 Leszek Koltunski
88
    dLeft.add( new Static3D(  0,  0,0) );
89
    dLeft.add( new Static3D(-20,-20,0) );
90
91
    dRight.add( new Static3D(  0,  0,0) );
92
    dRight.add( new Static3D( 20,-10,0) );
93 f6d884d5 Leszek Koltunski
94 d04a4886 Leszek Koltunski
    mEffects = new DistortedEffects();
95 392e16fd Leszek Koltunski
    mEffects.distort( dLeft, pLeft , rLeft );
96
    mEffects.distort(dRight, pRight, rRight);
97
98
    mScreen = new DistortedFramebuffer(0);
99 f6d884d5 Leszek Koltunski
    }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103
  private static void checkGlError(String op)
104
    {
105
    int error = GLES20.glGetError();
106
107
    if (error != GLES20.GL_NO_ERROR)
108
      {
109
      String msg = op + ": glError 0x" + Integer.toHexString(error);
110
      Log.e(TAG, msg);
111
      throw new RuntimeException(msg);
112
      }
113 862fcd79 Leszek Koltunski
    }
114
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116 bc0a685b Leszek Koltunski
117 862fcd79 Leszek Koltunski
  @Override
118
  public void run()
119
    {
120
    Looper.prepare();
121
    mHandler = new RenderHandler(this);
122
    eglCore = new EglCore(null, EglCore.FLAG_RECORDABLE | EglCore.FLAG_TRY_GLES3);
123
124
    synchronized (mStartLock)
125
      {
126
      mReady = true;
127
      mStartLock.notify();    // signal waitUntilReady()
128
      }
129
130
    Looper.loop();
131
    Log.d(TAG, "looper quit");
132
133
    checkGlError("releaseGl start");
134
135
    if (eglSurface != null)
136
      {
137
      eglCore.releaseSurface(eglSurface);
138
      eglSurface = EGL14.EGL_NO_SURFACE;
139
      }
140
141
    checkGlError("releaseGl done");
142
143
    eglCore.makeNothingCurrent();
144
    eglCore.release();
145
146
    synchronized (mStartLock)
147
      {
148
      mReady = false;
149
      }
150
    }
151
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153 bc0a685b Leszek Koltunski
154 f6d884d5 Leszek Koltunski
  void waitUntilReady()
155 862fcd79 Leszek Koltunski
    {
156
    synchronized (mStartLock)
157
      {
158
      while (!mReady)
159
        {
160
        try
161
          {
162
          mStartLock.wait();
163
          }
164 bc0a685b Leszek Koltunski
        catch (InterruptedException ie) {  }
165 862fcd79 Leszek Koltunski
        }
166
      }
167
    }
168
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170 bc0a685b Leszek Koltunski
171 862fcd79 Leszek Koltunski
  void shutdown()
172
    {
173
    Log.d(TAG, "shutdown");
174
    Looper.myLooper().quit();
175
    }
176
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178 bc0a685b Leszek Koltunski
179 f6d884d5 Leszek Koltunski
  RenderHandler getHandler()
180 bc0a685b Leszek Koltunski
    {
181
    return mHandler;
182
    }
183 862fcd79 Leszek Koltunski
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185 bc0a685b Leszek Koltunski
186 862fcd79 Leszek Koltunski
  void surfaceCreated()
187
    {
188
    Surface surface = mSurfaceHolder.getSurface();
189
190
    eglSurface = eglCore.createWindowSurface(surface);
191
    eglCore.makeCurrent(eglSurface);
192
193 e7a4ef16 Leszek Koltunski
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
194
195 862fcd79 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
196
    Bitmap bmp;
197
198
    try
199
      {
200
      bmp = BitmapFactory.decodeStream(is);
201
      }
202
    finally
203
      {
204
      try
205
        {
206
        is.close();
207
        }
208
      catch(IOException io) {}
209
      }
210
211 e8b6aa95 Leszek Koltunski
    bmpHeight = bmp.getHeight();
212
    bmpWidth  = bmp.getWidth();
213
214 7451c98a Leszek Koltunski
    mTexture = new DistortedTexture(bmpWidth,bmpHeight);
215 f6d884d5 Leszek Koltunski
    mTexture.setTexture(bmp);
216 862fcd79 Leszek Koltunski
217 b01acdaf Leszek Koltunski
    mMesh = new MeshFlat(9,9*bmpHeight/bmpWidth);  // more-or-less square Grid with 9 columns.
218 862fcd79 Leszek Koltunski
219
    try
220
      {
221 76f9798b Leszek Koltunski
      Distorted.onCreate(mView.getContext());
222 862fcd79 Leszek Koltunski
      }
223
    catch(Exception ex)
224
      {
225 ee76cb8f Leszek Koltunski
      Log.e("PlainMonaLisa", ex.getMessage() );
226 862fcd79 Leszek Koltunski
      }
227
    }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
  void surfaceChanged(int width, int height)
232
    {
233
    Log.d(TAG, "surfaceChanged " + width + "x" + height);
234
235 392e16fd Leszek Koltunski
    mEffects.abortEffects(EffectTypes.MATRIX);
236 862fcd79 Leszek Koltunski
237 5055b5d4 Leszek Koltunski
    if( (float)bmpHeight/bmpWidth > (float)height/width )
238 862fcd79 Leszek Koltunski
      {
239
      int w = (height*bmpWidth)/bmpHeight;
240 7589635e Leszek Koltunski
      float factor = (float)height/bmpHeight;
241
242 392e16fd Leszek Koltunski
      mEffects.move( new Static3D((width-w)/2,0,0) );
243
      mEffects.scale( factor );
244 862fcd79 Leszek Koltunski
      }
245
    else
246
      {
247
      int h = (width*bmpHeight)/bmpWidth;
248 7589635e Leszek Koltunski
      float factor = (float)width/bmpWidth;
249
250 392e16fd Leszek Koltunski
      mEffects.move( new Static3D(0,(height-h)/2,0) );
251
      mEffects.scale( factor );
252 862fcd79 Leszek Koltunski
      }
253
254 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
255 862fcd79 Leszek Koltunski
    }
256
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258
259 f6d884d5 Leszek Koltunski
  void doFrame(long frameTimeNs)
260 862fcd79 Leszek Koltunski
    {
261 e52f9d96 Leszek Koltunski
    if( PlainMonaLisaSurfaceView.isPaused() )
262
      {
263
      android.util.Log.e("Thread", "Got here after onPaused- ignoring frame draw call!!");
264
      return;
265
      }
266
267 862fcd79 Leszek Koltunski
    eglCore.makeCurrent(eglSurface);
268
269
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
270 b01acdaf Leszek Koltunski
    mScreen.renderTo(mTexture, mMesh, mEffects, System.currentTimeMillis() );
271 862fcd79 Leszek Koltunski
272
    eglCore.swapBuffers(eglSurface);
273
    }
274
275
  }