EGL Context 创建

2023-05-16

继续 EGL context 创建的分析。

eglInitialize()

来看 EGL10.eglInitialize() 的实现。com.google.android.gles_jni.EGLImpl 中,这个方法的实现如下:

    public native boolean     eglInitialize(EGLDisplay display, int[] major_minor);

它是一个本地层方法。其实际实现位于 frameworks/base/core/jni/com_google_android_gles_jni_EGLImpl.cpp

static jfieldID gDisplay_EGLDisplayFieldID;
. . . . . .
static void nativeClassInit(JNIEnv *_env, jclass eglImplClass)
{
. . . . . .
    jclass display_class = _env->FindClass("com/google/android/gles_jni/EGLDisplayImpl");
    gDisplay_EGLDisplayFieldID = _env->GetFieldID(display_class, "mEGLDisplay", "J");
. . . . . .
}

. . . . . .
static inline EGLDisplay getDisplay(JNIEnv* env, jobject o) {
    if (!o) return EGL_NO_DISPLAY;
    return (EGLDisplay)env->GetLongField(o, gDisplay_EGLDisplayFieldID);
}
. . . . . .
static jboolean jni_eglInitialize(JNIEnv *_env, jobject _this, jobject display,
        jintArray major_minor) {
    if (display == NULL || (major_minor != NULL &&
            _env->GetArrayLength(major_minor) < 2)) {
        jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
        return JNI_FALSE;
    }

    EGLDisplay dpy = getDisplay(_env, display);
    EGLBoolean success = eglInitialize(dpy, NULL, NULL);
    if (success && major_minor) {
        int len = _env->GetArrayLength(major_minor);
        if (len) {
            // we're exposing only EGL 1.0
            jint* base = (jint *)_env->GetPrimitiveArrayCritical(major_minor, (jboolean *)0);
            if (len >= 1) base[0] = 1;
            if (len >= 2) base[1] = 0;
            _env->ReleasePrimitiveArrayCritical(major_minor, base, 0);
        }
    }
    return EglBoolToJBool(success);
}

EGL10.eglInitialize()EGLDisplay 对象及一个 int 数组为参数,其中 int 数组为出参,用于返回版本号,并通过方法返回值表示初始化是否成功。

jni_eglInitialize() 中,它通过传入的 Java EGLDisplay 对象获得本地层 Display 对象的句柄,执行 EGL 库 (EGL wrapper 库) 的 eglInitialize() 函数完成初始化,并返回版本号,版本号总是 1.0

EGL 库 (EGL wrapper 库) 的 eglInitialize() 的定义如下:

EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
{
    clearError();

    egl_display_ptr dp = get_display(dpy);
    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);

    EGLBoolean res = dp->initialize(major, minor);

    return res;
}

在这个函数中先获得本地层 Display 对象的指针对象 egl_display_ptr,然后执行 egl_display_t::initialize(EGLint *major, EGLint *minor)

egl_display_ptr 定义(位于 frameworks/native/opengl/libs/EGL/egl_display.h)如下:

class egl_display_ptr {
public:
    explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) {
        if (mDpy) {
            if (CC_UNLIKELY(!mDpy->enter())) {
                mDpy = NULL;
            }
        }
    }

    // We only really need a C++11 move constructor, not a copy constructor.
    // A move constructor would save an enter()/leave() pair on every EGL API
    // call. But enabling -std=c++0x causes lots of errors elsewhere, so I
    // can't use a move constructor until those are cleaned up.
    //
    // egl_display_ptr(egl_display_ptr&& other) {
    //     mDpy = other.mDpy;
    //     other.mDpy = NULL;
    // }
    //
    egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) {
        if (mDpy) {
            mDpy->enter();
        }
    }

    ~egl_display_ptr() {
        if (mDpy) {
            mDpy->leave();
        }
    }

    const egl_display_t* operator->() const { return mDpy; }
          egl_display_t* operator->()       { return mDpy; }

    const egl_display_t* get() const { return mDpy; }
          egl_display_t* get()       { return mDpy; }

    operator bool() const { return mDpy != NULL; }

private:
    egl_display_t* mDpy;

    // non-assignable
    egl_display_ptr& operator=(const egl_display_ptr&);
};

这是 egl_display_t 对象的智能指针,该指针对象创建时执行 egl_display_t::enter(),对象销毁时执行 egl_display_t::leave()

get_display()定义(位于 frameworks/native/opengl/libs/EGL/egl_display.h)如下:

inline egl_display_ptr get_display(EGLDisplay dpy) {
    return egl_display_ptr(egl_display_t::get(dpy));
}

egl_display_t::get(dpy) 定义(位于 frameworks/native/opengl/libs/EGL/egl_display.cpp)如下:

egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
. . . . . .
egl_display_t* egl_display_t::get(EGLDisplay dpy) {
    uintptr_t index = uintptr_t(dpy)-1U;
    if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
        return nullptr;
    }
    return &sDisplay[index];
}

本地层 Display 对象句柄为本地层全局静态 egl_display_t 对象数组中的索引值加 1。

egl_display_t::initialize() 定义如下:

EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {

    {
        Mutex::Autolock _rf(refLock);

        refs++;
        if (refs > 1) {
            if (major != NULL)
                *major = VERSION_MAJOR;
            if (minor != NULL)
                *minor = VERSION_MINOR;
            while(!eglIsInitialized) refCond.wait(refLock);
            return EGL_TRUE;
        }

        while(eglIsInitialized) refCond.wait(refLock);
    }

    {
        Mutex::Autolock _l(lock);

        setGLHooksThreadSpecific(&gHooksNoContext);

        // initialize each EGL and
        // build our own extension string first, based on the extension we know
        // and the extension supported by our client implementation

        egl_connection_t* const cnx = &gEGLImpl;
        cnx->major = -1;
        cnx->minor = -1;
        if (cnx->dso) {
            EGLDisplay idpy = disp.dpy;
            if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
                //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
                //        idpy, cnx->major, cnx->minor, cnx);

                // display is now initialized
                disp.state = egl_display_t::INITIALIZED;

                // get the query-strings for this display for each implementation
                disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
                        EGL_VENDOR);
                disp.queryString.version = cnx->egl.eglQueryString(idpy,
                        EGL_VERSION);
                disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
                        EGL_EXTENSIONS);
                disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
                        EGL_CLIENT_APIS);

            } else {
                ALOGW("eglInitialize(%p) failed (%s)", idpy,
                        egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
            }
        }

        // the query strings are per-display
        mVendorString.setTo(sVendorString);
        mVersionString.setTo(sVersionString);
        mClientApiString.setTo(sClientApiString);

        mExtensionString.setTo(gBuiltinExtensionString);
        char const* start = gExtensionString;
        do {
            // length of the extension name
            size_t len = strcspn(start, " ");
            if (len) {
                // NOTE: we could avoid the copy if we had strnstr.
                const String8 ext(start, len);
                if (findExtension(disp.queryString.extensions, ext.string(),
                        len)) {
                    mExtensionString.append(ext + " ");
                }
                // advance to the next extension name, skipping the space.
                start += len;
                start += (*start == ' ') ? 1 : 0;
            }
        } while (*start != '\0');

        egl_cache_t::get()->initialize(this);

        char value[PROPERTY_VALUE_MAX];
        property_get("debug.egl.finish", value, "0");
        if (atoi(value)) {
            finishOnSwap = true;
        }

        property_get("debug.egl.traceGpuCompletion", value, "0");
        if (atoi(value)) {
            traceGpuCompletion = true;
        }

        if (major != NULL)
            *major = VERSION_MAJOR;
        if (minor != NULL)
            *minor = VERSION_MINOR;

        mHibernation.setDisplayValid(true);
    }

    {
        Mutex::Autolock _rf(refLock);
        eglIsInitialized = true;
        refCond.broadcast();
    }

    return EGL_TRUE;
}

