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_mapping.h" |
| 12 | |
| 13 | #include <comdef.h> |
| 14 | #include <DXGI.h> |
| 15 | #include <DXGI1_2.h> |
| 16 | |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 17 | #include "webrtc/rtc_base/checks.h" |
| 18 | #include "webrtc/rtc_base/logging.h" |
zijiehe | 2d618de | 2016-08-08 17:50:21 -0700 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
zijiehe | 5fea5fb | 2017-02-16 12:07:44 -0800 | [diff] [blame] | 22 | DxgiTextureMapping::DxgiTextureMapping(IDXGIOutputDuplication* duplication) |
| 23 | : duplication_(duplication) { |
zijiehe | 2d618de | 2016-08-08 17:50:21 -0700 | [diff] [blame] | 24 | RTC_DCHECK(duplication_); |
| 25 | } |
| 26 | |
| 27 | DxgiTextureMapping::~DxgiTextureMapping() = default; |
| 28 | |
zijiehe | 5fea5fb | 2017-02-16 12:07:44 -0800 | [diff] [blame] | 29 | bool DxgiTextureMapping::CopyFromTexture( |
| 30 | const DXGI_OUTDUPL_FRAME_INFO& frame_info, |
| 31 | ID3D11Texture2D* texture) { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 32 | RTC_DCHECK_GT(frame_info.AccumulatedFrames, 0); |
| 33 | RTC_DCHECK(texture); |
zijiehe | 5fea5fb | 2017-02-16 12:07:44 -0800 | [diff] [blame] | 34 | *rect() = {0}; |
| 35 | _com_error error = duplication_->MapDesktopSurface(rect()); |
zijiehe | 2d618de | 2016-08-08 17:50:21 -0700 | [diff] [blame] | 36 | if (error.Error() != S_OK) { |
zijiehe | 5fea5fb | 2017-02-16 12:07:44 -0800 | [diff] [blame] | 37 | *rect() = {0}; |
zijiehe | 2d618de | 2016-08-08 17:50:21 -0700 | [diff] [blame] | 38 | LOG(LS_ERROR) << "Failed to map the IDXGIOutputDuplication to a bitmap, " |
| 39 | "error " |
| 40 | << error.ErrorMessage() << ", code " << error.Error(); |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | bool DxgiTextureMapping::DoRelease() { |
| 48 | _com_error error = duplication_->UnMapDesktopSurface(); |
| 49 | if (error.Error() != S_OK) { |
| 50 | LOG(LS_ERROR) << "Failed to unmap the IDXGIOutputDuplication, error " |
| 51 | << error.ErrorMessage() << ", code " << error.Error(); |
| 52 | return false; |
| 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | } // namespace webrtc |