PS
本篇繼續上一篇的內容,咱們來對Native EGL環境實現的各個步驟進行詳細解析
上一篇文章Android OpenGL ES - EGL源碼解析以及C++實現java
咱們仍是先來回顧一下流程圖
android
以及代碼git
//(1) 將線程附加到虛擬機,並獲取env if (that->m_jvm_for_thread->AttachCurrentThread(&env, NULL) != JNI_OK) { LOGE(that->TAG, "線程初始化異常"); return; } // (2) 初始化 EGL if (!that->InitEGL()) { //解除線程和jvm關聯 that->m_jvm_for_thread->DetachCurrentThread(); return; } //進入循環 while (true) { //根據OpenGL渲染狀態進入不一樣的處理 switch (that->m_state) { //刷新Surface,從外面設置Surface後m_state置爲該狀態,說明已經從外部(java層)得到Surface的對象了 case FRESH_SURFACE: LOGI(that->TAG, "Loop Render FRESH_SURFACE") // (3) 初始化Window that->InitDspWindow(env); // (4) 建立EglSurface that->CreateSurface(); // m_state置爲RENDERING狀態進入渲染 that->m_state = RENDERING; break; case RENDERING: LOGI(that->TAG, "Loop Render RENDERING") // (5) 渲染 that->Render(); break; case STOP: LOGI(that->TAG, "Loop Render STOP") //(6) 解除線程和jvm關聯 that->ReleaseRender(); that->m_jvm_for_thread->DetachCurrentThread(); return; case SURFACE_DESTROY: LOGI(that->TAG, "Loop Render SURFACE_DESTROY") //(7) 釋放資源 that->DestroySurface(); that->m_state = NO_SURFACE; break; case NO_SURFACE: default: break; } usleep(20000); } }
首先第(1)步將線程附加到虛擬機,並獲取env
,這一步簡單明瞭,咱們從第(2)步開始github
咱們在上一篇就知道了EGL的一些基礎知識,EGLDiaplay
,EGLConfig
,EGLSurface
,EGLContext
,咱們須要把這些基礎類進行封裝,那麼如何進行封裝呢,咱們先看一下對於咱們上篇文章中自定義的GLRender類須要什麼
gl_render.hsegmentfault
//Surface引用,必需要使用引用,不然沒法在線程中操做 jobject m_surface_ref = NULL; //本地屏幕 ANativeWindow *m_native_window = NULL; //EGL顯示錶面 注意這裏是咱們自定義的EglSurface包裝類而不是系統提供的EGLSurface哦 EglSurface *m_egl_surface = NULL;
對於gl_render來講輸入的是外部的Surface對象
,咱們這裏的是jobject m_surface_ref
,那麼輸出須要的是ANativeWindow
,EglSurface
app
關於
ANativeWindow
能夠查看官方文檔
ANativeWindow
那麼EglSurface
呢,jvm
egl_surface.hoop
class EglSurface { private: const char *TAG = "EglSurface"; //本地屏幕 ANativeWindow *m_native_window = NULL; //封裝了EGLDisplay EGLConfig EGLContext的自定義類 EglCore *m_core; //EGL API提供的 EGLSurface EGLSurface m_surface; }
能夠看到咱們上面的定義的思想也是V(View)和C(Controller)進行了分離。
egl_core.hthis
class EglCore { private: const char *TAG = "EglCore"; //EGL顯示窗口 EGLDisplay m_egl_dsp = EGL_NO_DISPLAY; //EGL上下文 EGLContext m_egl_context = EGL_NO_CONTEXT; //EGL配置 EGLConfig m_egl_config; }
有了上面的準備工做後,咱們就跟着流程圖的步驟來一步步走spa
gl_render.cpp
bool GLRender::InitEGL() { //建立EglSurface對象 m_egl_surface = new EglSurface(); //調用EglSurface的init方法 return m_egl_surface->Init(); }
egl_surface.cpp
PS
咱們上面也說了EGL的初始化主要是對EGLDisplay EGLConfig EGLContext的操做,因此如今是對EGLCore的操做
EglSurface::EglSurface() { //建立EGLCore m_core = new EglCore(); } bool EglSurface::Init() { //調用EGLCore的init方法 return m_core->Init(NULL); }
egl_core.cpp
EglCore::EglCore() { } bool EglCore::Init(EGLContext share_ctx) { if (m_egl_dsp != EGL_NO_DISPLAY) { LOGE(TAG, "EGL already set up") return true; } if (share_ctx == NULL) { share_ctx = EGL_NO_CONTEXT; } //獲取Dispaly m_egl_dsp = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (m_egl_dsp == EGL_NO_DISPLAY || eglGetError() != EGL_SUCCESS) { LOGE(TAG, "EGL init display fail") return false; } EGLint major_ver, minor_ver; //初始化egl EGLBoolean success = eglInitialize(m_egl_dsp, &major_ver, &minor_ver); if (success != EGL_TRUE || eglGetError() != EGL_SUCCESS) { LOGE(TAG, "EGL init fail") return false; } LOGI(TAG, "EGL version: %d.%d", major_ver, minor_ver) //獲取EGLConfig m_egl_config = GetEGLConfig(); const EGLint attr[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE}; //建立EGLContext m_egl_context = eglCreateContext(m_egl_dsp, m_egl_config, share_ctx, attr); if (m_egl_context == EGL_NO_CONTEXT) { LOGE(TAG, "EGL create fail, error is %x", eglGetError()); return false; } EGLint egl_format; success = eglGetConfigAttrib(m_egl_dsp, m_egl_config, EGL_NATIVE_VISUAL_ID, &egl_format); if (success != EGL_TRUE || eglGetError() != EGL_SUCCESS) { LOGE(TAG, "EGL get config fail, error is %x", eglGetError()) return false; } LOGI(TAG, "EGL init success") return true; } EGLConfig EglCore::GetEGLConfig() { EGLint numConfigs; EGLConfig config; //但願的最小配置, static const EGLint CONFIG_ATTRIBS[] = { EGL_BUFFER_SIZE, EGL_DONT_CARE, EGL_RED_SIZE, 8,//R 位數 EGL_GREEN_SIZE, 8,//G 位數 EGL_BLUE_SIZE, 8,//B 位數 EGL_ALPHA_SIZE, 8,//A 位數 EGL_DEPTH_SIZE, 16,//深度 EGL_STENCIL_SIZE, EGL_DONT_CARE, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE // the end 結束標誌 }; //根據你所設定的最小配置系統會選擇一個知足你最低要求的配置,這個真正的配置每每要比你指望的屬性更多 EGLBoolean success = eglChooseConfig(m_egl_dsp, CONFIG_ATTRIBS, &config, 1, &numConfigs); if (!success || eglGetError() != EGL_SUCCESS) { LOGE(TAG, "EGL config fail") return NULL; } return config; }
gl_render.cpp
void GLRender::InitDspWindow(JNIEnv *env) { //傳進來的Surface對象的引用 if (m_surface_ref != NULL) { // 初始化窗口 m_native_window = ANativeWindow_fromSurface(env, m_surface_ref); // 繪製區域的寬高 m_window_width = ANativeWindow_getWidth(m_native_window); m_window_height = ANativeWindow_getHeight(m_native_window); //設置寬高限制緩衝區中的像素數量 ANativeWindow_setBuffersGeometry(m_native_window, m_window_width, m_window_height, WINDOW_FORMAT_RGBA_8888); LOGD(TAG, "View Port width: %d, height: %d", m_window_width, m_window_height) } }
gl_render.cpp
void GLRender::CreateSurface() { m_egl_surface->CreateEglSurface(m_native_window, m_window_width, m_window_height); glViewport(0, 0, m_window_width, m_window_height); }
egl_surface.cpp
/** * * @param native_window 傳入上一步建立的ANativeWindow * @param width * @param height */ void EglSurface::CreateEglSurface(ANativeWindow *native_window, int width, int height) { if (native_window != NULL) { this->m_native_window = native_window; m_surface = m_core->CreateWindSurface(m_native_window); } else { m_surface = m_core->CreateOffScreenSurface(width, height); } if (m_surface == NULL) { LOGE(TAG, "EGL create window surface fail") Release(); } MakeCurrent(); } void EglSurface::MakeCurrent() { m_core->MakeCurrent(m_surface); }
egl_core.cpp
EGLSurface EglCore::CreateWindSurface(ANativeWindow *window) { //調用EGL Native API建立Window Surface EGLSurface surface = eglCreateWindowSurface(m_egl_dsp, m_egl_config, window, 0); if (eglGetError() != EGL_SUCCESS) { LOGI(TAG, "EGL create window surface fail") return NULL; } return surface; } void EglCore::MakeCurrent(EGLSurface egl_surface) { //調用EGL Native API 綁定渲染環境到當前線程 if (!eglMakeCurrent(m_egl_dsp, egl_surface, egl_surface, m_egl_context)) { LOGE(TAG, "EGL make current fail"); } }
gl_render.cpp
void GLRender::Render() { if (RENDERING == m_state) { pImageRender->DoDraw();//畫畫畫.... m_egl_surface->SwapBuffers(); } }
egl_surface.cpp
void EglSurface::SwapBuffers() { m_core->SwapBuffer(m_surface); }
egl_core.cpp
void EglCore::SwapBuffer(EGLSurface egl_surface) { //調用EGL Native API eglSwapBuffers(m_egl_dsp, egl_surface); }
後面的中止與銷燬就交給讀者自行研究了。