每次执行 egl_display_t::initialize() 初始化时,都会递增 refs,每次执行 egl_display_t::terminate() 终止时,则会递减它。在 egl_display_t 对象创建时,refs 被初始化为 0。

egl_display_t::initialize() 中,会首先处理初始化/终止的同步。增加 refs 之后,当它大于 1 时,表明对象已经被初始化了,可以直接返回,但如果 eglIsInitializedfalse 表明,有另一个线程在初始化,但初始化还没有完成,这需要等待初始化的完成。

增加 refs 之后,当它等于 1 时,表明对象还没有初始化过,或者已经被终止,若 eglIsInitializedtrue 则表明终止还没有结束,此时则需要等待终止过程结束。

对于 refs 的同步,反正在这里都会同步地等待,为什么不把 refLock 锁的锁定范围定位整个函数呢?即锁定整个 egl_display_t::initialize() 函数或 egl_display_t::terminate()

处理初始化/终止的同步之后,才开始了正式的初始化。
1. 首先设置线程特有 GL Hooks 为gHooksNoContext
2. 执行设备特有的实际 EGL 库实现的 eglInitialize() 函数来初始化,并从实际 EGL 库实现中查询一些与图形硬件特性有关的字符串,包括图形硬件的生产商,支持的 EGL 版本,支持的扩展和客户端 API。
对于 Google Pixel 设备而言,这些字符串的实际值如下:

queryString.vendor = Qualcomm Inc.
queryString.version = 1.4
queryString.extensions = EGL_QUALCOMM_shared_image EGL_KHR_image EGL_KHR_image_base EGL_QCOM_create_image EGL_QCOM_gpu_perf EGL_KHR_lock_surface EGL_KHR_lock_surface2 EGL_KHR_lock_surface3 EGL_KHR_fence_sync EGL_KHR_wait_sync EGL_KHR_cl_event EGL_KHR_cl_event2 EGL_KHR_reusable_sync EGL_IMG_context_priority EGL_KHR_gl_texture_2D_image EGL_KHR_gl_texture_cubemap_image EGL_KHR_gl_texture_3D_image EGL_KHR_gl_renderbuffer_image EGL_EXT_create_context_robustness EGL_EXT_yuv_surface EGL_ANDROID_blob_cache EGL_KHR_create_context EGL_KHR_gl_colorspace EGL_KHR_surfaceless_context EGL_KHR_create_context_no_error EGL_KHR_get_all_proc_addresses EGL_QCOM_lock_image2 EGL_KHR_partial_update EGL_EXT_protected_content EGL_KHR_mutable_render_buffer EGL_ANDROID_recordable EGL_ANDROID_native_fence_sync EGL_ANDROID_image_native_buffer EGL_ANDROID_framebuffer_target EGL_ANDROID_image_crop EGL_IMG_image_plane_attribs 
queryString.clientApi = OpenGL_ES
  1. 前面查询的字符串为图形硬件的特性。mVendorString 等则为 Android 图形系统的特性信息,设备生产商,EGL 版本,客户端版本和支持的 EGL 扩展等信息。这些信息如下。
static char const * const sVendorString     = "Android";
static char const * const sVersionString    = "1.4 Android META-EGL";
static char const * const sClientApiString  = "OpenGL_ES";

extern char const * const gBuiltinExtensionString;
extern char const * const gExtensionString;

gBuiltinExtensionStringgExtensionString 定义(位于 frameworks/native/opengl/libs/EGL/eglApi.cpp)如下:

extern char const * const gBuiltinExtensionString =
        "EGL_KHR_get_all_proc_addresses "
        "EGL_ANDROID_presentation_time "
        "EGL_KHR_swap_buffers_with_damage "
        "EGL_ANDROID_create_native_client_buffer "
        "EGL_ANDROID_front_buffer_auto_refresh "
#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
        "EGL_ANDROID_get_frame_timestamps "
#endif
        ;
extern char const * const gExtensionString  =
        "EGL_KHR_image "                        // mandatory
        "EGL_KHR_image_base "                   // mandatory
        "EGL_KHR_image_pixmap "
        "EGL_KHR_lock_surface "
#if (ENABLE_EGL_KHR_GL_COLORSPACE != 0)
        "EGL_KHR_gl_colorspace "
#endif
        "EGL_KHR_gl_texture_2D_image "
        "EGL_KHR_gl_texture_3D_image "
        "EGL_KHR_gl_texture_cubemap_image "
        "EGL_KHR_gl_renderbuffer_image "
        "EGL_KHR_reusable_sync "
        "EGL_KHR_fence_sync "
        "EGL_KHR_create_context "
        "EGL_KHR_config_attribs "
        "EGL_KHR_surfaceless_context "
        "EGL_KHR_stream "
        "EGL_KHR_stream_fifo "
        "EGL_KHR_stream_producer_eglsurface "
        "EGL_KHR_stream_consumer_gltexture "
        "EGL_KHR_stream_cross_process_fd "
        "EGL_EXT_create_context_robustness "
        "EGL_NV_system_time "
        "EGL_ANDROID_image_native_buffer "      // mandatory
        "EGL_KHR_wait_sync "                    // strongly recommended
        "EGL_ANDROID_recordable "               // mandatory
        "EGL_KHR_partial_update "               // strongly recommended
        "EGL_EXT_buffer_age "                   // strongly recommended with partial_update
        "EGL_KHR_create_context_no_error "
        "EGL_KHR_mutable_render_buffer "
        "EGL_EXT_yuv_surface "
        "EGL_EXT_protected_content "
        ;

这里初始化获得的 EGL 扩展特性是最终暴露给应用程序的 EGL 扩展特性。(gBuiltinExtensionString + gExtensionString) 为 Android 图形系统可以识别并应用的 EGL 扩展,其中 (gBuiltinExtensionString) 是完全由 Android 的 EGL wrapper 库实现并总是可用的。其余的 (gExtensionString) 则依赖于图形硬件 EGL 驱动中的支持。其中的一些必须得到支持,因为它们由 Android 系统本身使用,它们是上面在注释中标记了 mandatory 的那些,CDD 对它们有做要求。系统 假设 设备总是支持那些强制的 EGL 扩展,如果那些扩展缺失的话,则设备运行可能会出问题。
实际暴露给应用程序的 EGL 扩展特性将是 Android 图形系统可识别的与图形硬件支持的交集,其中 Android 图形系统可识别的包括由 EGL wrapper 实现的与需要图形硬件支持的。
4. 初始化 egl_cache_t
5. 根据调试有关的一些系统属性设置状态。
6. 返回主、次版本号。
7. 将 eglIsInitialized 置为 true 并发送广播出去通知其它线程,初始化结束。

eglChooseConfig()

接下来来看 eglChooseConfig()。在 EGLImpl 中,它同样为本地层方法:

    public native boolean     eglChooseConfig(EGLDisplay display, int[] attrib_list, EGLConfig[] configs, int config_size, int[] num_config);

eglChooseConfig() 方法的本地层实现(位于 frameworks/base/core/jni/com_google_android_gles_jni_EGLImpl.cpp)如下:

