Skip to content

fix(videowidget): fix hi-dpi display scaling in video widget#493

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix-369935-fix-hi-dpi-display-scaling
Jul 13, 2026
Merged

fix(videowidget): fix hi-dpi display scaling in video widget#493
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix-369935-fix-hi-dpi-display-scaling

Conversation

@add-uos

@add-uos add-uos commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Use devicePixelRatioF() for proper HiDPI scaling, prevent integer overflow in aspect ratio comparison, and fix scene rect calculation with logical coordinates.

修复视频预览控件在高DPI屏幕下的显示缩放问题,使用 devicePixelRatioF() 处理像素比例,防止整数溢出,并修复场景矩形计算。

Log: 修复 videowidget 高DPI显示缩放问题
PMS: BUG-369935
Influence: 修复后视频预览在高DPI屏幕上正常显示,画面比例和场景矩形坐标正确。

Summary by Sourcery

Fix HiDPI video preview scaling and scene rectangle calculation in the video widget.

Bug Fixes:

  • Correct video preview scaling on high-DPI displays by using device pixel ratio in widget size and pixmap handling.
  • Prevent integer overflow when comparing aspect ratios between the video image and the widget.

Enhancements:

  • Adjust scene rectangle to use logical coordinates based on device pixel ratio so the displayed frame size matches the widget accurately.

Use devicePixelRatioF() for proper HiDPI scaling, prevent integer overflow in aspect ratio comparison, and fix scene rect calculation with logical coordinates.

修复视频预览控件在高DPI屏幕下的显示缩放问题,使用 devicePixelRatioF() 处理像素比例,防止整数溢出,并修复场景矩形计算。

Log: 修复 videowidget 高DPI显示缩放问题
PMS: BUG-369935
Influence: 修复后视频预览在高DPI屏幕上正常显示,画面比例和场景矩形坐标正确。
@sourcery-ai

sourcery-ai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts video widget scaling logic to be HiDPI‑aware, prevents overflow in aspect ratio comparison, and aligns scene rectangle setup with logical coordinates.

Flow diagram for updated HiDPI-aware video widget scaling

flowchart TD
    A[ReceiveMajorImage] --> B[compute dpr using devicePixelRatioF]
    B --> C[compute widgetwidth and widgetheight in physical pixels]
    C --> D{image width * widgetheight > widgetwidth * image height}
    D -->|true| E[set targetWidth = widgetwidth and targetHeight = min of scaled height and widgetheight]
    D -->|false| F[set targetHeight = widgetheight and targetWidth = min of scaled width and widgetwidth]
    E --> G[scale image to targetWidth,targetHeight]
    F --> G
    G --> H[create m_framePixmap from scaled image]
    H --> I[setDevicePixelRatio on m_framePixmap]
    I --> J[compute logical sceneRect using m_framePixmap width,height / dpr]
    J --> K[setSceneRect on m_pNormalScene]
    K --> L[setPixmap on m_pNormalItem]
Loading

File-Level Changes

Change Details Files
Make video widget image scaling HiDPI-aware and robust against overflow while keeping aspect ratio within widget bounds.
  • Use devicePixelRatioF() and round widget dimensions to device pixels before scaling the image.
  • Compare image and widget aspect ratios using 64-bit integer arithmetic to avoid overflow.
  • Compute target scaled width/height with qRound and clamp them with qMin to not exceed widget dimensions.
  • Create QPixmap from the scaled image and set its device pixel ratio to match the widget.
  • Set the QGraphicsScene scene rect using logical coordinates derived from pixmap size divided by devicePixelRatioF().
src/src/videowidget.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • Given the added HiDPI handling, consider extracting the device-pixel scaling and target size computation into a small helper function to make the aspect-ratio logic clearer and easier to reuse or adjust in other code paths.
  • The use of Qt::IgnoreAspectRatio with explicit aspect-ratio–based targetWidth/targetHeight is slightly contradictory; if the goal is to preserve the video’s aspect ratio, it may be clearer to use Qt::KeepAspectRatio and let Qt handle the scaling instead of manual ratio calculations.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Given the added HiDPI handling, consider extracting the device-pixel scaling and target size computation into a small helper function to make the aspect-ratio logic clearer and easier to reuse or adjust in other code paths.
