xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/win/dxgi_texture.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 <wrl/client.h>
16 
17 #include "modules/desktop_capture/desktop_region.h"
18 #include "modules/desktop_capture/win/desktop_capture_utils.h"
19 #include "rtc_base/checks.h"
20 #include "rtc_base/logging.h"
21 
22 using Microsoft::WRL::ComPtr;
23 
24 namespace webrtc {
25 
26 namespace {
27 
28 class DxgiDesktopFrame : public DesktopFrame {
29  public:
DxgiDesktopFrame(const DxgiTexture & texture)30   explicit DxgiDesktopFrame(const DxgiTexture& texture)
31       : DesktopFrame(texture.desktop_size(),
32                      texture.pitch(),
33                      texture.bits(),
34                      nullptr) {}
35 
36   ~DxgiDesktopFrame() override = default;
37 };
38 
39 }  // namespace
40 
41 DxgiTexture::DxgiTexture() = default;
42 DxgiTexture::~DxgiTexture() = default;
43 
CopyFrom(const DXGI_OUTDUPL_FRAME_INFO & frame_info,IDXGIResource * resource)44 bool DxgiTexture::CopyFrom(const DXGI_OUTDUPL_FRAME_INFO& frame_info,
45                            IDXGIResource* resource) {
46   RTC_DCHECK_GT(frame_info.AccumulatedFrames, 0);
47   RTC_DCHECK(resource);
48   ComPtr<ID3D11Texture2D> texture;
49   _com_error error = resource->QueryInterface(
50       __uuidof(ID3D11Texture2D),
51       reinterpret_cast<void**>(texture.GetAddressOf()));
52   if (error.Error() != S_OK || !texture) {
53     RTC_LOG(LS_ERROR) << "Failed to convert IDXGIResource to ID3D11Texture2D: "
54                       << desktop_capture::utils::ComErrorToString(error);
55     return false;
56   }
57 
58   D3D11_TEXTURE2D_DESC desc = {0};
59   texture->GetDesc(&desc);
60   desktop_size_.set(desc.Width, desc.Height);
61 
62   return CopyFromTexture(frame_info, texture.Get());
63 }
64 
AsDesktopFrame()65 const DesktopFrame& DxgiTexture::AsDesktopFrame() {
66   if (!frame_) {
67     frame_.reset(new DxgiDesktopFrame(*this));
68   }
69   return *frame_;
70 }
71 
Release()72 bool DxgiTexture::Release() {
73   frame_.reset();
74   return DoRelease();
75 }
76 
rect()77 DXGI_MAPPED_RECT* DxgiTexture::rect() {
78   return &rect_;
79 }
80 
81 }  // namespace webrtc
82