blob: 8d10827e29cc2b78d472e8c28beaeb90064f1b81 [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
kwiberg4485ffb2016-04-26 08:14:39 -070015#include "webrtc/base/constructormagic.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010016#include "webrtc/system_wrappers/include/atomic32.h"
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000017
18namespace webrtc {
19
20class SharedDesktopFrame::Core {
21 public:
22 Core(DesktopFrame* frame) : frame_(frame) {}
23
24 DesktopFrame* frame() { return frame_.get(); }
25
26 bool HasOneRef() { return ref_count_.Value() == 1; }
27
28 virtual int32_t AddRef() {
29 return ++ref_count_;
30 }
31
32 virtual int32_t Release() {
33 int32_t ref_count;
34 ref_count = --ref_count_;
35 if (ref_count == 0)
36 delete this;
37 return ref_count;
38 }
39
40 private:
41 virtual ~Core() {}
42
43 Atomic32 ref_count_;
kwiberg2bb3afa2016-03-16 15:58:08 -070044 std::unique_ptr<DesktopFrame> frame_;
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000045
henrikg3c089d72015-09-16 05:37:44 -070046 RTC_DISALLOW_COPY_AND_ASSIGN(Core);
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000047};
48
49SharedDesktopFrame::~SharedDesktopFrame() {}
50
51// static
zijiehe809dcb42016-04-22 16:08:39 -070052SharedDesktopFrame* SharedDesktopFrame::Wrap(DesktopFrame* desktop_frame) {
Peter Boström26b08602015-06-04 15:18:17 +020053 rtc::scoped_refptr<Core> core(new Core(desktop_frame));
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000054 return new SharedDesktopFrame(core);
55}
56
57DesktopFrame* SharedDesktopFrame::GetUnderlyingFrame() {
58 return core_->frame();
59}
60
61SharedDesktopFrame* SharedDesktopFrame::Share() {
62 SharedDesktopFrame* result = new SharedDesktopFrame(core_);
63 result->set_dpi(dpi());
64 result->set_capture_time_ms(capture_time_ms());
65 *result->mutable_updated_region() = updated_region();
66 return result;
67}
68
69bool SharedDesktopFrame::IsShared() {
70 return !core_->HasOneRef();
71}
72
Peter Boström26b08602015-06-04 15:18:17 +020073SharedDesktopFrame::SharedDesktopFrame(rtc::scoped_refptr<Core> core)
74 : DesktopFrame(core->frame()->size(),
75 core->frame()->stride(),
76 core->frame()->data(),
77 core->frame()->shared_memory()),
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000078 core_(core) {
79}
80
81} // namespace webrtc