zijiehe | 2d618de | 2016-08-08 17:50:21 -0700 | [diff] [blame] | 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 "webrtc/modules/desktop_capture/win/dxgi_texture.h" |
| 12 | |
zijiehe | 5fea5fb | 2017-02-16 12:07:44 -0800 | [diff] [blame] | 13 | #include <comdef.h> |
| 14 | #include <wrl/client.h> |
| 15 | #include <D3D11.h> |
| 16 | |
zijiehe | 2d618de | 2016-08-08 17:50:21 -0700 | [diff] [blame] | 17 | #include "webrtc/modules/desktop_capture/desktop_region.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 18 | #include "webrtc/rtc_base/checks.h" |
| 19 | #include "webrtc/rtc_base/logging.h" |
zijiehe | 5fea5fb | 2017-02-16 12:07:44 -0800 | [diff] [blame] | 20 | |
| 21 | using Microsoft::WRL::ComPtr; |
zijiehe | 2d618de | 2016-08-08 17:50:21 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | class DxgiDesktopFrame : public DesktopFrame { |
| 28 | public: |
| 29 | explicit DxgiDesktopFrame(const DxgiTexture& texture) |
zijiehe | e61fbff | 2016-11-29 16:09:51 -0800 | [diff] [blame] | 30 | : DesktopFrame(texture.desktop_size(), |
zijiehe | 2d618de | 2016-08-08 17:50:21 -0700 | [diff] [blame] | 31 | texture.pitch(), |
| 32 | texture.bits(), |
| 33 | nullptr) {} |
| 34 | |
sergeyu | e183121 | 2016-10-26 13:15:42 -0700 | [diff] [blame] | 35 | ~DxgiDesktopFrame() override = default; |
zijiehe | 2d618de | 2016-08-08 17:50:21 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | } // namespace |
| 39 | |
zijiehe | 5fea5fb | 2017-02-16 12:07:44 -0800 | [diff] [blame] | 40 | DxgiTexture::DxgiTexture() = default; |
| 41 | DxgiTexture::~DxgiTexture() = default; |
zijiehe | 2d618de | 2016-08-08 17:50:21 -0700 | [diff] [blame] | 42 | |
zijiehe | 5fea5fb | 2017-02-16 12:07:44 -0800 | [diff] [blame] | 43 | bool DxgiTexture::CopyFrom(const DXGI_OUTDUPL_FRAME_INFO& frame_info, |
| 44 | IDXGIResource* resource) { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 45 | RTC_DCHECK_GT(frame_info.AccumulatedFrames, 0); |
| 46 | RTC_DCHECK(resource); |
zijiehe | 5fea5fb | 2017-02-16 12:07:44 -0800 | [diff] [blame] | 47 | ComPtr<ID3D11Texture2D> texture; |
| 48 | _com_error error = resource->QueryInterface( |
| 49 | __uuidof(ID3D11Texture2D), |
| 50 | reinterpret_cast<void**>(texture.GetAddressOf())); |
| 51 | if (error.Error() != S_OK || !texture) { |
| 52 | LOG(LS_ERROR) << "Failed to convert IDXGIResource to ID3D11Texture2D, " |
| 53 | "error " |
| 54 | << error.ErrorMessage() << ", code " << error.Error(); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | D3D11_TEXTURE2D_DESC desc = {0}; |
| 59 | texture->GetDesc(&desc); |
| 60 | desktop_size_.set(desc.Width, desc.Height); |
zijiehe | 5fea5fb | 2017-02-16 12:07:44 -0800 | [diff] [blame] | 61 | |
| 62 | return CopyFromTexture(frame_info, texture.Get()); |
| 63 | } |
sergeyu | e183121 | 2016-10-26 13:15:42 -0700 | [diff] [blame] | 64 | |
zijiehe | 2d618de | 2016-08-08 17:50:21 -0700 | [diff] [blame] | 65 | const DesktopFrame& DxgiTexture::AsDesktopFrame() { |
| 66 | if (!frame_) { |
| 67 | frame_.reset(new DxgiDesktopFrame(*this)); |
| 68 | } |
| 69 | return *frame_; |
| 70 | } |
| 71 | |
| 72 | bool DxgiTexture::Release() { |
| 73 | frame_.reset(); |
| 74 | return DoRelease(); |
| 75 | } |
| 76 | |
zijiehe | 5fea5fb | 2017-02-16 12:07:44 -0800 | [diff] [blame] | 77 | DXGI_MAPPED_RECT* DxgiTexture::rect() { |
| 78 | return &rect_; |
| 79 | } |
| 80 | |
zijiehe | 2d618de | 2016-08-08 17:50:21 -0700 | [diff] [blame] | 81 | } // namespace webrtc |