Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(741)

Side by Side Diff: webrtc/modules/desktop_capture/shared_desktop_frame.cc

Issue 1988783003: Use std::unique_ptr<> to pass frame ownership in DesktopCapturer impls. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/desktop_capture/shared_desktop_frame.h" 11 #include "webrtc/modules/desktop_capture/shared_desktop_frame.h"
12 12
13 #include <memory> 13 #include <memory>
14 14
15 #include "webrtc/base/constructormagic.h" 15 #include "webrtc/base/constructormagic.h"
16 #include "webrtc/system_wrappers/include/atomic32.h" 16 #include "webrtc/system_wrappers/include/atomic32.h"
17 17
18 namespace webrtc { 18 namespace webrtc {
19 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_;
44 std::unique_ptr<DesktopFrame> frame_;
45
46 RTC_DISALLOW_COPY_AND_ASSIGN(Core);
47 };
48
49 SharedDesktopFrame::~SharedDesktopFrame() {} 20 SharedDesktopFrame::~SharedDesktopFrame() {}
50 21
51 // static 22 // static
23 std::unique_ptr<SharedDesktopFrame> SharedDesktopFrame::Wrap(
24 std::unique_ptr<DesktopFrame> desktop_frame) {
25 return std::unique_ptr<SharedDesktopFrame>(
26 new SharedDesktopFrame(new Core(desktop_frame.release())));
27 }
28
52 SharedDesktopFrame* SharedDesktopFrame::Wrap(DesktopFrame* desktop_frame) { 29 SharedDesktopFrame* SharedDesktopFrame::Wrap(DesktopFrame* desktop_frame) {
53 rtc::scoped_refptr<Core> core(new Core(desktop_frame)); 30 return Wrap(std::unique_ptr<DesktopFrame>(desktop_frame)).release();
54 return new SharedDesktopFrame(core);
55 } 31 }
56 32
57 DesktopFrame* SharedDesktopFrame::GetUnderlyingFrame() { 33 DesktopFrame* SharedDesktopFrame::GetUnderlyingFrame() {
58 return core_->frame(); 34 return core_->get();
59 } 35 }
60 36
61 SharedDesktopFrame* SharedDesktopFrame::Share() { 37 std::unique_ptr<SharedDesktopFrame> SharedDesktopFrame::Share() {
62 SharedDesktopFrame* result = new SharedDesktopFrame(core_); 38 std::unique_ptr<SharedDesktopFrame> result(new SharedDesktopFrame(core_));
63 result->set_dpi(dpi()); 39 result->set_dpi(dpi());
64 result->set_capture_time_ms(capture_time_ms()); 40 result->set_capture_time_ms(capture_time_ms());
65 *result->mutable_updated_region() = updated_region(); 41 *result->mutable_updated_region() = updated_region();
66 return result; 42 return result;
67 } 43 }
68 44
69 bool SharedDesktopFrame::IsShared() { 45 bool SharedDesktopFrame::IsShared() {
70 return !core_->HasOneRef(); 46 return !core_->HasOneRef();
71 } 47 }
72 48
73 SharedDesktopFrame::SharedDesktopFrame(rtc::scoped_refptr<Core> core) 49 SharedDesktopFrame::SharedDesktopFrame(rtc::scoped_refptr<Core> core)
74 : DesktopFrame(core->frame()->size(), 50 : DesktopFrame((*core)->size(),
75 core->frame()->stride(), 51 (*core)->stride(),
76 core->frame()->data(), 52 (*core)->data(),
77 core->frame()->shared_memory()), 53 (*core)->shared_memory()),
78 core_(core) { 54 core_(core) {}
79 }
80 55
81 } // namespace webrtc 56 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698