OpenGL ES EGL eglCreateWindowSurface

零基礎 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 之間的連接

三.eglCreateWindowSurface 函數簡介

EGLSurface 也稱爲 渲染區域,相當於 OpenGL ES 繪圖的畫布 (一塊內存空間),用戶想繪製的信息首先都要先繪製到 EGLSurface 上,然後通過 EGLDisplay 顯示

1.eglCreateWindowSurface 函數

創建 EGLSurface 需要 EGLConfig,函數聲明如下:

/*描述:創建 OpenGL ES EGLSurface
 *參數:
 *    display:指定顯示的連接
 *    config:配置 EGLConfig
 *    native_window:原生窗口
 *    attribList:指定操作的屬性列表
 *
 *返回值:成功時返回新創建的 EGLSurface,失敗時返回EGL_NO_SURFACE.
 */

EGLSurface eglCreateWindowSurface(
	EGLDisplay display,
 	EGLConfig config,
 	NativeWindowType native_window,
 	EGLint const * attrib_list);

如果創建 EGLSurface 失敗,可以通過 eglGetError 獲取錯誤類型,可能產生錯誤號:

EGL_BAD_DISPLAY: 連接不是一個EGL display連接
EGL_NOT_INITIALIZED: EGL沒有初始化
EGL_BAD_CONFIG: EGL frame buffer配置無效
EGL_BAD_NATIVE_WINDOW: native window不是與display相同平臺的有效Native Window
EGL_BAD_ATTRIBUTE: attrib_list包含無效參數,或者參數未被識別,或者參數越界
EGL_BAD_ALLOC:已經有一個與native window關聯的Surface,或者無法爲新的EGL窗口分配資源,
EGL_BAD_MATCH:本機窗口的像素格式與配置所需的顏色緩衝區的格式、類型和大小不一致

2.EGLSurface 分類

在文章 《OpenGL ES EGL 名詞解釋》有詳細介紹, EGLSurface 一共分爲三類:

1.Surface – 可顯示的 Surface,實際上就是一個 FrameBuffer,用於綁定窗口後預覽顯示,通過 eglCreateWindowSurface 創建;

2.PixmapSurface – 不是可顯示的 Surface,保存在系統內存中的位圖;

3.PBufferSurface – 不是可顯示的 Surface,保存在顯存中的幀,用於離屏渲染,不需要綁定窗口通過 eglCreatePbufferSurface 創建

四.eglCreateWindowSurface 函數使用

/******************************************************************************************/
//@Author:猿說編程
//@Blog(個人博客地址): www.codersrc.com
//@File:OpenGL ES EGL eglCreateWindowSurface
//@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;
}

五.猜你喜歡

  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

本文由博客 - 猿說編程 猿說編程 發佈!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章