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.surfaceview;
|
21
|
|
22
|
import android.os.Handler;
|
23
|
import android.os.Message;
|
24
|
import android.util.Log;
|
25
|
|
26
|
import java.lang.ref.WeakReference;
|
27
|
|
28
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
29
|
// Used for messages sent from the UI thread to the render thread.
|
30
|
|
31
|
class RenderHandler extends Handler
|
32
|
{
|
33
|
private static final String TAG = "RenderHandler";
|
34
|
|
35
|
private static final int MSG_SURFACE_CREATED = 0;
|
36
|
private static final int MSG_SURFACE_CHANGED = 1;
|
37
|
private static final int MSG_DO_FRAME = 2;
|
38
|
private static final int MSG_SHUTDOWN = 4;
|
39
|
|
40
|
private WeakReference<RenderThread> mWeakRenderThread;
|
41
|
|
42
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43
|
|
44
|
RenderHandler(RenderThread rt)
|
45
|
{
|
46
|
mWeakRenderThread = new WeakReference<>(rt);
|
47
|
}
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
void sendSurfaceCreated()
|
52
|
{
|
53
|
sendMessage(obtainMessage(RenderHandler.MSG_SURFACE_CREATED));
|
54
|
}
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
|
58
|
void sendSurfaceChanged(int format, int width, int height)
|
59
|
{
|
60
|
sendMessage(obtainMessage(RenderHandler.MSG_SURFACE_CHANGED, width, height));
|
61
|
}
|
62
|
|
63
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
64
|
|
65
|
void sendDoFrame(long frameTimeNanos)
|
66
|
{
|
67
|
sendMessage(obtainMessage(RenderHandler.MSG_DO_FRAME, (int) (frameTimeNanos >> 32), (int) frameTimeNanos));
|
68
|
}
|
69
|
|
70
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71
|
|
72
|
void sendShutdown()
|
73
|
{
|
74
|
sendMessage(obtainMessage(RenderHandler.MSG_SHUTDOWN));
|
75
|
}
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
@Override // runs on RenderThread
|
79
|
public void handleMessage(Message msg)
|
80
|
{
|
81
|
int what = msg.what;
|
82
|
|
83
|
RenderThread renderThread = mWeakRenderThread.get();
|
84
|
|
85
|
if (renderThread == null)
|
86
|
{
|
87
|
Log.w(TAG, "RenderHandler.handleMessage: weak ref is null");
|
88
|
return;
|
89
|
}
|
90
|
|
91
|
switch (what)
|
92
|
{
|
93
|
case MSG_SURFACE_CREATED: renderThread.surfaceCreated();
|
94
|
break;
|
95
|
case MSG_SURFACE_CHANGED: renderThread.surfaceChanged(msg.arg1, msg.arg2);
|
96
|
break;
|
97
|
case MSG_DO_FRAME: long timestamp = (((long) msg.arg1) << 32) | (((long) msg.arg2) & 0xffffffffL);
|
98
|
renderThread.doFrame(timestamp);
|
99
|
break;
|
100
|
case MSG_SHUTDOWN: renderThread.shutdown();
|
101
|
break;
|
102
|
default: throw new RuntimeException("unknown message " + what);
|
103
|
}
|
104
|
}
|
105
|
}
|
106
|
|
107
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|