目錄
- 一. EGL 前言
- 二. EGL 繪制流程簡介
- 三.eglMakeCurrent 函式簡介
- 1.eglMakeCurrent 簡介
- 2.eglMakeCurrent 實作
- 3.eglMakeCurrent 使用
- 四.關于多個 EGLContext
- 五.共享 EGLContext
- 六.猜你喜歡
零基礎 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 的配置屬性
EGLContext – OpenGL ES 圖形背景關系
二. EGL 繪制流程簡介
- 獲取 EGL Display 物件:eglGetDisplay
- 初始化與 EGLDisplay 之間的連接:eglInitialize
- 獲取 EGLConfig 物件:eglChooseConfig / eglGetConfigs
- 創建 EGLContext 實體:eglCreateContext
- 創建 EGLSurface 實體:eglCreateWindowSurface / eglCreatePbufferSurface
- 連接 EGLContext 和 EGLSurface 背景關系 eglMakeCurrent
- 使用 OpenGL ES API 繪制圖形:gl_*
- 切換 front buffer 和 back buffer 顯示:eglSwapBuffer
- 斷開并釋放與 EGLSurface 關聯的 EGLContext 物件:eglRelease
- 洗掉 EGLSurface 物件
- 洗掉 EGLContext 物件
- 終止與 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);
六.猜你喜歡
- OpenGL ES 簡介
- OpenGL ES 版本介紹
- OpenGL ES 2.0 和 3.0 區別
- OpenGL ES 名詞解釋(一)
- OpenGL ES 名詞解釋(二)
- OpenGL ES GLSL 著色器使用程序
- OpenGL ES EGL 簡介
- OpenGL ES EGL 名詞解釋
- OpenGL ES EGL eglGetDisplay
- OpenGL ES EGL eglInitialize
- OpenGL ES EGL eglGetConfigs
- OpenGL ES EGL eglChooseConfig
- OpenGL ES EGL eglGetError
- OpenGL ES EGL eglCreateContext
- OpenGL ES EGL eglCreateWindowSurface
- OpenGL ES EGL eglCreatePbufferSurface
- OpenGL ES EGL eglMakeCurrent
本文由博客 - 猿說編程 猿說編程 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/528880.html
標籤:C
