1 /*
2 * Copyright (c) 2016 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 "modules/desktop_capture/screen_drawer.h"
12
13 #include <stdint.h>
14
15 #include <atomic>
16 #include <memory>
17
18 #include "api/function_view.h"
19 #include "rtc_base/checks.h"
20 #include "rtc_base/logging.h"
21 #include "rtc_base/platform_thread.h"
22 #include "rtc_base/random.h"
23 #include "rtc_base/time_utils.h"
24 #include "system_wrappers/include/sleep.h"
25 #include "test/gtest.h"
26
27 #if defined(WEBRTC_POSIX)
28 #include "modules/desktop_capture/screen_drawer_lock_posix.h"
29 #endif
30
31 namespace webrtc {
32
33 namespace {
34
TestScreenDrawerLock(rtc::FunctionView<std::unique_ptr<ScreenDrawerLock> ()> ctor)35 void TestScreenDrawerLock(
36 rtc::FunctionView<std::unique_ptr<ScreenDrawerLock>()> ctor) {
37 constexpr int kLockDurationMs = 100;
38
39 std::atomic<bool> created(false);
40 std::atomic<bool> ready(false);
41
42 class Task {
43 public:
44 Task(std::atomic<bool>* created,
45 const std::atomic<bool>& ready,
46 rtc::FunctionView<std::unique_ptr<ScreenDrawerLock>()> ctor)
47 : created_(created), ready_(ready), ctor_(ctor) {}
48
49 ~Task() = default;
50
51 static void RunTask(void* me) {
52 Task* task = static_cast<Task*>(me);
53 std::unique_ptr<ScreenDrawerLock> lock = task->ctor_();
54 ASSERT_TRUE(!!lock);
55 task->created_->store(true);
56 // Wait for the main thread to get the signal of created_.
57 while (!task->ready_.load()) {
58 SleepMs(1);
59 }
60 // At this point, main thread should begin to create a second lock. Though
61 // it's still possible the second lock won't be created before the
62 // following sleep has been finished, the possibility will be
63 // significantly reduced.
64 const int64_t current_ms = rtc::TimeMillis();
65 // SleepMs() may return early. See
66 // https://cs.chromium.org/chromium/src/third_party/webrtc/system_wrappers/include/sleep.h?rcl=4a604c80cecce18aff6fc5e16296d04675312d83&l=20
67 // But we need to ensure at least 100 ms has been passed before unlocking
68 // |lock|.
69 while (rtc::TimeMillis() - current_ms < kLockDurationMs) {
70 SleepMs(kLockDurationMs - (rtc::TimeMillis() - current_ms));
71 }
72 }
73
74 private:
75 std::atomic<bool>* const created_;
76 const std::atomic<bool>& ready_;
77 const rtc::FunctionView<std::unique_ptr<ScreenDrawerLock>()> ctor_;
78 } task(&created, ready, ctor);
79
80 rtc::PlatformThread lock_thread(&Task::RunTask, &task, "lock_thread");
81 lock_thread.Start();
82
83 // Wait for the first lock in Task::RunTask() to be created.
84 // TODO(zijiehe): Find a better solution to wait for the creation of the first
85 // lock. See
86 // https://chromium-review.googlesource.com/c/607688/13/webrtc/modules/desktop_capture/screen_drawer_unittest.cc
87 while (!created.load()) {
88 SleepMs(1);
89 }
90
91 const int64_t start_ms = rtc::TimeMillis();
92 ready.store(true);
93 // This is unlikely to fail, but just in case current thread is too laggy and
94 // cause the SleepMs() in RunTask() to finish before we creating another lock.
95 ASSERT_GT(kLockDurationMs, rtc::TimeMillis() - start_ms);
96 ctor();
97 ASSERT_LE(kLockDurationMs, rtc::TimeMillis() - start_ms);
98 lock_thread.Stop();
99 }
100
101 } // namespace
102
103 // These are a set of manual test cases, as we do not have an automatical way to
104 // detect whether a ScreenDrawer on a certain platform works well without
105 // ScreenCapturer(s). So you may execute these test cases with
106 // --gtest_also_run_disabled_tests --gtest_filter=ScreenDrawerTest.*.
TEST(ScreenDrawerTest,DISABLED_DrawRectangles)107 TEST(ScreenDrawerTest, DISABLED_DrawRectangles) {
108 std::unique_ptr<ScreenDrawer> drawer = ScreenDrawer::Create();
109 if (!drawer) {
110 RTC_LOG(LS_WARNING)
111 << "No ScreenDrawer implementation for current platform.";
112 return;
113 }
114
115 if (drawer->DrawableRegion().is_empty()) {
116 RTC_LOG(LS_WARNING)
117 << "ScreenDrawer of current platform does not provide a "
118 "non-empty DrawableRegion().";
119 return;
120 }
121
122 DesktopRect rect = drawer->DrawableRegion();
123 Random random(rtc::TimeMicros());
124 for (int i = 0; i < 100; i++) {
125 // Make sure we at least draw one pixel.
126 int left = random.Rand(rect.left(), rect.right() - 2);
127 int top = random.Rand(rect.top(), rect.bottom() - 2);
128 drawer->DrawRectangle(
129 DesktopRect::MakeLTRB(left, top, random.Rand(left + 1, rect.right()),
130 random.Rand(top + 1, rect.bottom())),
131 RgbaColor(random.Rand<uint8_t>(), random.Rand<uint8_t>(),
132 random.Rand<uint8_t>(), random.Rand<uint8_t>()));
133
134 if (i == 50) {
135 SleepMs(10000);
136 }
137 }
138
139 SleepMs(10000);
140 }
141
142 #if defined(THREAD_SANITIZER) // bugs.webrtc.org/10019
143 #define MAYBE_TwoScreenDrawerLocks DISABLED_TwoScreenDrawerLocks
144 #else
145 #define MAYBE_TwoScreenDrawerLocks TwoScreenDrawerLocks
146 #endif
TEST(ScreenDrawerTest,MAYBE_TwoScreenDrawerLocks)147 TEST(ScreenDrawerTest, MAYBE_TwoScreenDrawerLocks) {
148 #if defined(WEBRTC_POSIX)
149 // ScreenDrawerLockPosix won't be able to unlink the named semaphore. So use a
150 // different semaphore name here to avoid deadlock.
151 const char* semaphore_name = "GSDL8784541a812011e788ff67427b";
152 ScreenDrawerLockPosix::Unlink(semaphore_name);
153
154 TestScreenDrawerLock([semaphore_name]() {
155 return std::make_unique<ScreenDrawerLockPosix>(semaphore_name);
156 });
157 #elif defined(WEBRTC_WIN)
158 TestScreenDrawerLock([]() { return ScreenDrawerLock::Create(); });
159 #endif
160 }
161
162 } // namespace webrtc
163