From 3037980689f3f7c3dec5a55a71641fc3a2c0018d Mon Sep 17 00:00:00 2001 From: Paul Adenot Date: Mon, 11 Jul 2022 13:32:40 +0000 Subject: [PATCH] Bug 1757637 - Use the ImageContainer to determine video{Width,Height} instead of passing through the state machine. r=alwu This means there is significantly less chance of desynchronization between the actual size of the image and the reported size of the image. Differential Revision: https://phabricator.services.mozilla.com/D149150 --- dom/html/HTMLVideoElement.cpp | 40 ++++++++++++++++++++++++++++++++++- dom/html/HTMLVideoElement.h | 26 +++++------------------ 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/dom/html/HTMLVideoElement.cpp b/dom/html/HTMLVideoElement.cpp index 4142bb0a6052..cda92efba380 100644 --- a/dom/html/HTMLVideoElement.cpp +++ b/dom/html/HTMLVideoElement.cpp @@ -215,6 +215,44 @@ bool HTMLVideoElement::IsInteractiveHTMLContent() const { HTMLMediaElement::IsInteractiveHTMLContent(); } +gfx::IntSize HTMLVideoElement::GetVideoIntrinsicDimensions() { + layers::ImageContainer* container = GetImageContainer(); + // Prefer the size of the container as it's more up to date. + if (container && container->GetCurrentSize().width != 0) { + // But adjust to the aspect ratio of the container. + float dar = static_cast(mMediaInfo.mVideo.mDisplay.width) / + mMediaInfo.mVideo.mDisplay.height; + gfx::IntSize size = container->GetCurrentSize(); + float imageDar = static_cast(size.width) / size.height; + return gfx::IntSize(int(size.width * (dar / imageDar)), size.height); + } + return mMediaInfo.mVideo.mDisplay; +} + +uint32_t HTMLVideoElement::VideoWidth() { + if (!mMediaInfo.HasVideo()) { + return 0; + } + gfx::IntSize size = GetVideoIntrinsicDimensions(); + if (mMediaInfo.mVideo.mRotation == VideoInfo::Rotation::kDegree_90 || + mMediaInfo.mVideo.mRotation == VideoInfo::Rotation::kDegree_270) { + return size.height; + } + return size.width; +} + +uint32_t HTMLVideoElement::VideoHeight() { + if (!mMediaInfo.HasVideo()) { + return 0; + } + gfx::IntSize size = GetVideoIntrinsicDimensions(); + if (mMediaInfo.mVideo.mRotation == VideoInfo::Rotation::kDegree_90 || + mMediaInfo.mVideo.mRotation == VideoInfo::Rotation::kDegree_270) { + return size.width; + } + return size.height; +} + uint32_t HTMLVideoElement::MozParsedFrames() const { MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread."); if (!IsVideoStatsEnabled()) { @@ -241,7 +279,7 @@ uint32_t HTMLVideoElement::MozDecodedFrames() const { return mDecoder ? mDecoder->GetFrameStatistics().GetDecodedFrames() : 0; } -uint32_t HTMLVideoElement::MozPresentedFrames() const { +uint32_t HTMLVideoElement::MozPresentedFrames() { MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread."); if (!IsVideoStatsEnabled()) { return 0; diff --git a/dom/html/HTMLVideoElement.h b/dom/html/HTMLVideoElement.h index f89f41017959..8d0e5d45d00b 100644 --- a/dom/html/HTMLVideoElement.h +++ b/dom/html/HTMLVideoElement.h @@ -81,27 +81,9 @@ class HTMLVideoElement final : public HTMLMediaElement { SetUnsignedIntAttr(nsGkAtoms::height, aValue, 0, aRv); } - uint32_t VideoWidth() const { - if (mMediaInfo.HasVideo()) { - if (mMediaInfo.mVideo.mRotation == VideoInfo::Rotation::kDegree_90 || - mMediaInfo.mVideo.mRotation == VideoInfo::Rotation::kDegree_270) { - return mMediaInfo.mVideo.mDisplay.height; - } - return mMediaInfo.mVideo.mDisplay.width; - } - return 0; - } + uint32_t VideoWidth(); - uint32_t VideoHeight() const { - if (mMediaInfo.HasVideo()) { - if (mMediaInfo.mVideo.mRotation == VideoInfo::Rotation::kDegree_90 || - mMediaInfo.mVideo.mRotation == VideoInfo::Rotation::kDegree_270) { - return mMediaInfo.mVideo.mDisplay.width; - } - return mMediaInfo.mVideo.mDisplay.height; - } - return 0; - } + uint32_t VideoHeight(); VideoInfo::Rotation RotationDegrees() const { return mMediaInfo.mVideo.mRotation; @@ -120,7 +102,7 @@ class HTMLVideoElement final : public HTMLMediaElement { uint32_t MozDecodedFrames() const; - uint32_t MozPresentedFrames() const; + uint32_t MozPresentedFrames(); uint32_t MozPaintedFrames(); @@ -169,6 +151,8 @@ class HTMLVideoElement final : public HTMLMediaElement { void CreateVideoWakeLockIfNeeded(); void ReleaseVideoWakeLockIfExists(); + gfx::IntSize GetVideoIntrinsicDimensions(); + RefPtr mScreenWakeLock; bool mIsOrientationLocked;