ARView.java
7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package cn.syar.engine;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.VideoView;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;
public class ARView extends GLSurfaceView {
private VideoRenderer videoRenderer;
public ARView(Context context){
super(context);
init(context);
ARView.context = context;
}
private void init(Context context) {
setEGLContextFactory(new EGLContextFactory(){
private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
@Override
public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig){
// Trying to create an OpenGL ES 3.0 context
int[] attribList = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL10.EGL_NONE};
EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attribList);
if ((null == context) || context.equals(EGL10.EGL_NO_CONTEXT)) {
attribList[1] = 2;
context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attribList);
}
return context;
}
/**
* It destroys the OpenGL Context.
*/
@Override
public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context){
egl.eglDestroyContext(display, context);
}
});
///
setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser(){
@Override
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display){
final int EGL_OPENGL_ES2_BIT = 0x0004;
final int[] attrib = { EGL10.EGL_RED_SIZE, 4, EGL10.EGL_GREEN_SIZE, 4, EGL10.EGL_BLUE_SIZE, 4,
EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL10.EGL_NONE };
int[] num_config = new int[1];
egl.eglChooseConfig(display, attrib, null, 0, num_config);
int numConfigs = num_config[0];
if (numConfigs <= 0)
throw new IllegalArgumentException("fail to choose EGL configs");
EGLConfig[] configs = new EGLConfig[numConfigs];
egl.eglChooseConfig(display, attrib, configs, numConfigs,
num_config);
for (EGLConfig config : configs)
{
int[] val = new int[1];
int r = 0, g = 0, b = 0, a = 0, d = 0;
if (egl.eglGetConfigAttrib(display, config, EGL10.EGL_DEPTH_SIZE, val))
d = val[0];
if (d < 16)
continue;
if (egl.eglGetConfigAttrib(display, config, EGL10.EGL_RED_SIZE, val))
r = val[0];
if (egl.eglGetConfigAttrib(display, config, EGL10.EGL_GREEN_SIZE, val))
g = val[0];
if (egl.eglGetConfigAttrib(display, config, EGL10.EGL_BLUE_SIZE, val))
b = val[0];
if (egl.eglGetConfigAttrib(display, config, EGL10.EGL_ALPHA_SIZE, val))
a = val[0];
if (r == 8 && g == 8 && b == 8 && a == 0)
return config;
}
return configs[0];
}
});
////设置渲染器
setRenderer(new GLSurfaceView.Renderer() {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
SyARLogger.getInstance().debug("ARView onSurfaceCreated");
videoRenderer = new VideoRenderer();
}
@Override
public void onSurfaceChanged(GL10 gl, int w, int h) {
SyARLogger.getInstance().debug("ARView onSurfaceChanged w="+w +" h="+h);
try {
synchronized (this) {
videoRenderer.init(w,h);
}
} catch (Throwable ex) {
}
}
@Override
public void onDrawFrame(GL10 gl) {
try {
synchronized (this) {
videoRenderer.draw(null,0,0,0);
}
} catch (Throwable ex) {
}
}
});
}
@Override
protected void onDetachedFromWindow()
{
SyARLogger.getInstance().debug("### ARView dispose");
try {
synchronized (this) {
videoRenderer.destroy();
if(videoView != null){
exitFullScreenCallback();
videoView = null;
}
}
} catch (Throwable ex) {
}
super.onDetachedFromWindow();
}
boolean firstTouch = false;
long startTime = 0;
@Override
public boolean onTouchEvent(final MotionEvent event) {
if(event.getAction() == event.ACTION_DOWN){
if(firstTouch && (System.currentTimeMillis() - startTime) <= 300) {
//do stuff here for double tap
SyARLogger.getInstance().debug(" ###doubleTap### ");
doubleTap();
firstTouch = false;
} else {
firstTouch = true;
startTime = System.currentTimeMillis();
return false;
}
}
return true;
}
private static VideoView videoView;
public static ViewGroup preview;
public static Context context;
private static boolean isVideoFullScreen = false;
public static void fullScreenVideoPlay(final String videoURL){
SyARLogger.getInstance().debug("###fullScreenVideoPlay "+videoURL);
UnityActivityHelper.currActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
if(videoView == null){
videoView = new VideoView(context);
FrameLayout.LayoutParams layoutParams=new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER;
preview.addView(videoView,layoutParams);
MediaControllerEx mediaController = new MediaControllerEx(UnityActivityHelper.currActivity,videoView);
}
videoView.setVideoPath(videoURL);
videoView.requestFocus();
videoView.start();
videoView.setZOrderOnTop( true );
isVideoFullScreen = true;
}
});
}
public static void exitVideoFullScreen(){
if(videoView != null) {
videoView.stopPlayback();
videoView.clearFocus();
preview.removeView(videoView);
videoView = null;
exitFullScreenCallback();
}
}
private native void doubleTap();
private static native void exitFullScreenCallback();
}