Adds SetFrameDataToBlack() and FrameDataIsBlack() to DesktopFrame

Bug: None
Change-Id: I558b2398de409b0b44f331520e3d4692e45b9315
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/296884
Reviewed-by: Alexander Cooper <alcooper@chromium.org>
Commit-Queue: Henrik Andreassson <henrika@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#39537}
This commit is contained in:
henrika 2023-03-10 16:35:09 +01:00 committed by WebRTC LUCI CQ
parent 86eb6ba207
commit bedb69f26d
3 changed files with 41 additions and 0 deletions

View file

@ -144,6 +144,23 @@ void DesktopFrame::MoveFrameInfoFrom(DesktopFrame* other) {
set_icc_profile(other->icc_profile()); set_icc_profile(other->icc_profile());
} }
bool DesktopFrame::FrameDataIsBlack() const {
if (size().is_empty())
return false;
uint32_t* pixel = reinterpret_cast<uint32_t*>(data());
for (int i = 0; i < size().width() * size().height(); ++i) {
if (*pixel++)
return false;
}
return true;
}
void DesktopFrame::SetFrameDataToBlack() {
const uint8_t kBlackPixelValue = 0x00;
memset(data(), kBlackPixelValue, stride() * size().height());
}
BasicDesktopFrame::BasicDesktopFrame(DesktopSize size) BasicDesktopFrame::BasicDesktopFrame(DesktopSize size)
: DesktopFrame(size, : DesktopFrame(size,
kBytesPerPixel * size.width(), kBytesPerPixel * size.width(),

View file

@ -142,6 +142,13 @@ class RTC_EXPORT DesktopFrame {
icc_profile_ = icc_profile; icc_profile_ = icc_profile;
} }
// Sets all pixel values in the data buffer to zero.
void SetFrameDataToBlack();
// Returns true if all pixel values in the data buffer are zero or false
// otherwise. Also returns false if the frame is empty.
bool FrameDataIsBlack() const;
protected: protected:
DesktopFrame(DesktopSize size, DesktopFrame(DesktopSize size,
int stride, int stride,

View file

@ -87,6 +87,23 @@ void RunTests(const TestData* tests, int num_tests) {
} // namespace } // 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, FrameDataSwitchesBetweenNonBlackAndBlack) {
auto frame = CreateTestFrame(DesktopRect::MakeXYWH(0, 0, 10, 10), 0xff);
EXPECT_FALSE(frame->FrameDataIsBlack());
frame->SetFrameDataToBlack();
EXPECT_TRUE(frame->FrameDataIsBlack());
}
TEST(DesktopFrameTest, CopyIntersectingPixelsMatchingRects) { TEST(DesktopFrameTest, CopyIntersectingPixelsMatchingRects) {
const TestData tests[] = { const TestData tests[] = {
{"0 origin", {"0 origin",