static jboolean jni_eglChooseConfig(JNIEnv *_env, jobject _this, jobject display,
        jintArray attrib_list, jobjectArray configs, jint config_size, jintArray num_config) {
    if (display == NULL
        || !validAttribList(_env, attrib_list)
        || (configs != NULL && _env->GetArrayLength(configs) < config_size)
        || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
        jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
        return JNI_FALSE;
    }
    EGLDisplay dpy = getDisplay(_env, display);
    EGLBoolean success = EGL_FALSE;

    if (configs == NULL) {
        config_size = 0;
    }
    EGLConfig nativeConfigs[config_size];

    int num = 0;
    jint* attrib_base = beginNativeAttribList(_env, attrib_list);
    success = eglChooseConfig(dpy, attrib_base, configs ? nativeConfigs : 0, config_size, &num);
    endNativeAttributeList(_env, attrib_list, attrib_base);

    if (num_config != NULL) {
        _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
    }

    if (success && configs!=NULL) {
        for (int i=0 ; i<num ; i++) {
            jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, reinterpret_cast<jlong>(nativeConfigs[i]));
            _env->SetObjectArrayElement(configs, i, obj);
        }
    }
    return EglBoolToJBool(success);
}

eglChooseConfig() 接收 EGLDisplayint[]attrib_list 作为入参,接收 EGLConfig[]configsint[]num_config 接收返回值,而 int 型的 config_size 则用来表示 configs 的长度。

jni_eglChooseConfig() 做的事情如下:
1. 检查参数有效性。参数无效的时候,抛出异常并返回。
2. 参数转换。将 Java 对象转换为本地层所用的结构。
3. 执行 EGL wrapper 库的 eglChooseConfig()
4. 返回值给 Java 层。对于 int[]num_config,通过 JNI 函数更新其内容。对于 EGLConfig[]configs,则会先构造 Java 对象。

static jclass gConfig_class;

static jmethodID gConfig_ctorID;
. . . . . .
static void nativeClassInit(JNIEnv *_env, jclass eglImplClass)
{
    jclass config_class = _env->FindClass("com/google/android/gles_jni/EGLConfigImpl");
    gConfig_class = (jclass) _env->NewGlobalRef(config_class);
    gConfig_ctorID = _env->GetMethodID(gConfig_class,  "<init>", "(J)V");
    gConfig_EGLConfigFieldID = _env->GetFieldID(gConfig_class,  "mEGLConfig",  "J");

可以看一下这里用到的一些函数的定义:

static bool validAttribList(JNIEnv *_env, jintArray attrib_list) {
    if (attrib_list == NULL) {
        return true;
    }
    jsize len = _env->GetArrayLength(attrib_list);
    if (len < 1) {
        return false;
    }
    jint item = 0;
    _env->GetIntArrayRegion(attrib_list, len-1, 1, &item);
    return item == EGL_NONE;
}

static jint* beginNativeAttribList(JNIEnv *_env, jintArray attrib_list) {
    if (attrib_list != NULL) {
        return _env->GetIntArrayElements(attrib_list, (jboolean *)0);
    } else {
        return(jint*) gNull_attrib_base;
    }
}

static void endNativeAttributeList(JNIEnv *_env, jintArray attrib_list, jint* attrib_base) {
    if (attrib_list != NULL) {
        _env->ReleaseIntArrayElements(attrib_list, attrib_base, 0);
    }
}

EGL wrapper 库中 eglChooseConfig() 定义如下:

egl_display_ptr validate_display(EGLDisplay dpy) {
    egl_display_ptr dp = get_display(dpy);
    if (!dp)
        return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
    if (!dp->isReady())
        return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));

    return dp;
}
. . . . . .
EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
                            EGLConfig *configs, EGLint config_size,
                            EGLint *num_config)
{
    clearError();

    const egl_display_ptr dp = validate_display(dpy);
    if (!dp) return EGL_FALSE;

    if (num_config==0) {
        return setError(EGL_BAD_PARAMETER, EGL_FALSE);
    }

    EGLBoolean res = EGL_FALSE;
    *num_config = 0;

    egl_connection_t* const cnx = &gEGLImpl;
    if (cnx->dso) {
        if (attrib_list) {
            char value[PROPERTY_VALUE_MAX];
            property_get("debug.egl.force_msaa", value, "false");

            if (!strcmp(value, "true")) {
                size_t attribCount = 0;
                EGLint attrib = attrib_list[0];

                // Only enable MSAA if the context is OpenGL ES 2.0 and
                // if no caveat is requested
                const EGLint *attribRendererable = NULL;
                const EGLint *attribCaveat = NULL;

                // Count the number of attributes and look for
                // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
                while (attrib != EGL_NONE) {
                    attrib = attrib_list[attribCount];
                    switch (attrib) {
                        case EGL_RENDERABLE_TYPE:
                            attribRendererable = &attrib_list[attribCount];
                            break;
                        case EGL_CONFIG_CAVEAT:
                            attribCaveat = &attrib_list[attribCount];
                            break;
                    }
                    attribCount++;
                }

                if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
                        (!attribCaveat || attribCaveat[1] != EGL_NONE)) {

                    // Insert 2 extra attributes to force-enable MSAA 4x
                    EGLint aaAttribs[attribCount + 4];
                    aaAttribs[0] = EGL_SAMPLE_BUFFERS;
                    aaAttribs[1] = 1;
                    aaAttribs[2] = EGL_SAMPLES;
                    aaAttribs[3] = 4;

                    memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));

                    EGLint numConfigAA;
                    EGLBoolean resAA = cnx->egl.eglChooseConfig(
                            dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);

                    if (resAA == EGL_TRUE && numConfigAA > 0) {
                        ALOGD("Enabling MSAA 4x");
                        *num_config = numConfigAA;
                        return resAA;
                    }
                }
            }
        }

        res = cnx->egl.eglChooseConfig(
                dp->disp.dpy, attrib_list, configs, config_size, num_config);
    }
    return res;
}

在这里,如果为了调试强制使用 MSAA,即多重采样抗锯齿(MultiSampling Anti-Aliasing,简称MSAA),则会插入 2 个额外的属性来强制启用 MSAA 4x,然后调用图形硬件特有的实际 EGL 实现库的 eglChooseConfig() 完成设置。否则直接用设备特有的实际 EGL 实现库的 eglChooseConfig() 完成设置。

我们前面 在 Android 中使用 OpenGL 一文中的示例里,attrib_list 的实际值如下:

        int[] mConfigSpec = { EGL10.EGL_RED_SIZE, 5, 
                EGL10.EGL_GREEN_SIZE, 6, EGL10.EGL_BLUE_SIZE, 5, 
                EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };

这个配置大概用于配置 OpenGL ES 渲染所用的颜色模式,深度大小等。

eglCreateWindowSurface()

然后来看 eglCreateWindowSurface()。在 EGLImpl 中,它有着如下这样的定义:

    public EGLSurface eglCreateWindowSurface(EGLDisplay display, EGLConfig config, Object native_window, int[] attrib_list) {
        Surface sur = null;
        if (native_window instanceof SurfaceView) {
            SurfaceView surfaceView = (SurfaceView)native_window;
            sur = surfaceView.getHolder().getSurface();
        } else if (native_window instanceof SurfaceHolder) {
            SurfaceHolder holder = (SurfaceHolder)native_window;
            sur = holder.getSurface();
        } else if (native_window instanceof Surface) {
            sur = (Surface) native_window;
        }

        long eglSurfaceId;
        if (sur != null) {
            eglSurfaceId = _eglCreateWindowSurface(display, config, sur, attrib_list);
        } else if (native_window instanceof SurfaceTexture) {
            eglSurfaceId = _eglCreateWindowSurfaceTexture(display, config,
                    native_window, attrib_list);
        } else {
            throw new java.lang.UnsupportedOperationException(
                "eglCreateWindowSurface() can only be called with an instance of " +
                "Surface, SurfaceView, SurfaceHolder or SurfaceTexture at the moment.");
        }

        if (eglSurfaceId == 0) {
            return EGL10.EGL_NO_SURFACE;
        }
        return new EGLSurfaceImpl( eglSurfaceId );
    }
. . . . . .
    private native long _eglCreateWindowSurface(EGLDisplay display, EGLConfig config, Object native_window, int[] attrib_list);
    private native long _eglCreateWindowSurfaceTexture(EGLDisplay display, EGLConfig config, Object native_window, int[] attrib_list);

