summaryrefslogtreecommitdiffstats
path: root/src/multimedia/video/qvideowindow_p.h
blob: c7324ec3f98bf31e87f7ee77db85b88f679fd009 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QVIDEOWINDOW_P_H
#define QVIDEOWINDOW_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QWindow>
#include <qtextlayout.h>

#include <QtGui/private/qrhinull_p.h>
#if QT_CONFIG(opengl)
#include <QtGui/private/qrhigles2_p.h>
#include <QOffscreenSurface>
#endif
#if QT_CONFIG(vulkan)
#include <QtGui/private/qrhivulkan_p.h>
#endif
#ifdef Q_OS_WIN
#include <QtGui/private/qrhid3d11_p.h>
#endif
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS)
#include <QtGui/private/qrhimetal_p.h>
#endif

#include <qvideoframe.h>
#include <private/qplatformvideosink_p.h>
#include <private/qvideotexturehelper_p.h>
#include <qbackingstore.h>

QT_BEGIN_NAMESPACE

class QVideoWindow;

class QVideoWindowPrivate {
public:
    QVideoWindowPrivate(QVideoWindow *q);
    ~QVideoWindowPrivate();
    bool canRender() const { return m_useRhi; }

    QRhi *rhi() const { return m_rhi.get(); }

    void init();
    void render();

    void initRhi();

    void resizeSwapChain();
    void releaseSwapChain();

    void updateTextures(QRhiResourceUpdateBatch *rub);
    void updateSubtitle(QRhiResourceUpdateBatch *rub);
    void freeTextures();

    void setupGraphicsPipeline(QRhiGraphicsPipeline *pipeline, QRhiShaderResourceBindings *bindings, QVideoFrameFormat::PixelFormat fmt);

    QVideoWindow *q = nullptr;
    Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio;

    QBackingStore *backingStore = nullptr;

#if QT_CONFIG(opengl)
    std::unique_ptr<QOffscreenSurface> m_fallbackSurface;
#endif
    std::unique_ptr<QRhi> m_rhi;
    std::unique_ptr<QRhiSwapChain> m_swapChain;
    std::unique_ptr<QRhiRenderPassDescriptor> m_renderPass;

    std::unique_ptr<QRhiBuffer> m_vertexBuf;
    bool m_vertexBufReady = false;
    std::unique_ptr<QRhiBuffer> m_uniformBuf;
    QRhiTexture *m_frameTextures[3] = {};
    std::unique_ptr<QRhiSampler> m_textureSampler;
    std::unique_ptr<QRhiShaderResourceBindings> m_shaderResourceBindings;
    std::unique_ptr<QRhiGraphicsPipeline> m_graphicsPipeline;

    std::unique_ptr<QRhiTexture> m_subtitleTexture;
    std::unique_ptr<QRhiShaderResourceBindings> m_subtitleResourceBindings;
    std::unique_ptr<QRhiGraphicsPipeline> m_subtitlePipeline;
    std::unique_ptr<QRhiBuffer> m_subtitleUniformBuf;

    std::unique_ptr<QVideoSink> m_sink;
    QRhi::Implementation m_graphicsApi = QRhi::Null;
    QSize m_frameSize = QSize(-1, -1);
    QVideoFrame m_currentFrame;
    QVideoTextureHelper::SubtitleLayout m_subtitleLayout;

    bool initialized = false;
    bool isExposed = false;
    bool m_useRhi = true;
    bool m_hasSwapChain = false;
    bool m_texturesDirty = true;
    bool m_subtitleDirty = false;
    bool m_hasSubtitle = false;
    QVideoFrameFormat::PixelFormat format = QVideoFrameFormat::Format_Invalid;
};

class Q_MULTIMEDIA_EXPORT QVideoWindow : public QWindow
{
    Q_OBJECT
public:
    explicit QVideoWindow(QScreen *screen = nullptr);
    explicit QVideoWindow(QWindow *parent);
    ~QVideoWindow();

    Q_INVOKABLE QVideoSink *videoSink() const;

    Qt::AspectRatioMode aspectRatioMode() const;

public Q_SLOTS:
    void setAspectRatioMode(Qt::AspectRatioMode mode);

Q_SIGNALS:
    void aspectRatioModeChanged(Qt::AspectRatioMode mode);

protected:
    bool event(QEvent *e) override;
    void resizeEvent(QResizeEvent *) override;

private Q_SLOTS:
    void setVideoFrame(const QVideoFrame &frame);

private:
    friend class QVideoWindowPrivate;
    std::unique_ptr<QVideoWindowPrivate> d;
};

QT_END_NAMESPACE

#endif