d3d11-duplicator.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. ComPtr<IDXGIAdapter> dxgiAdapter;
  19. ComPtr<IDXGIDevice> dxgiDevice;
  20. HRESULT hr;
  21. hr = device->device->QueryInterface(__uuidof(IDXGIDevice),
  22. (void**)dxgiDevice.Assign());
  23. if (FAILED(hr))
  24. throw HRError("Failed to query IDXGIDevice", hr);
  25. hr = dxgiDevice->GetAdapter(dxgiAdapter.Assign());
  26. if (FAILED(hr))
  27. throw HRError("Failed to get adapter", hr);
  28. hr = dxgiAdapter->EnumOutputs(monitor_idx, dxgiOutput);
  29. if (FAILED(hr)) {
  30. if (hr == DXGI_ERROR_NOT_FOUND)
  31. return false;
  32. throw HRError("Failed to get output", hr);
  33. }
  34. return true;
  35. }
  36. gs_duplicator::gs_duplicator(gs_device_t *device_, int monitor_idx)
  37. : texture(nullptr), device(device_)
  38. {
  39. ComPtr<IDXGIOutput1> output1;
  40. ComPtr<IDXGIOutput> output;
  41. HRESULT hr;
  42. if (!get_monitor(device, monitor_idx, output.Assign()))
  43. throw "Invalid monitor index";
  44. hr = output->QueryInterface(__uuidof(IDXGIOutput1),
  45. (void**)output1.Assign());
  46. if (FAILED(hr))
  47. throw HRError("Failed to query IDXGIOutput1", hr);
  48. hr = output1->DuplicateOutput(device->device, duplicator.Assign());
  49. if (FAILED(hr))
  50. throw HRError("Failed to duplicate output", hr);
  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. EXPORT gs_duplicator_t *device_duplicator_create(gs_device_t *device,
  96. int monitor_idx)
  97. {
  98. gs_duplicator *duplicator = nullptr;
  99. try {
  100. duplicator = new gs_duplicator(device, monitor_idx);
  101. } catch (const char *error) {
  102. blog(LOG_ERROR, "device_duplicator_create: %s",
  103. error);
  104. return nullptr;
  105. } catch (HRError error) {
  106. blog(LOG_ERROR, "device_duplicator_create: %s (%08lX)",
  107. error.str, error.hr);
  108. return nullptr;
  109. }
  110. return duplicator;
  111. }
  112. EXPORT void gs_duplicator_destroy(gs_duplicator_t *duplicator)
  113. {
  114. delete duplicator;
  115. }
  116. static inline void copy_texture(gs_duplicator_t *d, ID3D11Texture2D *tex)
  117. {
  118. D3D11_TEXTURE2D_DESC desc;
  119. tex->GetDesc(&desc);
  120. if (!d->texture ||
  121. d->texture->width != desc.Width ||
  122. d->texture->height != desc.Height) {
  123. delete d->texture;
  124. d->texture = (gs_texture_2d*)gs_texture_create(
  125. desc.Width, desc.Height,
  126. ConvertDXGITextureFormat(desc.Format), 1,
  127. nullptr, 0);
  128. }
  129. if (!!d->texture)
  130. d->device->context->CopyResource(d->texture->texture,
  131. tex);
  132. }
  133. EXPORT bool gs_duplicator_update_frame(gs_duplicator_t *d)
  134. {
  135. DXGI_OUTDUPL_FRAME_INFO info;
  136. ComPtr<ID3D11Texture2D> tex;
  137. ComPtr<IDXGIResource> res;
  138. HRESULT hr;
  139. hr = d->duplicator->AcquireNextFrame(0, &info, res.Assign());
  140. if (hr == DXGI_ERROR_ACCESS_LOST) {
  141. return false;
  142. } else if (hr == DXGI_ERROR_WAIT_TIMEOUT) {
  143. return true;
  144. } else if (FAILED(hr)) {
  145. blog(LOG_ERROR, "gs_duplicator_update_frame: Failed to update "
  146. "frame (%08lX)", hr);
  147. return true;
  148. }
  149. hr = res->QueryInterface(__uuidof(ID3D11Texture2D),
  150. (void**)tex.Assign());
  151. if (FAILED(hr)) {
  152. blog(LOG_ERROR, "gs_duplicator_update_frame: Failed to query "
  153. "ID3D11Texture2D (%08lX)", hr);
  154. d->duplicator->ReleaseFrame();
  155. return true;
  156. }
  157. copy_texture(d, tex);
  158. d->duplicator->ReleaseFrame();
  159. return true;
  160. }
  161. EXPORT gs_texture_t *gs_duplicator_get_texture(gs_duplicator_t *duplicator)
  162. {
  163. return duplicator->texture;
  164. }
  165. }