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");
      }
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

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

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");
        }
      }
    }
  });
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
HarmonyOS
2024-12-27 16:02:21
浏览
收藏 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;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
分享
微博
QQ
微信
回复
2024-12-27 18:33:09


相关问题
Web组件是否支持渲染
2320浏览 • 0回复 待解决
HarmonyOS VideoDecoder使用OpenGL渲染
877浏览 • 1回复 待解决
HarmonyOS 关于native线程问题
613浏览 • 1回复 待解决
HarmonyOS TaskPool线程单例问题
1200浏览 • 1回复 待解决
HarmonyOS 创建window后横竖切换问题
1049浏览 • 1回复 待解决
OpenGL无法正常渲染某些分辨率YUV数据
1411浏览 • 0回复 待解决
HarmonyOS 线程内类型判断无效
449浏览 • 1回复 待解决
HarmonyOS 线程获取数据
476浏览 • 1回复 待解决
HarmonyOS 窗口页面返回事件无效
567浏览 • 1回复 待解决
HarmonyOS Token无效问题
591浏览 • 1回复 待解决