d3d11-duplicator.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <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, struct gs_monitor_info *info)
  59. {
  60. DXGI_OUTPUT_DESC desc;
  61. HRESULT hr;
  62. try {
  63. ComPtr<IDXGIOutput> output;
  64. if (!get_monitor(device, monitor_idx, output.Assign()))
  65. return false;
  66. hr = output->GetDesc(&desc);
  67. if (FAILED(hr))
  68. throw HRError("GetDesc failed", hr);
  69. } catch (HRError error) {
  70. blog(LOG_ERROR, "device_get_duplicator_monitor_info: "
  71. "%s (%08lX)", error.str, error.hr);
  72. return false;
  73. }
  74. switch (desc.Rotation) {
  75. case DXGI_MODE_ROTATION_UNSPECIFIED:
  76. case DXGI_MODE_ROTATION_IDENTITY:
  77. info->rotation_degrees = 0;
  78. break;
  79. case DXGI_MODE_ROTATION_ROTATE90:
  80. info->rotation_degrees = 90;
  81. break;
  82. case DXGI_MODE_ROTATION_ROTATE180:
  83. info->rotation_degrees = 180;
  84. break;
  85. case DXGI_MODE_ROTATION_ROTATE270:
  86. info->rotation_degrees = 270;
  87. break;
  88. }
  89. info->x = desc.DesktopCoordinates.left;
  90. info->y = desc.DesktopCoordinates.top;
  91. info->cx = desc.DesktopCoordinates.right - info->x;
  92. info->cy = desc.DesktopCoordinates.bottom - info->y;
  93. return true;
  94. }
  95. static std::map<int, gs_duplicator*> instances;
  96. void reset_duplicators(void)
  97. {
  98. for (auto &pair : instances) {
  99. pair.second->updated = false;
  100. }
  101. }
  102. EXPORT gs_duplicator_t *device_duplicator_create(gs_device_t *device,
  103. int monitor_idx)
  104. {
  105. gs_duplicator *duplicator = nullptr;
  106. auto it = instances.find(monitor_idx);
  107. if (it != instances.end()) {
  108. duplicator = it->second;
  109. duplicator->refs++;
  110. return duplicator;
  111. }
  112. try {
  113. duplicator = new gs_duplicator(device, monitor_idx);
  114. instances[monitor_idx] = duplicator;
  115. } catch (const char *error) {
  116. blog(LOG_DEBUG, "device_duplicator_create: %s",
  117. error);
  118. return nullptr;
  119. } catch (HRError error) {
  120. blog(LOG_DEBUG, "device_duplicator_create: %s (%08lX)",
  121. error.str, error.hr);
  122. return nullptr;
  123. }
  124. return duplicator;
  125. }
  126. EXPORT void gs_duplicator_destroy(gs_duplicator_t *duplicator)
  127. {
  128. if (--duplicator->refs == 0) {
  129. instances.erase(duplicator->idx);
  130. delete duplicator;
  131. }
  132. }
  133. static inline void copy_texture(gs_duplicator_t *d, ID3D11Texture2D *tex)
  134. {
  135. D3D11_TEXTURE2D_DESC desc;
  136. tex->GetDesc(&desc);
  137. if (!d->texture ||
  138. d->texture->width != desc.Width ||
  139. d->texture->height != desc.Height) {
  140. delete d->texture;
  141. d->texture = (gs_texture_2d*)gs_texture_create(
  142. desc.Width, desc.Height,
  143. ConvertDXGITextureFormat(desc.Format), 1,
  144. nullptr, 0);
  145. }
  146. if (!!d->texture)
  147. d->device->context->CopyResource(d->texture->texture,
  148. tex);
  149. }
  150. EXPORT bool gs_duplicator_update_frame(gs_duplicator_t *d)
  151. {
  152. DXGI_OUTDUPL_FRAME_INFO info;
  153. ComPtr<ID3D11Texture2D> tex;
  154. ComPtr<IDXGIResource> res;
  155. HRESULT hr;
  156. if (!d->duplicator) {
  157. return false;
  158. }
  159. if (d->updated) {
  160. return true;
  161. }
  162. hr = d->duplicator->AcquireNextFrame(0, &info, res.Assign());
  163. if (hr == DXGI_ERROR_ACCESS_LOST) {
  164. return false;
  165. } else if (hr == DXGI_ERROR_WAIT_TIMEOUT) {
  166. return true;
  167. } else if (FAILED(hr)) {
  168. blog(LOG_ERROR, "gs_duplicator_update_frame: Failed to update "
  169. "frame (%08lX)", hr);
  170. return true;
  171. }
  172. hr = res->QueryInterface(__uuidof(ID3D11Texture2D),
  173. (void**)tex.Assign());
  174. if (FAILED(hr)) {
  175. blog(LOG_ERROR, "gs_duplicator_update_frame: Failed to query "
  176. "ID3D11Texture2D (%08lX)", hr);
  177. d->duplicator->ReleaseFrame();
  178. return true;
  179. }
  180. copy_texture(d, tex);
  181. d->duplicator->ReleaseFrame();
  182. d->updated = true;
  183. return true;
  184. }
  185. EXPORT gs_texture_t *gs_duplicator_get_texture(gs_duplicator_t *duplicator)
  186. {
  187. return duplicator->texture;
  188. }
  189. }