Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-115581

Add a writable devicePixelRatio property to QQuickItem

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • P2: Important
    • None
    • None
    • Quick: SceneGraph
    • None
    • All

    Description

      An example: qtquick-rendertarget-bug.zip

      This example program is a simplified version of my project. I need to render a portion of the QQuickWindow into multiple given QQuickRenderTargets. Additionally, different QQuickRenderTargets may have different devicePixelRatio, depending on the runtime environment, which is not fixed and can also be set by the user.

      However, when changing the QQuickRenderTarget of the QQuickWindow, it also simultaneously changes the QQuickWindow::effectiveDevicePixelRatio. This causes some resources to be recreated or reloaded, such as QQuickImage, which may reload image files, and QRhiSGLayer, which recreates QRhiRenderBuffer, etc. As I need to render separately to different QQuickRenderTargets within the same rendering cycle, the frequent changes to QQuickWindow::effectiveDevicePixelRatio lead to performance issues. In the example, the CPU usage is very high, as shown in the screenshot below. If we can ensure that QQuickWindow::effectiveDevicePixelRatio remains constant during rendering, the CPU usage in the example can be reduced to one-fifth. Yes, there is a way to fix QQuickWindow::effectiveDevicePixelRatio, and that is by overriding QQuickRenderControl::renderWindow. However, this is not easy to implement as I cannot set the QWindow's devicePixelRatio, which may not have the desired value.

      To address this, I have a suggestion. I believe we should separate the devicePixelRatio used for controlling resource loading and the one used for controlling rendering effects. Unfortunately, QQuickWindow::effectiveDevicePixelRatio confuses these concepts and has been misused. Since resource loading occurs in QQuickItem, we can add a devicePixelRatio property to QQuickItem, allowing users to control the devicePixelRatio for certain QQuickItems. Upon initialization, the devicePixelRatio of QQuickItem follows QQuickWindow::effectiveDevicePixelRatio. If the user sets it, then it and all its child items will use the user-specified value, controlling resource loading only.

      Additionally, there are other benefits to adding devicePixelRatio to QQuickItem. QQuickItem may not necessarily be inside a QWindow. Currently, the handling is such that if the window of QQuickItem is nullptr, the devicePixelRatio falls back to QGuiApplication::devicePixelRatio. However, if QQuickItem can have its own devicePixelRatio setting, it can be more useful than relying solely on QGuiApplication::devicePixelRatio.

      In my project, I can determine the maximum scaling factor for the resources I need to load. Although there are multiple QQuickRenderTargets, their devicePixelRatio will not change frequently, and I can easily find the maximum value. Therefore, I only need to set the devicePixelRatio of QQuickWindow::contentItem to this maximum value, avoiding resource reloads caused by changing QQuickRenderTargets. Additionally, there is another benefit; I can set different devicePixelRatios for different QQuickItems. Some QML Images will only be rendered in one specific QQuickRenderTarget. Hence, I can keep these Image's devicePixelRatio consistent with the corresponding QQuickRenderTarget's devicePixelRatio without using the maximum devicePixelRatio for all QQuickItems.

      Attachments

        For Gerrit Dashboard: QTBUG-115581
        # Subject Branch Project Status CR V

        Activity

          People

            qt.team.graphics.and.multimedia Qt Graphics Team
            zccrs JiDe Zhang
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There is 1 open Gerrit change