sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "modules/desktop_capture/differ_block.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 12 | |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include "test/gtest.h" |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | // Run 900 times to mimic 1280x720. |
| 20 | // TODO(fbarchard): Remove benchmark once performance is non-issue. |
| 21 | static const int kTimesToRun = 900; |
| 22 | |
| 23 | static void GenerateData(uint8_t* data, int size) { |
| 24 | for (int i = 0; i < size; ++i) { |
| 25 | data[i] = i; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // Memory buffer large enough for 2 blocks aligned to 16 bytes. |
| 30 | static const int kSizeOfBlock = kBlockSize * kBlockSize * kBytesPerPixel; |
| 31 | uint8_t block_buffer[kSizeOfBlock * 2 + 16]; |
| 32 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 33 | void PrepareBuffers(uint8_t*& block1, uint8_t*& block2) { |
| 34 | block1 = reinterpret_cast<uint8_t*>( |
| 35 | (reinterpret_cast<uintptr_t>(&block_buffer[0]) + 15) & ~15); |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 36 | GenerateData(block1, kSizeOfBlock); |
| 37 | block2 = block1 + kSizeOfBlock; |
| 38 | memcpy(block2, block1, kSizeOfBlock); |
| 39 | } |
| 40 | |
| 41 | TEST(BlockDifferenceTestSame, BlockDifference) { |
| 42 | uint8_t* block1; |
| 43 | uint8_t* block2; |
| 44 | PrepareBuffers(block1, block2); |
| 45 | |
| 46 | // These blocks should match. |
| 47 | for (int i = 0; i < kTimesToRun; ++i) { |
| 48 | int result = BlockDifference(block1, block2, kBlockSize * kBytesPerPixel); |
| 49 | EXPECT_EQ(0, result); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | TEST(BlockDifferenceTestLast, BlockDifference) { |
| 54 | uint8_t* block1; |
| 55 | uint8_t* block2; |
| 56 | PrepareBuffers(block1, block2); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 57 | block2[kSizeOfBlock - 2] += 1; |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 58 | |
| 59 | for (int i = 0; i < kTimesToRun; ++i) { |
| 60 | int result = BlockDifference(block1, block2, kBlockSize * kBytesPerPixel); |
| 61 | EXPECT_EQ(1, result); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | TEST(BlockDifferenceTestMid, BlockDifference) { |
| 66 | uint8_t* block1; |
| 67 | uint8_t* block2; |
| 68 | PrepareBuffers(block1, block2); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 69 | block2[kSizeOfBlock / 2 + 1] += 1; |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 70 | |
| 71 | for (int i = 0; i < kTimesToRun; ++i) { |
| 72 | int result = BlockDifference(block1, block2, kBlockSize * kBytesPerPixel); |
| 73 | EXPECT_EQ(1, result); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | TEST(BlockDifferenceTestFirst, BlockDifference) { |
| 78 | uint8_t* block1; |
| 79 | uint8_t* block2; |
| 80 | PrepareBuffers(block1, block2); |
| 81 | block2[0] += 1; |
| 82 | |
| 83 | for (int i = 0; i < kTimesToRun; ++i) { |
| 84 | int result = BlockDifference(block1, block2, kBlockSize * kBytesPerPixel); |
| 85 | EXPECT_EQ(1, result); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | } // namespace webrtc |