OpenGLWidgetAPI.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "OpenGLWidgetAPI.h"
  2. #include <QApplication>
  3. #include <QLibrary>
  4. #include "spdlog/spdlog.h"
  5. Image_YUV420P::Image_YUV420P(Image_YUV420P&& other): width(other.width), height(other.height),
  6. yData(std::move(other.yData)), uData(std::move(other.uData)), vData(std::move(other.vData))
  7. {}
  8. Image_YUV420P::Image_YUV420P(const Image_YUV420P& other)
  9. : width(other.width), height(other.height),
  10. yData(other.yData), uData(other.uData), vData(other.vData)
  11. {}
  12. Image_YUV420P& Image_YUV420P::operator=(Image_YUV420P&& other)
  13. {
  14. if (this != &other) {
  15. width = other.width;
  16. height = other.height;
  17. yData = std::move(other.yData);
  18. uData = std::move(other.uData);
  19. vData = std::move(other.vData);
  20. }
  21. return *this;
  22. }
  23. Image_YUV420P& Image_YUV420P::operator=(const Image_YUV420P& other)
  24. {
  25. if (this != &other) {
  26. width = other.width;
  27. height = other.height;
  28. yData = other.yData;
  29. uData = other.uData;
  30. vData = other.vData;
  31. }
  32. return *this;
  33. }
  34. bool Image_YUV420P::isValid() const
  35. {
  36. return width > 0 && height > 0 && !yData.isEmpty() && !uData.isEmpty() && !vData.isEmpty();
  37. }
  38. void Image_YUV420P::clear()
  39. {
  40. width = 0;
  41. height = 0;
  42. yData.clear();
  43. uData.clear();
  44. vData.clear();
  45. }
  46. /* ----------------------------------------------------------------
  47. * OpenGLWidget动态库 API
  48. * ---------------------------------------------------------------- */
  49. using funcCreateOpenGLWidget = QWidget* (*)(QWidget*);
  50. using funcDestroyOpenGLWidget = void (*)(QWidget* widget);
  51. using funcRefreshRGBAImage = void (*)(QWidget* widget, QImage& image);
  52. using funcRefreshYUV420Image = void (*)(QWidget* widget, Image_YUV420P* yuvData);
  53. funcCreateOpenGLWidget pCreateOpenGLWidget = nullptr;
  54. funcDestroyOpenGLWidget pDestroyOpenGLWidget = nullptr;
  55. funcRefreshRGBAImage pRefreshRGBAImage = nullptr;
  56. funcRefreshYUV420Image pRefreshYUV420Image = nullptr;
  57. static std::atomic_bool isLibraryLoaded = false;
  58. /* 加载动态库 */
  59. bool loadOpenGLWidgetLibrary()
  60. {
  61. if(isLibraryLoaded.load())
  62. {
  63. return true;
  64. }
  65. QString libPath = QApplication::applicationDirPath();
  66. #ifdef Q_OS_WIN
  67. libPath += "/libOpenGLWidget.dll";
  68. #elif defined(Q_OS_MACOS)
  69. libPath += "/libOpenGLWidget.dylib";
  70. #elif defined(Q_OS_LINUX)
  71. libPath += "/libOpenGLWidget.so";
  72. #endif
  73. QLibrary lib(libPath);
  74. if (!lib.load())
  75. {
  76. SPDLOG_ERROR("加载OpenGLWidget动态库失败: {}", libPath.toStdString());
  77. SPDLOG_ERROR("错误信息: {}", lib.errorString().toStdString());
  78. return false;
  79. }
  80. pCreateOpenGLWidget = reinterpret_cast<funcCreateOpenGLWidget>(lib.resolve("createOpenGLWidget"));
  81. pDestroyOpenGLWidget = reinterpret_cast<funcDestroyOpenGLWidget>(lib.resolve("destroyOpenGLWidget"));
  82. pRefreshRGBAImage = reinterpret_cast<funcRefreshRGBAImage>(lib.resolve("refreshRGBAImage"));
  83. pRefreshYUV420Image = reinterpret_cast<funcRefreshYUV420Image>(lib.resolve("refreshYUV420Image"));
  84. if(!pCreateOpenGLWidget || !pDestroyOpenGLWidget ||
  85. !pRefreshRGBAImage || !pRefreshYUV420Image)
  86. {
  87. SPDLOG_ERROR("加载OpenGLWidget动态库函数失败");
  88. return false;
  89. }
  90. isLibraryLoaded = true;
  91. return true;
  92. }
  93. /* 创建OpenGL窗口,返回QWidget指针 */
  94. QWidget* createOpenGLWidget(QWidget* parent)
  95. {
  96. if(pCreateOpenGLWidget == nullptr)
  97. {
  98. SPDLOG_ERROR("OpenGLWidget动态库函数未加载");
  99. return nullptr;
  100. }
  101. return pCreateOpenGLWidget(parent);
  102. }
  103. /* 销毁OpenGL窗口 */
  104. void destroyOpenGLWidget(QWidget* widget)
  105. {
  106. if(pDestroyOpenGLWidget == nullptr)
  107. {
  108. SPDLOG_ERROR("OpenGLWidget动态库函数未加载");
  109. return;
  110. }
  111. pDestroyOpenGLWidget(widget);
  112. }
  113. /* 刷新一帧RGBA图片 */
  114. void refreshRGBAImage(QWidget* widget, QImage& image)
  115. {
  116. if(pRefreshRGBAImage == nullptr)
  117. {
  118. SPDLOG_ERROR("OpenGLWidget动态库函数未加载");
  119. return;
  120. }
  121. pRefreshRGBAImage(widget, image);
  122. }
  123. /* 刷新一帧YUV420图片 */
  124. void refreshYUV420Image(QWidget* widget, Image_YUV420P& yuvData)
  125. {
  126. if(pRefreshYUV420Image == nullptr)
  127. {
  128. SPDLOG_ERROR("OpenGLWidget动态库函数未加载");
  129. return;
  130. }
  131. pRefreshYUV420Image(widget, &yuvData);
  132. }