Set the correct filtering when the texture is used as a shader source
[profile/ivi/qtwayland.git] / src / qt-compositor / compositor_api / waylandsurfaceitem.cpp
1 /****************************************************************************
2 **
3 ** This file is part of QtCompositor**
4 **
5 ** Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies).
6 ** All rights reserved.
7 **
8 ** Contact:  Nokia Corporation qt-info@nokia.com
9 **
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **
16 ** Redistributions of source code must retain the above copyright
17 ** notice, this list of conditions and the following disclaimer.
18 **
19 ** Redistributions in binary form must reproduce the above copyright
20 ** notice, this list of conditions and the following disclaimer in the
21 ** documentation and/or other materials provided with the distribution.
22 **
23 ** Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the
24 ** names of its contributors may be used to endorse or promote products
25 ** derived from this software without specific prior written permission.
26 **
27 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 **
39 ****************************************************************************/
40
41 #include "waylandsurfaceitem.h"
42 #include "waylandsurface.h"
43
44 #include <qsgengine.h>
45 #include <private/qsgitem_p.h>
46
47 #include <QKeyEvent>
48
49 #include <qsgsimpletexturenode.h>
50
51 void WaylandSurfaceItem::surfaceDamaged(const QRect &)
52 {
53     if (m_texture)
54         delete m_texture;
55
56     if (m_surface->type() == WaylandSurface::Texture) {
57         m_texture = canvas()->sceneGraphEngine()->createTextureFromId(m_surface->texture(),
58                                                                       m_surface->geometry().size(),
59                                                                       QSGEngine::TextureOwnsGLTexture);
60     } else {
61         m_texture = canvas()->sceneGraphEngine()->createTextureFromImage(m_surface->image());
62     }
63
64     emit textureChanged();
65 }
66
67 WaylandSurfaceItem::WaylandSurfaceItem(QSGItem *parent)
68     : QSGItem(parent)
69     , m_surface(0)
70     , m_texture(0)
71 {
72 }
73
74 WaylandSurfaceItem::WaylandSurfaceItem(WaylandSurface *surface, QSGItem *parent)
75     : QSGItem(parent)
76     , m_surface(0)
77     , m_texture(0)
78 {
79     init(surface);
80 }
81
82 void WaylandSurfaceItem::init(WaylandSurface *surface)
83 {
84     if (!surface)
85         return;
86
87     m_surface = surface;
88
89     setWidth(surface->geometry().width());
90     setHeight(surface->geometry().height());
91
92     setSmooth(true);
93     setFlag(ItemHasContents);
94     setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
95     connect(surface, SIGNAL(mapped(const QRect &)), this, SLOT(surfaceMapped(const QRect &)));
96     connect(surface, SIGNAL(destroyed(QObject *)), this, SLOT(surfaceDestroyed(QObject *)));
97     connect(this, SIGNAL(textureChanged()), this, SLOT(update()));
98     connect(surface, SIGNAL(damaged(const QRect &)), this, SLOT(surfaceDamaged(const QRect &)));
99 }
100
101 WaylandSurfaceItem::~WaylandSurfaceItem()
102 {
103     delete m_texture;
104 }
105
106 void WaylandSurfaceItem::setSurface(WaylandSurface *surface)
107 {
108     init(surface);
109 }
110
111 QSGTexture *WaylandSurfaceItem::texture() const
112 {
113     if (m_texture)
114         m_texture->setFiltering(smooth() ? QSGTexture::Linear : QSGTexture::Nearest);
115     return m_texture;
116 }
117
118 void WaylandSurfaceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
119 {
120     if (m_surface)
121         m_surface->sendMousePressEvent(toSurface(event->pos()), event->button());
122 }
123
124 void WaylandSurfaceItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
125 {
126     if (m_surface)
127         m_surface->sendMouseMoveEvent(toSurface(event->pos()));
128 }
129
130 void WaylandSurfaceItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
131 {
132     if (m_surface)
133         m_surface->sendMouseReleaseEvent(toSurface(event->pos()), event->button());
134 }
135
136 void WaylandSurfaceItem::keyPressEvent(QKeyEvent *event)
137 {
138     if (m_surface && hasFocus())
139         m_surface->sendKeyPressEvent(event->nativeScanCode());
140 }
141
142 void WaylandSurfaceItem::keyReleaseEvent(QKeyEvent *event)
143 {
144     if (m_surface && hasFocus())
145         m_surface->sendKeyReleaseEvent(event->nativeScanCode());
146 }
147
148 void WaylandSurfaceItem::takeFocus()
149 {
150     setFocus(true);
151
152     if (m_surface)
153         m_surface->setInputFocus();
154 }
155
156 QPoint WaylandSurfaceItem::toSurface(const QPointF &pos) const
157 {
158     return pos.toPoint();
159 }
160
161 void WaylandSurfaceItem::surfaceMapped(const QRect &rect)
162 {
163     setWidth(rect.width());
164     setHeight(rect.height());
165 }
166
167 void WaylandSurfaceItem::surfaceDestroyed(QObject *)
168 {
169     m_surface = 0;
170 }
171
172 QSGNode *WaylandSurfaceItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
173 {
174     QSGSimpleTextureNode *node = static_cast<QSGSimpleTextureNode *>(oldNode);
175
176     if (!m_texture) {
177         delete oldNode;
178         return 0;
179     }
180
181     if (!node) {
182         node = new QSGSimpleTextureNode();
183         node->setTexture(m_texture);
184     }
185
186     node->setRect(QRectF(0, 0, width(), height()));
187     node->setFiltering(QSGItemPrivate::get(this)->smooth
188                        ? QSGTexture::Linear
189                        : QSGTexture::Nearest);
190
191     return node;
192 }