Project

General

Profile

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

examples / src / main / java / org / distorted / examples / plainmonalisa / RenderThread.java @ 6637d0f2

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