 Chromium Code Reviews
 Chromium Code Reviews Issue 2202443002:
  [WebRTC] Add ScreenCapturerDifferWrapper to share Differ across ScreenCapturers  (Closed) 
  Base URL: https://chromium.googlesource.com/external/webrtc.git@master
    
  
    Issue 2202443002:
  [WebRTC] Add ScreenCapturerDifferWrapper to share Differ across ScreenCapturers  (Closed) 
  Base URL: https://chromium.googlesource.com/external/webrtc.git@master| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2016 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 "webrtc/modules/desktop_capture/screen_capturer_differ_wrapper.h" | |
| 12 | |
| 13 #include <algorithm> | |
| 14 #include <utility> | |
| 15 | |
| 16 #include "webrtc/base/checks.h" | |
| 17 #include "webrtc/base/timeutils.h" | |
| 18 #include "webrtc/modules/desktop_capture/desktop_geometry.h" | |
| 19 #include "webrtc/modules/desktop_capture/differ_block.h" | |
| 20 | |
| 21 namespace webrtc { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Returns true if (0, 0) - (|width|, |height|) vector in |old_buffer| and | |
| 26 // |new_buffer| are equal. |width| should be less than 32 | |
| 27 // (defined by kBlockSize), otherwise BlockDifference() should be used. | |
| 28 bool PartialBlockDifference(const uint8_t* old_buffer, | |
| 29 const uint8_t* new_buffer, | |
| 30 int width, | |
| 31 int height, | |
| 32 int stride) { | |
| 33 RTC_DCHECK_LT(width, kBlockSize); | |
| 34 const int width_bytes = width * DesktopFrame::kBytesPerPixel; | |
| 35 for (int i = 0; i < height; i++) { | |
| 36 if (memcmp(old_buffer, new_buffer, width_bytes) != 0) { | |
| 37 return true; | |
| 38 } | |
| 39 old_buffer += stride; | |
| 40 new_buffer += stride; | |
| 41 } | |
| 42 return false; | |
| 43 } | |
| 44 | |
| 45 // Compares columns in the range of [|column_left|, | |
| 46 // |column_left| + |block_count| * kBlockSize + |last_block_width|), in a row in | |
| 47 // the range of [row_top, row_top + row_height), starts from |old_buffer| and | |
| 48 // |new_buffer|, and outputs updated regions into |output|. |stride| is the | |
| 49 // DesktopFrame::stride(). | |
| 50 void CompareRow(const uint8_t* old_buffer, | |
| 51 const uint8_t* new_buffer, | |
| 52 const int column_left, | |
| 53 const int block_count, | |
| 54 const int last_block_width, | |
| 55 const int row_top, | |
| 56 const int row_height, | |
| 57 const int stride, | |
| 58 DesktopRegion* const output) { | |
| 59 RTC_DCHECK(last_block_width <= kBlockSize && last_block_width > 0); | |
| 60 const int block_x_offset = kBlockSize * DesktopFrame::kBytesPerPixel; | |
| 61 const int row_bottom = row_top + row_height; | |
| 62 const int column_right = | |
| 63 column_left + block_count * kBlockSize + last_block_width; | |
| 64 | |
| 65 // The first block-column in a continuous dirty area in current block-row. | |
| 66 int first_dirty_x_block = -1; | |
| 67 | |
| 68 // We always need to add dirty area into |output| in the last block, so handle | |
| 69 // it separatedly. | |
| 70 for (int x = 0; x < block_count; x++) { | |
| 71 if (BlockDifference(old_buffer, new_buffer, row_height, stride)) { | |
| 72 if (first_dirty_x_block == -1) { | |
| 73 // This is the first dirty block in a continuous dirty area. | |
| 74 first_dirty_x_block = x; | |
| 75 } | |
| 76 } else if (first_dirty_x_block != -1) { | |
| 77 // The block on the left is the last dirty block in a continuous | |
| 78 // dirty area. | |
| 79 output->AddRect(DesktopRect::MakeLTRB( | |
| 80 first_dirty_x_block * kBlockSize + column_left, row_top, | |
| 81 x * kBlockSize + column_left, row_bottom)); | |
| 82 first_dirty_x_block = -1; | |
| 83 } | |
| 84 old_buffer += block_x_offset; | |
| 85 new_buffer += block_x_offset; | |
| 86 } | |
| 87 | |
| 88 bool last_block_diff; | |
| 89 if (last_block_width < kBlockSize) { | |
| 90 // The last one is a partial vector. | |
| 91 last_block_diff = PartialBlockDifference( | |
| 92 old_buffer, new_buffer, last_block_width, row_height, stride); | |
| 93 } else { | |
| 94 last_block_diff = | |
| 95 BlockDifference(old_buffer, new_buffer, row_height, stride); | |
| 96 } | |
| 97 if (last_block_diff) { | |
| 98 if (first_dirty_x_block == -1) { | |
| 
Sergey Ulanov
2016/09/02 19:23:03
if (first_dirty_x_block == -1)
  first_dirty_x_blo
 
Hzj_jie
2016/09/02 21:46:30
Done.
 | |
| 99 output->AddRect( | |
| 100 DesktopRect::MakeLTRB(column_left + block_count * kBlockSize, row_top, | |
| 101 column_right, row_bottom)); | |
| 102 } else { | |
| 103 output->AddRect( | |
| 104 DesktopRect::MakeLTRB(first_dirty_x_block * kBlockSize + column_left, | |
| 105 row_top, column_right, row_bottom)); | |
| 106 } | |
| 107 } else if (first_dirty_x_block != -1) { | |
| 108 output->AddRect(DesktopRect::MakeLTRB( | |
| 109 first_dirty_x_block * kBlockSize + column_left, row_top, | |
| 110 block_count * kBlockSize + column_left, row_bottom)); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 // Compares |rect| area in |old_frame| and |new_frame|, and outputs dirty | |
| 115 // regions into |output|. | |
| 116 void CompareFrames(const DesktopFrame& old_frame, | |
| 117 const DesktopFrame& new_frame, | |
| 118 DesktopRect rect, | |
| 119 DesktopRegion* const output) { | |
| 120 RTC_DCHECK(old_frame.size().equals(new_frame.size())); | |
| 121 RTC_DCHECK_EQ(old_frame.stride(), new_frame.stride()); | |
| 122 rect.IntersectWith(DesktopRect::MakeSize(old_frame.size())); | |
| 123 | |
| 124 const int x_block_count = (rect.width() - 1) / kBlockSize; | |
| 125 const int last_x_block_width = rect.width() - x_block_count * kBlockSize; | |
| 126 const int y_block_count = (rect.height() - 1) / kBlockSize; | |
| 127 const int last_y_block_height = rect.height() - y_block_count * kBlockSize; | |
| 128 // Offset from the start of one block-row to the next. | |
| 129 const int block_y_stride = old_frame.stride() * kBlockSize; | |
| 130 const uint8_t* prev_block_row_start = | |
| 131 old_frame.GetFrameDataAtPos(rect.top_left()); | |
| 132 const uint8_t* curr_block_row_start = | |
| 133 new_frame.GetFrameDataAtPos(rect.top_left()); | |
| 134 | |
| 135 int row_top = rect.top(); | |
| 136 // The last row may have a different height, so we handle it separately. | |
| 137 for (int y = 0; y < y_block_count; y++) { | |
| 138 CompareRow(prev_block_row_start, curr_block_row_start, rect.left(), | |
| 139 x_block_count, last_x_block_width, row_top, kBlockSize, | |
| 140 old_frame.stride(), output); | |
| 141 row_top += kBlockSize; | |
| 142 prev_block_row_start += block_y_stride; | |
| 143 curr_block_row_start += block_y_stride; | |
| 144 } | |
| 145 CompareRow(prev_block_row_start, curr_block_row_start, rect.left(), | |
| 146 x_block_count, last_x_block_width, row_top, last_y_block_height, | |
| 147 old_frame.stride(), output); | |
| 148 } | |
| 149 | |
| 150 } // namespace | |
| 151 | |
| 152 ScreenCapturerDifferWrapper::ScreenCapturerDifferWrapper( | |
| 153 std::unique_ptr<ScreenCapturer> base_capturer) | |
| 154 : base_capturer_(std::move(base_capturer)) { | |
| 155 RTC_DCHECK(base_capturer_); | |
| 156 } | |
| 157 | |
| 158 ScreenCapturerDifferWrapper::~ScreenCapturerDifferWrapper() {} | |
| 159 | |
| 160 void ScreenCapturerDifferWrapper::Start(DesktopCapturer::Callback* callback) { | |
| 161 callback_ = callback; | |
| 162 base_capturer_->Start(this); | |
| 163 } | |
| 164 | |
| 165 void ScreenCapturerDifferWrapper::SetSharedMemoryFactory( | |
| 166 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) { | |
| 167 base_capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory)); | |
| 168 } | |
| 169 | |
| 170 void ScreenCapturerDifferWrapper::Capture(const DesktopRegion& region) { | |
| 171 base_capturer_->Capture(region); | |
| 172 } | |
| 173 | |
| 174 bool ScreenCapturerDifferWrapper::GetScreenList(ScreenList* screens) { | |
| 175 return base_capturer_->GetScreenList(screens); | |
| 176 } | |
| 177 | |
| 178 bool ScreenCapturerDifferWrapper::SelectScreen(ScreenId id) { | |
| 179 return base_capturer_->SelectScreen(id); | |
| 180 } | |
| 181 | |
| 182 void ScreenCapturerDifferWrapper::OnCaptureResult( | |
| 183 Result result, | |
| 184 std::unique_ptr<DesktopFrame> input_frame) { | |
| 185 int64_t start_time_nanos = rtc::TimeNanos(); | |
| 186 if (!input_frame) { | |
| 187 callback_->OnCaptureResult(result, nullptr); | |
| 188 return; | |
| 189 } | |
| 190 | |
| 191 std::unique_ptr<SharedDesktopFrame> frame = | |
| 192 SharedDesktopFrame::Wrap(std::move(input_frame)); | |
| 193 if (result == Result::SUCCESS) { | |
| 
Sergey Ulanov
2016/09/02 19:23:03
I don't think you need this. If input_frame != nul
 
Hzj_jie
2016/09/02 21:46:30
Done.
 | |
| 194 if (last_frame_ && | |
| 195 (last_frame_->size().width() != frame->size().width() || | |
| 196 last_frame_->size().height() != frame->size().height() || | |
| 197 last_frame_->stride() != frame->stride())) { | |
| 198 last_frame_.reset(); | |
| 199 } | |
| 200 | |
| 201 if (last_frame_) { | |
| 202 DesktopRegion hints; | |
| 203 hints.Swap(frame->GetUnderlyingFrame()->mutable_updated_region()); | |
| 204 for (DesktopRegion::Iterator it(hints); !it.IsAtEnd(); it.Advance()) { | |
| 205 CompareFrames(*last_frame_, *frame, it.rect(), | |
| 206 frame->mutable_updated_region()); | |
| 207 } | |
| 208 } else { | |
| 209 frame->mutable_updated_region()->SetRect( | |
| 210 DesktopRect::MakeSize(frame->size())); | |
| 211 } | |
| 212 last_frame_ = frame->Share(); | |
| 213 } else { | |
| 214 last_frame_.reset(); | |
| 215 } | |
| 216 | |
| 217 frame->set_capture_time_ms(frame->GetUnderlyingFrame()->capture_time_ms() + | |
| 218 (rtc::TimeNanos() - start_time_nanos) / | |
| 219 rtc::kNumNanosecsPerMillisec); | |
| 220 callback_->OnCaptureResult(result, std::move(frame)); | |
| 221 } | |
| 222 | |
| 223 } // namespace webrtc | |
| OLD | NEW |