blob: 7651816473e591025d65b5594fba43dfc660e69c [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
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000013#include "webrtc/base/scoped_ptr.h"
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000014#include "webrtc/system_wrappers/interface/atomic32.h"
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000015
16namespace webrtc {
17
18class SharedDesktopFrame::Core {
19 public:
20 Core(DesktopFrame* frame) : frame_(frame) {}
21
22 DesktopFrame* frame() { return frame_.get(); }
23
24 bool HasOneRef() { return ref_count_.Value() == 1; }
25
26 virtual int32_t AddRef() {
27 return ++ref_count_;
28 }
29
30 virtual int32_t Release() {
31 int32_t ref_count;
32 ref_count = --ref_count_;
33 if (ref_count == 0)
34 delete this;
35 return ref_count;
36 }
37
38 private:
39 virtual ~Core() {}
40
41 Atomic32 ref_count_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000042 rtc::scoped_ptr<DesktopFrame> frame_;
sergeyu@chromium.org3d34f662013-06-04 18:51:23 +000043
44 DISALLOW_COPY_AND_ASSIGN(Core);
45};
46
47SharedDesktopFrame::~SharedDesktopFrame() {}
48
49// static
50SharedDesktopFrame* SharedDesktopFrame::Wrap(
51 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