Project

General

Profile

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

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

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.os.Trace;
29
import android.util.Log;
30
import android.view.Surface;
31
import android.view.SurfaceHolder;
32
import android.view.SurfaceView;
33

    
34
import org.distorted.library.Distorted;
35
import org.distorted.library.DistortedBitmap;
36
import org.distorted.library.EffectTypes;
37
import org.distorted.library.type.Dynamic3D;
38
import org.distorted.library.type.Static2D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41
import org.distorted.examples.R;
42

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

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
public class RenderThread extends Thread
49
  {
50
  private static final String TAG = "RenderThread";
51

    
52
  // Object must be created on render thread to get correct Looper, but is used from
53
  // UI thread, so we need to declare it volatile to ensure the UI thread sees a fully
54
  // constructed object.
55
  private volatile RenderHandler mHandler;
56

    
57
  // Used to wait for the thread to start.
58
  private Object mStartLock = new Object();
59
  private boolean mReady = false;
60
  private volatile SurfaceHolder mSurfaceHolder;  // may be updated by UI thread
61
  private EglCore eglCore;
62
  private EGLSurface eglSurface;
63

    
64
  private DistortedBitmap monaLisa;
65
  private int bmpHeight, bmpWidth;
66

    
67
  private Static2D pLeft, pRight;
68
  private Static4D rLeft, rRight;
69

    
70
  private Dynamic3D dLeft, dRight;
71

    
72
  SurfaceView mView;
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  public RenderThread(SurfaceHolder holder, SurfaceView view)
77
    {
78
    mSurfaceHolder = holder;
79
    mView = view;
80

    
81
    pLeft = new Static2D( 90, 258);
82
    pRight= new Static2D(176, 255);
83

    
84
    rLeft = new Static4D(-10,-10,25,25);
85
    rRight= new Static4D( 10, -5,25,25);
86

    
87
    dLeft = new Dynamic3D(1000,0.0f);
88
    dRight= new Dynamic3D(1000,0.0f);
89

    
90
    dLeft.add( new Static3D(  0,  0,0) );
91
    dLeft.add( new Static3D(-20,-20,0) );
92

    
93
    dRight.add( new Static3D(  0,  0,0) );
94
    dRight.add( new Static3D( 20,-10,0) );
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  @Override
100
  public void run()
101
    {
102
    Looper.prepare();
103
    mHandler = new RenderHandler(this);
104
    eglCore = new EglCore(null, EglCore.FLAG_RECORDABLE | EglCore.FLAG_TRY_GLES3);
105

    
106
    synchronized (mStartLock)
107
      {
108
      mReady = true;
109
      mStartLock.notify();    // signal waitUntilReady()
110
      }
111

    
112
    Looper.loop();
113
    Log.d(TAG, "looper quit");
114

    
115
    checkGlError("releaseGl start");
116

    
117
    if (eglSurface != null)
118
      {
119
      eglCore.releaseSurface(eglSurface);
120
      eglSurface = EGL14.EGL_NO_SURFACE;
121
      }
122

    
123
    checkGlError("releaseGl done");
124

    
125
    eglCore.makeNothingCurrent();
126
    eglCore.release();
127

    
128
    synchronized (mStartLock)
129
      {
130
      mReady = false;
131
      }
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  public void waitUntilReady()
137
    {
138
    synchronized (mStartLock)
139
      {
140
      while (!mReady)
141
        {
142
        try
143
          {
144
          mStartLock.wait();
145
          }
146
        catch (InterruptedException ie) {  }
147
        }
148
      }
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  void shutdown()
154
    {
155
    Log.d(TAG, "shutdown");
156
    Looper.myLooper().quit();
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  public RenderHandler getHandler()
162
    {
163
    return mHandler;
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  void surfaceCreated()
169
    {
170
    Surface surface = mSurfaceHolder.getSurface();
171

    
172
    eglSurface = eglCore.createWindowSurface(surface);
173
    eglCore.makeCurrent(eglSurface);
174

    
175
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
176
    Bitmap bmp;
177

    
178
    try
179
      {
180
      bmp = BitmapFactory.decodeStream(is);
181
      }
182
    finally
183
      {
184
      try
185
        {
186
        is.close();
187
        }
188
      catch(IOException io) {}
189
      }
190

    
191
    monaLisa = new DistortedBitmap(bmp, 10);
192
    monaLisa.distort( dLeft, pLeft , rLeft );
193
    monaLisa.distort(dRight, pRight, rRight);
194

    
195
    bmpHeight = bmp.getHeight();
196
    bmpWidth  = bmp.getWidth();
197

    
198
    try
199
      {
200
      Distorted.onSurfaceCreated(mView.getContext());
201
      }
202
    catch(Exception ex)
203
      {
204
      Log.e("MonaLisa", ex.getMessage() );
205
      }
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  void surfaceChanged(int width, int height)
211
    {
212
    Log.d(TAG, "surfaceChanged " + width + "x" + height);
213

    
214
    monaLisa.abortEffects(EffectTypes.MATRIX);
215

    
216
    if( bmpHeight/bmpWidth > height/width )
217
      {
218
      int w = (height*bmpWidth)/bmpHeight;
219
      float factor = (float)height/bmpHeight;
220

    
221
      monaLisa.move( new Static3D((width-w)/2,0,0) );
222
      monaLisa.scale( factor );
223
      }
224
    else
225
      {
226
      int h = (width*bmpHeight)/bmpWidth;
227
      float factor = (float)width/bmpWidth;
228

    
229
      monaLisa.move( new Static3D(0,(height-h)/2,0) );
230
      monaLisa.scale( factor );
231
      }
232

    
233
    Distorted.onSurfaceChanged(width, height);
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  public void doFrame(long frameTimeNs)
239
    {
240
    Trace.beginSection("doFrame draw");
241
    eglCore.makeCurrent(eglSurface);
242

    
243
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
244
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
245
    monaLisa.draw(System.currentTimeMillis());
246

    
247
    eglCore.swapBuffers(eglSurface);
248
    Trace.endSection();
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  public static void checkGlError(String op)
254
    {
255
    int error = GLES20.glGetError();
256

    
257
    if (error != GLES20.GL_NO_ERROR)
258
      {
259
      String msg = op + ": glError 0x" + Integer.toHexString(error);
260
      Log.e(TAG, msg);
261
      throw new RuntimeException(msg);
262
      }
263
    }
264
  }
(5-5/5)