d3d11-duplicator.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_B8G8R8A8_UNORM,
  41. };
  42. hr = output5->DuplicateOutput1(device->device, 0,
  43. _countof(supportedFormats),
  44. supportedFormats,
  45. duplicator.Assign());
  46. if (FAILED(hr))
  47. throw HRError("Failed to DuplicateOutput1", hr);
  48. } else {
  49. hr = output->QueryInterface(IID_PPV_ARGS(output1.Assign()));
  50. if (FAILED(hr))
  51. throw HRError("Failed to query IDXGIOutput1", hr);
  52. hr = output1->DuplicateOutput(device->device,
  53. duplicator.Assign());
  54. if (FAILED(hr))
  55. throw HRError("Failed to DuplicateOutput", hr);
  56. }
  57. }
  58. gs_duplicator::gs_duplicator(gs_device_t *device_, int monitor_idx)
  59. : gs_obj(device_, gs_type::gs_duplicator),
  60. texture(nullptr),
  61. idx(monitor_idx),
  62. refs(1),
  63. updated(false)
  64. {
  65. Start();
  66. }
  67. gs_duplicator::~gs_duplicator()
  68. {
  69. delete texture;
  70. }
  71. extern "C" {
  72. EXPORT bool device_get_duplicator_monitor_info(gs_device_t *device,
  73. int monitor_idx,
  74. struct gs_monitor_info *info)
  75. {
  76. DXGI_OUTPUT_DESC desc;
  77. HRESULT hr;
  78. try {
  79. ComPtr<IDXGIOutput> output;
  80. if (!get_monitor(device, monitor_idx, output.Assign()))
  81. return false;
  82. hr = output->GetDesc(&desc);
  83. if (FAILED(hr))
  84. throw HRError("GetDesc failed", hr);
  85. } catch (const HRError &error) {
  86. blog(LOG_ERROR,
  87. "device_get_duplicator_monitor_info: "
  88. "%s (%08lX)",
  89. error.str, error.hr);
  90. return false;
  91. }
  92. switch (desc.Rotation) {
  93. case DXGI_MODE_ROTATION_UNSPECIFIED:
  94. case DXGI_MODE_ROTATION_IDENTITY:
  95. info->rotation_degrees = 0;
  96. break;
  97. case DXGI_MODE_ROTATION_ROTATE90:
  98. info->rotation_degrees = 90;
  99. break;
  100. case DXGI_MODE_ROTATION_ROTATE180:
  101. info->rotation_degrees = 180;
  102. break;
  103. case DXGI_MODE_ROTATION_ROTATE270:
  104. info->rotation_degrees = 270;
  105. break;
  106. }
  107. info->x = desc.DesktopCoordinates.left;
  108. info->y = desc.DesktopCoordinates.top;
  109. info->cx = desc.DesktopCoordinates.right - info->x;
  110. info->cy = desc.DesktopCoordinates.bottom - info->y;
  111. return true;
  112. }
  113. EXPORT int device_duplicator_get_monitor_index(gs_device_t *device,
  114. void *monitor)
  115. {
  116. const HMONITOR handle = (HMONITOR)monitor;
  117. int index = -1;
  118. UINT output = 0;
  119. while (index == -1) {
  120. IDXGIOutput *pOutput;
  121. const HRESULT hr =
  122. 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,
  155. int monitor_idx)
  156. {
  157. gs_duplicator *duplicator = nullptr;
  158. const auto it = instances.find(monitor_idx);
  159. if (it != instances.end()) {
  160. duplicator = it->second;
  161. duplicator->refs++;
  162. return duplicator;
  163. }
  164. try {
  165. duplicator = new gs_duplicator(device, monitor_idx);
  166. instances[monitor_idx] = duplicator;
  167. } catch (const char *error) {
  168. blog(LOG_DEBUG, "device_duplicator_create: %s", error);
  169. return nullptr;
  170. } catch (const HRError &error) {
  171. blog(LOG_DEBUG, "device_duplicator_create: %s (%08lX)",
  172. error.str, error.hr);
  173. return nullptr;
  174. }
  175. return duplicator;
  176. }
  177. EXPORT void gs_duplicator_destroy(gs_duplicator_t *duplicator)
  178. {
  179. if (--duplicator->refs == 0) {
  180. instances.erase(duplicator->idx);
  181. delete duplicator;
  182. }
  183. }
  184. static inline void copy_texture(gs_duplicator_t *d, ID3D11Texture2D *tex)
  185. {
  186. D3D11_TEXTURE2D_DESC desc;
  187. tex->GetDesc(&desc);
  188. const gs_color_format format = ConvertDXGITextureFormat(desc.Format);
  189. const gs_color_format general_format = gs_generalize_format(format);
  190. if (!d->texture || (d->texture->width != desc.Width) ||
  191. (d->texture->height != desc.Height) ||
  192. (d->texture->format != general_format)) {
  193. delete d->texture;
  194. d->texture = (gs_texture_2d *)gs_texture_create(
  195. desc.Width, desc.Height, general_format, 1, nullptr, 0);
  196. }
  197. if (d->texture)
  198. d->device->context->CopyResource(d->texture->texture, tex);
  199. }
  200. EXPORT bool gs_duplicator_update_frame(gs_duplicator_t *d)
  201. {
  202. DXGI_OUTDUPL_FRAME_INFO info;
  203. ComPtr<ID3D11Texture2D> tex;
  204. ComPtr<IDXGIResource> res;
  205. HRESULT hr;
  206. if (!d->duplicator) {
  207. return false;
  208. }
  209. if (d->updated) {
  210. return true;
  211. }
  212. hr = d->duplicator->AcquireNextFrame(0, &info, res.Assign());
  213. if (hr == DXGI_ERROR_ACCESS_LOST) {
  214. return false;
  215. } else if (hr == DXGI_ERROR_WAIT_TIMEOUT) {
  216. return true;
  217. } else if (FAILED(hr)) {
  218. blog(LOG_ERROR,
  219. "gs_duplicator_update_frame: Failed to update "
  220. "frame (%08lX)",
  221. hr);
  222. return true;
  223. }
  224. hr = res->QueryInterface(__uuidof(ID3D11Texture2D),
  225. (void **)tex.Assign());
  226. if (FAILED(hr)) {
  227. blog(LOG_ERROR,
  228. "gs_duplicator_update_frame: Failed to query "
  229. "ID3D11Texture2D (%08lX)",
  230. hr);
  231. d->duplicator->ReleaseFrame();
  232. return true;
  233. }
  234. copy_texture(d, tex);
  235. d->duplicator->ReleaseFrame();
  236. d->updated = true;
  237. return true;
  238. }
  239. EXPORT gs_texture_t *gs_duplicator_get_texture(gs_duplicator_t *duplicator)
  240. {
  241. return duplicator->texture;
  242. }
  243. }