d3d11-duplicator.cpp 6.2 KB

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