d3d11-duplicator.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. hdr = false;
  38. sdr_white_nits = 80.f;
  39. if (SUCCEEDED(hr)) {
  40. constexpr DXGI_FORMAT supportedFormats[]{
  41. DXGI_FORMAT_R16G16B16A16_FLOAT,
  42. DXGI_FORMAT_B8G8R8A8_UNORM,
  43. };
  44. hr = output5->DuplicateOutput1(device->device, 0,
  45. _countof(supportedFormats),
  46. supportedFormats,
  47. duplicator.Assign());
  48. if (FAILED(hr))
  49. throw HRError("Failed to DuplicateOutput1", hr);
  50. DXGI_OUTPUT_DESC desc;
  51. if (SUCCEEDED(output->GetDesc(&desc))) {
  52. gs_monitor_color_info info =
  53. device->GetMonitorColorInfo(desc.Monitor);
  54. hdr = info.hdr;
  55. sdr_white_nits = (float)info.sdr_white_nits;
  56. }
  57. } else {
  58. hr = output->QueryInterface(IID_PPV_ARGS(output1.Assign()));
  59. if (FAILED(hr))
  60. throw HRError("Failed to query IDXGIOutput1", hr);
  61. hr = output1->DuplicateOutput(device->device,
  62. duplicator.Assign());
  63. if (FAILED(hr))
  64. throw HRError("Failed to DuplicateOutput", hr);
  65. }
  66. }
  67. gs_duplicator::gs_duplicator(gs_device_t *device_, int monitor_idx)
  68. : gs_obj(device_, gs_type::gs_duplicator),
  69. texture(nullptr),
  70. idx(monitor_idx),
  71. refs(1),
  72. updated(false)
  73. {
  74. Start();
  75. }
  76. gs_duplicator::~gs_duplicator()
  77. {
  78. delete texture;
  79. }
  80. extern "C" {
  81. EXPORT bool device_get_duplicator_monitor_info(gs_device_t *device,
  82. int monitor_idx,
  83. struct gs_monitor_info *info)
  84. {
  85. DXGI_OUTPUT_DESC desc;
  86. HRESULT hr;
  87. try {
  88. ComPtr<IDXGIOutput> output;
  89. if (!get_monitor(device, monitor_idx, output.Assign()))
  90. return false;
  91. hr = output->GetDesc(&desc);
  92. if (FAILED(hr))
  93. throw HRError("GetDesc failed", hr);
  94. } catch (const HRError &error) {
  95. blog(LOG_ERROR,
  96. "device_get_duplicator_monitor_info: "
  97. "%s (%08lX)",
  98. error.str, error.hr);
  99. return false;
  100. }
  101. switch (desc.Rotation) {
  102. case DXGI_MODE_ROTATION_UNSPECIFIED:
  103. case DXGI_MODE_ROTATION_IDENTITY:
  104. info->rotation_degrees = 0;
  105. break;
  106. case DXGI_MODE_ROTATION_ROTATE90:
  107. info->rotation_degrees = 90;
  108. break;
  109. case DXGI_MODE_ROTATION_ROTATE180:
  110. info->rotation_degrees = 180;
  111. break;
  112. case DXGI_MODE_ROTATION_ROTATE270:
  113. info->rotation_degrees = 270;
  114. break;
  115. }
  116. info->x = desc.DesktopCoordinates.left;
  117. info->y = desc.DesktopCoordinates.top;
  118. info->cx = desc.DesktopCoordinates.right - info->x;
  119. info->cy = desc.DesktopCoordinates.bottom - info->y;
  120. return true;
  121. }
  122. EXPORT int device_duplicator_get_monitor_index(gs_device_t *device,
  123. void *monitor)
  124. {
  125. const HMONITOR handle = (HMONITOR)monitor;
  126. int index = -1;
  127. UINT output = 0;
  128. while (index == -1) {
  129. IDXGIOutput *pOutput;
  130. const HRESULT hr =
  131. device->adapter->EnumOutputs(output, &pOutput);
  132. if (hr == DXGI_ERROR_NOT_FOUND)
  133. break;
  134. if (SUCCEEDED(hr)) {
  135. DXGI_OUTPUT_DESC desc;
  136. if (SUCCEEDED(pOutput->GetDesc(&desc))) {
  137. if (desc.Monitor == handle)
  138. index = output;
  139. } else {
  140. blog(LOG_ERROR,
  141. "device_duplicator_get_monitor_index: "
  142. "Failed to get desc (%08lX)",
  143. hr);
  144. }
  145. pOutput->Release();
  146. } else if (hr == DXGI_ERROR_NOT_FOUND) {
  147. blog(LOG_ERROR,
  148. "device_duplicator_get_monitor_index: "
  149. "Failed to get output (%08lX)",
  150. hr);
  151. }
  152. ++output;
  153. }
  154. return index;
  155. }
  156. static std::unordered_map<int, gs_duplicator *> instances;
  157. void reset_duplicators(void)
  158. {
  159. for (std::pair<const int, gs_duplicator *> &pair : instances) {
  160. pair.second->updated = false;
  161. }
  162. }
  163. EXPORT gs_duplicator_t *device_duplicator_create(gs_device_t *device,
  164. int monitor_idx)
  165. {
  166. gs_duplicator *duplicator = nullptr;
  167. const auto it = instances.find(monitor_idx);
  168. if (it != instances.end()) {
  169. duplicator = it->second;
  170. duplicator->refs++;
  171. return duplicator;
  172. }
  173. try {
  174. duplicator = new gs_duplicator(device, monitor_idx);
  175. instances[monitor_idx] = duplicator;
  176. } catch (const char *error) {
  177. blog(LOG_DEBUG, "device_duplicator_create: %s", error);
  178. return nullptr;
  179. } catch (const HRError &error) {
  180. blog(LOG_DEBUG, "device_duplicator_create: %s (%08lX)",
  181. error.str, error.hr);
  182. return nullptr;
  183. }
  184. return duplicator;
  185. }
  186. EXPORT void gs_duplicator_destroy(gs_duplicator_t *duplicator)
  187. {
  188. if (--duplicator->refs == 0) {
  189. instances.erase(duplicator->idx);
  190. delete duplicator;
  191. }
  192. }
  193. static inline void copy_texture(gs_duplicator_t *d, ID3D11Texture2D *tex)
  194. {
  195. D3D11_TEXTURE2D_DESC desc;
  196. tex->GetDesc(&desc);
  197. const gs_color_format format = ConvertDXGITextureFormat(desc.Format);
  198. const gs_color_format general_format = gs_generalize_format(format);
  199. if (!d->texture || (d->texture->width != desc.Width) ||
  200. (d->texture->height != desc.Height) ||
  201. (d->texture->format != general_format)) {
  202. delete d->texture;
  203. d->texture = (gs_texture_2d *)gs_texture_create(
  204. desc.Width, desc.Height, general_format, 1, nullptr, 0);
  205. d->color_space = d->hdr ? GS_CS_709_SCRGB
  206. : ((desc.Format ==
  207. DXGI_FORMAT_R16G16B16A16_FLOAT)
  208. ? GS_CS_SRGB_16F
  209. : GS_CS_SRGB);
  210. }
  211. if (d->texture)
  212. d->device->context->CopyResource(d->texture->texture, tex);
  213. }
  214. EXPORT bool gs_duplicator_update_frame(gs_duplicator_t *d)
  215. {
  216. DXGI_OUTDUPL_FRAME_INFO info;
  217. ComPtr<ID3D11Texture2D> tex;
  218. ComPtr<IDXGIResource> res;
  219. HRESULT hr;
  220. if (!d->duplicator) {
  221. return false;
  222. }
  223. if (d->updated) {
  224. return true;
  225. }
  226. hr = d->duplicator->AcquireNextFrame(0, &info, res.Assign());
  227. if (hr == DXGI_ERROR_ACCESS_LOST) {
  228. return false;
  229. } else if (hr == DXGI_ERROR_WAIT_TIMEOUT) {
  230. return true;
  231. } else if (FAILED(hr)) {
  232. blog(LOG_ERROR,
  233. "gs_duplicator_update_frame: Failed to update "
  234. "frame (%08lX)",
  235. hr);
  236. return true;
  237. }
  238. hr = res->QueryInterface(__uuidof(ID3D11Texture2D),
  239. (void **)tex.Assign());
  240. if (FAILED(hr)) {
  241. blog(LOG_ERROR,
  242. "gs_duplicator_update_frame: Failed to query "
  243. "ID3D11Texture2D (%08lX)",
  244. hr);
  245. d->duplicator->ReleaseFrame();
  246. return true;
  247. }
  248. copy_texture(d, tex);
  249. d->duplicator->ReleaseFrame();
  250. d->updated = true;
  251. return true;
  252. }
  253. EXPORT gs_texture_t *gs_duplicator_get_texture(gs_duplicator_t *duplicator)
  254. {
  255. return duplicator->texture;
  256. }
  257. EXPORT enum gs_color_space
  258. gs_duplicator_get_color_space(gs_duplicator_t *duplicator)
  259. {
  260. return duplicator->color_space;
  261. }
  262. EXPORT float gs_duplicator_get_sdr_white_level(gs_duplicator_t *duplicator)
  263. {
  264. return duplicator->sdr_white_nits;
  265. }
  266. }