diff --git a/modules/desktop_capture/desktop_frame.cc b/modules/desktop_capture/desktop_frame.cc index d6f574588c..030bd8f060 100644 --- a/modules/desktop_capture/desktop_frame.cc +++ b/modules/desktop_capture/desktop_frame.cc @@ -144,6 +144,23 @@ void DesktopFrame::MoveFrameInfoFrom(DesktopFrame* other) { set_icc_profile(other->icc_profile()); } +bool DesktopFrame::FrameDataIsBlack() const { + if (size().is_empty()) + return false; + + uint32_t* pixel = reinterpret_cast(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) : DesktopFrame(size, kBytesPerPixel * size.width(), diff --git a/modules/desktop_capture/desktop_frame.h b/modules/desktop_capture/desktop_frame.h index 3ee1867e70..35ac8e2475 100644 --- a/modules/desktop_capture/desktop_frame.h +++ b/modules/desktop_capture/desktop_frame.h @@ -142,6 +142,13 @@ class RTC_EXPORT DesktopFrame { 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: DesktopFrame(DesktopSize size, int stride, diff --git a/modules/desktop_capture/desktop_frame_unittest.cc b/modules/desktop_capture/desktop_frame_unittest.cc index ce0cbb45f5..22df1a7f53 100644 --- a/modules/desktop_capture/desktop_frame_unittest.cc +++ b/modules/desktop_capture/desktop_frame_unittest.cc @@ -87,6 +87,23 @@ void RunTests(const TestData* tests, int num_tests) { } // namespace +TEST(DesktopFrameTest, NewFrameIsBlack) { + auto frame = std::make_unique(DesktopSize(10, 10)); + EXPECT_TRUE(frame->FrameDataIsBlack()); +} + +TEST(DesktopFrameTest, EmptyFrameIsNotBlack) { + auto frame = std::make_unique(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) { const TestData tests[] = { {"0 origin",