d3d9ex_interop.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. // A sample program demonstrating interoperability of OpenCV cv::UMat with Direct X surface
  3. // At first, the data obtained from video file or camera and placed onto Direct X surface,
  4. // following mapping of this Direct X surface to OpenCV cv::UMat and call cv::Blur function.
  5. // The result is mapped back to Direct X surface and rendered through Direct X API.
  6. */
  7. #define WIN32_LEAN_AND_MEAN
  8. #include <windows.h>
  9. #include <d3d9.h>
  10. #include "opencv2/core.hpp"
  11. #include "opencv2/core/directx.hpp"
  12. #include "opencv2/core/ocl.hpp"
  13. #include "opencv2/imgproc.hpp"
  14. #include "opencv2/videoio.hpp"
  15. #include "d3dsample.hpp"
  16. #pragma comment (lib, "d3d9.lib")
  17. class D3D9ExWinApp : public D3DSample
  18. {
  19. public:
  20. D3D9ExWinApp(int width, int height, std::string& window_name, cv::VideoCapture& cap) :
  21. D3DSample(width, height, window_name, cap) {}
  22. ~D3D9ExWinApp() {}
  23. int create(void)
  24. {
  25. // base initialization
  26. D3DSample::create();
  27. // initialize DirectX
  28. HRESULT r;
  29. r = ::Direct3DCreate9Ex(D3D_SDK_VERSION, &m_pD3D9Ex);
  30. if (FAILED(r))
  31. {
  32. return EXIT_FAILURE;
  33. }
  34. DWORD flags = D3DCREATE_HARDWARE_VERTEXPROCESSING |
  35. D3DCREATE_PUREDEVICE |
  36. D3DCREATE_NOWINDOWCHANGES |
  37. D3DCREATE_MULTITHREADED |
  38. D3DCREATE_FPU_PRESERVE;
  39. D3DPRESENT_PARAMETERS d3dpp;
  40. ::ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
  41. d3dpp.Windowed = true;
  42. d3dpp.Flags = 0;
  43. d3dpp.BackBufferCount = 0;
  44. d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
  45. d3dpp.BackBufferHeight = m_height;
  46. d3dpp.BackBufferWidth = m_width;
  47. d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
  48. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  49. d3dpp.hDeviceWindow = m_hWnd;
  50. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  51. d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  52. r = m_pD3D9Ex->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, flags, &d3dpp, NULL, &m_pD3D9DevEx);
  53. if (FAILED(r))
  54. {
  55. return EXIT_FAILURE;
  56. }
  57. r = m_pD3D9DevEx->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &m_pBackBuffer);
  58. if (FAILED(r))
  59. {
  60. return EXIT_FAILURE;
  61. }
  62. r = m_pD3D9DevEx->CreateOffscreenPlainSurface(m_width, m_height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pSurface, NULL);
  63. if (FAILED(r))
  64. {
  65. std::cerr << "Can't create surface for result" << std::endl;
  66. return EXIT_FAILURE;
  67. }
  68. // initialize OpenCL context of OpenCV lib from DirectX
  69. if (cv::ocl::haveOpenCL())
  70. {
  71. m_oclCtx = cv::directx::ocl::initializeContextFromDirect3DDevice9(m_pD3D9DevEx);
  72. }
  73. m_oclDevName = cv::ocl::useOpenCL() ?
  74. cv::ocl::Context::getDefault().device(0).name() :
  75. "No OpenCL device";
  76. return EXIT_SUCCESS;
  77. } // create()
  78. // get media data on DX surface for further processing
  79. int get_surface(LPDIRECT3DSURFACE9* ppSurface)
  80. {
  81. HRESULT r;
  82. if (!m_cap.read(m_frame_bgr))
  83. return EXIT_FAILURE;
  84. cv::cvtColor(m_frame_bgr, m_frame_rgba, cv::COLOR_BGR2BGRA);
  85. D3DLOCKED_RECT memDesc = { 0, NULL };
  86. RECT rc = { 0, 0, m_width, m_height };
  87. r = m_pSurface->LockRect(&memDesc, &rc, 0);
  88. if (FAILED(r))
  89. {
  90. return r;
  91. }
  92. cv::Mat m(m_height, m_width, CV_8UC4, memDesc.pBits, memDesc.Pitch);
  93. // copy video frame data to surface
  94. m_frame_rgba.copyTo(m);
  95. r = m_pSurface->UnlockRect();
  96. if (FAILED(r))
  97. {
  98. return r;
  99. }
  100. *ppSurface = m_pSurface;
  101. return EXIT_SUCCESS;
  102. } // get_surface()
  103. // process and render media data
  104. int render()
  105. {
  106. try
  107. {
  108. if (m_shutdown)
  109. return EXIT_SUCCESS;
  110. // capture user input once
  111. MODE mode = m_mode == MODE_GPU_NV12 ? MODE_GPU_RGBA : m_mode;
  112. HRESULT r;
  113. LPDIRECT3DSURFACE9 pSurface;
  114. r = get_surface(&pSurface);
  115. if (FAILED(r))
  116. {
  117. return EXIT_FAILURE;
  118. }
  119. m_timer.reset();
  120. m_timer.start();
  121. switch (mode)
  122. {
  123. case MODE_CPU:
  124. {
  125. // process video frame on CPU
  126. D3DLOCKED_RECT memDesc = { 0, NULL };
  127. RECT rc = { 0, 0, m_width, m_height };
  128. r = pSurface->LockRect(&memDesc, &rc, 0);
  129. if (FAILED(r))
  130. {
  131. return EXIT_FAILURE;
  132. }
  133. cv::Mat m(m_height, m_width, CV_8UC4, memDesc.pBits, memDesc.Pitch);
  134. if (m_demo_processing)
  135. {
  136. // blur D3D9 surface with OpenCV on CPU
  137. cv::blur(m, m, cv::Size(15, 15));
  138. }
  139. r = pSurface->UnlockRect();
  140. if (FAILED(r))
  141. {
  142. return EXIT_FAILURE;
  143. }
  144. break;
  145. }
  146. case MODE_GPU_RGBA:
  147. {
  148. // process video frame on GPU
  149. cv::UMat u;
  150. cv::directx::convertFromDirect3DSurface9(pSurface, u);
  151. if (m_demo_processing)
  152. {
  153. // blur D3D9 surface with OpenCV on GPU with OpenCL
  154. cv::blur(u, u, cv::Size(15, 15));
  155. }
  156. cv::directx::convertToDirect3DSurface9(u, pSurface);
  157. break;
  158. }
  159. } // switch
  160. m_timer.stop();
  161. print_info(pSurface, m_mode, m_timer.getTimeMilli(), m_oclDevName);
  162. // traditional DX render pipeline:
  163. // BitBlt surface to backBuffer and flip backBuffer to frontBuffer
  164. r = m_pD3D9DevEx->StretchRect(pSurface, NULL, m_pBackBuffer, NULL, D3DTEXF_NONE);
  165. if (FAILED(r))
  166. {
  167. return EXIT_FAILURE;
  168. }
  169. // present the back buffer contents to the display
  170. r = m_pD3D9DevEx->Present(NULL, NULL, NULL, NULL);
  171. if (FAILED(r))
  172. {
  173. return EXIT_FAILURE;
  174. }
  175. } // try
  176. catch (const cv::Exception& e)
  177. {
  178. std::cerr << "Exception: " << e.what() << std::endl;
  179. return 10;
  180. }
  181. return EXIT_SUCCESS;
  182. } // render()
  183. void print_info(LPDIRECT3DSURFACE9 pSurface, int mode, double time, cv::String oclDevName)
  184. {
  185. HDC hDC;
  186. HRESULT r = pSurface->GetDC(&hDC);
  187. if (FAILED(r))
  188. {
  189. return;
  190. }
  191. HFONT hFont = (HFONT)::GetStockObject(SYSTEM_FONT);
  192. HFONT hOldFont = (HFONT)::SelectObject(hDC, hFont);
  193. if (hOldFont)
  194. {
  195. TEXTMETRIC tm;
  196. ::GetTextMetrics(hDC, &tm);
  197. char buf[256];
  198. int y = 0;
  199. buf[0] = 0;
  200. sprintf(buf, "mode: %s", m_modeStr[mode].c_str());
  201. ::TextOut(hDC, 0, y, buf, (int)strlen(buf));
  202. y += tm.tmHeight;
  203. buf[0] = 0;
  204. sprintf(buf, m_demo_processing ? "blur frame" : "copy frame");
  205. ::TextOut(hDC, 0, y, buf, (int)strlen(buf));
  206. y += tm.tmHeight;
  207. buf[0] = 0;
  208. sprintf(buf, "time: %4.1f msec", time);
  209. ::TextOut(hDC, 0, y, buf, (int)strlen(buf));
  210. y += tm.tmHeight;
  211. buf[0] = 0;
  212. sprintf(buf, "OpenCL device: %s", oclDevName.c_str());
  213. ::TextOut(hDC, 0, y, buf, (int)strlen(buf));
  214. ::SelectObject(hDC, hOldFont);
  215. }
  216. r = pSurface->ReleaseDC(hDC);
  217. return;
  218. } // print_info()
  219. int cleanup(void)
  220. {
  221. SAFE_RELEASE(m_pSurface);
  222. SAFE_RELEASE(m_pBackBuffer);
  223. SAFE_RELEASE(m_pD3D9DevEx);
  224. SAFE_RELEASE(m_pD3D9Ex);
  225. D3DSample::cleanup();
  226. return EXIT_SUCCESS;
  227. } // cleanup()
  228. private:
  229. LPDIRECT3D9EX m_pD3D9Ex;
  230. LPDIRECT3DDEVICE9EX m_pD3D9DevEx;
  231. LPDIRECT3DSURFACE9 m_pBackBuffer;
  232. LPDIRECT3DSURFACE9 m_pSurface;
  233. cv::ocl::Context m_oclCtx;
  234. cv::String m_oclPlatformName;
  235. cv::String m_oclDevName;
  236. };
  237. // main func
  238. int main(int argc, char** argv)
  239. {
  240. std::string title = "D3D9Ex interop sample";
  241. return d3d_app<D3D9ExWinApp>(argc, argv, title);
  242. }