summaryrefslogtreecommitdiffstats
path: root/src/hardwareintegration/client/shm-emulation-server/shmserverbufferintegration.cpp
blob: a2df1b84cc721d325a83a65e92e62cd8fad854fd (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
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "shmserverbufferintegration.h"
#include <QtWaylandClient/private/qwaylanddisplay_p.h>
#include <QDebug>
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLTexture>
#include <QtGui/QImage>
#include <QtCore/QSharedMemory>

QT_BEGIN_NAMESPACE

static QOpenGLTexture *createTextureFromShm(const QString &key, int w, int h, int bpl, int format)
{
    QSharedMemory shm(key);
    bool ok;
    ok = shm.attach(QSharedMemory::ReadOnly);
    if (!ok) {
        qWarning() << "Could not attach to" << key;
        return nullptr;
    }
    ok = shm.lock();
    if (!ok) {
        qWarning() << "Could not lock" << key << "for reading";
        return nullptr;
    }

    QImage::Format imageFormat;
    switch (format) {
        case QtWayland::qt_shm_emulation_server_buffer::format_RGBA32:
            imageFormat = QImage::Format_RGBA8888;
            break;
        case QtWayland::qt_shm_emulation_server_buffer::format_A8:
            imageFormat = QImage::Format_Alpha8;
            break;
        default:
            qWarning() << "ShmServerBuffer: unknown format" << format;
            imageFormat = QImage::Format_RGBA8888;
            break;
    }

    QImage image(static_cast<const uchar*>(shm.constData()), w, h, bpl, imageFormat);

    if (!QOpenGLContext::currentContext())
        qWarning("ShmServerBuffer: creating texture with no current context");

    auto *tex = new QOpenGLTexture(image, QOpenGLTexture::DontGenerateMipMaps);
    shm.unlock();
    return tex;
}


namespace QtWaylandClient {

ShmServerBuffer::ShmServerBuffer(const QString &key, const QSize& size, int bytesPerLine, QWaylandServerBuffer::Format format)
    : m_key(key)
    , m_bpl(bytesPerLine)
{
    m_format = format;
    m_size = size;
}

ShmServerBuffer::~ShmServerBuffer()
{
}

QOpenGLTexture *ShmServerBuffer::toOpenGlTexture()
{
    if (!m_texture)
        m_texture = createTextureFromShm(m_key, m_size.width(), m_size.height(), m_bpl, m_format);

    return m_texture;
}

void ShmServerBufferIntegration::initialize(QWaylandDisplay *display)
{
    m_display = display;
    display->addRegistryListener(&wlDisplayHandleGlobal, this);
}

QWaylandServerBuffer *ShmServerBufferIntegration::serverBuffer(struct qt_server_buffer *buffer)
{
    return static_cast<QWaylandServerBuffer *>(qt_server_buffer_get_user_data(buffer));
}

void ShmServerBufferIntegration::wlDisplayHandleGlobal(void *data, ::wl_registry *registry, uint32_t id, const QString &interface, uint32_t version)
{
    Q_UNUSED(version);
    if (interface == "qt_shm_emulation_server_buffer") {
        auto *integration = static_cast<ShmServerBufferIntegration *>(data);
        integration->QtWayland::qt_shm_emulation_server_buffer::init(registry, id, 1);
    }
}


void QtWaylandClient::ShmServerBufferIntegration::shm_emulation_server_buffer_server_buffer_created(qt_server_buffer *id, const QString &key, int32_t width, int32_t height, int32_t bytes_per_line, int32_t format)
{
    QSize size(width, height);
    auto fmt = QWaylandServerBuffer::Format(format);
    auto *server_buffer = new ShmServerBuffer(key, size, bytes_per_line, fmt);
    qt_server_buffer_set_user_data(id, server_buffer);
}

}

QT_END_NAMESPACE