Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
vulkanserverbufferintegration.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QtWaylandClient/private/qwaylanddisplay_p.h>
6#include <QDebug>
7#include <QtOpenGL/QOpenGLTexture>
8#include <QtGui/QOpenGLContext>
9#include <QtGui/qopengl.h>
10#include <QtGui/QImage>
11#include <QtCore/QCoreApplication>
12
14
15namespace QtWaylandClient {
16
17static constexpr bool sbiExtraDebug =
18#ifdef VULKAN_SERVER_BUFFER_EXTRA_DEBUG
19 true;
20#else
21 false;
22#endif
23
24#define DECL_GL_FUNCTION(name, type) \
25 type name
26
27#define FIND_GL_FUNCTION(name, type) \
28 do { \
29 name = reinterpret_cast<type>(glContext->getProcAddress(#name)); \
30 if (!name) { \
31 qWarning() << "ERROR in GL proc lookup. Could not find " #name; \
32 return false; \
33 } \
34 } while (0)
35
37{
38 DECL_GL_FUNCTION(glCreateMemoryObjectsEXT, PFNGLCREATEMEMORYOBJECTSEXTPROC);
39 DECL_GL_FUNCTION(glImportMemoryFdEXT, PFNGLIMPORTMEMORYFDEXTPROC);
40 DECL_GL_FUNCTION(glTextureStorageMem2DEXT, PFNGLTEXTURESTORAGEMEM2DEXTPROC);
41 DECL_GL_FUNCTION(glTexStorageMem2DEXT, PFNGLTEXSTORAGEMEM2DEXTPROC);
42 DECL_GL_FUNCTION(glDeleteMemoryObjectsEXT, PFNGLDELETEMEMORYOBJECTSEXTPROC);
43
44 bool init(QOpenGLContext *glContext)
45 {
46 FIND_GL_FUNCTION(glCreateMemoryObjectsEXT, PFNGLCREATEMEMORYOBJECTSEXTPROC);
47 FIND_GL_FUNCTION(glImportMemoryFdEXT, PFNGLIMPORTMEMORYFDEXTPROC);
48 FIND_GL_FUNCTION(glTextureStorageMem2DEXT, PFNGLTEXTURESTORAGEMEM2DEXTPROC);
49 FIND_GL_FUNCTION(glTexStorageMem2DEXT, PFNGLTEXSTORAGEMEM2DEXTPROC);
50 FIND_GL_FUNCTION(glDeleteMemoryObjectsEXT, PFNGLDELETEMEMORYOBJECTSEXTPROC);
51
52 return true;
53 }
54 static bool create(QOpenGLContext *glContext);
55};
56
58
60{
61 if (funcs)
62 return true;
64 if (!funcs->init(glContext)) {
65 delete funcs;
66 funcs = nullptr;
67 return false;
68 }
69 return true;
70}
71
73 int32_t fd, uint32_t width, uint32_t height, uint32_t memory_size, uint32_t format)
74 : m_integration(integration)
75 , m_server_buffer(id)
76 , m_fd(fd)
77 , m_memorySize(memory_size)
78 , m_internalFormat(format)
79{
81}
82
84{
86 return; // can't trust anything at this point
87
88 if (m_texture) { //only do gl cleanup if import has been called
89 m_integration->deleteGLTextureWhenPossible(m_texture);
90
91 if (sbiExtraDebug) qDebug() << "glDeleteMemoryObjectsEXT" << m_memoryObject;
92 funcs->glDeleteMemoryObjectsEXT(1, &m_memoryObject);
93 }
94 qt_server_buffer_release(m_server_buffer);
95 qt_server_buffer_destroy(m_server_buffer);
96}
97
98void VulkanServerBuffer::import()
99{
100 if (m_texture)
101 return;
102
103 if (sbiExtraDebug) qDebug() << "importing" << m_fd << Qt::hex << glGetError();
104
105 auto *glContext = QOpenGLContext::currentContext();
106 if (!glContext)
107 return;
108
110 return;
111
112 funcs->glCreateMemoryObjectsEXT(1, &m_memoryObject);
113 if (sbiExtraDebug) qDebug() << "glCreateMemoryObjectsEXT" << Qt::hex << glGetError();
114 funcs->glImportMemoryFdEXT(m_memoryObject, m_memorySize, GL_HANDLE_TYPE_OPAQUE_FD_EXT, m_fd);
115 if (sbiExtraDebug) qDebug() << "glImportMemoryFdEXT" << Qt::hex << glGetError();
116
117
119 m_texture->create();
120
121 if (sbiExtraDebug) qDebug() << "created texture" << m_texture->textureId() << Qt::hex << glGetError();
122
123 m_texture->bind();
124 if (sbiExtraDebug) qDebug() << "bound texture" << Qt::hex << glGetError();
125 funcs->glTexStorageMem2DEXT(GL_TEXTURE_2D, 1, m_internalFormat, m_size.width(), m_size.height(), m_memoryObject, 0 );
126 if (sbiExtraDebug) qDebug() << "glTexStorageMem2DEXT" << Qt::hex << glGetError();
127 if (sbiExtraDebug) qDebug() << "format" << Qt::hex << m_internalFormat << GL_RGBA8;
128}
129
131{
132 m_integration->deleteOrphanedTextures();
133 if (!m_texture)
134 import();
135 return m_texture;
136}
137
139{
140 m_display = display;
141 display->addRegistryListener(&wlDisplayHandleGlobal, this);
142}
143
145{
146 return static_cast<QWaylandServerBuffer *>(qt_server_buffer_get_user_data(buffer));
147}
148
149void VulkanServerBufferIntegration::wlDisplayHandleGlobal(void *data, ::wl_registry *registry, uint32_t id, const QString &interface, uint32_t version)
150{
151 Q_UNUSED(version);
152 if (interface == "zqt_vulkan_server_buffer_v1") {
153 auto *integration = static_cast<VulkanServerBufferIntegration *>(data);
154 integration->QtWayland::zqt_vulkan_server_buffer_v1::init(registry, id, 1);
155 }
156}
157
158void 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)
159{
160 if (sbiExtraDebug) qDebug() << "vulkan_server_buffer_server_buffer_created" << fd;
161 auto *server_buffer = new VulkanServerBuffer(this, id, fd, width, height, memory_size, format);
162 qt_server_buffer_set_user_data(id, server_buffer);
163}
164
166{
168 qWarning("VulkanServerBufferIntegration::deleteOrphanedTextures with no current context!");
169 return;
170 }
171 qDeleteAll(orphanedTextures);
172 orphanedTextures.clear();
173}
174
175}
176
static bool closingDown()
Returns true if the application objects are being destroyed; otherwise returns false.
void clear()
Definition qlist.h:434
\inmodule QtGui
static QOpenGLContext * currentContext()
Returns the last context which called makeCurrent in the current thread, or \nullptr,...
\inmodule QtGui
bool create()
Creates the underlying OpenGL texture object.
void bind()
Binds this texture to the currently active texture unit ready for rendering.
GLuint textureId() const
Returns the name of the underlying OpenGL texture object or 0 if it has not yet been created.
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:133
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QWaylandServerBuffer * serverBuffer(struct qt_server_buffer *buffer) override
void 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) override
VulkanServerBuffer(VulkanServerBufferIntegration *integration, struct ::qt_server_buffer *id, int32_t fd, uint32_t width, uint32_t height, uint32_t memory_size, uint32_t format)
#define FIND_GL_FUNCTION(name, type)
qDeleteAll(list.begin(), list.end())
struct wl_display * display
Definition linuxdmabuf.h:41
Combined button and popup list for selecting options.
static VulkanServerBufferGlFunctions * funcs
static constexpr bool sbiExtraDebug
QTextStream & hex(QTextStream &stream)
Calls QTextStream::setIntegerBase(16) on stream and returns stream.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char * interface
#define qDebug
[1]
Definition qlogging.h:164
#define qWarning
Definition qlogging.h:166
#define GL_HANDLE_TYPE_OPAQUE_FD_EXT
GLint GLsizei GLsizei height
GLenum GLuint id
[7]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint buffer
GLint GLsizei width
GLuint64 GLenum GLint fd
GLint GLsizei GLsizei GLenum format
#define GL_RGBA8
#define Q_UNUSED(x)
view create()
DECL_GL_FUNCTION(glTexStorageMem2DEXT, PFNGLTEXSTORAGEMEM2DEXTPROC)
DECL_GL_FUNCTION(glTextureStorageMem2DEXT, PFNGLTEXTURESTORAGEMEM2DEXTPROC)
DECL_GL_FUNCTION(glCreateMemoryObjectsEXT, PFNGLCREATEMEMORYOBJECTSEXTPROC)
DECL_GL_FUNCTION(glImportMemoryFdEXT, PFNGLIMPORTMEMORYFDEXTPROC)
DECL_GL_FUNCTION(glDeleteMemoryObjectsEXT, PFNGLDELETEMEMORYOBJECTSEXTPROC)