eglCreateWindowSurface() 根据传入的本地窗口创建 EGLSurface

Android 中可以作为本地窗口传入的有 SurfaceView 对象,SurfaceViewSurfaceHolderSurface 对象,或者 TextureViewSurfaceTexture

传入的 native_window 如果是 Surface 对象,则调用本地层方法 _eglCreateWindowSurface() 创建本地层 EGL Surface。如果传入的
native_windowSurfaceViewSurfaceHolder,则会先从中获得 Surface 对象,然后调用本地层方法 _eglCreateWindowSurface() 创建本地层 EGL Surface。

如果传入的 native_windowSurfaceTexture,则会调用本地层方法 _eglCreateWindowSurfaceTexture() 创建本地层 EGL Surface。

有了地层 EGL Surface 之后,则创建对象 EGLSurfaceImpl 封装本地层 EGL Surface 并返回给调用者。

EGLSurfaceImpl 类定义如下:

public class EGLSurfaceImpl extends EGLSurface {
    long mEGLSurface;
    private long mNativePixelRef;
    public EGLSurfaceImpl() {
        mEGLSurface = 0;
        mNativePixelRef = 0;
    }
    public EGLSurfaceImpl(long surface) {
        mEGLSurface = surface;
        mNativePixelRef = 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        EGLSurfaceImpl that = (EGLSurfaceImpl) o;

        return mEGLSurface == that.mEGLSurface;

    }

    @Override
    public int hashCode() {
        /*
         * Based on the algorithm suggested in
         * http://developer.android.com/reference/java/lang/Object.html
         */
        int result = 17;
        result = 31 * result + (int) (mEGLSurface ^ (mEGLSurface >>> 32));
        return result;
    }
}

EGLSurfaceImpl 仅仅是本地层对象句柄的简单封装。

本地层方法 _eglCreateWindowSurface() 的实现如下:

static jfieldID gConfig_EGLConfigFieldID;
. . . . . .
static inline EGLConfig getConfig(JNIEnv* env, jobject o) {
    if (!o) return 0;
    return (EGLConfig)env->GetLongField(o, gConfig_EGLConfigFieldID);
}
. . . . . .
static void nativeClassInit(JNIEnv *_env, jclass eglImplClass)
{
    jclass config_class = _env->FindClass("com/google/android/gles_jni/EGLConfigImpl");
    gConfig_class = (jclass) _env->NewGlobalRef(config_class);
    gConfig_ctorID = _env->GetMethodID(gConfig_class,  "<init>", "(J)V");
    gConfig_EGLConfigFieldID = _env->GetFieldID(gConfig_class,  "mEGLConfig",  "J");
. . . . . .
static jlong jni_eglCreateWindowSurface(JNIEnv *_env, jobject _this, jobject display,
        jobject config, jobject native_window, jintArray attrib_list) {
    if (display == NULL || config == NULL
        || !validAttribList(_env, attrib_list)) {
        jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
        return JNI_FALSE;
    }
    EGLDisplay dpy = getDisplay(_env, display);
    EGLContext cnf = getConfig(_env, config);
    sp<ANativeWindow> window;
    if (native_window == NULL) {
not_valid_surface:
        jniThrowException(_env, "java/lang/IllegalArgumentException",
                "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface");
        return 0;
    }

    window = android_view_Surface_getNativeWindow(_env, native_window);
    if (window == NULL)
        goto not_valid_surface;

    jint* base = beginNativeAttribList(_env, attrib_list);
    EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
    endNativeAttributeList(_env, attrib_list, base);
    return reinterpret_cast<jlong>(sur);
}

在这个函数中,首先从 Display 和 Config 的 Java 对象中获得其相应的本地层对象的句柄。上面的代码中用 EGLContext cnf 来保存 getConfig(_env, config) 的返回值。怀疑这是代码作者的 bug,只是由于 EGLContextEGLConfig 都是 void *typedef,所以才没有出现实际的问题。

然后通过 android_view_Surface_getNativeWindow() (定义位于 frameworks/base/core/jni/android_view_Surface.cpp)从 Java 层的 Surface 对象获得本地层的 ANativeWindow

sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) {
    return android_view_Surface_getSurface(env, surfaceObj);
}

sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) {
    sp<Surface> sur;
    jobject lock = env->GetObjectField(surfaceObj,
            gSurfaceClassInfo.mLock);
    if (env->MonitorEnter(lock) == JNI_OK) {
        sur = reinterpret_cast<Surface *>(
                env->GetLongField(surfaceObj, gSurfaceClassInfo.mNativeObject));
        env->MonitorExit(lock);
    }
    env->DeleteLocalRef(lock);
    return sur;
}

得到的 ANativeWindow 实际为本地层的 Surface 类对象。

最后调用 EGL wrapper 库的 eglCreateWindowSurface() 创建 EGLSurface 并返回给调用者。

EGL wrapper 库的 eglCreateWindowSurface() 定义(位于 frameworks/native/opengl/libs/EGL/eglApi.cpp)如下:

EGLSurface eglCreateWindowSurface(  EGLDisplay dpy, EGLConfig config,
                                    NativeWindowType window,
                                    const EGLint *attrib_list)
{
    clearError();

    egl_connection_t* cnx = NULL;
    egl_display_ptr dp = validate_display_connection(dpy, cnx);
    if (dp) {
        EGLDisplay iDpy = dp->disp.dpy;

        int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
        if (result != OK) {
            ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
                    "failed (%#x) (already connected to another API?)",
                    window, result);
            return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
        }

        // Set the native window's buffers format to match what this config requests.
        // Whether to use sRGB gamma is not part of the EGLconfig, but is part
        // of our native format. So if sRGB gamma is requested, we have to
        // modify the EGLconfig's format before setting the native window's
        // format.

        // by default, just pick RGBA_8888
        EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
        android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;

        EGLint a = 0;
        cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
        if (a > 0) {
            // alpha-channel requested, there's really only one suitable format
            format = HAL_PIXEL_FORMAT_RGBA_8888;
        } else {
            EGLint r, g, b;
            r = g = b = 0;
            cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE,   &r);
            cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
            cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE,  &b);
            EGLint colorDepth = r + g + b;
            if (colorDepth <= 16) {
                format = HAL_PIXEL_FORMAT_RGB_565;
            } else {
                format = HAL_PIXEL_FORMAT_RGBX_8888;
            }
        }

        // now select a corresponding sRGB format if needed
        if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
            for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
                if (*attr == EGL_GL_COLORSPACE_KHR) {
                    if (ENABLE_EGL_KHR_GL_COLORSPACE) {
                        dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
                    } else {
                        // Normally we'd pass through unhandled attributes to
                        // the driver. But in case the driver implements this
                        // extension but we're disabling it, we want to prevent
                        // it getting through -- support will be broken without
                        // our help.
                        ALOGE("sRGB window surfaces not supported");
                        return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
                    }
                }
            }
        }

        if (format != 0) {
            int err = native_window_set_buffers_format(window, format);
            if (err != 0) {
                ALOGE("error setting native window pixel format: %s (%d)",
                        strerror(-err), err);
                native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
                return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
            }
        }

        if (dataSpace != 0) {
            int err = native_window_set_buffers_data_space(window, dataSpace);
            if (err != 0) {
                ALOGE("error setting native window pixel dataSpace: %s (%d)",
                        strerror(-err), err);
                native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
                return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
            }
        }

        // the EGL spec requires that a new EGLSurface default to swap interval
        // 1, so explicitly set that on the window here.
        ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
        anw->setSwapInterval(anw, 1);

        EGLSurface surface = cnx->egl.eglCreateWindowSurface(
                iDpy, config, window, attrib_list);
        if (surface != EGL_NO_SURFACE) {
            egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
                    surface, cnx);
            return s;
        }

        // EGLSurface creation failed
        native_window_set_buffers_format(window, 0);
        native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
    }
    return EGL_NO_SURFACE;
}

