summaryrefslogtreecommitdiff
path: root/src/hardwareintegration/client/vulkan-server/vulkanserverbufferintegration.cpp
blob: e3bcef1f8f9c58f3b57ade432037eddb51470da1 (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
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "vulkanserverbufferintegration.h"
#include <QtWaylandClient/private/qwaylanddisplay_p.h>
#include <QDebug>
#include <QtOpenGL/QOpenGLTexture>
#include <QtGui/QOpenGLContext>
#include <QtGui/qopengl.h>
#include <QtGui/QImage>
#include <QtCore/QCoreApplication>

QT_BEGIN_NAMESPACE

namespace QtWaylandClient {

static constexpr bool extraDebug =
#ifdef VULKAN_SERVER_BUFFER_EXTRA_DEBUG
    true;
#else
    false;
#endif

#define DECL_GL_FUNCTION(name, type) \
    type name

#define FIND_GL_FUNCTION(name, type) \
    do { \
        name = reinterpret_cast<type>(glContext->getProcAddress(#name)); \
        if (!name) {                                                    \
            qWarning() << "ERROR in GL proc lookup. Could not find " #name; \
            return false;                                               \
        } \
    } while (0)

struct VulkanServerBufferGlFunctions
{
    DECL_GL_FUNCTION(glCreateMemoryObjectsEXT, PFNGLCREATEMEMORYOBJECTSEXTPROC);
    DECL_GL_FUNCTION(glImportMemoryFdEXT, PFNGLIMPORTMEMORYFDEXTPROC);
    DECL_GL_FUNCTION(glTextureStorageMem2DEXT, PFNGLTEXTURESTORAGEMEM2DEXTPROC);
    DECL_GL_FUNCTION(glTexStorageMem2DEXT, PFNGLTEXSTORAGEMEM2DEXTPROC);
    DECL_GL_FUNCTION(glDeleteMemoryObjectsEXT, PFNGLDELETEMEMORYOBJECTSEXTPROC);

    bool init(QOpenGLContext *glContext)
    {
        FIND_GL_FUNCTION(glCreateMemoryObjectsEXT, PFNGLCREATEMEMORYOBJECTSEXTPROC);
        FIND_GL_FUNCTION(glImportMemoryFdEXT, PFNGLIMPORTMEMORYFDEXTPROC);
        FIND_GL_FUNCTION(glTextureStorageMem2DEXT, PFNGLTEXTURESTORAGEMEM2DEXTPROC);
        FIND_GL_FUNCTION(glTexStorageMem2DEXT, PFNGLTEXSTORAGEMEM2DEXTPROC);
        FIND_GL_FUNCTION(glDeleteMemoryObjectsEXT, PFNGLDELETEMEMORYOBJECTSEXTPROC);

        return true;
    }
    static bool create(QOpenGLContext *glContext);
};

static VulkanServerBufferGlFunctions *funcs = nullptr;

bool VulkanServerBufferGlFunctions::create(QOpenGLContext *glContext)
{
    if (funcs)
        return true;
    funcs = new VulkanServerBufferGlFunctions;
    if (!funcs->init(glContext)) {
        delete funcs;
        funcs = nullptr;
        return false;
    }
    return true;
}

VulkanServerBuffer::VulkanServerBuffer(VulkanServerBufferIntegration *integration, struct ::qt_server_buffer *id,
                                       int32_t fd, uint32_t width, uint32_t height, uint32_t memory_size, uint32_t format)
    : m_integration(integration)
    , m_server_buffer(id)
    , m_fd(fd)
    , m_memorySize(memory_size)
    , m_internalFormat(format)
{
    m_size = QSize(width, height);
}

VulkanServerBuffer::~VulkanServerBuffer()
{
    if (QCoreApplication::closingDown())
        return; // can't trust anything at this point

    if (m_texture) { //only do gl cleanup if import has been called
        m_integration->deleteGLTextureWhenPossible(m_texture);

        if (extraDebug) qDebug() << "glDeleteMemoryObjectsEXT" << m_memoryObject;
        funcs->glDeleteMemoryObjectsEXT(1, &m_memoryObject);
    }
    qt_server_buffer_release(m_server_buffer);
    qt_server_buffer_destroy(m_server_buffer);
}

void VulkanServerBuffer::import()
{
    if (m_texture)
        return;

    if (extraDebug) qDebug() << "importing" << m_fd << Qt::hex << glGetError();

    auto *glContext = QOpenGLContext::currentContext();
    if (!glContext)
        return;

    if (!funcs && !VulkanServerBufferGlFunctions::create(glContext))
        return;

    funcs->glCreateMemoryObjectsEXT(1, &m_memoryObject);
    if (extraDebug) qDebug() << "glCreateMemoryObjectsEXT" << Qt::hex << glGetError();
    funcs->glImportMemoryFdEXT(m_memoryObject, m_memorySize, GL_HANDLE_TYPE_OPAQUE_FD_EXT, m_fd);
    if (extraDebug) qDebug() << "glImportMemoryFdEXT" << Qt::hex << glGetError();


    m_texture = new QOpenGLTexture(QOpenGLTexture::Target2D);
    m_texture->create();

    if (extraDebug) qDebug() << "created texture" << m_texture->textureId() << Qt::hex << glGetError();

    m_texture->bind();
    if (extraDebug) qDebug() << "bound texture" << Qt::hex << glGetError();
    funcs->glTexStorageMem2DEXT(GL_TEXTURE_2D, 1, m_internalFormat, m_size.width(), m_size.height(), m_memoryObject, 0 );
    if (extraDebug) qDebug() << "glTexStorageMem2DEXT" << Qt::hex << glGetError();
    if (extraDebug) qDebug() << "format" << Qt::hex  << m_internalFormat << GL_RGBA8;
}

QOpenGLTexture *VulkanServerBuffer::toOpenGlTexture()
{
    m_integration->deleteOrphanedTextures();
    if (!m_texture)
        import();
    return m_texture;
}

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

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

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

void VulkanServerBufferIntegration::zqt_vulkan_server_buffer_v1_server_buffer_created(qt_server_buffer *id, int32_t fd, uint32_t width, uint32_t height, uint32_t memory_size, uint32_t format)
{
    if (extraDebug) qDebug() << "vulkan_server_buffer_server_buffer_created" << fd;
    auto *server_buffer = new VulkanServerBuffer(this, id, fd, width, height, memory_size, format);
    qt_server_buffer_set_user_data(id, server_buffer);
}

void VulkanServerBufferIntegration::deleteOrphanedTextures()
{
    if (!QOpenGLContext::currentContext()) {
        qWarning("VulkanServerBufferIntegration::deleteOrphanedTextures with no current context!");
        return;
    }
    qDeleteAll(orphanedTextures);
    orphanedTextures.clear();
}

}

QT_END_NAMESPACE