d3d11-duplicator.cpp 7.5 KB

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