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 Static3D 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 Static3D( 90, 258, 0);
|
81
|
pRight= new Static3D(176, 255, 0);
|
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
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
175
|
|
176
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
|
177
|
Bitmap bmp;
|
178
|
|
179
|
try
|
180
|
{
|
181
|
bmp = BitmapFactory.decodeStream(is);
|
182
|
}
|
183
|
finally
|
184
|
{
|
185
|
try
|
186
|
{
|
187
|
is.close();
|
188
|
}
|
189
|
catch(IOException io) {}
|
190
|
}
|
191
|
|
192
|
monaLisa = new DistortedBitmap(bmp, 9);
|
193
|
monaLisa.distort( dLeft, pLeft , rLeft );
|
194
|
monaLisa.distort(dRight, pRight, rRight);
|
195
|
|
196
|
bmpHeight = bmp.getHeight();
|
197
|
bmpWidth = bmp.getWidth();
|
198
|
|
199
|
try
|
200
|
{
|
201
|
Distorted.onSurfaceCreated(mView.getContext());
|
202
|
}
|
203
|
catch(Exception ex)
|
204
|
{
|
205
|
Log.e("PlainMonaLisa", ex.getMessage() );
|
206
|
}
|
207
|
}
|
208
|
|
209
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
210
|
|
211
|
void surfaceChanged(int width, int height)
|
212
|
{
|
213
|
Log.d(TAG, "surfaceChanged " + width + "x" + height);
|
214
|
|
215
|
monaLisa.abortEffects(EffectTypes.MATRIX);
|
216
|
|
217
|
if( bmpHeight/bmpWidth > height/width )
|
218
|
{
|
219
|
int w = (height*bmpWidth)/bmpHeight;
|
220
|
float factor = (float)height/bmpHeight;
|
221
|
|
222
|
monaLisa.move( new Static3D((width-w)/2,0,0) );
|
223
|
monaLisa.scale( factor );
|
224
|
}
|
225
|
else
|
226
|
{
|
227
|
int h = (width*bmpHeight)/bmpWidth;
|
228
|
float factor = (float)width/bmpWidth;
|
229
|
|
230
|
monaLisa.move( new Static3D(0,(height-h)/2,0) );
|
231
|
monaLisa.scale( factor );
|
232
|
}
|
233
|
|
234
|
Distorted.onSurfaceChanged(width, height);
|
235
|
}
|
236
|
|
237
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
238
|
|
239
|
public void doFrame(long frameTimeNs)
|
240
|
{
|
241
|
if( PlainMonaLisaSurfaceView.isPaused() )
|
242
|
{
|
243
|
android.util.Log.e("Thread", "Got here after onPaused- ignoring frame draw call!!");
|
244
|
return;
|
245
|
}
|
246
|
|
247
|
eglCore.makeCurrent(eglSurface);
|
248
|
|
249
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
250
|
monaLisa.draw(System.currentTimeMillis());
|
251
|
|
252
|
eglCore.swapBuffers(eglSurface);
|
253
|
}
|
254
|
|
255
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
256
|
|
257
|
public static void checkGlError(String op)
|
258
|
{
|
259
|
int error = GLES20.glGetError();
|
260
|
|
261
|
if (error != GLES20.GL_NO_ERROR)
|
262
|
{
|
263
|
String msg = op + ": glError 0x" + Integer.toHexString(error);
|
264
|
Log.e(TAG, msg);
|
265
|
throw new RuntimeException(msg);
|
266
|
}
|
267
|
}
|
268
|
}
|