eglCreateWindowSurface() 执行步骤如下:
第一步,获得 egl_connection_tegl_display_ptr

egl_display_ptr validate_display(EGLDisplay dpy) {
    egl_display_ptr dp = get_display(dpy);
    if (!dp)
        return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
    if (!dp->isReady())
        return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));

    return dp;
}

egl_display_ptr validate_display_connection(EGLDisplay dpy,
        egl_connection_t*& cnx) {
    cnx = NULL;
    egl_display_ptr dp = validate_display(dpy);
    if (!dp)
        return dp;
    cnx = &gEGLImpl;
    if (cnx->dso == 0) {
        return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL));
    }
    return dp;
}

第二步,通过函数 native_window_api_connect() (定义位于 system/core/include/system/window.h) 为本地窗口连接 EGL API:

/*
 * native_window_api_connect(..., int api)
 * connects an API to this window. only one API can be connected at a time.
 * Returns -EINVAL if for some reason the window cannot be connected, which
 * can happen if it's connected to some other API.
 */
static inline int native_window_api_connect(
        struct ANativeWindow* window, int api)
{
    return window->perform(window, NATIVE_WINDOW_API_CONNECT, api);
}

第三步,根据配置计算颜色模式。

第四步,如果 GPU 图形硬件支持 EGL_KHR_gl_colorspace 扩展,则计算得到色彩空间。

// The EGL_KHR_gl_colorspace spec hasn't been ratified yet, so these haven't
// been added to the Khronos egl.h.
#define EGL_GL_COLORSPACE_KHR           EGL_VG_COLORSPACE
#define EGL_GL_COLORSPACE_SRGB_KHR      EGL_VG_COLORSPACE_sRGB
#define EGL_GL_COLORSPACE_LINEAR_KHR    EGL_VG_COLORSPACE_LINEAR

// Turn linear formats into corresponding sRGB formats when colorspace is
// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
// the modification isn't possible, the original dataSpace is returned.
static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
                                                EGLint colorspace) {
    if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
        return HAL_DATASPACE_SRGB_LINEAR;
    } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
        return HAL_DATASPACE_SRGB;
    }
    return dataSpace;
}
. . . . . .
        // now select a corresponding sRGB format if needed
        if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
            for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
                if (*attr == EGL_GL_COLORSPACE_KHR) {
                    if (ENABLE_EGL_KHR_GL_COLORSPACE) {
                        dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
                    } else {
                        // Normally we'd pass through unhandled attributes to
                        // the driver. But in case the driver implements this
                        // extension but we're disabling it, we want to prevent
                        // it getting through -- support will be broken without
                        // our help.
                        ALOGE("sRGB window surfaces not supported");
                        return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
                    }
                }
            }
        }

第五步,为 window (本地窗口) 设置颜色模式。比较奇怪,获得色彩模式与设置色彩模式之间,为什么要隔一段计算颜色空间的逻辑呢?

/*
 * native_window_set_buffers_format(..., int format)
 * All buffers dequeued after this call will have the format specified.
 *
 * If the specified format is 0, the default buffer format will be used.
 */
static inline int native_window_set_buffers_format(
        struct ANativeWindow* window,
        int format)
{
    return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_FORMAT, format);
}

第六步,为 window 设置色彩空间。

/*
 * native_window_set_buffers_data_space(..., int dataSpace)
 * All buffers queued after this call will be associated with the dataSpace
 * parameter specified.
 *
 * dataSpace specifies additional information about the buffer that's dependent
 * on the buffer format and the endpoints. For example, it can be used to convey
 * the color space of the image data in the buffer, or it can be used to
 * indicate that the buffers contain depth measurement data instead of color
 * images.  The default dataSpace is 0, HAL_DATASPACE_UNKNOWN, unless it has been
 * overridden by the consumer.
 */
static inline int native_window_set_buffers_data_space(
        struct ANativeWindow* window,
        android_dataspace_t dataSpace)
{
    return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_DATASPACE,
            dataSpace);
}

第七步,设置 Swap interval。

        // the EGL spec requires that a new EGLSurface default to swap interval
        // 1, so explicitly set that on the window here.
        ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
        anw->setSwapInterval(anw, 1);

第八步,调用实际的设备 EGL 库接口创建 EGLSurface,并据此创建 egl_surface_t 返回给调用者。

EGLImpl 中的本地层方法 _eglCreateWindowSurfaceTexture() 的实现如下:

static jlong jni_eglCreateWindowSurfaceTexture(JNIEnv *_env, jobject _this, jobject display,
        jobject config, jobject native_window, jintArray attrib_list) {
    if (display == NULL || config == NULL
        || !validAttribList(_env, attrib_list)) {
        jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
        return 0;
    }
    EGLDisplay dpy = getDisplay(_env, display);
    EGLContext cnf = getConfig(_env, config);
    sp<ANativeWindow> window;
    if (native_window == 0) {
not_valid_surface:
        jniThrowException(_env, "java/lang/IllegalArgumentException",
                "Make sure the SurfaceTexture is valid");
        return 0;
    }

    sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(_env, native_window));
    window = new Surface(producer, true);
    if (window == NULL)
        goto not_valid_surface;

    jint* base = beginNativeAttribList(_env, attrib_list);
    EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
    endNativeAttributeList(_env, attrib_list, base);
    return reinterpret_cast<jlong>(sur);
}

这个函数与 jni_eglCreateWindowSurface() 函数类似。只是它会从传入的 Java 对象 SurfaceTexture 获得本地层的 IGraphicBufferProducer 对象引用,然后利用该引用创建本地层 Surface。后面的流程就与 jni_eglCreateWindowSurface() 的流程就完全一样。

由此不难理解本地层的 SurfaceIGraphicBufferProducer 的封装,并提供函数来方便操作 IGraphicBufferProducer

eglCreateContext()

Android 的 OpenGL 应用,通过 EGL.eglCreateContext() 创建EGL context。在 EGLImpl 中该方法定义如下:

    public EGLContext eglCreateContext(EGLDisplay display, EGLConfig config, EGLContext share_context, int[] attrib_list) {
        long eglContextId = _eglCreateContext(display, config, share_context, attrib_list);
        if (eglContextId == 0) {
            return EGL10.EGL_NO_CONTEXT;
        }
        return new EGLContextImpl( eglContextId );
    }
. . . . . .
    private native long _eglCreateContext(EGLDisplay display, EGLConfig config, EGLContext share_context, int[] attrib_list);

这个方方法调用本地层方法 _eglCreateContext() 创建本地层 EGL context 并获得其 ID,然后创建 EGLContextImpl 对象并返回给调用者。

EGLContextImpl 定义如下:

public class EGLContextImpl extends EGLContext {
    private GLImpl mGLContext;
    long mEGLContext;

    public EGLContextImpl(long ctx) {
        mEGLContext = ctx;
        mGLContext = new GLImpl();
    }

    @Override
    public GL getGL() {
        return mGLContext;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        EGLContextImpl that = (EGLContextImpl) o;

        return mEGLContext == that.mEGLContext;
    }

    @Override
    public int hashCode() {
        /*
         * Based on the algorithm suggested in
         * http://developer.android.com/reference/java/lang/Object.html
         */
        int result = 17;
        result = 31 * result + (int) (mEGLContext ^ (mEGLContext >>> 32));
        return result;
    }
}

它是对本地层的 EGL context ID 和 GLImpl 的简单封装。

本地层方法 _eglCreateContext() 实现如下:

