• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 namespace webrtc {
17 
18 class SharedDesktopFrame::Core {
19  public:
Core(DesktopFrame * frame)20   Core(DesktopFrame* frame) : frame_(frame) {}
21 
frame()22   DesktopFrame* frame() { return frame_.get(); }
23 
HasOneRef()24   bool HasOneRef() { return ref_count_.Value() == 1; }
25 
AddRef()26   virtual int32_t AddRef() {
27     return ++ref_count_;
28   }
29 
Release()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:
~Core()39   virtual ~Core() {}
40 
41   Atomic32 ref_count_;
42   scoped_ptr<DesktopFrame> frame_;
43 
44   DISALLOW_COPY_AND_ASSIGN(Core);
45 };
46 
~SharedDesktopFrame()47 SharedDesktopFrame::~SharedDesktopFrame() {}
48 
49 // static
Wrap(DesktopFrame * desktop_frame)50 SharedDesktopFrame* SharedDesktopFrame::Wrap(
51     DesktopFrame* desktop_frame) {
52   scoped_refptr<Core> core(new Core(desktop_frame));
53   return new SharedDesktopFrame(core);
54 }
55 
GetUnderlyingFrame()56 DesktopFrame* SharedDesktopFrame::GetUnderlyingFrame() {
57   return core_->frame();
58 }
59 
Share()60 SharedDesktopFrame* 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 
IsShared()68 bool SharedDesktopFrame::IsShared() {
69   return !core_->HasOneRef();
70 }
71 
SharedDesktopFrame(scoped_refptr<Core> core)72 SharedDesktopFrame::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