NvCudaProc.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of NVIDIA CORPORATION nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  24. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <stdio.h>
  29. #include <cuda_runtime_api.h>
  30. #include <EGL/egl.h>
  31. #include <EGL/eglext.h>
  32. #include <GLES2/gl2.h>
  33. #include <GLES2/gl2ext.h>
  34. #include <cuda.h>
  35. #include <cuda_runtime.h>
  36. #include "cudaEGL.h"
  37. #include "NvAnalysis.h"
  38. #include "NvCudaProc.h"
  39. static void
  40. Handle_EGLImage(EGLImageKHR image);
  41. void
  42. HandleEGLImage(void *pEGLImage)
  43. {
  44. EGLImageKHR *pImage = (EGLImageKHR *)pEGLImage;
  45. Handle_EGLImage(*pImage);
  46. }
  47. /**
  48. * Performs CUDA Operations on egl image.
  49. *
  50. * @param image : EGL image
  51. */
  52. static void
  53. Handle_EGLImage(EGLImageKHR image)
  54. {
  55. CUresult status;
  56. CUeglFrame eglFrame;
  57. CUgraphicsResource pResource = NULL;
  58. cudaFree(0);
  59. status = cuGraphicsEGLRegisterImage(&pResource, image,
  60. CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
  61. if (status != CUDA_SUCCESS)
  62. {
  63. printf("cuGraphicsEGLRegisterImage failed: %d, cuda process stop\n",
  64. status);
  65. return;
  66. }
  67. status = cuGraphicsResourceGetMappedEglFrame(&eglFrame, pResource, 0, 0);
  68. if (status != CUDA_SUCCESS)
  69. {
  70. printf("cuGraphicsSubResourceGetMappedArray failed\n");
  71. }
  72. status = cuCtxSynchronize();
  73. if (status != CUDA_SUCCESS)
  74. {
  75. printf("cuCtxSynchronize failed\n");
  76. }
  77. if (eglFrame.frameType == CU_EGL_FRAME_TYPE_PITCH)
  78. {
  79. //Rect label in plan Y, you can replace this with any cuda algorithms.
  80. addLabels((CUdeviceptr) eglFrame.frame.pPitch[0], eglFrame.pitch);
  81. }
  82. status = cuCtxSynchronize();
  83. if (status != CUDA_SUCCESS)
  84. {
  85. printf("cuCtxSynchronize failed after memcpy\n");
  86. }
  87. status = cuGraphicsUnregisterResource(pResource);
  88. if (status != CUDA_SUCCESS)
  89. {
  90. printf("cuGraphicsEGLUnRegisterResource failed: %d\n", status);
  91. }
  92. }
  93. /**
  94. * Performs map egl image into cuda memory.
  95. *
  96. * @param pEGLImage: EGL image
  97. * @param width: Image width
  98. * @param height: Image height
  99. * @param color_format: The input color format
  100. * @param cuda_buf: destnation cuda address
  101. */
  102. void mapEGLImage2Float(void* pEGLImage, int width, int height,
  103. COLOR_FORMAT color_format,
  104. void* cuda_buf,
  105. void* offsets,
  106. void* scales)
  107. {
  108. CUresult status;
  109. CUeglFrame eglFrame;
  110. CUgraphicsResource pResource = NULL;
  111. EGLImageKHR *pImage = (EGLImageKHR *)pEGLImage;
  112. cudaFree(0);
  113. status = cuGraphicsEGLRegisterImage(&pResource, *pImage,
  114. CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
  115. if (status != CUDA_SUCCESS)
  116. {
  117. printf("cuGraphicsEGLRegisterImage failed: %d, cuda process stop\n",
  118. status);
  119. return;
  120. }
  121. status = cuGraphicsResourceGetMappedEglFrame(&eglFrame, pResource, 0, 0);
  122. if (status != CUDA_SUCCESS)
  123. {
  124. printf("cuGraphicsSubResourceGetMappedArray failed\n");
  125. }
  126. status = cuCtxSynchronize();
  127. if (status != CUDA_SUCCESS)
  128. {
  129. printf("cuCtxSynchronize failed\n");
  130. }
  131. if (eglFrame.frameType == CU_EGL_FRAME_TYPE_PITCH)
  132. {
  133. // Using GPU to convert int buffer into float buffer.
  134. convertIntToFloat((CUdeviceptr) eglFrame.frame.pPitch[0],
  135. width,
  136. height,
  137. eglFrame.pitch,
  138. color_format,
  139. offsets,
  140. scales,
  141. cuda_buf);
  142. }
  143. status = cuCtxSynchronize();
  144. if (status != CUDA_SUCCESS)
  145. {
  146. printf("cuCtxSynchronize failed after memcpy\n");
  147. }
  148. status = cuGraphicsUnregisterResource(pResource);
  149. if (status != CUDA_SUCCESS)
  150. {
  151. printf("cuGraphicsEGLUnRegisterResource failed: %d\n", status);
  152. }
  153. }
  154. void convertEglFrameIntToFloat(void* pEglFrame, int width, int height,
  155. COLOR_FORMAT color_format,
  156. void* cuda_buf,
  157. void* offsets,
  158. void* scales, void* pstream)
  159. {
  160. CUeglFrame eglFrame = *(CUeglFrame*)pEglFrame;
  161. if (eglFrame.frameType == CU_EGL_FRAME_TYPE_PITCH)
  162. {
  163. // Using GPU to convert int buffer into float buffer.
  164. convertIntToFloat((CUdeviceptr) eglFrame.frame.pPitch[0],
  165. width,
  166. height,
  167. eglFrame.pitch,
  168. color_format,
  169. offsets,
  170. scales,
  171. cuda_buf, pstream);
  172. }
  173. }