2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
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.
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "webrtc/modules/desktop_capture/differ.h"
13 #include "webrtc/modules/desktop_capture/differ_block.h"
14 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
18 // 96x96 screen gives a 4x4 grid of blocks.
19 const int kScreenWidth= 96;
20 const int kScreenHeight = 96;
22 // To test partial blocks, we need a width and height that are not multiples
23 // of 16 (or 32, depending on current block size).
24 const int kPartialScreenWidth = 70;
25 const int kPartialScreenHeight = 70;
27 class DifferTest : public testing::Test {
33 void InitDiffer(int width, int height) {
36 bytes_per_pixel_ = kBytesPerPixel;
37 stride_ = (kBytesPerPixel * width);
38 buffer_size_ = width_ * height_ * bytes_per_pixel_;
40 differ_.reset(new Differ(width_, height_, bytes_per_pixel_, stride_));
42 prev_.reset(new uint8_t[buffer_size_]);
43 memset(prev_.get(), 0, buffer_size_);
45 curr_.reset(new uint8_t[buffer_size_]);
46 memset(curr_.get(), 0, buffer_size_);
49 void ClearBuffer(uint8_t* buffer) {
50 memset(buffer, 0, buffer_size_);
53 // Here in DifferTest so that tests can access private methods of Differ.
54 void MarkDirtyBlocks(const void* prev_buffer, const void* curr_buffer) {
55 differ_->MarkDirtyBlocks(prev_buffer, curr_buffer);
58 void MergeBlocks(DesktopRegion* dirty) {
59 differ_->MergeBlocks(dirty);
62 // Convenience method to count rectangles in a region.
63 int RegionRectCount(const DesktopRegion& region) {
65 for (DesktopRegion::Iterator iter(region);
66 !iter.IsAtEnd(); iter.Advance()) {
72 // Convenience wrapper for Differ's DiffBlock that calculates the appropriate
73 // offset to the start of the desired block.
74 DiffInfo DiffBlock(int block_x, int block_y) {
75 // Offset from upper-left of buffer to upper-left of requested block.
76 int block_offset = ((block_y * stride_) + (block_x * bytes_per_pixel_))
78 return BlockDifference(prev_.get() + block_offset,
79 curr_.get() + block_offset,
83 // Write the pixel |value| into the specified block in the |buffer|.
84 // This is a convenience wrapper around WritePixel().
85 void WriteBlockPixel(uint8_t* buffer, int block_x, int block_y,
86 int pixel_x, int pixel_y, uint32_t value) {
87 WritePixel(buffer, (block_x * kBlockSize) + pixel_x,
88 (block_y * kBlockSize) + pixel_y, value);
91 // Write the test pixel |value| into the |buffer| at the specified |x|,|y|
93 // Only the low-order bytes from |value| are written (assuming little-endian).
94 // So, for |value| = 0xaabbccdd:
95 // If bytes_per_pixel = 4, then ddccbbaa will be written as the pixel value.
99 void WritePixel(uint8_t* buffer, int x, int y, uint32_t value) {
100 uint8_t* pixel = reinterpret_cast<uint8_t*>(&value);
101 buffer += (y * stride_) + (x * bytes_per_pixel_);
102 for (int b = bytes_per_pixel_ - 1; b >= 0; b--) {
103 *buffer++ = pixel[b];
107 // DiffInfo utility routines.
108 // These are here so that we don't have to make each DifferText_Xxx_Test
109 // class a friend class to Differ.
111 // Clear out the entire |diff_info_| buffer.
112 void ClearDiffInfo() {
113 memset(differ_->diff_info_.get(), 0, differ_->diff_info_size_);
116 // Get the value in the |diff_info_| array at (x,y).
117 DiffInfo GetDiffInfo(int x, int y) {
118 DiffInfo* diff_info = differ_->diff_info_.get();
119 return diff_info[(y * GetDiffInfoWidth()) + x];
122 // Width of |diff_info_| array.
123 int GetDiffInfoWidth() {
124 return differ_->diff_info_width_;
127 // Height of |diff_info_| array.
128 int GetDiffInfoHeight() {
129 return differ_->diff_info_height_;
132 // Size of |diff_info_| array.
133 int GetDiffInfoSize() {
134 return differ_->diff_info_size_;
137 void SetDiffInfo(int x, int y, const DiffInfo& value) {
138 DiffInfo* diff_info = differ_->diff_info_.get();
139 diff_info[(y * GetDiffInfoWidth()) + x] = value;
142 // Mark the range of blocks specified.
143 void MarkBlocks(int x_origin, int y_origin, int width, int height) {
144 for (int y = 0; y < height; y++) {
145 for (int x = 0; x < width; x++) {
146 SetDiffInfo(x_origin + x, y_origin + y, 1);
151 // Verify that |region| contains a rectangle defined by |x|, |y|, |width| and
153 // |x|, |y|, |width| and |height| are specified in block (not pixel) units.
154 bool CheckDirtyRegionContainsRect(const DesktopRegion& region,
156 int width, int height) {
158 DesktopRect::MakeXYWH(x * kBlockSize, y * kBlockSize,
159 width * kBlockSize, height * kBlockSize);
160 for (DesktopRegion::Iterator i(region); !i.IsAtEnd(); i.Advance()) {
161 if (i.rect().equals(r))
167 // Mark the range of blocks specified and then verify that they are
169 // Only one rectangular region of blocks can be checked with this routine.
170 bool MarkBlocksAndCheckMerge(int x_origin, int y_origin,
171 int width, int height) {
173 MarkBlocks(x_origin, y_origin, width, height);
179 DesktopRect expected_rect = DesktopRect::MakeXYWH(
180 x_origin * kBlockSize, y_origin * kBlockSize,
181 width * kBlockSize, height * kBlockSize);
183 // Verify that the region contains expected_rect and it's the only
185 DesktopRegion::Iterator it(dirty);
186 return !it.IsAtEnd() && expected_rect.equals(it.rect()) &&
187 (it.Advance(), it.IsAtEnd());
190 // The differ class we're testing.
191 scoped_ptr<Differ> differ_;
193 // Screen/buffer info.
196 int bytes_per_pixel_;
199 // Size of each screen buffer.
202 // Previous and current screen buffers.
203 scoped_ptr<uint8_t[]> prev_;
204 scoped_ptr<uint8_t[]> curr_;
207 DISALLOW_COPY_AND_ASSIGN(DifferTest);
210 TEST_F(DifferTest, Setup) {
211 InitDiffer(kScreenWidth, kScreenHeight);
212 // 96x96 pixels results in 3x3 array. Add 1 to each dimension as boundary.
215 // +---+---+---+---+ o = blocks mapped to screen pixels
217 // +---+---+---+---+ _ = boundary blocks
222 EXPECT_EQ(4, GetDiffInfoWidth());
223 EXPECT_EQ(4, GetDiffInfoHeight());
224 EXPECT_EQ(16, GetDiffInfoSize());
227 TEST_F(DifferTest, MarkDirtyBlocks_All) {
228 InitDiffer(kScreenWidth, kScreenHeight);
231 // Update a pixel in each block.
232 for (int y = 0; y < GetDiffInfoHeight() - 1; y++) {
233 for (int x = 0; x < GetDiffInfoWidth() - 1; x++) {
234 WriteBlockPixel(curr_.get(), x, y, 10, 10, 0xff00ff);
238 MarkDirtyBlocks(prev_.get(), curr_.get());
240 // Make sure each block is marked as dirty.
241 for (int y = 0; y < GetDiffInfoHeight() - 1; y++) {
242 for (int x = 0; x < GetDiffInfoWidth() - 1; x++) {
243 EXPECT_EQ(1, GetDiffInfo(x, y))
244 << "when x = " << x << ", and y = " << y;
249 TEST_F(DifferTest, MarkDirtyBlocks_Sampling) {
250 InitDiffer(kScreenWidth, kScreenHeight);
253 // Update some pixels in image.
254 WriteBlockPixel(curr_.get(), 1, 0, 10, 10, 0xff00ff);
255 WriteBlockPixel(curr_.get(), 2, 1, 10, 10, 0xff00ff);
256 WriteBlockPixel(curr_.get(), 0, 2, 10, 10, 0xff00ff);
258 MarkDirtyBlocks(prev_.get(), curr_.get());
260 // Make sure corresponding blocks are updated.
261 EXPECT_EQ(0, GetDiffInfo(0, 0));
262 EXPECT_EQ(0, GetDiffInfo(0, 1));
263 EXPECT_EQ(1, GetDiffInfo(0, 2));
264 EXPECT_EQ(1, GetDiffInfo(1, 0));
265 EXPECT_EQ(0, GetDiffInfo(1, 1));
266 EXPECT_EQ(0, GetDiffInfo(1, 2));
267 EXPECT_EQ(0, GetDiffInfo(2, 0));
268 EXPECT_EQ(1, GetDiffInfo(2, 1));
269 EXPECT_EQ(0, GetDiffInfo(2, 2));
272 TEST_F(DifferTest, DiffBlock) {
273 InitDiffer(kScreenWidth, kScreenHeight);
275 // Verify no differences at start.
276 EXPECT_EQ(0, DiffBlock(0, 0));
277 EXPECT_EQ(0, DiffBlock(1, 1));
279 // Write new data into the 4 corners of the middle block and verify that
280 // neighboring blocks are not affected.
281 int max = kBlockSize - 1;
282 WriteBlockPixel(curr_.get(), 1, 1, 0, 0, 0xffffff);
283 WriteBlockPixel(curr_.get(), 1, 1, 0, max, 0xffffff);
284 WriteBlockPixel(curr_.get(), 1, 1, max, 0, 0xffffff);
285 WriteBlockPixel(curr_.get(), 1, 1, max, max, 0xffffff);
286 EXPECT_EQ(0, DiffBlock(0, 0));
287 EXPECT_EQ(0, DiffBlock(0, 1));
288 EXPECT_EQ(0, DiffBlock(0, 2));
289 EXPECT_EQ(0, DiffBlock(1, 0));
290 EXPECT_EQ(1, DiffBlock(1, 1)); // Only this block should change.
291 EXPECT_EQ(0, DiffBlock(1, 2));
292 EXPECT_EQ(0, DiffBlock(2, 0));
293 EXPECT_EQ(0, DiffBlock(2, 1));
294 EXPECT_EQ(0, DiffBlock(2, 2));
297 TEST_F(DifferTest, Partial_Setup) {
298 InitDiffer(kPartialScreenWidth, kPartialScreenHeight);
299 // 70x70 pixels results in 3x3 array: 2x2 full blocks + partials around
300 // the edge. One more is added to each dimension as a boundary.
303 // +---+---+---+---+ o = blocks mapped to screen pixels
305 // +---+---+---+---+ + = partial blocks (top/left mapped to screen pixels)
307 // +---+---+---+---+ _ = boundary blocks
310 EXPECT_EQ(4, GetDiffInfoWidth());
311 EXPECT_EQ(4, GetDiffInfoHeight());
312 EXPECT_EQ(16, GetDiffInfoSize());
315 TEST_F(DifferTest, Partial_FirstPixel) {
316 InitDiffer(kPartialScreenWidth, kPartialScreenHeight);
319 // Update the first pixel in each block.
320 for (int y = 0; y < GetDiffInfoHeight() - 1; y++) {
321 for (int x = 0; x < GetDiffInfoWidth() - 1; x++) {
322 WriteBlockPixel(curr_.get(), x, y, 0, 0, 0xff00ff);
326 MarkDirtyBlocks(prev_.get(), curr_.get());
328 // Make sure each block is marked as dirty.
329 for (int y = 0; y < GetDiffInfoHeight() - 1; y++) {
330 for (int x = 0; x < GetDiffInfoWidth() - 1; x++) {
331 EXPECT_EQ(1, GetDiffInfo(x, y))
332 << "when x = " << x << ", and y = " << y;
337 TEST_F(DifferTest, Partial_BorderPixel) {
338 InitDiffer(kPartialScreenWidth, kPartialScreenHeight);
341 // Update the right/bottom border pixels.
342 for (int y = 0; y < height_; y++) {
343 WritePixel(curr_.get(), width_ - 1, y, 0xff00ff);
345 for (int x = 0; x < width_; x++) {
346 WritePixel(curr_.get(), x, height_ - 1, 0xff00ff);
349 MarkDirtyBlocks(prev_.get(), curr_.get());
351 // Make sure last (partial) block in each row/column is marked as dirty.
352 int x_last = GetDiffInfoWidth() - 2;
353 for (int y = 0; y < GetDiffInfoHeight() - 1; y++) {
354 EXPECT_EQ(1, GetDiffInfo(x_last, y))
355 << "when x = " << x_last << ", and y = " << y;
357 int y_last = GetDiffInfoHeight() - 2;
358 for (int x = 0; x < GetDiffInfoWidth() - 1; x++) {
359 EXPECT_EQ(1, GetDiffInfo(x, y_last))
360 << "when x = " << x << ", and y = " << y_last;
362 // All other blocks are clean.
363 for (int y = 0; y < GetDiffInfoHeight() - 2; y++) {
364 for (int x = 0; x < GetDiffInfoWidth() - 2; x++) {
365 EXPECT_EQ(0, GetDiffInfo(x, y)) << "when x = " << x << ", and y = " << y;
370 TEST_F(DifferTest, MergeBlocks_Empty) {
371 InitDiffer(kScreenWidth, kScreenHeight);
388 EXPECT_TRUE(dirty.is_empty());
391 TEST_F(DifferTest, MergeBlocks_SingleBlock) {
392 InitDiffer(kScreenWidth, kScreenHeight);
393 // Mark a single block and make sure that there is a single merged
394 // rect with the correct bounds.
395 for (int y = 0; y < GetDiffInfoHeight() - 1; y++) {
396 for (int x = 0; x < GetDiffInfoWidth() - 1; x++) {
397 ASSERT_TRUE(MarkBlocksAndCheckMerge(x, y, 1, 1)) << "x: " << x
403 TEST_F(DifferTest, MergeBlocks_BlockRow) {
404 InitDiffer(kScreenWidth, kScreenHeight);
415 ASSERT_TRUE(MarkBlocksAndCheckMerge(0, 0, 2, 1));
426 ASSERT_TRUE(MarkBlocksAndCheckMerge(0, 1, 3, 1));
437 ASSERT_TRUE(MarkBlocksAndCheckMerge(1, 2, 2, 1));
440 TEST_F(DifferTest, MergeBlocks_BlockColumn) {
441 InitDiffer(kScreenWidth, kScreenHeight);
452 ASSERT_TRUE(MarkBlocksAndCheckMerge(0, 0, 1, 2));
463 ASSERT_TRUE(MarkBlocksAndCheckMerge(1, 1, 1, 2));
474 ASSERT_TRUE(MarkBlocksAndCheckMerge(2, 0, 1, 3));
477 TEST_F(DifferTest, MergeBlocks_BlockRect) {
478 InitDiffer(kScreenWidth, kScreenHeight);
489 ASSERT_TRUE(MarkBlocksAndCheckMerge(0, 0, 2, 2));
500 ASSERT_TRUE(MarkBlocksAndCheckMerge(1, 1, 2, 2));
511 ASSERT_TRUE(MarkBlocksAndCheckMerge(1, 0, 2, 3));
522 ASSERT_TRUE(MarkBlocksAndCheckMerge(0, 1, 3, 2));
533 ASSERT_TRUE(MarkBlocksAndCheckMerge(0, 0, 3, 3));
536 // This tests marked regions that require more than 1 single dirty rect.
537 // The exact rects returned depend on the current implementation, so these
538 // may need to be updated if we modify how we merge blocks.
539 TEST_F(DifferTest, MergeBlocks_MultiRect) {
540 InitDiffer(kScreenWidth, kScreenHeight);
543 // +---+---+---+---+ +---+---+---+
544 // | | X | | _ | | | 0 | |
545 // +---+---+---+---+ +---+---+---+
546 // | X | | | _ | | 1 | | |
547 // +---+---+---+---+ => +---+---+---+
548 // | | | X | _ | | | | 2 |
549 // +---+---+---+---+ +---+---+---+
553 MarkBlocks(1, 0, 1, 1);
554 MarkBlocks(0, 1, 1, 1);
555 MarkBlocks(2, 2, 1, 1);
560 ASSERT_EQ(3, RegionRectCount(dirty));
561 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 1, 0, 1, 1));
562 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 0, 1, 1, 1));
563 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 2, 2, 1, 1));
565 // +---+---+---+---+ +---+---+---+
566 // | | | X | _ | | | | 0 |
567 // +---+---+---+---+ +---+---+---+
568 // | X | X | X | _ | | 1 1 1 |
569 // +---+---+---+---+ => + +
570 // | X | X | X | _ | | 1 1 1 |
571 // +---+---+---+---+ +---+---+---+
575 MarkBlocks(2, 0, 1, 1);
576 MarkBlocks(0, 1, 3, 2);
581 ASSERT_EQ(2, RegionRectCount(dirty));
582 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 2, 0, 1, 1));
583 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 0, 1, 3, 2));
585 // +---+---+---+---+ +---+---+---+
586 // | | | | _ | | | | |
587 // +---+---+---+---+ +---+---+---+
588 // | X | | X | _ | | 0 | | 1 |
589 // +---+---+---+---+ => +---+---+---+
590 // | X | X | X | _ | | 2 2 2 |
591 // +---+---+---+---+ +---+---+---+
595 MarkBlocks(0, 1, 1, 1);
596 MarkBlocks(2, 1, 1, 1);
597 MarkBlocks(0, 2, 3, 1);
602 ASSERT_EQ(3, RegionRectCount(dirty));
603 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 0, 1, 1, 1));
604 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 2, 1, 1, 1));
605 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 0, 2, 3, 1));
607 // +---+---+---+---+ +---+---+---+
608 // | X | X | X | _ | | 0 0 0 |
609 // +---+---+---+---+ +---+---+---+
610 // | X | | X | _ | | 1 | | 2 |
611 // +---+---+---+---+ => +---+---+---+
612 // | X | X | X | _ | | 3 3 3 |
613 // +---+---+---+---+ +---+---+---+
617 MarkBlocks(0, 0, 3, 1);
618 MarkBlocks(0, 1, 1, 1);
619 MarkBlocks(2, 1, 1, 1);
620 MarkBlocks(0, 2, 3, 1);
625 ASSERT_EQ(4, RegionRectCount(dirty));
626 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 0, 0, 3, 1));
627 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 0, 1, 1, 1));
628 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 2, 1, 1, 1));
629 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 0, 2, 3, 1));
631 // +---+---+---+---+ +---+---+---+
632 // | X | X | | _ | | 0 0 | |
633 // +---+---+---+---+ + +---+
634 // | X | X | | _ | | 0 0 | |
635 // +---+---+---+---+ => +---+---+---+
636 // | | X | | _ | | | 1 | |
637 // +---+---+---+---+ +---+---+---+
641 MarkBlocks(0, 0, 2, 2);
642 MarkBlocks(1, 2, 1, 1);
647 ASSERT_EQ(2, RegionRectCount(dirty));
648 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 0, 0, 2, 2));
649 ASSERT_TRUE(CheckDirtyRegionContainsRect(dirty, 1, 2, 1, 1));
652 } // namespace webrtc