Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: webrtc/modules/desktop_capture/screen_capturer_differ_wrapper.cc

Issue 2202443002: [WebRTC] Add ScreenCapturerDifferWrapper to share Differ across ScreenCapturers (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Disable perf test without SSE2 Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 static const int kBlockXOffset = kBlockSize * DesktopFrame::kBytesPerPixel;
26
27 // Checks whether (0, 0) - (|width|, |height|) area in |prev| and |curr| are
28 // equal. |stride| is used to indicate the bytes count between two rows in |old|
29 // and |new|. |width| or |height| should be less than 32 (defined by
30 // kBlockSize), otherwise BlockDifference() should be used.
Sergey Ulanov 2016/08/12 05:25:51 The comment should make it clear if true result in
Hzj_jie 2016/08/15 00:38:06 Considering Differ class will be removed, I would
31 bool PartialBlockDifference(const uint8_t* prev,
Sergey Ulanov 2016/08/12 05:25:51 Style guide discourages names like 'prev' and 'cur
Hzj_jie 2016/08/15 00:38:06 Done.
32 const uint8_t* curr,
33 int stride,
Sergey Ulanov 2016/08/12 05:25:50 potentially stride may be different for the two bu
Hzj_jie 2016/08/15 00:38:06 These functions are only called from CompareFrames
Sergey Ulanov 2016/08/15 22:04:50 Mention it in screen_capturer_differ_wrapper.h?
Hzj_jie 2016/08/16 07:10:04 Done.
34 int width,
35 int height) {
36 RTC_DCHECK(width < kBlockSize || height < kBlockSize);
Sergey Ulanov 2016/08/12 05:25:50 Why do you need to check it here? Also, I think
Hzj_jie 2016/08/15 00:38:06 This seems not true for NEON (though we have not i
Sergey Ulanov 2016/08/15 22:04:50 Why is it 4x4 blocks? can you point me instruction
Hzj_jie 2016/08/16 07:10:04 Not really, this class will try to use as many blo
37 const int width_bytes = width * DesktopFrame::kBytesPerPixel;
38 for (int y = 0; y < height; y++) {
39 if (memcmp(prev, curr, width_bytes) != 0)
40 return true;
41 prev += stride;
42 curr += stride;
43 }
44
45 return false;
46 }
47
48 // Compares block-columns in the range of [0, |x_end| - |x_start|), in
49 // block-rows start from |prev| and |curr|, and outputs dirty regions into
50 // |output|. |row_top| is the original position of the top of the row in the
51 // DesktopFrame, |width| is the DesktopFrame::size()::width(), |stride| is the
52 // DesktopFrame::stride().
53 void CompareBlockRow(const uint8_t* prev,
54 const uint8_t* curr,
55 const int x_start,
56 const int x_end,
57 const int row_top,
58 const int width,
59 const int stride,
60 DesktopRegion* const output) {
61 const int row_bottom = row_top + kBlockSize;
62 const int row_height = kBlockSize;
63 const int column_right = std::min(x_end * kBlockSize, 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 = x_start; x < x_end - 1; x++) {
71 if (BlockDifference(prev, curr, 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(first_dirty_x_block * kBlockSize,
80 row_top, x * kBlockSize,
81 row_bottom));
82 first_dirty_x_block = -1;
83 }
84 prev += kBlockXOffset;
85 curr += kBlockXOffset;
86 }
87
88 bool last_block_diff;
89 if (x_end * kBlockSize > width) {
90 // The last one is a partial block.
91 last_block_diff = PartialBlockDifference(
92 prev, curr, stride, column_right - (x_end - 1) * kBlockSize,
93 row_height);
94 } else {
95 last_block_diff = BlockDifference(prev, curr, stride);
96 }
97 if (last_block_diff) {
98 if (first_dirty_x_block == -1) {
99 output->AddRect(DesktopRect::MakeLTRB((x_end - 1) * kBlockSize, row_top,
100 column_right, row_bottom));
101 } else {
102 output->AddRect(DesktopRect::MakeLTRB(first_dirty_x_block * kBlockSize,
103 row_top, column_right, row_bottom));
104 }
105 } else if (first_dirty_x_block != -1) {
106 output->AddRect(DesktopRect::MakeLTRB(first_dirty_x_block * kBlockSize,
107 row_top, (x_end - 1) * kBlockSize,
108 row_bottom));
109 }
110 }
111
112 // Compares block-columns in the range of [0, |x_end| - |x_start|), in partial
113 // block-rows start from |prev| and |curr|, and outputs dirty regions into
114 // |output|.
115 void ComparePartialBlockRow(const uint8_t* prev,
Sergey Ulanov 2016/08/12 05:25:50 See my previous comment. I think the optimized blo
Hzj_jie 2016/08/15 00:38:06 I am happy to do so, if we are on the same page fo
116 const uint8_t* curr,
117 const int x_start,
118 const int x_end,
119 const int row_top,
120 const int width,
121 const int height,
122 const int stride,
123 DesktopRegion* const output) {
124 const int row_bottom = height;
125 const int row_height = height - row_top;
126 const int column_right = std::min(x_end * kBlockSize, width);
127
128 // The first block-column in a continuous dirty area in current block-row.
129 int first_dirty_x_block = -1;
130
131 // We always need to add dirty area into |output| in the last block, so handle
132 // it separatedly.
133 for (int x = x_start; x < x_end - 1; x++) {
134 if (PartialBlockDifference(prev, curr, stride, kBlockSize, row_height)) {
135 if (first_dirty_x_block == -1) {
136 // This is the first dirty block in a continuous dirty area.
137 first_dirty_x_block = x;
138 }
139 } else if (first_dirty_x_block != -1) {
140 // The block on the left is the last dirty block in a continuous
141 // dirty area.
142 output->AddRect(DesktopRect::MakeLTRB(first_dirty_x_block * kBlockSize,
143 row_top, x * kBlockSize,
144 row_bottom));
145 first_dirty_x_block = -1;
146 }
147 prev += kBlockXOffset;
148 curr += kBlockXOffset;
149 }
150
151 if (PartialBlockDifference(prev, curr, stride,
152 column_right - (x_end - 1) * kBlockSize,
153 row_height)) {
154 if (first_dirty_x_block == -1) {
155 output->AddRect(DesktopRect::MakeLTRB((x_end - 1) * kBlockSize, row_top,
156 column_right, row_bottom));
157 } else {
158 output->AddRect(DesktopRect::MakeLTRB(first_dirty_x_block * kBlockSize,
159 row_top, column_right, row_bottom));
160 }
161 } else if (first_dirty_x_block != -1) {
162 output->AddRect(DesktopRect::MakeLTRB(first_dirty_x_block * kBlockSize,
163 row_top, (x_end - 1) * kBlockSize,
164 row_bottom));
165 }
166 }
167
168 // Compares |rect| area in |old_frame| and |new_frame|, and outputs dirty
169 // regions into |output|.
170 void CompareFrames(const DesktopFrame& old_frame,
171 const DesktopFrame& new_frame,
172 const DesktopRect rect,
Sergey Ulanov 2016/08/12 05:25:51 DesktopRect&
Hzj_jie 2016/08/15 00:38:06 Done.
173 DesktopRegion* const output) {
174 RTC_DCHECK(old_frame.size().equals(new_frame.size()));
175 RTC_DCHECK(DesktopRect::MakeSize(old_frame.size()).ContainsRect(rect));
176 RTC_DCHECK_EQ(old_frame.stride(), new_frame.stride());
177
178 const int x_start = rect.left() / kBlockSize;
179 const int y_start = rect.top() / kBlockSize;
180 const int x_end = (rect.right() + kBlockSize - 1) / kBlockSize;
181 const int y_end = (rect.bottom() + kBlockSize - 1) / kBlockSize;
182 const DesktopVector block_top_left(x_start * kBlockSize,
183 y_start * kBlockSize);
184
185 // Offset from the start of one block-row to the next.
186 const int block_y_stride = old_frame.stride() * kBlockSize;
187
188 const uint8_t* prev_block_row_start =
189 old_frame.GetFrameDataAtPos(block_top_left);
190 const uint8_t* curr_block_row_start =
191 new_frame.GetFrameDataAtPos(block_top_left);
192
193 int row_top = y_start * kBlockSize;
194 // The last row may be a partial row, so we handle it separately.
195 for (int y = y_start; y < y_end - 1; y++) {
196 CompareBlockRow(prev_block_row_start, curr_block_row_start, x_start, x_end,
197 row_top, old_frame.size().width(), old_frame.stride(),
198 output);
199 row_top += kBlockSize;
200 prev_block_row_start += block_y_stride;
201 curr_block_row_start += block_y_stride;
202 }
203 if (row_top + kBlockSize > old_frame.size().height()) {
204 // The last row is a partial row.
205 ComparePartialBlockRow(prev_block_row_start, curr_block_row_start, x_start,
206 x_end, row_top, old_frame.size().width(),
207 old_frame.size().height(), old_frame.stride(),
208 output);
209 } else {
210 CompareBlockRow(prev_block_row_start, curr_block_row_start, x_start, x_end,
211 row_top, old_frame.size().width(), old_frame.stride(),
212 output);
213 }
214 }
215
216 } // namespace
217
218 ScreenCapturerDifferWrapper::ScreenCapturerDifferWrapper(
219 std::unique_ptr<ScreenCapturer> base_capturer,
220 bool diff_entire_frame)
221 : base_capturer_(std::move(base_capturer)),
222 diff_entire_frame_(diff_entire_frame) {
223 RTC_DCHECK(base_capturer_);
224 }
225
226 ScreenCapturerDifferWrapper::~ScreenCapturerDifferWrapper() = default;
Sergey Ulanov 2016/08/12 05:25:50 {} instead of = default; would be cleaner here
Hzj_jie 2016/08/15 00:38:06 Done.
227
228 void ScreenCapturerDifferWrapper::Start(DesktopCapturer::Callback* callback) {
229 callback_ = callback;
230 base_capturer_->Start(this);
231 }
232
233 void ScreenCapturerDifferWrapper::SetSharedMemoryFactory(
234 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) {
235 base_capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory));
236 }
237
238 void ScreenCapturerDifferWrapper::Capture(const DesktopRegion& region) {
239 base_capturer_->Capture(region);
240 }
241
242 bool ScreenCapturerDifferWrapper::GetScreenList(ScreenList* screens) {
243 return base_capturer_->GetScreenList(screens);
244 }
245
246 bool ScreenCapturerDifferWrapper::SelectScreen(ScreenId id) {
247 return base_capturer_->SelectScreen(id);
248 }
249
250 void ScreenCapturerDifferWrapper::OnCaptureResult(
251 Result result,
252 std::unique_ptr<DesktopFrame> frame) {
253 int64_t start_time_nanos = rtc::TimeNanos();
254 if (result == Result::SUCCESS && frame) {
255 if (last_frame_ &&
256 (last_frame_->size().width() != frame->size().width() ||
257 last_frame_->size().height() != frame->size().height() ||
258 last_frame_->stride() != frame->stride())) {
259 last_frame_.reset();
260 }
261
262 if (last_frame_) {
263 if (diff_entire_frame_) {
264 CompareFrames(*last_frame_, *frame,
265 DesktopRect::MakeSize(frame->size()),
266 frame->mutable_updated_region());
267 } else {
268 DesktopRegion hints;
269 hints.Swap(frame->mutable_updated_region());
270 for (DesktopRegion::Iterator it(hints); !it.IsAtEnd(); it.Advance()) {
271 // Note, here we always expect the underlying capturer won't return a
272 // dirty region which excceeds the DesktopFrame::size().
273 CompareFrames(*last_frame_, *frame, it.rect(),
274 frame->mutable_updated_region());
275 }
276 }
277 } else {
278 frame->mutable_updated_region()->SetRect(
279 DesktopRect::MakeSize(frame->size()));
280 }
281 last_frame_ = static_cast<SharedDesktopFrame*>(frame.get())->Share();
282 }
283
284 frame->set_capture_time_ms(frame->capture_time_ms() +
Sergey Ulanov 2016/08/12 05:25:50 frame may be nullptr here. Handle errors case firs
Hzj_jie 2016/08/15 00:38:06 Done.
285 (rtc::TimeNanos() - start_time_nanos) /
286 rtc::kNumNanosecsPerMillisec);
287 callback_->OnCaptureResult(result, std::move(frame));
288 }
289
290 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698