0

我在尝试仅在 Android 模拟器上初始化 EGLContext 时遇到问题。

出于一个奇怪的原因,当我尝试获取 EGL3 配置时,它什么也没找到……

这是我正在使用的逻辑:

EGL14.eglInitialize(mEGLDisplay, version, 0, version, 1)
EGLConfig config = getConfig(flags, 3);
if (config != null) {
    int[] attrib3_list = {
            EGL14.EGL_CONTEXT_CLIENT_VERSION, 3,
            EGL14.EGL_NONE
    };
    EGLContext context = EGL14.eglCreateContext(mEGLDisplay, config, sharedContext,
            attrib3_list, 0);


    if (EGL14.eglGetError() == EGL14.EGL_SUCCESS) {
        //Log.d(TAG, "Got GLES 3 config");
        mEGLConfig = config;
        mEGLContext = context;
        mGlVersion = 3;
    }
}

这是getConfig方法

/**
 * Finds a suitable EGLConfig.
 *
 * @param flags Bit flags from constructor.
 * @param version Must be 2 or 3.
 */
private EGLConfig getConfig(int flags, int version) {
    int renderableType = EGL14.EGL_OPENGL_ES2_BIT;
    if (version >= 3) {
        renderableType |= EGLExt.EGL_OPENGL_ES3_BIT_KHR;
    }

    // The actual surface is generally RGBA or RGBX, so situationally omitting alpha
    // doesn't really help.  It can also lead to a huge performance hit on glReadPixels()
    // when reading into a GL_RGBA buffer.
    int[] attribList = {
            EGL14.EGL_RED_SIZE, 8,
            EGL14.EGL_GREEN_SIZE, 8,
            EGL14.EGL_BLUE_SIZE, 8,
            EGL14.EGL_ALPHA_SIZE, 8,
            EGL14.EGL_SAMPLES, 4,
            //EGL14.EGL_DEPTH_SIZE, 16,
            //EGL14.EGL_STENCIL_SIZE, 8,
            EGL14.EGL_RENDERABLE_TYPE, renderableType,
            EGL14.EGL_NONE, 0,      // placeholder for recordable [@-3]
            EGL14.EGL_NONE
    };
    if ((flags & FLAG_RECORDABLE) != 0) {
        attribList[attribList.length - 3] = EGL_RECORDABLE_ANDROID;
        attribList[attribList.length - 2] = 1;
    }
    EGLConfig[] configs = new EGLConfig[1];
    int[] numConfigs = new int[1];
    if (!EGL14.eglChooseConfig(mEGLDisplay, attribList, 0, configs, 0, configs.length,
            numConfigs, 0)) {
        Log.w(TAG, "unable to find RGB8888 / " + version + " EGLConfig");
        return null;
    }
    return configs[0];
}

已经兼容 EGL3 的模拟器;因为我收到以下日志:D/EGL_emulation: eglMakeCurrent: 0xe7205360: ver 3 0 (tinfo 0xe72031f0)

我在这里没有想法......

4

1 回答 1

0

最后!我能够获得有效的配置。所以问题是由以下给出的if

if (!EGL14.eglChooseConfig(mEGLDisplay, attribList, 0, configs, 0, configs.length,
        numConfigs, 0)) {
    Log.w(TAG, "unable to find RGB8888 / " + version + " EGLConfig");
    return null;
}

只有在模拟器上eglChooseConfig才会总是返回 false,即使它找到了一个有效的配置......

所以我最终得到了以下逻辑:

EGL14.eglChooseConfig(mEGLDisplay, attribList, 0, configs, 0, configs.length,
        numConfigs, 0);
if (EGL14.eglGetError() != EGL14.EGL_SUCCESS) {
    Log.w(TAG, "unable to find RGB8888 / " + version + " EGLConfig " + EGL14.eglGetError());
    return null;
}

我知道这有点像黑客,但暂时“它有效”

于 2018-10-11T17:16:24.427 回答