HarmonyOS OpenGL离屏渲染子线程无效问题

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

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
21h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

HarmonyOS 系统支持在子线程中离屏渲染的方式。

请参考如下代码:

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;
}
分享
微博
QQ
微信
回复
19h前
相关问题
Web组件是否支持渲染
1844浏览 • 0回复 待解决
HarmonyOS VideoDecoder使用OpenGL渲染
182浏览 • 1回复 待解决
HarmonyOS TaskPool线程单例问题
36浏览 • 1回复 待解决
OpenGL无法正常渲染某些分辨率YUV数据
436浏览 • 0回复 待解决
HarmonyOS 线程内类型判断无效
53浏览 • 1回复 待解决
HarmonyOS 窗口页面返回事件无效
53浏览 • 1回复 待解决
HarmonyOS 线程获取数据
17浏览 • 1回复 待解决
TaskPool线程和主线程如何通信
2352浏览 • 1回复 待解决
HarmonyOS 组件渲染超过了父布局
33浏览 • 1回复 待解决
HarmonyOS huks支持线程调用吗?
5浏览 • 1回复 待解决
HarmonyOS lazyforEach渲染问题
56浏览 • 1回复 待解决