From bb917acb10ddc9e131eec5efb352c45cee850840 Mon Sep 17 00:00:00 2001 From: henrika Date: Mon, 19 Jun 2023 16:36:29 +0200 Subject: [PATCH] Fixes crash in WgcCaptureSession::ProcessFrame This change fixes a minor issue where we previosuly assumed that the following was true: RTC_DCHECK_EQ(map_info.RowPitch, current_frame->stride()) It turns out that this is not always the case when sharing a window where the stride can sometimes be a few bytes smaller than the rowpitch. The code is behind a command-line flag and no tests are affected. Given limited review resources I therefore plan to bypass the CQ. I know that it is not recommended but the change has been tested locally on two different Windows platforms and it does avoid an existing crash. Code-Review: alcooper@chromium.org No-Try: true Bug: chromium:1421242 Change-Id: I01e7105a6f9fca7ce1349a57635dd373c28d160b Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/309342 Reviewed-by: Ilya Nikolaevskiy Commit-Queue: Henrik Andreassson Cr-Commit-Position: refs/heads/main@{#40308} --- modules/desktop_capture/win/wgc_capture_session.cc | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/desktop_capture/win/wgc_capture_session.cc b/modules/desktop_capture/win/wgc_capture_session.cc index 61138c04de..a291f20167 100644 --- a/modules/desktop_capture/win/wgc_capture_session.cc +++ b/modules/desktop_capture/win/wgc_capture_session.cc @@ -455,8 +455,11 @@ HRESULT WgcCaptureSession::ProcessFrame() { uint8_t* dst_data = current_frame->data(); uint8_t* prev_data = frame_content_can_be_compared ? previous_frame->data() : nullptr; - RTC_DCHECK_EQ(map_info.RowPitch, current_frame->stride()); - const int width_in_bytes = map_info.RowPitch; + + const int width_in_bytes = + current_frame->size().width() * DesktopFrame::kBytesPerPixel; + RTC_DCHECK_GE(current_frame->stride(), width_in_bytes); + RTC_DCHECK_GE(map_info.RowPitch, width_in_bytes); const int middle_pixel_offset = (image_width / 2) * DesktopFrame::kBytesPerPixel; for (int i = 0; i < image_height; i++) { @@ -466,10 +469,10 @@ HRESULT WgcCaptureSession::ProcessFrame() { uint8_t* current_pixel = dst_data + middle_pixel_offset; frame_content_has_changed = memcmp(previous_pixel, current_pixel, DesktopFrame::kBytesPerPixel); - prev_data += width_in_bytes; + prev_data += current_frame->stride(); } - dst_data += width_in_bytes; - src_data += width_in_bytes; + dst_data += current_frame->stride(); + src_data += map_info.RowPitch; } d3d_context->Unmap(mapped_texture_.Get(), 0);