blob: 79a9016b0fe605dd0c846aef1dd1721904c3012b [file] [log] [blame]
zijiehe1a761be2017-04-20 19:06:041/*
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
zijiehe29860db2017-04-24 18:50:0513#include <string.h>
14
zijiehe1a761be2017-04-20 19:06:0415#include <utility>
16
Henrik Kjellandera67d9602017-07-01 14:42:2217#include "webrtc/base/checks.h"
18#include "webrtc/base/logging.h"
zijiehe1a761be2017-04-20 19:06:0419#include "webrtc/modules/desktop_capture/desktop_frame.h"
20#include "webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.h"
21
22namespace webrtc {
23
24DxgiFrame::DxgiFrame(SharedMemoryFactory* factory)
25 : factory_(factory) {}
26
27DxgiFrame::~DxgiFrame() = default;
28
29bool 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) {
zijiehe68337ca2017-06-28 05:04:2150 LOG(LS_WARNING) << "DxgiFrame cannot create a new DesktopFrame.";
zijiehe1a761be2017-04-20 19:06:0451 return false;
52 }
zijiehe29860db2017-04-24 18:50:0553 // 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());
zijiehe1a761be2017-04-20 19:06:0460
61 frame_ = SharedDesktopFrame::Wrap(std::move(frame));
62 }
63
64 return !!frame_;
65}
66
67SharedDesktopFrame* DxgiFrame::frame() const {
68 RTC_DCHECK(frame_);
69 return frame_.get();
70}
71
72DxgiFrame::Context* DxgiFrame::context() {
73 RTC_DCHECK(frame_);
74 return &context_;
75}
76
77} // namespace webrtc