Move WaylandSurfaceItem into common API.
authorSamuel Rødal <samuel.rodal@nokia.com>
Fri, 1 Apr 2011 11:28:39 +0000 (13:28 +0200)
committerSamuel Rødal <samuel.rodal@nokia.com>
Fri, 1 Apr 2011 11:28:39 +0000 (13:28 +0200)
examples/qml-compositor/main.cpp
examples/qml-compositor/qml-compositor.pro
examples/qwidget-compositor/qt-compositor.pro
src/qt-compositor/compositor_api/compositor_api.pri
src/qt-compositor/compositor_api/waylandsurface.h
src/qt-compositor/compositor_api/waylandsurfaceitem.cpp [new file with mode: 0644]
src/qt-compositor/compositor_api/waylandsurfaceitem.h [new file with mode: 0644]

index 519786d..6b7985a 100644 (file)
 ****************************************************************************/
 
 #include "waylandcompositor.h"
-
 #include "waylandsurface.h"
+#include "waylandsurfaceitem.h"
 
 #include <QApplication>
 #include <QTimer>
 #include <QPainter>
 #include <QMouseEvent>
 
+#include <QDeclarativeContext>
 #include <QDeclarativeView>
 
-#ifdef QT_COMPOSITOR_WAYLAND_GL
-#include <QGLContext>
-#include <QGLWidget>
-#endif
-
 #include <QSGItem>
 #include <QSGView>
 
