Index: webrtc/modules/desktop_capture/win/screen_capturer_win_directx.cc |
diff --git a/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.cc b/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8fe4a57ad4c56bd44d838f112413923dcaae8bd2 |
--- /dev/null |
+++ b/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.cc |
@@ -0,0 +1,485 @@ |
+/* |
+ * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include "webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h" |
+ |
+#include <assert.h> |
+#include <string.h> |
+ |
+#include <comdef.h> |
+#include <wincodec.h> |
+#include <DXGI.h> |
+ |
+#include "webrtc/modules/desktop_capture/desktop_frame_win.h" |
+#include "webrtc/modules/desktop_capture/win/screen_capture_utils.h" |
+#include "webrtc/system_wrappers/include/logging.h" |
+#include "webrtc/system_wrappers/include/tick_util.h" |
+ |
+namespace webrtc { |
+ |
+using Microsoft::WRL::ComPtr; |
+using rtc::scoped_ptr; |
Sergey Ulanov
2016/03/31 18:41:16
scoped_ptr<> is used in only one place, so you can
Hzj_jie
2016/04/05 23:15:17
Done.
|
+using std::unique_ptr; |
Sergey Ulanov
2016/03/31 18:41:17
unique_ptr is not used anywhere in this file.
Hzj_jie
2016/04/05 23:15:17
Done.
|
+ |
+bool ScreenCapturerWinDirectX::kInitialized { false }; |
Sergey Ulanov
2016/03/31 18:41:16
All of these are not really constants, so you shou
Sergey Ulanov
2016/03/31 18:41:16
= false.
C++11 style initialization syntax is not
Hzj_jie
2016/04/05 23:15:17
Done.
Hzj_jie
2016/04/05 23:15:18
Yes, I have also had the same feeling, but logical
|
+bool ScreenCapturerWinDirectX::kInitializeResult { false }; |
+ID3D11Device* ScreenCapturerWinDirectX::kD3D11Device { nullptr }; |
+ID3D11DeviceContext* ScreenCapturerWinDirectX::kD3D11Context { nullptr }; |
+IDXGIOutput1* ScreenCapturerWinDirectX::kDXGIOutput1 { nullptr }; |
+ComPtr<IDXGIOutputDuplication> |
Sergey Ulanov
2016/03/31 18:41:16
Static variables are allowed only for POD types. S
Hzj_jie
2016/04/05 23:15:18
According to MSDN http://shortn/_geEQFIizM3, one a
Sergey Ulanov
2016/04/08 21:22:26
All global variables _must_ be POD. It doesn't mat
|
+ ScreenCapturerWinDirectX::kDXGIOutputDuplication {}; |
+DesktopSize ScreenCapturerWinDirectX::kDesktopSize {}; |
+ComPtr<ID3D11Texture2D> ScreenCapturerWinDirectX::kStage {}; |
+ComPtr<IDXGISurface> ScreenCapturerWinDirectX::kSurface {}; |
+std::vector<BYTE> ScreenCapturerWinDirectX::kMetaDataBuffer {}; |
+CriticalSectionWrapper ScreenCapturerWinDirectX::kInitializeLock {}; |
+CriticalSectionWrapper ScreenCapturerWinDirectX::kDuplicationLock {}; |
+CriticalSectionWrapper ScreenCapturerWinDirectX::kAcquireLock {}; |
+ |
+bool ScreenCapturerWinDirectX::Initialize() { |
+ if (!kInitialized) { |
+ CriticalSectionScoped lock(&kInitializeLock); |
+ if (!kInitialized) { |
+ kInitializeResult = DoInitialize(); |
+ kInitialized = true; |
+ if (kInitializeResult) { |
+ return true; |
+ } |
+ |
+ // Clean up if DirectX cannot work on the system. |
+ if (kDXGIOutputDuplication) { |
+ kDXGIOutputDuplication.Reset(); |
+ } |
+ |
+ if (kDXGIOutput1 != nullptr) { |
+ kDXGIOutput1->Release(); |
Sergey Ulanov
2016/03/31 18:41:15
Why do you need this? Doesn't ComPtr<> release the
Hzj_jie
2016/04/05 23:15:18
Yes, ComPtr does, but this instance is a pure poin
|
+ kDXGIOutput1 = nullptr; |
+ } |
+ |
+ if (kD3D11Context != nullptr) { |
+ kD3D11Context->Release(); |
+ kD3D11Context = nullptr; |
+ } |
+ |
+ if (kD3D11Device != nullptr) { |
+ kD3D11Device->Release(); |
+ kD3D11Device = nullptr; |
+ } |
+ |
+ return false; |
+ } |
+ } |
+ |
+ return kInitializeResult; |
+} |
+ |
+bool ScreenCapturerWinDirectX::DoInitialize() { |
+ D3D_FEATURE_LEVEL feature_level; |
+ _com_error err(D3D11CreateDevice(nullptr, |
+ D3D_DRIVER_TYPE_HARDWARE, |
+ nullptr, |
+ D3D11_CREATE_DEVICE_SINGLETHREADED, |
Sergey Ulanov
2016/03/31 18:41:17
DirectX may be used from other threads in chrome,
Hzj_jie
2016/04/05 23:15:17
We always have only one thread to access an ID3D11
|
+ nullptr, |
+ 0, |
+ D3D11_SDK_VERSION, |
+ &kD3D11Device, |
+ &feature_level, |
+ &kD3D11Context)); |
+ if (err.Error() != S_OK || |
+ kD3D11Device == nullptr || |
+ kD3D11Context == nullptr) { |
+ LOG(LS_WARNING) << "D3D11CreateDeivce returns error " << err.ErrorMessage() |
+ << " with code " << err.Error(); |
+ return false; |
+ } |
+ |
+ if (feature_level < D3D_FEATURE_LEVEL_11_0) { |
+ LOG(LS_WARNING) << "D3D11CreateDevice returns an instance without DirectX " |
+ "11 support, level " << feature_level; |
+ return false; |
+ } |
+ |
+ ComPtr<IDXGIDevice> device; |
+ err = _com_error(kD3D11Device->QueryInterface( |
+ __uuidof(IDXGIDevice), |
+ reinterpret_cast<void**>(device.GetAddressOf()))); |
+ if (err.Error() != S_OK || !device) { |
+ LOG(LS_WARNING) << "ID3D11Device is not an implementation of IDXGIDevice, " |
+ "this usually means the system does not support DirectX " |
+ "11"; |
+ return false; |
+ } |
+ |
+ ComPtr<IDXGIAdapter> adapter; |
+ err = _com_error(device->GetAdapter(adapter.GetAddressOf())); |
+ if (err.Error() != S_OK || !adapter) { |
+ LOG(LS_WARNING) << "Failed to get an IDXGIAdapter implementation from " |
+ "IDXGIDevice."; |
+ return false; |
+ } |
+ |
+ ComPtr<IDXGIOutput> output; |
+ for (int i = 0;; i++) { |
Sergey Ulanov
2016/03/31 18:41:16
while() loop would be more readable here. E.g. see
Hzj_jie
2016/04/05 23:15:17
The scenario is a little bit different, we are loo
|
+ err = _com_error(adapter->EnumOutputs(i, output.GetAddressOf())); |
+ if (err.Error() == DXGI_ERROR_NOT_FOUND) { |
+ LOG(LS_WARNING) << "No output detected."; |
+ return false; |
+ } else if (err.Error() == S_OK && output) { |
Sergey Ulanov
2016/03/31 18:41:15
what if err.Error() is any error other than DXGI_
Sergey Ulanov
2016/03/31 18:41:16
no else after return please: https://www.chromium.
Hzj_jie
2016/04/05 23:15:18
I do not see a statement in MSDN to say this funct
|
+ DXGI_OUTPUT_DESC desc; |
+ err = _com_error(output->GetDesc(&desc)); |
+ if (err.Error() == S_OK) { |
+ if (desc.AttachedToDesktop) { |
+ // Current output instance is the device attached to desktop. |
+ break; |
+ } |
+ } else { |
+ LOG(LS_WARNING) << "Failed to get output description of device " << i |
+ << ", ignore."; |
+ } |
+ } |
+ } |
+ |
+ assert(output); |
+ err = _com_error(output.CopyTo(__uuidof(IDXGIOutput1), |
+ reinterpret_cast<void**>(&kDXGIOutput1))); |
+ if (err.Error() != S_OK || kDXGIOutput1 == nullptr) { |
+ LOG(LS_WARNING) << "Failed to convert IDXGIOutput to IDXGIOutput1, this " |
+ "usually means the system does not support DirectX 11"; |
+ return false; |
+ } |
+ |
+ return DuplicateOutput(); |
+} |
+ |
+bool ScreenCapturerWinDirectX::DuplicateOutput() { |
+ assert(kDXGIOutput1 != nullptr); |
+ // We are updating the instance. |
+ CriticalSectionScoped lock(&kDuplicationLock); |
+ // Make sure nobody is using current instance. |
+ CriticalSectionScoped lock2(&kAcquireLock); |
+ if (kDXGIOutputDuplication) { |
+ kDXGIOutputDuplication.Reset(); |
+ } |
+ _com_error err(kDXGIOutput1->DuplicateOutput( |
+ static_cast<IUnknown*>(kD3D11Device), |
+ kDXGIOutputDuplication.GetAddressOf())); |
+ if (err.Error() != S_OK || !kDXGIOutputDuplication) { |
+ LOG(LS_WARNING) << "Failed to duplicate output from IDXGIOutput1, error " |
+ << err.ErrorMessage() << ", with code " << err.Error(); |
+ return false; |
+ } |
+ |
+ DXGI_OUTDUPL_DESC desc; |
+ kDXGIOutputDuplication->GetDesc(&desc); |
+ kDesktopSize.set(desc.ModeDesc.Width, desc.ModeDesc.Height); |
+ kStage.Reset(); |
+ kSurface.Reset(); |
+ return true; |
+} |
+ |
+ScreenCapturerWinDirectX::ScreenCapturerWinDirectX( |
+ const DesktopCaptureOptions& options) : |
+ callback_(nullptr), |
+ set_thread_execution_state_failed_(false) { |
+ assert(kInitialized && kInitializeResult); |
+} |
+ |
+ScreenCapturerWinDirectX::~ScreenCapturerWinDirectX() {} |
+ |
+void ScreenCapturerWinDirectX::Start(Callback* callback) { |
+ assert(callback_ == nullptr); |
Sergey Ulanov
2016/03/31 18:41:16
Here and everywhere else please use RTC_DCHECK() i
Hzj_jie
2016/04/05 23:15:18
Done.
|
+ assert(callback != nullptr); |
+ |
+ callback_ = callback; |
+} |
+ |
+// We do not need to allocate memory in this class. |
+void ScreenCapturerWinDirectX::SetSharedMemoryFactory( |
+ rtc::scoped_ptr<SharedMemoryFactory> shared_memory_factory) {} |
Sergey Ulanov
2016/03/31 18:41:16
We actually don't want to ignore this call. On win
Hzj_jie
2016/04/05 23:15:18
Yes, done.
|
+ |
+bool ScreenCapturerWinDirectX::CreateTexture(ID3D11Texture2D* texture) { |
+ assert(texture != nullptr); |
+ D3D11_TEXTURE2D_DESC desc; |
+ texture->GetDesc(&desc); |
+ desc.Usage = D3D11_USAGE_STAGING; |
+ desc.BindFlags = 0; |
+ desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; |
+ desc.MiscFlags = 0; |
+ if (kStage) { |
+ { |
+ ComPtr<IUnknown> left; |
+ ComPtr<IUnknown> right; |
+ assert(SUCCEEDED(kStage.CopyTo( |
Sergey Ulanov
2016/03/31 18:41:16
Don't put any statements with side-effects inside
Hzj_jie
2016/04/05 23:15:18
Done.
|
+ __uuidof(IUnknown), |
+ reinterpret_cast<void**>(left.GetAddressOf())))); |
+ assert(SUCCEEDED(kSurface.CopyTo( |
+ __uuidof(IUnknown), |
+ reinterpret_cast<void**>(right.GetAddressOf())))); |
+ assert(left.Get() == right.Get()); |
+ } |
+ _com_error err(kSurface->Unmap()); // This buffer should be used already. |
+ if (err.Error() == S_OK) { |
+ D3D11_TEXTURE2D_DESC orgi_desc; |
+ kStage->GetDesc(&orgi_desc); |
+ if (memcmp(&desc, &orgi_desc, sizeof(D3D11_TEXTURE2D_DESC)) == 0) { |
Sergey Ulanov
2016/03/31 18:41:16
You don't need this check. memcmp() is not expecte
Hzj_jie
2016/04/05 23:15:18
This logic is to check whether current buffer (sta
|
+ return true; |
+ } |
+ } else { |
+ // Let's recreate kSurface later. |
+ LOG(LS_ERROR) << "Failed to unmap surface, error " << err.ErrorMessage() |
+ << ", code " << err.Error(); |
+ } |
+ kStage.Reset(); |
+ kSurface.Reset(); |
+ } |
+ |
+ _com_error err = _com_error(kD3D11Device->CreateTexture2D( |
+ &desc, |
+ nullptr, |
+ kStage.GetAddressOf())); |
+ if (err.Error() != S_OK || !kStage) { |
+ LOG(LS_ERROR) << "Failed to create a new ID3D11Texture2D as stage, " |
+ "error " << err.ErrorMessage() |
+ << ", code " << err.Error(); |
+ return false; |
+ } |
+ |
+ err = _com_error(kStage.CopyTo( |
+ __uuidof(IDXGISurface), |
+ reinterpret_cast<void**>(kSurface.GetAddressOf()))); |
+ if (err.Error() != S_OK || !kSurface) { |
+ LOG(LS_ERROR) << "Failed to convert ID3D11Texture2D to IDXGISurface, " |
+ "error " << err.ErrorMessage() |
+ << ", code " << err.Error(); |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+bool ScreenCapturerWinDirectX::DetectUpdatedRegion( |
+ const DXGI_OUTDUPL_FRAME_INFO& frame_info, |
Sergey Ulanov
2016/03/31 18:41:16
incorrect indentation. Please use clang-format: ht
Hzj_jie
2016/04/05 23:15:17
Done.
|
+ DesktopFrame* frame) { |
+ assert(kDXGIOutputDuplication); |
+ assert(frame != nullptr); |
+ DesktopRegion& updated_region = *frame->mutable_updated_region(); |
+ updated_region.Clear(); |
+ if (frame_info.TotalMetadataBufferSize == 0) { |
+ // This should not happen, since frame_info.AccumulatedFrames > 0. |
+ LOG(LS_ERROR) << "frame_info.AccumulatedFrames > 0, " |
+ "but TotalMetadataBufferSize == 0"; |
+ return false; |
+ } |
+ |
+ if (kMetaDataBuffer.size() < frame_info.TotalMetadataBufferSize) { |
+ kMetaDataBuffer.clear(); // Avoid data copy |
+ kMetaDataBuffer.reserve(frame_info.TotalMetadataBufferSize); |
+ } |
+ |
+ UINT buff_size = 0; |
+ DXGI_OUTDUPL_MOVE_RECT* move_rects = nullptr; |
+ size_t move_rects_count = 0; |
+ RECT* dirty_rects = nullptr; |
+ size_t dirty_rects_count = 0; |
+ for (int i = 0; i < 2; i++) { |
Sergey Ulanov
2016/03/31 18:41:16
This looks strange. You have a loop that iterates
Hzj_jie
2016/04/05 23:15:17
To share most of the logic below. I agree it looks
|
+ _com_error err(S_OK); |
+ if (i == 0) { |
+ move_rects = |
+ reinterpret_cast<DXGI_OUTDUPL_MOVE_RECT*>(kMetaDataBuffer.data()); |
+ err = _com_error(kDXGIOutputDuplication->GetFrameMoveRects( |
+ kMetaDataBuffer.capacity(), |
+ move_rects, |
+ &buff_size)); |
+ } else { |
+ dirty_rects = |
+ reinterpret_cast<RECT*>(kMetaDataBuffer.data() + buff_size); |
+ err = _com_error(kDXGIOutputDuplication->GetFrameDirtyRects( |
+ kMetaDataBuffer.capacity() - buff_size, |
+ dirty_rects, |
+ &buff_size)); |
+ } |
+ if (err.Error() != S_OK) { |
+ if (err.Error() == DXGI_ERROR_ACCESS_LOST) { |
+ if (!DuplicateOutput()) { |
+ LOG(LS_ERROR) << "Failed to regenerate an IDXGIOutputDuplication."; |
+ } |
+ } else { |
+ LOG(LS_ERROR) << "Failed to get " << (i == 0 ? "move" : "dirty") |
+ << " rectangles, error " << err.ErrorMessage() |
+ << ", code " << err.Error(); |
+ } |
+ // Send whole desktop as we cannot get dirty or move rectangles. |
+ return false; |
+ } |
+ if (i == 0) { |
+ move_rects_count = buff_size / sizeof(DXGI_OUTDUPL_MOVE_RECT); |
+ } else { |
+ dirty_rects_count = buff_size / sizeof(RECT); |
+ } |
+ } |
+ |
+ while (move_rects_count > 0) { |
+ updated_region.AddRect(DesktopRect::MakeXYWH( |
+ move_rects->SourcePoint.x, |
+ move_rects->SourcePoint.y, |
+ move_rects->DestinationRect.right - move_rects->DestinationRect.left, |
+ move_rects->DestinationRect.bottom - move_rects->DestinationRect.top)); |
+ updated_region.AddRect(DesktopRect::MakeLTRB( |
+ move_rects->DestinationRect.left, |
+ move_rects->DestinationRect.top, |
+ move_rects->DestinationRect.right, |
+ move_rects->DestinationRect.bottom)); |
+ move_rects++; |
+ move_rects_count--; |
+ } |
+ |
+ while (dirty_rects_count > 0) { |
+ updated_region.AddRect(DesktopRect::MakeLTRB( |
+ dirty_rects->left, |
+ dirty_rects->top, |
+ dirty_rects->right, |
+ dirty_rects->bottom)); |
+ dirty_rects++; |
+ dirty_rects_count--; |
+ } |
+ |
+ return true; |
+} |
+ |
+bool ScreenCapturerWinDirectX::ProcessFrame( |
+ const DXGI_OUTDUPL_FRAME_INFO& frame_info, |
+ IDXGIResource* resource, |
+ DesktopFrame** frame) { |
+ assert(resource != nullptr); |
+ assert(frame != nullptr); |
+ assert(frame_info.AccumulatedFrames > 0); |
+ |
+ ComPtr<ID3D11Texture2D> texture; |
+ _com_error err = _com_error(resource->QueryInterface( |
+ __uuidof(ID3D11Texture2D), |
+ reinterpret_cast<void**>(texture.GetAddressOf()))); |
+ if (err.Error() != S_OK || !texture) { |
+ LOG(LS_ERROR) << "Failed to convert IDXGIResource to ID3D11Texture2D, " |
+ "error " << err.ErrorMessage() << ", code " |
+ << err.Error(); |
+ return false; |
+ } |
+ |
+ // AcquireNextFrame returns a CPU inaccessible IDXGIResource, so we need to |
+ // make a copy. |
+ if (!CreateTexture(texture.Get())) { |
+ return false; |
+ } |
+ |
+ kD3D11Context->CopyResource(static_cast<ID3D11Resource*>(kStage.Get()), |
+ static_cast<ID3D11Resource*>(texture.Get())); |
+ |
+ DXGI_MAPPED_RECT rect; |
+ err = _com_error(kSurface->Map(&rect, DXGI_MAP_READ)); |
+ if (err.Error() != S_OK) { |
+ LOG(LS_ERROR) << "Failed to map the IDXGISurface to a bitmap, error " |
+ << err.ErrorMessage() << ", code " << err.Error(); |
+ return false; |
+ } |
+ |
+ *frame = new DesktopFrameWinDXGI(kDesktopSize, kSurface, rect); |
+ // kSurface->Unmap will be called next time we capture an image to avoid |
+ // memory copy. |
+ if (!DetectUpdatedRegion(frame_info, *frame)) { |
+ (*frame)->mutable_updated_region()->Clear(); |
+ (*frame)->mutable_updated_region()->AddRect( |
+ DesktopRect::MakeSize(kDesktopSize)); |
+ } |
+ return true; |
+} |
+ |
+void ScreenCapturerWinDirectX::Capture(const DesktopRegion& region) { |
+ if (!kDXGIOutputDuplication) { |
+ // Receive a capture request when application is shutting down. |
+ CallbackError(); |
+ return; |
+ } |
+ |
+ assert(callback_ != nullptr); |
+ TickTime capture_start_time = TickTime::Now(); |
+ |
+ if (!SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED)) { |
+ if (!set_thread_execution_state_failed_) { |
+ set_thread_execution_state_failed_ = true; |
+ LOG(LS_WARNING) << "Failed to make system & display power assertion: " |
+ << GetLastError(); |
+ } |
+ } |
+ |
+ DXGI_OUTDUPL_FRAME_INFO frame_info = { 0 }; |
+ ComPtr<IDXGIResource> resource = nullptr; |
+ CriticalSectionScoped lock(&kAcquireLock); |
+ _com_error err(kDXGIOutputDuplication->AcquireNextFrame( |
+ kAcquireTimeout, |
+ &frame_info, |
+ resource.GetAddressOf())); |
+ if (err.Error() == DXGI_ERROR_ACCESS_LOST) { |
+ if (DuplicateOutput()) { |
+ CallbackUnchanged(); |
+ } else { |
+ LOG(LS_ERROR) << "Failed to regenerate an IDXGIOutputDuplication"; |
+ CallbackError(); |
+ } |
+ return; |
+ } else if (err.Error() == DXGI_ERROR_WAIT_TIMEOUT) { |
Sergey Ulanov
2016/03/31 18:41:17
here and below: no else after return please
Hzj_jie
2016/04/05 23:15:18
Done.
|
+ // Nothing changed. |
+ CallbackUnchanged(); |
+ return; |
+ } else if (err.Error() != S_OK) { |
+ CallbackError(); |
+ return; |
+ } else { |
+ if (frame_info.AccumulatedFrames > 0) { |
+ // Everything looks good so far, build CaptureFrame. |
+ DesktopFrame* frame = nullptr; |
+ bool result = ProcessFrame(frame_info, resource.Get(), &frame); |
+ kDXGIOutputDuplication->ReleaseFrame(); |
+ if (result) { |
+ assert(frame != nullptr); |
+ frame->set_capture_time_ms( |
+ (TickTime::Now() - capture_start_time).Milliseconds()); |
+ callback_->OnCaptureCompleted(frame); |
+ } else { |
+ assert(frame == nullptr); |
+ CallbackError(); |
+ } |
+ } else { |
+ // Only mouse cursor moved, ignore. |
+ CallbackUnchanged(); |
+ kDXGIOutputDuplication->ReleaseFrame(); |
+ } |
+ } |
+} |
+ |
+bool ScreenCapturerWinDirectX::GetScreenList(ScreenList* screens) { |
+ assert(screens != nullptr); |
+ assert(screens->size() == 0); |
+ screens->push_back(Screen { 0 }); |
+ return true; |
+} |
+ |
+bool ScreenCapturerWinDirectX::SelectScreen(ScreenId id) { |
+ return id == 0 || id == kFullDesktopScreenId; |
+} |
+ |
+void ScreenCapturerWinDirectX::CallbackUnchanged() { |
+ callback_->OnCaptureCompleted(new DesktopFrameWinDXGI(kDesktopSize)); |
Sergey Ulanov
2016/03/31 18:41:16
When nothing is changed we want to emit a frame th
Hzj_jie
2016/04/05 23:15:18
Done.
|
+} |
+ |
+void ScreenCapturerWinDirectX::CallbackError() { |
+ callback_->OnCaptureCompleted(nullptr); |
+} |
+ |
+} // namespace webrtc |