qtquick2videosinkdelegate.cpp 3.85 KB
Newer Older
Gus Grubba's avatar
Gus Grubba committed
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
/*
    Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). <qt-info@nokia.com>
    Copyright (C) 2011-2013 Collabora Ltd. <info@collabora.com>

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License version 2.1
    as published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file
 *   @brief Extracted from QtGstreamer to avoid overly complex dependency
 *   @author Gus Grubba <mavlink@grubba.com>
 */

#include "qtquick2videosinkdelegate.h"
#include "../painters/videonode.h"

QtQuick2VideoSinkDelegate::QtQuick2VideoSinkDelegate(GstElement *sink, QObject *parent)
    : BaseDelegate(sink, parent)
{
}

QSGNode* QtQuick2VideoSinkDelegate::updateNode(QSGNode *node, const QRectF & targetArea)
{
    GST_TRACE_OBJECT(m_sink, "updateNode called");
    bool sgnodeFormatChanged = false;

    VideoNode *vnode = dynamic_cast<VideoNode*>(node);
    if (!vnode) {
        GST_INFO_OBJECT(m_sink, "creating new VideoNode");
        vnode = new VideoNode;
    }

    if (!m_buffer) {
        if (vnode->materialType() != VideoNode::MaterialTypeSolidBlack) {
            vnode->setMaterialTypeSolidBlack();
            sgnodeFormatChanged = true;
        }
        if (sgnodeFormatChanged || targetArea != m_areas.targetArea) {
            m_areas.targetArea = targetArea;
            vnode->updateGeometry(m_areas);
        }
    } else {
        //change format before geometry, so that we change QSGGeometry as well
        if (m_formatDirty) {
            vnode->changeFormat(m_bufferFormat);
            sgnodeFormatChanged = true;
        }

        //recalculate the video area if needed
        QReadLocker forceAspectRatioLocker(&m_forceAspectRatioLock);
        if (sgnodeFormatChanged || targetArea != m_areas.targetArea || m_forceAspectRatioDirty) {
            m_forceAspectRatioDirty = false;

            QReadLocker pixelAspectRatioLocker(&m_pixelAspectRatioLock);
            Qt::AspectRatioMode aspectRatioMode = m_forceAspectRatio ?
                    Qt::KeepAspectRatio : Qt::IgnoreAspectRatio;
            m_areas.calculate(targetArea, m_bufferFormat.frameSize(),
                    m_bufferFormat.pixelAspectRatio(), m_pixelAspectRatio,
                    aspectRatioMode);
            pixelAspectRatioLocker.unlock();

            GST_LOG_OBJECT(m_sink,
                "Recalculated paint areas: "
                "Frame size: " QSIZE_FORMAT ", "
                "target area: " QRECTF_FORMAT ", "
                "video area: " QRECTF_FORMAT ", "
                "black1: " QRECTF_FORMAT ", "
                "black2: " QRECTF_FORMAT,
                QSIZE_FORMAT_ARGS(m_bufferFormat.frameSize()),
                QRECTF_FORMAT_ARGS(m_areas.targetArea),
                QRECTF_FORMAT_ARGS(m_areas.videoArea),
                QRECTF_FORMAT_ARGS(m_areas.blackArea1),
                QRECTF_FORMAT_ARGS(m_areas.blackArea2)
            );

            vnode->updateGeometry(m_areas);
        }
        forceAspectRatioLocker.unlock();

        if (m_formatDirty) {
            m_formatDirty = false;

            //make sure to update the colors after changing material
            m_colorsDirty = true;
        }

        QReadLocker colorsLocker(&m_colorsLock);
        if (m_colorsDirty) {
            vnode->updateColors(m_brightness, m_contrast, m_hue, m_saturation);
            m_colorsDirty = false;
        }
        colorsLocker.unlock();

        vnode->setCurrentFrame(m_buffer);
    }

    return vnode;
}