d3d11-duplicator.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "d3d11-subsystem.hpp"
  15. #include <unordered_map>
  16. static inline bool get_monitor(gs_device_t *device, int monitor_idx,
  17. IDXGIOutput **dxgiOutput)
  18. {
  19. HRESULT hr;
  20. hr = device->adapter->EnumOutputs(monitor_idx, dxgiOutput);
  21. if (FAILED(hr)) {
  22. if (hr == DXGI_ERROR_NOT_FOUND)
  23. return false;
  24. throw HRError("Failed to get output", hr);
  25. }
  26. return true;
  27. }
  28. void gs_duplicator::Start()
  29. {
  30. ComPtr<IDXGIOutput5> output5;
  31. ComPtr<IDXGIOutput1> output1;
  32. ComPtr<IDXGIOutput> output;
  33. HRESULT hr;
  34. if (!get_monitor(device, idx, output.Assign()))
  35. throw "Invalid monitor index";
  36. hr = output->QueryInterface(IID_PPV_ARGS(output5.Assign()));
  37. if (SUCCEEDED(hr)) {
  38. constexpr DXGI_FORMAT supportedFormats[]{
  39. DXGI_FORMAT_R16G16B16A16_FLOAT,
  40. DXGI_FORMAT_R10G10B10A2_UNORM,
  41. DXGI_FORMAT_B8G8R8A8_UNORM,
  42. };
  43. hr = output5->DuplicateOutput1(device->device, 0,
  44. _countof(supportedFormats),
  45. supportedFormats,
  46. duplicator.Assign());
  47. if (FAILED(hr))
  48. throw HRError("Failed to DuplicateOutput1", hr);
  49. } else {
  50. hr = output->QueryInterface(IID_PPV_ARGS(output1.Assign()));
  51. if (FAILED(hr))
  52. throw HRError("Failed to query IDXGIOutput1", hr);
  53. hr = output1->DuplicateOutput(device->device,
  54. duplicator.Assign());
  55. if (FAILED(hr))
  56. throw HRError("Failed to DuplicateOutput", hr);
  57. }
  58. }
  59. gs_duplicator::gs_duplicator(gs_device_t *device_, int monitor_idx)
  60. : gs_obj(device_, gs_type::gs_duplicator),
  61. texture(nullptr),
  62. idx(monitor_idx),
  63. refs(1),
  64. updated(false)
  65. {
  66. Start();
  67. }
  68. gs_duplicator::~gs_duplicator()
  69. {
  70. delete texture;
  71. }
  72. extern "C" {
  73. EXPORT bool device_get_duplicator_monitor_info(gs_device_t *device,
  74. int monitor_idx,
  75. struct gs_monitor_info *info)
  76. {
  77. DXGI_OUTPUT_DESC desc;
  78. HRESULT hr;
  79. try {
  80. ComPtr<IDXGIOutput> output;
  81. if (!get_monitor(device, monitor_idx, output.Assign()))
  82. return false;
  83. hr = output->GetDesc(&desc);
  84. if (FAILED(hr))
  85. throw HRError("GetDesc failed", hr);
  86. } catch (const HRError &error) {
  87. blog(LOG_ERROR,
  88. "device_get_duplicator_monitor_info: "
  89. "%s (%08lX)",
  90. error.str, error.hr);
  91. return false;
  92. }
  93. switch (desc.Rotation) {
  94. case DXGI_MODE_ROTATION_UNSPECIFIED:
  95. case DXGI_MODE_ROTATION_IDENTITY:
  96. info->rotation_degrees = 0;
  97. break;
  98. case DXGI_MODE_ROTATION_ROTATE90:
  99. info->rotation_degrees = 90;
  100. break;
  101. case DXGI_MODE_ROTATION_ROTATE180:
  102. info->rotation_degrees = 180;
  103. break;
  104. case DXGI_MODE_ROTATION_ROTATE270:
  105. info->rotation_degrees = 270;
  106. break;
  107. }
  108. info->x = desc.DesktopCoordinates.left;
  109. info->y = desc.DesktopCoordinates.top;
  110. info->cx = desc.DesktopCoordinates.right - info->x;
  111. info->cy = desc.DesktopCoordinates.bottom - info->y;
  112. return true;
  113. }
  114. EXPORT int device_duplicator_get_monitor_index(gs_device_t *device,
  115. void *monitor)
  116. {
  117. const HMONITOR handle = (HMONITOR)monitor;
  118. int index = -1;
  119. UINT output = 0;
  120. while (index == -1) {
  121. IDXGIOutput *pOutput;
  122. const HRESULT hr =
  123. device->adapter->EnumOutputs(output, &pOutput);
  124. if (hr == DXGI_ERROR_NOT_FOUND)
  125. break;
  126. if (SUCCEEDED(hr)) {
  127. DXGI_OUTPUT_DESC desc;
  128. if (SUCCEEDED(pOutput->GetDesc(&desc))) {
  129. if (desc.Monitor == handle)
  130. index = output;
  131. } else {
  132. blog(LOG_ERROR,
  133. "device_duplicator_get_monitor_index: "
  134. "Failed to get desc (%08lX)",
  135. hr);
  136. }
  137. pOutput->Release();
  138. } else if (hr == DXGI_ERROR_NOT_FOUND) {
  139. blog(LOG_ERROR,
  140. "device_duplicator_get_monitor_index: "
  141. "Failed to get output (%08lX)",
  142. hr);
  143. }
  144. ++output;
  145. }
  146. return index;
  147. }
  148. static std::unordered_map<int, gs_duplicator *> instances;
  149. void reset_duplicators(void)
  150. {
  151. for (std::pair<const int, gs_duplicator *> &pair : instances) {
  152. pair.second->updated = false;
  153. }
  154. }
  155. EXPORT gs_duplicator_t *device_duplicator_create(gs_device_t *device,
  156. int monitor_idx)
  157. {
  158. gs_duplicator *duplicator = nullptr;
  159. const auto it = instances.find(monitor_idx);
  160. if (it != instances.end()) {
  161. duplicator = it->second;
  162. duplicator->refs++;
  163. return duplicator;
  164. }
  165. try {
  166. duplicator = new gs_duplicator(device, monitor_idx);
  167. instances[monitor_idx] = duplicator;
  168. } catch (const char *error) {
  169. blog(LOG_DEBUG, "device_duplicator_create: %s", error);
  170. return nullptr;
  171. } catch (const HRError &error) {
  172. blog(LOG_DEBUG, "device_duplicator_create: %s (%08lX)",
  173. error.str, error.hr);
  174. return nullptr;
  175. }
  176. return duplicator;
  177. }
  178. EXPORT void gs_duplicator_destroy(gs_duplicator_t *duplicator)
  179. {
  180. if (--duplicator->refs == 0) {
  181. instances.erase(duplicator->idx);
  182. delete duplicator;
  183. }
  184. }
  185. static inline void copy_texture(gs_duplicator_t *d, ID3D11Texture2D *tex)
  186. {
  187. D3D11_TEXTURE2D_DESC desc;
  188. tex->GetDesc(&desc);
  189. const gs_color_format format = ConvertDXGITextureFormat(desc.Format);
  190. const gs_color_format general_format = gs_generalize_format(format);
  191. if (!d->texture || (d->texture->width != desc.Width) ||
  192. (d->texture->height != desc.Height) ||
  193. (d->texture->format != general_format)) {
  194. delete d->texture;
  195. d->texture = (gs_texture_2d *)gs_texture_create(
  196. desc.Width, desc.Height, general_format, 1, nullptr, 0);
  197. }
  198. if (d->texture)
  199. d->device->context->CopyResource(d->texture->texture, tex);
  200. }
  201. EXPORT bool gs_duplicator_update_frame(gs_duplicator_t *d)
  202. {
  203. DXGI_OUTDUPL_FRAME_INFO info;
  204. ComPtr<ID3D11Texture2D> tex;
  205. ComPtr<IDXGIResource> res;
  206. HRESULT hr;
  207. if (!d->duplicator) {
  208. return false;
  209. }
  210. if (d->updated) {
  211. return true;
  212. }
  213. hr = d->duplicator->AcquireNextFrame(0, &info, res.Assign());
  214. if (hr == DXGI_ERROR_ACCESS_LOST) {
  215. return false;
  216. } else if (hr == DXGI_ERROR_WAIT_TIMEOUT) {
  217. return true;
  218. } else if (FAILED(hr)) {
  219. blog(LOG_ERROR,
  220. "gs_duplicator_update_frame: Failed to update "
  221. "frame (%08lX)",
  222. hr);
  223. return true;
  224. }
  225. hr = res->QueryInterface(__uuidof(ID3D11Texture2D),
  226. (void **)tex.Assign());
  227. if (FAILED(hr)) {
  228. blog(LOG_ERROR,
  229. "gs_duplicator_update_frame: Failed to query "
  230. "ID3D11Texture2D (%08lX)",
  231. hr);
  232. d->duplicator->ReleaseFrame();
  233. return true;
  234. }
  235. copy_texture(d, tex);
  236. d->duplicator->ReleaseFrame();
  237. d->updated = true;
  238. return true;
  239. }
  240. EXPORT gs_texture_t *gs_duplicator_get_texture(gs_duplicator_t *duplicator)
  241. {
  242. return duplicator->texture;
  243. }
  244. }