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

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

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

Powered by Google App Engine
This is Rietveld 408576698