-#include <private/qsgadaptationlayer_p.h>
-#include <private/qsgcontext_p.h>
-#include <private/qsgitem_p.h>
-#include <private/qsgtexture_p.h>
-#include <private/qsgrectangle_p.h>
-#include <private/qsgtextureprovider_p.h>
-
-class WaylandSurfaceTextureProvider : public QSGTextureProvider
-{
-    Q_OBJECT
-public:
-    WaylandSurfaceTextureProvider(WaylandSurface *surface);
-    ~WaylandSurfaceTextureProvider();
-
-    QSGTextureRef texture() {
-        return m_textureRef;
-    }
-
-private slots:
-    void surfaceDamaged(const QRect &rect);
-
-private:
-    WaylandSurface *m_surface;
-
-    QSGPlainTexture *m_texture;
-    QSGTextureRef m_textureRef;
-};
-
-WaylandSurfaceTextureProvider::WaylandSurfaceTextureProvider(WaylandSurface *surface)
-    : m_surface(surface)
-    , m_texture(new QSGPlainTexture)
-    , m_textureRef(m_texture)
-{
-    connect(surface, SIGNAL(damaged(const QRect &)), this, SLOT(surfaceDamaged(const QRect &)));
-}
-
-WaylandSurfaceTextureProvider::~WaylandSurfaceTextureProvider()
-{
-}
-
-void WaylandSurfaceTextureProvider::surfaceDamaged(const QRect &)
-{
-    if (m_surface->type() == WaylandSurface::Texture) {
-        m_texture->setTextureId(m_surface->texture());
-        m_texture->setHasAlphaChannel(false);
-        m_texture->setTextureSize(m_surface->geometry().size());
-    } else {
-        m_texture->setImage(m_surface->image());
-    }
-
-    m_texture->setOwnsTexture(true);
-    m_texture->setHasMipmaps(false);
-
-    emit textureChanged();
-}
-
-class WindowItem : public QSGItem, public QSGTextureProviderInterface
-{
-    Q_OBJECT
-    Q_INTERFACES(QSGTextureProviderInterface)
-public:
-    WindowItem(WaylandSurface *surface, QSGItem *parent = 0);
-    ~WindowItem();
-
-    QSGTextureProvider *textureProvider() const
-    {
-        return m_textureProvider;
-    }
-
-    void mousePressEvent(QGraphicsSceneMouseEvent *event);
-    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
-    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
-
-    void keyPressEvent(QKeyEvent *event);
-    void keyReleaseEvent(QKeyEvent *event);
-
-public slots:
-    void takeFocus();
-
-private slots:
-    void surfaceMapped(const QRect &rect);
-
-protected:
-    QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *);
-
-private:
-    QPoint toSurface(const QPointF &pos) const;
-
-    WaylandSurface *m_surface;
-    WaylandSurfaceTextureProvider *m_textureProvider;
-};
-
-WindowItem::WindowItem(WaylandSurface *surface, QSGItem *parent)
-    : QSGItem(parent)
-    , m_surface(surface)
-    , m_textureProvider(new WaylandSurfaceTextureProvider(surface))
-{
-    setWidth(surface->geometry().width());
-    setHeight(surface->geometry().height());
-
-    setSmooth(true);
-    setFlag(ItemHasContents);
-    setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
-    connect(surface, SIGNAL(mapped(const QRect &)), this, SLOT(surfaceMapped(const QRect &)));
-    connect(m_textureProvider, SIGNAL(textureChanged()), this, SLOT(update()));
-}
-
-WindowItem::~WindowItem()
-{
-    delete m_textureProvider;
-}
-
-void WindowItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
-{
-    if (hasFocus())
-        m_surface->sendMousePressEvent(toSurface(event->pos()), event->button());
-}
-
-void WindowItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
-{
-    if (hasFocus())
-        m_surface->sendMouseMoveEvent(toSurface(event->pos()));
-}
-
-void WindowItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
-{
-    if (hasFocus())
-        m_surface->sendMouseReleaseEvent(toSurface(event->pos()), event->button());
-}
-
-void WindowItem::keyPressEvent(QKeyEvent *event)
-{
-    if (hasFocus())
-        m_surface->sendKeyPressEvent(event->nativeScanCode());
-}
-
-void WindowItem::keyReleaseEvent(QKeyEvent *event)
-{
-    if (hasFocus())
-        m_surface->sendKeyReleaseEvent(event->nativeScanCode());
-}
-
-void WindowItem::takeFocus()
-{
-    setFocus(true);
-    m_surface->setInputFocus();
-}
-
-QPoint WindowItem::toSurface(const QPointF &pos) const
-{
-    return pos.toPoint();
-}
-
-void WindowItem::surfaceMapped(const QRect &rect)
-{
-    setWidth(rect.width());
-    setHeight(rect.height());
-
-    update();
-}
-
-QSGNode *WindowItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
-{
-    QSGImageNode *node = static_cast<QSGImageNode *>(oldNode);
-    if (!node) {
-        node = QSGContext::current->createImageNode();
-        node->setFlag(QSGNode::UsePreprocess, false);
-        node->setTexture(m_textureProvider);
-    }
-
-    m_textureProvider->setHorizontalWrapMode(QSGTextureProvider::ClampToEdge);
-    m_textureProvider->setVerticalWrapMode(QSGTextureProvider::ClampToEdge);
-    m_textureProvider->setFiltering(QSGItemPrivate::get(this)->smooth
-                                    ? QSGTextureProvider::Linear : QSGTextureProvider::Nearest);
-
-    node->setTargetRect(QRectF(0, 0, width(), height()));
-    node->setSourceRect(QRectF(0, 0, 1, 1));
-    node->update();
-
-    return node;
-}
-
-
 class QmlCompositor : public QSGView, public WaylandCompositor
 {
     Q_OBJECT
@@ -268,7 +81,7 @@ private slots:
         surface->setGeometry(rect);
 
         if (!m_windowMap.contains(surface)) {
-            WindowItem *item = new WindowItem(surface, rootObject());
+            WaylandSurfaceItem *item = new WaylandSurfaceItem(surface, rootObject());
             connect(surface, SIGNAL(destroyed(QObject *)), this, SLOT(surfaceDestroyed(QObject *)));
             emit windowAdded(QVariant::fromValue(static_cast<QSGItem *>(item)));
             m_windowMap[surface] = item;
@@ -278,7 +91,7 @@ private slots:
     }
 
     void surfaceDestroyed(QObject *object) {
-        WindowItem *item = m_windowMap.take(object);
+        WaylandSurfaceItem *item = m_windowMap.take(object);
         emit windowDestroyed(QVariant::fromValue(static_cast<QSGItem *>(item)));
     }
 
@@ -294,7 +107,7 @@ protected:
     }
 
 private:
-    QMap<QObject *, WindowItem *> m_windowMap;
+    QMap<QObject *, WaylandSurfaceItem *> m_windowMap;
 };
 
 int main(int argc, char *argv[])
index 9c6af04..050a247 100644 (file)
@@ -8,8 +8,8 @@ DEPENDPATH += .
 INCLUDEPATH += .
 
 # comment out the following CONFIG lines to disable DRM
-CONFIG += wayland_gl
-CONFIG += mesa_egl
+#CONFIG += wayland_gl
+#CONFIG += mesa_egl
 #CONFIG += dri2_xcb
 
 # comment out the following to not use pkg-config in the pri files
@@ -19,11 +19,11 @@ DESTDIR=$$PWD/../../bin/
 
 LIBS += -L ../../lib
 
-include (../../src/qt-compositor/qt-compositor.pri)
-
 QT += declarative
 QT += opengl
 
+include (../../src/qt-compositor/qt-compositor.pri)
+
 # Input
 SOURCES += main.cpp
 
index 007b2e2..7add9c1 100644 (file)
@@ -1,5 +1,5 @@
 TEMPLATE = app
