zijiehe | cf5753d | 2017-04-20 12:06:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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_frame.h" |
| 12 | |
zijiehe | 9d1ea5c | 2017-04-24 11:50:05 -0700 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
zijiehe | cf5753d | 2017-04-20 12:06:04 -0700 | [diff] [blame] | 15 | #include <utility> |
| 16 | |
zijiehe | cf5753d | 2017-04-20 12:06:04 -0700 | [diff] [blame] | 17 | #include "webrtc/modules/desktop_capture/desktop_frame.h" |
| 18 | #include "webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 19 | #include "webrtc/rtc_base/checks.h" |
| 20 | #include "webrtc/rtc_base/logging.h" |
zijiehe | cf5753d | 2017-04-20 12:06:04 -0700 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | DxgiFrame::DxgiFrame(SharedMemoryFactory* factory) |
| 25 | : factory_(factory) {} |
| 26 | |
| 27 | DxgiFrame::~DxgiFrame() = default; |
| 28 | |
| 29 | bool DxgiFrame::Prepare(DesktopSize size, DesktopCapturer::SourceId source_id) { |
| 30 | if (source_id != source_id_) { |
| 31 | // Once the source has been changed, the entire source should be copied. |
| 32 | source_id_ = source_id; |
| 33 | context_.Reset(); |
| 34 | } |
| 35 | |
Zijie He | 7e1c24c | 2017-07-27 18:06:12 -0700 | [diff] [blame] | 36 | if (resolution_tracker_.SetResolution(size)) { |
zijiehe | cf5753d | 2017-04-20 12:06:04 -0700 | [diff] [blame] | 37 | // Once the output size changed, recreate the SharedDesktopFrame. |
| 38 | frame_.reset(); |
zijiehe | cf5753d | 2017-04-20 12:06:04 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | if (!frame_) { |
| 42 | std::unique_ptr<DesktopFrame> frame; |
| 43 | if (factory_) { |
| 44 | frame = SharedMemoryDesktopFrame::Create(size, factory_); |
| 45 | } else { |
| 46 | frame.reset(new BasicDesktopFrame(size)); |
| 47 | } |
| 48 | if (!frame) { |
zijiehe | 3dd574a | 2017-06-27 22:04:21 -0700 | [diff] [blame] | 49 | LOG(LS_WARNING) << "DxgiFrame cannot create a new DesktopFrame."; |
zijiehe | cf5753d | 2017-04-20 12:06:04 -0700 | [diff] [blame] | 50 | return false; |
| 51 | } |
zijiehe | 9d1ea5c | 2017-04-24 11:50:05 -0700 | [diff] [blame] | 52 | // DirectX capturer won't paint each pixel in the frame due to its one |
| 53 | // capturer per monitor design. So once the new frame is created, we should |
| 54 | // clear it to avoid the legacy image to be remained on it. See |
| 55 | // http://crbug.com/708766. |
| 56 | RTC_DCHECK_EQ(frame->stride(), |
| 57 | frame->size().width() * DesktopFrame::kBytesPerPixel); |
| 58 | memset(frame->data(), 0, frame->stride() * frame->size().height()); |
zijiehe | cf5753d | 2017-04-20 12:06:04 -0700 | [diff] [blame] | 59 | |
| 60 | frame_ = SharedDesktopFrame::Wrap(std::move(frame)); |
| 61 | } |
| 62 | |
| 63 | return !!frame_; |
| 64 | } |
| 65 | |
| 66 | SharedDesktopFrame* DxgiFrame::frame() const { |
| 67 | RTC_DCHECK(frame_); |
| 68 | return frame_.get(); |
| 69 | } |
| 70 | |
| 71 | DxgiFrame::Context* DxgiFrame::context() { |
| 72 | RTC_DCHECK(frame_); |
| 73 | return &context_; |
| 74 | } |
| 75 | |
| 76 | } // namespace webrtc |