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