summaryrefslogtreecommitdiffstats
path: root/tests/auto/unit/mockbackend/qmocksurfacecapture.h
blob: 0924b725516f440a269b8e0447437078264a7d8b (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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef QMOCKSURFACECAPTURE_H
#define QMOCKSURFACECAPTURE_H

#include "private/qplatformsurfacecapture_p.h"

#include "qmockvideobuffer.h"
#include "qthread.h"

QT_BEGIN_NAMESPACE

class QMockSurfaceCapture : public QPlatformSurfaceCapture
{
    class Grabber : public QThread
    {
    public:
        Grabber(QMockSurfaceCapture &capture) : QThread(&capture), m_capture(capture) { }

        void run() override
        {
            for (int i = 0; !isInterruptionRequested(); ++i) {
                QImage image(m_capture.m_imageSize, QImage::Format_ARGB32);

                image.fill(i % 2 ? Qt::red : Qt::blue);

                QVideoFrame frame(new QMockVideoBuffer(image),
                                  QVideoFrameFormat(m_capture.m_imageSize,
                                                    QVideoFrameFormat::pixelFormatFromImageFormat(
                                                            m_capture.m_imageFormat)));

                emit m_capture.newVideoFrame(frame);
            }
        }

    private:
        QMockSurfaceCapture &m_capture;
    };

public:
    using QPlatformSurfaceCapture::QPlatformSurfaceCapture;

    ~QMockSurfaceCapture() { resetGrabber(); }

    bool setActiveInternal(bool active) override
    {
        if (active) {
            m_grabber = std::make_unique<Grabber>(*this);
            m_grabber->start();
        } else {
            resetGrabber();
        }

        return true;
    }

    bool isActive() const override { return bool(m_grabber); }

    QVideoFrameFormat frameFormat() const override
    {
        return m_grabber ? QVideoFrameFormat(
                       m_imageSize, QVideoFrameFormat::pixelFormatFromImageFormat(m_imageFormat))
                         : QVideoFrameFormat{};
    }

private:
    void resetGrabber()
    {
        if (m_grabber) {
            m_grabber->requestInterruption();
            m_grabber->quit();
            m_grabber->wait();
            m_grabber.reset();
        }
    }

private:
    std::unique_ptr<Grabber> m_grabber;
    const QImage::Format m_imageFormat = QImage::Format_ARGB32;
    const QSize m_imageSize = QSize(2, 3);
};

QT_END_NAMESPACE

#endif // QMOCKSURFACECAPTURE_H