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.os.Handler;
|
23 |
|
|
import android.os.Message;
|
24 |
|
|
import android.util.Log;
|
25 |
|
|
|
26 |
|
|
import java.lang.ref.WeakReference;
|
27 |
|
|
|
28 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
29 |
bc0a685b
|
Leszek Koltunski
|
// Used for messages sent from the UI thread to the render thread.
|
30 |
862fcd79
|
Leszek Koltunski
|
|
31 |
f6d884d5
|
Leszek Koltunski
|
class RenderHandler extends Handler
|
32 |
862fcd79
|
Leszek Koltunski
|
{
|
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 |
bc0a685b
|
Leszek Koltunski
|
|
44 |
f6d884d5
|
Leszek Koltunski
|
RenderHandler(RenderThread rt)
|
45 |
bc0a685b
|
Leszek Koltunski
|
{
|
46 |
|
|
mWeakRenderThread = new WeakReference<>(rt);
|
47 |
|
|
}
|
48 |
862fcd79
|
Leszek Koltunski
|
|
49 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50 |
bc0a685b
|
Leszek Koltunski
|
|
51 |
f6d884d5
|
Leszek Koltunski
|
void sendSurfaceCreated()
|
52 |
862fcd79
|
Leszek Koltunski
|
{
|
53 |
|
|
sendMessage(obtainMessage(RenderHandler.MSG_SURFACE_CREATED));
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57 |
bc0a685b
|
Leszek Koltunski
|
|
58 |
f6d884d5
|
Leszek Koltunski
|
void sendSurfaceChanged(int format, int width, int height)
|
59 |
862fcd79
|
Leszek Koltunski
|
{
|
60 |
|
|
sendMessage(obtainMessage(RenderHandler.MSG_SURFACE_CHANGED, width, height));
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
64 |
bc0a685b
|
Leszek Koltunski
|
|
65 |
f6d884d5
|
Leszek Koltunski
|
void sendDoFrame(long frameTimeNanos)
|
66 |
862fcd79
|
Leszek Koltunski
|
{
|
67 |
|
|
sendMessage(obtainMessage(RenderHandler.MSG_DO_FRAME, (int) (frameTimeNanos >> 32), (int) frameTimeNanos));
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71 |
bc0a685b
|
Leszek Koltunski
|
|
72 |
f6d884d5
|
Leszek Koltunski
|
void sendShutdown()
|
73 |
bc0a685b
|
Leszek Koltunski
|
{
|
74 |
|
|
sendMessage(obtainMessage(RenderHandler.MSG_SHUTDOWN));
|
75 |
|
|
}
|
76 |
862fcd79
|
Leszek Koltunski
|
|
77 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78 |
|
|
@Override // runs on RenderThread
|
79 |
|
|
public void handleMessage(Message msg)
|
80 |
|
|
{
|
81 |
|
|
int what = msg.what;
|
82 |
bc0a685b
|
Leszek Koltunski
|
|
83 |
862fcd79
|
Leszek Koltunski
|
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 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|