d3d11-duplicator.cpp 4.8 KB

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