1
0

d3d11-duplicator.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. static std::unordered_map<int, gs_duplicator *> instances;
  99. void reset_duplicators(void)
  100. {
  101. for (std::pair<const int, gs_duplicator *> &pair : instances) {
  102. pair.second->updated = false;
  103. }
  104. }
  105. EXPORT gs_duplicator_t *device_duplicator_create(gs_device_t *device,
  106. int monitor_idx)
  107. {
  108. gs_duplicator *duplicator = nullptr;
  109. const auto it = instances.find(monitor_idx);
  110. if (it != instances.end()) {
  111. duplicator = it->second;
  112. duplicator->refs++;
  113. return duplicator;
  114. }
  115. try {
  116. duplicator = new gs_duplicator(device, monitor_idx);
  117. instances[monitor_idx] = duplicator;
  118. } catch (const char *error) {
  119. blog(LOG_DEBUG, "device_duplicator_create: %s", error);
  120. return nullptr;
  121. } catch (const HRError &error) {
  122. blog(LOG_DEBUG, "device_duplicator_create: %s (%08lX)",
  123. error.str, error.hr);
  124. return nullptr;
  125. }
  126. return duplicator;
  127. }
  128. EXPORT void gs_duplicator_destroy(gs_duplicator_t *duplicator)
  129. {
  130. if (--duplicator->refs == 0) {
  131. instances.erase(duplicator->idx);
  132. delete duplicator;
  133. }
  134. }
  135. static inline void copy_texture(gs_duplicator_t *d, ID3D11Texture2D *tex)
  136. {
  137. D3D11_TEXTURE2D_DESC desc;
  138. tex->GetDesc(&desc);
  139. if (!d->texture || d->texture->width != desc.Width ||
  140. d->texture->height != desc.Height) {
  141. delete d->texture;
  142. d->texture = (gs_texture_2d *)gs_texture_create(
  143. desc.Width, desc.Height,
  144. ConvertDXGITextureFormat(desc.Format), 1, nullptr, 0);
  145. }
  146. if (!!d->texture)
  147. d->device->context->CopyResource(d->texture->texture, tex);
  148. }
  149. EXPORT bool gs_duplicator_update_frame(gs_duplicator_t *d)
  150. {
  151. DXGI_OUTDUPL_FRAME_INFO info;
  152. ComPtr<ID3D11Texture2D> tex;
  153. ComPtr<IDXGIResource> res;
  154. HRESULT hr;
  155. if (!d->duplicator) {
  156. return false;
  157. }
  158. if (d->updated) {
  159. return true;
  160. }
  161. hr = d->duplicator->AcquireNextFrame(0, &info, res.Assign());
  162. if (hr == DXGI_ERROR_ACCESS_LOST) {
  163. return false;
  164. } else if (hr == DXGI_ERROR_WAIT_TIMEOUT) {
  165. return true;
  166. } else if (FAILED(hr)) {
  167. blog(LOG_ERROR,
  168. "gs_duplicator_update_frame: Failed to update "
  169. "frame (%08lX)",
  170. hr);
  171. return true;
  172. }
  173. hr = res->QueryInterface(__uuidof(ID3D11Texture2D),
  174. (void **)tex.Assign());
  175. if (FAILED(hr)) {
  176. blog(LOG_ERROR,
  177. "gs_duplicator_update_frame: Failed to query "
  178. "ID3D11Texture2D (%08lX)",
  179. hr);
  180. d->duplicator->ReleaseFrame();
  181. return true;
  182. }
  183. copy_texture(d, tex);
  184. d->duplicator->ReleaseFrame();
  185. d->updated = true;
  186. return true;
  187. }
  188. EXPORT gs_texture_t *gs_duplicator_get_texture(gs_duplicator_t *duplicator)
  189. {
  190. return duplicator->texture;
  191. }
  192. }