| 169 | } |
| 170 | |
| 171 | bool FramePoolScreencap::init() |
| 172 | { |
| 173 | LogFunc; |
| 174 | |
| 175 | if (!hwnd_) { |
| 176 | LogError << "hwnd_ is nullptr"; |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | HRESULT ret = S_OK; |
| 181 | |
| 182 | DXGI_SWAP_CHAIN_DESC swap_chain_desc = { }; |
| 183 | swap_chain_desc.BufferCount = 1; |
| 184 | swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| 185 | swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; |
| 186 | swap_chain_desc.OutputWindow = hwnd_; |
| 187 | swap_chain_desc.SampleDesc.Count = 1; |
| 188 | swap_chain_desc.Windowed = TRUE; |
| 189 | |
| 190 | ret = D3D11CreateDeviceAndSwapChain( |
| 191 | nullptr, |
| 192 | D3D_DRIVER_TYPE_HARDWARE, |
| 193 | nullptr, |
| 194 | 0, |
| 195 | nullptr, |
| 196 | 0, |
| 197 | D3D11_SDK_VERSION, |
| 198 | &swap_chain_desc, |
| 199 | dxgi_swap_chain_.put(), |
| 200 | d3d_device_.put(), |
| 201 | nullptr, |
| 202 | d3d_context_.put()); |
| 203 | |
| 204 | if (FAILED(ret)) { |
| 205 | LogError << "D3D11CreateDeviceAndSwapChain failed" << VAR(ret); |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | try { |
| 210 | auto activation_factory = winrt::get_activation_factory<winrt::Windows::Graphics::Capture::GraphicsCaptureItem>(); |
| 211 | auto interop_factory = activation_factory.try_as<IGraphicsCaptureItemInterop>(); |
| 212 | if (!interop_factory) { |
| 213 | LogError << "Failed to get IGraphicsCaptureItemInterop"; |
| 214 | return false; |
| 215 | } |
| 216 | ret = interop_factory->CreateForWindow( |
| 217 | hwnd_, |
| 218 | winrt::guid_of<ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>(), |
| 219 | winrt::put_abi(cap_item_)); |
| 220 | } |
| 221 | catch (const winrt::hresult_error& e) { |
| 222 | LogError << "Failed to create GraphicsCaptureItem" << VAR(e.code()) << VAR(winrt::to_string(e.message())); |
| 223 | return false; |
| 224 | } |
| 225 | if (FAILED(ret)) { |
| 226 | LogError << "CreateForWindow GraphicsCaptureItem failed" << VAR(ret); |
| 227 | return false; |
| 228 | } |