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 static const int kBlockXOffset = kBlockSize * DesktopFrame::kBytesPerPixel; | |
Sergey Ulanov
2016/09/01 19:27:56
nit: this const is used in only 2 places, so I don
Hzj_jie
2016/09/01 23:26:42
Done.
| |
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(width < kBlockSize); | |
Sergey Ulanov
2016/09/01 19:27:56
RTC_DCHECK_LT
Hzj_jie
2016/09/01 23:26:43
Done.
| |
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 [|column_left|, | |
48 // |column_left| + |block_count| * kBlockSize + |last_block_width|), in a row in | |
49 // the range of [row_top, row_top + row_height), starts from |old_buffer| and | |
50 // |new_buffer|, and outputs updated regions into |output|. |stride| is the | |
51 // DesktopFrame::stride(). | |
52 void CompareRow(const uint8_t* old_buffer, | |
53 const uint8_t* new_buffer, | |
54 const int column_left, | |
55 const int block_count, | |
56 const int last_block_width, | |
Sergey Ulanov
2016/09/01 19:27:56
Instead of passing block_count and last_block_wid
Hzj_jie
2016/09/01 23:26:43
I believe besides the functionality, the first pri
Sergey Ulanov
2016/09/02 19:23:03
You also calculate column_right below, so I don't
Hzj_jie
2016/09/02 21:46:30
Done.
| |
57 const int row_top, | |
58 const int row_height, | |
59 const int stride, | |
60 DesktopRegion* const output) { | |
61 RTC_DCHECK(last_block_width <= kBlockSize && last_block_width > 0); | |
62 const int row_bottom = row_top + row_height; | |
63 const int column_right = | |
64 column_left + block_count * kBlockSize + last_block_width; | |
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, row_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) { | |
Sergey Ulanov
2016/09/01 19:27:56
I don't think it's worth to merge the blocks horiz
Hzj_jie
2016/09/01 23:26:43
By using this merging logic, we can save at least
| |
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 + column_left, row_top, | |
82 x * kBlockSize + column_left, row_bottom)); | |
83 first_dirty_x_block = -1; | |
84 } | |
85 old_buffer += kBlockXOffset; | |
86 new_buffer += kBlockXOffset; | |
87 } | |
88 | |
89 bool last_block_diff; | |
90 if (last_block_width < kBlockSize) { | |
Sergey Ulanov
2016/09/01 19:27:56
Why would we want last_block_width = kBlockSize? i
Hzj_jie
2016/09/01 23:26:42
It relates to the merging logic above, we always n
| |
91 // The last one is a partial vector. | |
92 last_block_diff = PartialBlockDifference( | |
93 old_buffer, new_buffer, last_block_width, row_height, stride); | |
94 } else { | |
95 last_block_diff = | |
96 BlockDifference(old_buffer, new_buffer, row_height, stride); | |
97 } | |
98 if (last_block_diff) { | |
99 if (first_dirty_x_block == -1) { | |
100 output->AddRect( | |
101 DesktopRect::MakeLTRB(column_left + block_count * kBlockSize, row_top, | |
102 column_right, row_bottom)); | |
103 } else { | |
104 output->AddRect( | |
105 DesktopRect::MakeLTRB(first_dirty_x_block * kBlockSize + column_left, | |
106 row_top, column_right, row_bottom)); | |
107 } | |
108 } else if (first_dirty_x_block != -1) { | |
109 output->AddRect(DesktopRect::MakeLTRB( | |
110 first_dirty_x_block * kBlockSize + column_left, row_top, | |
111 block_count * kBlockSize + column_left, row_bottom)); | |
112 } | |
113 } | |
114 | |
115 // Compares |rect| area in |old_frame| and |new_frame|, and outputs dirty | |
116 // regions into |output|. | |
117 void CompareFrames(const DesktopFrame& old_frame, | |
118 const DesktopFrame& new_frame, | |
119 DesktopRect rect, | |
120 DesktopRegion* const output) { | |
121 RTC_DCHECK(old_frame.size().equals(new_frame.size())); | |
122 RTC_DCHECK_EQ(old_frame.stride(), new_frame.stride()); | |
123 rect.IntersectWith(DesktopRect::MakeSize(old_frame.size())); | |
124 | |
125 const int x_block_count = (rect.width() - 1) / kBlockSize; | |
Sergey Ulanov
2016/09/01 19:27:56
Don't need -1 here
Hzj_jie
2016/09/01 23:26:42
This relates to the merging logic in CompareRow.
| |
126 const int last_x_block_width = rect.width() - x_block_count * kBlockSize; | |
127 const int y_block_count = (rect.height() - 1) / kBlockSize; | |
Sergey Ulanov
2016/09/01 19:27:56
don't need -1 here
Hzj_jie
2016/09/01 23:26:42
By -1 here, and adding a CompareRow in 146, we can
| |
128 const int last_y_block_height = rect.height() - y_block_count * kBlockSize; | |
129 // Offset from the start of one block-row to the next. | |
130 const int block_y_stride = old_frame.stride() * kBlockSize; | |
131 const uint8_t* prev_block_row_start = | |
132 old_frame.GetFrameDataAtPos(rect.top_left()); | |
133 const uint8_t* curr_block_row_start = | |
134 new_frame.GetFrameDataAtPos(rect.top_left()); | |
135 | |
136 int row_top = rect.top(); | |
137 // The last row may have a different height, so we handle it separately. | |
138 for (int y = 0; y < y_block_count; y++) { | |
139 CompareRow(prev_block_row_start, curr_block_row_start, rect.left(), | |
140 x_block_count, last_x_block_width, row_top, kBlockSize, | |
141 old_frame.stride(), output); | |
142 row_top += kBlockSize; | |
143 prev_block_row_start += block_y_stride; | |
144 curr_block_row_start += block_y_stride; | |
145 } | |
146 CompareRow(prev_block_row_start, curr_block_row_start, rect.left(), | |
147 x_block_count, last_x_block_width, row_top, last_y_block_height, | |
148 old_frame.stride(), output); | |
149 } | |
150 | |
151 } // namespace | |
152 | |
153 ScreenCapturerDifferWrapper::ScreenCapturerDifferWrapper( | |
154 std::unique_ptr<ScreenCapturer> base_capturer) | |
155 : base_capturer_(std::move(base_capturer)) { | |
156 RTC_DCHECK(base_capturer_); | |
157 } | |
158 | |
159 ScreenCapturerDifferWrapper::~ScreenCapturerDifferWrapper() {} | |
160 | |
161 void ScreenCapturerDifferWrapper::Start(DesktopCapturer::Callback* callback) { | |
162 callback_ = callback; | |
163 base_capturer_->Start(this); | |
164 } | |
165 | |
166 void ScreenCapturerDifferWrapper::SetSharedMemoryFactory( | |
167 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) { | |
168 base_capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory)); | |
169 } | |
170 | |
171 void ScreenCapturerDifferWrapper::Capture(const DesktopRegion& region) { | |
172 base_capturer_->Capture(region); | |
173 } | |
174 | |
175 bool ScreenCapturerDifferWrapper::GetScreenList(ScreenList* screens) { | |
176 return base_capturer_->GetScreenList(screens); | |
177 } | |
178 | |
179 bool ScreenCapturerDifferWrapper::SelectScreen(ScreenId id) { | |
180 return base_capturer_->SelectScreen(id); | |
181 } | |
182 | |
183 void ScreenCapturerDifferWrapper::OnCaptureResult( | |
184 Result result, | |
185 std::unique_ptr<DesktopFrame> input_frame) { | |
186 int64_t start_time_nanos = rtc::TimeNanos(); | |
187 if (!input_frame) { | |
188 callback_->OnCaptureResult(result, nullptr); | |
189 return; | |
190 } | |
191 | |
192 std::unique_ptr<SharedDesktopFrame> frame = | |
193 SharedDesktopFrame::Wrap(std::move(input_frame)); | |
194 if (result == Result::SUCCESS) { | |
195 if (last_frame_ && | |
196 (last_frame_->size().width() != frame->size().width() || | |
197 last_frame_->size().height() != frame->size().height() || | |
198 last_frame_->stride() != frame->stride())) { | |
199 last_frame_.reset(); | |
200 } | |
201 | |
202 if (last_frame_) { | |
203 DesktopRegion hints; | |
204 hints.Swap(frame->GetUnderlyingFrame()->mutable_updated_region()); | |
205 for (DesktopRegion::Iterator it(hints); !it.IsAtEnd(); it.Advance()) { | |
206 CompareFrames(*last_frame_, *frame, it.rect(), | |
207 frame->mutable_updated_region()); | |
208 } | |
209 } else { | |
210 frame->mutable_updated_region()->SetRect( | |
211 DesktopRect::MakeSize(frame->size())); | |
212 } | |
213 last_frame_ = frame->Share(); | |
214 } else { | |
215 last_frame_.reset(); | |
216 } | |
217 | |
218 frame->set_capture_time_ms(frame->GetUnderlyingFrame()->capture_time_ms() + | |
219 (rtc::TimeNanos() - start_time_nanos) / | |
220 rtc::kNumNanosecsPerMillisec); | |
221 callback_->OnCaptureResult(result, std::move(frame)); | |
222 } | |
223 | |
224 } // namespace webrtc | |
OLD | NEW |