Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
libhybriseglserverbufferintegration.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 Jolla Ltd, author: <giulio.camuffo@jollamobile.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:critical reason:network-protocol
4
6
7#include <QtGui/QOpenGLContext>
8#include <QtGui/QOpenGLTexture>
9#include <hybris/eglplatformcommon/hybris_nativebufferext.h>
10#include <wayland-server-core.h>
11
12QT_BEGIN_NAMESPACE
13LibHybrisEglServerBuffer::LibHybrisEglServerBuffer(LibHybrisEglServerBufferIntegration *integration, const QImage &qimage, QtWayland::ServerBuffer::Format format)
14 : QtWayland::ServerBuffer(qimage.size(),format)
15 , m_integration(integration)
16{
17 m_format = format;
18
19 EGLint egl_format;
20 switch (m_format) {
21 case RGBA32:
22 m_hybris_format = QtWaylandServer::qt_libhybris_egl_server_buffer::format_RGBA32;
23 egl_format = HYBRIS_PIXEL_FORMAT_RGBA_8888;
24 break;
25 default:
26 qWarning("LibHybrisEglServerBuffer: unsupported format");
27 m_hybris_format = QtWaylandServer::qt_libhybris_egl_server_buffer::format_RGBA32;
28 egl_format = HYBRIS_PIXEL_FORMAT_RGBA_8888;
29 break;
30 }
31
32 if (!m_integration->eglHybrisCreateNativeBuffer(m_size.width(), m_size.height(), HYBRIS_USAGE_HW_TEXTURE, egl_format, &m_stride, &m_buffer)) {
33 qWarning("LibHybrisEglServerBuffer: Failed to create egl buffer");
34 return;
35 }
36
37 int numInts;
38 int numFds;
39 m_integration->eglHybrisGetNativeBufferInfo(m_buffer, &numInts, &numFds);
40 m_ints.resize(numInts);
41 m_fds.resize(numFds);
42 m_integration->eglHybrisSerializeNativeBuffer(m_buffer, m_ints.data(), m_fds.data());
43
44 m_image = m_integration->eglCreateImageKHR(EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, m_buffer, 0);
45
46 if (!QOpenGLContext::currentContext()) {
47 qWarning("LibHybrisEglServerBuffer: No current context when creating buffer. Texture loading will fail");
48 return;
49 }
50
51 m_texture = new QOpenGLTexture(QOpenGLTexture::Target2D);
52 m_texture->create();
53
54 m_texture->bind();
55
56 m_integration->glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, m_image);
57
58 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, qimage.width(), qimage.height(), GL_RGBA, GL_UNSIGNED_BYTE, qimage.constBits());
59
60 m_texture->release();
61 m_texture->setSize(m_size.width(), m_size.height());
62}
63
64struct ::wl_resource *LibHybrisEglServerBuffer::resourceForClient(struct ::wl_client *client)
65{
66 auto *bufferResource = resourceMap().value(client);
67 if (!bufferResource) {
68 auto integrationResource = m_integration->resourceMap().value(client);
69 if (!integrationResource) {
70 qWarning("LibHybrisEglServerBuffer::resourceForClient: Trying to get resource for ServerBuffer. But client is not bound to the libhybris_egl interface");
71 return 0;
72 }
73 struct ::wl_resource *egl_integration_resource = integrationResource->handle;
74 Resource *resource = add(client, 1);
75 wl_resource *bufRes = wl_resource_create(client, &qt_libhybris_buffer_interface,-1, 0);
76
77 m_integration->send_server_buffer_created(egl_integration_resource, resource->handle, bufRes, m_fds.size(), QByteArray((char *)m_ints.data(), m_ints.size() * sizeof(int32_t)),
78 m_name, m_size.width(), m_size.height(), m_stride, m_format);
79
80 for (int i = 0; i < m_fds.size(); ++i) {
81 send_add_fd(resource->handle, m_fds.at(i));
82 }
83
84 return resource->handle;
85 }
86 return bufferResource->handle;
87}
88
93
97
101
102bool LibHybrisEglServerBufferIntegration::initializeHardware(QWaylandCompositor *compositor)
103{
104 Q_ASSERT(QGuiApplication::platformNativeInterface());
105
106 m_egl_display = static_cast<EGLDisplay>(QGuiApplication::platformNativeInterface()->nativeResourceForIntegration("egldisplay"));
107 if (!m_egl_display) {
108 qWarning("Can't initialize libhybris egl server buffer integration. Missing egl display from platform plugin");
109 return false;
110 }
111
112 m_egl_create_buffer = reinterpret_cast<PFNEGLHYBRISCREATENATIVEBUFFERPROC>(eglGetProcAddress("eglHybrisCreateNativeBuffer"));
113 if (!m_egl_create_buffer) {
114 qWarning("Failed to initialize libhybris egl server buffer integration. Could not find eglHybrisCreateNativeBuffer.\n");
115 return false;
116 }
117 m_egl_get_buffer_info = reinterpret_cast<PFNEGLHYBRISGETNATIVEBUFFERINFOPROC>(eglGetProcAddress("eglHybrisGetNativeBufferInfo"));
118 if (!m_egl_get_buffer_info) {
119 qWarning("Failed to initialize libhybris egl server buffer integration. Could not find eglHybrisGetNativeBufferInfo.\n");
120 return false;
121 }
122 m_egl_serialize_buffer = reinterpret_cast<PFNEGLHYBRISSERIALIZENATIVEBUFFERPROC>(eglGetProcAddress("eglHybrisSerializeNativeBuffer"));
123 if (!m_egl_serialize_buffer) {
124 qWarning("Failed to initialize libhybris egl server buffer integration. Could not find eglHybrisSerializeNativeBuffer.\n");
125 return false;
126 }
127
128 const char *extensionString = eglQueryString(m_egl_display, EGL_EXTENSIONS);
129 if (!extensionString || !strstr(extensionString, "EGL_KHR_image")) {
130 qWarning("Failed to initialize libhybris egl server buffer integration. There is no EGL_KHR_image extension.\n");
131 return false;
132 }
133 m_egl_create_image = reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(eglGetProcAddress("eglCreateImageKHR"));
134 m_egl_destroy_image = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress("eglDestroyImageKHR"));
135 if (!m_egl_create_image || !m_egl_destroy_image) {
136 qWarning("Failed to initialize libhybris egl server buffer integration. Could not resolve eglCreateImageKHR or eglDestroyImageKHR");
137 return false;
138 }
139
140 m_gl_egl_image_target_texture_2d = reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress("glEGLImageTargetTexture2DOES"));
141 if (!m_gl_egl_image_target_texture_2d) {
142 qWarning("Failed to initialize libhybris egl server buffer integration. Could not find glEGLImageTargetTexture2DOES.\n");
143 return false;
144 }
145
146 QtWaylandServer::qt_libhybris_egl_server_buffer::init(compositor->display(), 1);
147 return true;
148}
149
150bool LibHybrisEglServerBufferIntegration::supportsFormat(QtWayland::ServerBuffer::Format format) const
151{
152 switch (format) {
153 case QtWayland::ServerBuffer::RGBA32:
154 return true;
155 case QtWayland::ServerBuffer::A8:
156 return false;
157 default:
158 return false;
159 }
160}
161
162QtWayland::ServerBuffer *LibHybrisEglServerBufferIntegration::createServerBufferFromImage(const QImage &qimage, QtWayland::ServerBuffer::Format format)
163{
164 return new LibHybrisEglServerBuffer(this, qimage, format);
165}
166
167QT_END_NAMESPACE
bool supportsFormat(QtWayland::ServerBuffer::Format format) const override
QtWayland::ServerBuffer * createServerBufferFromImage(const QImage &qimage, QtWayland::ServerBuffer::Format format) override