static jlong jni_eglCreateContext(JNIEnv *_env, jobject _this, jobject display,
        jobject config, jobject share_context, jintArray attrib_list) {
    if (display == NULL || config == NULL || share_context == NULL
        || !validAttribList(_env, attrib_list)) {
        jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
        return JNI_FALSE;
    }
    EGLDisplay dpy = getDisplay(_env, display);
    EGLConfig  cnf = getConfig(_env, config);
    EGLContext shr = getContext(_env, share_context);
    jint* base = beginNativeAttribList(_env, attrib_list);
    EGLContext ctx = eglCreateContext(dpy, cnf, shr, base);
    endNativeAttributeList(_env, attrib_list, base);
    return reinterpret_cast<jlong>(ctx);
}

这个方法从传入的 Java 对象中获得相应的本地层对象,随后通过 EGL wrapper 库的 eglCreateContext() 创建本地层 EGL context 对象,并将其 ID 返回个调用者。

EGL wrapper 库中 eglCreateContext() 的定义如下:

EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
                            EGLContext share_list, const EGLint *attrib_list)
{
    clearError();

    egl_connection_t* cnx = NULL;
    const egl_display_ptr dp = validate_display_connection(dpy, cnx);
    if (dp) {
        if (share_list != EGL_NO_CONTEXT) {
            if (!ContextRef(dp.get(), share_list).get()) {
                return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
            }
            egl_context_t* const c = get_context(share_list);
            share_list = c->context;
        }
        EGLContext context = cnx->egl.eglCreateContext(
                dp->disp.dpy, config, share_list, attrib_list);
        if (context != EGL_NO_CONTEXT) {
            // figure out if it's a GLESv1 or GLESv2
            int version = 0;
            if (attrib_list) {
                while (*attrib_list != EGL_NONE) {
                    GLint attr = *attrib_list++;
                    GLint value = *attrib_list++;
                    if (attr == EGL_CONTEXT_CLIENT_VERSION) {
                        if (value == 1) {
                            version = egl_connection_t::GLESv1_INDEX;
                        } else if (value == 2 || value == 3) {
                            version = egl_connection_t::GLESv2_INDEX;
                        }
                    }
                };
            }
            egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
                    version);
            return c;
        }
    }
    return EGL_NO_CONTEXT;
}

创建 EGLContext 分为如下几步来完成:
1. 获得共享 Context。(EGL 的共享 context 是个什么概念?
2. 根据共享 context 及传入的 EGLDisplay 等,通过图形硬件特有的实际 EGL 实现库的 eglCreateContext() 创建 EGLContext
3. 获取传入的 attrib_list 中的 OpenGL ES 版本信息。
4. 根据前面创建的 EGLContext 和 OpenGL ES 版本信息创建 egl_context_t

在 EGL Wrapper 库这一级,EGL context 即为 egl_context_t 类对象。该类定义如下:

class egl_context_t: public egl_object_t {
protected:
    ~egl_context_t() {}
public:
    typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;

    egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
            egl_connection_t const* cnx, int version);

    void onLooseCurrent();
    void onMakeCurrent(EGLSurface draw, EGLSurface read);

    EGLDisplay dpy;
    EGLContext context;
    EGLConfig config;
    EGLSurface read;
    EGLSurface draw;
    egl_connection_t const* cnx;
    int version;
    String8 gl_extensions;
    Vector<String8> tokenized_gl_extensions;
};

此时这个 egl_context_t 还无法实际使用,它还没有关联 EGLSurface

eglMakeCurrent()

eglMakeCurrent()EGLContext 关联 EGLSurface,并为当前线程启用该 EGLContextEGLImpleglMakeCurrent() 定义如下:

    public native boolean     eglMakeCurrent(EGLDisplay display, EGLSurface draw, EGLSurface read, EGLContext context);

这是一个本地层方法,其本地层实现如下:

static jboolean jni_eglMakeCurrent(JNIEnv *_env, jobject _this, jobject display, jobject draw, jobject read, jobject context) {
    if (display == NULL || draw == NULL || read == NULL || context == NULL) {
        jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
        return JNI_FALSE;
    }
    EGLDisplay dpy = getDisplay(_env, display);
    EGLSurface sdr = getSurface(_env, draw);
    EGLSurface srd = getSurface(_env, read);
    EGLContext ctx = getContext(_env, context);
    return EglBoolToJBool(eglMakeCurrent(dpy, sdr, srd, ctx));
}

这个实现也很直接:它从传入的 Java 对象参数中获得它们本地层对象,然后调用 EGL wrapper 库的 eglMakeCurrent() 并将结果返回给调用者。

EGL wrapper 库中 eglMakeCurrent() 定义如下:

EGLBoolean eglMakeCurrent(  EGLDisplay dpy, EGLSurface draw,
                            EGLSurface read, EGLContext ctx)
{
    clearError();

    egl_display_ptr dp = validate_display(dpy);
    if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);

    // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
    // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
    // a valid but uninitialized display.
    if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
         (draw != EGL_NO_SURFACE) ) {
        if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
    }

    // get a reference to the object passed in
    ContextRef _c(dp.get(), ctx);
    SurfaceRef _d(dp.get(), draw);
    SurfaceRef _r(dp.get(), read);

    // validate the context (if not EGL_NO_CONTEXT)
    if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
        // EGL_NO_CONTEXT is valid
        return setError(EGL_BAD_CONTEXT, EGL_FALSE);
    }

    // these are the underlying implementation's object
    EGLContext impl_ctx  = EGL_NO_CONTEXT;
    EGLSurface impl_draw = EGL_NO_SURFACE;
    EGLSurface impl_read = EGL_NO_SURFACE;

    // these are our objects structs passed in
    egl_context_t       * c = NULL;
    egl_surface_t const * d = NULL;
    egl_surface_t const * r = NULL;

    // these are the current objects structs
    egl_context_t * cur_c = get_context(getContext());

    if (ctx != EGL_NO_CONTEXT) {
        c = get_context(ctx);
        impl_ctx = c->context;
    } else {
        // no context given, use the implementation of the current context
        if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
            // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
            return setError(EGL_BAD_MATCH, EGL_FALSE);
        }
        if (cur_c == NULL) {
            // no current context
            // not an error, there is just no current context.
            return EGL_TRUE;
        }
    }

    // retrieve the underlying implementation's draw EGLSurface
    if (draw != EGL_NO_SURFACE) {
        if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
        d = get_surface(draw);
        impl_draw = d->surface;
    }

    // retrieve the underlying implementation's read EGLSurface
    if (read != EGL_NO_SURFACE) {
        if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
        r = get_surface(read);
        impl_read = r->surface;
    }


    EGLBoolean result = dp->makeCurrent(c, cur_c,
            draw, read, ctx,
            impl_draw, impl_read, impl_ctx);

    if (result == EGL_TRUE) {
        if (c) {
            setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
            egl_tls_t::setContext(ctx);
            _c.acquire();
            _r.acquire();
            _d.acquire();
        } else {
            setGLHooksThreadSpecific(&gHooksNoContext);
            egl_tls_t::setContext(EGL_NO_CONTEXT);
        }
    } else {
        // this will ALOGE the error
        egl_connection_t* const cnx = &gEGLImpl;
        result = setError(cnx->egl.eglGetError(), EGL_FALSE);
    }
    return result;
}

eglMakeCurrent() 首先获得线程当前关联的 EGL context:

static inline EGLContext getContext() { return egl_tls_t::getContext(); }
. . . . . .
    // these are the current objects structs
    egl_context_t * cur_c = get_context(getContext());

在 EGL wrapper 这一级,通过线程局部存储保存当前线程关联的 egl_context_t

EGLContext egl_tls_t::getContext() {
    if (sKey == TLS_KEY_NOT_INITIALIZED) {
        return EGL_NO_CONTEXT;
    }
    egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey);
    if (!tls) return EGL_NO_CONTEXT;
    return tls->ctx;
}

