|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Meltytech, LLC |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "hdrpreviewwindow.h" |
| 19 | + |
| 20 | +#include "qmltypes/qmlutilities.h" |
| 21 | + |
| 22 | +#include <private/qrhi_p.h> |
| 23 | +#include <QDebug> |
| 24 | +#include <QDir> |
| 25 | +#include <QQmlContext> |
| 26 | +#include <QQmlEngine> |
| 27 | +#include <QQuickItem> |
| 28 | + |
| 29 | +#ifdef Q_OS_MACOS |
| 30 | +#include "macos.h" |
| 31 | +#endif |
| 32 | + |
| 33 | +// HLG OETF: scene-referred linear to HLG electrical signal. |
| 34 | +// See ITU-R BT.2100-2. |
| 35 | +static float hlgOetf(float linear) |
| 36 | +{ |
| 37 | + const float a = 0.17883277f; |
| 38 | + const float b = 0.28466892f; |
| 39 | + const float c = 0.55991073f; |
| 40 | + if (linear < 1.0f / 12.0f) |
| 41 | + return sqrtf(3.0f * linear); |
| 42 | + return a * logf(12.0f * linear - b) + c; |
| 43 | +} |
| 44 | + |
| 45 | +HdrPreviewWindow::HdrPreviewWindow(QWindow *parent) |
| 46 | + : QQuickView(QmlUtilities::sharedEngine(), parent) |
| 47 | +{ |
| 48 | + setTitle(tr("HDR Preview")); |
| 49 | + setResizeMode(QQuickView::SizeRootObjectToView); |
| 50 | + setColor(Qt::black); |
| 51 | + |
| 52 | + // Request HDR swapchain via the internal Qt scene graph property. |
| 53 | + // Qt's video fragment shaders only select the linear HDR output path |
| 54 | + // (nv12_bt2020_hlg_linear.frag) for HDRExtendedSrgbLinear, not for |
| 55 | + // HDRExtendedDisplayP3Linear. So we must use "scrgb" on all platforms. |
| 56 | + setProperty("_qt_sg_hdr_format", QByteArrayLiteral("scrgb")); |
| 57 | + |
| 58 | + rootContext()->setContextProperty("hdrWindow", this); |
| 59 | + |
| 60 | + QDir qmlDir = QmlUtilities::qmlDir(); |
| 61 | + setSource(QUrl::fromLocalFile(qmlDir.filePath("views/HdrPreview.qml"))); |
| 62 | + |
| 63 | + resize(960, 540); |
| 64 | + |
| 65 | +#ifdef Q_OS_MACOS |
| 66 | + // Override NSScreen.maximumExtendedDynamicRangeColorComponentValue so that |
| 67 | + // Qt's video shader outputs > 1.0 values (HDR) on the first frame, which |
| 68 | + // then causes macOS to allocate real EDR headroom. |
| 69 | + macosOverrideEdrHeadroom(true); |
| 70 | + |
| 71 | + // Monitor EDR headroom every second for the first 30 seconds. |
| 72 | + connect(&m_edrTimer, &QTimer::timeout, this, &HdrPreviewWindow::checkEdrHeadroom); |
| 73 | + m_edrTimer.start(1000); |
| 74 | +#endif |
| 75 | +} |
| 76 | + |
| 77 | +HdrPreviewWindow::~HdrPreviewWindow() |
| 78 | +{ |
| 79 | +#ifdef Q_OS_MACOS |
| 80 | + macosOverrideEdrHeadroom(false); |
| 81 | +#endif |
| 82 | +} |
| 83 | + |
| 84 | +void HdrPreviewWindow::setVideoSink(QVideoSink *sink) |
| 85 | +{ |
| 86 | + m_videoSink = sink; |
| 87 | +} |
| 88 | + |
| 89 | +void HdrPreviewWindow::pushFrame(const QVideoFrame &frame) |
| 90 | +{ |
| 91 | + if (m_videoSink && isVisible()) { |
| 92 | + if (!m_loggedSwapChain) { |
| 93 | + m_loggedSwapChain = true; |
| 94 | + auto *sc = swapChain(); |
| 95 | + if (sc) { |
| 96 | + qDebug() << "HDR Preview: swapChain format =" << sc->format() |
| 97 | + << "hdrInfo =" << sc->hdrInfo(); |
| 98 | + } else { |
| 99 | + qDebug() << "HDR Preview: swapChain() returned nullptr!"; |
| 100 | + } |
| 101 | + qDebug() << "HDR Preview frame: pixelFormat =" << frame.surfaceFormat().pixelFormat() |
| 102 | + << "colorTransfer =" << frame.surfaceFormat().colorTransfer() |
| 103 | + << "maxLuminance =" << frame.surfaceFormat().maxLuminance(); |
| 104 | +#ifdef Q_OS_MACOS |
| 105 | + auto wid = winId(); |
| 106 | + qDebug() << "HDR Preview EDR:" |
| 107 | + << "current =" << macosCurrentEdrHeadroom(wid) |
| 108 | + << "potential =" << macosPotentialEdrHeadroom(wid) |
| 109 | + << "reference =" << macosReferenceEdrHeadroom(wid); |
| 110 | +#endif |
| 111 | + } |
| 112 | + updateHdrGain(); |
| 113 | + m_videoSink->setVideoFrame(frame); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +void HdrPreviewWindow::setHlg(bool isHlg) |
| 118 | +{ |
| 119 | + if (m_isHlg != isHlg) { |
| 120 | + m_isHlg = isHlg; |
| 121 | + if (!m_isHlg && !qFuzzyCompare(m_hdrGain, 1.0f)) { |
| 122 | + m_hdrGain = 1.0f; |
| 123 | + emit hdrGainChanged(); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +void HdrPreviewWindow::updateHdrGain() |
| 129 | +{ |
| 130 | + if (!m_isHlg) |
| 131 | + return; |
| 132 | + |
| 133 | + auto *sc = swapChain(); |
| 134 | + if (!sc || sc->format() != QRhiSwapChain::HDRExtendedSrgbLinear) |
| 135 | + return; |
| 136 | + |
| 137 | + auto info = sc->hdrInfo(); |
| 138 | + float headroom = 1.0f; |
| 139 | + if (info.limitsType == QRhiSwapChainHdrInfo::ColorComponentValue) |
| 140 | + headroom = info.limits.colorComponentValue.maxColorComponentValue; |
| 141 | + |
| 142 | + if (headroom <= 1.0f) |
| 143 | + return; |
| 144 | + |
| 145 | + // Qt's HLG shader has a bug: maxLum is HLG-encoded but used as a linear |
| 146 | + // multiplier in the OOTF. Compensate by multiplying the rendered output |
| 147 | + // by the ratio of the correct linear value to the HLG-encoded one. |
| 148 | + float newGain = headroom / hlgOetf(headroom); |
| 149 | + if (!qFuzzyCompare(newGain, m_hdrGain)) { |
| 150 | + m_hdrGain = newGain; |
| 151 | + qDebug() << "HDR Preview: gain =" << m_hdrGain << "(headroom =" << headroom << ")"; |
| 152 | + emit hdrGainChanged(); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +void HdrPreviewWindow::checkEdrHeadroom() |
| 157 | +{ |
| 158 | +#ifdef Q_OS_MACOS |
| 159 | + float headroom = macosCurrentEdrHeadroom(winId()); |
| 160 | + if (headroom != m_lastLoggedHeadroom) { |
| 161 | + m_lastLoggedHeadroom = headroom; |
| 162 | + auto *sc = swapChain(); |
| 163 | + qDebug() << "HDR Preview: EDR headroom =" << headroom |
| 164 | + << "(swapChain hdrInfo =" << (sc ? sc->hdrInfo() : QRhiSwapChainHdrInfo()) << ")"; |
| 165 | + } |
| 166 | + if (++m_edrCheckCount >= 30) |
| 167 | + m_edrTimer.stop(); |
| 168 | +#endif |
| 169 | +} |
0 commit comments