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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/*
* Copyright (C) 2018, 2019, 2021 Igalia S.L
* Copyright (C) 2018, 2019 Zodiac Inflight Innovations
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "WPEQtViewBackend.h"
#include "WPEQtView.h"
#include <QGuiApplication>
#include <QOpenGLFunctions>
#include <QtGlobal>
static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC imageTargetTexture2DOES;
std::unique_ptr<WPEQtViewBackend> WPEQtViewBackend::create(const QSizeF& size, QPointer<QOpenGLContext> context, EGLDisplay eglDisplay, QPointer<WPEQtView> view)
{
if (!context || !view)
return nullptr;
if (eglDisplay == EGL_NO_DISPLAY)
return nullptr;
eglInitialize(eglDisplay, nullptr, nullptr);
if (!eglBindAPI(EGL_OPENGL_ES_API) || !wpe_fdo_initialize_for_egl_display(eglDisplay))
return nullptr;
static const EGLint configAttributes[13] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 1,
EGL_GREEN_SIZE, 1,
EGL_BLUE_SIZE, 1,
EGL_ALPHA_SIZE, 1,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
EGLint count = 0;
if (!eglGetConfigs(eglDisplay, nullptr, 0, &count) || count < 1)
return nullptr;
EGLConfig eglConfig;
EGLint matched = 0;
EGLContext eglContext = nullptr;
if (eglChooseConfig(eglDisplay, configAttributes, &eglConfig, 1, &matched) && !!matched) {
static const EGLint contextAttributes[3] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
eglContext = eglCreateContext(eglDisplay, eglConfig, nullptr, contextAttributes);
}
if (!eglContext)
return nullptr;
return std::make_unique<WPEQtViewBackend>(size, eglDisplay, eglContext, context, view);
}
WPEQtViewBackend::WPEQtViewBackend(const QSizeF& size, EGLDisplay display, EGLContext eglContext, QPointer<QOpenGLContext> context, QPointer<WPEQtView> view)
: m_eglDisplay(display)
, m_eglContext(eglContext)
, m_view(view)
, m_size(size)
{
wpe_loader_init("libWPEBackend-fdo-1.0.so.1");
imageTargetTexture2DOES = reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress("glEGLImageTargetTexture2DOES"));
static const char* vertexShaderSource =
"attribute vec2 pos;\n"
"attribute vec2 texture;\n"
"varying vec2 v_texture;\n"
"void main() {\n"
" v_texture = texture;\n"
" gl_Position = vec4(pos, 0, 1);\n"
"}\n";
static const char* fragmentShaderSource =
"precision mediump float;\n"
"uniform sampler2D u_texture;\n"
"varying vec2 v_texture;\n"
"void main() {\n"
" gl_FragColor = texture2D(u_texture, v_texture);\n"
"}\n";
QOpenGLFunctions* glFunctions = context->functions();
GLuint vertexShader = glFunctions->glCreateShader(GL_VERTEX_SHADER);
glFunctions->glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glFunctions->glCompileShader(vertexShader);
GLuint fragmentShader = glFunctions->glCreateShader(GL_FRAGMENT_SHADER);
glFunctions->glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glFunctions->glCompileShader(fragmentShader);
m_program = glFunctions->glCreateProgram();
glFunctions->glAttachShader(m_program, vertexShader);
glFunctions->glAttachShader(m_program, fragmentShader);
glFunctions->glLinkProgram(m_program);
glFunctions->glBindAttribLocation(m_program, 0, "pos");
glFunctions->glBindAttribLocation(m_program, 1, "texture");
m_textureUniform = glFunctions->glGetUniformLocation(m_program, "u_texture");
static struct wpe_view_backend_exportable_fdo_egl_client exportableClient = {
// export_egl_image
nullptr,
[](void* data, struct wpe_fdo_egl_exported_image* image)
{
static_cast<WPEQtViewBackend*>(data)->displayImage(image);
},
// padding
nullptr, nullptr, nullptr
};
m_exportable = wpe_view_backend_exportable_fdo_egl_create(&exportableClient, this, m_size.width(), m_size.height());
wpe_view_backend_add_activity_state(backend(), wpe_view_activity_state_visible | wpe_view_activity_state_focused | wpe_view_activity_state_in_window);
m_surface.setFormat(context->format());
m_surface.create();
}
WPEQtViewBackend::~WPEQtViewBackend()
{
wpe_view_backend_exportable_fdo_destroy(m_exportable);
eglDestroyContext(m_eglDisplay, m_eglContext);
}
void WPEQtViewBackend::resize(const QSizeF& newSize)
{
if (!newSize.isValid())
return;
m_size = newSize;
wpe_view_backend_dispatch_set_size(backend(), m_size.width(), m_size.height());
}
GLuint WPEQtViewBackend::texture(QOpenGLContext* context)
{
if (!m_lockedImage || !hasValidSurface())
return 0;
context->makeCurrent(&m_surface);
QOpenGLFunctions* glFunctions = context->functions();
if (!m_textureId) {
glFunctions->glGenTextures(1, &m_textureId);
glFunctions->glBindTexture(GL_TEXTURE_2D, m_textureId);
glFunctions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glFunctions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFunctions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFunctions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFunctions->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_size.width(), m_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glFunctions->glBindTexture(GL_TEXTURE_2D, 0);
}
glFunctions->glClearColor(1, 0, 0, 1);
glFunctions->glClear(GL_COLOR_BUFFER_BIT);
glFunctions->glUseProgram(m_program);
glFunctions->glActiveTexture(GL_TEXTURE0);
glFunctions->glBindTexture(GL_TEXTURE_2D, m_textureId);
imageTargetTexture2DOES(GL_TEXTURE_2D, wpe_fdo_egl_exported_image_get_egl_image(m_lockedImage));
glFunctions->glUniform1i(m_textureUniform, 0);
static const GLfloat vertices[4][2] = {
{ -1.0, 1.0 },
{ 1.0, 1.0 },
{ -1.0, -1.0 },
{ 1.0, -1.0 },
};
static const GLfloat texturePos[4][2] = {
{ 0, 0 },
{ 1, 0 },
{ 0, 1 },
{ 1, 1 },
};
glFunctions->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glFunctions->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texturePos);
glFunctions->glEnableVertexAttribArray(0);
glFunctions->glEnableVertexAttribArray(1);
glFunctions->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glFunctions->glDisableVertexAttribArray(0);
glFunctions->glDisableVertexAttribArray(1);
wpe_view_backend_exportable_fdo_dispatch_frame_complete(m_exportable);
wpe_view_backend_exportable_fdo_egl_dispatch_release_exported_image(m_exportable, m_lockedImage);
m_lockedImage = nullptr;
return m_textureId;
}
void WPEQtViewBackend::displayImage(struct wpe_fdo_egl_exported_image* image)
{
Q_ASSUME(!m_lockedImage);
m_lockedImage = image;
if (m_view)
m_view->triggerUpdate();
}
uint32_t WPEQtViewBackend::modifiers() const
{
uint32_t mask = m_keyboardModifiers;
if (m_mouseModifiers)
mask |= m_mouseModifiers;
return mask;
}
void WPEQtViewBackend::dispatchHoverEnterEvent(QHoverEvent*)
{
m_hovering = true;
m_mouseModifiers = 0;
}
void WPEQtViewBackend::dispatchHoverLeaveEvent(QHoverEvent*)
{
m_hovering = false;
}
void WPEQtViewBackend::dispatchHoverMoveEvent(QHoverEvent* event)
{
if (!m_hovering)
return;
uint32_t state = !!m_mousePressedButton;
struct wpe_input_pointer_event wpeEvent = { wpe_input_pointer_event_type_motion,
static_cast<uint32_t>(event->timestamp()),
event->pos().x(), event->pos().y(),
m_mousePressedButton, state, modifiers() };
wpe_view_backend_dispatch_pointer_event(backend(), &wpeEvent);
}
void WPEQtViewBackend::dispatchMousePressEvent(QMouseEvent* event)
{
uint32_t button = 0;
uint32_t modifier = 0;
switch (event->button()) {
case Qt::LeftButton:
button = 1;
modifier = wpe_input_pointer_modifier_button1;
break;
case Qt::RightButton:
button = 2;
modifier = wpe_input_pointer_modifier_button2;
break;
default:
break;
}
m_mousePressedButton = button;
uint32_t state = 1;
m_mouseModifiers |= modifier;
struct wpe_input_pointer_event wpeEvent = { wpe_input_pointer_event_type_button,
static_cast<uint32_t>(event->timestamp()),
event->x(), event->y(), button, state, modifiers() };
wpe_view_backend_dispatch_pointer_event(backend(), &wpeEvent);
}
void WPEQtViewBackend::dispatchMouseReleaseEvent(QMouseEvent* event)
{
uint32_t button = 0;
uint32_t modifier = 0;
switch (event->button()) {
case Qt::LeftButton:
button = 1;
modifier = wpe_input_pointer_modifier_button1;
break;
case Qt::RightButton:
button = 2;
modifier = wpe_input_pointer_modifier_button2;
break;
default:
break;
}
m_mousePressedButton = 0;
uint32_t state = 0;
m_mouseModifiers &= ~modifier;
struct wpe_input_pointer_event wpeEvent = { wpe_input_pointer_event_type_button,
static_cast<uint32_t>(event->timestamp()),
event->x(), event->y(), button, state, modifiers() };
wpe_view_backend_dispatch_pointer_event(backend(), &wpeEvent);
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
#define QWHEEL_POSITION position()
#else
#define QWHEEL_POSITION posF()
#endif
void WPEQtViewBackend::dispatchWheelEvent(QWheelEvent* event)
{
QPoint delta = event->angleDelta();
QPoint numDegrees = delta / 8;
struct wpe_input_axis_2d_event wpeEvent;
if (delta.y() == event->QWHEEL_POSITION.y())
wpeEvent.x_axis = numDegrees.x();
else
wpeEvent.y_axis = numDegrees.y();
wpeEvent.base.type = static_cast<wpe_input_axis_event_type>(wpe_input_axis_event_type_mask_2d | wpe_input_axis_event_type_motion_smooth);
wpeEvent.base.x = event->QWHEEL_POSITION.x();
wpeEvent.base.y = event->QWHEEL_POSITION.y();
wpe_view_backend_dispatch_axis_event(backend(), &wpeEvent.base);
}
void WPEQtViewBackend::dispatchKeyEvent(QKeyEvent* event, bool state)
{
uint32_t keysym = event->nativeVirtualKey();
if (!keysym)
keysym = wpe_input_xkb_context_get_key_code(wpe_input_xkb_context_get_default(), event->key(), state);
uint32_t modifiers = 0;
Qt::KeyboardModifiers qtModifiers = event->modifiers();
if (!qtModifiers)
qtModifiers = QGuiApplication::keyboardModifiers();
if (qtModifiers & Qt::ShiftModifier)
modifiers |= wpe_input_keyboard_modifier_shift;
if (qtModifiers & Qt::ControlModifier)
modifiers |= wpe_input_keyboard_modifier_control;
if (qtModifiers & Qt::MetaModifier)
modifiers |= wpe_input_keyboard_modifier_meta;
if (qtModifiers & Qt::AltModifier)
modifiers |= wpe_input_keyboard_modifier_alt;
struct wpe_input_keyboard_event wpeEvent = { static_cast<uint32_t>(event->timestamp()),
keysym, event->nativeScanCode(), state, modifiers };
wpe_view_backend_dispatch_keyboard_event(backend(), &wpeEvent);
}
void WPEQtViewBackend::dispatchTouchEvent(QTouchEvent* event)
{
wpe_input_touch_event_type eventType;
switch (event->type()) {
case QEvent::TouchBegin:
eventType = wpe_input_touch_event_type_down;
break;
case QEvent::TouchUpdate:
eventType = wpe_input_touch_event_type_motion;
break;
case QEvent::TouchEnd:
eventType = wpe_input_touch_event_type_up;
break;
default:
eventType = wpe_input_touch_event_type_null;
break;
}
int i = 0;
struct wpe_input_touch_event_raw* rawEvents = g_new0(wpe_input_touch_event_raw, event->touchPoints().length());
for (auto& point : event->touchPoints()) {
rawEvents[i] = { eventType, static_cast<uint32_t>(event->timestamp()),
point.id(), static_cast<int32_t>(point.pos().x()), static_cast<int32_t>(point.pos().y()) };
i++;
}
struct wpe_input_touch_event wpeEvent = { rawEvents, static_cast<uint64_t>(i), eventType,
static_cast<int32_t>(rawEvents[0].id),
static_cast<uint32_t>(event->timestamp()), modifiers() };
wpe_view_backend_dispatch_touch_event(backend(), &wpeEvent);
g_free(rawEvents);
}
|