summaryrefslogtreecommitdiffstats
path: root/src/compositor/compositor_api/waylandsurfacenode.cpp
blob: e6571751c765924abcdc8703dfb141dcc6cd621d (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
#include "waylandsurfacenode.h"
#include "waylandsurfaceitem.h"

#include <QtCore/QMutexLocker>
#include <QtQuick/QSGTexture>
#include <QtQuick/QSGSimpleTextureNode>
#include <QtQuick/QSGFlatColorMaterial>

WaylandSurfaceNode::WaylandSurfaceNode(WaylandSurfaceItem *item)
    : m_item(item)
    , m_textureUpdated(false)
    , m_useTextureAlpha(false)
    , m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4)
{
    m_textureMaterial = WaylandSurfaceTextureMaterial::createMaterial();
    m_opaqueTextureMaterial = WaylandSurfaceTextureOpaqueMaterial::createMaterial();

    m_currentMaterial = m_opaqueTextureMaterial;

    setGeometry(&m_geometry);
    setMaterial(m_currentMaterial);

    if (m_item)
        m_item->m_node = this;
    setFlag(UsePreprocess,true);
}


WaylandSurfaceNode::~WaylandSurfaceNode()
{
    QMutexLocker locker(WaylandSurfaceItem::mutex);
    if (m_item)
        m_item->m_node = 0;
    delete m_textureMaterial;
    delete m_opaqueTextureMaterial;
}

void WaylandSurfaceNode::preprocess()
{
    QMutexLocker locker(WaylandSurfaceItem::mutex);

    //Update if the item is dirty and we haven't done an updateTexture for this frame
    if (m_item && m_item->m_damaged && !m_textureUpdated) {
        m_item->updateTexture();
        updateTexture();
    }
    //Reset value for next frame: we have not done updatePaintNode yet
    m_textureUpdated = false;
}

void WaylandSurfaceNode::updateTexture()
{
    Q_ASSERT(m_item && m_item->textureProvider());

    //If m_item->useTextureAlpha has changed to true use m_texureMaterial
    //otherwise use m_opaqueTextureMaterial.
    if (m_item->useTextureAlpha() != m_useTextureAlpha) {
        m_useTextureAlpha = m_item->useTextureAlpha();
        if (m_useTextureAlpha) {
            m_currentMaterial = m_textureMaterial;
        } else {
            m_currentMaterial = m_opaqueTextureMaterial;
        }
        setMaterial(m_currentMaterial);
    }

    QSGTexture *texture = m_item->textureProvider()->texture();
    setTexture(texture);
}

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

    if (texture()) {
        QSize ts = texture()->textureSize();
        QRectF sourceRect(0, 0, ts.width(), ts.height());
        QSGGeometry::updateTexturedRectGeometry(&m_geometry, m_rect, texture()->convertToNormalizedSourceRect(sourceRect));
    }
}

void WaylandSurfaceNode::setTexture(QSGTexture *texture)
{
    if (m_currentMaterial->state()->texture() == texture)
        return;
    m_currentMaterial->state()->setTexture(texture);

    QSize ts = texture->textureSize();
    QRectF sourceRect(0, 0, ts.width(), ts.height());
    QSGGeometry::updateTexturedRectGeometry(&m_geometry, m_rect, texture->convertToNormalizedSourceRect(sourceRect));
    markDirty(DirtyMaterial);
}

QSGTexture *WaylandSurfaceNode::texture() const
{
    return m_currentMaterial->state()->texture();
}

void WaylandSurfaceNode::setItem(WaylandSurfaceItem *item)
{
    m_item = item;
}