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