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