Project

General

Profile

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

examples / src / main / java / org / distorted / examples / plainmonalisa / RenderThread.java @ 08eabc44

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.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 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
37 08eabc44 Leszek Koltunski
import org.distorted.library.type.Float2D;
38
import org.distorted.library.type.Float3D;
39
import org.distorted.library.type.Float4D;
40 862fcd79 Leszek Koltunski
import org.distorted.examples.R;
41
42
import java.io.IOException;
43
import java.io.InputStream;
44
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46 bc0a685b Leszek Koltunski
47 862fcd79 Leszek Koltunski
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 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 Float2D pLeft, pRight;
67
  private Float4D rLeft, rRight;
68
  private Float3D vLeft, vRight;
69
70
  SurfaceView mView;
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73 bc0a685b Leszek Koltunski
74 862fcd79 Leszek Koltunski
  public RenderThread(SurfaceHolder holder, SurfaceView view)
75
    {
76
    mSurfaceHolder = holder;
77
    mView = view;
78
79
    pLeft = new Float2D( 90, 258);
80
    pRight= new Float2D(176, 255);
81
82
    rLeft = new Float4D(-10,-10,25,25);
83
    rRight= new Float4D( 10, -5,25,25);
84
85
    vLeft = new Float3D(-20,-20,0);
86
    vRight= new Float3D( 20,-10,0);
87
    }
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90 bc0a685b Leszek Koltunski
91 862fcd79 Leszek Koltunski
  @Override
92
  public void run()
93
    {
94
    Looper.prepare();
95
    mHandler = new RenderHandler(this);
96
    eglCore = new EglCore(null, EglCore.FLAG_RECORDABLE | EglCore.FLAG_TRY_GLES3);
97
98
    synchronized (mStartLock)
99
      {
100
      mReady = true;
101
      mStartLock.notify();    // signal waitUntilReady()
102
      }
103
104
    Looper.loop();
105
    Log.d(TAG, "looper quit");
106
107
    checkGlError("releaseGl start");
108
109
    if (eglSurface != null)
110
      {
111
      eglCore.releaseSurface(eglSurface);
112
      eglSurface = EGL14.EGL_NO_SURFACE;
113
      }
114
115
    checkGlError("releaseGl done");
116
117
    eglCore.makeNothingCurrent();
118
    eglCore.release();
119
120
    synchronized (mStartLock)
121
      {
122
      mReady = false;
123
      }
124
    }
125
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127 bc0a685b Leszek Koltunski
128 862fcd79 Leszek Koltunski
  public void waitUntilReady()
129
    {
130
    synchronized (mStartLock)
131
      {
132
      while (!mReady)
133
        {
134
        try
135
          {
136
          mStartLock.wait();
137
          }
138 bc0a685b Leszek Koltunski
        catch (InterruptedException ie) {  }
139 862fcd79 Leszek Koltunski
        }
140
      }
141
    }
142
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144 bc0a685b Leszek Koltunski
145 862fcd79 Leszek Koltunski
  void shutdown()
146
    {
147
    Log.d(TAG, "shutdown");
148
    Looper.myLooper().quit();
149
    }
150
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152 bc0a685b Leszek Koltunski
153 862fcd79 Leszek Koltunski
  public RenderHandler getHandler()
154 bc0a685b Leszek Koltunski
    {
155
    return mHandler;
156
    }
157 862fcd79 Leszek Koltunski
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159 bc0a685b Leszek Koltunski
160 862fcd79 Leszek Koltunski
  void surfaceCreated()
161
    {
162
    Surface surface = mSurfaceHolder.getSurface();
163
164
    eglSurface = eglCore.createWindowSurface(surface);
165
    eglCore.makeCurrent(eglSurface);
166
167
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
168
    Bitmap bmp;
169
170
    try
171
      {
172
      bmp = BitmapFactory.decodeStream(is);
173
      }
174
    finally
175
      {
176
      try
177
        {
178
        is.close();
179
        }
180
      catch(IOException io) {}
181
      }
182
183
    monaLisa = new DistortedBitmap(bmp, 10);
184
    monaLisa.distort( vLeft, rLeft , pLeft, 1000, 0);
185
    monaLisa.distort(vRight, rRight, pRight,1000, 0);
186
187
    bmpHeight = bmp.getHeight();
188
    bmpWidth  = bmp.getWidth();
189
190
    try
191
      {
192
      Distorted.onSurfaceCreated(mView.getContext());
193
      }
194
    catch(Exception ex)
195
      {
196
      Log.e("MonaLisa", ex.getMessage() );
197
      }
198
    }
199
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
202
  void surfaceChanged(int width, int height)
203
    {
204
    Log.d(TAG, "surfaceChanged " + width + "x" + height);
205
206 a8c3ada7 Leszek Koltunski
    monaLisa.abortEffects(EffectTypes.MATRIX);
207 862fcd79 Leszek Koltunski
208
    if( bmpHeight/bmpWidth > height/width )
209
      {
210
      int w = (height*bmpWidth)/bmpHeight;
211
      monaLisa.move((width-w)/2 ,0, 0);
212
      monaLisa.scale((float)height/bmpHeight);
213
      }
214
    else
215
      {
216
      int h = (width*bmpHeight)/bmpWidth;
217
      monaLisa.move(0 ,(height-h)/2, 0);
218
      monaLisa.scale((float)width/bmpWidth);
219
      }
220
221
    Distorted.onSurfaceChanged(width, height);
222
    }
223
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225
226
  public void doFrame(long frameTimeNs)
227
    {
228
    Trace.beginSection("doFrame draw");
229
    eglCore.makeCurrent(eglSurface);
230
231
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
232
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
233
    monaLisa.draw(System.currentTimeMillis());
234
235
    eglCore.swapBuffers(eglSurface);
236
    Trace.endSection();
237
    }
238
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240
241
  public static void checkGlError(String op)
242
    {
243
    int error = GLES20.glGetError();
244
245
    if (error != GLES20.GL_NO_ERROR)
246
      {
247
      String msg = op + ": glError 0x" + Integer.toHexString(error);
248
      Log.e(TAG, msg);
249
      throw new RuntimeException(msg);
250
      }
251
    }
252
  }