-TARGET = qt-compositor
+TARGET = qwidget-compositor
 DEPENDPATH += .
 INCLUDEPATH += .
 
@@ -13,6 +13,8 @@ CONFIG += use_pkgconfig
 
 DESTDIR=$$PWD/../../bin/
 
+QT += opengl
+
 include (../../src/qt-compositor/qt-compositor.pri)
 
 # Input
index 7537036..c48bf39 100644 (file)
@@ -7,3 +7,9 @@ HEADERS += \
 SOURCES += \
     $$PWD/waylandcompositor.cpp \
     $$PWD/waylandsurface.cpp
+
+contains(QT, declarative) {
+    echo("Got here!");
+    SOURCES += $$PWD/waylandsurfaceitem.cpp
+    HEADERS += $$PWD/waylandsurfaceitem.h
+}
index e269698..e1ccca5 100644 (file)
@@ -1,3 +1,43 @@
+/****************************************************************************
+**
+** This file is part of QtCompositor**
+**
+** Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+**
+** Contact:  Nokia Corporation qt-info@nokia.com
+**
+** You may use this file under the terms of the BSD license as follows:
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+**
+** Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+**
+** Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in the
+** documentation and/or other materials provided with the distribution.
+**
+** Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the
+** names of its contributors may be used to endorse or promote products
+** derived from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+**
+****************************************************************************/
+
 #ifndef WAYLANDSURFACE_H
 #define WAYLANDSURFACE_H
 
