sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 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 | |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/include/atomic32.h" |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | class 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_; |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 43 | std::unique_ptr<DesktopFrame> frame_; |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 44 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 45 | RTC_DISALLOW_COPY_AND_ASSIGN(Core); |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | SharedDesktopFrame::~SharedDesktopFrame() {} |
| 49 | |
| 50 | // static |
zijiehe | 809dcb4 | 2016-04-22 16:08:39 -0700 | [diff] [blame^] | 51 | SharedDesktopFrame* SharedDesktopFrame::Wrap(DesktopFrame* desktop_frame) { |
Peter Boström | 26b0860 | 2015-06-04 15:18:17 +0200 | [diff] [blame] | 52 | rtc::scoped_refptr<Core> core(new Core(desktop_frame)); |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 53 | return new SharedDesktopFrame(core); |
| 54 | } |
| 55 | |
| 56 | DesktopFrame* SharedDesktopFrame::GetUnderlyingFrame() { |
| 57 | return core_->frame(); |
| 58 | } |
| 59 | |
| 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 | |
| 68 | bool SharedDesktopFrame::IsShared() { |
| 69 | return !core_->HasOneRef(); |
| 70 | } |
| 71 | |
Peter Boström | 26b0860 | 2015-06-04 15:18:17 +0200 | [diff] [blame] | 72 | SharedDesktopFrame::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.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 77 | core_(core) { |
| 78 | } |
| 79 | |
| 80 | } // namespace webrtc |