summaryrefslogtreecommitdiff
path: root/examples/wayland/server-buffer/compositor/sharebufferextension.cpp
blob: b1b499ffab2607a5eb6b1144cfd74108f197661d (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) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "sharebufferextension.h"

#include <QWaylandSurface>

#include <QDebug>

#include <QQuickWindow>

#include <QPainter>
#include <QPen>

ShareBufferExtension::ShareBufferExtension(QWaylandCompositor *compositor)
    : QWaylandCompositorExtensionTemplate(compositor)
{
}

void ShareBufferExtension::initialize()
{
    QWaylandCompositorExtensionTemplate::initialize();
    QWaylandCompositor *compositor = static_cast<QWaylandCompositor *>(extensionContainer());
    init(compositor->display(), 1);
}

QtWayland::ServerBuffer *ShareBufferExtension::addImage(const QImage &img)
{
    if (!m_server_buffer_integration) {
        QWaylandCompositor *compositor = static_cast<QWaylandCompositor *>(extensionContainer());

        m_server_buffer_integration = QWaylandCompositorPrivate::get(compositor)->serverBufferIntegration();
        if (!m_server_buffer_integration) {
            qWarning("Could not find a server buffer integration");
            return nullptr;
        }
    }

    QImage image = img.convertToFormat(QImage::Format_RGBA8888);

    auto *buffer = m_server_buffer_integration->createServerBufferFromImage(image, QtWayland::ServerBuffer::RGBA32);

    m_server_buffers.append(buffer);
    return buffer;
}

void ShareBufferExtension::createServerBuffers()
{
    QImage image(100,100,QImage::Format_ARGB32_Premultiplied);
    image.fill(QColor(0x55,0x0,0x55,0x01));
    {
        QPainter p(&image);
        QPen pen = p.pen();
        pen.setWidthF(3);
        pen.setColor(Qt::red);
        p.setPen(pen);
        p.drawLine(0,0,100,100);
        pen.setColor(Qt::green);
        p.setPen(pen);
        p.drawLine(100,0,0,100);
        pen.setColor(Qt::blue);
        p.setPen(pen);
        p.drawLine(25,15,75,15);
    }

    addImage(image);

    QImage image2(":/images/Siberischer_tiger_de_edit02.jpg");
    addImage(image2);

    m_server_buffers_created = true;
}


void ShareBufferExtension::share_buffer_bind_resource(Resource *resource)
{
    if (!m_server_buffers_created)
        createServerBuffers();

    for (auto *buffer : std::as_const(m_server_buffers)) {
        qDebug() << "sending" << buffer << "to client";
        struct ::wl_client *client = wl_resource_get_client(resource->handle);
        struct ::wl_resource *buffer_resource = buffer->resourceForClient(client);
        send_cross_buffer(resource->handle, buffer_resource);
    }
}