9#include <qpa/qwindowsysteminterface.h>
10#include <QtGui/QOpenGLContext>
11#include <QtGui/QOpenGLFunctions>
12#include <QtCore/QFile>
13#include <QtCore/QJsonDocument>
14#include <QtCore/QJsonArray>
15#include <QtCore/QJsonObject>
17#include <QtGui/private/qguiapplication_p.h>
18#include <QtOpenGL/private/qopenglvertexarrayobject_p.h>
20#ifndef GL_VERTEX_ARRAY_BINDING
21#define GL_VERTEX_ARRAY_BINDING 0x85B5
26using namespace Qt::StringLiterals;
28QEglFSCursor::QEglFSCursor(QPlatformScreen *screen)
30 m_screen(
static_cast<QEglFSScreen *>(screen)),
31 m_activeScreen(
nullptr),
32 m_deviceListener(
nullptr),
33 m_updateRequested(
false)
35 QByteArray hideCursorVal = qgetenv(
"QT_QPA_EGLFS_HIDECURSOR");
36 if (!hideCursorVal.isEmpty())
37 m_visible = hideCursorVal.toInt() == 0;
41 int rotation = qEnvironmentVariableIntValue(
"QT_QPA_EGLFS_ROTATION");
43 m_rotationMatrix.rotate(rotation, 0, 0, 1);
51 QCursor cursor(Qt::ArrowCursor);
52 setCurrentCursor(&cursor);
55 m_deviceListener =
new QEglFSCursorDeviceListener(
this);
56 connect(QGuiApplicationPrivate::inputDeviceManager(), &QInputDeviceManager::deviceListChanged,
57 m_deviceListener, &QEglFSCursorDeviceListener::onDeviceListChanged);
61QEglFSCursor::~QEglFSCursor()
64 delete m_deviceListener;
67void QEglFSCursor::updateMouseStatus()
69 m_visible = m_deviceListener->hasMouse();
74 return QGuiApplicationPrivate::inputDeviceManager()->deviceCount(QInputDeviceManager::DeviceTypePointer) > 0;
79 if (type == QInputDeviceManager::DeviceTypePointer)
80 m_cursor->updateMouseStatus();
83void QEglFSCursor::resetResources()
85 m_cursor.customCursorPending = !m_cursor.customCursorImage.isNull();
88void QEglFSCursor::createShaderPrograms()
90 static const char *textureVertexProgram =
91 "attribute highp vec2 vertexCoordEntry;\n"
92 "attribute highp vec2 textureCoordEntry;\n"
93 "varying highp vec2 textureCoord;\n"
94 "uniform highp mat4 mat;\n"
96 " textureCoord = textureCoordEntry;\n"
97 " gl_Position = mat * vec4(vertexCoordEntry, 1.0, 1.0);\n"
100 static const char *textureFragmentProgram =
101 "uniform sampler2D texture;\n"
102 "varying highp vec2 textureCoord;\n"
104 " gl_FragColor = texture2D(texture, textureCoord).bgra;\n"
107 QEglFSCursorData &gfx =
static_cast<QEglFSContext*>(QOpenGLContext::currentContext()->handle())->cursorData;
108 gfx.program.reset(
new QOpenGLShaderProgram);
109 gfx.program->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, textureVertexProgram);
110 gfx.program->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, textureFragmentProgram);
111 gfx.program->bindAttributeLocation(
"vertexCoordEntry", 0);
112 gfx.program->bindAttributeLocation(
"textureCoordEntry", 1);
115 gfx.textureEntry = gfx.program->uniformLocation(
"texture");
116 gfx.matEntry = gfx.program->uniformLocation(
"mat");
119void QEglFSCursor::createCursorTexture(uint *texture,
const QImage &image)
121 Q_ASSERT(QOpenGLContext::currentContext());
122 QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
124 f->glGenTextures(1, texture);
125 f->glBindTexture(GL_TEXTURE_2D, *texture);
126 f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
127 f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
128 f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
129 f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
131 f->glTexImage2D(GL_TEXTURE_2D, 0 , GL_RGBA, image.width(), image.height(), 0 ,
132 GL_RGBA, GL_UNSIGNED_BYTE, image.constBits());
135void QEglFSCursor::initCursorAtlas()
137 static QByteArray json = qgetenv(
"QT_QPA_EGLFS_CURSOR");
139 json =
":/cursor.json";
141 QFile file(QString::fromUtf8(json));
142 if (!file.open(QFile::ReadOnly)) {
147 QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
148 QJsonObject object = doc.object();
150 QString atlas = object.value(
"image"_L1).toString();
151 Q_ASSERT(!atlas.isEmpty());
153 const int cursorsPerRow = object.value(
"cursorsPerRow"_L1).toDouble();
154 Q_ASSERT(cursorsPerRow);
155 m_cursorAtlas.cursorsPerRow = cursorsPerRow;
157 const QJsonArray hotSpots = object.value(
"hotSpots"_L1).toArray();
158 Q_ASSERT(hotSpots.count() == Qt::LastCursor + 1);
159 for (
int i = 0; i < hotSpots.count(); i++) {
160 QPoint hotSpot(hotSpots[i].toArray()[0].toDouble(), hotSpots[i].toArray()[1].toDouble());
161 m_cursorAtlas.hotSpots << hotSpot;
164 QImage image = QImage(atlas).convertToFormat(QImage::Format_ARGB32_Premultiplied);
165 m_cursorAtlas.cursorWidth = image.width() / m_cursorAtlas.cursorsPerRow;
166 m_cursorAtlas.cursorHeight = image.height() / ((Qt::LastCursor + cursorsPerRow) / cursorsPerRow);
167 m_cursorAtlas.width = image.width();
168 m_cursorAtlas.height = image.height();
169 m_cursorAtlas.image = image;
173void QEglFSCursor::changeCursor(QCursor *cursor, QWindow *window)
176 const QRect oldCursorRect = cursorRect();
177 if (setCurrentCursor(cursor))
178 update(oldCursorRect | cursorRect(),
false);
181bool QEglFSCursor::setCurrentCursor(QCursor *cursor)
186 const Qt::CursorShape newShape = cursor ? cursor->shape() : Qt::ArrowCursor;
187 if (m_cursor.shape == newShape && newShape != Qt::BitmapCursor)
190 if (m_cursor.shape == Qt::BitmapCursor) {
191 m_cursor.customCursorImage = QImage();
192 m_cursor.customCursorPending =
false;
194 m_cursor.shape = newShape;
195 if (newShape != Qt::BitmapCursor) {
196 const float ws = (
float)m_cursorAtlas.cursorWidth / m_cursorAtlas.width,
197 hs = (
float)m_cursorAtlas.cursorHeight / m_cursorAtlas.height;
198 m_cursor.textureRect = QRectF(ws * (m_cursor.shape % m_cursorAtlas.cursorsPerRow),
199 hs * (m_cursor.shape / m_cursorAtlas.cursorsPerRow),
201 m_cursor.hotSpot = m_cursorAtlas.hotSpots[m_cursor.shape];
202 m_cursor.useCustomCursor =
false;
203 m_cursor.size = QSize(m_cursorAtlas.cursorWidth, m_cursorAtlas.cursorHeight);
205 QImage image = cursor->pixmap().toImage();
206 m_cursor.textureRect = QRectF(0, 0, 1, 1);
207 m_cursor.hotSpot = cursor->hotSpot();
208 m_cursor.useCustomCursor =
false;
209 m_cursor.size = image.size();
210 m_cursor.customCursorImage = image;
211 m_cursor.customCursorPending =
true;
212 m_cursor.customCursorKey = m_cursor.customCursorImage.cacheKey();
226 m_allScreens(allScreens)
238bool QEglFSCursor::event(QEvent *e)
240 if (e->type() == QEvent::User + 1) {
241 CursorUpdateEvent *ev =
static_cast<CursorUpdateEvent *>(e);
242 m_updateRequested =
false;
243 if (!ev->allScreens()) {
244 QWindow *w = m_screen->topLevelAt(ev->pos());
246 QWindowSystemInterface::handleExposeEvent(w, ev->rect());
247 QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents);
250 for (QWindow *w : qGuiApp->topLevelWindows())
251 QWindowSystemInterface::handleExposeEvent(w, w->geometry());
252 QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents);
256 return QPlatformCursor::event(e);
259void QEglFSCursor::update(
const QRect &rect,
bool allScreens)
261 if (!m_updateRequested) {
265 m_updateRequested =
true;
266 QCoreApplication::postEvent(
this,
new CursorUpdateEvent(m_cursor.pos, rect, allScreens));
270QRect QEglFSCursor::cursorRect()
const
272 return QRect(m_cursor.pos - m_cursor.hotSpot, m_cursor.size);
275QPoint QEglFSCursor::pos()
const
280void QEglFSCursor::setPos(
const QPoint &pos)
282 QGuiApplicationPrivate::inputDeviceManager()->setCursorPos(pos);
283 const QRect oldCursorRect = cursorRect();
285 update(oldCursorRect | cursorRect(),
false);
286 for (QPlatformScreen *screen : m_screen->virtualSiblings())
287 static_cast<QEglFSScreen *>(screen)->handleCursorMove(m_cursor.pos);
290void QEglFSCursor::pointerEvent(
const QMouseEvent &event)
292 if (event.type() != QEvent::MouseMove)
294 const QRect oldCursorRect = cursorRect();
295 m_cursor.pos = event.globalPosition().toPoint();
296 update(oldCursorRect | cursorRect(),
false);
297 for (QPlatformScreen *screen : m_screen->virtualSiblings())
298 static_cast<QEglFSScreen *>(screen)->handleCursorMove(m_cursor.pos);
301void QEglFSCursor::paintOnScreen()
308 QRectF cr = cursorRect();
314 for (QPlatformScreen *screen : m_screen->virtualSiblings()) {
315 if (screen->geometry().contains(cr.topLeft().toPoint() + m_cursor.hotSpot))
317 cr.translate(-screen->geometry().topLeft());
318 const QSize screenSize = screen->geometry().size();
319 const GLfloat x1 = 2 * (cr.left() / GLfloat(screenSize.width())) - 1;
320 const GLfloat x2 = 2 * (cr.right() / GLfloat(screenSize.width())) - 1;
321 const GLfloat y1 = 1 - (cr.top() / GLfloat(screenSize.height())) * 2;
322 const GLfloat y2 = 1 - (cr.bottom() / GLfloat(screenSize.height())) * 2;
323 QRectF r(QPointF(x1, y1), QPointF(x2, y2));
327 if (screen != m_activeScreen) {
328 m_activeScreen = screen;
330 update(cursorRect(),
true);
346 vaoHelper = QOpenGLVertexArrayObjectHelper::vertexArrayObjectHelperForContext(QOpenGLContext::currentContext());
348 static bool windowsChecked =
false;
349 static bool shouldSave =
true;
350 if (!windowsChecked) {
351 windowsChecked =
true;
352 QWindowList windows = QGuiApplication::allWindows();
353 if (!windows.isEmpty() && windows[0]->inherits(
"QQuickWindow"))
360 f->glGetIntegerv(GL_CURRENT_PROGRAM, &program);
361 f->glGetIntegerv(GL_TEXTURE_BINDING_2D, &texture);
362 f->glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTexture);
363 f->glGetIntegerv(GL_FRONT_FACE, &frontFace);
364 cull = f->glIsEnabled(GL_CULL_FACE);
365 depthTest = f->glIsEnabled(GL_DEPTH_TEST);
366 blend = f->glIsEnabled(GL_BLEND);
367 f->glGetIntegerv(GL_BLEND_SRC_RGB, blendFunc);
368 f->glGetIntegerv(GL_BLEND_SRC_ALPHA, blendFunc + 1);
369 f->glGetIntegerv(GL_BLEND_DST_RGB, blendFunc + 2);
370 f->glGetIntegerv(GL_BLEND_DST_ALPHA, blendFunc + 3);
371 scissor = f->glIsEnabled(GL_SCISSOR_TEST);
372 stencil = f->glIsEnabled(GL_STENCIL_TEST);
373 f->glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &arrayBuf);
374 if (vaoHelper->isValid())
378 for (
int i = 0; i < 2; ++i) {
379 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &va[i].enabled);
380 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_SIZE, &va[i].size);
381 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_TYPE, &va[i].type);
382 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &va[i].normalized);
383 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &va[i].stride);
384 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &va[i].buffer);
385 f->glGetVertexAttribPointerv(i, GL_VERTEX_ATTRIB_ARRAY_POINTER, &va[i].pointer);
390 f->glUseProgram(program);
391 f->glBindTexture(GL_TEXTURE_2D, texture);
392 f->glActiveTexture(activeTexture);
393 f->glFrontFace(frontFace);
395 f->glEnable(GL_CULL_FACE);
397 f->glEnable(GL_DEPTH_TEST);
399 f->glDisable(GL_BLEND);
400 f->glBlendFuncSeparate(blendFunc[0], blendFunc[1], blendFunc[2], blendFunc[3]);
402 f->glEnable(GL_SCISSOR_TEST);
404 f->glEnable(GL_STENCIL_TEST);
405 f->glBindBuffer(GL_ARRAY_BUFFER, arrayBuf);
406 if (vaoHelper->isValid())
407 vaoHelper->glBindVertexArray(vao);
408 for (
int i = 0; i < 2; ++i) {
410 f->glEnableVertexAttribArray(i);
412 f->glDisableVertexAttribArray(i);
413 f->glBindBuffer(GL_ARRAY_BUFFER, va[i].buffer);
414 f->glVertexAttribPointer(i, va[i].size, va[i].type, va[i].normalized, va[i].stride, va[i].pointer);
436void QEglFSCursor::draw(
const QRectF &r)
438 Q_ASSERT(QOpenGLContext::currentContext());
439 QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
440 StateSaver stateSaver(f);
442 QEglFSCursorData &gfx =
static_cast<QEglFSContext*>(QOpenGLContext::currentContext()->handle())->cursorData;
444 createShaderPrograms();
446 if (!gfx.atlasTexture) {
447 createCursorTexture(&gfx.atlasTexture, m_cursorAtlas.image);
449 if (m_cursor.shape != Qt::BitmapCursor)
450 m_cursor.useCustomCursor =
false;
454 if (m_cursor.shape == Qt::BitmapCursor && (m_cursor.customCursorPending || m_cursor.customCursorKey != gfx.customCursorKey)) {
456 createCursorTexture(&gfx.customCursorTexture, m_cursor.customCursorImage);
457 m_cursor.useCustomCursor =
true;
458 m_cursor.customCursorPending =
false;
459 gfx.customCursorKey = m_cursor.customCursorKey;
462 GLuint cursorTexture = !m_cursor.useCustomCursor ? gfx.atlasTexture : gfx.customCursorTexture;
463 Q_ASSERT(cursorTexture);
467 const GLfloat x1 = r.left();
468 const GLfloat x2 = r.right();
469 const GLfloat y1 = r.top();
470 const GLfloat y2 = r.bottom();
471 const GLfloat cursorCoordinates[] = {
478 const GLfloat s1 = m_cursor.textureRect.left();
479 const GLfloat s2 = m_cursor.textureRect.right();
480 const GLfloat t1 = m_cursor.textureRect.top();
481 const GLfloat t2 = m_cursor.textureRect.bottom();
482 const GLfloat textureCoordinates[] = {
489 f->glActiveTexture(GL_TEXTURE0);
490 f->glBindTexture(GL_TEXTURE_2D, cursorTexture);
492 if (stateSaver.vaoHelper->isValid())
493 stateSaver.vaoHelper->glBindVertexArray(0);
495 f->glBindBuffer(GL_ARRAY_BUFFER, 0);
497 gfx.program->enableAttributeArray(0);
498 gfx.program->enableAttributeArray(1);
499 gfx.program->setAttributeArray(0, cursorCoordinates, 2);
500 gfx.program->setAttributeArray(1, textureCoordinates, 2);
502 gfx.program->setUniformValue(gfx.textureEntry, 0);
503 gfx.program->setUniformValue(gfx.matEntry, m_rotationMatrix);
505 f->glDisable(GL_CULL_FACE);
506 f->glFrontFace(GL_CCW);
507 f->glEnable(GL_BLEND);
508 f->glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
509 f->glDisable(GL_DEPTH_TEST);
510 f->glDisable(GL_SCISSOR_TEST);
511 f->glDisable(GL_STENCIL_TEST);
513 f->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
515 gfx.program->disableAttributeArray(0);
516 gfx.program->disableAttributeArray(1);
517 gfx.program->release();
522#include "moc_qeglfscursor_p.cpp"
CursorUpdateEvent(const QPoint &pos, const QRect &rect, bool allScreens)
#define GL_VERTEX_ARRAY_BINDING
StateSaver(QOpenGLFunctions *func)
QOpenGLVertexArrayObjectHelper * vaoHelper