Chromium Code Reviews| Index: webrtc/modules/desktop_capture/screen_capturer_differ_wrapper_unittest.cc |
| diff --git a/webrtc/modules/desktop_capture/screen_capturer_differ_wrapper_unittest.cc b/webrtc/modules/desktop_capture/screen_capturer_differ_wrapper_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..64ce3f4b7961a60b9401692d6c0da07f5e58f6c7 |
| --- /dev/null |
| +++ b/webrtc/modules/desktop_capture/screen_capturer_differ_wrapper_unittest.cc |
| @@ -0,0 +1,302 @@ |
| +/* |
| + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/desktop_capture/screen_capturer_differ_wrapper.h" |
| + |
| +#include <initializer_list> |
| +#include <memory> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "webrtc/base/random.h" |
| +#include "webrtc/base/timeutils.h" |
| +#include "webrtc/modules/desktop_capture/desktop_geometry.h" |
| +#include "webrtc/modules/desktop_capture/desktop_region.h" |
| +#include "webrtc/modules/desktop_capture/differ_block.h" |
| +#include "webrtc/modules/desktop_capture/screen_capturer_mock_objects.h" |
| +#include "webrtc/system_wrappers/include/cpu_features_wrapper.h" |
| +#include "webrtc/typedefs.h" |
| + |
| +namespace webrtc { |
| + |
| +namespace { |
| + |
| +// ScreenCapturerDifferWrapper always use a 32 pixel block. So we normalize the |
| +// |rects|. |
| +DesktopRect NormalizeDesktopRect(DesktopRect rect, DesktopSize size) { |
| + DesktopRect block_size_rect = DesktopRect::MakeLTRB( |
| + rect.left() / kBlockSize * kBlockSize, |
| + rect.top() / kBlockSize * kBlockSize, |
| + (rect.right() + kBlockSize - 1) / kBlockSize * kBlockSize, |
| + (rect.bottom() + kBlockSize - 1) / kBlockSize * kBlockSize); |
| + block_size_rect.IntersectWith(DesktopRect::MakeSize(size)); |
| + return block_size_rect; |
| +} |
| + |
| +// Compares and asserts |frame|.updated_region() equals to |rects|. This |
| +// function does not care about the order of the |rects|. |
| +template <template <typename, typename...> class T = std::initializer_list, |
| + typename... Rect> |
| +void AssertUpdatedRegionIs(const DesktopFrame& frame, |
| + const T<DesktopRect, Rect...>& rects) { |
| + for (const DesktopRect& rect : rects) { |
| + DesktopRect block_size_rect = NormalizeDesktopRect(rect, frame.size()); |
| + bool found = false; |
| + for (DesktopRegion::Iterator it(frame.updated_region()); !it.IsAtEnd(); |
| + it.Advance()) { |
| + if (it.rect().equals(block_size_rect)) { |
| + found = true; |
| + break; |
| + } |
| + } |
| + ASSERT_TRUE(found); |
| + } |
| +} |
| + |
| +// Compares and asserts |frame|.updated_region() covers all rectangles in |
| +// |rects|, but does not cover areas other than a kBlockSize expansion. This |
| +// function does not care about the order of the |rects|, and it does not expect |
| +// DesktopRegion to return an exact area of each rectangle in |rects|. |
| +template <template <typename, typename...> class T = std::initializer_list, |
| + typename... Rect> |
| +void AssertUpdatedRegionCovers(const DesktopFrame& frame, |
| + const T<DesktopRect, Rect...>& rects) { |
| + DesktopRegion region; |
| + for (const auto& rect : rects) { |
| + region.AddRect(NormalizeDesktopRect(rect, frame.size())); |
| + } |
| + ASSERT_TRUE(frame.updated_region().Equals(region)); |
| +} |
| + |
| +template <template <typename, typename...> class T = std::initializer_list, |
| + typename... Rect> |
| +void ExecuteDifferWrapperCase(MockScreenCapturer* mock_capturer, |
| + ScreenCapturerDifferWrapper* capturer, |
| + MockScreenCapturerCallback* callback, |
| + const T<DesktopRect, Rect...>& dirty_region, |
| + bool check_result, |
| + bool exactly_match) { |
| + EXPECT_CALL(*callback, |
| + OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, testing::_)) |
| + .Times(1) |
| + .WillOnce(testing::Invoke([&dirty_region, check_result, exactly_match]( |
| + DesktopCapturer::Result result, |
| + std::unique_ptr<DesktopFrame>* frame) { |
| + ASSERT_EQ(result, DesktopCapturer::Result::SUCCESS); |
| + if (check_result) { |
| + if (exactly_match) { |
| + AssertUpdatedRegionIs(**frame, dirty_region); |
| + } else { |
| + AssertUpdatedRegionCovers(**frame, dirty_region); |
| + } |
| + } |
| + })); |
| + EXPECT_CALL(*mock_capturer, Capture(testing::_)).Times(1); |
| + for (const auto& rect : dirty_region) { |
| + mock_capturer->dirty_region()->AddRect(rect); |
| + } |
| + capturer->Capture(DesktopRegion()); |
| +} |
| + |
| +// Executes a ScreenCapturerDifferWrapper::Capture(), if dirty_region() is not |
| +// set, this function will reset ScreenCapturerDifferWrapper internal |
| +// DesktopFrame into white. |
| +void ExecuteCapturer(MockScreenCapturer* mock_capturer, |
| + ScreenCapturerDifferWrapper* capturer, |
| + MockScreenCapturerCallback* callback) { |
| + EXPECT_CALL(*callback, |
| + OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, testing::_)) |
| + .Times(1); |
| + EXPECT_CALL(*mock_capturer, Capture(testing::_)).Times(1); |
| + capturer->Capture(DesktopRegion()); |
| +} |
| + |
| +void ExecuteDifferWrapperTest(bool with_hints, |
| + bool enlarge_dirty_region, |
| + bool random_dirty_region, |
| + bool check_result) { |
| + std::unique_ptr<MockScreenCapturer> mock(new MockScreenCapturer()); |
| + MockScreenCapturer* mock_capturer = mock.get(); |
| + ScreenCapturerDifferWrapper capturer(std::move(mock), !with_hints); |
| + MockScreenCapturerCallback callback; |
| + *mock_capturer->provide_dirty_region_hints() = with_hints; |
| + *mock_capturer->enlarge_dirty_region() = enlarge_dirty_region; |
| + *mock_capturer->random_dirty_region() = random_dirty_region; |
| + |
| + EXPECT_CALL(*mock_capturer, Start(testing::_)).Times(1); |
| + capturer.Start(&callback); |
| + |
| + EXPECT_CALL(callback, |
| + OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, testing::_)) |
| + .Times(1) |
| + .WillOnce(testing::Invoke([](DesktopCapturer::Result result, |
| + std::unique_ptr<DesktopFrame>* frame) { |
| + ASSERT_EQ(result, DesktopCapturer::Result::SUCCESS); |
| + AssertUpdatedRegionIs(**frame, |
| + {DesktopRect::MakeSize((*frame)->size())}); |
| + })); |
| + EXPECT_CALL(*mock_capturer, Capture(testing::_)).Times(1); |
| + capturer.Capture(DesktopRegion()); |
| + |
| + ExecuteDifferWrapperCase(mock_capturer, &capturer, &callback, |
| + {DesktopRect::MakeLTRB(100, 100, 200, 200), |
| + DesktopRect::MakeLTRB(300, 300, 400, 400)}, |
| + check_result, true); |
| + ExecuteCapturer(mock_capturer, &capturer, &callback); |
| + |
| + ExecuteDifferWrapperCase( |
| + mock_capturer, &capturer, &callback, |
| + {DesktopRect::MakeLTRB(0, 0, 40, 40), |
| + DesktopRect::MakeLTRB(0, mock_capturer->size()->height() - 40, 40, |
| + mock_capturer->size()->height()), |
| + DesktopRect::MakeLTRB(mock_capturer->size()->width() - 40, 0, |
| + mock_capturer->size()->width(), 40), |
| + DesktopRect::MakeLTRB(mock_capturer->size()->width() - 40, |
| + mock_capturer->size()->height() - 40, |
| + mock_capturer->size()->width(), |
| + mock_capturer->size()->height())}, |
| + check_result, true); |
| + |
| + Random random(rtc::TimeMillis()); |
| + // Fuzzing tests. |
| + for (int i = 0; i < 1000; i++) { |
| + if (enlarge_dirty_region) { |
| + *mock_capturer->enlarge_range() = random.Rand(1, 50); |
| + } |
| + mock_capturer->size()->set(random.Rand(500, 2000), random.Rand(500, 2000)); |
| + ExecuteCapturer(mock_capturer, &capturer, &callback); |
| + std::vector<DesktopRect> dirty_region; |
| + for (int j = random.Rand(50); j >= 0; j--) { |
| + // At least a 1 x 1 dirty region. |
| + const int left = random.Rand(0, mock_capturer->size()->width() - 2); |
| + const int top = random.Rand(0, mock_capturer->size()->height() - 2); |
| + const int right = random.Rand(left + 1, mock_capturer->size()->width()); |
| + const int bottom = random.Rand(top + 1, mock_capturer->size()->height()); |
| + dirty_region.push_back(DesktopRect::MakeLTRB(left, top, right, bottom)); |
| + } |
| + ExecuteDifferWrapperCase(mock_capturer, &capturer, &callback, dirty_region, |
| + check_result, false); |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +TEST(ScreenCapturerDifferWrapperTest, FunctionForwarding) { |
| + std::unique_ptr<MockScreenCapturer> mock(new MockScreenCapturer()); |
| + MockScreenCapturer* mock_capturer = mock.get(); |
| + ScreenCapturerDifferWrapper capturer(std::move(mock), true); |
| + MockScreenCapturerCallback callback; |
| + |
| + EXPECT_CALL(*mock_capturer, Start(testing::_)).Times(1); |
| + capturer.Start(&callback); |
| + |
| + EXPECT_CALL(*mock_capturer, Capture(testing::_)).Times(1); |
| + EXPECT_CALL(callback, |
| + OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, testing::_)) |
| + .Times(1); |
| + capturer.Capture(DesktopRegion()); |
| + |
| + EXPECT_CALL(*mock_capturer, GetScreenList(testing::_)).Times(1); |
| + ASSERT_TRUE(capturer.GetScreenList(nullptr)); |
| + |
| + EXPECT_CALL(*mock_capturer, SelectScreen(testing::_)).Times(2); |
| + ASSERT_FALSE(capturer.SelectScreen(ScreenId{100})); |
| + ASSERT_TRUE(capturer.SelectScreen(kFullDesktopScreenId)); |
| +} |
| + |
| +TEST(ScreenCapturerDifferWrapperTest, CaptureWithoutHints) { |
| + ExecuteDifferWrapperTest(false, false, false, true); |
| +} |
| + |
| +TEST(ScreenCapturerDifferWrapperTest, CaptureWithHints) { |
| + ExecuteDifferWrapperTest(true, false, false, true); |
| +} |
| + |
| +TEST(ScreenCapturerDifferWrapperTest, CaptureWithEnlargedHints) { |
| + ExecuteDifferWrapperTest(true, true, false, true); |
| +} |
| + |
| +TEST(ScreenCapturerDifferWrapperTest, CaptureWithRandomHints) { |
| + ExecuteDifferWrapperTest(true, false, true, true); |
| +} |
| + |
| +TEST(ScreenCapturerDifferWrapperTest, CaptureWithEnlargedAndRandomHints) { |
| + ExecuteDifferWrapperTest(true, true, true, true); |
| +} |
| + |
| +// SSE2 is not available on ARM / MIPS, so the performance should be extremely |
| +// lower. |
| +#if defined(WEBRTC_ARCH_X86_FAMILY) |
| +// When hints are enabled, ScreenCapturerDifferWrapper has a slightly better |
| +// performance in current configuration, but not so significant. Following is |
| +// one run result. |
| +// [ RUN ] CaptureWithoutHintsPerf |
| +// [ OK ] CaptureWithoutHintsPerf (8889 ms) |
| +// [ RUN ] CaptureWithHintsPerf |
| +// [ OK ] CaptureWithHintsPerf (7814 ms) |
| +// [ RUN ] CaptureWithEnlargedHintsPerf |
| +// [ OK ] CaptureWithEnlargedHintsPerf (7999 ms) |
| +// [ RUN ] CaptureWithRandomHintsPerf |
| +// [ OK ] CaptureWithRandomHintsPerf (8429 ms) |
| +// [ RUN ] CaptureWithEnlargedAndRandomHintsPerf |
| +// [ OK ] CaptureWithEnlargedAndRandomHintsPerf (8552 ms) |
| +TEST(ScreenCapturerDifferWrapperTest, CaptureWithoutHintsPerf) { |
|
Sergey Ulanov
2016/08/12 05:25:51
Perf test shouldn't be mixed with unittests. They
Hzj_jie
2016/08/15 00:38:06
Done.
|
| + if (WebRtc_GetCPUInfo(kSSE2) == 0) { |
| + return; |
| + } |
| + |
| + int64_t started = rtc::TimeMillis(); |
| + ExecuteDifferWrapperTest(false, false, false, false); |
| + ASSERT_LE(rtc::TimeMillis() - started, 15000); |
| +} |
| + |
| +TEST(ScreenCapturerDifferWrapperTest, CaptureWithHintsPerf) { |
| + if (WebRtc_GetCPUInfo(kSSE2) == 0) { |
| + return; |
| + } |
| + |
| + int64_t started = rtc::TimeMillis(); |
| + ExecuteDifferWrapperTest(true, false, false, false); |
| + ASSERT_LE(rtc::TimeMillis() - started, 15000); |
| +} |
| + |
| +TEST(ScreenCapturerDifferWrapperTest, CaptureWithEnlargedHintsPerf) { |
| + if (WebRtc_GetCPUInfo(kSSE2) == 0) { |
| + return; |
| + } |
| + |
| + int64_t started = rtc::TimeMillis(); |
| + ExecuteDifferWrapperTest(true, true, false, false); |
| + ASSERT_LE(rtc::TimeMillis() - started, 15000); |
| +} |
| + |
| +TEST(ScreenCapturerDifferWrapperTest, CaptureWithRandomHintsPerf) { |
| + if (WebRtc_GetCPUInfo(kSSE2) == 0) { |
| + return; |
| + } |
| + |
| + int64_t started = rtc::TimeMillis(); |
| + ExecuteDifferWrapperTest(true, false, true, false); |
| + ASSERT_LE(rtc::TimeMillis() - started, 15000); |
| +} |
| + |
| +TEST(ScreenCapturerDifferWrapperTest, CaptureWithEnlargedAndRandomHintsPerf) { |
| + if (WebRtc_GetCPUInfo(kSSE2) == 0) { |
| + return; |
| + } |
| + |
| + int64_t started = rtc::TimeMillis(); |
| + ExecuteDifferWrapperTest(true, true, true, false); |
| + ASSERT_LE(rtc::TimeMillis() - started, 15000); |
| +} |
| +#endif |
| + |
| +} // namespace webrtc |