• OpenGL ES EGL eglMakeCurrent


    目录

    零基础 OpenGL ES 学习路线推荐 : OpenGL ES 学习目录 >> OpenGL ES 基础

    零基础 OpenGL ES 学习路线推荐 : OpenGL ES 学习目录 >> OpenGL ES 特效

    零基础 OpenGL ES 学习路线推荐 : OpenGL ES 学习目录 >> OpenGL ES 转场

    零基础 OpenGL ES 学习路线推荐 : OpenGL ES 学习目录 >> OpenGL ES 函数

    零基础 OpenGL ES 学习路线推荐 : OpenGL ES 学习目录 >> OpenGL ES GPUImage 使用

    零基础 OpenGL ES 学习路线推荐 : OpenGL ES 学习目录 >> OpenGL ES GLSL 编程

    一. EGL 前言

    EGLNativeDisplayType – 系统显示类型,标识你所开发设备的物理屏幕,DX/OPenGL ES/Metal/Vulkan….

    EGLNativeWindowType – 系统窗口,渲染显示的窗口句柄

    EGLDisplay – 关联 EGLNativeDisplayType 系统物理屏幕的通用数据类型,是平台上 WGL / GLX / AGL 的等价物

    EGLSurface – 渲染区域,相当于 OpenGL ES 绘图的画布 (一块内存空间),用户想绘制的信息首先都要先绘制到 EGLSurface 上,然后通过 EGLDisplay 显示

    EGLConfig – 对 EGLSurface 的 EGL 配置,可以理解为绘制目标 framebuffer 的配置属性

    EGLContextOpenGL ES 图形上下文

    二. EGL 绘制流程简介

    1. 获取 EGL Display 对象:eglGetDisplay
    2. 初始化与 EGLDisplay 之间的连接:eglInitialize
    3. 获取 EGLConfig 对象:eglChooseConfig / eglGetConfigs
    4. 创建 EGLContext 实例:eglCreateContext
    5. 创建 EGLSurface 实例:eglCreateWindowSurface / eglCreatePbufferSurface
    6. 连接 EGLContext 和 EGLSurface 上下文 eglMakeCurrent
    7. 使用 OpenGL ES API 绘制图形:gl_*
    8. 切换 front buffer 和 back buffer 显示:eglSwapBuffer
    9. 断开并释放与 EGLSurface 关联的 EGLContext 对象:eglRelease
    10. 删除 EGLSurface 对象
    11. 删除 EGLContext 对象
    12. 终止与 EGLDisplay 之间的连接

    三.eglMakeCurrent 函数简介

    EGLContext 上下文包含了操作所需的所有状态信息,OpenGL ES 必须有一个可用的上下文才能进行绘图。

    1.eglMakeCurrent 简介

    创建了 EGLSurface 和 EGLContext 之后,因为可能有多个 EGLSurface 和 EGLContext ,所以需要通过 eglMakeCurrent 绑定 EGLSurface 来指定当前上下文

    /*描述:创建 OpenGL ES EGLSurface
     *参数:
     *    display:指定显示的连接
     *    draw:EGL 绘图表面
     *    read:EGL 绘图表面
     *    context:通过 eglCreateContext 创建的上下文
     *
     *返回值:成功是返回 EGL_TRUE,失败时返回 EGL_FALSE
     */
    
    EGLAPI EGLBoolean EGLAPIENTRY eglMakeCurrent(
            	EGLDisplay display,
                    EGLSurface draw,
                  	EGLSurface read,
                    EGLContext context);
    

    相关错误码:

    EGL_BAD_MATCH :提供了与窗口属性不匹配的 EGLConfig,或该 EGLConfig 不支持渲染EGL_BAD_DISPLAY
    EGL_NOT_INITIALIZED
    EGL_BAD_SURFACE
    EGL_BAD_CONTEXT
    EGL_BAD_MATCH
    EGL_BAD_ACCESS
    EGL_BAD_NATIVE_PIXMAP
    EGL_BAD_NATIVE_WINDOW
    EGL_BAD_CURRENT_SURFACE
    EGL_BAD_ALLOC
    EGL_CONTEXT_LOST
    

    2.eglMakeCurrent 实现

    /******************************************************************************************/
    //@Author:猿说编程
    //@Blog(个人博客地址): www.codersrc.com
    //@File:OpenGL ES EGL eglMakeCurrent
    //@Time:2022/08/04 07:30
    //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
    /******************************************************************************************/
    
    
    EGLBoolean eglMakeCurrent(  EGLDisplay dpy, EGLSurface draw,
    
                                EGLSurface read, EGLContext ctx)
    
    {
    
        if (egl_display_t::is_valid(dpy) == EGL_FALSE)
    
            return setError(EGL_BAD_DISPLAY, EGL_FALSE);
    
        if (draw) {
    
            egl_surface_t* s = (egl_surface_t*)draw;
    
            if (!s->isValid())
    
                return setError(EGL_BAD_SURFACE, EGL_FALSE);
    
            if (s->dpy != dpy)
    
                return setError(EGL_BAD_DISPLAY, EGL_FALSE);
    
            // TODO: check that draw is compatible with the context
    
        }
    
        if (read && read!=draw) {
    
            egl_surface_t* s = (egl_surface_t*)read;
    
            if (!s->isValid())
    
                return setError(EGL_BAD_SURFACE, EGL_FALSE);
    
            if (s->dpy != dpy)
    
                return setError(EGL_BAD_DISPLAY, EGL_FALSE);
    
            // TODO: check that read is compatible with the context
    
        }
    
    
    
        EGLContext current_ctx = EGL_NO_CONTEXT;
    
    
    
        if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
    
            return setError(EGL_BAD_MATCH, EGL_FALSE);
    
    
    
        if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
    
            return setError(EGL_BAD_MATCH, EGL_FALSE);
    
    
    
        if (ctx == EGL_NO_CONTEXT) {
    
            // if we're detaching, we need the current context
    
            current_ctx = (EGLContext)getGlThreadSpecific();
    
        } else {
    
            egl_context_t* c = egl_context_t::context(ctx);
    
            egl_surface_t* d = (egl_surface_t*)draw;
    
            egl_surface_t* r = (egl_surface_t*)read;
    
            if ((d && d->ctx && d->ctx != ctx) ||
    
                (r && r->ctx && r->ctx != ctx)) {
    
                // one of the surface is bound to a context in another thread
    
                return setError(EGL_BAD_ACCESS, EGL_FALSE);
    
            }
    
        }
    
    // 调用makeCurrent,将gl和当前的进程进行绑定。
    
        ogles_context_t* gl = (ogles_context_t*)ctx;
    
        if (makeCurrent(gl) == 0) {
    
            if (ctx) {
    
                egl_context_t* c = egl_context_t::context(ctx);
    
                egl_surface_t* d = (egl_surface_t*)draw;
    
                egl_surface_t* r = (egl_surface_t*)read;
    
    
    
                if (c->draw) {// 断开当前draw surface的绑定
    
                    egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw);
    
                    s->disconnect();
    
                    s->ctx = EGL_NO_CONTEXT;
    
                    if (s->zombie)
    
                        delete s;
    
                }
    
                if (c->read) {
    
                    // FIXME: unlock/disconnect the read surface too
    
                }
    
                // 将draw & read 绑定到当前的上下文。
    
                c->draw = draw;
    
                c->read = read;
    
    
    
                if (c->flags & egl_context_t::NEVER_CURRENT) {
    
                    c->flags &= ~egl_context_t::NEVER_CURRENT;
    
                    GLint w = 0;
    
                    GLint h = 0;
    
                    if (draw) {
    
                        w = d->getWidth();
    
                        h = d->getHeight();
    
                    }
    
                    ogles_surfaceport(gl, 0, 0);
    
                    ogles_viewport(gl, 0, 0, w, h);
    
                    ogles_scissor(gl, 0, 0, w, h);
    
                }
    
                if (d) {
    
                    if (d->connect() == EGL_FALSE) {
    
                        return EGL_FALSE;
    
                    }
    
                    d->ctx = ctx;
    
                    d->bindDrawSurface(gl);
    
                }
    
                if (r) {
    
                    // FIXME: lock/connect the read surface too
    
                    r->ctx = ctx;
    
                    r->bindReadSurface(gl);
    
                }
    
            } else {//取消绑定
    
                // if surfaces were bound to the context bound to this thread
    
                // mark then as unbound.
    
                if (current_ctx) {
    
                    egl_context_t* c = egl_context_t::context(current_ctx);
    
                    egl_surface_t* d = (egl_surface_t*)c->draw;
    
                    egl_surface_t* r = (egl_surface_t*)c->read;
    
                    if (d) {
    
                        c->draw = 0;
    
                        d->disconnect();
    
                        d->ctx = EGL_NO_CONTEXT;
    
                        if (d->zombie)
    
                            delete d;
    
                    }
    
                    if (r) {
    
                        c->read = 0;
    
                        r->ctx = EGL_NO_CONTEXT;
    
                        // FIXME: unlock/disconnect the read surface too
    
                    }
    
                }
    
            }
    
            return EGL_TRUE;
    
        }
    
        return setError(EGL_BAD_ACCESS, EGL_FALSE);
    
    }
    

    3.eglMakeCurrent 使用

    一个应用程序可能创建多个 EGLContext 用于不同的用途,所以我们需要关联特定的 EGLContext 和渲染表面。这一过程被称作“指定当前上下文”,使用如下调用,关联特定的 EGLContext 和某个 EGLSurface:

    /******************************************************************************************/
    //@Author:猿说编程
    //@Blog(个人博客地址): www.codersrc.com
    //@File:OpenGL ES EGL eglMakeCurrent
    //@Time:2022/08/04 07:30
    //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
    /******************************************************************************************/
    
    EGLBoolean initializeWindow(EGLNativeWindow nativeWindow)
    {
        const EGLint configAttribs[] = {EGL_RENDER_TYPE, EGL_WINDOW_BIT,
                                        EGL_RED_SIZE,    8,
                                        EGL_GREEN_SIZE,  8,
                                        EGL_BLUE_SIZE,   8,
                                        EGL_DEPTH_SIZE,  24,
                                        EGL_NONE};
    
        const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE};
    
        EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY)
        if (display == EGL_NO_DISPLAY)
        {
            return EGL_FALSE;
        }
    
        EGLint major, minor;
        if (!eglInitialize(display, &major, &minor))
        {
            return EGL_FALSE;
        }
    
        EGLConfig config;
        EGLint numConfigs;
        if (!eglChooseConfig(display, configAttribs, &config, 1, &numConfigs))
        {
            return EGL_FALSE;
        }
    
        EGLSurface window = eglCreateWindowSurface(display, config, nativeWindow, NULL);
        if (window == EGL_NO_SURFACE)
        {
            return EGL_FALSE;
        }
    
        //创建上下文
        EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
        if (context == EGL_NO_CONTEXT)
        {
            return EGL_FALSE;
        }
    
        //绑定使用当前上下文
        if (!eglMakeCurrent(display, window, window, context))
        {
            return EGL_FALSE;
        }
        return EGL_TRUE;
    }
    

    四.关于多个 EGLContext

    某些情况下,我们想创建、使用多个 EGLContext ,对于这种情况,需要注意以下几个情况:

    • 不能在 2 个线程里绑定同一个 EGLContext
    • 不能在 2 个不同的线程里,绑定相同的 EGLSurface 到 2 个不同的 EGLContext 上。
    • 在 2 个不同的线程里,绑定 2 个不同 EGLSurface 到 2 个 EGLContext 上,取决于使用的 GPU 的具体实现,可能成功,也可能失败。

    五.共享 EGLContext

    共享 EGLContext 这种方式在加载阶段很有用。由于上传数据到 GPU(尤其是纹理数据(textures))这类操作很重,如果想要维持帧率稳定,应该在另一个线程进行上传。

    然而,对于上面多个 EGLContext 的 3 种情况的限制,必须在第一个 EGLContext 之外,创建第二个 EGLContext ,这个 EGLContext 将使用第一个 EGLContext 使用的内部状态信息。这两个 Context 即共享 Context 上下文。

    需要注意的是:这****两个 EGLContext 共享的只是内部状态信息,它们两个并不共享调用缓存(每个 EGLContext 各自拥有一个调用缓存)。

    创建第二个 EGLContext 的方法:

    /*描述:创建 OpenGL ES 上下文 EGLContext
     *参数:
     *    display:指定显示的连接
     *    config:配置 EGLConfig
     *    share_context:允许其它 EGLContext 共享数据,使用 EGL_NO_CONTEXT 表示不共享
     *    attribList:指定操作的属性列表,只能接受一个属性 EGL_CONTEXT_CLIENT_VERSION(设置 OpenGL ES 版本)
     *
     *返回值:成功时返回新创建的 EGLContext,失败时返回 EGL_NO_CONTEXT
     */
    
    EGLContext eglCreateContext(
        EGLDisplay display,
        EGLConfig config,
        EGLContext share_context,
        EGLint const * attrib_list);
    

    注意:第三个参数 share_context 是最重要的,它就是第一个 Context ,表示共享上下文

    在第二个线程,不进行任何的绘制,只进行上传数据到 GPU 的操作。所以,给第二个 Context 的 Surface 应该是一个像素缓冲 (pixel buffer) Surface

    EGLSurface eglCreatePbufferSurface(
        EGLDisplay display,
        EGLConfig config,
        EGLint const * attrib_list);
    

    六.猜你喜欢

    1. OpenGL ES 简介
    2. OpenGL ES 版本介绍
    3. OpenGL ES 2.0 和 3.0 区别
    4. OpenGL ES 名词解释(一)
    5. OpenGL ES 名词解释(二)
    6. OpenGL ES GLSL 着色器使用过程
    7. OpenGL ES EGL 简介
    8. OpenGL ES EGL 名词解释
    9. OpenGL ES EGL eglGetDisplay
    10. OpenGL ES EGL eglInitialize
    11. OpenGL ES EGL eglGetConfigs
    12. OpenGL ES EGL eglChooseConfig
    13. OpenGL ES EGL eglGetError
    14. OpenGL ES EGL eglCreateContext
    15. OpenGL ES EGL eglCreateWindowSurface
    16. OpenGL ES EGL eglCreatePbufferSurface
    17. OpenGL ES EGL eglMakeCurrent

    本文由博客 - 猿说编程 猿说编程 发布!

  • 相关阅读:
    POJ2425 A Chess Game[博弈论 SG函数]
    POJ1740A New Stone Game[组合游戏]
    Vijos P1196吃糖果游戏[组合游戏]
    CF724D. Dense Subsequence[贪心 字典序!]
    CF724B. Batch Sort[枚举]
    CF731C. Socks[DFS 贪心]
    CF733D Kostya the Sculptor[贪心 排序]
    CF733C Epidemic in Monstropolis[模拟 构造 贪心]
    洛谷P1991无线通讯网[kruskal | 二分答案 并查集]
    NOIP2015斗地主[DFS 贪心]
  • 原文地址:https://www.cnblogs.com/shuopython/p/16868455.html
Copyright © 2020-2023  润新知