ThreadManager.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved
  7. #include "ThreadManager.h"
  8. DWORD WINAPI DDProc(_In_ void* Param);
  9. THREADMANAGER::THREADMANAGER() : m_ThreadCount(0),
  10. m_ThreadHandles(nullptr),
  11. m_ThreadData(nullptr)
  12. {
  13. RtlZeroMemory(&m_PtrInfo, sizeof(m_PtrInfo));
  14. }
  15. THREADMANAGER::~THREADMANAGER()
  16. {
  17. Clean();
  18. }
  19. //
  20. // Clean up resources
  21. //
  22. void THREADMANAGER::Clean()
  23. {
  24. if (m_PtrInfo.PtrShapeBuffer) {
  25. delete [] m_PtrInfo.PtrShapeBuffer;
  26. m_PtrInfo.PtrShapeBuffer = nullptr;
  27. }
  28. RtlZeroMemory(&m_PtrInfo, sizeof(m_PtrInfo));
  29. if (m_ThreadHandles) {
  30. for (UINT i = 0; i < m_ThreadCount; ++i) {
  31. if (m_ThreadHandles[i]) {
  32. CloseHandle(m_ThreadHandles[i]);
  33. }
  34. }
  35. delete [] m_ThreadHandles;
  36. m_ThreadHandles = nullptr;
  37. }
  38. if (m_ThreadData) {
  39. for (UINT i = 0; i < m_ThreadCount; ++i) {
  40. CleanDx(&m_ThreadData[i].DxRes);
  41. }
  42. delete [] m_ThreadData;
  43. m_ThreadData = nullptr;
  44. }
  45. m_ThreadCount = 0;
  46. }
  47. //
  48. // Clean up DX_RESOURCES
  49. //
  50. void THREADMANAGER::CleanDx(_Inout_ DX_RESOURCES* Data)
  51. {
  52. if (Data->Device) {
  53. Data->Device->Release();
  54. Data->Device = nullptr;
  55. }
  56. if (Data->Context) {
  57. Data->Context->Release();
  58. Data->Context = nullptr;
  59. }
  60. if (Data->VertexShader) {
  61. Data->VertexShader->Release();
  62. Data->VertexShader = nullptr;
  63. }
  64. if (Data->PixelShader) {
  65. Data->PixelShader->Release();
  66. Data->PixelShader = nullptr;
  67. }
  68. if (Data->InputLayout) {
  69. Data->InputLayout->Release();
  70. Data->InputLayout = nullptr;
  71. }
  72. if (Data->SamplerLinear) {
  73. Data->SamplerLinear->Release();
  74. Data->SamplerLinear = nullptr;
  75. }
  76. }
  77. //
  78. // Start up threads for DDA
  79. //
  80. DUPL_RETURN THREADMANAGER::Initialize(INT SingleOutput, UINT OutputCount, HANDLE UnexpectedErrorEvent, HANDLE ExpectedErrorEvent, HANDLE TerminateThreadsEvent, HANDLE SharedHandle, _In_ const struct tmedia_producer_s* Producer, _In_ RECT* DesktopDim)
  81. {
  82. m_ThreadCount = OutputCount;
  83. m_ThreadHandles = new (std::nothrow) HANDLE[m_ThreadCount];
  84. m_ThreadData = new (std::nothrow) THREAD_DATA[m_ThreadCount];
  85. if (!m_ThreadHandles || !m_ThreadData) {
  86. return ProcessFailure(nullptr, L"Failed to allocate array for threads", L"Error", E_OUTOFMEMORY);
  87. }
  88. // Create appropriate # of threads for duplication
  89. DUPL_RETURN Ret = DUPL_RETURN_SUCCESS;
  90. for (UINT i = 0; i < m_ThreadCount; ++i) {
  91. m_ThreadData[i].UnexpectedErrorEvent = UnexpectedErrorEvent;
  92. m_ThreadData[i].ExpectedErrorEvent = ExpectedErrorEvent;
  93. m_ThreadData[i].TerminateThreadsEvent = TerminateThreadsEvent;
  94. m_ThreadData[i].Output = (SingleOutput < 0) ? i : SingleOutput;
  95. m_ThreadData[i].TexSharedHandle = SharedHandle;
  96. m_ThreadData[i].OffsetX = DesktopDim->left;
  97. m_ThreadData[i].OffsetY = DesktopDim->top;
  98. m_ThreadData[i].PtrInfo = &m_PtrInfo;
  99. m_ThreadData[i].Producer = Producer;
  100. RtlZeroMemory(&m_ThreadData[i].DxRes, sizeof(DX_RESOURCES));
  101. Ret = InitializeDx(&m_ThreadData[i].DxRes);
  102. if (Ret != DUPL_RETURN_SUCCESS) {
  103. return Ret;
  104. }
  105. DWORD ThreadId;
  106. m_ThreadHandles[i] = CreateThread(nullptr, 0, DDProc, &m_ThreadData[i], 0, &ThreadId);
  107. if (m_ThreadHandles[i] == nullptr) {
  108. return ProcessFailure(nullptr, L"Failed to create thread", L"Error", E_FAIL);
  109. }
  110. }
  111. return Ret;
  112. }
  113. //
  114. // Get DX_RESOURCES
  115. //
  116. DUPL_RETURN THREADMANAGER::InitializeDx(_Out_ DX_RESOURCES* Data)
  117. {
  118. HRESULT hr = S_OK;
  119. // Driver types supported
  120. D3D_DRIVER_TYPE DriverTypes[] = {
  121. D3D_DRIVER_TYPE_HARDWARE,
  122. D3D_DRIVER_TYPE_WARP,
  123. D3D_DRIVER_TYPE_REFERENCE,
  124. };
  125. UINT NumDriverTypes = ARRAYSIZE(DriverTypes);
  126. // Feature levels supported
  127. D3D_FEATURE_LEVEL FeatureLevels[] = {
  128. D3D_FEATURE_LEVEL_11_0,
  129. D3D_FEATURE_LEVEL_10_1,
  130. D3D_FEATURE_LEVEL_10_0,
  131. D3D_FEATURE_LEVEL_9_1
  132. };
  133. UINT NumFeatureLevels = ARRAYSIZE(FeatureLevels);
  134. D3D_FEATURE_LEVEL FeatureLevel;
  135. // Create device
  136. for (UINT DriverTypeIndex = 0; DriverTypeIndex < NumDriverTypes; ++DriverTypeIndex) {
  137. hr = D3D11CreateDevice(nullptr, DriverTypes[DriverTypeIndex], nullptr, 0, FeatureLevels, NumFeatureLevels,
  138. D3D11_SDK_VERSION, &Data->Device, &FeatureLevel, &Data->Context);
  139. if (SUCCEEDED(hr)) {
  140. // Device creation success, no need to loop anymore
  141. break;
  142. }
  143. }
  144. if (FAILED(hr)) {
  145. return ProcessFailure(nullptr, L"Failed to create device in InitializeDx", L"Error", hr);
  146. }
  147. // VERTEX shader
  148. UINT Size = ARRAYSIZE(g_VS);
  149. hr = Data->Device->CreateVertexShader(g_VS, Size, nullptr, &Data->VertexShader);
  150. if (FAILED(hr)) {
  151. return ProcessFailure(Data->Device, L"Failed to create vertex shader in InitializeDx", L"Error", hr, SystemTransitionsExpectedErrors);
  152. }
  153. // Input layout
  154. D3D11_INPUT_ELEMENT_DESC Layout[] = {
  155. {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
  156. {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}
  157. };
  158. UINT NumElements = ARRAYSIZE(Layout);
  159. hr = Data->Device->CreateInputLayout(Layout, NumElements, g_VS, Size, &Data->InputLayout);
  160. if (FAILED(hr)) {
  161. return ProcessFailure(Data->Device, L"Failed to create input layout in InitializeDx", L"Error", hr, SystemTransitionsExpectedErrors);
  162. }
  163. Data->Context->IASetInputLayout(Data->InputLayout);
  164. // Pixel shader
  165. Size = ARRAYSIZE(g_PS);
  166. hr = Data->Device->CreatePixelShader(g_PS, Size, nullptr, &Data->PixelShader);
  167. if (FAILED(hr)) {
  168. return ProcessFailure(Data->Device, L"Failed to create pixel shader in InitializeDx", L"Error", hr, SystemTransitionsExpectedErrors);
  169. }
  170. // Set up sampler
  171. D3D11_SAMPLER_DESC SampDesc;
  172. RtlZeroMemory(&SampDesc, sizeof(SampDesc));
  173. SampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  174. SampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
  175. SampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
  176. SampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
  177. SampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
  178. SampDesc.MinLOD = 0;
  179. SampDesc.MaxLOD = D3D11_FLOAT32_MAX;
  180. hr = Data->Device->CreateSamplerState(&SampDesc, &Data->SamplerLinear);
  181. if (FAILED(hr)) {
  182. return ProcessFailure(Data->Device, L"Failed to create sampler state in InitializeDx", L"Error", hr, SystemTransitionsExpectedErrors);
  183. }
  184. return DUPL_RETURN_SUCCESS;
  185. }
  186. //
  187. // Getter for the PTR_INFO structure
  188. //
  189. PTR_INFO* THREADMANAGER::GetPointerInfo()
  190. {
  191. return &m_PtrInfo;
  192. }
  193. //
  194. // Waits infinitely for all spawned threads to terminate
  195. //
  196. bool THREADMANAGER::WaitForThreadTermination(DWORD timeout /*= INFINITE*/)
  197. {
  198. bool bRet = true;
  199. if (m_ThreadCount != 0) {
  200. bRet = (WaitForMultipleObjectsEx(m_ThreadCount, m_ThreadHandles, TRUE, timeout, FALSE) != WAIT_TIMEOUT);
  201. }
  202. return bRet;
  203. }