10#include <qpa/qwindowsysteminterface.h>
11#include <QtGui/QOpenGLContext>
12#include <QtGui/QOpenGLFunctions>
13#include <QtCore/QFile>
14#include <QtCore/QJsonDocument>
15#include <QtCore/QJsonArray>
16#include <QtCore/QJsonObject>
18#include <QtGui/private/qguiapplication_p.h>
19#include <QtOpenGL/private/qopenglvertexarrayobject_p.h>
21#ifndef GL_VERTEX_ARRAY_BINDING
22#define GL_VERTEX_ARRAY_BINDING 0x85B5
27using namespace Qt::StringLiterals;
29QEglFSCursor::QEglFSCursor(QPlatformScreen *screen)
31 m_screen(
static_cast<QEglFSScreen *>(screen)),
32 m_activeScreen(
nullptr),
33 m_deviceListener(
nullptr),
34 m_updateRequested(
false)
36 QByteArray hideCursorVal = qgetenv(
"QT_QPA_EGLFS_HIDECURSOR");
37 if (!hideCursorVal.isEmpty())
38 m_visible = hideCursorVal.toInt() == 0;
42 int rotation = qEnvironmentVariableIntValue(
"QT_QPA_EGLFS_ROTATION");
44 m_rotationMatrix.rotate(rotation, 0, 0, 1);
52 QCursor cursor(Qt::ArrowCursor);
53 setCurrentCursor(&cursor);
56 m_deviceListener =
new QEglFSCursorDeviceListener(
this);
57 connect(QGuiApplicationPrivate::inputDeviceManager(), &QInputDeviceManager::deviceListChanged,
58 m_deviceListener, &QEglFSCursorDeviceListener::onDeviceListChanged);
62QEglFSCursor::~QEglFSCursor()
65 delete m_deviceListener;
68void QEglFSCursor::updateMouseStatus()
70 m_visible = m_deviceListener->hasMouse();
75 return QGuiApplicationPrivate::inputDeviceManager()->deviceCount(QInputDeviceManager::DeviceTypePointer) > 0;
80 if (type == QInputDeviceManager::DeviceTypePointer)
81 m_cursor->updateMouseStatus();
84void QEglFSCursor::resetResources()
86 m_cursor.customCursorPending = !m_cursor.customCursorImage.isNull();
89void QEglFSCursor::createShaderPrograms()
91 static const char *textureVertexProgram =
92 "attribute highp vec2 vertexCoordEntry;\n"
93 "attribute highp vec2 textureCoordEntry;\n"
94 "varying highp vec2 textureCoord;\n"
95 "uniform highp mat4 mat;\n"
97 " textureCoord = textureCoordEntry;\n"
98 " gl_Position = mat * vec4(vertexCoordEntry, 1.0, 1.0);\n"
101 static const char *textureFragmentProgram =
102 "uniform sampler2D texture;\n"
103 "varying highp vec2 textureCoord;\n"
105 " gl_FragColor = texture2D(texture, textureCoord).bgra;\n"
108 QEglFSCursorData &gfx =
static_cast<QEglFSContext*>(QOpenGLContext::currentContext()->handle())->cursorData;
109 gfx.program.reset(
new QOpenGLShaderProgram);
110 gfx.program->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, textureVertexProgram);
111 gfx.program->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, textureFragmentProgram);
112 gfx.program->bindAttributeLocation(
"vertexCoordEntry", 0);
113 gfx.program->bindAttributeLocation(
"textureCoordEntry", 1);
116 gfx.textureEntry = gfx.program->uniformLocation(
"texture");
117 gfx.matEntry = gfx.program->uniformLocation(
"mat");
120void QEglFSCursor::createCursorTexture(uint *texture,
const QImage &image)
122 Q_ASSERT(QOpenGLContext::currentContext());
123 QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
125 f->glGenTextures(1, texture);
126 f->glBindTexture(GL_TEXTURE_2D, *texture);
127 f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
128 f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
129 f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
130 f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
132 f->glTexImage2D(GL_TEXTURE_2D, 0 , GL_RGBA, image.width(), image.height(), 0 ,
133 GL_RGBA, GL_UNSIGNED_BYTE, image.constBits());
136void QEglFSCursor::initCursorAtlas()
138 static QByteArray json = qgetenv(
"QT_QPA_EGLFS_CURSOR");
140 json =
":/cursor.json";
142 QFile file(QString::fromUtf8(json));
143 if (!file.open(QFile::ReadOnly)) {
148 QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
149 QJsonObject object = doc.object();
151 QString atlas = object.value(
"image"_L1).toString();
152 Q_ASSERT(!atlas.isEmpty());
154 const int cursorsPerRow = object.value(
"cursorsPerRow"_L1).toDouble();
155 Q_ASSERT(cursorsPerRow);
156 m_cursorAtlas.cursorsPerRow = cursorsPerRow;
158 const QJsonArray hotSpots = object.value(
"hotSpots"_L1).toArray();
159 Q_ASSERT(hotSpots.count() == Qt::LastCursor + 1);
160 for (
int i = 0; i < hotSpots.count(); i++) {
161 QPoint hotSpot(hotSpots[i].toArray()[0].toDouble(), hotSpots[i].toArray()[1].toDouble());
162 m_cursorAtlas.hotSpots << hotSpot;
165 QImage image = QImage(atlas).convertToFormat(QImage::Format_ARGB32_Premultiplied);
166 m_cursorAtlas.cursorWidth = image.width() / m_cursorAtlas.cursorsPerRow;
167 m_cursorAtlas.cursorHeight = image.height() / ((Qt::LastCursor + cursorsPerRow) / cursorsPerRow);
168 m_cursorAtlas.width = image.width();
169 m_cursorAtlas.height = image.height();
170 m_cursorAtlas.image = image;
174void QEglFSCursor::changeCursor(QCursor *cursor, QWindow *window)
177 const QRect oldCursorRect = cursorRect();
178 if (setCurrentCursor(cursor))
179 update(oldCursorRect | cursorRect(),
false);
182bool QEglFSCursor::setCurrentCursor(QCursor *cursor)
187 const Qt::CursorShape newShape = cursor ? cursor->shape() : Qt::ArrowCursor;
188 if (m_cursor.shape == newShape && newShape != Qt::BitmapCursor)
191 if (m_cursor.shape == Qt::BitmapCursor) {
192 m_cursor.customCursorImage = QImage();
193 m_cursor.customCursorPending =
false;
195 m_cursor.shape = newShape;
196 if (newShape != Qt::BitmapCursor) {
197 const float ws = (
float)m_cursorAtlas.cursorWidth / m_cursorAtlas.width,
198 hs = (
float)m_cursorAtlas.cursorHeight / m_cursorAtlas.height;
199 m_cursor.textureRect = QRectF(ws * (m_cursor.shape % m_cursorAtlas.cursorsPerRow),
200 hs * (m_cursor.shape / m_cursorAtlas.cursorsPerRow),
202 m_cursor.hotSpot = m_cursorAtlas.hotSpots[m_cursor.shape];
203 m_cursor.useCustomCursor =
false;
204 m_cursor.size = QSize(m_cursorAtlas.cursorWidth, m_cursorAtlas.cursorHeight);
206 QImage image = cursor->pixmap().toImage();
207 m_cursor.textureRect = QRectF(0, 0, 1, 1);
208 m_cursor.hotSpot = cursor->hotSpot();
209 m_cursor.useCustomCursor =
false;
210 m_cursor.size = image.size();
211 m_cursor.customCursorImage = image;
212 m_cursor.customCursorPending =
true;
213 m_cursor.customCursorKey = m_cursor.customCursorImage.cacheKey();
227 m_allScreens(allScreens)
239bool QEglFSCursor::event(QEvent *e)
241 if (e->type() == QEvent::User + 1) {
242 CursorUpdateEvent *ev =
static_cast<CursorUpdateEvent *>(e);
243 m_updateRequested =
false;
244 if (!ev->allScreens()) {
245 QWindow *w = m_screen->topLevelAt(ev->pos());
247 QWindowSystemInterface::handleExposeEvent(w, ev->rect());
248 QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents);
251 const auto windows = qGuiApp->topLevelWindows();
252 for (QWindow *w : windows)
253 QWindowSystemInterface::handleExposeEvent(w, w->geometry());
254 QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents);
258 return QPlatformCursor::event(e);
261void QEglFSCursor::update(
const QRect &rect,
bool allScreens)
263 if (!m_updateRequested) {
267 m_updateRequested =
true;
268 QCoreApplication::postEvent(
this,
new CursorUpdateEvent(m_cursor.pos, rect, allScreens));
272QRect QEglFSCursor::cursorRect()
const
274 return QRect(m_cursor.pos - m_cursor.hotSpot, m_cursor.size);
277QPoint QEglFSCursor::pos()
const
282void QEglFSCursor::setPos(
const QPoint &pos)
284 QGuiApplicationPrivate::inputDeviceManager()->setCursorPos(pos);
285 const QRect oldCursorRect = cursorRect();
287 update(oldCursorRect | cursorRect(),
false);
288 const auto siblings = m_screen->virtualSiblings();
289 for (QPlatformScreen *screen : siblings)
290 static_cast<QEglFSScreen *>(screen)->handleCursorMove(m_cursor.pos);
293void QEglFSCursor::pointerEvent(
const QMouseEvent &event)
295 if (event.type() != QEvent::MouseMove)
297 const QRect oldCursorRect = cursorRect();
298 m_cursor.pos = event.globalPosition().toPoint();
299 update(oldCursorRect | cursorRect(),
false);
300 const auto siblings = m_screen->virtualSiblings();
301 for (QPlatformScreen *screen : siblings)
302 static_cast<QEglFSScreen *>(screen)->handleCursorMove(m_cursor.pos);
305void QEglFSCursor::paintOnScreen()
312 QRectF cr = cursorRect();
318 const auto siblings = m_screen->virtualSiblings();
319 for (QPlatformScreen *screen : siblings) {
320 if (screen->geometry().contains(cr.topLeft().toPoint() + m_cursor.hotSpot))
322 cr.translate(-screen->geometry().topLeft());
323 const QSize screenSize = screen->geometry().size();
324 const GLfloat x1 = 2 * (cr.left() / GLfloat(screenSize.width())) - 1;
325 const GLfloat x2 = 2 * (cr.right() / GLfloat(screenSize.width())) - 1;
326 const GLfloat y1 = 1 - (cr.top() / GLfloat(screenSize.height())) * 2;
327 const GLfloat y2 = 1 - (cr.bottom() / GLfloat(screenSize.height())) * 2;
328 QRectF r(QPointF(x1, y1), QPointF(x2, y2));
332 if (screen != m_activeScreen) {
333 m_activeScreen = screen;
335 update(cursorRect(),
true);
351 vaoHelper = QOpenGLVertexArrayObjectHelper::vertexArrayObjectHelperForContext(QOpenGLContext::currentContext());
353 static bool windowsChecked =
false;
354 static bool shouldSave =
true;
355 if (!windowsChecked) {
356 windowsChecked =
true;
357 QWindowList windows = QGuiApplication::allWindows();
358 if (!windows.isEmpty() && windows[0]->inherits(
"QQuickWindow"))
365 f->glGetIntegerv(GL_CURRENT_PROGRAM, &program);
366 f->glGetIntegerv(GL_TEXTURE_BINDING_2D, &texture);
367 f->glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTexture);
368 f->glGetIntegerv(GL_FRONT_FACE, &frontFace);
369 cull = f->glIsEnabled(GL_CULL_FACE);
370 depthTest = f->glIsEnabled(GL_DEPTH_TEST);
371 blend = f->glIsEnabled(GL_BLEND);
372 f->glGetIntegerv(GL_BLEND_SRC_RGB, blendFunc);
373 f->glGetIntegerv(GL_BLEND_SRC_ALPHA, blendFunc + 1);
374 f->glGetIntegerv(GL_BLEND_DST_RGB, blendFunc + 2);
375 f->glGetIntegerv(GL_BLEND_DST_ALPHA, blendFunc + 3);
376 scissor = f->glIsEnabled(GL_SCISSOR_TEST);
377 stencil = f->glIsEnabled(GL_STENCIL_TEST);
378 f->glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &arrayBuf);
379 if (vaoHelper->isValid())
383 for (
int i = 0; i < 2; ++i) {
384 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &va[i].enabled);
385 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_SIZE, &va[i].size);
386 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_TYPE, &va[i].type);
387 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &va[i].normalized);
388 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &va[i].stride);
389 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &va[i].buffer);
390 f->glGetVertexAttribPointerv(i, GL_VERTEX_ATTRIB_ARRAY_POINTER, &va[i].pointer);
395 f->glUseProgram(program);
396 f->glBindTexture(GL_TEXTURE_2D, texture);
397 f->glActiveTexture(activeTexture);
398 f->glFrontFace(frontFace);
400 f->glEnable(GL_CULL_FACE);
402 f->glEnable(GL_DEPTH_TEST);
404 f->glDisable(GL_BLEND);
405 f->glBlendFuncSeparate(blendFunc[0], blendFunc[1], blendFunc[2], blendFunc[3]);
407 f->glEnable(GL_SCISSOR_TEST);
409 f->glEnable(GL_STENCIL_TEST);
410 f->glBindBuffer(GL_ARRAY_BUFFER, arrayBuf);
411 if (vaoHelper->isValid())
412 vaoHelper->glBindVertexArray(vao);
413 for (
int i = 0; i < 2; ++i) {
415 f->glEnableVertexAttribArray(i);
417 f->glDisableVertexAttribArray(i);
418 f->glBindBuffer(GL_ARRAY_BUFFER, va[i].buffer);
419 f->glVertexAttribPointer(i, va[i].size, va[i].type, va[i].normalized, va[i].stride, va[i].pointer);
441void QEglFSCursor::draw(
const QRectF &r)
443 Q_ASSERT(QOpenGLContext::currentContext());
444 QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
445 StateSaver stateSaver(f);
447 QEglFSCursorData &gfx =
static_cast<QEglFSContext*>(QOpenGLContext::currentContext()->handle())->cursorData;
449 createShaderPrograms();
451 if (!gfx.atlasTexture) {
452 createCursorTexture(&gfx.atlasTexture, m_cursorAtlas.image);
454 if (m_cursor.shape != Qt::BitmapCursor)
455 m_cursor.useCustomCursor =
false;
459 if (m_cursor.shape == Qt::BitmapCursor && (m_cursor.customCursorPending || m_cursor.customCursorKey != gfx.customCursorKey)) {
461 createCursorTexture(&gfx.customCursorTexture, m_cursor.customCursorImage);
462 m_cursor.useCustomCursor =
true;
463 m_cursor.customCursorPending =
false;
464 gfx.customCursorKey = m_cursor.customCursorKey;
467 GLuint cursorTexture = !m_cursor.useCustomCursor ? gfx.atlasTexture : gfx.customCursorTexture;
468 Q_ASSERT(cursorTexture);
472 const GLfloat x1 = r.left();
473 const GLfloat x2 = r.right();
474 const GLfloat y1 = r.top();
475 const GLfloat y2 = r.bottom();
476 const GLfloat cursorCoordinates[] = {
483 const GLfloat s1 = m_cursor.textureRect.left();
484 const GLfloat s2 = m_cursor.textureRect.right();
485 const GLfloat t1 = m_cursor.textureRect.top();
486 const GLfloat t2 = m_cursor.textureRect.bottom();
487 const GLfloat textureCoordinates[] = {
494 f->glActiveTexture(GL_TEXTURE0);
495 f->glBindTexture(GL_TEXTURE_2D, cursorTexture);
497 if (stateSaver.vaoHelper->isValid())
498 stateSaver.vaoHelper->glBindVertexArray(0);
500 f->glBindBuffer(GL_ARRAY_BUFFER, 0);
502 gfx.program->enableAttributeArray(0);
503 gfx.program->enableAttributeArray(1);
504 gfx.program->setAttributeArray(0, cursorCoordinates, 2);
505 gfx.program->setAttributeArray(1, textureCoordinates, 2);
507 gfx.program->setUniformValue(gfx.textureEntry, 0);
508 gfx.program->setUniformValue(gfx.matEntry, m_rotationMatrix);
510 f->glDisable(GL_CULL_FACE);
511 f->glFrontFace(GL_CCW);
512 f->glEnable(GL_BLEND);
513 f->glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
514 f->glDisable(GL_DEPTH_TEST);
515 f->glDisable(GL_SCISSOR_TEST);
516 f->glDisable(GL_STENCIL_TEST);
518 f->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
520 gfx.program->disableAttributeArray(0);
521 gfx.program->disableAttributeArray(1);
522 gfx.program->release();
527#include "moc_qeglfscursor_p.cpp"
CursorUpdateEvent(const QPoint &pos, const QRect &rect, bool allScreens)
Combined button and popup list for selecting options.
#define GL_VERTEX_ARRAY_BINDING
StateSaver(QOpenGLFunctions *func)
QOpenGLVertexArrayObjectHelper * vaoHelper