qt: assign value type source size as a whole to prevent potential double image loading
Based on !6572.
Partially changing a value type changes the whole value. In image case, this means that the images can get re-loaded with indeterminate size just to discard them after the size is determined.
Fortunately this does not happen currently, because the sizes are determined before the component is complete and QQuickImage only loads if the component is complete.
However, due to the declarative nature of QML, bindings are evaluated and assigned one by one, this means that source size would be changed two times even if .width and .height are pending re-evaluation at the same time (such as both depend on DPR). At the same time, the order is also not defined, so with such a setup as following:
sourceSize.width: width * eDPR
sourceSize.height: height * eDPR
When eDPR changes, Qt evaluates the binding for width or height, adjusts sourceSize, sourceSize changes and change signal is signalled, then evaluates the other sub-part of the value type (width or height), adjusts sourceSize, sourceSize changes again and change signal is signalled. Qt could technically optimize this, but as of Qt 6.8.1 it is not the case.
Meanwhile with the following:
sourceSize: Qt.size(width * eDPR, height * eDPR)
When eDPR changes, Qt evaluates the binding and adjusts the source size, sourceSize changes and change signal is signalled. Source size does not change two times, and the image would not be loaded two times.
With Qt 6.8.1, I added onSourceSizeChanged: console.log(sourceSize, source, status)
, with
sourceSize.width: root.pictureWidth * Screen.devicePixelRatio
sourceSize.height: root.pictureHeight * Screen.devicePixelRatio
The logs are:
[0000577327341080] [qt] qt generic debug: (qml) QSize(0, 0) 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 0) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/1.jpg 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/1.jpg 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(0, 0) 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 0) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/12.jpg 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/12.jpg 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(0, 0) 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 0) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/3.jpg 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/3.jpg 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(0, 0) 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 0) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/11.jpg 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/11.jpg 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(0, 0) 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 0) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/13.jpg 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/13.jpg 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(0, 0) 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 0) 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 325) 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(0, 0) 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 0) 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 325) 0
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/1.jpg 1
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/13.jpg 1
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/11.jpg 1
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/3.jpg 1
[0000577327341080] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/12.jpg 1
With
sourceSize: Qt.size(root.pictureWidth * Screen.devicePixelRatio,
root.pictureHeight * Screen.devicePixelRatio)
The logs are:
[0000653f54762d10] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/1.jpg 0
[0000653f54762d10] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/12.jpg 0
[0000653f54762d10] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/3.jpg 0
[0000653f54762d10] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/11.jpg 0
[0000653f54762d10] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/13.jpg 0
[0000653f54762d10] [qt] qt generic debug: (qml) QSize(325, 325) 0
[0000653f54762d10] [qt] qt generic debug: (qml) QSize(325, 325) 0
[0000653f54762d10] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/1.jpg 1
[0000653f54762d10] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/13.jpg 1
[0000653f54762d10] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/11.jpg 1
[0000653f54762d10] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/3.jpg 1
[0000653f54762d10] [qt] qt generic debug: (qml) QSize(325, 325) file:///home/fuzun/.local/share/vlc/ml/mlstorage/thumbnails/12.jpg 1
A while ago I warned about this, but it was before !5270 (merged), so it is understandable why this approach was used as Qt advertises this way of adjustment (at least with font
) for value types.
Request review @chub.