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.content.Context;
|
23
|
import android.view.Choreographer;
|
24
|
import android.view.SurfaceView;
|
25
|
import android.view.SurfaceHolder;
|
26
|
import android.util.Log;
|
27
|
|
28
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
29
|
|
30
|
class PlainMonaLisaSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Choreographer.FrameCallback
|
31
|
{
|
32
|
private static final String TAG = "MonaLisaSurface";
|
33
|
private RenderThread mRenderThread;
|
34
|
|
35
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
36
|
|
37
|
public PlainMonaLisaSurfaceView(Context context)
|
38
|
{
|
39
|
super(context);
|
40
|
getHolder().addCallback(this);
|
41
|
}
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
|
45
|
public void surfaceCreated(SurfaceHolder holder)
|
46
|
{
|
47
|
Log.d(TAG, "surfaceCreated holder=" + holder);
|
48
|
|
49
|
mRenderThread = new RenderThread(holder, this);
|
50
|
mRenderThread.setName("GL render");
|
51
|
mRenderThread.start();
|
52
|
mRenderThread.waitUntilReady();
|
53
|
|
54
|
RenderHandler rh = mRenderThread.getHandler();
|
55
|
|
56
|
if (rh != null)
|
57
|
{
|
58
|
rh.sendSurfaceCreated();
|
59
|
}
|
60
|
|
61
|
Choreographer.getInstance().postFrameCallback(this);
|
62
|
}
|
63
|
|
64
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
65
|
|
66
|
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
|
67
|
{
|
68
|
Log.d(TAG, "surfaceChanged fmt=" + format + " size=" + width + "x" + height +" holder=" + holder);
|
69
|
|
70
|
RenderHandler rh = mRenderThread.getHandler();
|
71
|
|
72
|
if (rh != null)
|
73
|
{
|
74
|
rh.sendSurfaceChanged(format, width, height);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79
|
|
80
|
public void surfaceDestroyed(SurfaceHolder holder)
|
81
|
{
|
82
|
Log.d(TAG, "surfaceDestroyed holder=" + holder);
|
83
|
|
84
|
// We need to wait for the render thread to shut down before continuing because we
|
85
|
// don't want the Surface to disappear out from under it mid-render. The frame
|
86
|
// notifications will have been stopped back in onPause(), but there might have
|
87
|
// been one in progress.
|
88
|
|
89
|
RenderHandler rh = mRenderThread.getHandler();
|
90
|
|
91
|
if (rh != null)
|
92
|
{
|
93
|
rh.sendShutdown();
|
94
|
|
95
|
try
|
96
|
{
|
97
|
mRenderThread.join();
|
98
|
}
|
99
|
catch (InterruptedException ie)
|
100
|
{
|
101
|
throw new RuntimeException("join was interrupted", ie);
|
102
|
}
|
103
|
}
|
104
|
mRenderThread = null;
|
105
|
|
106
|
Log.d(TAG, "surfaceDestroyed complete");
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
|
111
|
public void onPause()
|
112
|
{
|
113
|
Log.d(TAG, "onPause unhooking choreographer");
|
114
|
Choreographer.getInstance().removeFrameCallback(this);
|
115
|
}
|
116
|
|
117
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118
|
|
119
|
public void onResume()
|
120
|
{
|
121
|
if (mRenderThread != null)
|
122
|
{
|
123
|
Log.d(TAG, "onResume re-hooking choreographer");
|
124
|
Choreographer.getInstance().postFrameCallback(this);
|
125
|
}
|
126
|
}
|
127
|
|
128
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
129
|
|
130
|
@Override
|
131
|
public void doFrame(long frameTimeNanos)
|
132
|
{
|
133
|
RenderHandler rh = mRenderThread.getHandler();
|
134
|
|
135
|
if (rh != null)
|
136
|
{
|
137
|
Choreographer.getInstance().postFrameCallback(this);
|
138
|
rh.sendDoFrame(frameTimeNanos);
|
139
|
}
|
140
|
}
|
141
|
}
|
142
|
|
143
|
|