summaryrefslogtreecommitdiffstats
path: root/src/gui/painting/qrhibackingstore.cpp
blob: 3d9932e5ee268839ec697eb3d43f26995417f58a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qrhibackingstore_p.h"
#include "qpa/qplatformwindow.h"
#include <private/qimage_p.h>

QT_BEGIN_NAMESPACE

QRhiBackingStore::QRhiBackingStore(QWindow *window)
    : QRasterBackingStore(window)
{
}

QRhiBackingStore::~QRhiBackingStore()
{
}

void QRhiBackingStore::flush(QWindow *flushedWindow, const QRegion &region, const QPoint &offset)
{
    Q_UNUSED(region);
    Q_UNUSED(offset);

    if (!rhi(flushedWindow)) {
        QPlatformBackingStoreRhiConfig rhiConfig;
        switch (flushedWindow->surfaceType()) {
        case QSurface::OpenGLSurface:
            rhiConfig.setApi(QPlatformBackingStoreRhiConfig::OpenGL);
            break;
        case QSurface::MetalSurface:
            rhiConfig.setApi(QPlatformBackingStoreRhiConfig::Metal);
            break;
        case QSurface::Direct3DSurface:
            rhiConfig.setApi(QPlatformBackingStoreRhiConfig::D3D11);
            break;
        case QSurface::VulkanSurface:
            rhiConfig.setApi(QPlatformBackingStoreRhiConfig::Vulkan);
            break;
        default:
            Q_UNREACHABLE();
        }

        rhiConfig.setEnabled(true);
        createRhi(flushedWindow, rhiConfig);
    }

    // The backing store operates on behalf of its window(), even if we're
    // flushing a child window, so pull the source DPR from the window().
    const qreal sourceDevicePixelRatio = window()->devicePixelRatio();

    // QBackingStore::flush will convert the region and offset from device independent
    // pixels to native pixels before calling QPlatformBackingStore::flush, which means
    // we can't pass on the window's DPR as the sourceTransformFactor, as that will include
    // the Qt scale factor, which has already been applied. Instead we ask the platform
    // window, which only reflect the remaining scale factor from the OS.
    const qreal sourceTransformFactor = flushedWindow->handle()->devicePixelRatio();

    static QPlatformTextureList emptyTextureList;
    bool translucentBackground = m_image.hasAlphaChannel();
    rhiFlush(flushedWindow, sourceDevicePixelRatio,
        region, offset, &emptyTextureList, translucentBackground,
        sourceTransformFactor);
}

QImage::Format QRhiBackingStore::format() const
{
    QImage::Format fmt = QRasterBackingStore::format();

    // With render-to-texture widgets and QRhi-based flushing the backingstore
    // image must have an alpha channel. Hence upgrading the format. Matches
    // what other platforms (Windows, xcb) do.
    if (QImage::toPixelFormat(fmt).alphaUsage() != QPixelFormat::UsesAlpha)
        fmt = qt_maybeDataCompatibleAlphaVersion(fmt);

    return fmt;
}

QT_END_NAMESPACE