被称为未实现的OpenGL ES API Android

时间:2013-12-09 15:20:43

标签: java android opengl-es

在我的清单中我有这个:

<uses-feature android:glEsVersion="0x00010001" android:required="true" />
<uses-sdk android:minSdkVersion="7"/>   

<uses-feature
    android:name="android.hardware.vibrate"
    android:required="false" />
<uses-feature
    android:name="android.hardware.wifi"
    android:required="false" />

<application
    android:hardwareAccelerated="true"
    android:debuggable="false"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar" >
    <activity
        android:name=".Activity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar"
        android:windowSoftInputMode="stateHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这是我的GLview活动:

abstract class GLView extends SurfaceView implements SurfaceHolder.Callback {
private boolean hasSurface;
private boolean paused;
private EGL10 egl;
private EGLDisplay display;
private EGLConfig config;    
private EGLSurface surface;
private EGLContext eglContext;
private GL11 gl;
protected int width, height;
private boolean mIsLooping;

abstract void onDrawFrame(GL10 gl);
abstract void onSurfaceCreated(GL10 gl, int width, int height);

public String captureScreenshot() {
    Bitmap bitmap = getRawPixels(gl, width, height);
    Util.bitmapBGRtoRGB(bitmap, width, height);
    return Util.saveBitmap(bitmap, Grapher.SCREENSHOT_DIR, "calculator");
}

private static Bitmap getRawPixels(GL10 gl, int width, int height) {
    int size = width * height;
    ByteBuffer buf = ByteBuffer.allocateDirect(size * 4);
    buf.order(ByteOrder.nativeOrder());
    gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, buf);
    int data[] = new int[size];
    buf.asIntBuffer().get(data);
    buf = null;
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    bitmap.setPixels(data, size-width, -width, 0, 0, width, height);
    return bitmap;
}

private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            glDraw();

        }
    };

public GLView(Context context) {
    super(context);
    init();
}

public GLView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {
    SurfaceHolder holder = getHolder();
    holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
    holder.addCallback(this);
}

public void onResume() {
    Calculator.log("onResume " + this);
    paused = false;
    if (hasSurface) {
        initGL();
    }
}

public void onPause() {
    Calculator.log("onPause " + this);
    deinitGL();
}

private void initGL() {
    egl = (EGL10) EGLContext.getEGL();
    display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);       
    int[] ver = new int[2];
    egl.eglInitialize(display, ver);

    int[] configSpec = {EGL10.EGL_NONE};
    EGLConfig[] configOut = new EGLConfig[1];
    int[] nConfig = new int[1];
    egl.eglChooseConfig(display, configSpec, configOut, 1, nConfig);

    config = configOut[0];

    eglContext = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, null);
    surface = egl.eglCreateWindowSurface(display, config, getHolder(), null);
    egl.eglMakeCurrent(display, surface, surface, eglContext);
    gl = (GL11) eglContext.getGL();
    onSurfaceCreated(gl, width, height);
    requestDraw();
}


private void deinitGL() {
    paused = true;
    if (display != null) {
        egl.eglMakeCurrent(display, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
        egl.eglDestroySurface(display, surface);
        egl.eglDestroyContext(display, eglContext);
        egl.eglTerminate(display);

        egl = null;
        config = null;
        eglContext = null;
        surface = null;
        display = null;
        gl = null;
    }
}

protected void glDraw() {
    if (hasSurface && !paused) {
        onDrawFrame(gl);
        if (!egl.eglSwapBuffers(display, surface)) {
            Calculator.log("swapBuffers error " + egl.eglGetError());
        }
        if (egl.eglGetError() == EGL11.EGL_CONTEXT_LOST) {
            Calculator.log("egl context lost " + this);
            paused = true;
        }
        if (mIsLooping) {
            requestDraw();
        }
    }
}

public void surfaceCreated(SurfaceHolder holder) {
    Calculator.log("surfaceCreated " + this);
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Calculator.log("surfaceChanged " + format + ' ' + this);
    this.width  = width;
    this.height = height;
    boolean doInit = !hasSurface && !paused;
    hasSurface = true;
    if (doInit) {
        initGL();
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    Calculator.log("surfaceDestroyed " + this);
    hasSurface = false;
    deinitGL();
}

public void startLooping() {
    if (!mIsLooping) {
        Calculator.log("start looping");
        mIsLooping = true;
        glDraw();
    }
}

public void stopLooping() {
    if (mIsLooping) {
        Calculator.log("stop looping");
        mIsLooping = false;
    }
}

public boolean isLooping() {
    return mIsLooping;
}

public void requestDraw() {
    handler.sendEmptyMessage(1);
}
}

我遇到所有这些问题,因为我已经包含了这条指令:

Android:hardwareAccelerated =“true”

我不知道我添加的代码是否足以识别问题,因为现在代码给了我这个错误:

称为未实现的OpenGL ES API

我希望在活动调用OpenGL类时禁用android:hardwareAccelerated =“true”

1 个答案:

答案 0 :(得分:0)

您尚未在清单中启用OpenGL ES。你需要添加这个:

<uses-feature android:glEsVersion="0x00010001" android:required="true" />
<uses-sdk android:minSdkVersion="7"/>

请参阅本文中的“OpenGL ES的清单声明”:

http://software.intel.com/en-us/articles/porting-opengl-games-to-android-on-intel-atom-processors-part-2