OLD | NEW |
(Empty) | |
| 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 #ifndef MODULES_DESKTOP_CAPTURE_WIN_DXGI_OUTPUT_DUPLICATOR_H_ |
| 12 #define MODULES_DESKTOP_CAPTURE_WIN_DXGI_OUTPUT_DUPLICATOR_H_ |
| 13 |
| 14 #include <comdef.h> |
| 15 #include <wrl/client.h> |
| 16 #include <DXGI.h> |
| 17 #include <DXGI1_2.h> |
| 18 |
| 19 #include <memory> |
| 20 #include <vector> |
| 21 |
| 22 #include "webrtc/base/criticalsection.h" |
| 23 #include "webrtc/base/thread_annotations.h" |
| 24 #include "webrtc/modules/desktop_capture/desktop_frame.h" |
| 25 #include "webrtc/modules/desktop_capture/desktop_geometry.h" |
| 26 #include "webrtc/modules/desktop_capture/desktop_region.h" |
| 27 #include "webrtc/modules/desktop_capture/win/d3d_device.h" |
| 28 #include "webrtc/modules/desktop_capture/win/dxgi_context.h" |
| 29 #include "webrtc/modules/desktop_capture/win/dxgi_texture.h" |
| 30 |
| 31 namespace webrtc { |
| 32 |
| 33 // Duplicates the content on one IDXGIOutput, i.e. one monitor attached to one |
| 34 // video card. None of functions in this class is thread-safe. |
| 35 // TODO(zijiehe): Understand the meaning of rotation. |
| 36 class DxgiOutputDuplicator { |
| 37 public: |
| 38 // Creates an instance of DxgiOutputDuplicator from a D3dDevice and one of its |
| 39 // IDXGIOutput1. Caller must maintain the lifetime of device, to make sure it |
| 40 // outlives this instance. Only DxgiAdapterDuplicator can create an instance. |
| 41 DxgiOutputDuplicator(const D3dDevice& device, |
| 42 const Microsoft::WRL::ComPtr<IDXGIOutput1>& output, |
| 43 const DXGI_OUTPUT_DESC& desc); |
| 44 |
| 45 // To allow this class to work with vector. |
| 46 DxgiOutputDuplicator(DxgiOutputDuplicator&& other); |
| 47 |
| 48 // Destructs this instance. We need to make sure texture_ has been released |
| 49 // before duplication_. |
| 50 ~DxgiOutputDuplicator(); |
| 51 |
| 52 // Initializes duplication_ object. |
| 53 bool Initialize(); |
| 54 |
| 55 // Copies the content of current IDXGIOutput to the |target|. To improve the |
| 56 // performance, this function copies only regions merged from |
| 57 // |last_frame|.updated_region and DetectUpdatedRegion. The offset decides the |
| 58 // offset in the |target| where the content should be copied to. i.e. this |
| 59 // function copies the content to the rectangle of (offset.x(), offset.y()) to |
| 60 // (offset.x() + desktop_rect_.width(), offset.y() + desktop_rect_.height()). |
| 61 // The |last_frame| is always expected to be translated by the same offset. |
| 62 // Returns false if this function failed to execute Windows APIs. |
| 63 bool Duplicate(DxgiContext* context, |
| 64 DesktopFrame* target, |
| 65 const DesktopFrame* last_frame, |
| 66 const DesktopVector offset); |
| 67 |
| 68 // Returns the desktop rect covered by this DxgiOutputDuplicator. |
| 69 DesktopRect desktop_rect() const { return desktop_rect_; } |
| 70 |
| 71 private: |
| 72 friend class DxgiAdapterDuplicator; |
| 73 |
| 74 // Detects updated region translated by offset from IDXGIOutput1. This |
| 75 // function will set the |updated_region| as entire DesktopRect starts from |
| 76 // offset if it failed to execute Windows APIs. |
| 77 void DetectUpdatedRegion(const DXGI_OUTDUPL_FRAME_INFO& frame_info, |
| 78 const DesktopVector offset, |
| 79 DesktopRegion* updated_region); |
| 80 |
| 81 // Returns untranslated updated region, which are directly returned by Windows |
| 82 // APIs. Returns false if this function failed to execute Windows APIs. |
| 83 bool DoDetectUpdatedRegion(const DXGI_OUTDUPL_FRAME_INFO& frame_info, |
| 84 DesktopRegion* updated_region); |
| 85 |
| 86 bool ReleaseFrame(); |
| 87 |
| 88 // Initializes duplication_ instance. Expects duplication_ is in empty status. |
| 89 // Returns false if system does not support IDXGIOutputDuplication. |
| 90 bool DuplicateOutput(); |
| 91 |
| 92 // Returns a DesktopRect with the same size of desktop_size_, but translated |
| 93 // by offset. |
| 94 DesktopRect TranslatedDesktopRect(const DesktopVector offset); |
| 95 |
| 96 void Setup(DxgiContext* context); |
| 97 |
| 98 void Unregister(const DxgiContext* const context); |
| 99 |
| 100 // Spreads changes from |context| to other registered DxgiContext(s) in |
| 101 // contexts_. |
| 102 void SpreadContextChange(const DxgiContext* const context); |
| 103 |
| 104 const D3dDevice& device_; |
| 105 const Microsoft::WRL::ComPtr<IDXGIOutput1> output_; |
| 106 const DesktopRect desktop_rect_; |
| 107 Microsoft::WRL::ComPtr<IDXGIOutputDuplication> duplication_; |
| 108 DXGI_OUTDUPL_DESC desc_; |
| 109 std::vector<uint8_t> metadata; |
| 110 std::unique_ptr<DxgiTexture> texture_; |
| 111 |
| 112 // After each AcquireNextFrame() function call, updated_region_(s) of all |
| 113 // active DxgiContext(s) need to be updated. Since they have missed the change |
| 114 // this time. And during next Duplicate() function call, their updated_region_ |
| 115 // will be merged and copied. |
| 116 std::vector<DxgiContext*> contexts_; |
| 117 }; |
| 118 |
| 119 } // namespace webrtc |
| 120 |
| 121 #endif // MODULES_DESKTOP_CAPTURE_WIN_DXGI_OUTPUT_DUPLICATOR_H_ |
OLD | NEW |