1
0

d3d11-duplicator.cpp 4.9 KB

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