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
qeglfscursor.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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// Qt-Security score:significant reason:default
4
9
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>
17
18#include <QtGui/private/qguiapplication_p.h>
19#include <QtOpenGL/private/qopenglvertexarrayobject_p.h>
20
21#ifndef GL_VERTEX_ARRAY_BINDING
22#define GL_VERTEX_ARRAY_BINDING 0x85B5
23#endif
24
26
27using namespace Qt::StringLiterals;
28
29QEglFSCursor::QEglFSCursor(QPlatformScreen *screen)
30 : m_visible(true),
31 m_screen(static_cast<QEglFSScreen *>(screen)),
32 m_activeScreen(nullptr),
33 m_deviceListener(nullptr),
34 m_updateRequested(false)
35{
36 QByteArray hideCursorVal = qgetenv("QT_QPA_EGLFS_HIDECURSOR");
37 if (!hideCursorVal.isEmpty())
38 m_visible = hideCursorVal.toInt() == 0;
39 if (!m_visible)
40 return;
41
42 int rotation = qEnvironmentVariableIntValue("QT_QPA_EGLFS_ROTATION");
43 if (rotation)
44 m_rotationMatrix.rotate(rotation, 0, 0, 1);
45
46 // Try to load the cursor atlas. If this fails, m_visible is set to false and
47 // paintOnScreen() and setCurrentCursor() become no-ops.
48 initCursorAtlas();
49
50 // initialize the cursor
51#ifndef QT_NO_CURSOR
52 QCursor cursor(Qt::ArrowCursor);
53 setCurrentCursor(&cursor);
54#endif
55
56 m_deviceListener = new QEglFSCursorDeviceListener(this);
57 connect(QGuiApplicationPrivate::inputDeviceManager(), &QInputDeviceManager::deviceListChanged,
58 m_deviceListener, &QEglFSCursorDeviceListener::onDeviceListChanged);
59 updateMouseStatus();
60}
61
62QEglFSCursor::~QEglFSCursor()
63{
64 resetResources();
65 delete m_deviceListener;
66}
67
68void QEglFSCursor::updateMouseStatus()
69{
70 m_visible = m_deviceListener->hasMouse();
71}
72
73bool QEglFSCursorDeviceListener::hasMouse() const
74{
75 return QGuiApplicationPrivate::inputDeviceManager()->deviceCount(QInputDeviceManager::DeviceTypePointer) > 0;
76}
77
78void QEglFSCursorDeviceListener::onDeviceListChanged(QInputDeviceManager::DeviceType type)
79{
80 if (type == QInputDeviceManager::DeviceTypePointer)
81 m_cursor->updateMouseStatus();
82}
83
84void QEglFSCursor::resetResources()
85{
86 m_cursor.customCursorPending = !m_cursor.customCursorImage.isNull();
87}
88
89void QEglFSCursor::createShaderPrograms()
90{
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"
96 "void main() {\n"
97 " textureCoord = textureCoordEntry;\n"
98 " gl_Position = mat * vec4(vertexCoordEntry, 1.0, 1.0);\n"
99 "}\n";
100
101 static const char *textureFragmentProgram =
102 "uniform sampler2D texture;\n"
103 "varying highp vec2 textureCoord;\n"
104 "void main() {\n"
105 " gl_FragColor = texture2D(texture, textureCoord).bgra;\n"
106 "}\n";
107
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);
114 gfx.program->link();
115
116 gfx.textureEntry = gfx.program->uniformLocation("texture");
117 gfx.matEntry = gfx.program->uniformLocation("mat");
118}
119
120void QEglFSCursor::createCursorTexture(uint *texture, const QImage &image)
121{
122 Q_ASSERT(QOpenGLContext::currentContext());
123 QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
124 if (!*texture)
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);
131
132 f->glTexImage2D(GL_TEXTURE_2D, 0 /* level */, GL_RGBA, image.width(), image.height(), 0 /* border */,
133 GL_RGBA, GL_UNSIGNED_BYTE, image.constBits());
134}
135
136void QEglFSCursor::initCursorAtlas()
137{
138 static QByteArray json = qgetenv("QT_QPA_EGLFS_CURSOR");
139 if (json.isEmpty())
140 json = ":/cursor.json";
141
142 QFile file(QString::fromUtf8(json));
143 if (!file.open(QFile::ReadOnly)) {
144 m_visible = false;
145 return;
146 }
147
148 QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
149 QJsonObject object = doc.object();
150
151 QString atlas = object.value("image"_L1).toString();
152 Q_ASSERT(!atlas.isEmpty());
153
154 const int cursorsPerRow = object.value("cursorsPerRow"_L1).toDouble();
155 Q_ASSERT(cursorsPerRow);
156 m_cursorAtlas.cursorsPerRow = cursorsPerRow;
157
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;
163 }
164
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;
171}
172
173#ifndef QT_NO_CURSOR
174void QEglFSCursor::changeCursor(QCursor *cursor, QWindow *window)
175{
176 Q_UNUSED(window);
177 const QRect oldCursorRect = cursorRect();
178 if (setCurrentCursor(cursor))
179 update(oldCursorRect | cursorRect(), false);
180}
181
182bool QEglFSCursor::setCurrentCursor(QCursor *cursor)
183{
184 if (!m_visible)
185 return false;
186
187 const Qt::CursorShape newShape = cursor ? cursor->shape() : Qt::ArrowCursor;
188 if (m_cursor.shape == newShape && newShape != Qt::BitmapCursor)
189 return false;
190
191 if (m_cursor.shape == Qt::BitmapCursor) {
192 m_cursor.customCursorImage = QImage();
193 m_cursor.customCursorPending = false;
194 }
195 m_cursor.shape = newShape;
196 if (newShape != Qt::BitmapCursor) { // standard cursor
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),
201 ws, hs);
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);
205 } else {
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; // will get updated in the next render()
210 m_cursor.size = image.size();
211 m_cursor.customCursorImage = image;
212 m_cursor.customCursorPending = true;
213 m_cursor.customCursorKey = m_cursor.customCursorImage.cacheKey();
214 }
215
216 return true;
217}
218#endif
219
221{
222public:
223 CursorUpdateEvent(const QPoint &pos, const QRect &rect, bool allScreens)
224 : QEvent(QEvent::Type(QEvent::User + 1)),
225 m_pos(pos),
226 m_rect(rect),
227 m_allScreens(allScreens)
228 { }
229 QPoint pos() const { return m_pos; }
230 QRegion rect() const { return m_rect; }
231 bool allScreens() const { return m_allScreens; }
232
233private:
234 QPoint m_pos;
235 QRect m_rect;
236 bool m_allScreens;
237};
238
239bool QEglFSCursor::event(QEvent *e)
240{
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()); // works for the entire virtual desktop, no need to loop
246 if (w) {
247 QWindowSystemInterface::handleExposeEvent(w, ev->rect());
248 QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents);
249 }
250 } else {
251 for (QWindow *w : qGuiApp->topLevelWindows())
252 QWindowSystemInterface::handleExposeEvent(w, w->geometry());
253 QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents);
254 }
255 return true;
256 }
257 return QPlatformCursor::event(e);
258}
259
260void QEglFSCursor::update(const QRect &rect, bool allScreens)
261{
262 if (!m_updateRequested) {
263 // Must not flush the window system events directly from here since we are likely to
264 // be a called directly from QGuiApplication's processMouseEvents. Flushing events
265 // could cause reentering by dispatching more queued mouse events.
266 m_updateRequested = true;
267 QCoreApplication::postEvent(this, new CursorUpdateEvent(m_cursor.pos, rect, allScreens));
268 }
269}
270
271QRect QEglFSCursor::cursorRect() const
272{
273 return QRect(m_cursor.pos - m_cursor.hotSpot, m_cursor.size);
274}
275
276QPoint QEglFSCursor::pos() const
277{
278 return m_cursor.pos;
279}
280
281void QEglFSCursor::setPos(const QPoint &pos)
282{
283 QGuiApplicationPrivate::inputDeviceManager()->setCursorPos(pos);
284 const QRect oldCursorRect = cursorRect();
285 m_cursor.pos = pos;
286 update(oldCursorRect | cursorRect(), false);
287 for (QPlatformScreen *screen : m_screen->virtualSiblings())
288 static_cast<QEglFSScreen *>(screen)->handleCursorMove(m_cursor.pos);
289}
290
291void QEglFSCursor::pointerEvent(const QMouseEvent &event)
292{
293 if (event.type() != QEvent::MouseMove)
294 return;
295 const QRect oldCursorRect = cursorRect();
296 m_cursor.pos = event.globalPosition().toPoint();
297 update(oldCursorRect | cursorRect(), false);
298 for (QPlatformScreen *screen : m_screen->virtualSiblings())
299 static_cast<QEglFSScreen *>(screen)->handleCursorMove(m_cursor.pos);
300}
301
302void QEglFSCursor::paintOnScreen()
303{
304 if (!m_visible)
305 return;
306
307 // cr must be a QRectF, otherwise cr.right() and bottom() would be off by
308 // one in the calculations below.
309 QRectF cr = cursorRect(); // hotspot included
310
311 // Support virtual desktop too. Backends with multi-screen support (e.g. all
312 // variants of KMS/DRM) will enable this by default. In this case all
313 // screens are siblings of each other. When not enabled, the sibling list
314 // only contains m_screen itself.
315 for (QPlatformScreen *screen : m_screen->virtualSiblings()) {
316 if (screen->geometry().contains(cr.topLeft().toPoint() + m_cursor.hotSpot))
317 {
318 cr.translate(-screen->geometry().topLeft());
319 const QSize screenSize = screen->geometry().size();
320 const GLfloat x1 = 2 * (cr.left() / GLfloat(screenSize.width())) - 1;
321 const GLfloat x2 = 2 * (cr.right() / GLfloat(screenSize.width())) - 1;
322 const GLfloat y1 = 1 - (cr.top() / GLfloat(screenSize.height())) * 2;
323 const GLfloat y2 = 1 - (cr.bottom() / GLfloat(screenSize.height())) * 2;
324 QRectF r(QPointF(x1, y1), QPointF(x2, y2));
325
326 draw(r);
327
328 if (screen != m_activeScreen) {
329 m_activeScreen = screen;
330 // Do not want a leftover cursor on the screen the cursor just left.
331 update(cursorRect(), true);
332 }
333
334 break;
335 }
336 }
337}
338
339// In order to prevent breaking code doing custom OpenGL rendering while
340// expecting the state in the context unchanged, save and restore all the state
341// we touch. The exception is Qt Quick where the scenegraph is known to be able
342// to deal with the changes we make.
344{
345 StateSaver(QOpenGLFunctions* func) {
346 f = func;
347 vaoHelper = QOpenGLVertexArrayObjectHelper::vertexArrayObjectHelperForContext(QOpenGLContext::currentContext());
348
349 static bool windowsChecked = false;
350 static bool shouldSave = true;
351 if (!windowsChecked) {
352 windowsChecked = true;
353 QWindowList windows = QGuiApplication::allWindows();
354 if (!windows.isEmpty() && windows[0]->inherits("QQuickWindow"))
355 shouldSave = false;
356 }
357 saved = shouldSave;
358 if (!shouldSave)
359 return;
360
361 f->glGetIntegerv(GL_CURRENT_PROGRAM, &program);
362 f->glGetIntegerv(GL_TEXTURE_BINDING_2D, &texture);
363 f->glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTexture);
364 f->glGetIntegerv(GL_FRONT_FACE, &frontFace);
365 cull = f->glIsEnabled(GL_CULL_FACE);
366 depthTest = f->glIsEnabled(GL_DEPTH_TEST);
367 blend = f->glIsEnabled(GL_BLEND);
368 f->glGetIntegerv(GL_BLEND_SRC_RGB, blendFunc);
369 f->glGetIntegerv(GL_BLEND_SRC_ALPHA, blendFunc + 1);
370 f->glGetIntegerv(GL_BLEND_DST_RGB, blendFunc + 2);
371 f->glGetIntegerv(GL_BLEND_DST_ALPHA, blendFunc + 3);
372 scissor = f->glIsEnabled(GL_SCISSOR_TEST);
373 stencil = f->glIsEnabled(GL_STENCIL_TEST);
374 f->glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &arrayBuf);
375 if (vaoHelper->isValid())
376 f->glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &vao);
377 else
378 vao = 0;
379 for (int i = 0; i < 2; ++i) {
380 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &va[i].enabled);
381 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_SIZE, &va[i].size);
382 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_TYPE, &va[i].type);
383 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &va[i].normalized);
384 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &va[i].stride);
385 f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &va[i].buffer);
386 f->glGetVertexAttribPointerv(i, GL_VERTEX_ATTRIB_ARRAY_POINTER, &va[i].pointer);
387 }
388 }
390 if (saved) {
391 f->glUseProgram(program);
392 f->glBindTexture(GL_TEXTURE_2D, texture);
393 f->glActiveTexture(activeTexture);
394 f->glFrontFace(frontFace);
395 if (cull)
396 f->glEnable(GL_CULL_FACE);
397 if (depthTest)
398 f->glEnable(GL_DEPTH_TEST);
399 if (!blend)
400 f->glDisable(GL_BLEND);
401 f->glBlendFuncSeparate(blendFunc[0], blendFunc[1], blendFunc[2], blendFunc[3]);
402 if (scissor)
403 f->glEnable(GL_SCISSOR_TEST);
404 if (stencil)
405 f->glEnable(GL_STENCIL_TEST);
406 f->glBindBuffer(GL_ARRAY_BUFFER, arrayBuf);
407 if (vaoHelper->isValid())
408 vaoHelper->glBindVertexArray(vao);
409 for (int i = 0; i < 2; ++i) {
410 if (va[i].enabled)
411 f->glEnableVertexAttribArray(i);
412 else
413 f->glDisableVertexAttribArray(i);
414 f->glBindBuffer(GL_ARRAY_BUFFER, va[i].buffer);
415 f->glVertexAttribPointer(i, va[i].size, va[i].type, va[i].normalized, va[i].stride, va[i].pointer);
416 }
417 }
418 }
421 bool saved;
426 bool cull;
428 bool blend;
435};
436
437void QEglFSCursor::draw(const QRectF &r)
438{
439 Q_ASSERT(QOpenGLContext::currentContext());
440 QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
441 StateSaver stateSaver(f);
442
443 QEglFSCursorData &gfx = static_cast<QEglFSContext*>(QOpenGLContext::currentContext()->handle())->cursorData;
444 if (!gfx.program) {
445 createShaderPrograms();
446
447 if (!gfx.atlasTexture) {
448 createCursorTexture(&gfx.atlasTexture, m_cursorAtlas.image);
449
450 if (m_cursor.shape != Qt::BitmapCursor)
451 m_cursor.useCustomCursor = false;
452 }
453 }
454
455 if (m_cursor.shape == Qt::BitmapCursor && (m_cursor.customCursorPending || m_cursor.customCursorKey != gfx.customCursorKey)) {
456 // upload the custom cursor
457 createCursorTexture(&gfx.customCursorTexture, m_cursor.customCursorImage);
458 m_cursor.useCustomCursor = true;
459 m_cursor.customCursorPending = false;
460 gfx.customCursorKey = m_cursor.customCursorKey;
461 }
462
463 GLuint cursorTexture = !m_cursor.useCustomCursor ? gfx.atlasTexture : gfx.customCursorTexture;
464 Q_ASSERT(cursorTexture);
465
466 gfx.program->bind();
467
468 const GLfloat x1 = r.left();
469 const GLfloat x2 = r.right();
470 const GLfloat y1 = r.top();
471 const GLfloat y2 = r.bottom();
472 const GLfloat cursorCoordinates[] = {
473 x1, y2,
474 x2, y2,
475 x1, y1,
476 x2, y1
477 };
478
479 const GLfloat s1 = m_cursor.textureRect.left();
480 const GLfloat s2 = m_cursor.textureRect.right();
481 const GLfloat t1 = m_cursor.textureRect.top();
482 const GLfloat t2 = m_cursor.textureRect.bottom();
483 const GLfloat textureCoordinates[] = {
484 s1, t2,
485 s2, t2,
486 s1, t1,
487 s2, t1
488 };
489
490 f->glActiveTexture(GL_TEXTURE0);
491 f->glBindTexture(GL_TEXTURE_2D, cursorTexture);
492
493 if (stateSaver.vaoHelper->isValid())
494 stateSaver.vaoHelper->glBindVertexArray(0);
495
496 f->glBindBuffer(GL_ARRAY_BUFFER, 0);
497
498 gfx.program->enableAttributeArray(0);
499 gfx.program->enableAttributeArray(1);
500 gfx.program->setAttributeArray(0, cursorCoordinates, 2);
501 gfx.program->setAttributeArray(1, textureCoordinates, 2);
502
503 gfx.program->setUniformValue(gfx.textureEntry, 0);
504 gfx.program->setUniformValue(gfx.matEntry, m_rotationMatrix);
505
506 f->glDisable(GL_CULL_FACE);
507 f->glFrontFace(GL_CCW);
508 f->glEnable(GL_BLEND);
509 f->glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
510 f->glDisable(GL_DEPTH_TEST); // disable depth testing to make sure cursor is always on top
511 f->glDisable(GL_SCISSOR_TEST);
512 f->glDisable(GL_STENCIL_TEST);
513
514 f->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
515
516 gfx.program->disableAttributeArray(0);
517 gfx.program->disableAttributeArray(1);
518 gfx.program->release();
519}
520
521QT_END_NAMESPACE
522
523#include "moc_qeglfscursor_p.cpp"
CursorUpdateEvent(const QPoint &pos, const QRect &rect, bool allScreens)
QPoint pos() const
QRegion rect() const
bool allScreens() const
Combined button and popup list for selecting options.
#define GL_VERTEX_ARRAY_BINDING
GLvoid * pointer
StateSaver(QOpenGLFunctions *func)
QOpenGLFunctions * f
QOpenGLVertexArrayObjectHelper * vaoHelper
GLint activeTexture
GLint blendFunc[4]