| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/video/capture/screen/differ.h" | 5 #include "media/video/capture/screen/differ.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "media/video/capture/screen/differ_block.h" | 8 #include "media/video/capture/screen/differ_block.h" |
| 9 | 9 |
| 10 namespace media { | 10 namespace media { |
| 11 | 11 |
| 12 Differ::Differ(int width, int height, int bpp, int stride) { | 12 Differ::Differ(int width, int height, int bpp, int stride) { |
| 13 // Dimensions of screen. | 13 // Dimensions of screen. |
| 14 width_ = width; | 14 width_ = width; |
| 15 height_ = height; | 15 height_ = height; |
| 16 bytes_per_pixel_ = bpp; | 16 bytes_per_pixel_ = bpp; |
| 17 bytes_per_row_ = stride; | 17 bytes_per_row_ = stride; |
| 18 | 18 |
| 19 // Calc number of blocks (full and partial) required to cover entire image. | 19 // Calc number of blocks (full and partial) required to cover entire image. |
| 20 // One additional row/column is added as a boundary on the right & bottom. | 20 // One additional row/column is added as a boundary on the right & bottom. |
| 21 diff_info_width_ = ((width_ + kBlockSize - 1) / kBlockSize) + 1; | 21 diff_info_width_ = ((width_ + kBlockSize - 1) / kBlockSize) + 1; |
| 22 diff_info_height_ = ((height_ + kBlockSize - 1) / kBlockSize) + 1; | 22 diff_info_height_ = ((height_ + kBlockSize - 1) / kBlockSize) + 1; |
| 23 diff_info_size_ = diff_info_width_ * diff_info_height_ * sizeof(DiffInfo); | 23 diff_info_size_ = diff_info_width_ * diff_info_height_ * sizeof(DiffInfo); |
| 24 diff_info_.reset(new DiffInfo[diff_info_size_]); | 24 diff_info_.reset(new DiffInfo[diff_info_size_]); |
| 25 } | 25 } |
| 26 | 26 |
| 27 Differ::~Differ() {} | 27 Differ::~Differ() {} |
| 28 | 28 |
| 29 void Differ::CalcDirtyRegion(const void* prev_buffer, const void* curr_buffer, | 29 void Differ::CalcDirtyRegion(const void* prev_buffer, const void* curr_buffer, |
| 30 SkRegion* region) { | 30 webrtc::DesktopRegion* region) { |
| 31 if (!region) { | |
| 32 return; | |
| 33 } | |
| 34 region->setEmpty(); | |
| 35 | |
| 36 if (!prev_buffer || !curr_buffer) { | |
| 37 return; | |
| 38 } | |
| 39 | |
| 40 // Identify all the blocks that contain changed pixels. | 31 // Identify all the blocks that contain changed pixels. |
| 41 MarkDirtyBlocks(prev_buffer, curr_buffer); | 32 MarkDirtyBlocks(prev_buffer, curr_buffer); |
| 42 | 33 |
| 43 // Now that we've identified the blocks that have changed, merge adjacent | 34 // Now that we've identified the blocks that have changed, merge adjacent |
| 44 // blocks to minimize the number of rects that we return. | 35 // blocks to minimize the number of rects that we return. |
| 45 MergeBlocks(region); | 36 MergeBlocks(region); |
| 46 } | 37 } |
| 47 | 38 |
| 48 void Differ::MarkDirtyBlocks(const void* prev_buffer, const void* curr_buffer) { | 39 void Differ::MarkDirtyBlocks(const void* prev_buffer, const void* curr_buffer) { |
| 49 memset(diff_info_.get(), 0, diff_info_size_); | 40 memset(diff_info_.get(), 0, diff_info_size_); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 int width_bytes = width * bytes_per_pixel_; | 115 int width_bytes = width * bytes_per_pixel_; |
| 125 for (int y = 0; y < height; y++) { | 116 for (int y = 0; y < height; y++) { |
| 126 if (memcmp(prev_buffer, curr_buffer, width_bytes) != 0) | 117 if (memcmp(prev_buffer, curr_buffer, width_bytes) != 0) |
| 127 return 1; | 118 return 1; |
| 128 prev_buffer += bytes_per_row_; | 119 prev_buffer += bytes_per_row_; |
| 129 curr_buffer += bytes_per_row_; | 120 curr_buffer += bytes_per_row_; |
| 130 } | 121 } |
| 131 return 0; | 122 return 0; |
| 132 } | 123 } |
| 133 | 124 |
| 134 void Differ::MergeBlocks(SkRegion* region) { | 125 void Differ::MergeBlocks(webrtc::DesktopRegion* region) { |
| 135 DCHECK(region); | 126 region->Clear(); |
| 136 region->setEmpty(); | |
| 137 | 127 |
| 138 uint8* diff_info_row_start = static_cast<uint8*>(diff_info_.get()); | 128 uint8* diff_info_row_start = static_cast<uint8*>(diff_info_.get()); |
| 139 int diff_info_stride = diff_info_width_ * sizeof(DiffInfo); | 129 int diff_info_stride = diff_info_width_ * sizeof(DiffInfo); |
| 140 | 130 |
| 141 for (int y = 0; y < diff_info_height_; y++) { | 131 for (int y = 0; y < diff_info_height_; y++) { |
| 142 uint8* diff_info = diff_info_row_start; | 132 uint8* diff_info = diff_info_row_start; |
| 143 for (int x = 0; x < diff_info_width_; x++) { | 133 for (int x = 0; x < diff_info_width_; x++) { |
| 144 if (*diff_info != 0) { | 134 if (*diff_info != 0) { |
| 145 // We've found a modified block. Look at blocks to the right and below | 135 // We've found a modified block. Look at blocks to the right and below |
| 146 // to group this block with as many others as we can. | 136 // to group this block with as many others as we can. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 | 178 |
| 189 // Add rect to list of dirty rects. | 179 // Add rect to list of dirty rects. |
| 190 width *= kBlockSize; | 180 width *= kBlockSize; |
| 191 if (left + width > width_) { | 181 if (left + width > width_) { |
| 192 width = width_ - left; | 182 width = width_ - left; |
| 193 } | 183 } |
| 194 height *= kBlockSize; | 184 height *= kBlockSize; |
| 195 if (top + height > height_) { | 185 if (top + height > height_) { |
| 196 height = height_ - top; | 186 height = height_ - top; |
| 197 } | 187 } |
| 198 region->op(SkIRect::MakeXYWH(left, top, width, height), | 188 region->AddRect( |
| 199 SkRegion::kUnion_Op); | 189 webrtc::DesktopRect::MakeXYWH(left, top, width, height)); |
| 200 } | 190 } |
| 201 | 191 |
| 202 // Increment to next block in this row. | 192 // Increment to next block in this row. |
| 203 diff_info++; | 193 diff_info++; |
| 204 } | 194 } |
| 205 | 195 |
| 206 // Go to start of next row. | 196 // Go to start of next row. |
| 207 diff_info_row_start += diff_info_stride; | 197 diff_info_row_start += diff_info_stride; |
| 208 } | 198 } |
| 209 } | 199 } |
| 210 | 200 |
| 211 } // namespace media | 201 } // namespace media |
| OLD | NEW |