frameworks/native/opengl/libs/EGL/egl_object.hget_context() 的定义如下:

template<typename NATIVE, typename EGL>
static inline NATIVE* egl_to_native_cast(EGL arg) {
    return reinterpret_cast<NATIVE*>(arg);
}
. . . . . .
static inline
egl_context_t* get_context(EGLContext context) {
    return egl_to_native_cast<egl_context_t>(context);
}

eglMakeCurrent() 接口有两个主要的功能:一是为一个有效的 EGLContext 关联 Surface,并把该 EGLContext 关联到当前线程,此时对 Surface 没有特别要求,这也就意味着 EGLContext 可以在不关联 Surface 被设置为当前 EGLContext;二是当传入的 EGLContext 为空时,则将当前线程关联的 EGLContext 接触关联,且当 EGLContext 为空时,传入的 Surface 必须为 EGL_NO_SURFACE

eglMakeCurrent() 通过 egl_display_t::makeCurrent() 执行底层图形硬件 EGL 库实现级别的 make current:

EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
        EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
        EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
{
    EGLBoolean result;

    // by construction, these are either 0 or valid (possibly terminated)
    // it should be impossible for these to be invalid
    ContextRef _cur_c(cur_c);
    SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
    SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);

    { // scope for the lock
        Mutex::Autolock _l(lock);
        if (c) {
            result = c->cnx->egl.eglMakeCurrent(
                    disp.dpy, impl_draw, impl_read, impl_ctx);
            if (result == EGL_TRUE) {
                c->onMakeCurrent(draw, read);
                if (!cur_c) {
                    mHibernation.incWakeCount(HibernationMachine::STRONG);
                }
            }
        } else {
            result = cur_c->cnx->egl.eglMakeCurrent(
                    disp.dpy, impl_draw, impl_read, impl_ctx);
            if (result == EGL_TRUE) {
                cur_c->onLooseCurrent();
                mHibernation.decWakeCount(HibernationMachine::STRONG);
            }
        }
    }

    if (result == EGL_TRUE) {
        // This cannot be called with the lock held because it might end-up
        // calling back into EGL (in particular when a surface is destroyed
        // it calls ANativeWindow::disconnect
        _cur_c.release();
        _cur_r.release();
        _cur_d.release();
    }

    return result;
}

这里调用底层的图形硬件 EGL 库实现的 eglMakeCurrent() 完成操作,并根据需要回调 egl_context_t 的函数。随后释放当前的 context。

如果传入的 EGLContext 有效,且当前已经关联了一个 EGLContext,则新的替换旧的,但是旧的 egl_context_t 的回调 onLooseCurrent() 没有被调到。

如果是要为当前线程关联 EGLContext 的话,则设置线程局部的 GL Hooks 为 EGLContext 的 OpenGL ES 版本所对应的 Hooks,并在线程局部存储中保存 EGLContext,然后增加 EGLContext 和 Surface 的引用计数:

void setGLHooksThreadSpecific(gl_hooks_t const *value) {
    setGlThreadSpecific(value);
}
. . . . . .
void setGlThreadSpecific(gl_hooks_t const *value) {
    gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
    tls_hooks[TLS_SLOT_OPENGL_API] = value;
}

egl_tls_t::setContext(ctx) 定义如下:

egl_tls_t* egl_tls_t::getTLS() {
    egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
    if (tls == 0) {
        tls = new egl_tls_t;
        pthread_setspecific(sKey, tls);
    }
    return tls;
}
. . . . . .
void egl_tls_t::setContext(EGLContext ctx) {
    validateTLSKey();
    getTLS()->ctx = ctx;
}

如果是要清除当前线程关联的 EGLContext 的话,则置线程局部的 GL Hooks 为 gHooksNoContext,并设置当前线程关联的 EGLContextEGL_NO_CONTEXT

猜测在设备特有的 EGL 库实现一级,无论是软件实现,还是硬件实现,都存在着另外的线程局部存储变量来保存那一级的 EGLContext 数据。

自此之后,就可以使用 OpenGL ES 的接口来渲染图形了。

Done.

Android OpenGL 图形系统分析系列文章

在 Android 中使用 OpenGL
Android 图形驱动初始化
EGL Context 创建

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

EGL Context 创建 的相关文章

  • Spring Application Context 注入问题

    熟悉spring bean生命周期的都知道 xff0c 在其生命周期中有个很重要的接口 xff1a Aware 如果要注入application xff0c 可以用如下方式 64 Component public class SpringC
  • EGL综述

    参考 xff1a https www khronos org registry EGL specs eglspec 1 5 pdf 什么是EGL EGL是支持多平台 多操作系统的 xff0c 比如安卓 Unix Windows等 为了扩展性
  • EGL Context 创建

    继续 EGL context 创建的分析 eglInitialize 来看 EGL10 eglInitialize 的实现 com google android gles jni EGLImpl 中 xff0c 这个方法的实现如下 xff1
  • Spring源码(4)Context篇之AbstractApplicationContext(下)

    上一篇 Spring源码 4 Context篇之AbstractApplicationContext xff08 上 xff09 讲解了Spring的AbstractApplicationContext类refresh 方法 xff0c 前
  • springMVC之配置(context:component-scan) 详解

    lt context component scan base package 61 34 com wjx betalot 34 lt 扫描的基本包路径 gt annotation config 61 34 true 34 lt 是否激活属性
  • android graphic(14)—EGL和OpenGL ES之间的关系

    OpenGL ES EGL 例子 EGL加载OpenGL ES库 涉及的库 库的加载 小结 OpenGL ES 什么是OpenGL xff1f Open Graphics Library OpenGL is a cross language
  • EGL在PC和移动端上的支持共享上下文环境情况

    PC端 xff1a 不支持共享上下文 移动端 xff1a 支持共享上下文
  • Context介绍

    目录 Context设计原理默认上下文取消信号传值方法小结 Context 上下文 context Context Go 语言中用来设置截止日期 同步信号 xff0c 传递请求相关值的结构体 上下文与 Goroutine 有比较密切的关系
  • SVN连接不上,提示:Error running context: The server unexpectedly closed the connection.

    结果 xff0c 询问一起其他伙伴 xff0c 人家都能正常使用 最终找到的问题是 xff1a 把TortoiseSVN gt Settings gt Network gt Enable Proxy Server 这个勾选项取消勾选 就可以
  • activity的startActivity和context的startActivity区别

    我们以 startActivity Intent 这个最常用的 api 来讲 1 首先 xff0c Context 类有一个 abstract 方法 Same as 64 link startActivity Intent Bundle w
  • golang中的context

    一 Context含义 1 context定义 context是golang中的上下文 goroutine的相关环境快照 xff0c 其中包含函数调用以及涉及的相关的变量值 golang通过Context机制解决一个request中多个go
  • 深入理解Golang中的Context包

    context Context是Go语言中独特的设计 xff0c 在其他编程语言中我们很少见到类似的概念 context Context深度支持Golang的高并发 1 Goroutine和Channel 在理解context包之前 xff
  • Exception encountered during context initialization - cancelling refresh attempt:

    以个人经验 xff0c 报这个错一般有三个原因 xff1a 原因一 xff1a 配置文件写错了 检查最新写过的xml文件 xff0c 比如mapper xml里面格式 id等是否写对了 原因二 xff1a 依赖冲突 检查最新添加过的依赖包
  • Go的 context 包的使用

    文章目录 背景 简介 主要方法 获得顶级上下文 当前协程上下文的操作 创建下级协程的Context 场景示例 背景 在父子协程协作过程中 父协程需要给子协程传递信息 子协程依据父协程传递的信息来决定自己的操作 这种需求下可以使用 conte
  • Android中opengles,egl库的结构

    目录 egl opengl es的软硬件实现 需要的库 库的分工 加载模块 软件实现模块 硬件实现模块 egl opengl es的软硬件实现 需要的库 算是android中的egl库 用来加载具体的实现 软件实现或者硬件实现 system
  • gin 框架中的 gin.Context

    前言 Context 是 gin 中最重要的部分 例如 它允许我们在中间件之间传递变量 管理流程 验证请求的 JSON 并呈现 JSON 响应 Context 中封装了原生的 Go HTTP 请求和响应对象 同时还提供了一些方法 用于获取请
  • android native 使用opengl es画点线面图形(纯c++)

    一 首先需要对EGL进行初始化 void Renderer initEGL const EGLint attribs EGL SURFACE TYPE EGL WINDOW BIT EGL BLUE SIZE 8 EGL GREEN SIZ
  • Fedora 19 使用 rpmfussion 的 NVIDIA 驱动程序:libGL 错误:无法加载驱动程序:swrast

    在我的 Fedora 19 机器上运行使用 Qt 4 7 的应用程序时 我从应用程序中收到以下错误 libGL screen 0 does not appear to be DRI2 capable libGL OpenDriver try
  • 使用 GraphicBuffers 快速读取纹理

    我正在编写一些使用着色器在 Android 上运行一些 GPGPU 代码的代码 通常是一个Framebuffer使用因此计算结果存储在纹理中 输入数据通常也是纹理 为了提高性能 最好摆脱glTexImage2D and glReadPixe
  • EGL 链接器错误

    我正在尝试在 Ubuntu Trusty 系统上使用 g 4 9 1 链接一个非常简单的 GLES2 和 EGL 程序 我正在使用台面库 我收到 EGL 函数的链接器错误 test cpp text 0x342 undefined refe

