您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
9. XEGL的线程安全处理和Close清理函数编写~1
发布时间:2021-06-13 16:41:18编辑:雪饮阅读()
前面實現了SLAudioPlay的线程安全处理和Close清理函数编写,對於XEGL同樣也要實現綫程安全以及close清理函數,都差球不多。首先cpp/XEGL.h:
#define XPLAY_XEGL_H
class XEGL
{
public:
virtual bool Init(void *win) = 0;
virtual void Close() = 0;
virtual void Draw() = 0;
static XEGL *Get();
protected:
XEGL(){}
};
#endif //XPLAY_XEGL_H
總體來説還是沒有什麽問題的
#include <EGL/egl.h>
#include <mutex>
#include "XEGL.h"
#include "XLog.h"
class CXEGL:public XEGL
{
public:
EGLDisplay display = EGL_NO_DISPLAY;
EGLSurface surface = EGL_NO_SURFACE;
EGLContext context = EGL_NO_CONTEXT;
std::mutex mux;
virtual void Draw()
{
mux.lock();
if(display == EGL_NO_DISPLAY || surface == EGL_NO_SURFACE)
{
mux.unlock();
return;
}
eglSwapBuffers(display,surface);
mux.unlock();
}
virtual void Close()
{
mux.lock();
if(display == EGL_NO_DISPLAY)
{
mux.unlock();
return;
}
eglMakeCurrent(display,EGL_NO_SURFACE,EGL_NO_SURFACE,EGL_NO_CONTEXT);
if(surface != EGL_NO_SURFACE)
eglDestroySurface(display,surface);
if(context != EGL_NO_CONTEXT)
eglDestroyContext(display,context);
eglTerminate(display);
display = EGL_NO_DISPLAY;
surface = EGL_NO_SURFACE;
context = EGL_NO_CONTEXT;
mux.unlock();
}
virtual bool Init(void *win)
{
ANativeWindow *nwin = (ANativeWindow *)win;
Close();
//初始化EGL
mux.lock();
//1 获取EGLDisplay对象 显示设备
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if(display == EGL_NO_DISPLAY)
{
mux.unlock();
XLOGE("eglGetDisplay failed!");
return false;
}
XLOGE("eglGetDisplay success!");
//2 初始化Display
if(EGL_TRUE != eglInitialize(display,0,0))
{
mux.unlock();
XLOGE("eglInitialize failed!");
return false;
}
XLOGE("eglInitialize success!");
//3 获取配置并创建surface
EGLint configSpec [] = {
EGL_RED_SIZE,8,
EGL_GREEN_SIZE,8,
EGL_BLUE_SIZE,8,
EGL_SURFACE_TYPE,EGL_WINDOW_BIT,
EGL_NONE
};
EGLConfig config = 0;
EGLint numConfigs = 0;
if(EGL_TRUE != eglChooseConfig(display,configSpec,&config,1,&numConfigs))
{
mux.unlock();
XLOGE("eglChooseConfig failed!");
return false;
}
XLOGE("eglChooseConfig success!");
surface = eglCreateWindowSurface(display,config,nwin,NULL);
//4 创建并打开EGL上下文
const EGLint ctxAttr[] = { EGL_CONTEXT_CLIENT_VERSION ,2, EGL_NONE};
context = eglCreateContext(display,config,EGL_NO_CONTEXT,ctxAttr);
if(context == EGL_NO_CONTEXT)
{
mux.unlock();
XLOGE("eglCreateContext failed!");
return false;
}
XLOGE("eglCreateContext success!");
if(EGL_TRUE != eglMakeCurrent(display,surface,surface,context))
{
mux.unlock();
XLOGE("eglMakeCurrent failed!");
return false;
}
XLOGE("eglMakeCurrent success!");
mux.unlock();
return true;
}
};
XEGL *XEGL::Get()
{
static CXEGL egl;
return &egl;
}
关键字词:XEGL
相关文章
-
无相关信息