- The use of Qt::IgnoreAspectRatio with explicit aspect-ratio–based targetWidth/targetHeight is slightly contradictory; if the goal is to preserve the video’s aspect ratio, it may be clearer to use Qt::KeepAspectRatio and let Qt handle the scaling instead of manual ratio calculations.

## Individual Comments

### Comment 1
<location path="src/src/videowidget.cpp" line_range="600-601" />
<code_context>
+                m_framePixmap = QPixmap::fromImage(image->scaled(targetWidth, targetHeight, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
+                m_framePixmap.setDevicePixelRatio(dpr);
+
+                const QRectF sceneRect(0, 0, qreal(m_framePixmap.width()) / dpr, qreal(m_framePixmap.height()) / dpr);
+                m_pNormalScene->setSceneRect(sceneRect);
                 m_pNormalItem->setPixmap(m_framePixmap);

</code_context>
<issue_to_address>
**question (bug_risk):** Scene rect now depends on devicePixelRatio; ensure this aligns with how the view interprets logical vs device coordinates.

This change correctly expresses the scene rect in logical coordinates, but it may diverge from any code that still treats the pixmap size as device pixels. Please verify that `QGraphicsView` configuration and any custom painting/transformations are consistent with logical coordinates to avoid subtle offset or zoom issues.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/src/videowidget.cpp
Comment on lines +600 to +601
const QRectF sceneRect(0, 0, qreal(m_framePixmap.width()) / dpr, qreal(m_framePixmap.height()) / dpr);
m_pNormalScene->setSceneRect(sceneRect);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (bug_risk): Scene rect now depends on devicePixelRatio; ensure this aligns with how the view interprets logical vs device coordinates.

This change correctly expresses the scene rect in logical coordinates, but it may diverge from any code that still treats the pixmap size as device pixels. Please verify that QGraphicsView configuration and any custom painting/transformations are consistent with logical coordinates to avoid subtle offset or zoom issues.

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:100分

■ 【总体评价】

代码修复了高DPI屏幕下视频显示模糊和尺寸不匹配的问题,同时解决了整数溢出风险,逻辑严谨且质量优秀。
修复了原有的高DPI适配和整数溢出缺陷,无安全漏洞,代码质量高。

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

videowidget::ReceiveMajorImage 函数中,新代码正确引入了 devicePixelRatioF() 计算物理像素尺寸,使用 qint64 避免了宽高乘积的整数溢出,并通过 qreal 进行除法运算保证精度。setSceneRect 使用逻辑像素尺寸,与 setDevicePixelRatio 配合符合 Qt 高DPI渲染机制。
潜在问题:无
建议:无需修改

  • 2.代码质量(良好)✓

代码消除了原有两个分支中重复的 QPixmap::fromImage 调用,统一了缩放逻辑。变量命名清晰,合理使用了 const 修饰,可读性显著提升。
潜在问题:无
建议:无需修改

  • 3.代码性能(良好)✓

缩放计算使用了基本算术运算,复杂度为 O(1),未引入额外性能开销。Qt::SmoothTransformation 虽有计算成本,但为保证画质是必要的。
潜在问题:无
建议:无需修改

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码修复了原有的整数溢出风险,未引入新的安全问题,无外部输入注入风险。

  • 建议:无需修改

■ 【改进建议代码示例】

// 当前代码已是最优实现,无需额外修改
// 以下为当前代码片段确认
const qreal dpr = this->devicePixelRatioF();
const int widgetwidth = qRound(this->width() * dpr);
const int widgetheight = qRound(this->height() * dpr);

int targetWidth, targetHeight;
if (qint64(image->width()) * widgetheight > qint64(widgetwidth) * image->height()) {
    targetWidth = widgetwidth;
    targetHeight = qMin(qRound(qreal(widgetwidth) * image->height() / image->width()), widgetheight);
} else {
    targetHeight = widgetheight;
    targetWidth = qMin(qRound(qreal(widgetheight) * image->width() / image->height()), widgetwidth);
}

m_framePixmap = QPixmap::fromImage(image->scaled(targetWidth, targetHeight, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
m_framePixmap.setDevicePixelRatio(dpr);

const QRectF sceneRect(0, 0, qreal(m_framePixmap.width()) / dpr, qreal(m_framePixmap.height()) / dpr);
m_pNormalScene->setSceneRect(sceneRect);
m_pNormalItem->setPixmap(m_framePixmap);

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@add-uos

add-uos commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot deepin-bot Bot merged commit 3d2e329 into linuxdeepin:master Jul 13, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants