Project

General

Profile

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

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

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.DistortedBitmap;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.Static2D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40
import org.distorted.examples.R;
41

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

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

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

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

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

    
69
  private Dynamic3D dLeft, dRight;
70

    
71
  SurfaceView mView;
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

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

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

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

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

    
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
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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

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

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

    
114
    checkGlError("releaseGl start");
115

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

    
122
    checkGlError("releaseGl done");
123

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

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

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

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

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

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

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

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

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

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

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

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

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

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

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

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

    
213
    monaLisa.abortEffects(EffectTypes.MATRIX);
214

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

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

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

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

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  public void doFrame(long frameTimeNs)
238
    {
239
    if( PlainMonaLisaSurfaceView.isPaused() )
240
      {
241
      android.util.Log.e("Thread", "Got here after onPaused- ignoring frame draw call!!");
242
      return;
243
      }
244

    
245
    eglCore.makeCurrent(eglSurface);
246

    
247
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
248
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
249
    monaLisa.draw(System.currentTimeMillis());
250

    
251
    eglCore.swapBuffers(eglSurface);
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  public static void checkGlError(String op)
257
    {
258
    int error = GLES20.glGetError();
259

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