blob: d0af1f9349240fadd10740392fded2b380c91143 [file] [log] [blame]
zijiehecf5753d2017-04-20 12:06:04 -07001/*
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
zijiehe9d1ea5c2017-04-24 11:50:05 -070013#include <string.h>
14
zijiehecf5753d2017-04-20 12:06:04 -070015#include <utility>
16
zijiehecf5753d2017-04-20 12:06:04 -070017#include "webrtc/modules/desktop_capture/desktop_frame.h"
18#include "webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020019#include "webrtc/rtc_base/checks.h"
20#include "webrtc/rtc_base/logging.h"
zijiehecf5753d2017-04-20 12:06:04 -070021
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
Zijie He7e1c24c2017-07-27 18:06:12 -070036 if (resolution_tracker_.SetResolution(size)) {
zijiehecf5753d2017-04-20 12:06:04 -070037 // Once the output size changed, recreate the SharedDesktopFrame.
38 frame_.reset();
zijiehecf5753d2017-04-20 12:06:04 -070039 }
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) {
zijiehe3dd574a2017-06-27 22:04:21 -070049 LOG(LS_WARNING) << "DxgiFrame cannot create a new DesktopFrame.";
zijiehecf5753d2017-04-20 12:06:04 -070050 return false;
51 }
zijiehe9d1ea5c2017-04-24 11:50:05 -070052 // 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());
zijiehecf5753d2017-04-20 12:06:04 -070059
60 frame_ = SharedDesktopFrame::Wrap(std::move(frame));
61 }
62
63 return !!frame_;
64}
65
66SharedDesktopFrame* DxgiFrame::frame() const {
67 RTC_DCHECK(frame_);
68 return frame_.get();
69}
70
71DxgiFrame::Context* DxgiFrame::context() {
72 RTC_DCHECK(frame_);
73 return &context_;
74}
75
76} // namespace webrtc