diff --git a/src/qt-compositor/compositor_api/waylandsurfaceitem.cpp b/src/qt-compositor/compositor_api/waylandsurfaceitem.cpp
new file mode 100644 (file)
index 0000000..abc9ea3
--- /dev/null
@@ -0,0 +1,198 @@
+/****************************************************************************
+**
+** This file is part of QtCompositor**
+**
+** Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+**
+** Contact:  Nokia Corporation qt-info@nokia.com
+**
+** You may use this file under the terms of the BSD license as follows:
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+**
+** Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+**
+** Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in the
+** documentation and/or other materials provided with the distribution.
+**
+** Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the
+** names of its contributors may be used to endorse or promote products
+** derived from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+**
+****************************************************************************/
+
+#include "waylandsurfaceitem.h"
+#include "waylandsurface.h"
+
+#include <private/qsgadaptationlayer_p.h>
+#include <private/qsgcontext_p.h>
+#include <private/qsgitem_p.h>
+#include <private/qsgtexture_p.h>
+#include <private/qsgrectangle_p.h>
+#include <private/qsgtextureprovider_p.h>
+
+#include <QKeyEvent>
+
+class WaylandSurfaceTextureProvider : public QSGTextureProvider
+{
+    Q_OBJECT
+public:
+    WaylandSurfaceTextureProvider(WaylandSurface *surface);
+    ~WaylandSurfaceTextureProvider();
+
+    QSGTextureRef texture() {
+        return m_textureRef;
+    }
+
+private slots:
+    void surfaceDamaged(const QRect &rect);
+
+private:
+    WaylandSurface *m_surface;
+
+    QSGPlainTexture *m_texture;
+    QSGTextureRef m_textureRef;
+};
+
+WaylandSurfaceTextureProvider::WaylandSurfaceTextureProvider(WaylandSurface *surface)
+    : m_surface(surface)
+    , m_texture(new QSGPlainTexture)
+    , m_textureRef(m_texture)
+{
+    connect(surface, SIGNAL(damaged(const QRect &)), this, SLOT(surfaceDamaged(const QRect &)));
+}
+
+WaylandSurfaceTextureProvider::~WaylandSurfaceTextureProvider()
+{
+}
+
+void WaylandSurfaceTextureProvider::surfaceDamaged(const QRect &)
+{
+    if (m_surface->type() == WaylandSurface::Texture) {
+        m_texture->setTextureId(m_surface->texture());
+        m_texture->setHasAlphaChannel(false);
+        m_texture->setTextureSize(m_surface->geometry().size());
+    } else {
+        m_texture->setImage(m_surface->image());
+    }
+
+    m_texture->setOwnsTexture(true);
+    m_texture->setHasMipmaps(false);
+
+    emit textureChanged();
+}
+
+WaylandSurfaceItem::WaylandSurfaceItem(WaylandSurface *surface, QSGItem *parent)
+    : QSGItem(parent)
+    , m_surface(surface)
+    , m_textureProvider(new WaylandSurfaceTextureProvider(surface))
+{
+    setWidth(surface->geometry().width());
+    setHeight(surface->geometry().height());
+
+    setSmooth(true);
+    setFlag(ItemHasContents);
+    setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
+    connect(surface, SIGNAL(mapped(const QRect &)), this, SLOT(surfaceMapped(const QRect &)));
+    connect(m_textureProvider, SIGNAL(textureChanged()), this, SLOT(update()));
+}
+
+WaylandSurfaceItem::~WaylandSurfaceItem()
+{
+    delete m_textureProvider;
+}
+
+
+QSGTextureProvider *WaylandSurfaceItem::textureProvider() const
+{
+    return m_textureProvider;
+}
+
+void WaylandSurfaceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
+{
+    if (hasFocus())
+        m_surface->sendMousePressEvent(toSurface(event->pos()), event->button());
+}
+
+void WaylandSurfaceItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
+{
+    if (hasFocus())
+        m_surface->sendMouseMoveEvent(toSurface(event->pos()));
+}
+
+void WaylandSurfaceItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
+{
+    if (hasFocus())
+        m_surface->sendMouseReleaseEvent(toSurface(event->pos()), event->button());
+}
+
+void WaylandSurfaceItem::keyPressEvent(QKeyEvent *event)
+{
+    if (hasFocus())
+        m_surface->sendKeyPressEvent(event->nativeScanCode());
+}
+
+void WaylandSurfaceItem::keyReleaseEvent(QKeyEvent *event)
+{
+    if (hasFocus())
+        m_surface->sendKeyReleaseEvent(event->nativeScanCode());
+}
+
+void WaylandSurfaceItem::takeFocus()
+{
+    setFocus(true);
+    m_surface->setInputFocus();
+}
+
+QPoint WaylandSurfaceItem::toSurface(const QPointF &pos) const
+{
+    return pos.toPoint();
+}
+
+void WaylandSurfaceItem::surfaceMapped(const QRect &rect)
+{
+    setWidth(rect.width());
+    setHeight(rect.height());
+
+    update();
+}
+
+QSGNode *WaylandSurfaceItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
+{
+    QSGImageNode *node = static_cast<QSGImageNode *>(oldNode);
+    if (!node) {
+        node = QSGContext::current->createImageNode();
+        node->setFlag(QSGNode::UsePreprocess, false);
+        node->setTexture(m_textureProvider);
+    }
+
+    m_textureProvider->setHorizontalWrapMode(QSGTextureProvider::ClampToEdge);
+    m_textureProvider->setVerticalWrapMode(QSGTextureProvider::ClampToEdge);
+    m_textureProvider->setFiltering(QSGItemPrivate::get(this)->smooth
+                                    ? QSGTextureProvider::Linear : QSGTextureProvider::Nearest);
+
+    node->setTargetRect(QRectF(0, 0, width(), height()));
+    node->setSourceRect(QRectF(0, 0, 1, 1));
+    node->update();
+
+    return node;
+}
+
+#include "waylandsurfaceitem.moc"
diff --git a/src/qt-compositor/compositor_api/waylandsurfaceitem.h b/src/qt-compositor/compositor_api/waylandsurfaceitem.h
new file mode 100644 (file)
index 0000000..70e0985
--- /dev/null
@@ -0,0 +1,83 @@
+/****************************************************************************
+**
+** This file is part of QtCompositor**
+**
+** Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+**
+** Contact:  Nokia Corporation qt-info@nokia.com
+**
+** You may use this file under the terms of the BSD license as follows:
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+**
+** Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+**
+** Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in the
+** documentation and/or other materials provided with the distribution.
+**
+** Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the
+** names of its contributors may be used to endorse or promote products
+** derived from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+**
+****************************************************************************/
+
+#ifndef WAYLANDSURFACEITEM_H
+#define WAYLANDSURFACEITEM_H
+
+#include <QSGItem>
+#include <private/qsgtextureprovider_p.h>
+
+class WaylandSurface;
+class WaylandSurfaceTextureProvider;
+
+class WaylandSurfaceItem : public QSGItem, public QSGTextureProviderInterface
+{
+    Q_OBJECT
+    Q_INTERFACES(QSGTextureProviderInterface)
+public:
+    WaylandSurfaceItem(WaylandSurface *surface, QSGItem *parent = 0);
+    ~WaylandSurfaceItem();
+
+    QSGTextureProvider *textureProvider() const;
+
+    void mousePressEvent(QGraphicsSceneMouseEvent *event);
+    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
+    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
+
+    void keyPressEvent(QKeyEvent *event);
+    void keyReleaseEvent(QKeyEvent *event);
+
+public slots:
+    void takeFocus();
+
+private slots:
+    void surfaceMapped(const QRect &rect);
+
+protected:
+    QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *);
+
+private:
+    QPoint toSurface(const QPointF &pos) const;
+
+    WaylandSurface *m_surface;
+    WaylandSurfaceTextureProvider *m_textureProvider;
+};
+
+#endif