1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*
* Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/desktop_capture/desktop_frame.h"
#include <memory>
#include "modules/desktop_capture/desktop_region.h"
#include "modules/desktop_capture/test_utils.h"
#include "rtc_base/arraysize.h"
#include "test/gtest.h"
namespace webrtc {
namespace {
std::unique_ptr<DesktopFrame> CreateTestFrame(DesktopRect rect,
int pixels_value) {
DesktopSize size = rect.size();
auto frame = std::make_unique<BasicDesktopFrame>(size);
frame->set_top_left(rect.top_left());
memset(frame->data(), pixels_value, frame->stride() * size.height());
return frame;
}
struct TestData {
const char* description;
DesktopRect dest_frame_rect;
DesktopRect src_frame_rect;
double horizontal_scale;
double vertical_scale;
DesktopRect expected_overlap_rect;
};
void RunTest(const TestData& test) {
// Copy a source frame with all bits set into a dest frame with none set.
auto dest_frame = CreateTestFrame(test.dest_frame_rect, 0);
auto src_frame = CreateTestFrame(test.src_frame_rect, 0xff);
dest_frame->CopyIntersectingPixelsFrom(*src_frame, test.horizontal_scale,
test.vertical_scale);
// Translate the expected overlap rect to be relative to the dest frame/rect.
DesktopVector dest_frame_origin = test.dest_frame_rect.top_left();
DesktopRect relative_expected_overlap_rect = test.expected_overlap_rect;
relative_expected_overlap_rect.Translate(-dest_frame_origin.x(),
-dest_frame_origin.y());
// Confirm bits are now set in the dest frame if & only if they fall in the
// expected range.
for (int y = 0; y < dest_frame->size().height(); ++y) {
SCOPED_TRACE(y);
for (int x = 0; x < dest_frame->size().width(); ++x) {
SCOPED_TRACE(x);
DesktopVector point(x, y);
uint8_t* data = dest_frame->GetFrameDataAtPos(point);
uint32_t pixel_value = *reinterpret_cast<uint32_t*>(data);
bool was_copied = pixel_value == 0xffffffff;
ASSERT_TRUE(was_copied || pixel_value == 0);
bool expected_to_be_copied =
relative_expected_overlap_rect.Contains(point);
ASSERT_EQ(was_copied, expected_to_be_copied);
}
}
}
void RunTests(const TestData* tests, int num_tests) {
for (int i = 0; i < num_tests; i++) {
const TestData& test = tests[i];
SCOPED_TRACE(test.description);
RunTest(test);
}
}
} // namespace
TEST(DesktopFrameTest, NewFrameIsBlack) {
auto frame = std::make_unique<BasicDesktopFrame>(DesktopSize(10, 10));
EXPECT_TRUE(frame->FrameDataIsBlack());
}
TEST(DesktopFrameTest, EmptyFrameIsNotBlack) {
auto frame = std::make_unique<BasicDesktopFrame>(DesktopSize());
EXPECT_FALSE(frame->FrameDataIsBlack());
}
TEST(DesktopFrameTest, FrameHasDefaultDeviceScaleFactor) {
auto frame = std::make_unique<BasicDesktopFrame>(DesktopSize());
EXPECT_EQ(frame->device_scale_factor(), std::nullopt);
}
TEST(DesktopFrameTest, FrameSetsDeviceScaleFactorCorrectly) {
auto frame = std::make_unique<BasicDesktopFrame>(DesktopSize());
EXPECT_EQ(frame->device_scale_factor(), std::nullopt);
float device_scale_factor = 1.5f;
frame->set_device_scale_factor(device_scale_factor);
EXPECT_EQ(frame->device_scale_factor(), device_scale_factor);
}
TEST(DesktopFrameTest, FrameDataSwitchesBetweenNonBlackAndBlack) {
auto frame = CreateTestFrame(DesktopRect::MakeXYWH(0, 0, 10, 10), 0xff);
EXPECT_FALSE(frame->FrameDataIsBlack());
frame->SetFrameDataToBlack();
EXPECT_TRUE(frame->FrameDataIsBlack());
}
TEST(DesktopFrameTest, CopyIntersectingPixelsMatchingRects) {
// clang-format off
const TestData tests[] = {
{"0 origin",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(0, 0, 2, 2),
1.0, 1.0,
DesktopRect::MakeXYWH(0, 0, 2, 2)},
{"Negative origin",
DesktopRect::MakeXYWH(-1, -1, 2, 2),
DesktopRect::MakeXYWH(-1, -1, 2, 2),
1.0, 1.0,
DesktopRect::MakeXYWH(-1, -1, 2, 2)}
};
// clang-format on
RunTests(tests, arraysize(tests));
}
TEST(DesktopFrameTest, CopyIntersectingPixelsMatchingRectsScaled) {
// The scale factors shouldn't affect matching rects (they're only applied
// to any difference between the origins)
// clang-format off
const TestData tests[] = {
{"0 origin 2x",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(0, 0, 2, 2),
2.0, 2.0,
DesktopRect::MakeXYWH(0, 0, 2, 2)},
{"0 origin 0.5x",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(0, 0, 2, 2),
0.5, 0.5,
DesktopRect::MakeXYWH(0, 0, 2, 2)},
{"Negative origin 2x",
DesktopRect::MakeXYWH(-1, -1, 2, 2),
DesktopRect::MakeXYWH(-1, -1, 2, 2),
2.0, 2.0,
DesktopRect::MakeXYWH(-1, -1, 2, 2)},
{"Negative origin 0.5x",
DesktopRect::MakeXYWH(-1, -1, 2, 2),
DesktopRect::MakeXYWH(-1, -1, 2, 2),
0.5, 0.5,
DesktopRect::MakeXYWH(-1, -1, 2, 2)}
};
// clang-format on
RunTests(tests, arraysize(tests));
}
TEST(DesktopFrameTest, CopyIntersectingPixelsFullyContainedRects) {
// clang-format off
const TestData tests[] = {
{"0 origin top left",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(0, 0, 1, 1),
1.0, 1.0,
DesktopRect::MakeXYWH(0, 0, 1, 1)},
{"0 origin bottom right",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(1, 1, 1, 1),
1.0, 1.0,
DesktopRect::MakeXYWH(1, 1, 1, 1)},
{"Negative origin bottom left",
DesktopRect::MakeXYWH(-1, -1, 2, 2),
DesktopRect::MakeXYWH(-1, 0, 1, 1),
1.0, 1.0,
DesktopRect::MakeXYWH(-1, 0, 1, 1)}
};
// clang-format on
RunTests(tests, arraysize(tests));
}
TEST(DesktopFrameTest, CopyIntersectingPixelsFullyContainedRectsScaled) {
// clang-format off
const TestData tests[] = {
{"0 origin top left 2x",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(0, 0, 1, 1),
2.0, 2.0,
DesktopRect::MakeXYWH(0, 0, 1, 1)},
{"0 origin top left 0.5x",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(0, 0, 1, 1),
0.5, 0.5,
DesktopRect::MakeXYWH(0, 0, 1, 1)},
{"0 origin bottom left 2x",
DesktopRect::MakeXYWH(0, 0, 4, 4),
DesktopRect::MakeXYWH(1, 1, 2, 2),
2.0, 2.0,
DesktopRect::MakeXYWH(2, 2, 2, 2)},
{"0 origin bottom middle 2x/1x",
DesktopRect::MakeXYWH(0, 0, 4, 3),
DesktopRect::MakeXYWH(1, 1, 2, 2),
2.0, 1.0,
DesktopRect::MakeXYWH(2, 1, 2, 2)},
{"0 origin middle 0.5x",
DesktopRect::MakeXYWH(0, 0, 3, 3),
DesktopRect::MakeXYWH(2, 2, 1, 1),
0.5, 0.5,
DesktopRect::MakeXYWH(1, 1, 1, 1)},
{"Negative origin bottom left 2x",
DesktopRect::MakeXYWH(-1, -1, 3, 3),
DesktopRect::MakeXYWH(-1, 0, 1, 1),
2.0, 2.0,
DesktopRect::MakeXYWH(-1, 1, 1, 1)},
{"Negative origin near middle 0.5x",
DesktopRect::MakeXYWH(-2, -2, 2, 2),
DesktopRect::MakeXYWH(0, 0, 1, 1),
0.5, 0.5,
DesktopRect::MakeXYWH(-1, -1, 1, 1)}
};
// clang-format on
RunTests(tests, arraysize(tests));
}
TEST(DesktopFrameTest, CopyIntersectingPixelsPartiallyContainedRects) {
// clang-format off
const TestData tests[] = {
{"Top left",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(-1, -1, 2, 2),
1.0, 1.0,
DesktopRect::MakeXYWH(0, 0, 1, 1)},
{"Top right",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(1, -1, 2, 2),
1.0, 1.0,
DesktopRect::MakeXYWH(1, 0, 1, 1)},
{"Bottom right",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(1, 1, 2, 2),
1.0, 1.0,
DesktopRect::MakeXYWH(1, 1, 1, 1)},
{"Bottom left",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(-1, 1, 2, 2),
1.0, 1.0,
DesktopRect::MakeXYWH(0, 1, 1, 1)}
};
// clang-format on
RunTests(tests, arraysize(tests));
}
TEST(DesktopFrameTest, CopyIntersectingPixelsPartiallyContainedRectsScaled) {
// clang-format off
const TestData tests[] = {
{"Top left 2x",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(-1, -1, 3, 3),
2.0, 2.0,
DesktopRect::MakeXYWH(0, 0, 1, 1)},
{"Top right 0.5x",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(2, -2, 2, 2),
0.5, 0.5,
DesktopRect::MakeXYWH(1, 0, 1, 1)},
{"Bottom right 2x",
DesktopRect::MakeXYWH(0, 0, 3, 3),
DesktopRect::MakeXYWH(-1, 1, 3, 3),
2.0, 2.0,
DesktopRect::MakeXYWH(0, 2, 1, 1)},
{"Bottom left 0.5x",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(-2, 2, 2, 2),
0.5, 0.5,
DesktopRect::MakeXYWH(0, 1, 1, 1)}
};
// clang-format on
RunTests(tests, arraysize(tests));
}
TEST(DesktopFrameTest, CopyIntersectingPixelsUncontainedRects) {
// clang-format off
const TestData tests[] = {
{"Left",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(-1, 0, 1, 2),
1.0, 1.0,
DesktopRect::MakeXYWH(0, 0, 0, 0)},
{"Top",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(0, -1, 2, 1),
1.0, 1.0,
DesktopRect::MakeXYWH(0, 0, 0, 0)},
{"Right",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(2, 0, 1, 2),
1.0, 1.0,
DesktopRect::MakeXYWH(0, 0, 0, 0)},
{"Bottom",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(0, 2, 2, 1),
1.0, 1.0,
DesktopRect::MakeXYWH(0, 0, 0, 0)}
};
// clang-format on
RunTests(tests, arraysize(tests));
}
TEST(DesktopFrameTest, CopyIntersectingPixelsUncontainedRectsScaled) {
// clang-format off
const TestData tests[] = {
{"Left 2x",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(-1, 0, 2, 2),
2.0, 2.0,
DesktopRect::MakeXYWH(0, 0, 0, 0)},
{"Top 0.5x",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(0, -2, 2, 1),
0.5, 0.5,
DesktopRect::MakeXYWH(0, 0, 0, 0)},
{"Right 2x",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(1, 0, 1, 2),
2.0, 2.0,
DesktopRect::MakeXYWH(0, 0, 0, 0)},
{"Bottom 0.5x",
DesktopRect::MakeXYWH(0, 0, 2, 2),
DesktopRect::MakeXYWH(0, 4, 2, 1),
0.5, 0.5,
DesktopRect::MakeXYWH(0, 0, 0, 0)}
};
// clang-format on
RunTests(tests, arraysize(tests));
}
} // namespace webrtc
|