HarmonyOS鸿蒙Next中OpenGL离屏渲染子线程无效问题

发布于 1周前 作者 ionicwang 来自 鸿蒙OS

HarmonyOS鸿蒙Next中OpenGL离屏渲染子线程无效问题

OpenGL渲染的相关操作在子线程中无效,DemoNdkXComponent,下面这种写法是可以渲染出来的。

```cpp
void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window) {
    OHLOG("OnSurfaceCreatedCB");

    if ((component == nullptr) || (window == nullptr)) {
        OHLOG("OnSurfaceCreatedCB window or component is null");
        return;
    }

    char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = { '\0' };
    uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
    if (OH_NativeXComponent_GetXComponentId(component, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
        OHLOG("OnSurfaceCreatedCB Unable to get XComponent id");
        return;
    }

    {
        std::stringstream ss;
        ss << "OnSurfaceCreatedCB get XComponent id Success , tid = " << gettid();
        OHLOG(ss.str().c_str());
    }
    std::string id(idStr);
    auto render = PluginRender::GetInstance(id);
    g_render_test = render;
    uint64_t width;
    uint64_t height;
    int32_t xSize = OH_NativeXComponent_GetXComponentSize(component, window, &width, &height);
    if ((xSize == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) && (render != nullptr)) {
        if (render->m_eglCore->EglContextInit(window, width, height)) {
            if (id == OPENGL_XCOMPONENT_ID) {
                render->m_eglCore->Background();
                OHLOG("Render Background");
            }
        }
    }
}

但是,这样写就无法渲染也没有报错。

void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window) {
    OHLOG("OnSurfaceCreatedCB");

    if ((component == nullptr) || (window == nullptr)) {
        OHLOG("OnSurfaceCreatedCB window or component is null");
        return;
    }

    char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = { '\0' };
    uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
    if (OH_NativeXComponent_GetXComponentId(component, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
        OHLOG("OnSurfaceCreatedCB Unable to get XComponent id");
        return;
    }

    {
        std::stringstream ss;
        ss << "OnSurfaceCreatedCB get XComponent id Success , tid = " << gettid();
        OHLOG(ss.str().c_str());
    }

    new std::thread([&](){
        std::string id(idStr);
        auto render = PluginRender::GetInstance(id);
        g_render_test = render;
        uint64_t width;
        uint64_t height;
        int32_t xSize = OH_NativeXComponent_GetXComponentSize(component, window, &width, &height);
        if ((xSize == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) && (render != nullptr)) {
            if (render->m_eglCore->EglContextInit(window, width, height)) {
                if (id == OPENGL_XCOMPONENT_ID) {
                    render->m_eglCore->Background();
                    OHLOG("Render Background");
                }
            }
        }
    });
}

更多关于HarmonyOS鸿蒙Next中OpenGL离屏渲染子线程无效问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

鸿蒙系统支持在子线程中离屏渲染的方式。 请参考如下代码:https://gitee.com/GuYueFei/harmonyOSNativeCamera/tree/master/entry/src/main/cpp/render

demo代码中定义了一个渲染线程:RenderThread,在应用启动时创建,一直在运行,进行实时的渲染。

PluginRender::PluginRender(std::string &id)
: renderThread_(std::make_unique<RenderThread>())
{
  this->id_ = id;
}

更多关于HarmonyOS鸿蒙Next中OpenGL离屏渲染子线程无效问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,OpenGL离屏渲染在子线程中无效的问题,可能是由于鸿蒙系统的图形渲染机制与OpenGL的线程模型不兼容导致的。鸿蒙系统采用了分布式架构和微内核设计,其图形渲染引擎可能与传统的OpenGL实现存在差异。

具体表现为,OpenGL上下文在某些子线程中无法正常创建或绑定,导致离屏渲染操作失败。这可能与鸿蒙系统的线程管理机制、图形渲染管线的调度策略或OpenGL ES的扩展支持有关。

解决此类问题通常需要深入分析鸿蒙系统的图形渲染框架,并调整OpenGL的线程使用方式,确保其与系统的渲染机制兼容。

在HarmonyOS鸿蒙Next中,OpenGL离屏渲染在子线程中无效,可能是由于以下原因:

  1. EGL上下文未正确绑定:确保在子线程中正确创建并绑定EGL上下文。EGL上下文是OpenGL渲染的基础,必须在线程中正确初始化。

  2. 线程资源隔离:鸿蒙系统可能对线程资源有严格的管理,确保子线程具备足够的权限和资源进行OpenGL操作。

  3. 同步问题:如果主线程和子线程之间存在资源共享,确保使用适当的同步机制(如互斥锁)来避免竞态条件。

  4. 系统限制:某些系统版本或设备可能对子线程的OpenGL支持有限,建议查阅官方文档或更新系统版本。

建议检查EGL上下文管理,并确保子线程具备完整的OpenGL操作权限。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!