• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "modules/desktop_capture/differ_block.h"
12 
13 #include <string.h>
14 
15 #include "test/gtest.h"
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 
GenerateData(uint8_t * data,int size)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 
PrepareBuffers(uint8_t * & block1,uint8_t * & block2)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);
36   GenerateData(block1, kSizeOfBlock);
37   block2 = block1 + kSizeOfBlock;
38   memcpy(block2, block1, kSizeOfBlock);
39 }
40 
TEST(BlockDifferenceTestSame,BlockDifference)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 
TEST(BlockDifferenceTestLast,BlockDifference)53 TEST(BlockDifferenceTestLast, BlockDifference) {
54   uint8_t* block1;
55   uint8_t* block2;
56   PrepareBuffers(block1, block2);
57   block2[kSizeOfBlock - 2] += 1;
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 
TEST(BlockDifferenceTestMid,BlockDifference)65 TEST(BlockDifferenceTestMid, BlockDifference) {
66   uint8_t* block1;
67   uint8_t* block2;
68   PrepareBuffers(block1, block2);
69   block2[kSizeOfBlock / 2 + 1] += 1;
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 
TEST(BlockDifferenceTestFirst,BlockDifference)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
90