zijiehe | 1a761be | 2017-04-20 19:06:04 | [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 | 29860db | 2017-04-24 18:50:05 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
zijiehe | 1a761be | 2017-04-20 19:06:04 | [diff] [blame] | 15 | #include <utility> |
| 16 | |
Henrik Kjellander | a67d960 | 2017-07-01 14:42:22 | [diff] [blame^] | 17 | #include "webrtc/base/checks.h" |
| 18 | #include "webrtc/base/logging.h" |
zijiehe | 1a761be | 2017-04-20 19:06:04 | [diff] [blame] | 19 | #include "webrtc/modules/desktop_capture/desktop_frame.h" |
| 20 | #include "webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.h" |
| 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 | |
| 36 | if (resolution_change_detector_.IsChanged(size)) { |
| 37 | // Once the output size changed, recreate the SharedDesktopFrame. |
| 38 | frame_.reset(); |
| 39 | resolution_change_detector_.Reset(); |
| 40 | } |
| 41 | |
| 42 | if (!frame_) { |
| 43 | std::unique_ptr<DesktopFrame> frame; |
| 44 | if (factory_) { |
| 45 | frame = SharedMemoryDesktopFrame::Create(size, factory_); |
| 46 | } else { |
| 47 | frame.reset(new BasicDesktopFrame(size)); |
| 48 | } |
| 49 | if (!frame) { |
zijiehe | 68337ca | 2017-06-28 05:04:21 | [diff] [blame] | 50 | LOG(LS_WARNING) << "DxgiFrame cannot create a new DesktopFrame."; |
zijiehe | 1a761be | 2017-04-20 19:06:04 | [diff] [blame] | 51 | return false; |
| 52 | } |
zijiehe | 29860db | 2017-04-24 18:50:05 | [diff] [blame] | 53 | // DirectX capturer won't paint each pixel in the frame due to its one |
| 54 | // capturer per monitor design. So once the new frame is created, we should |
| 55 | // clear it to avoid the legacy image to be remained on it. See |
| 56 | // http://crbug.com/708766. |
| 57 | RTC_DCHECK_EQ(frame->stride(), |
| 58 | frame->size().width() * DesktopFrame::kBytesPerPixel); |
| 59 | memset(frame->data(), 0, frame->stride() * frame->size().height()); |
zijiehe | 1a761be | 2017-04-20 19:06:04 | [diff] [blame] | 60 | |
| 61 | frame_ = SharedDesktopFrame::Wrap(std::move(frame)); |
| 62 | } |
| 63 | |
| 64 | return !!frame_; |
| 65 | } |
| 66 | |
| 67 | SharedDesktopFrame* DxgiFrame::frame() const { |
| 68 | RTC_DCHECK(frame_); |
| 69 | return frame_.get(); |
| 70 | } |
| 71 | |
| 72 | DxgiFrame::Context* DxgiFrame::context() { |
| 73 | RTC_DCHECK(frame_); |
| 74 | return &context_; |
| 75 | } |
| 76 | |
| 77 | } // namespace webrtc |