1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the Qt Compositor.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 ** the names of its contributors may be used to endorse or promote
22 ** products derived from this software without specific prior written
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
41 #include "waylandsurfaceitem.h"
42 #include "waylandsurfacenode.h"
43 #include "waylandsurface.h"
44 #include "waylandcompositor.h"
45 #include "waylandinput.h"
47 #include "wlsurface.h"
48 #include "wlextendedsurface.h"
50 #include <QtGui/QKeyEvent>
51 #include <QtGui/QGuiApplication>
52 #include <QtGui/QScreen>
54 #include <QtQuick/QSGSimpleRectNode>
55 #include <QtQuick/QQuickCanvas>
57 #include <QtCore/QMutexLocker>
58 #include <QtCore/QMutex>
61 class WaylandSurfaceTextureProvider : public QSGTextureProvider
66 WaylandSurfaceTextureProvider() : t(0) { }
67 ~WaylandSurfaceTextureProvider() { delete t; }
69 QSGTexture *texture() const {
71 t->setFiltering(smooth ? QSGTexture::Linear : QSGTexture::Nearest);
86 QMutex *WaylandSurfaceItem::mutex = 0;
88 WaylandSurfaceItem::WaylandSurfaceItem(QQuickItem *parent)
93 , m_paintEnabled(true)
94 , m_useTextureAlpha(false)
95 , m_clientRenderingEnabled(false)
96 , m_touchEventsEnabled(false)
102 WaylandSurfaceItem::WaylandSurfaceItem(WaylandSurface *surface, QQuickItem *parent)
107 , m_paintEnabled(true)
108 , m_useTextureAlpha(false)
109 , m_clientRenderingEnabled(false)
110 , m_touchEventsEnabled(false)
115 void WaylandSurfaceItem::init(WaylandSurface *surface)
121 m_surface->setSurfaceItem(0);
125 m_surface->setSurfaceItem(this);
126 if (m_clientRenderingEnabled) {
127 m_surface->sendOnScreenVisibilityChange(m_clientRenderingEnabled);
130 setWidth(surface->size().width());
131 setHeight(surface->size().height());
134 setFlag(ItemHasContents);
135 setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
136 setAcceptHoverEvents(true);
137 connect(surface, SIGNAL(mapped()), this, SLOT(surfaceMapped()));
138 connect(surface, SIGNAL(unmapped()), this, SLOT(surfaceUnmapped()));
139 connect(surface, SIGNAL(destroyed(QObject *)), this, SLOT(surfaceDestroyed(QObject *)));
140 connect(surface, SIGNAL(damaged(const QRect &)), this, SLOT(surfaceDamaged(const QRect &)));
141 connect(surface, SIGNAL(parentChanged(WaylandSurface*,WaylandSurface*)),
142 this, SLOT(parentChanged(WaylandSurface*,WaylandSurface*)));
143 connect(surface, SIGNAL(sizeChanged()), this, SLOT(updateSize()));
144 connect(surface, SIGNAL(posChanged()), this, SLOT(updatePosition()));
147 m_yInverted = surface ? surface->isYInverted() : true;
150 WaylandSurfaceItem::~WaylandSurfaceItem()
152 QMutexLocker locker(mutex);
156 m_surface->setSurfaceItem(0);
158 m_provider->deleteLater();
161 void WaylandSurfaceItem::setSurface(WaylandSurface *surface)
163 if (surface == m_surface)
167 emit surfaceChanged();
170 bool WaylandSurfaceItem::isYInverted() const
175 QSGTextureProvider *WaylandSurfaceItem::textureProvider() const
177 const_cast<WaylandSurfaceItem *>(this)->ensureProvider();
181 void WaylandSurfaceItem::ensureProvider()
184 m_provider = new WaylandSurfaceTextureProvider();
185 connect(canvas(), SIGNAL(sceneGraphInvalidated()), m_provider, SLOT(invalidate()), Qt::DirectConnection);
189 void WaylandSurfaceItem::mousePressEvent(QMouseEvent *event)
192 WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
193 if (inputDevice->mouseFocus() != m_surface)
194 inputDevice->setMouseFocus(m_surface, event->pos(), event->globalPos());
195 inputDevice->sendMousePressEvent(event->button(), toSurface(event->pos()));
199 void WaylandSurfaceItem::mouseMoveEvent(QMouseEvent *event)
202 WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
203 inputDevice->sendMouseMoveEvent(m_surface, toSurface(event->pos()));
207 void WaylandSurfaceItem::mouseReleaseEvent(QMouseEvent *event)
210 WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
211 inputDevice->sendMouseReleaseEvent(event->button(), toSurface(event->pos()));
215 void WaylandSurfaceItem::wheelEvent(QWheelEvent *event)
218 WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
219 inputDevice->sendMouseWheelEvent(event->orientation(), event->delta());
223 void WaylandSurfaceItem::keyPressEvent(QKeyEvent *event)
225 if (m_surface && hasFocus()) {
226 WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
227 inputDevice->sendFullKeyEvent(event);
231 void WaylandSurfaceItem::keyReleaseEvent(QKeyEvent *event)
233 if (m_surface && hasFocus()) {
234 WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
235 inputDevice->sendFullKeyEvent(event);
239 void WaylandSurfaceItem::touchEvent(QTouchEvent *event)
241 if (m_touchEventsEnabled && m_surface) {
242 WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
244 if (inputDevice->mouseFocus() != m_surface) {
246 QList<QTouchEvent::TouchPoint> points = event->touchPoints();
247 if (!points.isEmpty())
248 pointPos = points.at(0).pos().toPoint();
249 inputDevice->setMouseFocus(m_surface, pointPos, pointPos);
251 inputDevice->sendFullTouchEvent(event);
257 void WaylandSurfaceItem::takeFocus()
262 WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
263 inputDevice->setKeyboardFocus(m_surface);
267 QPoint WaylandSurfaceItem::toSurface(const QPointF &pos) const
269 return pos.toPoint();
272 void WaylandSurfaceItem::surfaceMapped()
274 setPaintEnabled(true);
277 void WaylandSurfaceItem::surfaceUnmapped()
279 setPaintEnabled(false);
282 void WaylandSurfaceItem::surfaceDestroyed(QObject *)
287 void WaylandSurfaceItem::setDamagedFlag(bool on)
293 void WaylandSurfaceItem::surfaceDamaged(const QRect &)
297 bool inverted = m_surface->isYInverted();
298 if (inverted != m_yInverted) {
299 m_yInverted = inverted;
300 emit yInvertedChanged();
303 emit textureChanged();
307 void WaylandSurfaceItem::parentChanged(WaylandSurface *newParent, WaylandSurface *oldParent)
311 WaylandSurfaceItem *item = newParent? newParent->surfaceItem():0;
315 setPaintEnabled(true);
322 void WaylandSurfaceItem::updateSize()
324 setSize(m_surface->size());
327 void WaylandSurfaceItem::updatePosition()
329 setPos(m_surface->pos());
332 bool WaylandSurfaceItem::paintEnabled() const
334 return m_paintEnabled;
337 void WaylandSurfaceItem::setPaintEnabled(bool enabled)
339 m_paintEnabled = enabled;
343 void WaylandSurfaceItem::updateTexture()
346 QSGTexture *texture = m_provider->t;
349 QSGTexture *oldTexture = texture;
350 if (m_surface->type() == WaylandSurface::Texture) {
351 QOpenGLContext *context = QOpenGLContext::currentContext();
352 QQuickCanvas::CreateTextureOptions opt = 0;
353 if (useTextureAlpha()) {
354 opt |= QQuickCanvas::TextureHasAlphaChannel;
356 texture = canvas()->createTextureFromId(m_surface->texture(context), m_surface->size(), opt);
358 texture = canvas()->createTextureFromImage(m_surface->image());
364 m_provider->t = texture;
365 m_provider->smooth = smooth();
368 QSGNode *WaylandSurfaceItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
376 if (!m_provider->t || !m_paintEnabled) {
381 WaylandSurfaceNode *node = static_cast<WaylandSurfaceNode *>(oldNode);
384 node = new WaylandSurfaceNode(this);
387 node->updateTexture();
389 if (surface()->isYInverted()) {
390 node->setRect(0, height(), width(), -height());
392 node->setRect(0, 0, width(), height());
395 node->setTextureUpdated(true);
400 void WaylandSurfaceItem::setUseTextureAlpha(bool useTextureAlpha)
402 m_useTextureAlpha = useTextureAlpha;
404 if ((flags() & ItemHasContents) != 0) {
409 void WaylandSurfaceItem::setClientRenderingEnabled(bool enabled)
411 if (m_clientRenderingEnabled != enabled) {
412 m_clientRenderingEnabled = enabled;
415 m_surface->sendOnScreenVisibilityChange(enabled);
418 emit clientRenderingEnabledChanged();
422 void WaylandSurfaceItem::setTouchEventsEnabled(bool enabled)
424 if (m_touchEventsEnabled != enabled) {
425 m_touchEventsEnabled = enabled;
426 emit touchEventsEnabledChanged();
430 #include "waylandsurfaceitem.moc"