| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | #include "qsgvideonode_texture_p.h" |
| 40 | #include <QtQuick/qsgtexturematerial.h> |
| 41 | #include <QtQuick/qsgmaterial.h> |
| 42 | #include <QtCore/qmutex.h> |
| 43 | #include <QtGui/QOpenGLContext> |
| 44 | #include <QtGui/QOpenGLFunctions> |
| 45 | #include <QtGui/QOpenGLShaderProgram> |
| 46 | #include <QtMultimedia/private/qmediaopenglhelper_p.h> |
| 47 | #include <QtMultimedia/private/qtmultimediaglobal_p.h> |
| 48 | |
| 49 | QT_BEGIN_NAMESPACE |
| 50 | |
| 51 | QList<QVideoFrame::PixelFormat> QSGVideoNodeFactory_Texture::supportedPixelFormats( |
| 52 | QAbstractVideoBuffer::HandleType handleType) const |
| 53 | { |
| 54 | QList<QVideoFrame::PixelFormat> pixelFormats; |
| 55 | |
| 56 | #ifdef Q_OS_MACOS |
| 57 | if (handleType == QAbstractVideoBuffer::GLTextureRectangleHandle) { |
| 58 | pixelFormats.append(QVideoFrame::Format_BGR32); |
| 59 | pixelFormats.append(QVideoFrame::Format_BGRA32); |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | if (handleType == QAbstractVideoBuffer::GLTextureHandle) { |
| 64 | pixelFormats.append(t: QVideoFrame::Format_RGB565); |
| 65 | pixelFormats.append(t: QVideoFrame::Format_RGB32); |
| 66 | pixelFormats.append(t: QVideoFrame::Format_ARGB32); |
| 67 | pixelFormats.append(t: QVideoFrame::Format_BGR32); |
| 68 | pixelFormats.append(t: QVideoFrame::Format_BGRA32); |
| 69 | #if !QT_CONFIG(gpu_vivante) |
| 70 | pixelFormats.append(t: QVideoFrame::Format_ABGR32); |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | return pixelFormats; |
| 75 | } |
| 76 | |
| 77 | QSGVideoNode *QSGVideoNodeFactory_Texture::createNode(const QVideoSurfaceFormat &format) |
| 78 | { |
| 79 | if (supportedPixelFormats(handleType: format.handleType()).contains(t: format.pixelFormat())) |
| 80 | return new QSGVideoNode_Texture(format); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | class QSGVideoMaterialShader_Texture : public QSGMaterialShader |
| 87 | { |
| 88 | public: |
| 89 | QSGVideoMaterialShader_Texture() |
| 90 | : QSGMaterialShader() |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
| 95 | |
| 96 | char const *const *attributeNames() const override { |
| 97 | static const char *names[] = { |
| 98 | "qt_VertexPosition" , |
| 99 | "qt_VertexTexCoord" , |
| 100 | 0 |
| 101 | }; |
| 102 | return names; |
| 103 | } |
| 104 | |
| 105 | protected: |
| 106 | void initialize() override { |
| 107 | m_id_matrix = program()->uniformLocation(name: "qt_Matrix" ); |
| 108 | m_id_Texture = program()->uniformLocation(name: "rgbTexture" ); |
| 109 | m_id_opacity = program()->uniformLocation(name: "opacity" ); |
| 110 | } |
| 111 | |
| 112 | int m_id_matrix; |
| 113 | int m_id_Texture; |
| 114 | int m_id_opacity; |
| 115 | }; |
| 116 | |
| 117 | class QSGVideoMaterialShader_Texture_2D : public QSGVideoMaterialShader_Texture |
| 118 | { |
| 119 | public: |
| 120 | QSGVideoMaterialShader_Texture_2D() |
| 121 | { |
| 122 | setShaderSourceFile(type: QOpenGLShader::Vertex, QStringLiteral(":/qtmultimediaquicktools/shaders/monoplanarvideo.vert" )); |
| 123 | setShaderSourceFile(type: QOpenGLShader::Fragment, QStringLiteral(":/qtmultimediaquicktools/shaders/rgbvideo.frag" )); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | class QSGVideoMaterialShader_Texture_2D_swizzle : public QSGVideoMaterialShader_Texture_2D |
| 128 | { |
| 129 | public: |
| 130 | QSGVideoMaterialShader_Texture_2D_swizzle(bool hasAlpha) |
| 131 | : m_hasAlpha(hasAlpha) |
| 132 | { |
| 133 | setShaderSourceFile(type: QOpenGLShader::Fragment, QStringLiteral(":/qtmultimediaquicktools/shaders/rgbvideo_swizzle.frag" )); |
| 134 | } |
| 135 | |
| 136 | protected: |
| 137 | void initialize() override { |
| 138 | QSGVideoMaterialShader_Texture_2D::initialize(); |
| 139 | program()->setUniformValue(location: program()->uniformLocation(name: "hasAlpha" ), value: GLboolean(m_hasAlpha)); |
| 140 | } |
| 141 | |
| 142 | int m_hasAlpha; |
| 143 | }; |
| 144 | |
| 145 | class QSGVideoMaterial_Texture : public QSGMaterial |
| 146 | { |
| 147 | public: |
| 148 | QSGVideoMaterial_Texture(const QVideoSurfaceFormat &format) : |
| 149 | m_format(format), |
| 150 | m_textureId(0), |
| 151 | m_opacity(1.0) |
| 152 | { |
| 153 | setFlag(flags: Blending, on: false); |
| 154 | } |
| 155 | |
| 156 | ~QSGVideoMaterial_Texture() |
| 157 | { |
| 158 | m_frame = QVideoFrame(); |
| 159 | } |
| 160 | |
| 161 | QSGMaterialType *type() const override { |
| 162 | static QSGMaterialType normalType, swizzleType; |
| 163 | return needsSwizzling() ? &swizzleType : &normalType; |
| 164 | } |
| 165 | |
| 166 | int compare(const QSGMaterial *other) const override { |
| 167 | const QSGVideoMaterial_Texture *m = static_cast<const QSGVideoMaterial_Texture *>(other); |
| 168 | |
| 169 | if (!m_textureId) |
| 170 | return 1; |
| 171 | |
| 172 | int diff = m_textureId - m->m_textureId; |
| 173 | if (diff) |
| 174 | return diff; |
| 175 | |
| 176 | diff = m_format.pixelFormat() - m->m_format.pixelFormat(); |
| 177 | if (diff) |
| 178 | return diff; |
| 179 | |
| 180 | return (m_opacity > m->m_opacity) ? 1 : -1; |
| 181 | } |
| 182 | |
| 183 | void updateBlending() { |
| 184 | setFlag(flags: Blending, on: qFuzzyCompare(p1: m_opacity, p2: qreal(1.0)) ? false : true); |
| 185 | } |
| 186 | |
| 187 | void setVideoFrame(const QVideoFrame &frame) { |
| 188 | QMutexLocker lock(&m_frameMutex); |
| 189 | m_frame = frame; |
| 190 | m_textureSize = frame.size(); |
| 191 | } |
| 192 | |
| 193 | virtual void bind() = 0; |
| 194 | |
| 195 | QVideoFrame m_frame; |
| 196 | QMutex m_frameMutex; |
| 197 | QSize m_textureSize; |
| 198 | QVideoSurfaceFormat m_format; |
| 199 | GLuint m_textureId; |
| 200 | qreal m_opacity; |
| 201 | |
| 202 | protected: |
| 203 | bool needsSwizzling() const { |
| 204 | return !QMediaOpenGLHelper::isANGLE() |
| 205 | && (m_format.pixelFormat() == QVideoFrame::Format_RGB32 |
| 206 | || m_format.pixelFormat() == QVideoFrame::Format_ARGB32); |
| 207 | } |
| 208 | }; |
| 209 | |
| 210 | class QSGVideoMaterial_Texture_2D : public QSGVideoMaterial_Texture |
| 211 | { |
| 212 | public: |
| 213 | QSGVideoMaterial_Texture_2D(const QVideoSurfaceFormat &format) : |
| 214 | QSGVideoMaterial_Texture(format) |
| 215 | { |
| 216 | } |
| 217 | |
| 218 | QSGMaterialShader *createShader() const override |
| 219 | { |
| 220 | const bool hasAlpha = m_format.pixelFormat() == QVideoFrame::Format_ARGB32; |
| 221 | return needsSwizzling() ? new QSGVideoMaterialShader_Texture_2D_swizzle(hasAlpha) |
| 222 | : new QSGVideoMaterialShader_Texture_2D; |
| 223 | } |
| 224 | |
| 225 | void bind() override |
| 226 | { |
| 227 | QMutexLocker lock(&m_frameMutex); |
| 228 | if (m_frame.isValid()) { |
| 229 | m_textureId = m_frame.handle().toUInt(); |
| 230 | QOpenGLFunctions *functions = QOpenGLContext::currentContext()->functions(); |
| 231 | functions->glBindTexture(GL_TEXTURE_2D, texture: m_textureId); |
| 232 | |
| 233 | functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 234 | functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 235 | functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 236 | functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 237 | } else { |
| 238 | m_textureId = 0; |
| 239 | } |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | #ifdef Q_OS_MACOS |
| 244 | class QSGVideoMaterialShader_Texture_Rectangle : public QSGVideoMaterialShader_Texture |
| 245 | { |
| 246 | public: |
| 247 | QSGVideoMaterialShader_Texture_Rectangle() |
| 248 | { |
| 249 | setShaderSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/qtmultimediaquicktools/shaders/rectsampler.vert" )); |
| 250 | setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/qtmultimediaquicktools/shaders/rectsampler_rgb.frag" )); |
| 251 | } |
| 252 | |
| 253 | void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override |
| 254 | { |
| 255 | QSGVideoMaterial_Texture *mat = static_cast<QSGVideoMaterial_Texture *>(newMaterial); |
| 256 | QVector2D size(mat->m_textureSize.width(), mat->m_textureSize.height()); |
| 257 | program()->setUniformValue(m_id_videoSize, size); |
| 258 | |
| 259 | QSGVideoMaterialShader_Texture::updateState(state, newMaterial, oldMaterial); |
| 260 | } |
| 261 | |
| 262 | protected: |
| 263 | void initialize() override |
| 264 | { |
| 265 | QSGVideoMaterialShader_Texture::initialize(); |
| 266 | m_id_videoSize = program()->uniformLocation("qt_videoSize" ); |
| 267 | } |
| 268 | |
| 269 | int m_id_videoSize; |
| 270 | }; |
| 271 | |
| 272 | class QSGVideoMaterial_Texture_Rectangle : public QSGVideoMaterial_Texture |
| 273 | { |
| 274 | public: |
| 275 | QSGVideoMaterial_Texture_Rectangle(const QVideoSurfaceFormat &format) : |
| 276 | QSGVideoMaterial_Texture(format) |
| 277 | { |
| 278 | } |
| 279 | |
| 280 | QSGMaterialShader *createShader() const override |
| 281 | { |
| 282 | Q_ASSERT(!needsSwizzling()); |
| 283 | return new QSGVideoMaterialShader_Texture_Rectangle; |
| 284 | } |
| 285 | |
| 286 | void bind() override |
| 287 | { |
| 288 | QMutexLocker lock(&m_frameMutex); |
| 289 | if (m_frame.isValid()) { |
| 290 | m_textureId = m_frame.handle().toUInt(); |
| 291 | QOpenGLFunctions *functions = QOpenGLContext::currentContext()->functions(); |
| 292 | functions->glActiveTexture(GL_TEXTURE0); |
| 293 | functions->glBindTexture(GL_TEXTURE_RECTANGLE, m_textureId); |
| 294 | |
| 295 | functions->glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 296 | functions->glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 297 | functions->glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 298 | functions->glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 299 | } else { |
| 300 | m_textureId = 0; |
| 301 | } |
| 302 | } |
| 303 | }; |
| 304 | #endif |
| 305 | |
| 306 | QSGVideoNode_Texture::QSGVideoNode_Texture(const QVideoSurfaceFormat &format) : |
| 307 | m_format(format) |
| 308 | { |
| 309 | setFlag(QSGNode::OwnsMaterial); |
| 310 | |
| 311 | #ifdef Q_OS_MACOS |
| 312 | if (format.handleType() == QAbstractVideoBuffer::GLTextureRectangleHandle) |
| 313 | m_material = new QSGVideoMaterial_Texture_Rectangle(format); |
| 314 | #endif |
| 315 | |
| 316 | if (!m_material) |
| 317 | m_material = new QSGVideoMaterial_Texture_2D(format); |
| 318 | |
| 319 | setMaterial(m_material); |
| 320 | } |
| 321 | |
| 322 | QSGVideoNode_Texture::~QSGVideoNode_Texture() |
| 323 | { |
| 324 | } |
| 325 | |
| 326 | void QSGVideoNode_Texture::setCurrentFrame(const QVideoFrame &frame, FrameFlags) |
| 327 | { |
| 328 | m_material->setVideoFrame(frame); |
| 329 | markDirty(bits: DirtyMaterial); |
| 330 | } |
| 331 | |
| 332 | void QSGVideoMaterialShader_Texture::updateState(const RenderState &state, |
| 333 | QSGMaterial *newMaterial, |
| 334 | QSGMaterial *oldMaterial) |
| 335 | { |
| 336 | Q_UNUSED(oldMaterial); |
| 337 | QSGVideoMaterial_Texture *mat = static_cast<QSGVideoMaterial_Texture *>(newMaterial); |
| 338 | program()->setUniformValue(location: m_id_Texture, value: 0); |
| 339 | |
| 340 | mat->bind(); |
| 341 | |
| 342 | if (state.isOpacityDirty()) { |
| 343 | mat->m_opacity = state.opacity(); |
| 344 | mat->updateBlending(); |
| 345 | program()->setUniformValue(location: m_id_opacity, value: GLfloat(mat->m_opacity)); |
| 346 | } |
| 347 | |
| 348 | if (state.isMatrixDirty()) |
| 349 | program()->setUniformValue(location: m_id_matrix, value: state.combinedMatrix()); |
| 350 | } |
| 351 | |
| 352 | QT_END_NAMESPACE |
| 353 | |