RgaColorTransfer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef RGA_COLOR_TRANSFER_H
  2. #define RGA_COLOR_TRANSFER_H
  3. #include <rga/RgaApi.h>
  4. #include <rga/im2d.hpp>
  5. #include <rga/RgaUtils.h>
  6. #include <rga/RgaMutex.h>
  7. #include <opencv4/opencv2/opencv.hpp>
  8. typedef struct Rga_Buffer
  9. {
  10. unsigned char *pBuffer{nullptr}; // Buffer address
  11. int accFenceFd{0}; // Fence file descriptor for releasing the buffer
  12. int releaseFenceFd{0}; // Fence file descriptor for acquiring the buffer
  13. int nSrcFormt; // Source image format
  14. rga_buffer_handle_t hSrc; // Source image buffer handle
  15. rga_buffer_t SrcImg; // Source image buffer structure
  16. int nWidth; // Image width
  17. int nHeight; // Image height
  18. uint32_t nType; // Image format
  19. bool RegisterResource(unsigned char *pBuffer, int nWidth, int nHeight, uint32_t nType)
  20. {
  21. if (pBuffer == nullptr)
  22. return false;
  23. im_handle_param_t srcParam;
  24. srcParam.width = nWidth;
  25. srcParam.height = nHeight;
  26. srcParam.format = nType;
  27. hSrc = importbuffer_virtualaddr(pBuffer, &srcParam);
  28. SrcImg = wrapbuffer_handle(hSrc, nWidth, nHeight, nType);
  29. this->nWidth = nWidth;
  30. this->nHeight = nHeight;
  31. this->nType = nType;
  32. this->pBuffer = pBuffer;
  33. return true;
  34. }
  35. void ReleaseResource()
  36. {
  37. if (releaseFenceFd != 0)
  38. {
  39. imsync(releaseFenceFd);
  40. releaseFenceFd = 0;
  41. }
  42. int ret = 0;
  43. ret = releasebuffer_handle(hSrc);
  44. if (ret != 0)
  45. {
  46. printf("releasebuffer_handle failed, ret = %d\n", ret);
  47. }
  48. accFenceFd = 0;
  49. }
  50. } RGA_BUFFER, *PRGA_BUFFER;
  51. class RgaColorTransfer
  52. {
  53. public:
  54. RgaColorTransfer();
  55. ~RgaColorTransfer();
  56. static RgaColorTransfer *getInstance()
  57. {
  58. if (m_instance == nullptr)
  59. {
  60. m_instance = new RgaColorTransfer();
  61. }
  62. return m_instance;
  63. }
  64. void setSrc(void *pbuffer, int nWidth, int nHeight, uint32_t nType)
  65. {
  66. if (m_Src != nullptr)
  67. m_Src->ReleaseResource();
  68. m_Src->RegisterResource((unsigned char *)pbuffer, nWidth, nHeight, nType);
  69. }
  70. void setDst(void *pbuffer, int nWidth, int nHeight, uint32_t nType)
  71. {
  72. if (m_Dst != nullptr)
  73. m_Dst->ReleaseResource();
  74. m_Dst->RegisterResource((unsigned char *)pbuffer, nWidth, nHeight, nType);
  75. }
  76. bool copyBuffer(cv::Rect srcRect, cv::Rect dstRect);
  77. bool transfer(int nType);
  78. bool resizeImage(cv::Rect srcRect, cv::Rect dstRect);
  79. void ReleaseResource();
  80. private:
  81. static RgaColorTransfer *m_instance; // 单例模式的实例
  82. PRGA_BUFFER m_Src;
  83. PRGA_BUFFER m_Dst;
  84. int m_releaseFenceFd;
  85. int m_accFenceFd;
  86. };
  87. int resize_image(void *src_buf, int src_width, int src_height, void *dst_buf, int dst_width, int dst_height);
  88. #endif // RGA_COLOR_TRANSFER_H