随机推荐

  • android wakelock

    1 android休眠控制锁 powermanager acquir 获取控制锁 powermanager release 释放控制锁 2 在powerManagerService中 xff0c 有检测当前framework层中的wakel
  • 产品经理常用术语

    产品经理常用术语 长尾理论 网络时代兴起的一种新理论 xff0c 由于成本和效率的因素 xff0c 当商品储存流通展示的场地和渠道足够宽广 xff0c 商品生产成本急剧下降以至于个人都可以进行生产 xff0c 并且商品的销售成本急剧降低时
  • 传统CD车机面板操作说明

    转自http bbs hifidiy net thread 675597 1 1 html 一般CD车机带USB SD卡的面板操作说明 1 1 控制面板外观图 2 0 基本 共同 功能操作说明 2 1 开启和关闭电源 静音开关 按 钮为开启
  • 汽车总线系统通信协议

    天合汽车零部件 xff08 上海 xff09 有限公司 上海交通大学区域光纤通信网与新型光通信系统国家重点实验室 xff08 上网时间 xff1a 2006 11 xff09 摘要 xff1a 本文主要针对汽车电子控制系统和车载多媒体系统
  • 算法——连续性后处理(把26邻域连续的变成6邻域连续的)

    文章目录 1 概念 1 1 6邻域连续 1 2 18邻域连续 1 3 26邻域连续 1 4 总结 2 目标 3 严格一点的 3 1 原理描述 3 1 1 18邻域连续补充 3 1 2 26邻域连续补充 3 2 代码实现 C 4 宽松一点的
  • RedHat Linux下安装JDK1.7报错Permission denied

    在RedHat Linux5 中安装JDK1 7时 xff0c 当我解压jdk xff0c 并且配置好了环境变量 xff0c 测试的时候 xff0c 报以下错误 root 64 jingfeng01 java version Error d
  • 几种压缩算法的压缩和速度比较

    Quick Benchmark Gzip vs Bzip2 vs LZMA vs XZ vs LZ4 vs LZO EDIT Add zstd Contents hide 1 Selected archives2 Test conditio
  • DDR低功耗模式

    DDR规格 xff1a DDR工作状态图 xff1a DDR 刷新描述 xff1a 电特性 xff1a 工作模式简介 xff1a 1 1 自刷新模式 xff08 Self Refresh Mode xff09 DDR4 SDRAM中自刷新超
  • 嵌入式Linux的低功耗策略

    引 言 由于Linux系统具备嵌入式操作系统需要的很多特色 xff0c 如适应于多种CPU和多种硬件平台 性能稳定 可裁剪性很好 源码开放 研发和使用简单等 现在 xff0c 基于Linux应用的嵌入式设备日益增多 xff0c Linux正
  • libevent实现的HTTP Server

    在使用C语言编码时 有时候需要实现一个HTTP接口 我们可以选择使用libevent库来实现 以下代码演示了使用libevent 并同时支持多线程处理HTTP的请求 头文件 引入的头文件 span class token macro pro
  • Python爬虫完整案例 - 爬取百度百科词条信息

    概述 一个完整的爬虫 xff0c 一般由以下5个组件构成 1 URL管理器 负责维护待爬取URL队列 和已爬取URL队列 xff0c 必须拥有去重功能 2 HTML下载器 负责根据调度器从URL管理器中取出的url xff0c 下载html
  • android apk 签名(平台和普通签名)

    因为做了太多的终端项目 xff0c 客户总会有自己的apk提供 xff0c 这时候各种签名问题就来了 xff0c 最近整理了一下相关知识 xff0c 分享给大家 签名的用处 xff1a 1 应用程序升级 xff1a 如果你希望用户无缝升级到
  • scikit-learn介绍

    在机器学习和数据挖掘的应用中 xff0c scikit learn是一个功能强大的python包 在数据量不是过大的情况下 xff0c 可以解决大部分问题 学习使用scikit learn的过程中 xff0c 我自己也在补充着机器学习和数据
  • 【JUC】CompletableFuture超时处理 配置线程池

    CompletableFuture 简介使用方法代码 简介 xff1a 项目中一个统计的业务场景 xff0c 使用原生的CompletableFuture异步多个任务查询mysql数据 xff0c 少量请求无问题 xff0c 但是测试过程中
  • 使用mysql命令行导出导入MariaDB库数据中文乱码问题解决

    问题 xff1a 到MariaDB bin目录下 xff0c 使用shit 43 右键打开powershell xff0c 执行mysqldump命令导出ems2库数据为sql文件 xff0c 命令中设置字符集为utf8 xff0c 则生成
  • :app:checkDebugDuplicateClasses Execute taskAction

    今天把AS从2 3 3升为3 4 2后 xff0c 导入项后发现报这个 app checkDebugDuplicateClasses的错 xff1a 然后查看了日志 xff0c 真是长篇大论 xff0c 一头雾水 org gradle ap
  • 3D Slicer/ITK-SNAP常见使用

    不是医生 是程序员 使用侧重点不一定相同 文章目录 1 3D Slicer使用 1 0 常见快捷键 1 1 三视图联动 1 2 在model视图查看三视图 1 3 最大化某个视图 1 4 改segment的标号 label 1 5 常见插件
  • 某些.csh .sh脚本无法在shell下执行的问题解决

    背景 最近换了一个环境 xff0c 登录后执行一个环境相关配置的 csh脚本时发现某些执行字段无法识别 xff0c 这种一般就是不同bash支持的脚本语言的差异问题 解决 查询当前shell的bash xff1a echo SHELL 当前
  • Linux 权能综述

    为了执行权限检查 xff0c 传统的 UNIX 实现区分两种类型的进程 xff1a 特权进程 xff08 其有效用户 ID 为0 xff0c 称为超级用户或 root xff09 xff0c 和非特权用户 xff08 其有效 UID 非0
  • EGL Context 创建

    继续 EGL context 创建的分析 eglInitialize 来看 EGL10 eglInitialize 的实现 com google android gles jni EGLImpl 中 xff0c 这个方法的实现如下 xff1