fastcv_hal_utils.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. #ifndef OPENCV_FASTCV_HAL_UTILS_HPP_INCLUDED
  6. #define OPENCV_FASTCV_HAL_UTILS_HPP_INCLUDED
  7. #include "fastcv.h"
  8. #include <opencv2/core/utils/logger.hpp>
  9. #define INITIALIZATION_CHECK \
  10. { \
  11. if (!FastCvContext::getContext().isInitialized) \
  12. { \
  13. return CV_HAL_ERROR_UNKNOWN; \
  14. } \
  15. }
  16. #define CV_HAL_RETURN(status, func) \
  17. { \
  18. if( status == FASTCV_SUCCESS ) \
  19. { \
  20. CV_LOG_DEBUG(NULL, "FastCV HAL for "<<#func<<" run successfully!"); \
  21. return CV_HAL_ERROR_OK; \
  22. } \
  23. else if(status == FASTCV_EBADPARAM || status == FASTCV_EUNALIGNPARAM || \
  24. status == FASTCV_EUNSUPPORTED || status == FASTCV_EHWQDSP || \
  25. status == FASTCV_EHWGPU) \
  26. { \
  27. CV_LOG_DEBUG(NULL, "FastCV status:"<<getFastCVErrorString(status) \
  28. <<", Switching to default OpenCV solution!"); \
  29. return CV_HAL_ERROR_NOT_IMPLEMENTED; \
  30. } \
  31. else \
  32. { \
  33. CV_LOG_ERROR(NULL,"FastCV error:"<<getFastCVErrorString(status)); \
  34. return CV_HAL_ERROR_UNKNOWN; \
  35. } \
  36. }
  37. #define CV_HAL_RETURN_NOT_IMPLEMENTED(reason) \
  38. { \
  39. CV_LOG_DEBUG(NULL,"Switching to default OpenCV\nInfo: "<<reason); \
  40. return CV_HAL_ERROR_NOT_IMPLEMENTED; \
  41. }
  42. #define FCV_KernelSize_SHIFT 3
  43. #define FCV_MAKETYPE(ksize,depth) ((ksize<<FCV_KernelSize_SHIFT) + depth)
  44. #define FCV_CMP_EQ(val1,val2) (fabs(val1 - val2) < FLT_EPSILON)
  45. const char* getFastCVErrorString(int status);
  46. const char* borderToString(int border);
  47. const char* interpolationToString(int interpolation);
  48. struct FastCvContext
  49. {
  50. public:
  51. // initialize at first call
  52. // Defines a static local variable context. Variable is created only once.
  53. static FastCvContext& getContext()
  54. {
  55. static FastCvContext context;
  56. return context;
  57. }
  58. FastCvContext()
  59. {
  60. if (fcvSetOperationMode(FASTCV_OP_CPU_PERFORMANCE) != 0)
  61. {
  62. CV_LOG_WARNING(NULL, "Failed to switch FastCV operation mode");
  63. isInitialized = false;
  64. }
  65. else
  66. {
  67. CV_LOG_INFO(NULL, "FastCV Operation Mode Switched");
  68. isInitialized = true;
  69. }
  70. }
  71. bool isInitialized;
  72. };
  73. #endif