1/*
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
13#include "webrtc/system_wrappers/interface/atomic32.h"
14#include "webrtc/system_wrappers/interface/scoped_ptr.h"
15
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_;
42  scoped_ptr<DesktopFrame> frame_;
43
44  DISALLOW_COPY_AND_ASSIGN(Core);
45};
46
47SharedDesktopFrame::~SharedDesktopFrame() {}
48
49// static
50SharedDesktopFrame* SharedDesktopFrame::Wrap(
51    DesktopFrame* desktop_frame) {
52  scoped_refptr<Core> core(new Core(desktop_frame));
53  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
72SharedDesktopFrame::SharedDesktopFrame(scoped_refptr<Core> core)
73    : DesktopFrame(core->frame()->size(), core->frame()->stride(),
74                   core->frame()->data(), core->frame()->shared_memory()),
75      core_(core) {
76}
77
78}  // namespace webrtc
79