diff options
Diffstat (limited to 'src/plugins/platforms/eglfs/api')
18 files changed, 3243 insertions, 0 deletions
diff --git a/src/plugins/platforms/eglfs/api/api.pri b/src/plugins/platforms/eglfs/api/api.pri new file mode 100644 index 0000000000..0ea65bd1ff --- /dev/null +++ b/src/plugins/platforms/eglfs/api/api.pri @@ -0,0 +1,21 @@ + +SOURCES += $$PWD/qeglfswindow.cpp \ + $$PWD/qeglfsscreen.cpp \ + $$PWD/qeglfscursor.cpp \ + $$PWD/qeglfshooks.cpp \ + $$PWD/qeglfsdeviceintegration.cpp \ + $$PWD/qeglfsintegration.cpp \ + $$PWD/qeglfscontext.cpp \ + $$PWD/qeglfsoffscreenwindow.cpp + +HEADERS += $$PWD/qeglfswindow_p.h \ + $$PWD/qeglfsscreen_p.h \ + $$PWD/qeglfscursor_p.h \ + $$PWD/qeglfshooks_p.h \ + $$PWD/qeglfsdeviceintegration_p.h \ + $$PWD/qeglfsintegration_p.h \ + $$PWD/qeglfscontext_p.h \ + $$PWD/qeglfsoffscreenwindow_p.h \ + $$PWD/qeglfsglobal_p.h + +INCLUDEPATH += $$PWD diff --git a/src/plugins/platforms/eglfs/api/qeglfscontext.cpp b/src/plugins/platforms/eglfs/api/qeglfscontext.cpp new file mode 100644 index 0000000000..c5cef34d8e --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfscontext.cpp @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qeglfsglobal_p.h" +#include <QtGui/QSurface> +#include <QtEglSupport/private/qeglconvenience_p.h> +#include <QtEglSupport/private/qeglpbuffer_p.h> + +#include "qeglfscontext_p.h" +#include "qeglfswindow_p.h" +#include "qeglfshooks_p.h" +#include "qeglfscursor_p.h" + +QT_BEGIN_NAMESPACE + +QEglFSContext::QEglFSContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display, + EGLConfig *config, const QVariant &nativeHandle) + : QEGLPlatformContext(format, share, display, config, nativeHandle, + qt_egl_device_integration()->supportsSurfacelessContexts() ? Flags(0) : QEGLPlatformContext::NoSurfaceless), + m_tempWindow(0) +{ +} + +EGLSurface QEglFSContext::eglSurfaceForPlatformSurface(QPlatformSurface *surface) +{ + if (surface->surface()->surfaceClass() == QSurface::Window) + return static_cast<QEglFSWindow *>(surface)->surface(); + else + return static_cast<QEGLPbuffer *>(surface)->pbuffer(); +} + +EGLSurface QEglFSContext::createTemporaryOffscreenSurface() +{ + if (qt_egl_device_integration()->supportsPBuffers()) + return QEGLPlatformContext::createTemporaryOffscreenSurface(); + + if (!m_tempWindow) { + m_tempWindow = qt_egl_device_integration()->createNativeOffscreenWindow(format()); + if (!m_tempWindow) { + qWarning("QEglFSContext: Failed to create temporary native window"); + return EGL_NO_SURFACE; + } + } + EGLConfig config = q_configFromGLFormat(eglDisplay(), format()); + return eglCreateWindowSurface(eglDisplay(), config, m_tempWindow, 0); +} + +void QEglFSContext::destroyTemporaryOffscreenSurface(EGLSurface surface) +{ + if (qt_egl_device_integration()->supportsPBuffers()) { + QEGLPlatformContext::destroyTemporaryOffscreenSurface(surface); + } else { + eglDestroySurface(eglDisplay(), surface); + qt_egl_device_integration()->destroyNativeWindow(m_tempWindow); + m_tempWindow = 0; + } +} + +void QEglFSContext::runGLChecks() +{ + // Note that even though there is an EGL context current here, + // QOpenGLContext and QOpenGLFunctions are not yet usable at this stage. + const char *renderer = reinterpret_cast<const char *>(glGetString(GL_RENDERER)); + // Be nice and warn about a common source of confusion. + if (renderer && strstr(renderer, "llvmpipe")) + qWarning("Running on a software rasterizer (LLVMpipe), expect limited performance."); +} + +void QEglFSContext::swapBuffers(QPlatformSurface *surface) +{ + // draw the cursor + if (surface->surface()->surfaceClass() == QSurface::Window) { + QPlatformWindow *window = static_cast<QPlatformWindow *>(surface); + if (QEglFSCursor *cursor = qobject_cast<QEglFSCursor *>(window->screen()->cursor())) + cursor->paintOnScreen(); + } + + qt_egl_device_integration()->waitForVSync(surface); + QEGLPlatformContext::swapBuffers(surface); + qt_egl_device_integration()->presentBuffer(surface); +} + +QT_END_NAMESPACE diff --git a/src/plugins/platforms/eglfs/api/qeglfscontext_p.h b/src/plugins/platforms/eglfs/api/qeglfscontext_p.h new file mode 100644 index 0000000000..ab5bf99c3c --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfscontext_p.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QEGLFSCONTEXT_H +#define QEGLFSCONTEXT_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qeglfsglobal_p.h" +#include <QtEglSupport/private/qeglplatformcontext_p.h> +#include <QtCore/QVariant> + +QT_BEGIN_NAMESPACE + +class Q_EGLFS_EXPORT QEglFSContext : public QEGLPlatformContext +{ +public: + QEglFSContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display, + EGLConfig *config, const QVariant &nativeHandle); + EGLSurface eglSurfaceForPlatformSurface(QPlatformSurface *surface) Q_DECL_OVERRIDE; + EGLSurface createTemporaryOffscreenSurface() Q_DECL_OVERRIDE; + void destroyTemporaryOffscreenSurface(EGLSurface surface) Q_DECL_OVERRIDE; + void runGLChecks() Q_DECL_OVERRIDE; + void swapBuffers(QPlatformSurface *surface) Q_DECL_OVERRIDE; + +private: + EGLNativeWindowType m_tempWindow; +}; + +QT_END_NAMESPACE + +#endif // QEGLFSCONTEXT_H diff --git a/src/plugins/platforms/eglfs/api/qeglfscursor.cpp b/src/plugins/platforms/eglfs/api/qeglfscursor.cpp new file mode 100644 index 0000000000..2b54251a06 --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfscursor.cpp @@ -0,0 +1,555 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qeglfscursor_p.h" +#include "qeglfsintegration_p.h" +#include "qeglfsscreen_p.h" + +#include <qpa/qwindowsysteminterface.h> +#include <QtGui/QOpenGLContext> +#include <QtGui/QOpenGLShaderProgram> +#include <QtCore/QFile> +#include <QtCore/QJsonDocument> +#include <QtCore/QJsonArray> +#include <QtCore/QJsonObject> + +#include <QtGui/private/qguiapplication_p.h> +#include <QtGui/private/qopenglvertexarrayobject_p.h> + +#ifndef GL_VERTEX_ARRAY_BINDING +#define GL_VERTEX_ARRAY_BINDING 0x85B5 +#endif + +QT_BEGIN_NAMESPACE + +QEglFSCursor::QEglFSCursor(QPlatformScreen *screen) + : m_visible(true), + m_screen(static_cast<QEglFSScreen *>(screen)), + m_activeScreen(nullptr), + m_deviceListener(0), + m_updateRequested(false) +{ + QByteArray hideCursorVal = qgetenv("QT_QPA_EGLFS_HIDECURSOR"); + if (!hideCursorVal.isEmpty()) + m_visible = hideCursorVal.toInt() == 0; + if (!m_visible) + return; + + int rotation = qEnvironmentVariableIntValue("QT_QPA_EGLFS_ROTATION"); + if (rotation) + m_rotationMatrix.rotate(rotation, 0, 0, 1); + + // Try to load the cursor atlas. If this fails, m_visible is set to false and + // paintOnScreen() and setCurrentCursor() become no-ops. + initCursorAtlas(); + + // initialize the cursor +#ifndef QT_NO_CURSOR + QCursor cursor(Qt::ArrowCursor); + setCurrentCursor(&cursor); +#endif + + m_deviceListener = new QEglFSCursorDeviceListener(this); + connect(QGuiApplicationPrivate::inputDeviceManager(), &QInputDeviceManager::deviceListChanged, + m_deviceListener, &QEglFSCursorDeviceListener::onDeviceListChanged); + updateMouseStatus(); +} + +QEglFSCursor::~QEglFSCursor() +{ + resetResources(); + delete m_deviceListener; +} + +void QEglFSCursor::updateMouseStatus() +{ + m_visible = m_deviceListener->hasMouse(); +} + +bool QEglFSCursorDeviceListener::hasMouse() const +{ + return QGuiApplicationPrivate::inputDeviceManager()->deviceCount(QInputDeviceManager::DeviceTypePointer) > 0; +} + +void QEglFSCursorDeviceListener::onDeviceListChanged(QInputDeviceManager::DeviceType type) +{ + if (type == QInputDeviceManager::DeviceTypePointer) + m_cursor->updateMouseStatus(); +} + +void QEglFSCursor::resetResources() +{ + if (QOpenGLContext *ctx = QOpenGLContext::currentContext()) { + GraphicsContextData &gfx(m_gfx[ctx]); + delete gfx.program; + glDeleteTextures(1, &gfx.customCursorTexture); + glDeleteTextures(1, &gfx.atlasTexture); + gfx = GraphicsContextData(); + } + m_cursor.customCursorPending = !m_cursor.customCursorImage.isNull(); +} + +void QEglFSCursor::createShaderPrograms() +{ + static const char *textureVertexProgram = + "attribute highp vec2 vertexCoordEntry;\n" + "attribute highp vec2 textureCoordEntry;\n" + "varying highp vec2 textureCoord;\n" + "uniform highp mat4 mat;\n" + "void main() {\n" + " textureCoord = textureCoordEntry;\n" + " gl_Position = mat * vec4(vertexCoordEntry, 1.0, 1.0);\n" + "}\n"; + + static const char *textureFragmentProgram = + "uniform sampler2D texture;\n" + "varying highp vec2 textureCoord;\n" + "void main() {\n" + " gl_FragColor = texture2D(texture, textureCoord).bgra;\n" + "}\n"; + + GraphicsContextData &gfx(m_gfx[QOpenGLContext::currentContext()]); + gfx.program = new QOpenGLShaderProgram; + gfx.program->addShaderFromSourceCode(QOpenGLShader::Vertex, textureVertexProgram); + gfx.program->addShaderFromSourceCode(QOpenGLShader::Fragment, textureFragmentProgram); + gfx.program->bindAttributeLocation("vertexCoordEntry", 0); + gfx.program->bindAttributeLocation("textureCoordEntry", 1); + gfx.program->link(); + + gfx.textureEntry = gfx.program->uniformLocation("texture"); + gfx.matEntry = gfx.program->uniformLocation("mat"); +} + +void QEglFSCursor::createCursorTexture(uint *texture, const QImage &image) +{ + if (!*texture) + glGenTextures(1, texture); + glBindTexture(GL_TEXTURE_2D, *texture); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glTexImage2D(GL_TEXTURE_2D, 0 /* level */, GL_RGBA, image.width(), image.height(), 0 /* border */, + GL_RGBA, GL_UNSIGNED_BYTE, image.constBits()); +} + +void QEglFSCursor::initCursorAtlas() +{ + static QByteArray json = qgetenv("QT_QPA_EGLFS_CURSOR"); + if (json.isEmpty()) + json = ":/cursor.json"; + + QFile file(QString::fromUtf8(json)); + if (!file.open(QFile::ReadOnly)) { + m_visible = false; + return; + } + + QJsonDocument doc = QJsonDocument::fromJson(file.readAll()); + QJsonObject object = doc.object(); + + QString atlas = object.value(QLatin1String("image")).toString(); + Q_ASSERT(!atlas.isEmpty()); + + const int cursorsPerRow = object.value(QLatin1String("cursorsPerRow")).toDouble(); + Q_ASSERT(cursorsPerRow); + m_cursorAtlas.cursorsPerRow = cursorsPerRow; + + const QJsonArray hotSpots = object.value(QLatin1String("hotSpots")).toArray(); + Q_ASSERT(hotSpots.count() == Qt::LastCursor + 1); + for (int i = 0; i < hotSpots.count(); i++) { + QPoint hotSpot(hotSpots[i].toArray()[0].toDouble(), hotSpots[i].toArray()[1].toDouble()); + m_cursorAtlas.hotSpots << hotSpot; + } + + QImage image = QImage(atlas).convertToFormat(QImage::Format_ARGB32_Premultiplied); + m_cursorAtlas.cursorWidth = image.width() / m_cursorAtlas.cursorsPerRow; + m_cursorAtlas.cursorHeight = image.height() / ((Qt::LastCursor + cursorsPerRow) / cursorsPerRow); + m_cursorAtlas.width = image.width(); + m_cursorAtlas.height = image.height(); + m_cursorAtlas.image = image; +} + +#ifndef QT_NO_CURSOR +void QEglFSCursor::changeCursor(QCursor *cursor, QWindow *window) +{ + Q_UNUSED(window); + const QRect oldCursorRect = cursorRect(); + if (setCurrentCursor(cursor)) + update(oldCursorRect | cursorRect(), false); +} + +bool QEglFSCursor::setCurrentCursor(QCursor *cursor) +{ + if (!m_visible) + return false; + + const Qt::CursorShape newShape = cursor ? cursor->shape() : Qt::ArrowCursor; + if (m_cursor.shape == newShape && newShape != Qt::BitmapCursor) + return false; + + if (m_cursor.shape == Qt::BitmapCursor) { + m_cursor.customCursorImage = QImage(); + m_cursor.customCursorPending = false; + } + m_cursor.shape = newShape; + if (newShape != Qt::BitmapCursor) { // standard cursor + const float ws = (float)m_cursorAtlas.cursorWidth / m_cursorAtlas.width, + hs = (float)m_cursorAtlas.cursorHeight / m_cursorAtlas.height; + m_cursor.textureRect = QRectF(ws * (m_cursor.shape % m_cursorAtlas.cursorsPerRow), + hs * (m_cursor.shape / m_cursorAtlas.cursorsPerRow), + ws, hs); + m_cursor.hotSpot = m_cursorAtlas.hotSpots[m_cursor.shape]; + m_cursor.useCustomCursor = false; + m_cursor.size = QSize(m_cursorAtlas.cursorWidth, m_cursorAtlas.cursorHeight); + } else { + QImage image = cursor->pixmap().toImage(); + m_cursor.textureRect = QRectF(0, 0, 1, 1); + m_cursor.hotSpot = cursor->hotSpot(); + m_cursor.useCustomCursor = false; // will get updated in the next render() + m_cursor.size = image.size(); + m_cursor.customCursorImage = image; + m_cursor.customCursorPending = true; + m_cursor.customCursorKey = m_cursor.customCursorImage.cacheKey(); + } + + return true; +} +#endif + +class CursorUpdateEvent : public QEvent +{ +public: + CursorUpdateEvent(const QPoint &pos, const QRect &rect, bool allScreens) + : QEvent(QEvent::Type(QEvent::User + 1)), + m_pos(pos), + m_rect(rect), + m_allScreens(allScreens) + { } + QPoint pos() const { return m_pos; } + QRegion rect() const { return m_rect; } + bool allScreens() const { return m_allScreens; } + +private: + QPoint m_pos; + QRect m_rect; + bool m_allScreens; +}; + +bool QEglFSCursor::event(QEvent *e) +{ + if (e->type() == QEvent::User + 1) { + CursorUpdateEvent *ev = static_cast<CursorUpdateEvent *>(e); + m_updateRequested = false; + if (!ev->allScreens()) { + QWindow *w = m_screen->topLevelAt(ev->pos()); // works for the entire virtual desktop, no need to loop + if (w) { + QWindowSystemInterface::handleExposeEvent(w, ev->rect()); + QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents); + } + } else { + for (QWindow *w : qGuiApp->topLevelWindows()) + QWindowSystemInterface::handleExposeEvent(w, w->geometry()); + QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents); + } + return true; + } + return QPlatformCursor::event(e); +} + +void QEglFSCursor::update(const QRect &rect, bool allScreens) +{ + if (!m_updateRequested) { + // Must not flush the window system events directly from here since we are likely to + // be a called directly from QGuiApplication's processMouseEvents. Flushing events + // could cause reentering by dispatching more queued mouse events. + m_updateRequested = true; + QCoreApplication::postEvent(this, new CursorUpdateEvent(m_cursor.pos, rect, allScreens)); + } +} + +QRect QEglFSCursor::cursorRect() const +{ + return QRect(m_cursor.pos - m_cursor.hotSpot, m_cursor.size); +} + +QPoint QEglFSCursor::pos() const +{ + return m_cursor.pos; +} + +void QEglFSCursor::setPos(const QPoint &pos) +{ + QGuiApplicationPrivate::inputDeviceManager()->setCursorPos(pos); + const QRect oldCursorRect = cursorRect(); + m_cursor.pos = pos; + update(oldCursorRect | cursorRect(), false); + for (QPlatformScreen *screen : m_screen->virtualSiblings()) + static_cast<QEglFSScreen *>(screen)->handleCursorMove(m_cursor.pos); +} + +void QEglFSCursor::pointerEvent(const QMouseEvent &event) +{ + if (event.type() != QEvent::MouseMove) + return; + const QRect oldCursorRect = cursorRect(); + m_cursor.pos = event.screenPos().toPoint(); + update(oldCursorRect | cursorRect(), false); + for (QPlatformScreen *screen : m_screen->virtualSiblings()) + static_cast<QEglFSScreen *>(screen)->handleCursorMove(m_cursor.pos); +} + +void QEglFSCursor::paintOnScreen() +{ + if (!m_visible) + return; + + // cr must be a QRectF, otherwise cr.right() and bottom() would be off by + // one in the calculations below. + QRectF cr = cursorRect(); // hotspot included + + // Support virtual desktop too. Backends with multi-screen support (e.g. all + // variants of KMS/DRM) will enable this by default. In this case all + // screens are siblings of each other. When not enabled, the sibling list + // only contains m_screen itself. + for (QPlatformScreen *screen : m_screen->virtualSiblings()) { + if (screen->geometry().contains(cr.topLeft().toPoint() + m_cursor.hotSpot) + && QOpenGLContext::currentContext()->screen() == screen->screen()) + { + cr.translate(-screen->geometry().topLeft()); + const QSize screenSize = screen->geometry().size(); + const GLfloat x1 = 2 * (cr.left() / GLfloat(screenSize.width())) - 1; + const GLfloat x2 = 2 * (cr.right() / GLfloat(screenSize.width())) - 1; + const GLfloat y1 = 1 - (cr.top() / GLfloat(screenSize.height())) * 2; + const GLfloat y2 = 1 - (cr.bottom() / GLfloat(screenSize.height())) * 2; + QRectF r(QPointF(x1, y1), QPointF(x2, y2)); + + draw(r); + + if (screen != m_activeScreen) { + m_activeScreen = screen; + // Do not want a leftover cursor on the screen the cursor just left. + update(cursorRect(), true); + } + + break; + } + } +} + +// In order to prevent breaking code doing custom OpenGL rendering while +// expecting the state in the context unchanged, save and restore all the state +// we touch. The exception is Qt Quick where the scenegraph is known to be able +// to deal with the changes we make. +struct StateSaver +{ + StateSaver() { + f = QOpenGLContext::currentContext()->functions(); + vaoHelper = new QOpenGLVertexArrayObjectHelper(QOpenGLContext::currentContext()); + + static bool windowsChecked = false; + static bool shouldSave = true; + if (!windowsChecked) { + windowsChecked = true; + QWindowList windows = QGuiApplication::allWindows(); + if (!windows.isEmpty() && windows[0]->inherits("QQuickWindow")) + shouldSave = false; + } + saved = shouldSave; + if (!shouldSave) + return; + + f->glGetIntegerv(GL_CURRENT_PROGRAM, &program); + f->glGetIntegerv(GL_TEXTURE_BINDING_2D, &texture); + f->glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTexture); + f->glGetIntegerv(GL_FRONT_FACE, &frontFace); + cull = f->glIsEnabled(GL_CULL_FACE); + depthTest = f->glIsEnabled(GL_DEPTH_TEST); + blend = f->glIsEnabled(GL_BLEND); + f->glGetIntegerv(GL_BLEND_SRC_RGB, blendFunc); + f->glGetIntegerv(GL_BLEND_SRC_ALPHA, blendFunc + 1); + f->glGetIntegerv(GL_BLEND_DST_RGB, blendFunc + 2); + f->glGetIntegerv(GL_BLEND_DST_ALPHA, blendFunc + 3); + f->glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &arrayBuf); + if (vaoHelper->isValid()) + f->glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &vao); + for (int i = 0; i < 2; ++i) { + f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &va[i].enabled); + f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_SIZE, &va[i].size); + f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_TYPE, &va[i].type); + f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &va[i].normalized); + f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &va[i].stride); + f->glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &va[i].buffer); + f->glGetVertexAttribPointerv(i, GL_VERTEX_ATTRIB_ARRAY_POINTER, &va[i].pointer); + } + } + ~StateSaver() { + if (saved) { + f->glUseProgram(program); + f->glBindTexture(GL_TEXTURE_2D, texture); + f->glActiveTexture(activeTexture); + f->glFrontFace(frontFace); + if (cull) + f->glEnable(GL_CULL_FACE); + else + f->glDisable(GL_CULL_FACE); + if (depthTest) + f->glEnable(GL_DEPTH_TEST); + else + f->glDisable(GL_DEPTH_TEST); + if (blend) + f->glEnable(GL_BLEND); + else + f->glDisable(GL_BLEND); + f->glBlendFuncSeparate(blendFunc[0], blendFunc[1], blendFunc[2], blendFunc[3]); + f->glBindBuffer(GL_ARRAY_BUFFER, arrayBuf); + if (vaoHelper->isValid()) + vaoHelper->glBindVertexArray(vao); + for (int i = 0; i < 2; ++i) { + if (va[i].enabled) + f->glEnableVertexAttribArray(i); + else + f->glDisableVertexAttribArray(i); + f->glBindBuffer(GL_ARRAY_BUFFER, va[i].buffer); + f->glVertexAttribPointer(i, va[i].size, va[i].type, va[i].normalized, va[i].stride, va[i].pointer); + } + } + delete vaoHelper; + } + QOpenGLFunctions *f; + QOpenGLVertexArrayObjectHelper *vaoHelper; + bool saved; + GLint program; + GLint texture; + GLint activeTexture; + GLint frontFace; + bool cull; + bool depthTest; + bool blend; + GLint blendFunc[4]; + GLint vao; + GLint arrayBuf; + struct { GLint enabled, type, size, normalized, stride, buffer; GLvoid *pointer; } va[2]; +}; + +void QEglFSCursor::draw(const QRectF &r) +{ + StateSaver stateSaver; + + GraphicsContextData &gfx(m_gfx[QOpenGLContext::currentContext()]); + if (!gfx.program) { + // one time initialization + initializeOpenGLFunctions(); + + createShaderPrograms(); + + if (!gfx.atlasTexture) { + createCursorTexture(&gfx.atlasTexture, m_cursorAtlas.image); + + if (m_cursor.shape != Qt::BitmapCursor) + m_cursor.useCustomCursor = false; + } + } + + if (m_cursor.shape == Qt::BitmapCursor && (m_cursor.customCursorPending || m_cursor.customCursorKey != gfx.customCursorKey)) { + // upload the custom cursor + createCursorTexture(&gfx.customCursorTexture, m_cursor.customCursorImage); + m_cursor.useCustomCursor = true; + m_cursor.customCursorPending = false; + gfx.customCursorKey = m_cursor.customCursorKey; + } + + GLuint cursorTexture = !m_cursor.useCustomCursor ? gfx.atlasTexture : gfx.customCursorTexture; + Q_ASSERT(cursorTexture); + + gfx.program->bind(); + + const GLfloat x1 = r.left(); + const GLfloat x2 = r.right(); + const GLfloat y1 = r.top(); + const GLfloat y2 = r.bottom(); + const GLfloat cursorCoordinates[] = { + x1, y2, + x2, y2, + x1, y1, + x2, y1 + }; + + const GLfloat s1 = m_cursor.textureRect.left(); + const GLfloat s2 = m_cursor.textureRect.right(); + const GLfloat t1 = m_cursor.textureRect.top(); + const GLfloat t2 = m_cursor.textureRect.bottom(); + const GLfloat textureCoordinates[] = { + s1, t2, + s2, t2, + s1, t1, + s2, t1 + }; + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, cursorTexture); + + if (stateSaver.vaoHelper->isValid()) + stateSaver.vaoHelper->glBindVertexArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + + gfx.program->enableAttributeArray(0); + gfx.program->enableAttributeArray(1); + gfx.program->setAttributeArray(0, cursorCoordinates, 2); + gfx.program->setAttributeArray(1, textureCoordinates, 2); + + gfx.program->setUniformValue(gfx.textureEntry, 0); + gfx.program->setUniformValue(gfx.matEntry, m_rotationMatrix); + + glDisable(GL_CULL_FACE); + glFrontFace(GL_CCW); + glEnable(GL_BLEND); + glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + glDisable(GL_DEPTH_TEST); // disable depth testing to make sure cursor is always on top + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + gfx.program->disableAttributeArray(0); + gfx.program->disableAttributeArray(1); + gfx.program->release(); +} + +QT_END_NAMESPACE diff --git a/src/plugins/platforms/eglfs/api/qeglfscursor_p.h b/src/plugins/platforms/eglfs/api/qeglfscursor_p.h new file mode 100644 index 0000000000..bf6dbc8a21 --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfscursor_p.h @@ -0,0 +1,159 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QEGLFSCURSOR_H +#define QEGLFSCURSOR_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qeglfsglobal_p.h" +#include <qpa/qplatformcursor.h> +#include <qpa/qplatformscreen.h> +#include <QtGui/QMatrix4x4> +#include <QtGui/QOpenGLFunctions> +#include <QtGui/private/qinputdevicemanager_p.h> + +QT_BEGIN_NAMESPACE + +class QOpenGLShaderProgram; +class QEglFSCursor; +class QEglFSScreen; + +class QEglFSCursorDeviceListener : public QObject +{ + Q_OBJECT + +public: + QEglFSCursorDeviceListener(QEglFSCursor *cursor) : m_cursor(cursor) { } + bool hasMouse() const; + +public slots: + void onDeviceListChanged(QInputDeviceManager::DeviceType type); + +private: + QEglFSCursor *m_cursor; +}; + +class Q_EGLFS_EXPORT QEglFSCursor : public QPlatformCursor, protected QOpenGLFunctions +{ + Q_OBJECT +public: + QEglFSCursor(QPlatformScreen *screen); + ~QEglFSCursor(); + +#ifndef QT_NO_CURSOR + void changeCursor(QCursor *cursor, QWindow *widget) Q_DECL_OVERRIDE; +#endif + void pointerEvent(const QMouseEvent &event) Q_DECL_OVERRIDE; + QPoint pos() const Q_DECL_OVERRIDE; + void setPos(const QPoint &pos) Q_DECL_OVERRIDE; + + QRect cursorRect() const; + void paintOnScreen(); + void resetResources(); + + void updateMouseStatus(); + +private: + bool event(QEvent *e) Q_DECL_OVERRIDE; +#ifndef QT_NO_CURSOR + bool setCurrentCursor(QCursor *cursor); +#endif + void draw(const QRectF &rect); + void update(const QRect &rect, bool allScreens); + void createShaderPrograms(); + void createCursorTexture(uint *texture, const QImage &image); + void initCursorAtlas(); + + // current cursor information + struct Cursor { + Cursor() : shape(Qt::BlankCursor), customCursorPending(false), customCursorKey(0), useCustomCursor(false) { } + Qt::CursorShape shape; + QRectF textureRect; // normalized rect inside texture + QSize size; // size of the cursor + QPoint hotSpot; + QImage customCursorImage; + QPoint pos; // current cursor position + bool customCursorPending; + qint64 customCursorKey; + bool useCustomCursor; + } m_cursor; + + // cursor atlas information + struct CursorAtlas { + CursorAtlas() : cursorsPerRow(0), cursorWidth(0), cursorHeight(0) { } + int cursorsPerRow; + int width, height; // width and height of the atlas + int cursorWidth, cursorHeight; // width and height of cursors inside the atlas + QList<QPoint> hotSpots; + QImage image; // valid until it's uploaded + } m_cursorAtlas; + + bool m_visible; + QEglFSScreen *m_screen; + QPlatformScreen *m_activeScreen; + QEglFSCursorDeviceListener *m_deviceListener; + bool m_updateRequested; + QMatrix4x4 m_rotationMatrix; + + struct GraphicsContextData { + GraphicsContextData() : program(nullptr), textureEntry(0), matEntry(0), + customCursorTexture(0), atlasTexture(0), customCursorKey(0) { } + QOpenGLShaderProgram *program; + int textureEntry; + int matEntry; + uint customCursorTexture; + uint atlasTexture; + qint64 customCursorKey; + }; + QHash<QOpenGLContext *, GraphicsContextData> m_gfx; +}; + +QT_END_NAMESPACE + +#endif // QEGLFSCURSOR_H diff --git a/src/plugins/platforms/eglfs/api/qeglfsdeviceintegration.cpp b/src/plugins/platforms/eglfs/api/qeglfsdeviceintegration.cpp new file mode 100644 index 0000000000..3e1e93f1e4 --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfsdeviceintegration.cpp @@ -0,0 +1,370 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qeglfsdeviceintegration_p.h" +#include "qeglfsintegration_p.h" +#include "qeglfscursor_p.h" +#include "qeglfswindow_p.h" +#include "qeglfsscreen_p.h" +#include "qeglfshooks_p.h" + +#include <QtEglSupport/private/qeglconvenience_p.h> +#include <QGuiApplication> +#include <private/qguiapplication_p.h> +#include <QScreen> +#include <QDir> +#include <QRegularExpression> +#include <QLoggingCategory> + +#if defined(Q_OS_LINUX) +#include <fcntl.h> +#include <unistd.h> +#include <linux/fb.h> +#include <sys/ioctl.h> +#endif + +#include <private/qfactoryloader_p.h> +#include <private/qcore_unix_p.h> + +QT_BEGIN_NAMESPACE + +Q_LOGGING_CATEGORY(qLcEglDevDebug, "qt.qpa.egldeviceintegration") + +Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader, + (QEglFSDeviceIntegrationFactoryInterface_iid, QLatin1String("/egldeviceintegrations"), Qt::CaseInsensitive)) + +#ifndef QT_NO_LIBRARY +Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, directLoader, + (QEglFSDeviceIntegrationFactoryInterface_iid, QLatin1String(""), Qt::CaseInsensitive)) + +#endif // QT_NO_LIBRARY + +QStringList QEglFSDeviceIntegrationFactory::keys(const QString &pluginPath) +{ + QStringList list; +#ifndef QT_NO_LIBRARY + if (!pluginPath.isEmpty()) { + QCoreApplication::addLibraryPath(pluginPath); + list = directLoader()->keyMap().values(); + if (!list.isEmpty()) { + const QString postFix = QLatin1String(" (from ") + + QDir::toNativeSeparators(pluginPath) + + QLatin1Char(')'); + const QStringList::iterator end = list.end(); + for (QStringList::iterator it = list.begin(); it != end; ++it) + (*it).append(postFix); + } + } +#else + Q_UNUSED(pluginPath); +#endif + list.append(loader()->keyMap().values()); + qCDebug(qLcEglDevDebug) << "EGL device integration plugin keys:" << list; + return list; +} + +QEglFSDeviceIntegration *QEglFSDeviceIntegrationFactory::create(const QString &key, const QString &pluginPath) +{ + QEglFSDeviceIntegration *integration = Q_NULLPTR; +#ifndef QT_NO_LIBRARY + if (!pluginPath.isEmpty()) { + QCoreApplication::addLibraryPath(pluginPath); + integration = qLoadPlugin<QEglFSDeviceIntegration, QEglFSDeviceIntegrationPlugin>(directLoader(), key); + } +#else + Q_UNUSED(pluginPath); +#endif + if (!integration) + integration = qLoadPlugin<QEglFSDeviceIntegration, QEglFSDeviceIntegrationPlugin>(loader(), key); + if (integration) + qCDebug(qLcEglDevDebug) << "Using EGL device integration" << key; + else + qCWarning(qLcEglDevDebug) << "Failed to load EGL device integration" << key; + + return integration; +} + +static int framebuffer = -1; + +QByteArray QEglFSDeviceIntegration::fbDeviceName() const +{ +#ifdef Q_OS_LINUX + QByteArray fbDev = qgetenv("QT_QPA_EGLFS_FB"); + if (fbDev.isEmpty()) + fbDev = QByteArrayLiteral("/dev/fb0"); + + return fbDev; +#else + return QByteArray(); +#endif +} + +int QEglFSDeviceIntegration::framebufferIndex() const +{ + int fbIndex = 0; +#ifndef QT_NO_REGULAREXPRESSION + QRegularExpression fbIndexRx(QLatin1String("fb(\\d+)")); + QRegularExpressionMatch match = fbIndexRx.match(QString::fromLocal8Bit(fbDeviceName())); + if (match.hasMatch()) + fbIndex = match.captured(1).toInt(); +#endif + return fbIndex; +} + +void QEglFSDeviceIntegration::platformInit() +{ +#ifdef Q_OS_LINUX + QByteArray fbDev = fbDeviceName(); + + framebuffer = qt_safe_open(fbDev, O_RDONLY); + + if (Q_UNLIKELY(framebuffer == -1)) { + qWarning("EGLFS: Failed to open %s", fbDev.constData()); + qFatal("EGLFS: Can't continue without a display"); + } + +#ifdef FBIOBLANK + ioctl(framebuffer, FBIOBLANK, VESA_NO_BLANKING); +#endif +#endif +} + +void QEglFSDeviceIntegration::platformDestroy() +{ +#ifdef Q_OS_LINUX + if (framebuffer != -1) + close(framebuffer); +#endif +} + +EGLNativeDisplayType QEglFSDeviceIntegration::platformDisplay() const +{ + return EGL_DEFAULT_DISPLAY; +} + +EGLDisplay QEglFSDeviceIntegration::createDisplay(EGLNativeDisplayType nativeDisplay) +{ + return eglGetDisplay(nativeDisplay); +} + +bool QEglFSDeviceIntegration::usesDefaultScreen() +{ + return true; +} + +void QEglFSDeviceIntegration::screenInit() +{ + // Nothing to do here. Called only when usesDefaultScreen is false. +} + +void QEglFSDeviceIntegration::screenDestroy() +{ + QGuiApplication *app = qGuiApp; + QEglFSIntegration *platformIntegration = static_cast<QEglFSIntegration *>( + QGuiApplicationPrivate::platformIntegration()); + while (!app->screens().isEmpty()) + platformIntegration->removeScreen(app->screens().constLast()->handle()); +} + +QSizeF QEglFSDeviceIntegration::physicalScreenSize() const +{ + return q_physicalScreenSizeFromFb(framebuffer, screenSize()); +} + +QSize QEglFSDeviceIntegration::screenSize() const +{ + return q_screenSizeFromFb(framebuffer); +} + +QDpi QEglFSDeviceIntegration::logicalDpi() const +{ + const QSizeF ps = physicalScreenSize(); + const QSize s = screenSize(); + + if (!ps.isEmpty() && !s.isEmpty()) + return QDpi(25.4 * s.width() / ps.width(), + 25.4 * s.height() / ps.height()); + else + return QDpi(100, 100); +} + +qreal QEglFSDeviceIntegration::pixelDensity() const +{ + return qRound(logicalDpi().first / qreal(100)); +} + +Qt::ScreenOrientation QEglFSDeviceIntegration::nativeOrientation() const +{ + return Qt::PrimaryOrientation; +} + +Qt::ScreenOrientation QEglFSDeviceIntegration::orientation() const +{ + return Qt::PrimaryOrientation; +} + +int QEglFSDeviceIntegration::screenDepth() const +{ + return q_screenDepthFromFb(framebuffer); +} + +QImage::Format QEglFSDeviceIntegration::screenFormat() const +{ + return screenDepth() == 16 ? QImage::Format_RGB16 : QImage::Format_RGB32; +} + +qreal QEglFSDeviceIntegration::refreshRate() const +{ + return q_refreshRateFromFb(framebuffer); +} + +EGLint QEglFSDeviceIntegration::surfaceType() const +{ + return EGL_WINDOW_BIT; +} + +QSurfaceFormat QEglFSDeviceIntegration::surfaceFormatFor(const QSurfaceFormat &inputFormat) const +{ + QSurfaceFormat format = inputFormat; + + static const bool force888 = qEnvironmentVariableIntValue("QT_QPA_EGLFS_FORCE888"); + if (force888) { + format.setRedBufferSize(8); + format.setGreenBufferSize(8); + format.setBlueBufferSize(8); + } + + return format; +} + +bool QEglFSDeviceIntegration::filterConfig(EGLDisplay, EGLConfig) const +{ + return true; +} + +QEglFSWindow *QEglFSDeviceIntegration::createWindow(QWindow *window) const +{ + return new QEglFSWindow(window); +} + +EGLNativeWindowType QEglFSDeviceIntegration::createNativeWindow(QPlatformWindow *platformWindow, + const QSize &size, + const QSurfaceFormat &format) +{ + Q_UNUSED(platformWindow); + Q_UNUSED(size); + Q_UNUSED(format); + return 0; +} + +EGLNativeWindowType QEglFSDeviceIntegration::createNativeOffscreenWindow(const QSurfaceFormat &format) +{ + Q_UNUSED(format); + return 0; +} + +void QEglFSDeviceIntegration::destroyNativeWindow(EGLNativeWindowType window) +{ + Q_UNUSED(window); +} + +bool QEglFSDeviceIntegration::hasCapability(QPlatformIntegration::Capability cap) const +{ + Q_UNUSED(cap); + return false; +} + +QPlatformCursor *QEglFSDeviceIntegration::createCursor(QPlatformScreen *screen) const +{ + return new QEglFSCursor(static_cast<QEglFSScreen *>(screen)); +} + +void QEglFSDeviceIntegration::waitForVSync(QPlatformSurface *surface) const +{ + Q_UNUSED(surface); + +#if defined(Q_OS_LINUX) && defined(FBIO_WAITFORVSYNC) + static const bool forceSync = qEnvironmentVariableIntValue("QT_QPA_EGLFS_FORCEVSYNC"); + if (forceSync && framebuffer != -1) { + int arg = 0; + if (ioctl(framebuffer, FBIO_WAITFORVSYNC, &arg) == -1) + qWarning("Could not wait for vsync."); + } +#endif +} + +void QEglFSDeviceIntegration::presentBuffer(QPlatformSurface *surface) +{ + Q_UNUSED(surface); +} + +bool QEglFSDeviceIntegration::supportsPBuffers() const +{ + return true; +} + +bool QEglFSDeviceIntegration::supportsSurfacelessContexts() const +{ + return true; +} + +void *QEglFSDeviceIntegration::wlDisplay() const +{ + return Q_NULLPTR; +} + +EGLConfig QEglFSDeviceIntegration::chooseConfig(EGLDisplay display, const QSurfaceFormat &format) +{ + class Chooser : public QEglConfigChooser { + public: + Chooser(EGLDisplay display) + : QEglConfigChooser(display) { } + bool filterConfig(EGLConfig config) const Q_DECL_OVERRIDE { + return qt_egl_device_integration()->filterConfig(display(), config) + && QEglConfigChooser::filterConfig(config); + } + }; + + Chooser chooser(display); + chooser.setSurfaceType(qt_egl_device_integration()->surfaceType()); + chooser.setSurfaceFormat(format); + return chooser.chooseConfig(); +} + +QT_END_NAMESPACE diff --git a/src/plugins/platforms/eglfs/api/qeglfsdeviceintegration_p.h b/src/plugins/platforms/eglfs/api/qeglfsdeviceintegration_p.h new file mode 100644 index 0000000000..4335554912 --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfsdeviceintegration_p.h @@ -0,0 +1,133 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QEGLFSDEVICEINTEGRATION_H +#define QEGLFSDEVICEINTEGRATION_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qeglfsglobal_p.h" +#include <qpa/qplatformintegration.h> +#include <qpa/qplatformscreen.h> +#include <QtCore/QString> +#include <QtGui/QSurfaceFormat> +#include <QtGui/QImage> + +QT_BEGIN_NAMESPACE + +class QPlatformSurface; +class QEglFSWindow; + +#define QEglFSDeviceIntegrationFactoryInterface_iid "org.qt-project.qt.qpa.egl.QEglFSDeviceIntegrationFactoryInterface.5.5" + +class Q_EGLFS_EXPORT QEglFSDeviceIntegration +{ +public: + virtual ~QEglFSDeviceIntegration() { } + + virtual void platformInit(); + virtual void platformDestroy(); + virtual EGLNativeDisplayType platformDisplay() const; + virtual EGLDisplay createDisplay(EGLNativeDisplayType nativeDisplay); + virtual bool usesDefaultScreen(); + virtual void screenInit(); + virtual void screenDestroy(); + virtual QSizeF physicalScreenSize() const; + virtual QSize screenSize() const; + virtual QDpi logicalDpi() const; + virtual qreal pixelDensity() const; + virtual Qt::ScreenOrientation nativeOrientation() const; + virtual Qt::ScreenOrientation orientation() const; + virtual int screenDepth() const; + virtual QImage::Format screenFormat() const; + virtual qreal refreshRate() const; + virtual QSurfaceFormat surfaceFormatFor(const QSurfaceFormat &inputFormat) const; + virtual EGLint surfaceType() const; + virtual QEglFSWindow *createWindow(QWindow *window) const; + virtual EGLNativeWindowType createNativeWindow(QPlatformWindow *platformWindow, + const QSize &size, + const QSurfaceFormat &format); + virtual EGLNativeWindowType createNativeOffscreenWindow(const QSurfaceFormat &format); + virtual void destroyNativeWindow(EGLNativeWindowType window); + virtual bool hasCapability(QPlatformIntegration::Capability cap) const; + virtual QPlatformCursor *createCursor(QPlatformScreen *screen) const; + virtual bool filterConfig(EGLDisplay display, EGLConfig config) const; + virtual void waitForVSync(QPlatformSurface *surface) const; + virtual void presentBuffer(QPlatformSurface *surface); + virtual QByteArray fbDeviceName() const; + virtual int framebufferIndex() const; + virtual bool supportsPBuffers() const; + virtual bool supportsSurfacelessContexts() const; + + virtual void *wlDisplay() const; + + static EGLConfig chooseConfig(EGLDisplay display, const QSurfaceFormat &format); +}; + +class Q_EGLFS_EXPORT QEglFSDeviceIntegrationPlugin : public QObject +{ + Q_OBJECT + +public: + virtual QEglFSDeviceIntegration *create() = 0; + + // the pattern expected by qLoadPlugin calls for a QString argument. + // we don't need it, so don't bother subclasses with it: + QEglFSDeviceIntegration *create(const QString &) { return create(); } +}; + +class Q_EGLFS_EXPORT QEglFSDeviceIntegrationFactory +{ +public: + static QStringList keys(const QString &pluginPath = QString()); + static QEglFSDeviceIntegration *create(const QString &name, const QString &platformPluginPath = QString()); +}; + +QT_END_NAMESPACE + +#endif // QEGLDEVICEINTEGRATION_H diff --git a/src/plugins/platforms/eglfs/api/qeglfsglobal_p.h b/src/plugins/platforms/eglfs/api/qeglfsglobal_p.h new file mode 100644 index 0000000000..8d76ff5ee0 --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfsglobal_p.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QEGLFSGLOBAL_H +#define QEGLFSGLOBAL_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtCore/qglobal.h> + +#include <QtEglSupport/private/qt_egl_p.h> + +QT_BEGIN_NAMESPACE + +#ifdef QT_BUILD_EGL_DEVICE_LIB +#define Q_EGLFS_EXPORT Q_DECL_EXPORT +#else +#define Q_EGLFS_EXPORT Q_DECL_IMPORT +#endif + +#undef Status +#undef None +#undef Bool +#undef CursorShape +#undef KeyPress +#undef KeyRelease +#undef FocusIn +#undef FocusOut +#undef FontChange +#undef Expose +#undef Unsorted + +QT_END_NAMESPACE + +#endif diff --git a/src/plugins/platforms/eglfs/api/qeglfshooks.cpp b/src/plugins/platforms/eglfs/api/qeglfshooks.cpp new file mode 100644 index 0000000000..d8332a94cb --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfshooks.cpp @@ -0,0 +1,137 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the qmake spec of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qeglfshooks_p.h" +#include <QLoggingCategory> + +QT_BEGIN_NAMESPACE + +Q_DECLARE_LOGGING_CATEGORY(qLcEglDevDebug) + +#ifdef EGLFS_PLATFORM_HOOKS + +QEglFSDeviceIntegration *qt_egl_device_integration() +{ + extern QEglFSHooks *platformHooks; + return platformHooks; +} + +#else + +class DeviceIntegration +{ +public: + DeviceIntegration(); + ~DeviceIntegration() { delete m_integration; } + QEglFSDeviceIntegration *integration() { return m_integration; } +private: + QEglFSDeviceIntegration *m_integration; +}; + +Q_GLOBAL_STATIC(DeviceIntegration, deviceIntegration) + +DeviceIntegration::DeviceIntegration() + : m_integration(nullptr) +{ + QStringList pluginKeys = QEglFSDeviceIntegrationFactory::keys(); + if (!pluginKeys.isEmpty()) { + // Some built-in logic: Prioritize either X11 or KMS/DRM. + if (qEnvironmentVariableIsSet("DISPLAY")) { + const QString x11key = QStringLiteral("eglfs_x11"); + if (pluginKeys.contains(x11key)) { + pluginKeys.removeOne(x11key); + pluginKeys.prepend(x11key); + } + } else { + const QString kmskey = QStringLiteral("eglfs_kms"); + if (pluginKeys.contains(kmskey)) { + pluginKeys.removeOne(kmskey); + pluginKeys.prepend(kmskey); + } + } + + QByteArray requested; + + // The environment variable can override everything. + if (qEnvironmentVariableIsSet("QT_QPA_EGLFS_INTEGRATION")) { + requested = qgetenv("QT_QPA_EGLFS_INTEGRATION"); + } else { + // Device-specific makespecs may define a preferred plugin. +#ifdef EGLFS_PREFERRED_PLUGIN +#define DEFAULT_PLUGIN EGLFS_PREFERRED_PLUGIN +#define STR(s) #s +#define STRQ(s) STR(s) + requested = STRQ(DEFAULT_PLUGIN); +#endif + } + + // Treat "none" as special. There has to be a way to indicate + // that plugins must be ignored when the device is known to be + // functional with the default, non-specialized integration. + if (requested != QByteArrayLiteral("none")) { + if (!requested.isEmpty()) { + QString reqStr = QString::fromLocal8Bit(requested); + pluginKeys.removeOne(reqStr); + pluginKeys.prepend(reqStr); + } + qCDebug(qLcEglDevDebug) << "EGL device integration plugin keys (sorted):" << pluginKeys; + while (!m_integration && !pluginKeys.isEmpty()) { + QString key = pluginKeys.takeFirst(); + qCDebug(qLcEglDevDebug) << "Trying to load device EGL integration" << key; + m_integration = QEglFSDeviceIntegrationFactory::create(key); + } + } + } + + if (!m_integration) { + // Use a default, non-specialized device integration when no plugin is available. + // For some systems this is sufficient. + qCDebug(qLcEglDevDebug) << "Using base device integration"; + m_integration = new QEglFSDeviceIntegration; + } +} + +QEglFSDeviceIntegration *qt_egl_device_integration() +{ + return deviceIntegration()->integration(); +} + +#endif // EGLFS_PLATFORM_HOOKS + +QT_END_NAMESPACE diff --git a/src/plugins/platforms/eglfs/api/qeglfshooks_p.h b/src/plugins/platforms/eglfs/api/qeglfshooks_p.h new file mode 100644 index 0000000000..4c3149c7c9 --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfshooks_p.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QEGLFSHOOKS_H +#define QEGLFSHOOKS_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qeglfsglobal_p.h" +#include "qeglfsdeviceintegration_p.h" + +QT_BEGIN_NAMESPACE + +class QEglFSHooks : public QEglFSDeviceIntegration +{ +}; + +Q_EGLFS_EXPORT QEglFSDeviceIntegration *qt_egl_device_integration(); + +QT_END_NAMESPACE + +#endif // QEGLFSHOOKS_H diff --git a/src/plugins/platforms/eglfs/api/qeglfsintegration.cpp b/src/plugins/platforms/eglfs/api/qeglfsintegration.cpp new file mode 100644 index 0000000000..733f0bd139 --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfsintegration.cpp @@ -0,0 +1,439 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore/qtextstream.h> +#include <QtGui/private/qguiapplication_p.h> + +#include <qpa/qplatformwindow.h> +#include <QtGui/QSurfaceFormat> +#include <QtGui/QOpenGLContext> +#include <QtGui/QScreen> +#include <QtGui/QOffscreenSurface> +#include <QtGui/QWindow> +#include <QtCore/QLoggingCategory> +#include <qpa/qwindowsysteminterface.h> +#include <qpa/qplatforminputcontextfactory_p.h> + +#include "qeglfsintegration_p.h" +#include "qeglfswindow_p.h" +#include "qeglfshooks_p.h" +#include "qeglfscontext_p.h" +#include "qeglfsoffscreenwindow_p.h" +#include "qeglfscursor_p.h" + +#include <QtEglSupport/private/qeglconvenience_p.h> +#include <QtEglSupport/private/qeglplatformcontext_p.h> +#include <QtEglSupport/private/qeglpbuffer_p.h> + +#include <QtFontDatabaseSupport/private/qgenericunixfontdatabase_p.h> +#include <QtServiceSupport/private/qgenericunixservices_p.h> +#include <QtThemeSupport/private/qgenericunixthemes_p.h> +#include <QtEventDispatcherSupport/private/qgenericunixeventdispatcher_p.h> +#include <QtFbSupport/private/qfbvthandler_p.h> +#include <QtPlatformCompositorSupport/private/qopenglcompositorbackingstore_p.h> + +#include <QtPlatformHeaders/QEGLNativeContext> + +#if QT_CONFIG(libinput) +#include <QtInputSupport/private/qlibinputhandler_p.h> +#endif + +#if QT_CONFIG(evdev) +#include <QtInputSupport/private/qevdevmousemanager_p.h> +#include <QtInputSupport/private/qevdevkeyboardmanager_p.h> +#include <QtInputSupport/private/qevdevtouchmanager_p.h> +#endif + +#if QT_CONFIG(tslib) +#include <QtInputSupport/private/qtslib_p.h> +#endif + +#include <QtPlatformHeaders/qeglfsfunctions.h> + +static void initResources() +{ +#ifndef QT_NO_CURSOR + Q_INIT_RESOURCE(cursor); +#endif +} + +QT_BEGIN_NAMESPACE + +QEglFSIntegration::QEglFSIntegration() + : m_display(EGL_NO_DISPLAY), + m_inputContext(0), + m_fontDb(new QGenericUnixFontDatabase), + m_services(new QGenericUnixServices), + m_kbdMgr(0), + m_disableInputHandlers(false) +{ + m_disableInputHandlers = qEnvironmentVariableIntValue("QT_QPA_EGLFS_DISABLE_INPUT"); + + initResources(); +} + +void QEglFSIntegration::addScreen(QPlatformScreen *screen) +{ + screenAdded(screen); +} + +void QEglFSIntegration::removeScreen(QPlatformScreen *screen) +{ + destroyScreen(screen); +} + +void QEglFSIntegration::initialize() +{ + qt_egl_device_integration()->platformInit(); + + m_display = qt_egl_device_integration()->createDisplay(nativeDisplay()); + if (Q_UNLIKELY(m_display == EGL_NO_DISPLAY)) + qFatal("Could not open egl display"); + + EGLint major, minor; + if (Q_UNLIKELY(!eglInitialize(m_display, &major, &minor))) + qFatal("Could not initialize egl display"); + + m_inputContext = QPlatformInputContextFactory::create(); + + m_vtHandler.reset(new QFbVtHandler); + + if (qt_egl_device_integration()->usesDefaultScreen()) + addScreen(new QEglFSScreen(display())); + else + qt_egl_device_integration()->screenInit(); + + // Input code may rely on the screens, so do it only after the screen init. + if (!m_disableInputHandlers) + createInputHandlers(); +} + +void QEglFSIntegration::destroy() +{ + foreach (QWindow *w, qGuiApp->topLevelWindows()) + w->destroy(); + + qt_egl_device_integration()->screenDestroy(); + + if (m_display != EGL_NO_DISPLAY) + eglTerminate(m_display); + + qt_egl_device_integration()->platformDestroy(); +} + +QAbstractEventDispatcher *QEglFSIntegration::createEventDispatcher() const +{ + return createUnixEventDispatcher(); +} + +QPlatformServices *QEglFSIntegration::services() const +{ + return m_services.data(); +} + +QPlatformFontDatabase *QEglFSIntegration::fontDatabase() const +{ + return m_fontDb.data(); +} + +QPlatformTheme *QEglFSIntegration::createPlatformTheme(const QString &name) const +{ + return QGenericUnixTheme::createUnixTheme(name); +} + +QPlatformBackingStore *QEglFSIntegration::createPlatformBackingStore(QWindow *window) const +{ + QOpenGLCompositorBackingStore *bs = new QOpenGLCompositorBackingStore(window); + if (!window->handle()) + window->create(); + static_cast<QEglFSWindow *>(window->handle())->setBackingStore(bs); + return bs; +} + +QPlatformWindow *QEglFSIntegration::createPlatformWindow(QWindow *window) const +{ + QWindowSystemInterface::flushWindowSystemEvents(); + QEglFSWindow *w = qt_egl_device_integration()->createWindow(window); + w->create(); + if (window->type() != Qt::ToolTip) + w->requestActivateWindow(); + return w; +} + +QPlatformOpenGLContext *QEglFSIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const +{ + EGLDisplay dpy = context->screen() ? static_cast<QEglFSScreen *>(context->screen()->handle())->display() : display(); + QPlatformOpenGLContext *share = context->shareHandle(); + QVariant nativeHandle = context->nativeHandle(); + + QEglFSContext *ctx; + QSurfaceFormat adjustedFormat = qt_egl_device_integration()->surfaceFormatFor(context->format()); + if (nativeHandle.isNull()) { + EGLConfig config = QEglFSDeviceIntegration::chooseConfig(dpy, adjustedFormat); + ctx = new QEglFSContext(adjustedFormat, share, dpy, &config, QVariant()); + } else { + ctx = new QEglFSContext(adjustedFormat, share, dpy, 0, nativeHandle); + } + nativeHandle = QVariant::fromValue<QEGLNativeContext>(QEGLNativeContext(ctx->eglContext(), dpy)); + + context->setNativeHandle(nativeHandle); + return ctx; +} + +QPlatformOffscreenSurface *QEglFSIntegration::createPlatformOffscreenSurface(QOffscreenSurface *surface) const +{ + EGLDisplay dpy = surface->screen() ? static_cast<QEglFSScreen *>(surface->screen()->handle())->display() : display(); + QSurfaceFormat fmt = qt_egl_device_integration()->surfaceFormatFor(surface->requestedFormat()); + if (qt_egl_device_integration()->supportsPBuffers()) { + QEGLPlatformContext::Flags flags = 0; + if (!qt_egl_device_integration()->supportsSurfacelessContexts()) + flags |= QEGLPlatformContext::NoSurfaceless; + return new QEGLPbuffer(dpy, fmt, surface, flags); + } else { + return new QEglFSOffscreenWindow(dpy, fmt, surface); + } + // Never return null. Multiple QWindows are not supported by this plugin. +} + +bool QEglFSIntegration::hasCapability(QPlatformIntegration::Capability cap) const +{ + // We assume that devices will have more and not less capabilities + if (qt_egl_device_integration()->hasCapability(cap)) + return true; + + switch (cap) { + case ThreadedPixmaps: return true; + case OpenGL: return true; + case ThreadedOpenGL: return true; + case WindowManagement: return false; + case RasterGLSurface: return true; + default: return QPlatformIntegration::hasCapability(cap); + } +} + +QPlatformNativeInterface *QEglFSIntegration::nativeInterface() const +{ + return const_cast<QEglFSIntegration *>(this); +} + +enum ResourceType { + EglDisplay, + EglWindow, + EglContext, + EglConfig, + NativeDisplay, + XlibDisplay, + WaylandDisplay +}; + +static int resourceType(const QByteArray &key) +{ + static const QByteArray names[] = { // match ResourceType + QByteArrayLiteral("egldisplay"), + QByteArrayLiteral("eglwindow"), + QByteArrayLiteral("eglcontext"), + QByteArrayLiteral("eglconfig"), + QByteArrayLiteral("nativedisplay"), + QByteArrayLiteral("display"), + QByteArrayLiteral("server_wl_display") + }; + const QByteArray *end = names + sizeof(names) / sizeof(names[0]); + const QByteArray *result = std::find(names, end, key); + if (result == end) + result = std::find(names, end, key.toLower()); + return int(result - names); +} + +void *QEglFSIntegration::nativeResourceForIntegration(const QByteArray &resource) +{ + void *result = 0; + + switch (resourceType(resource)) { + case EglDisplay: + result = display(); + break; + case NativeDisplay: + result = reinterpret_cast<void*>(nativeDisplay()); + break; + case WaylandDisplay: + result = qt_egl_device_integration()->wlDisplay(); + break; + default: + break; + } + + return result; +} + +void *QEglFSIntegration::nativeResourceForScreen(const QByteArray &resource, QScreen *) +{ + void *result = 0; + + switch (resourceType(resource)) { + case XlibDisplay: + // Play nice when using the x11 hooks: Be compatible with xcb that allows querying + // the X Display pointer, which is nothing but our native display. + result = reinterpret_cast<void*>(nativeDisplay()); + break; + default: + break; + } + + return result; +} + +void *QEglFSIntegration::nativeResourceForWindow(const QByteArray &resource, QWindow *window) +{ + void *result = 0; + + switch (resourceType(resource)) { + case EglDisplay: + if (window && window->handle()) + result = static_cast<QEglFSScreen *>(window->handle()->screen())->display(); + else + result = display(); + break; + case EglWindow: + if (window && window->handle()) + result = reinterpret_cast<void*>(static_cast<QEglFSWindow *>(window->handle())->eglWindow()); + break; + default: + break; + } + + return result; +} + +void *QEglFSIntegration::nativeResourceForContext(const QByteArray &resource, QOpenGLContext *context) +{ + void *result = 0; + + switch (resourceType(resource)) { + case EglContext: + if (context->handle()) + result = static_cast<QEglFSContext *>(context->handle())->eglContext(); + break; + case EglConfig: + if (context->handle()) + result = static_cast<QEglFSContext *>(context->handle())->eglConfig(); + break; + case EglDisplay: + if (context->handle()) + result = static_cast<QEglFSContext *>(context->handle())->eglDisplay(); + break; + default: + break; + } + + return result; +} + +static void *eglContextForContext(QOpenGLContext *context) +{ + Q_ASSERT(context); + + QEglFSContext *handle = static_cast<QEglFSContext *>(context->handle()); + if (!handle) + return 0; + + return handle->eglContext(); +} + +QPlatformNativeInterface::NativeResourceForContextFunction QEglFSIntegration::nativeResourceFunctionForContext(const QByteArray &resource) +{ + QByteArray lowerCaseResource = resource.toLower(); + if (lowerCaseResource == "get_egl_context") + return NativeResourceForContextFunction(eglContextForContext); + + return 0; +} + +QFunctionPointer QEglFSIntegration::platformFunction(const QByteArray &function) const +{ +#if QT_CONFIG(evdev) + if (function == QEglFSFunctions::loadKeymapTypeIdentifier()) + return QFunctionPointer(loadKeymapStatic); +#else + Q_UNUSED(function) +#endif + + return 0; +} + +void QEglFSIntegration::loadKeymapStatic(const QString &filename) +{ +#if QT_CONFIG(evdev) + QEglFSIntegration *self = static_cast<QEglFSIntegration *>(QGuiApplicationPrivate::platformIntegration()); + if (self->m_kbdMgr) + self->m_kbdMgr->loadKeymap(filename); + else + qWarning("QEglFSIntegration: Cannot load keymap, no keyboard handler found"); +#else + Q_UNUSED(filename); +#endif +} + +void QEglFSIntegration::createInputHandlers() +{ +#if QT_CONFIG(libinput) + if (!qEnvironmentVariableIntValue("QT_QPA_EGLFS_NO_LIBINPUT")) { + new QLibInputHandler(QLatin1String("libinput"), QString()); + return; + } +#endif + +#if QT_CONFIG(evdev) + m_kbdMgr = new QEvdevKeyboardManager(QLatin1String("EvdevKeyboard"), QString() /* spec */, this); + new QEvdevMouseManager(QLatin1String("EvdevMouse"), QString() /* spec */, this); +#if QT_CONFIG(tslib) + const bool useTslib = qEnvironmentVariableIntValue("QT_QPA_EGLFS_TSLIB"); + if (useTslib) + new QTsLibMouseHandler(QLatin1String("TsLib"), QString() /* spec */); + else +#endif + new QEvdevTouchManager(QLatin1String("EvdevTouch"), QString() /* spec */, this); +#endif +} + +EGLNativeDisplayType QEglFSIntegration::nativeDisplay() const +{ + return qt_egl_device_integration()->platformDisplay(); +} + +QT_END_NAMESPACE diff --git a/src/plugins/platforms/eglfs/api/qeglfsintegration_p.h b/src/plugins/platforms/eglfs/api/qeglfsintegration_p.h new file mode 100644 index 0000000000..1a3a44d441 --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfsintegration_p.h @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QEGLFSINTEGRATION_H +#define QEGLFSINTEGRATION_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qeglfsglobal_p.h" +#include <QtCore/QVariant> +#include <qpa/qplatformintegration.h> +#include <qpa/qplatformnativeinterface.h> +#include <qpa/qplatformscreen.h> + +QT_BEGIN_NAMESPACE + +class QEglFSWindow; +class QEglFSContext; +class QFbVtHandler; +class QEvdevKeyboardManager; + +class Q_EGLFS_EXPORT QEglFSIntegration : public QPlatformIntegration, public QPlatformNativeInterface +{ +public: + QEglFSIntegration(); + + void initialize() Q_DECL_OVERRIDE; + void destroy() Q_DECL_OVERRIDE; + + EGLDisplay display() const { return m_display; } + + QAbstractEventDispatcher *createEventDispatcher() const Q_DECL_OVERRIDE; + QPlatformFontDatabase *fontDatabase() const Q_DECL_OVERRIDE; + QPlatformServices *services() const Q_DECL_OVERRIDE; + QPlatformInputContext *inputContext() const Q_DECL_OVERRIDE { return m_inputContext; } + QPlatformTheme *createPlatformTheme(const QString &name) const Q_DECL_OVERRIDE; + + QPlatformWindow *createPlatformWindow(QWindow *window) const Q_DECL_OVERRIDE; + QPlatformBackingStore *createPlatformBackingStore(QWindow *window) const Q_DECL_OVERRIDE; + QPlatformOpenGLContext *createPlatformOpenGLContext(QOpenGLContext *context) const Q_DECL_OVERRIDE; + QPlatformOffscreenSurface *createPlatformOffscreenSurface(QOffscreenSurface *surface) const Q_DECL_OVERRIDE; + + bool hasCapability(QPlatformIntegration::Capability cap) const Q_DECL_OVERRIDE; + + QPlatformNativeInterface *nativeInterface() const Q_DECL_OVERRIDE; + + // QPlatformNativeInterface + void *nativeResourceForIntegration(const QByteArray &resource) Q_DECL_OVERRIDE; + void *nativeResourceForScreen(const QByteArray &resource, QScreen *screen) Q_DECL_OVERRIDE; + void *nativeResourceForWindow(const QByteArray &resource, QWindow *window) Q_DECL_OVERRIDE; + void *nativeResourceForContext(const QByteArray &resource, QOpenGLContext *context) Q_DECL_OVERRIDE; + NativeResourceForContextFunction nativeResourceFunctionForContext(const QByteArray &resource) Q_DECL_OVERRIDE; + + QFunctionPointer platformFunction(const QByteArray &function) const Q_DECL_OVERRIDE; + + QFbVtHandler *vtHandler() { return m_vtHandler.data(); } + + void addScreen(QPlatformScreen *screen); + void removeScreen(QPlatformScreen *screen); + +private: + EGLNativeDisplayType nativeDisplay() const; + void createInputHandlers(); + static void loadKeymapStatic(const QString &filename); + + EGLDisplay m_display; + QPlatformInputContext *m_inputContext; + QScopedPointer<QPlatformFontDatabase> m_fontDb; + QScopedPointer<QPlatformServices> m_services; + QScopedPointer<QFbVtHandler> m_vtHandler; + QEvdevKeyboardManager *m_kbdMgr; + bool m_disableInputHandlers; +}; + +QT_END_NAMESPACE + +#endif // QEGLFSINTEGRATION_H diff --git a/src/plugins/platforms/eglfs/api/qeglfsoffscreenwindow.cpp b/src/plugins/platforms/eglfs/api/qeglfsoffscreenwindow.cpp new file mode 100644 index 0000000000..864271cd3a --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfsoffscreenwindow.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qeglfsoffscreenwindow_p.h" +#include "qeglfshooks_p.h" +#include <QtGui/QOffscreenSurface> +#include <QtEglSupport/private/qeglconvenience_p.h> + +QT_BEGIN_NAMESPACE + +/* + In some cases pbuffers are not available. Triggering QtGui's built-in + fallback for a hidden QWindow is not suitable for eglfs since this would be + treated as an attempt to create multiple top-level, native windows. + + Therefore this class is provided as an alternative to QEGLPbuffer. + + This class requires the hooks to implement createNativeOffscreenWindow(). +*/ + +QEglFSOffscreenWindow::QEglFSOffscreenWindow(EGLDisplay display, const QSurfaceFormat &format, QOffscreenSurface *offscreenSurface) + : QPlatformOffscreenSurface(offscreenSurface) + , m_format(format) + , m_display(display) + , m_surface(EGL_NO_SURFACE) + , m_window(0) +{ + m_window = qt_egl_device_integration()->createNativeOffscreenWindow(format); + if (!m_window) { + qWarning("QEglFSOffscreenWindow: Failed to create native window"); + return; + } + EGLConfig config = q_configFromGLFormat(m_display, m_format); + m_surface = eglCreateWindowSurface(m_display, config, m_window, 0); + if (m_surface != EGL_NO_SURFACE) + m_format = q_glFormatFromConfig(m_display, config); +} + +QEglFSOffscreenWindow::~QEglFSOffscreenWindow() +{ + if (m_surface != EGL_NO_SURFACE) + eglDestroySurface(m_display, m_surface); + if (m_window) + qt_egl_device_integration()->destroyNativeWindow(m_window); +} + +QT_END_NAMESPACE diff --git a/src/plugins/platforms/eglfs/api/qeglfsoffscreenwindow_p.h b/src/plugins/platforms/eglfs/api/qeglfsoffscreenwindow_p.h new file mode 100644 index 0000000000..ec483c64e2 --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfsoffscreenwindow_p.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QEGLFSOFFSCREENWINDOW_H +#define QEGLFSOFFSCREENWINDOW_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qeglfsglobal_p.h" +#include <qpa/qplatformoffscreensurface.h> + +QT_BEGIN_NAMESPACE + +class Q_EGLFS_EXPORT QEglFSOffscreenWindow : public QPlatformOffscreenSurface +{ +public: + QEglFSOffscreenWindow(EGLDisplay display, const QSurfaceFormat &format, QOffscreenSurface *offscreenSurface); + ~QEglFSOffscreenWindow(); + + QSurfaceFormat format() const Q_DECL_OVERRIDE { return m_format; } + bool isValid() const Q_DECL_OVERRIDE { return m_surface != EGL_NO_SURFACE; } + +private: + QSurfaceFormat m_format; + EGLDisplay m_display; + EGLSurface m_surface; + EGLNativeWindowType m_window; +}; + +QT_END_NAMESPACE + +#endif // QEGLFSOFFSCREENWINDOW_H diff --git a/src/plugins/platforms/eglfs/api/qeglfsscreen.cpp b/src/plugins/platforms/eglfs/api/qeglfsscreen.cpp new file mode 100644 index 0000000000..5613179041 --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfsscreen.cpp @@ -0,0 +1,231 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore/qtextstream.h> +#include <QtGui/qwindow.h> +#include <qpa/qwindowsysteminterface.h> +#include <qpa/qplatformcursor.h> +#include <QtPlatformCompositorSupport/private/qopenglcompositor_p.h> + +#include "qeglfsscreen_p.h" +#include "qeglfswindow_p.h" +#include "qeglfshooks_p.h" + +QT_BEGIN_NAMESPACE + +QEglFSScreen::QEglFSScreen(EGLDisplay dpy) + : m_dpy(dpy), + m_surface(EGL_NO_SURFACE), + m_cursor(0) +{ + m_cursor = qt_egl_device_integration()->createCursor(this); +} + +QEglFSScreen::~QEglFSScreen() +{ + delete m_cursor; + QOpenGLCompositor::destroy(); +} + +QRect QEglFSScreen::geometry() const +{ + QRect r = rawGeometry(); + + static int rotation = qEnvironmentVariableIntValue("QT_QPA_EGLFS_ROTATION"); + switch (rotation) { + case 0: + case 180: + case -180: + break; + case 90: + case -90: { + int h = r.height(); + r.setHeight(r.width()); + r.setWidth(h); + break; + } + default: + qWarning("Invalid rotation %d specified in QT_QPA_EGLFS_ROTATION", rotation); + break; + } + + return r; +} + +QRect QEglFSScreen::rawGeometry() const +{ + return QRect(QPoint(0, 0), qt_egl_device_integration()->screenSize()); +} + +int QEglFSScreen::depth() const +{ + return qt_egl_device_integration()->screenDepth(); +} + +QImage::Format QEglFSScreen::format() const +{ + return qt_egl_device_integration()->screenFormat(); +} + +QSizeF QEglFSScreen::physicalSize() const +{ + return qt_egl_device_integration()->physicalScreenSize(); +} + +QDpi QEglFSScreen::logicalDpi() const +{ + return qt_egl_device_integration()->logicalDpi(); +} + +qreal QEglFSScreen::pixelDensity() const +{ + return qt_egl_device_integration()->pixelDensity(); +} + +Qt::ScreenOrientation QEglFSScreen::nativeOrientation() const +{ + return qt_egl_device_integration()->nativeOrientation(); +} + +Qt::ScreenOrientation QEglFSScreen::orientation() const +{ + return qt_egl_device_integration()->orientation(); +} + +QPlatformCursor *QEglFSScreen::cursor() const +{ + return m_cursor; +} + +qreal QEglFSScreen::refreshRate() const +{ + return qt_egl_device_integration()->refreshRate(); +} + +void QEglFSScreen::setPrimarySurface(EGLSurface surface) +{ + m_surface = surface; +} + +void QEglFSScreen::handleCursorMove(const QPoint &pos) +{ + const QOpenGLCompositor *compositor = QOpenGLCompositor::instance(); + const QList<QOpenGLCompositorWindow *> windows = compositor->windows(); + + // Generate enter and leave events like a real windowing system would do. + if (windows.isEmpty()) + return; + + // First window is always fullscreen. + if (windows.count() == 1) { + QWindow *window = windows[0]->sourceWindow(); + if (m_pointerWindow != window) { + m_pointerWindow = window; + QWindowSystemInterface::handleEnterEvent(window, window->mapFromGlobal(pos), pos); + } + return; + } + + QWindow *enter = 0, *leave = 0; + for (int i = windows.count() - 1; i >= 0; --i) { + QWindow *window = windows[i]->sourceWindow(); + const QRect geom = window->geometry(); + if (geom.contains(pos)) { + if (m_pointerWindow != window) { + leave = m_pointerWindow; + m_pointerWindow = window; + enter = window; + } + break; + } + } + + if (enter && leave) + QWindowSystemInterface::handleEnterLeaveEvent(enter, leave, enter->mapFromGlobal(pos), pos); +} + +QPixmap QEglFSScreen::grabWindow(WId wid, int x, int y, int width, int height) const +{ + QOpenGLCompositor *compositor = QOpenGLCompositor::instance(); + const QList<QOpenGLCompositorWindow *> windows = compositor->windows(); + Q_ASSERT(!windows.isEmpty()); + + QImage img; + + if (static_cast<QEglFSWindow *>(windows.first()->sourceWindow()->handle())->isRaster()) { + // Request the compositor to render everything into an FBO and read it back. This + // is of course slow, but it's safe and reliable. It will not include the mouse + // cursor, which is a plus. + img = compositor->grab(); + } else { + // Just a single OpenGL window without compositing. Do not support this case for now. Doing + // glReadPixels is not an option since it would read from the back buffer which may have + // undefined content when calling right after a swapBuffers (unless preserved swap is + // available and enabled, but we have no support for that). + qWarning("grabWindow: Not supported for non-composited OpenGL content. Use QQuickWindow::grabWindow() instead."); + return QPixmap(); + } + + if (!wid) { + const QSize screenSize = geometry().size(); + if (width < 0) + width = screenSize.width() - x; + if (height < 0) + height = screenSize.height() - y; + return QPixmap::fromImage(img).copy(x, y, width, height); + } + + foreach (QOpenGLCompositorWindow *w, windows) { + const QWindow *window = w->sourceWindow(); + if (window->winId() == wid) { + const QRect geom = window->geometry(); + if (width < 0) + width = geom.width() - x; + if (height < 0) + height = geom.height() - y; + QRect rect(geom.topLeft() + QPoint(x, y), QSize(width, height)); + rect &= window->geometry(); + return QPixmap::fromImage(img).copy(rect); + } + } + + return QPixmap(); +} + +QT_END_NAMESPACE diff --git a/src/plugins/platforms/eglfs/api/qeglfsscreen_p.h b/src/plugins/platforms/eglfs/api/qeglfsscreen_p.h new file mode 100644 index 0000000000..131e619e06 --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfsscreen_p.h @@ -0,0 +1,106 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QEGLFSSCREEN_H +#define QEGLFSSCREEN_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qeglfsglobal_p.h" +#include <QtCore/QPointer> + +#include <qpa/qplatformscreen.h> + +QT_BEGIN_NAMESPACE + +class QEglFSWindow; +class QOpenGLContext; + +class Q_EGLFS_EXPORT QEglFSScreen : public QPlatformScreen +{ +public: + QEglFSScreen(EGLDisplay display); + ~QEglFSScreen(); + + QRect geometry() const Q_DECL_OVERRIDE; + virtual QRect rawGeometry() const; + int depth() const Q_DECL_OVERRIDE; + QImage::Format format() const Q_DECL_OVERRIDE; + + QSizeF physicalSize() const Q_DECL_OVERRIDE; + QDpi logicalDpi() const Q_DECL_OVERRIDE; + qreal pixelDensity() const Q_DECL_OVERRIDE; + Qt::ScreenOrientation nativeOrientation() const Q_DECL_OVERRIDE; + Qt::ScreenOrientation orientation() const Q_DECL_OVERRIDE; + + QPlatformCursor *cursor() const Q_DECL_OVERRIDE; + + qreal refreshRate() const Q_DECL_OVERRIDE; + + QPixmap grabWindow(WId wid, int x, int y, int width, int height) const Q_DECL_OVERRIDE; + + EGLSurface primarySurface() const { return m_surface; } + + EGLDisplay display() const { return m_dpy; } + + void handleCursorMove(const QPoint &pos); + +private: + void setPrimarySurface(EGLSurface surface); + + EGLDisplay m_dpy; + QPointer<QWindow> m_pointerWindow; + EGLSurface m_surface; + QPlatformCursor *m_cursor; + + friend class QEglFSWindow; +}; + +QT_END_NAMESPACE + +#endif // QEGLFSSCREEN_H diff --git a/src/plugins/platforms/eglfs/api/qeglfswindow.cpp b/src/plugins/platforms/eglfs/api/qeglfswindow.cpp new file mode 100644 index 0000000000..e79b377d40 --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfswindow.cpp @@ -0,0 +1,338 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore/qtextstream.h> +#include <qpa/qwindowsysteminterface.h> +#include <qpa/qplatformintegration.h> +#include <private/qguiapplication_p.h> +#include <QtGui/private/qopenglcontext_p.h> +#include <QtGui/QOpenGLContext> +#include <QtEglSupport/private/qeglconvenience_p.h> +#include <QtPlatformCompositorSupport/private/qopenglcompositorbackingstore_p.h> + +#include "qeglfswindow_p.h" +#include "qeglfscursor_p.h" +#include "qeglfshooks_p.h" +#include "qeglfsdeviceintegration_p.h" + +QT_BEGIN_NAMESPACE + +QEglFSWindow::QEglFSWindow(QWindow *w) + : QPlatformWindow(w), + m_backingStore(0), + m_raster(false), + m_winId(0), + m_surface(EGL_NO_SURFACE), + m_window(0), + m_flags(0) +{ +} + +QEglFSWindow::~QEglFSWindow() +{ + destroy(); +} + +static WId newWId() +{ + static WId id = 0; + + if (id == std::numeric_limits<WId>::max()) + qWarning("QEGLPlatformWindow: Out of window IDs"); + + return ++id; +} + +void QEglFSWindow::create() +{ + if (m_flags.testFlag(Created)) + return; + + m_winId = newWId(); + + // Save the original surface type before changing to OpenGLSurface. + m_raster = (window()->surfaceType() == QSurface::RasterSurface); + if (m_raster) // change to OpenGL, but not for RasterGLSurface + window()->setSurfaceType(QSurface::OpenGLSurface); + + if (window()->type() == Qt::Desktop) { + QRect fullscreenRect(QPoint(), screen()->availableGeometry().size()); + QPlatformWindow::setGeometry(fullscreenRect); + QWindowSystemInterface::handleGeometryChange(window(), fullscreenRect); + return; + } + + m_flags = Created; + + if (window()->type() == Qt::Desktop) + return; + + // Stop if there is already a window backed by a native window and surface. Additional + // raster windows will not have their own native window, surface and context. Instead, + // they will be composited onto the root window's surface. + QEglFSScreen *screen = this->screen(); + QOpenGLCompositor *compositor = QOpenGLCompositor::instance(); + if (screen->primarySurface() != EGL_NO_SURFACE) { + if (Q_UNLIKELY(!isRaster() || !compositor->targetWindow())) { +#if !defined(Q_OS_ANDROID) + // We can have either a single OpenGL window or multiple raster windows. + // Other combinations cannot work. + qFatal("EGLFS: OpenGL windows cannot be mixed with others."); +#endif + return; + } + m_format = compositor->targetWindow()->format(); + return; + } + + m_flags |= HasNativeWindow; + setGeometry(QRect()); // will become fullscreen + QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(0, 0), geometry().size())); + + resetSurface(); + + if (Q_UNLIKELY(m_surface == EGL_NO_SURFACE)) { + EGLint error = eglGetError(); + eglTerminate(screen->display()); + qFatal("EGL Error : Could not create the egl surface: error = 0x%x\n", error); + } + + screen->setPrimarySurface(m_surface); + + if (isRaster()) { + QOpenGLContext *context = new QOpenGLContext(QGuiApplication::instance()); + context->setShareContext(qt_gl_global_share_context()); + context->setFormat(m_format); + context->setScreen(window()->screen()); + if (Q_UNLIKELY(!context->create())) + qFatal("EGLFS: Failed to create compositing context"); + compositor->setTarget(context, window(), screen->rawGeometry()); + compositor->setRotation(qEnvironmentVariableIntValue("QT_QPA_EGLFS_ROTATION")); + // If there is a "root" window into which raster and QOpenGLWidget content is + // composited, all other contexts must share with its context. + if (!qt_gl_global_share_context()) { + qt_gl_set_global_share_context(context); + // What we set up here is in effect equivalent to the application setting + // AA_ShareOpenGLContexts. Set the attribute to be fully consistent. + QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts); + } + } +} + +void QEglFSWindow::destroy() +{ + QEglFSScreen *screen = this->screen(); + if (m_flags.testFlag(HasNativeWindow)) { + QEglFSCursor *cursor = qobject_cast<QEglFSCursor *>(screen->cursor()); + if (cursor) + cursor->resetResources(); + + if (screen->primarySurface() == m_surface) + screen->setPrimarySurface(EGL_NO_SURFACE); + + invalidateSurface(); + } + + m_flags = 0; + QOpenGLCompositor::instance()->removeWindow(this); +} + +void QEglFSWindow::invalidateSurface() +{ + if (m_surface != EGL_NO_SURFACE) { + eglDestroySurface(screen()->display(), m_surface); + m_surface = EGL_NO_SURFACE; + } + qt_egl_device_integration()->destroyNativeWindow(m_window); + m_window = 0; +} + +void QEglFSWindow::resetSurface() +{ + EGLDisplay display = screen()->display(); + QSurfaceFormat platformFormat = qt_egl_device_integration()->surfaceFormatFor(window()->requestedFormat()); + + m_config = QEglFSDeviceIntegration::chooseConfig(display, platformFormat); + m_format = q_glFormatFromConfig(display, m_config, platformFormat); + const QSize surfaceSize = screen()->rawGeometry().size(); + m_window = qt_egl_device_integration()->createNativeWindow(this, surfaceSize, m_format); + m_surface = eglCreateWindowSurface(display, m_config, m_window, NULL); +} + +void QEglFSWindow::setVisible(bool visible) +{ + QOpenGLCompositor *compositor = QOpenGLCompositor::instance(); + QList<QOpenGLCompositorWindow *> windows = compositor->windows(); + QWindow *wnd = window(); + + if (wnd->type() != Qt::Desktop) { + if (visible) { + compositor->addWindow(this); + } else { + compositor->removeWindow(this); + windows = compositor->windows(); + if (windows.size()) + windows.last()->sourceWindow()->requestActivate(); + } + } + + QWindowSystemInterface::handleExposeEvent(wnd, QRect(QPoint(0, 0), wnd->geometry().size())); + + if (visible) + QWindowSystemInterface::flushWindowSystemEvents(); +} + +void QEglFSWindow::setGeometry(const QRect &r) +{ + QRect rect; + bool forceFullscreen = m_flags.testFlag(HasNativeWindow); + if (forceFullscreen) + rect = screen()->availableGeometry(); + else + rect = r; + + QPlatformWindow::setGeometry(rect); + + // if we corrected the size, trigger a resize event + if (rect != r) + QWindowSystemInterface::handleGeometryChange(window(), rect, r); +} + +QRect QEglFSWindow::geometry() const +{ + // For yet-to-become-fullscreen windows report the geometry covering the entire + // screen. This is particularly important for Quick where the root object may get + // sized to some geometry queried before calling create(). + if (!m_flags.testFlag(Created) && screen()->primarySurface() == EGL_NO_SURFACE) + return screen()->availableGeometry(); + + return QPlatformWindow::geometry(); +} + +void QEglFSWindow::requestActivateWindow() +{ + if (window()->type() != Qt::Desktop) + QOpenGLCompositor::instance()->moveToTop(this); + + QWindow *wnd = window(); + QWindowSystemInterface::handleWindowActivated(wnd); + QWindowSystemInterface::handleExposeEvent(wnd, QRect(QPoint(0, 0), wnd->geometry().size())); +} + +void QEglFSWindow::raise() +{ + QWindow *wnd = window(); + if (wnd->type() != Qt::Desktop) { + QOpenGLCompositor::instance()->moveToTop(this); + QWindowSystemInterface::handleExposeEvent(wnd, QRect(QPoint(0, 0), wnd->geometry().size())); + } +} + +void QEglFSWindow::lower() +{ + QOpenGLCompositor *compositor = QOpenGLCompositor::instance(); + QList<QOpenGLCompositorWindow *> windows = compositor->windows(); + if (window()->type() != Qt::Desktop && windows.count() > 1) { + int idx = windows.indexOf(this); + if (idx > 0) { + compositor->changeWindowIndex(this, idx - 1); + QWindowSystemInterface::handleExposeEvent(windows.last()->sourceWindow(), + QRect(QPoint(0, 0), windows.last()->sourceWindow()->geometry().size())); + } + } +} + +EGLSurface QEglFSWindow::surface() const +{ + return m_surface != EGL_NO_SURFACE ? m_surface : screen()->primarySurface(); +} + +QSurfaceFormat QEglFSWindow::format() const +{ + return m_format; +} + +EGLNativeWindowType QEglFSWindow::eglWindow() const +{ + return m_window; +} + +QEglFSScreen *QEglFSWindow::screen() const +{ + return static_cast<QEglFSScreen *>(QPlatformWindow::screen()); +} + +bool QEglFSWindow::isRaster() const +{ + return m_raster || window()->surfaceType() == QSurface::RasterGLSurface; +} + +QWindow *QEglFSWindow::sourceWindow() const +{ + return window(); +} + +const QPlatformTextureList *QEglFSWindow::textures() const +{ + if (m_backingStore) + return m_backingStore->textures(); + + return 0; +} + +void QEglFSWindow::endCompositing() +{ + if (m_backingStore) + m_backingStore->notifyComposited(); +} + +WId QEglFSWindow::winId() const +{ + return m_winId; +} + +void QEglFSWindow::setOpacity(qreal) +{ + if (!isRaster()) + qWarning("QEglFSWindow: Cannot set opacity for non-raster windows"); + + // Nothing to do here. The opacity is stored in the QWindow. +} + +QT_END_NAMESPACE diff --git a/src/plugins/platforms/eglfs/api/qeglfswindow_p.h b/src/plugins/platforms/eglfs/api/qeglfswindow_p.h new file mode 100644 index 0000000000..0889f27ae3 --- /dev/null +++ b/src/plugins/platforms/eglfs/api/qeglfswindow_p.h @@ -0,0 +1,129 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QEGLFSWINDOW_H +#define QEGLFSWINDOW_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qeglfsglobal_p.h" +#include "qeglfsintegration_p.h" +#include "qeglfsscreen_p.h" + +#include <qpa/qplatformwindow.h> +#include <QtPlatformCompositorSupport/private/qopenglcompositor_p.h> + +QT_BEGIN_NAMESPACE + +class QOpenGLCompositorBackingStore; +class QPlatformTextureList; + +class Q_EGLFS_EXPORT QEglFSWindow : public QPlatformWindow, public QOpenGLCompositorWindow +{ +public: + QEglFSWindow(QWindow *w); + ~QEglFSWindow(); + + void create(); + void destroy(); + + void setGeometry(const QRect &) Q_DECL_OVERRIDE; + QRect geometry() const Q_DECL_OVERRIDE; + void setVisible(bool visible) Q_DECL_OVERRIDE; + void requestActivateWindow() Q_DECL_OVERRIDE; + void raise() Q_DECL_OVERRIDE; + void lower() Q_DECL_OVERRIDE; + + void propagateSizeHints() Q_DECL_OVERRIDE { } + void setMask(const QRegion &) Q_DECL_OVERRIDE { } + bool setKeyboardGrabEnabled(bool) Q_DECL_OVERRIDE { return false; } + bool setMouseGrabEnabled(bool) Q_DECL_OVERRIDE { return false; } + void setOpacity(qreal) Q_DECL_OVERRIDE; + WId winId() const Q_DECL_OVERRIDE; + + QSurfaceFormat format() const Q_DECL_OVERRIDE; + + EGLNativeWindowType eglWindow() const; + EGLSurface surface() const; + QEglFSScreen *screen() const; + + bool hasNativeWindow() const { return m_flags.testFlag(HasNativeWindow); } + + virtual void invalidateSurface() Q_DECL_OVERRIDE; + virtual void resetSurface(); + + QOpenGLCompositorBackingStore *backingStore() { return m_backingStore; } + void setBackingStore(QOpenGLCompositorBackingStore *backingStore) { m_backingStore = backingStore; } + bool isRaster() const; + + QWindow *sourceWindow() const Q_DECL_OVERRIDE; + const QPlatformTextureList *textures() const Q_DECL_OVERRIDE; + void endCompositing() Q_DECL_OVERRIDE; + +protected: + QOpenGLCompositorBackingStore *m_backingStore; + bool m_raster; + WId m_winId; + + EGLSurface m_surface; + EGLNativeWindowType m_window; + + EGLConfig m_config; + QSurfaceFormat m_format; + + enum Flag { + Created = 0x01, + HasNativeWindow = 0x02 + }; + Q_DECLARE_FLAGS(Flags, Flag) + Flags m_flags; +}; + +QT_END_NAMESPACE + +#endif // QEGLFSWINDOW_H |