blob: 13d66c53d7a79b05c7df13135968ce12ad368282 [file] [log] [blame]
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +00001/*
2 * Copyright (c) 2013 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/shared_desktop_frame.h"
12
kwiberg2bb3afa2016-03-16 15:58:08 -070013#include <memory>
14
Henrik Kjellander98f53512015-10-28 18:17:40 +010015#include "webrtc/system_wrappers/include/atomic32.h"
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000016
17namespace webrtc {
18
19class SharedDesktopFrame::Core {
20 public:
21 Core(DesktopFrame* frame) : frame_(frame) {}
22
23 DesktopFrame* frame() { return frame_.get(); }
24
25 bool HasOneRef() { return ref_count_.Value() == 1; }
26
27 virtual int32_t AddRef() {
28 return ++ref_count_;
29 }
30
31 virtual int32_t Release() {
32 int32_t ref_count;
33 ref_count = --ref_count_;
34 if (ref_count == 0)
35 delete this;
36 return ref_count;
37 }
38
39 private:
40 virtual ~Core() {}
41
42 Atomic32 ref_count_;
kwiberg2bb3afa2016-03-16 15:58:08 -070043 std::unique_ptr<DesktopFrame> frame_;
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000044
henrikg3c089d72015-09-16 05:37:44 -070045 RTC_DISALLOW_COPY_AND_ASSIGN(Core);
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000046};
47
48SharedDesktopFrame::~SharedDesktopFrame() {}
49
50// static
zijiehe809dcb42016-04-22 16:08:39 -070051SharedDesktopFrame* SharedDesktopFrame::Wrap(DesktopFrame* desktop_frame) {
Peter Boström26b08602015-06-04 15:18:17 +020052 rtc::scoped_refptr<Core> core(new Core(desktop_frame));
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000053 return new SharedDesktopFrame(core);
54}
55
56DesktopFrame* SharedDesktopFrame::GetUnderlyingFrame() {
57 return core_->frame();
58}
59
60SharedDesktopFrame* SharedDesktopFrame::Share() {
61 SharedDesktopFrame* result = new SharedDesktopFrame(core_);
62 result->set_dpi(dpi());
63 result->set_capture_time_ms(capture_time_ms());
64 *result->mutable_updated_region() = updated_region();
65 return result;
66}
67
68bool SharedDesktopFrame::IsShared() {
69 return !core_->HasOneRef();
70}
71
Peter Boström26b08602015-06-04 15:18:17 +020072SharedDesktopFrame::SharedDesktopFrame(rtc::scoped_refptr<Core> core)
73 : DesktopFrame(core->frame()->size(),
74 core->frame()->stride(),
75 core->frame()->data(),
76 core->frame()->shared_memory()),
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000077 core_(core) {
78}
79
80} // namespace webrtc