d3d11-duplicator.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. static inline bool get_monitor(gs_device_t *device, int monitor_idx,
  16. IDXGIOutput **dxgiOutput)
  17. {
  18. HRESULT hr;
  19. hr = device->adapter->EnumOutputs(monitor_idx, dxgiOutput);
  20. if (FAILED(hr)) {
  21. if (hr == DXGI_ERROR_NOT_FOUND)
  22. return false;
  23. throw HRError("Failed to get output", hr);
  24. }
  25. return true;
  26. }
  27. void gs_duplicator::Start()
  28. {
  29. ComPtr<IDXGIOutput1> output1;
  30. ComPtr<IDXGIOutput> output;
  31. HRESULT hr;
  32. if (!get_monitor(device, idx, output.Assign()))
  33. throw "Invalid monitor index";
  34. hr = output->QueryInterface(__uuidof(IDXGIOutput1),
  35. (void**)output1.Assign());
  36. if (FAILED(hr))
  37. throw HRError("Failed to query IDXGIOutput1", hr);
  38. hr = output1->DuplicateOutput(device->device, duplicator.Assign());
  39. if (FAILED(hr))
  40. throw HRError("Failed to duplicate output", hr);
  41. }
  42. gs_duplicator::gs_duplicator(gs_device_t *device_, int monitor_idx)
  43. : gs_obj (device_, gs_type::gs_duplicator),
  44. texture (nullptr),
  45. idx (monitor_idx)
  46. {
  47. Start();
  48. }
  49. gs_duplicator::~gs_duplicator()
  50. {
  51. delete texture;
  52. }
  53. extern "C" {
  54. EXPORT bool device_get_duplicator_monitor_info(gs_device_t *device,
  55. int monitor_idx, struct gs_monitor_info *info)
  56. {
  57. DXGI_OUTPUT_DESC desc;
  58. HRESULT hr;
  59. try {
  60. ComPtr<IDXGIOutput> output;
  61. if (!get_monitor(device, monitor_idx, output.Assign()))
  62. return false;
  63. hr = output->GetDesc(&desc);
  64. if (FAILED(hr))
  65. throw HRError("GetDesc failed", hr);
  66. } catch (HRError error) {
  67. blog(LOG_ERROR, "device_get_duplicator_monitor_info: "
  68. "%s (%08lX)", error.str, error.hr);
  69. return false;
  70. }
  71. switch (desc.Rotation) {
  72. case DXGI_MODE_ROTATION_UNSPECIFIED:
  73. case DXGI_MODE_ROTATION_IDENTITY:
  74. info->rotation_degrees = 0;
  75. break;
  76. case DXGI_MODE_ROTATION_ROTATE90:
  77. info->rotation_degrees = 90;
  78. break;
  79. case DXGI_MODE_ROTATION_ROTATE180:
  80. info->rotation_degrees = 180;
  81. break;
  82. case DXGI_MODE_ROTATION_ROTATE270:
  83. info->rotation_degrees = 270;
  84. break;
  85. }
  86. info->x = desc.DesktopCoordinates.left;
  87. info->y = desc.DesktopCoordinates.top;
  88. info->cx = desc.DesktopCoordinates.right - info->x;
  89. info->cy = desc.DesktopCoordinates.bottom - info->y;
  90. return true;
  91. }
  92. EXPORT gs_duplicator_t *device_duplicator_create(gs_device_t *device,
  93. int monitor_idx)
  94. {
  95. gs_duplicator *duplicator = nullptr;
  96. try {
  97. duplicator = new gs_duplicator(device, monitor_idx);
  98. } catch (const char *error) {
  99. blog(LOG_DEBUG, "device_duplicator_create: %s",
  100. error);
  101. return nullptr;
  102. } catch (HRError error) {
  103. blog(LOG_DEBUG, "device_duplicator_create: %s (%08lX)",
  104. error.str, error.hr);
  105. return nullptr;
  106. }
  107. return duplicator;
  108. }
  109. EXPORT void gs_duplicator_destroy(gs_duplicator_t *duplicator)
  110. {
  111. delete duplicator;
  112. }
  113. static inline void copy_texture(gs_duplicator_t *d, ID3D11Texture2D *tex)
  114. {
  115. D3D11_TEXTURE2D_DESC desc;
  116. tex->GetDesc(&desc);
  117. if (!d->texture ||
  118. d->texture->width != desc.Width ||
  119. d->texture->height != desc.Height) {
  120. delete d->texture;
  121. d->texture = (gs_texture_2d*)gs_texture_create(
  122. desc.Width, desc.Height,
  123. ConvertDXGITextureFormat(desc.Format), 1,
  124. nullptr, 0);
  125. }
  126. if (!!d->texture)
  127. d->device->context->CopyResource(d->texture->texture,
  128. tex);
  129. }
  130. EXPORT bool gs_duplicator_update_frame(gs_duplicator_t *d)
  131. {
  132. DXGI_OUTDUPL_FRAME_INFO info;
  133. ComPtr<ID3D11Texture2D> tex;
  134. ComPtr<IDXGIResource> res;
  135. HRESULT hr;
  136. hr = d->duplicator->AcquireNextFrame(0, &info, res.Assign());
  137. if (hr == DXGI_ERROR_ACCESS_LOST) {
  138. return false;
  139. } else if (hr == DXGI_ERROR_WAIT_TIMEOUT) {
  140. return true;
  141. } else if (FAILED(hr)) {
  142. blog(LOG_ERROR, "gs_duplicator_update_frame: Failed to update "
  143. "frame (%08lX)", hr);
  144. return true;
  145. }
  146. hr = res->QueryInterface(__uuidof(ID3D11Texture2D),
  147. (void**)tex.Assign());
  148. if (FAILED(hr)) {
  149. blog(LOG_ERROR, "gs_duplicator_update_frame: Failed to query "
  150. "ID3D11Texture2D (%08lX)", hr);
  151. d->duplicator->ReleaseFrame();
  152. return true;
  153. }
  154. copy_texture(d, tex);
  155. d->duplicator->ReleaseFrame();
  156. return true;
  157. }
  158. EXPORT gs_texture_t *gs_duplicator_get_texture(gs_duplicator_t *duplicator)
  159. {
  160. return duplicator->texture;
  161. }
  162. }