问题描述
我尝试初始化GLES与EGL(因为我想从主线程画而是采用了渲染器和从内侧onDrawFrame图)。我得到的错误:确保SurfaceView或相关SurfaceHolder具有有效的面。显然mEgl.eglCreateWindowSurface失败,因为surfaceHolder还没有一个有效的表面。我如何获得一个SurfaceHolder一个有效的表面进行呢?
I try to initialize GLES with EGL (because I want to draw from the main threadinstead of using a Renderer and drawing from inside onDrawFrame). I get the error: "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface". Obviously mEgl.eglCreateWindowSurface fails because surfaceHolder has not a valid surface. How do I get a SurfaceHolder with a valid Surface to proceed?
我的Activity.onCreate的方法是:
My Activity.onCreate method is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GLSurfaceView surfaceView = new GLSurfaceView(this);
setContentView(surfaceView);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
Surface surface = surfaceHolder.getSurface();
Log.v("HelloAndroid", "surface.isValid()= " + Boolean.toString(surface.isValid()));
EGL10 mEgl = (EGL10) EGLContext.getEGL();
EGLDisplay mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
int[] version = new int[2];
mEgl.eglInitialize(mEglDisplay, version);
EGLConfig[] configs = new EGLConfig[1];
int[] num_config = new int[1];
int[] configSpec = {
EGL10.EGL_NONE
};
mEgl.eglChooseConfig(mEglDisplay, configSpec, configs, 1, num_config);
EGLConfig mEglConfig = configs[0];
EGLContext mEglContext = mEgl.eglCreateContext(mEglDisplay, mEglConfig,
EGL10.EGL_NO_CONTEXT, null);
EGLSurface mEglSurface = null;
Log.v("HelloAndroid", "M");
mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay,
mEglConfig, surfaceHolder, null);
Log.v("HelloAndroid", "N");
mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
mEglContext);
Log.v("HelloAndroid", "O");
}
日志输出我与平台工具/亚行logcat是:
The log output I get with platform-tools/adb logcat is:
V/HelloAndroid( 1861): surface.isValid()= false
D/libEGL ( 1861): egl.cfg not found, using default config
D/libEGL ( 1861): loaded /system/lib/egl/libGLES_android.so
V/HelloAndroid( 1861): M
D/AndroidRuntime( 1861): Shutting down VM
W/dalvikvm( 1861): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
E/AndroidRuntime( 1861): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 1861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.HelloAndroid}: java.lang.IllegalArgumentException: Make sure the SurfaceView or associated SurfaceHolder has a valid Surface
E/AndroidRuntime( 1861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
任何帮助是AP preciated。
Any help is appreciated.
推荐答案
支票有效期为表面应做时,表面created.So,surface.isValid()应该被称为onSurfaceCreated或onSurfaceChanged,很明显,你应该添加surfaceholder.callback
The check valid for surface should be did when surface is created.So,surface.isValid() should be called in onSurfaceCreated or onSurfaceChanged,obviously,you should add surfaceholder.callback
这篇关于如何得到一个有效的表面(需要EGL.eglCreateWindowSurface)一SurfaceHolder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!