d3d11-duplicator.cpp 6.5 KB

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