Android音视频 - EGL C++实现

PS
本篇继续上一篇的内容,我们来对Native EGL环境实现的各个步骤进行详细解析

EGL Native层的实现

上一篇文章Android OpenGL ES - EGL源码解析以及C++实现

我们还是先来回顾一下流程图
在这里插入图片描述

以及代码

 //(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)步开始

EGL封装准备

我们在上一篇就知道了EGL的一些基础知识,EGLDiaplay,EGLConfig,EGLSurface,EGLContext,我们需要把这些基础类进行封装,那么如何进行封装呢,我们先看一下对于我们上篇文章中自定义的GLRender类需要什么
gl_render.h

//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

关于ANativeWindow可以查看官方文档ANativeWindow

那么EglSurface呢,

egl_surface.h

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.h

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;
}

有了上面的准备工作后,我们就跟着流程图的步骤来一步步走

(2)初始化EGL

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;
}

(3)创建Window

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)
    }
}

(4)创建EglSurface并绑定到线程

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");
    }
}

(5)渲染

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);
}

后面的停止与销毁就交给读者自行研究了。

代码

EGLDemoActivity.java

EGL Native

To compile Wayland-EGL in Yocto, you can follow these steps: 1. Add the necessary layers to your Yocto build. This may include the meta-openembedded layer, which contains the recipes for Wayland and other related packages. 2. Configure your build to include the Wayland-EGL packages. You can do this by adding the following lines to your local.conf file: ``` IMAGE_INSTALL_append = " wayland-protocols" IMAGE_INSTALL_append = " wayland-egl" ``` This will ensure that the Wayland-Protocols and Wayland-EGL packages are included in your Yocto image. 3. Build your Yocto image. You can do this by running the `bitbake` command with the name of your image, such as: ``` bitbake core-image-minimal ``` This will compile the necessary packages and dependencies, and generate a Yocto image that includes Wayland-EGL. 4. Configure your system to use Wayland-EGL as the default display server. You can do this by modifying the `weston.ini` file in your image's root filesystem. Add the following lines to the file: ``` [core] modules=eglbackend.so,desktop-shell.so [shell] background-image=/usr/share/backgrounds/gnome/Aqua.jpg panel-location=none ``` This will configure the Weston compositor to use EGL as the backend for rendering graphics. 5. Boot your Yocto image and test Wayland-EGL. You can do this by running a Wayland client application, such as `weston-terminal` or `weston-flower`. If everything is configured correctly, you should see the application window rendered using EGL. These are the basic steps for compiling Wayland-EGL in Yocto. However, the exact steps may vary depending on your specific Yocto setup and hardware platform. It's recommended to consult the Yocto documentation and community resources for more detailed instructions and troubleshooting tips.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值