Do not pass texture ownership to scenegraph in WaylandSurfaceItem
[profile/ivi/qtwayland.git] / src / compositor / compositor_api / waylandsurfaceitem.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the Qt Compositor.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
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
19 **     distribution.
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
23 **     permission.
24 **
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."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "waylandsurfaceitem.h"
42 #include "waylandsurfacenode.h"
43 #include "waylandsurface.h"
44 #include "waylandcompositor.h"
45 #include "waylandinput.h"
46
47 #include "wlsurface.h"
48 #include "wlextendedsurface.h"
49
50 #include <QtGui/QKeyEvent>
51 #include <QtGui/QGuiApplication>
52 #include <QtGui/QScreen>
53
54 #include <QtQuick/QSGSimpleRectNode>
55 #include <QtQuick/QQuickCanvas>
56
57 #include <QtCore/QMutexLocker>
58 #include <QtCore/QMutex>
59
60
61 class WaylandSurfaceTextureProvider : public QSGTextureProvider
62 {
63     Q_OBJECT
64
65 public:
66     WaylandSurfaceTextureProvider() : t(0) { }
67     ~WaylandSurfaceTextureProvider() { delete t; }
68
69     QSGTexture *texture() const {
70         if (t)
71             t->setFiltering(smooth ? QSGTexture::Linear : QSGTexture::Nearest);
72         return t;
73     }
74
75     bool smooth;
76     QSGTexture *t;
77
78 public slots:
79     void invalidate()
80     {
81         delete t;
82         t = 0;
83     }
84 };
85
86 QMutex *WaylandSurfaceItem::mutex = 0;
87
88 WaylandSurfaceItem::WaylandSurfaceItem(QQuickItem *parent)
89     : QQuickItem(parent)
90     , m_surface(0)
91     , m_provider(0)
92     , m_node(0)
93     , m_paintEnabled(true)
94     , m_useTextureAlpha(false)
95     , m_clientRenderingEnabled(false)
96     , m_touchEventsEnabled(false)
97 {
98     if (!mutex)
99         mutex = new QMutex;
100 }
101
102 WaylandSurfaceItem::WaylandSurfaceItem(WaylandSurface *surface, QQuickItem *parent)
103     : QQuickItem(parent)
104     , m_surface(0)
105     , m_provider(0)
106     , m_node(0)
107     , m_paintEnabled(true)
108     , m_useTextureAlpha(false)
109     , m_clientRenderingEnabled(false)
110     , m_touchEventsEnabled(false)
111 {
112     init(surface);
113 }
114
115 void WaylandSurfaceItem::init(WaylandSurface *surface)
116 {
117     if (!surface)
118         return;
119
120     if (m_surface) {
121         m_surface->setSurfaceItem(0);
122     }
123
124     m_surface = surface;
125     m_surface->setSurfaceItem(this);
126     if (m_clientRenderingEnabled) {
127         m_surface->sendOnScreenVisibilityChange(m_clientRenderingEnabled);
128     }
129
130     setWidth(surface->size().width());
131     setHeight(surface->size().height());
132
133     setSmooth(true);
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()));
145
146     m_damaged = false;
147     m_yInverted = surface ? surface->isYInverted() : true;
148 }
149
150 WaylandSurfaceItem::~WaylandSurfaceItem()
151 {
152     QMutexLocker locker(mutex);
153     if (m_node)
154         m_node->setItem(0);
155     if (m_surface)
156         m_surface->setSurfaceItem(0);
157     if (m_provider)
158         m_provider->deleteLater();
159 }
160
161 void WaylandSurfaceItem::setSurface(WaylandSurface *surface)
162 {
163     if (surface == m_surface)
164         return;
165
166     init(surface);
167     emit surfaceChanged();
168 }
169
170 bool WaylandSurfaceItem::isYInverted() const
171 {
172     return m_yInverted;
173 }
174
175 QSGTextureProvider *WaylandSurfaceItem::textureProvider() const
176 {
177     const_cast<WaylandSurfaceItem *>(this)->ensureProvider();
178     return m_provider;
179 }
180
181 void WaylandSurfaceItem::ensureProvider()
182 {
183     if (!m_provider) {
184         m_provider = new WaylandSurfaceTextureProvider();
185         connect(canvas(), SIGNAL(sceneGraphInvalidated()), m_provider, SLOT(invalidate()), Qt::DirectConnection);
186     }
187 }
188
189 void WaylandSurfaceItem::mousePressEvent(QMouseEvent *event)
190 {
191     if (m_surface) {
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()));
196     }
197 }
198
199 void WaylandSurfaceItem::mouseMoveEvent(QMouseEvent *event)
200 {
201     if (m_surface){
202         WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
203         inputDevice->sendMouseMoveEvent(m_surface, toSurface(event->pos()));
204     }
205 }
206
207 void WaylandSurfaceItem::mouseReleaseEvent(QMouseEvent *event)
208 {
209     if (m_surface){
210         WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
211         inputDevice->sendMouseReleaseEvent(event->button(), toSurface(event->pos()));
212     }
213 }
214
215 void WaylandSurfaceItem::wheelEvent(QWheelEvent *event)
216 {
217     if (m_surface) {
218         WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
219         inputDevice->sendMouseWheelEvent(event->orientation(), event->delta());
220     }
221 }
222
223 void WaylandSurfaceItem::keyPressEvent(QKeyEvent *event)
224 {
225     if (m_surface && hasFocus()) {
226         WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
227         inputDevice->sendFullKeyEvent(event);
228     }
229 }
230
231 void WaylandSurfaceItem::keyReleaseEvent(QKeyEvent *event)
232 {
233     if (m_surface && hasFocus()) {
234         WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
235         inputDevice->sendFullKeyEvent(event);
236     }
237 }
238
239 void WaylandSurfaceItem::touchEvent(QTouchEvent *event)
240 {
241     if (m_touchEventsEnabled && m_surface) {
242         WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
243         event->accept();
244         if (inputDevice->mouseFocus() != m_surface) {
245             QPoint pointPos;
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);
250         }
251         inputDevice->sendFullTouchEvent(event);
252     } else {
253         event->ignore();
254     }
255 }
256
257 void WaylandSurfaceItem::takeFocus()
258 {
259     setFocus(true);
260
261     if (m_surface) {
262         WaylandInputDevice *inputDevice = m_surface->compositor()->defaultInputDevice();
263         inputDevice->setKeyboardFocus(m_surface);
264     }
265 }
266
267 QPoint WaylandSurfaceItem::toSurface(const QPointF &pos) const
268 {
269     return pos.toPoint();
270 }
271
272 void WaylandSurfaceItem::surfaceMapped()
273 {
274     setPaintEnabled(true);
275 }
276
277 void WaylandSurfaceItem::surfaceUnmapped()
278 {
279     setPaintEnabled(false);
280 }
281
282 void WaylandSurfaceItem::surfaceDestroyed(QObject *)
283 {
284     m_surface = 0;
285 }
286
287 void WaylandSurfaceItem::setDamagedFlag(bool on)
288 {
289     m_damaged = on;
290 }
291
292
293 void WaylandSurfaceItem::surfaceDamaged(const QRect &)
294 {
295     m_damaged = true;
296     if (m_surface) {
297         bool inverted = m_surface->isYInverted();
298         if (inverted  != m_yInverted) {
299             m_yInverted = inverted;
300             emit yInvertedChanged();
301         }
302     }
303     emit textureChanged();
304     update();
305 }
306
307 void WaylandSurfaceItem::parentChanged(WaylandSurface *newParent, WaylandSurface *oldParent)
308 {
309     Q_UNUSED(oldParent);
310
311     WaylandSurfaceItem *item = newParent? newParent->surfaceItem():0;
312     setParentItem(item);
313
314     if (newParent) {
315         setPaintEnabled(true);
316         setVisible(true);
317         setOpacity(1);
318         setEnabled(true);
319     }
320 }
321
322 void WaylandSurfaceItem::updateSize()
323 {
324     setSize(m_surface->size());
325 }
326
327 void WaylandSurfaceItem::updatePosition()
328 {
329     setPos(m_surface->pos());
330 }
331
332 bool WaylandSurfaceItem::paintEnabled() const
333 {
334     return m_paintEnabled;
335 }
336
337 void WaylandSurfaceItem::setPaintEnabled(bool enabled)
338 {
339     m_paintEnabled = enabled;
340     update();
341 }
342
343 void WaylandSurfaceItem::updateTexture()
344 {
345     ensureProvider();
346     QSGTexture *texture = m_provider->t;
347     if (m_damaged) {
348         m_damaged = false;
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;
355             }
356             texture = canvas()->createTextureFromId(m_surface->texture(context), m_surface->size(), opt);
357         } else {
358             texture = canvas()->createTextureFromImage(m_surface->image());
359         }
360         texture->bind();
361         delete oldTexture;
362     }
363
364     m_provider->t = texture;
365     m_provider->smooth = smooth();
366 }
367
368 QSGNode *WaylandSurfaceItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
369 {
370     if (!m_surface) {
371         delete oldNode;
372         return 0;
373     }
374
375     updateTexture();
376     if (!m_provider->t || !m_paintEnabled) {
377         delete oldNode;
378         return 0;
379     }
380
381     WaylandSurfaceNode *node = static_cast<WaylandSurfaceNode *>(oldNode);
382
383     if (!node) {
384         node = new WaylandSurfaceNode(this);
385     }
386
387     node->updateTexture();
388
389     if (surface()->isYInverted()) {
390         node->setRect(0, height(), width(), -height());
391     } else {
392         node->setRect(0, 0, width(), height());
393     }
394
395     node->setTextureUpdated(true);
396
397     return node;
398 }
399
400 void WaylandSurfaceItem::setUseTextureAlpha(bool useTextureAlpha)
401 {
402     m_useTextureAlpha = useTextureAlpha;
403
404     if ((flags() & ItemHasContents) != 0) {
405         update();
406     }
407 }
408
409 void WaylandSurfaceItem::setClientRenderingEnabled(bool enabled)
410 {
411     if (m_clientRenderingEnabled != enabled) {
412         m_clientRenderingEnabled = enabled;
413
414         if (m_surface) {
415             m_surface->sendOnScreenVisibilityChange(enabled);
416         }
417
418         emit clientRenderingEnabledChanged();
419     }
420 }
421
422 void WaylandSurfaceItem::setTouchEventsEnabled(bool enabled)
423 {
424     if (m_touchEventsEnabled != enabled) {
425         m_touchEventsEnabled = enabled;
426         emit touchEventsEnabledChanged();
427     }
428 }
429
430 #include "waylandsurfaceitem.moc"