Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
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
import org.distorted.library.DistortedEffects;
35
import org.distorted.library.DistortedFramebuffer;
36
import org.distorted.library.GridFlat;
37
import org.distorted.library.DistortedTexture;
38
import org.distorted.library.EffectTypes;
39
import org.distorted.library.type.Dynamic3D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42
import org.distorted.examples.R;
43

    
44
import java.io.IOException;
45
import java.io.InputStream;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
class RenderThread extends Thread
50
  {
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
  private final Object mStartLock = new Object();
60
  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
  private DistortedTexture mTexture;
66
  private DistortedEffects mEffects;
67
  private DistortedFramebuffer mScreen;
68
  private GridFlat mGrid;
69
  private int bmpHeight, bmpWidth;
70
  private SurfaceView mView;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  RenderThread(SurfaceHolder holder, SurfaceView view)
75
    {
76
    mSurfaceHolder = holder;
77
    mView = view;
78

    
79
    Static3D pLeft = new Static3D( 90, 258, 0);
80
    Static3D pRight= new Static3D(176, 255, 0);
81

    
82
    Static4D rLeft = new Static4D(-10,-10,25,25);
83
    Static4D rRight= new Static4D( 10, -5,25,25);
84

    
85
    Dynamic3D dLeft = new Dynamic3D(1000,0.0f);
86
    Dynamic3D dRight= new Dynamic3D(1000,0.0f);
87

    
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

    
94
    mEffects = new DistortedEffects();
95
    mEffects.distort( dLeft, pLeft , rLeft );
96
    mEffects.distort(dRight, pRight, rRight);
97

    
98
    mScreen = new DistortedFramebuffer(0);
99
    }
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
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  @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

    
154
  void waitUntilReady()
155
    {
156
    synchronized (mStartLock)
157
      {
158
      while (!mReady)
159
        {
160
        try
161
          {
162
          mStartLock.wait();
163
          }
164
        catch (InterruptedException ie) {  }
165
        }
166
      }
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  void shutdown()
172
    {
173
    Log.d(TAG, "shutdown");
174
    Looper.myLooper().quit();
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  RenderHandler getHandler()
180
    {
181
    return mHandler;
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  void surfaceCreated()
187
    {
188
    Surface surface = mSurfaceHolder.getSurface();
189

    
190
    eglSurface = eglCore.createWindowSurface(surface);
191
    eglCore.makeCurrent(eglSurface);
192

    
193
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
194

    
195
    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
    bmpHeight = bmp.getHeight();
212
    bmpWidth  = bmp.getWidth();
213

    
214
    mTexture = new DistortedTexture(bmpWidth,bmpHeight);
215
    mTexture.setTexture(bmp);
216

    
217
    mGrid= new GridFlat(9,9*bmpHeight/bmpWidth);  // more-or-less square Grid with 9 columns.
218

    
219
    try
220
      {
221
      Distorted.onSurfaceCreated(mView.getContext());
222
      }
223
    catch(Exception ex)
224
      {
225
      Log.e("PlainMonaLisa", ex.getMessage() );
226
      }
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  void surfaceChanged(int width, int height)
232
    {
233
    Log.d(TAG, "surfaceChanged " + width + "x" + height);
234

    
235
    mEffects.abortEffects(EffectTypes.MATRIX);
236

    
237
    if( (float)bmpHeight/bmpWidth > (float)height/width )
238
      {
239
      int w = (height*bmpWidth)/bmpHeight;
240
      float factor = (float)height/bmpHeight;
241

    
242
      mEffects.move( new Static3D((width-w)/2,0,0) );
243
      mEffects.scale( factor );
244
      }
245
    else
246
      {
247
      int h = (width*bmpHeight)/bmpWidth;
248
      float factor = (float)width/bmpWidth;
249

    
250
      mEffects.move( new Static3D(0,(height-h)/2,0) );
251
      mEffects.scale( factor );
252
      }
253

    
254
    mScreen.resize(width, height);
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  void doFrame(long frameTimeNs)
260
    {
261
    if( PlainMonaLisaSurfaceView.isPaused() )
262
      {
263
      android.util.Log.e("Thread", "Got here after onPaused- ignoring frame draw call!!");
264
      return;
265
      }
266

    
267
    eglCore.makeCurrent(eglSurface);
268

    
269
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
270
    mScreen.renderTo(mTexture, mGrid, mEffects, System.currentTimeMillis() );
271

    
272
    eglCore.swapBuffers(eglSurface);
273
    }
274

    
275
  }
(5-5/5)