2011-04-07 44 views
5

我嘗試使用EGL初始化GLES(因爲我想從主線程 中繪製而不是使用渲染器並從onDrawFrame內部繪製)。我得到錯誤:「確保SurfaceView或關聯的SurfaceHolder具有有效的Surface」。很明顯,mEgl.eglCreateWindowSurface失敗,因爲surfaceHolder沒有有效的曲面。我如何獲得具有有效曲面的SurfaceHolder?如何使用有效Surface(EGL.eglCreateWindowSurface所需)獲取SurfaceHolder?

我Activity.onCreate方法是:

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得到的是:

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) 

任何幫助表示讚賞。

回答

2

實施SurfaceHolder.Callback,並等到您被告知已創建曲面。當它被破壞時也要注意。您只應該依賴於匹配create和destroy調用之間的表面。

+0

這是正常的持有人花費大量的時間花費大量的時間來閱讀surface.is Valid()= true在應用程序啓動之後? – Ashwin 2012-10-21 02:59:32

4

支票有效期爲表面應在表面created.So,surface.isValid()應在onSurfaceCreated或onSurfaceChanged被調用,顯然可以做,你應該添加surfaceholder.callback

相關問題