summaryrefslogtreecommitdiffstats
path: root/src/imports/nativemedia/omxnode.cpp
blob: c3336229734477b2c8a5a96a0fb19cd585e841d0 (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
177
178
179
180
181
182
183
184
185
186
#include "omxnode.h"

#include <QtGui/QOpenGLContext>
#include <QtQuick/qsgtexture.h>
#include <QtQuick/qquickwindow.h>

#include <QTimer>

static const char omx_texture_material_vertex[] =
           "uniform highp mat4 qt_Matrix;                      \n"
           "attribute highp vec4 qt_VertexPosition;            \n"
           "attribute highp vec2 qt_VertexTexCoord;            \n"
           "varying highp vec2 qt_TexCoord;                    \n"
           "void main() {                                      \n"
           "    qt_TexCoord = qt_VertexTexCoord;               \n"
           "    gl_Position = qt_Matrix * qt_VertexPosition;   \n"
           "}";


static const char omx_texture_material_fragment[] =
           "#extension GL_OES_EGL_image_external : require     \n"
           "varying highp vec2 qt_TexCoord;                    \n"
           "uniform samplerExternalOES qt_Texture;             \n"
           "uniform lowp float qt_Opacity;                     \n"
           "void main() {                                      \n"
           "    gl_FragColor = texture2D(qt_Texture, qt_TexCoord) * qt_Opacity; \n"
           "}";

QList<QByteArray> OmxTextureMaterial::attributes() const
{
    QList<QByteArray> attributeList;
    attributeList << "qt_VertexPosition";
    attributeList << "qt_VertexTexCoord";
    return attributeList;
}

void OmxTextureMaterial::updateState(const OmxTextureState *newState, const OmxTextureState *oldState)
{
    Q_UNUSED(oldState);
    newState->player->updateTexture();
}

const char *OmxTextureMaterial::vertexShader() const
{
    return omx_texture_material_vertex;
}

const char *OmxTextureMaterial::fragmentShader() const
{
    return omx_texture_material_fragment;
}

OmxNode::OmxNode(OmxPlayer *player)
    : m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4)
    , m_player(player)
    , m_initialized(false)
{
    m_textureMaterial = OmxTextureMaterial::createMaterial();
    m_textureMaterial->state()->player = player;

    setGeometry(&m_geometry);
    setMaterial(m_textureMaterial);

    setFlag(UsePreprocess, true);
}

OmxNode::~OmxNode()
{
    delete m_textureMaterial;
}

void OmxNode::preprocess()
{
}

void OmxNode::setRect(const QRectF &rect)
{
    if (m_rect == rect)
        return;

    printf("OmxNode::setRect(%f %f %f %f)\n", rect.x(), rect.y(), rect.width(), rect.height());
    m_rect = rect;

    QRectF sourceRect(0, 0, 1, 1);
    QSGGeometry::updateTexturedRectGeometry(&m_geometry, m_rect, sourceRect);
}

OmxItem::OmxItem()
    : m_player(OmxPlayer::create())
    , m_hasFrame(false)
    , m_initialized(false)
    , m_paused(false)
    , m_sourceWidth(0)
    , m_sourceHeight(0)
{
    connect(m_player, SIGNAL(frameAvailable()), this, SLOT(triggerRender()));
    connect(m_player, SIGNAL(videoSize(int, int)), this, SLOT(videoSize(int, int)));

    setFlag(ItemHasContents, true);
}

void OmxItem::itemChange(ItemChange change, const ItemChangeData &)
{
    if (change == ItemSceneChange) {
        QQuickWindow *win = window();
        if (!win)
            return;

        // Connect the beforeRendering signal to our paint function.
        // Since this call is executed on the rendering thread it must be
        // a Qt::DirectConnection
        connect(win, SIGNAL(beforeRendering()), this, SLOT(beforeRendering()), Qt::DirectConnection);
    }
}


OmxItem::~OmxItem()
{
    delete m_player;
}

void OmxItem::triggerRender()
{
    m_hasFrame = true;
    update();
}

void OmxItem::videoSize(int w, int h)
{
    m_sourceWidth = w;
    m_sourceHeight = h;

    emit sourceWidthChanged();
    emit sourceHeightChanged();
}

void OmxItem::setSource(const QString &source)
{
    if (m_initialized || source == m_source)
        return;

    m_source = source;
    emit sourceChanged();
    update();
}

void OmxItem::beforeRendering()
{
    if (m_initialized || m_source.isNull())
        return;

    m_initialized = m_player->initialize(m_source.toLocal8Bit());

    GLuint tid;
    glGenTextures(1, &tid);

    // start playing if not paused
    if (m_initialized && !paused())
        m_player->setPaused(false);
}

QSGNode *OmxItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
{
    if (!m_hasFrame)
        return 0;

    OmxNode *node;
    if (oldNode)
        node = static_cast<OmxNode *>(oldNode);
    else
        node = new OmxNode(m_player);

    node->setRect(boundingRect());
    node->markDirty(QSGNode::DirtyMaterial);

    return node;
}

void OmxItem::setPaused(bool p)
{
    if (p == m_paused)
        return;
    m_player->setPaused(p);
    m_paused = p;
    emit pausedChanged();
}