dxgi_texture.cc (2291B)
1 /* 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "modules/desktop_capture/win/dxgi_texture.h" 12 13 #include <comdef.h> 14 #include <d3d11.h> 15 #include <dxgi.h> 16 #include <dxgi1_2.h> 17 #include <winerror.h> 18 #include <wrl/client.h> 19 20 #include "modules/desktop_capture/desktop_frame.h" 21 #include "modules/desktop_capture/win/desktop_capture_utils.h" 22 #include "rtc_base/checks.h" 23 #include "rtc_base/logging.h" 24 25 using Microsoft::WRL::ComPtr; 26 27 namespace webrtc { 28 29 namespace { 30 31 class DxgiDesktopFrame : public DesktopFrame { 32 public: 33 explicit DxgiDesktopFrame(const DxgiTexture& texture) 34 : DesktopFrame(texture.desktop_size(), 35 texture.pitch(), 36 texture.bits(), 37 nullptr) {} 38 39 ~DxgiDesktopFrame() override = default; 40 }; 41 42 } // namespace 43 44 DxgiTexture::DxgiTexture() = default; 45 DxgiTexture::~DxgiTexture() = default; 46 47 bool DxgiTexture::CopyFrom(const DXGI_OUTDUPL_FRAME_INFO& frame_info, 48 IDXGIResource* resource) { 49 RTC_DCHECK_GT(frame_info.AccumulatedFrames, 0); 50 RTC_DCHECK(resource); 51 ComPtr<ID3D11Texture2D> texture; 52 _com_error error = resource->QueryInterface( 53 __uuidof(ID3D11Texture2D), 54 reinterpret_cast<void**>(texture.GetAddressOf())); 55 if (error.Error() != S_OK || !texture) { 56 RTC_LOG(LS_ERROR) << "Failed to convert IDXGIResource to ID3D11Texture2D: " 57 << desktop_capture::utils::ComErrorToString(error); 58 return false; 59 } 60 61 D3D11_TEXTURE2D_DESC desc = {0}; 62 texture->GetDesc(&desc); 63 desktop_size_.set(desc.Width, desc.Height); 64 65 return CopyFromTexture(frame_info, texture.Get()); 66 } 67 68 const DesktopFrame& DxgiTexture::AsDesktopFrame() { 69 if (!frame_) { 70 frame_.reset(new DxgiDesktopFrame(*this)); 71 } 72 return *frame_; 73 } 74 75 bool DxgiTexture::Release() { 76 frame_.reset(); 77 return DoRelease(); 78 } 79 80 DXGI_MAPPED_RECT* DxgiTexture::rect() { 81 return &rect_; 82 } 83 84 } // namespace webrtc