device_directx9.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*********************************************************************************
  2. INTEL CORPORATION PROPRIETARY INFORMATION
  3. This software is supplied under the terms of a license agreement or nondisclosure
  4. agreement with Intel Corporation and may not be copied or disclosed except in
  5. accordance with the terms of that agreement
  6. Copyright(c) 2011-2015 Intel Corporation. All Rights Reserved.
  7. **********************************************************************************/
  8. // #include "mfx_samples_config.h"
  9. #if defined(WIN32) || defined(WIN64)
  10. //prefast signature used in combaseapi.h
  11. #ifndef _PREFAST_
  12. #pragma warning(disable:4068)
  13. #endif
  14. #include "device_directx9.h"
  15. // #include "igfx_s3dcontrol.h"
  16. #include "atlbase.h"
  17. // Macros
  18. #define MSDK_ZERO_MEMORY(VAR) {memset(&VAR, 0, sizeof(VAR));}
  19. #define MSDK_MEMCPY_VAR(dstVarName, src, count) memcpy_s(&(dstVarName), sizeof(dstVarName), (src), (count))
  20. CD3D9Device::CD3D9Device()
  21. {
  22. m_pD3D9 = NULL;
  23. m_pD3DD9 = NULL;
  24. m_pDeviceManager9 = NULL;
  25. MSDK_ZERO_MEMORY(m_D3DPP);
  26. m_resetToken = 0;
  27. m_nViews = 0;
  28. m_pS3DControl = NULL;
  29. MSDK_ZERO_MEMORY(m_backBufferDesc);
  30. m_pDXVAVPS = NULL;
  31. m_pDXVAVP_Left = NULL;
  32. m_pDXVAVP_Right = NULL;
  33. MSDK_ZERO_MEMORY(m_targetRect);
  34. MSDK_ZERO_MEMORY(m_VideoDesc);
  35. MSDK_ZERO_MEMORY(m_BltParams);
  36. MSDK_ZERO_MEMORY(m_Sample);
  37. // Initialize DXVA structures
  38. DXVA2_AYUVSample16 color = {
  39. 0x8000, // Cr
  40. 0x8000, // Cb
  41. 0x1000, // Y
  42. 0xffff // Alpha
  43. };
  44. DXVA2_ExtendedFormat format = { // DestFormat
  45. DXVA2_SampleProgressiveFrame, // SampleFormat
  46. DXVA2_VideoChromaSubsampling_MPEG2, // VideoChromaSubsampling
  47. DXVA_NominalRange_0_255, // NominalRange
  48. DXVA2_VideoTransferMatrix_BT709, // VideoTransferMatrix
  49. DXVA2_VideoLighting_bright, // VideoLighting
  50. DXVA2_VideoPrimaries_BT709, // VideoPrimaries
  51. DXVA2_VideoTransFunc_709 // VideoTransferFunction
  52. };
  53. // init m_VideoDesc structure
  54. MSDK_MEMCPY_VAR(m_VideoDesc.SampleFormat, &format, sizeof(DXVA2_ExtendedFormat));
  55. m_VideoDesc.SampleWidth = 0;
  56. m_VideoDesc.SampleHeight = 0;
  57. m_VideoDesc.InputSampleFreq.Numerator = 60;
  58. m_VideoDesc.InputSampleFreq.Denominator = 1;
  59. m_VideoDesc.OutputFrameFreq.Numerator = 60;
  60. m_VideoDesc.OutputFrameFreq.Denominator = 1;
  61. // init m_BltParams structure
  62. MSDK_MEMCPY_VAR(m_BltParams.DestFormat, &format, sizeof(DXVA2_ExtendedFormat));
  63. MSDK_MEMCPY_VAR(m_BltParams.BackgroundColor, &color, sizeof(DXVA2_AYUVSample16));
  64. // init m_Sample structure
  65. m_Sample.Start = 0;
  66. m_Sample.End = 1;
  67. m_Sample.SampleFormat = format;
  68. m_Sample.PlanarAlpha.Fraction = 0;
  69. m_Sample.PlanarAlpha.Value = 1;
  70. m_bIsA2rgb10 = FALSE;
  71. }
  72. bool CD3D9Device::CheckOverlaySupport()
  73. {
  74. D3DCAPS9 d3d9caps;
  75. D3DOVERLAYCAPS d3doverlaycaps = {0};
  76. IDirect3D9ExOverlayExtension *d3d9overlay = NULL;
  77. bool overlaySupported = false;
  78. memset(&d3d9caps, 0, sizeof(d3d9caps));
  79. HRESULT hr = m_pD3D9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3d9caps);
  80. if (FAILED(hr) || !(d3d9caps.Caps & D3DCAPS_OVERLAY))
  81. {
  82. overlaySupported = false;
  83. }
  84. else
  85. {
  86. hr = m_pD3D9->QueryInterface(IID_PPV_ARGS(&d3d9overlay));
  87. if (FAILED(hr) || (d3d9overlay == NULL))
  88. {
  89. overlaySupported = false;
  90. }
  91. else
  92. {
  93. hr = d3d9overlay->CheckDeviceOverlayType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
  94. m_D3DPP.BackBufferWidth,
  95. m_D3DPP.BackBufferHeight,
  96. m_D3DPP.BackBufferFormat, NULL,
  97. D3DDISPLAYROTATION_IDENTITY, &d3doverlaycaps);
  98. MSDK_SAFE_RELEASE(d3d9overlay);
  99. if (FAILED(hr))
  100. {
  101. overlaySupported = false;
  102. }
  103. else
  104. {
  105. overlaySupported = true;
  106. }
  107. }
  108. }
  109. return overlaySupported;
  110. }
  111. mfxStatus CD3D9Device::FillD3DPP(mfxHDL hWindow, mfxU16 nViews, D3DPRESENT_PARAMETERS &D3DPP)
  112. {
  113. mfxStatus sts = MFX_ERR_NONE;
  114. D3DPP.Windowed = true;
  115. D3DPP.hDeviceWindow = (HWND)hWindow;
  116. D3DPP.Flags = D3DPRESENTFLAG_VIDEO;
  117. D3DPP.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  118. D3DPP.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // note that this setting leads to an implicit timeBeginPeriod call
  119. D3DPP.BackBufferCount = 1;
  120. D3DPP.BackBufferFormat = (m_bIsA2rgb10) ? D3DFMT_A2R10G10B10 : D3DFMT_X8R8G8B8;
  121. if (hWindow)
  122. {
  123. RECT r;
  124. GetClientRect((HWND)hWindow, &r);
  125. int x = GetSystemMetrics(SM_CXSCREEN);
  126. int y = GetSystemMetrics(SM_CYSCREEN);
  127. D3DPP.BackBufferWidth = min(r.right - r.left, x);
  128. D3DPP.BackBufferHeight = min(r.bottom - r.top, y);
  129. }
  130. else
  131. {
  132. D3DPP.BackBufferWidth = GetSystemMetrics(SM_CYSCREEN);
  133. D3DPP.BackBufferHeight = GetSystemMetrics(SM_CYSCREEN);
  134. }
  135. //
  136. // Mark the back buffer lockable if software DXVA2 could be used.
  137. // This is because software DXVA2 device requires a lockable render target
  138. // for the optimal performance.
  139. //
  140. {
  141. D3DPP.Flags |= D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  142. }
  143. bool isOverlaySupported = CheckOverlaySupport();
  144. if (2 == nViews && !isOverlaySupported)
  145. return MFX_ERR_UNSUPPORTED;
  146. bool needOverlay = (2 == nViews) ? true : false;
  147. D3DPP.SwapEffect = needOverlay ? D3DSWAPEFFECT_OVERLAY : D3DSWAPEFFECT_DISCARD;
  148. return sts;
  149. }
  150. mfxStatus CD3D9Device::Init(
  151. mfxHDL hWindow,
  152. mfxU16 nViews,
  153. mfxU32 nAdapterNum)
  154. {
  155. mfxStatus sts = MFX_ERR_NONE;
  156. if (2 < nViews)
  157. return MFX_ERR_UNSUPPORTED;
  158. m_nViews = nViews;
  159. HRESULT hr = Direct3DCreate9Ex(D3D_SDK_VERSION, &m_pD3D9);
  160. if (!m_pD3D9 || FAILED(hr))
  161. return MFX_ERR_DEVICE_FAILED;
  162. ZeroMemory(&m_D3DPP, sizeof(m_D3DPP));
  163. sts = FillD3DPP(hWindow, nViews, m_D3DPP);
  164. MSDK_CHECK_RESULT(sts, MFX_ERR_NONE, sts);
  165. hr = m_pD3D9->CreateDeviceEx(
  166. nAdapterNum,
  167. D3DDEVTYPE_HAL,
  168. (HWND)hWindow,
  169. D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
  170. &m_D3DPP,
  171. NULL,
  172. &m_pD3DD9);
  173. if (FAILED(hr))
  174. return MFX_ERR_NULL_PTR;
  175. if(hWindow)
  176. {
  177. hr = m_pD3DD9->ResetEx(&m_D3DPP, NULL);
  178. if (FAILED(hr))
  179. return MFX_ERR_UNDEFINED_BEHAVIOR;
  180. hr = m_pD3DD9->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  181. if (FAILED(hr))
  182. return MFX_ERR_UNDEFINED_BEHAVIOR;
  183. }
  184. UINT resetToken = 0;
  185. hr = DXVA2CreateDirect3DDeviceManager9(&resetToken, &m_pDeviceManager9);
  186. if (FAILED(hr))
  187. return MFX_ERR_NULL_PTR;
  188. hr = m_pDeviceManager9->ResetDevice(m_pD3DD9, resetToken);
  189. if (FAILED(hr))
  190. return MFX_ERR_UNDEFINED_BEHAVIOR;
  191. m_resetToken = resetToken;
  192. return sts;
  193. }
  194. mfxStatus CD3D9Device::Reset()
  195. {
  196. HRESULT hr = NO_ERROR;
  197. MSDK_CHECK_POINTER(m_pD3DD9, MFX_ERR_NULL_PTR);
  198. if (m_D3DPP.Windowed)
  199. {
  200. RECT r;
  201. GetClientRect((HWND)m_D3DPP.hDeviceWindow, &r);
  202. int x = GetSystemMetrics(SM_CXSCREEN);
  203. int y = GetSystemMetrics(SM_CYSCREEN);
  204. m_D3DPP.BackBufferWidth = min(r.right - r.left, x);
  205. m_D3DPP.BackBufferHeight = min(r.bottom - r.top, y);
  206. }
  207. else
  208. {
  209. m_D3DPP.BackBufferWidth = GetSystemMetrics(SM_CXSCREEN);
  210. m_D3DPP.BackBufferHeight = GetSystemMetrics(SM_CYSCREEN);
  211. }
  212. // Reset will change the parameters, so use a copy instead.
  213. D3DPRESENT_PARAMETERS d3dpp = m_D3DPP;
  214. hr = m_pD3DD9->ResetEx(&d3dpp, NULL);
  215. if (FAILED(hr))
  216. return MFX_ERR_UNDEFINED_BEHAVIOR;
  217. hr = m_pDeviceManager9->ResetDevice(m_pD3DD9, m_resetToken);
  218. if (FAILED(hr))
  219. return MFX_ERR_UNDEFINED_BEHAVIOR;
  220. return MFX_ERR_NONE;
  221. }
  222. void CD3D9Device::Close()
  223. {
  224. MSDK_SAFE_RELEASE(m_pDXVAVP_Left);
  225. MSDK_SAFE_RELEASE(m_pDXVAVP_Right);
  226. MSDK_SAFE_RELEASE(m_pDXVAVPS);
  227. MSDK_SAFE_RELEASE(m_pDeviceManager9);
  228. MSDK_SAFE_RELEASE(m_pD3DD9);
  229. MSDK_SAFE_RELEASE(m_pD3D9);
  230. m_pS3DControl = NULL;
  231. }
  232. CD3D9Device::~CD3D9Device()
  233. {
  234. Close();
  235. }
  236. mfxStatus CD3D9Device::GetHandle(mfxHandleType type, mfxHDL *pHdl)
  237. {
  238. if (MFX_HANDLE_DIRECT3D_DEVICE_MANAGER9 == type && pHdl != NULL)
  239. {
  240. *pHdl = m_pDeviceManager9;
  241. return MFX_ERR_NONE;
  242. }
  243. else if (MFX_HANDLE_GFXS3DCONTROL == type && pHdl != NULL)
  244. {
  245. *pHdl = m_pS3DControl;
  246. return MFX_ERR_NONE;
  247. }
  248. return MFX_ERR_UNSUPPORTED;
  249. }
  250. mfxStatus CD3D9Device::SetHandle(mfxHandleType type, mfxHDL hdl)
  251. {
  252. if (MFX_HANDLE_GFXS3DCONTROL == type && hdl != NULL)
  253. {
  254. m_pS3DControl = (IGFXS3DControl*)hdl;
  255. return MFX_ERR_NONE;
  256. }
  257. else if (MFX_HANDLE_DEVICEWINDOW == type && hdl != NULL) //for render window handle
  258. {
  259. m_D3DPP.hDeviceWindow = (HWND)hdl;
  260. return MFX_ERR_NONE;
  261. }
  262. return MFX_ERR_UNSUPPORTED;
  263. }
  264. mfxStatus CD3D9Device::RenderFrame(mfxFrameSurface1 * pSurface, mfxFrameAllocator * pmfxAlloc)
  265. {
  266. HRESULT hr = S_OK;
  267. if (!(1 == m_nViews || (2 == m_nViews && NULL != m_pS3DControl)))
  268. return MFX_ERR_UNDEFINED_BEHAVIOR;
  269. MSDK_CHECK_POINTER(pSurface, MFX_ERR_NULL_PTR);
  270. MSDK_CHECK_POINTER(m_pDeviceManager9, MFX_ERR_NOT_INITIALIZED);
  271. MSDK_CHECK_POINTER(pmfxAlloc, MFX_ERR_NULL_PTR);
  272. // don't try to render second view if output rect changed since first view
  273. if (2 == m_nViews && (0 != pSurface->Info.FrameId.ViewId))
  274. return MFX_ERR_NONE;
  275. hr = m_pD3DD9->TestCooperativeLevel();
  276. switch (hr)
  277. {
  278. case D3D_OK :
  279. break;
  280. case D3DERR_DEVICELOST :
  281. {
  282. return MFX_ERR_DEVICE_LOST;
  283. }
  284. case D3DERR_DEVICENOTRESET :
  285. {
  286. return MFX_ERR_UNKNOWN;
  287. }
  288. default :
  289. {
  290. return MFX_ERR_UNKNOWN;
  291. }
  292. }
  293. CComPtr<IDirect3DSurface9> pBackBuffer;
  294. hr = m_pD3DD9->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
  295. mfxHDLPair* dxMemId = (mfxHDLPair*)pSurface->Data.MemId;
  296. hr = m_pD3DD9->StretchRect((IDirect3DSurface9*)dxMemId->first, NULL, pBackBuffer, NULL, D3DTEXF_LINEAR);
  297. if (FAILED(hr))
  298. {
  299. return MFX_ERR_UNKNOWN;
  300. }
  301. if (SUCCEEDED(hr)&& (1 == m_nViews || pSurface->Info.FrameId.ViewId == 1))
  302. {
  303. hr = m_pD3DD9->Present(NULL, NULL, NULL, NULL);
  304. }
  305. return SUCCEEDED(hr) ? MFX_ERR_NONE : MFX_ERR_DEVICE_FAILED;
  306. }
  307. /*
  308. mfxStatus CD3D9Device::CreateVideoProcessors()
  309. {
  310. if (!(1 == m_nViews || (2 == m_nViews && NULL != m_pS3DControl)))
  311. return MFX_ERR_UNDEFINED_BEHAVIOR;
  312. MSDK_SAFE_RELEASE(m_pDXVAVP_Left);
  313. MSDK_SAFE_RELEASE(m_pDXVAVP_Right);
  314. HRESULT hr ;
  315. if (2 == m_nViews && NULL != m_pS3DControl)
  316. {
  317. hr = m_pS3DControl->SetDevice(m_pDeviceManager9);
  318. if (FAILED(hr))
  319. {
  320. return MFX_ERR_DEVICE_FAILED;
  321. }
  322. }
  323. ZeroMemory(&m_backBufferDesc, sizeof(m_backBufferDesc));
  324. IDirect3DSurface9 *backBufferTmp = NULL;
  325. hr = m_pD3DD9->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBufferTmp);
  326. if (NULL != backBufferTmp)
  327. backBufferTmp->GetDesc(&m_backBufferDesc);
  328. MSDK_SAFE_RELEASE(backBufferTmp);
  329. if (SUCCEEDED(hr))
  330. {
  331. // Create DXVA2 Video Processor Service.
  332. hr = DXVA2CreateVideoService(m_pD3DD9,
  333. IID_IDirectXVideoProcessorService,
  334. (void**)&m_pDXVAVPS);
  335. }
  336. if (2 == m_nViews)
  337. {
  338. // Activate L channel
  339. if (SUCCEEDED(hr))
  340. {
  341. hr = m_pS3DControl->SelectLeftView();
  342. }
  343. if (SUCCEEDED(hr))
  344. {
  345. // Create VPP device for the L channel
  346. hr = m_pDXVAVPS->CreateVideoProcessor(DXVA2_VideoProcProgressiveDevice,
  347. &m_VideoDesc,
  348. m_D3DPP.BackBufferFormat,
  349. 1,
  350. &m_pDXVAVP_Left);
  351. }
  352. // Activate R channel
  353. if (SUCCEEDED(hr))
  354. {
  355. hr = m_pS3DControl->SelectRightView();
  356. }
  357. }
  358. if (SUCCEEDED(hr))
  359. {
  360. hr = m_pDXVAVPS->CreateVideoProcessor(DXVA2_VideoProcProgressiveDevice,
  361. &m_VideoDesc,
  362. m_D3DPP.BackBufferFormat,
  363. 1,
  364. &m_pDXVAVP_Right);
  365. }
  366. return SUCCEEDED(hr) ? MFX_ERR_NONE : MFX_ERR_DEVICE_FAILED;
  367. }
  368. */
  369. #endif // #if defined(WIN32) || defined(WIN64)