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

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: Sync latest changes Created 4 years, 3 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 // 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 [|left|, |right|), in a row in the
46 // range of [|top|, |top| + |height|), starts from |old_buffer| and
47 // |new_buffer|, and outputs updated regions into |output|. |stride| is the
48 // DesktopFrame::stride().
49 void CompareRow(const uint8_t* old_buffer,
50 const uint8_t* new_buffer,
51 const int left,
52 const int right,
53 const int top,
54 const int bottom,
55 const int stride,
56 DesktopRegion* const output) {
57 const int block_x_offset = kBlockSize * DesktopFrame::kBytesPerPixel;
58 const int width = right - left;
59 const int height = bottom - top;
60 const int block_count = (width - 1) / kBlockSize;
61 const int last_block_width = width - block_count * kBlockSize;
62 RTC_DCHECK(last_block_width <= kBlockSize && last_block_width > 0);
63
64 // The first block-column in a continuous dirty area in current block-row.
65 int first_dirty_x_block = -1;
66
67 // We always need to add dirty area into |output| in the last block, so handle
68 // it separatedly.
69 for (int x = 0; x < block_count; x++) {
70 if (BlockDifference(old_buffer, new_buffer, height, stride)) {
71 if (first_dirty_x_block == -1) {
72 // This is the first dirty block in a continuous dirty area.
73 first_dirty_x_block = x;
74 }
75 } else if (first_dirty_x_block != -1) {
76 // The block on the left is the last dirty block in a continuous
77 // dirty area.
78 output->AddRect(DesktopRect::MakeLTRB(
79 first_dirty_x_block * kBlockSize + left, top,
80 x * kBlockSize + left, bottom));
81 first_dirty_x_block = -1;
82 }
83 old_buffer += block_x_offset;
84 new_buffer += block_x_offset;
85 }
86
87 bool last_block_diff;
88 if (last_block_width < kBlockSize) {
89 // The last one is a partial vector.
90 last_block_diff = PartialBlockDifference(
91 old_buffer, new_buffer, last_block_width, height, stride);
92 } else {
93 last_block_diff =
94 BlockDifference(old_buffer, new_buffer, height, stride);
95 }
96 if (last_block_diff) {
97 if (first_dirty_x_block == -1) {
98 first_dirty_x_block = block_count;
99 }
100 output->AddRect(
101 DesktopRect::MakeLTRB(first_dirty_x_block * kBlockSize + left,
102 top, right, bottom));
103 } else if (first_dirty_x_block != -1) {
104 output->AddRect(DesktopRect::MakeLTRB(
105 first_dirty_x_block * kBlockSize + left, top,
106 block_count * kBlockSize + left, bottom));
107 }
108 }
109
110 // Compares |rect| area in |old_frame| and |new_frame|, and outputs dirty
111 // regions into |output|.
112 void CompareFrames(const DesktopFrame& old_frame,
113 const DesktopFrame& new_frame,
114 DesktopRect rect,
115 DesktopRegion* const output) {
116 RTC_DCHECK(old_frame.size().equals(new_frame.size()));
117 RTC_DCHECK_EQ(old_frame.stride(), new_frame.stride());
118 rect.IntersectWith(DesktopRect::MakeSize(old_frame.size()));
119
120 const int y_block_count = (rect.height() - 1) / kBlockSize;
121 const int last_y_block_height = rect.height() - y_block_count * kBlockSize;
122 // Offset from the start of one block-row to the next.
123 const int block_y_stride = old_frame.stride() * kBlockSize;
124 const uint8_t* prev_block_row_start =
125 old_frame.GetFrameDataAtPos(rect.top_left());
126 const uint8_t* curr_block_row_start =
127 new_frame.GetFrameDataAtPos(rect.top_left());
128
129 int top = rect.top();
130 // The last row may have a different height, so we handle it separately.
131 for (int y = 0; y < y_block_count; y++) {
132 CompareRow(prev_block_row_start, curr_block_row_start, rect.left(),
133 rect.right(), top, top + kBlockSize,
134 old_frame.stride(), output);
135 top += kBlockSize;
136 prev_block_row_start += block_y_stride;
137 curr_block_row_start += block_y_stride;
138 }
139 CompareRow(prev_block_row_start, curr_block_row_start, rect.left(),
140 rect.right(), top, top + last_y_block_height,
141 old_frame.stride(), output);
142 }
143
144 } // namespace
145
146 ScreenCapturerDifferWrapper::ScreenCapturerDifferWrapper(
147 std::unique_ptr<ScreenCapturer> base_capturer)
148 : base_capturer_(std::move(base_capturer)) {
149 RTC_DCHECK(base_capturer_);
150 }
151
152 ScreenCapturerDifferWrapper::~ScreenCapturerDifferWrapper() {}
153
154 void ScreenCapturerDifferWrapper::Start(DesktopCapturer::Callback* callback) {
155 callback_ = callback;
156 base_capturer_->Start(this);
157 }
158
159 void ScreenCapturerDifferWrapper::SetSharedMemoryFactory(
160 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) {
161 base_capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory));
162 }
163
164 void ScreenCapturerDifferWrapper::Capture(const DesktopRegion& region) {
165 base_capturer_->Capture(region);
166 }
167
168 bool ScreenCapturerDifferWrapper::GetScreenList(ScreenList* screens) {
169 return base_capturer_->GetScreenList(screens);
170 }
171
172 bool ScreenCapturerDifferWrapper::SelectScreen(ScreenId id) {
173 return base_capturer_->SelectScreen(id);
174 }
175
176 void ScreenCapturerDifferWrapper::OnCaptureResult(
177 Result result,
178 std::unique_ptr<DesktopFrame> input_frame) {
179 int64_t start_time_nanos = rtc::TimeNanos();
180 if (!input_frame) {
181 callback_->OnCaptureResult(result, nullptr);
182 return;
183 }
184 RTC_DCHECK(result == Result::SUCCESS);
185
186 std::unique_ptr<SharedDesktopFrame> frame =
187 SharedDesktopFrame::Wrap(std::move(input_frame));
188 if (last_frame_ &&
189 (last_frame_->size().width() != frame->size().width() ||
190 last_frame_->size().height() != frame->size().height() ||
191 last_frame_->stride() != frame->stride())) {
192 last_frame_.reset();
193 }
194
195 if (last_frame_) {
196 DesktopRegion hints;
197 hints.Swap(frame->GetUnderlyingFrame()->mutable_updated_region());
198 for (DesktopRegion::Iterator it(hints); !it.IsAtEnd(); it.Advance()) {
199 CompareFrames(*last_frame_, *frame, it.rect(),
200 frame->mutable_updated_region());
201 }
202 } else {
203 frame->mutable_updated_region()->SetRect(
204 DesktopRect::MakeSize(frame->size()));
205 }
206 last_frame_ = frame->Share();
207
208 frame->set_capture_time_ms(frame->GetUnderlyingFrame()->capture_time_ms() +
209 (rtc::TimeNanos() - start_time_nanos) /
210 rtc::kNumNanosecsPerMillisec);
211 callback_->OnCaptureResult(result, std::move(frame));
212 }
213
214 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698