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 "wlsurface.h"
43 #include "waylandsurface.h"
44 #include "waylandsurfaceitem.h"
46 #include "wlcompositor.h"
47 #include "wlinputdevice.h"
48 #include "wlextendedsurface.h"
50 #include "wlsubsurface.h"
51 #include "wlsurfacebuffer.h"
52 #include "wlshellsurface.h"
54 #include <QtCore/QDebug>
55 #include <QTouchEvent>
57 #include <wayland-server.h>
59 #ifdef QT_COMPOSITOR_WAYLAND_GL
60 #include "hardware_integration/graphicshardwareintegration.h"
61 #include <QtGui/QPlatformOpenGLContext>
64 #ifdef QT_WAYLAND_WINDOWMANAGER_SUPPORT
65 #include "waylandwindowmanagerintegration.h"
70 void destroy_surface(struct wl_resource *resource)
72 Surface *surface = resolve<Surface>(resource);
73 surface->compositor()->surfaceDestroyed(surface);
77 Surface::Surface(struct wl_client *client, uint32_t id, Compositor *compositor)
78 : m_compositor(compositor)
79 , m_waylandSurface(new WaylandSurface(this))
82 , m_surfaceMapped(false)
83 , m_extendedSurface(0)
87 wl_list_init(&m_frame_callback_list);
88 addClientResource(client, &base()->resource, id, &wl_surface_interface,
89 &Surface::surface_interface, destroy_surface);
90 for (int i = 0; i < buffer_pool_size; i++) {
91 m_bufferPool[i] = new SurfaceBuffer(this);
97 delete m_waylandSurface;
98 delete m_extendedSurface;
100 delete m_shellSurface;
102 for (int i = 0; i < buffer_pool_size; i++) {
103 if (!m_bufferPool[i]->pageFlipperHasBuffer())
104 delete m_bufferPool[i];
108 WaylandSurface::Type Surface::type() const
110 SurfaceBuffer *surfaceBuffer = currentSurfaceBuffer();
111 if (surfaceBuffer && surfaceBuffer->waylandBufferHandle()) {
112 if (surfaceBuffer->isShmBuffer()) {
113 return WaylandSurface::Shm;
115 return WaylandSurface::Texture;
118 return WaylandSurface::Invalid;
121 bool Surface::isYInverted() const
124 static bool negateReturn = qgetenv("QT_COMPOSITOR_NEGATE_INVERTED_Y").toInt();
125 GraphicsHardwareIntegration *graphicsHWIntegration = m_compositor->graphicsHWIntegration();
127 #ifdef QT_COMPOSITOR_WAYLAND_GL
128 SurfaceBuffer *surfacebuffer = currentSurfaceBuffer();
129 if (!surfacebuffer) {
131 } else if (graphicsHWIntegration && surfacebuffer->waylandBufferHandle() && type() != WaylandSurface::Shm) {
132 ret = graphicsHWIntegration->isYInverted(surfacebuffer->waylandBufferHandle());
137 return ret != negateReturn;
140 bool Surface::visible() const
143 SurfaceBuffer *surfacebuffer = currentSurfaceBuffer();
144 return surfacebuffer->waylandBufferHandle();
147 QPointF Surface::pos() const
149 return m_shellSurface ? m_shellSurface->adjustedPosToTransientParent() : m_position;
152 QPointF Surface::nonAdjustedPos() const
157 void Surface::setPos(const QPointF &pos)
159 bool emitChange = pos != m_position;
162 m_waylandSurface->posChanged();
165 QSize Surface::size() const
170 void Surface::setSize(const QSize &size)
172 if (size != m_size) {
173 m_opaqueRegion = QRegion();
174 m_inputRegion = QRegion(QRect(QPoint(), size));
176 if (m_shellSurface) {
177 m_shellSurface->adjustPosInResize();
179 m_waylandSurface->sizeChanged();
183 QRegion Surface::inputRegion() const
185 return m_inputRegion;
188 QRegion Surface::opaqueRegion() const
190 return m_opaqueRegion;
193 QImage Surface::image() const
195 SurfaceBuffer *surfacebuffer = currentSurfaceBuffer();
196 if (surfacebuffer && !surfacebuffer->isDestroyed() && type() == WaylandSurface::Shm) {
197 struct wl_buffer *buffer = surfacebuffer->waylandBufferHandle();
198 int stride = wl_shm_buffer_get_stride(buffer);
199 uint format = wl_shm_buffer_get_format(buffer);
201 void *data = wl_shm_buffer_get_data(buffer);
202 const uchar *char_data = static_cast<const uchar *>(data);
203 QImage img(char_data, buffer->width, buffer->height, stride, QImage::Format_ARGB32_Premultiplied);
209 #ifdef QT_COMPOSITOR_WAYLAND_GL
210 GLuint Surface::textureId(QOpenGLContext *context) const
212 const SurfaceBuffer *surfacebuffer = currentSurfaceBuffer();
214 if (m_compositor->graphicsHWIntegration() && type() == WaylandSurface::Texture
215 && !surfacebuffer->textureCreated()) {
216 GraphicsHardwareIntegration *hwIntegration = m_compositor->graphicsHWIntegration();
217 const_cast<SurfaceBuffer *>(surfacebuffer)->createTexture(hwIntegration,context);
219 return surfacebuffer->texture();
221 #endif // QT_COMPOSITOR_WAYLAND_GL
223 void Surface::sendFrameCallback()
225 SurfaceBuffer *surfacebuffer = currentSurfaceBuffer();
226 surfacebuffer->setDisplayed();
229 m_frontBuffer->disown();
230 m_frontBuffer = m_backBuffer;
233 bool updateNeeded = advanceBufferQueue();
235 uint time = Compositor::currentTimeMsecs();
236 struct wl_resource *frame_callback;
237 wl_list_for_each(frame_callback, &m_frame_callback_list, link) {
238 wl_callback_send_done(frame_callback, time);
239 wl_resource_destroy(frame_callback);
241 wl_list_init(&m_frame_callback_list);
247 void Surface::frameFinished()
249 m_compositor->frameFinished(this);
252 WaylandSurface * Surface::waylandSurface() const
254 return m_waylandSurface;
257 QPoint Surface::lastMousePos() const
259 return m_lastLocalMousePos;
262 void Surface::setExtendedSurface(ExtendedSurface *extendedSurface)
264 m_extendedSurface = extendedSurface;
265 if (m_extendedSurface)
266 emit m_waylandSurface->extendedSurfaceReady();
269 ExtendedSurface *Surface::extendedSurface() const
271 return m_extendedSurface;
274 void Surface::setSubSurface(SubSurface *subSurface)
276 m_subSurface = subSurface;
279 SubSurface *Surface::subSurface() const
284 void Surface::setShellSurface(ShellSurface *shellSurface)
286 m_shellSurface = shellSurface;
289 ShellSurface *Surface::shellSurface() const
291 return m_shellSurface;
294 Compositor *Surface::compositor() const
299 bool Surface::advanceBufferQueue()
301 //has current buffer been displayed,
302 //do we have another buffer in the queue
303 //and does it have a valid damage rect
305 if (m_bufferQueue.size()) {
309 width = m_backBuffer->width();
310 height = m_backBuffer->height();
313 m_backBuffer = m_bufferQueue.takeFirst();
314 while (m_backBuffer && m_backBuffer->isDestroyed()) {
315 m_backBuffer->disown();
316 m_backBuffer = m_bufferQueue.size() ? m_bufferQueue.takeFirst() : 0;
320 return false; //we have no new backbuffer;
322 if (m_backBuffer->waylandBufferHandle()) {
323 width = m_backBuffer->width();
324 height = m_backBuffer->height();
326 setSize(QSize(width,height));
329 if (m_backBuffer && (!m_subSurface || !m_subSurface->parent()) && !m_surfaceMapped) {
330 m_surfaceMapped = true;
331 emit m_waylandSurface->mapped();
332 } else if (m_backBuffer && !m_backBuffer->waylandBufferHandle() && m_surfaceMapped) {
333 m_surfaceMapped = false;
334 emit m_waylandSurface->unmapped();
345 void Surface::doUpdate() {
347 WaylandSurfaceItem *surfaceItem = waylandSurface()->surfaceItem();
349 surfaceItem->setDamagedFlag(true); // avoid flicker when we switch back to composited mode
352 SurfaceBuffer *surfaceBuffer = currentSurfaceBuffer();
354 if (surfaceBuffer->damageRect().isValid()) {
355 m_compositor->markSurfaceAsDirty(this);
356 emit m_waylandSurface->damaged(surfaceBuffer->damageRect());
362 SurfaceBuffer *Surface::createSurfaceBuffer(struct wl_buffer *buffer)
364 SurfaceBuffer *newBuffer = 0;
365 for (int i = 0; i < Surface::buffer_pool_size; i++) {
366 if (!m_bufferPool[i]->isRegisteredWithBuffer()) {
367 newBuffer = m_bufferPool[i];
368 newBuffer->initialize(buffer);
377 bool Surface::postBuffer() {
378 #ifdef QT_COMPOSITOR_WAYLAND_GL
379 if (m_waylandSurface->handle() == m_compositor->directRenderSurface()) {
380 SurfaceBuffer *surfaceBuffer = m_backBuffer? m_backBuffer : m_frontBuffer;
381 if (surfaceBuffer && m_compositor->pageFlipper()) {
382 if (m_compositor->pageFlipper()->displayBuffer(surfaceBuffer)) {
383 surfaceBuffer->setPageFlipperHasBuffer(true);
386 qDebug() << "could not post buffer";
394 void Surface::attach(struct wl_buffer *buffer)
396 SurfaceBuffer *last = m_bufferQueue.size()?m_bufferQueue.last():0;
398 if (last->waylandBufferHandle() == buffer)
400 if (!last->damageRect().isValid()) {
402 m_bufferQueue.takeLast();
406 m_bufferQueue << createSurfaceBuffer(buffer);
409 void Surface::damage(const QRect &rect)
411 SurfaceBuffer *surfaceBuffer = m_bufferQueue.isEmpty() ? currentSurfaceBuffer() : m_bufferQueue.last();
413 surfaceBuffer->setDamage(rect);
415 qWarning() << "Surface::damage() null buffer";
417 if (!m_bufferQueue.isEmpty() && !m_backBuffer)
418 advanceBufferQueue();
423 const struct wl_surface_interface Surface::surface_interface = {
424 Surface::surface_destroy,
425 Surface::surface_attach,
426 Surface::surface_damage,
427 Surface::surface_frame,
428 Surface::surface_set_opaque_region,
429 Surface::surface_set_input_region
432 void Surface::surface_destroy(struct wl_client *, struct wl_resource *surface_resource)
434 wl_resource_destroy(surface_resource);
437 void Surface::surface_attach(struct wl_client *client, struct wl_resource *surface,
438 struct wl_resource *buffer, int x, int y)
443 resolve<Surface>(surface)->attach(buffer ? reinterpret_cast<wl_buffer *>(buffer->data) : 0);
446 void Surface::surface_damage(struct wl_client *client, struct wl_resource *surface,
447 int32_t x, int32_t y, int32_t width, int32_t height)
450 resolve<Surface>(surface)->damage(QRect(x, y, width, height));
453 void Surface::surface_frame(struct wl_client *client,
454 struct wl_resource *resource,
457 Surface *surface = resolve<Surface>(resource);
458 struct wl_resource *frame_callback = wl_client_add_object(client,&wl_callback_interface,0,callback,surface);
459 wl_list_insert(&surface->m_frame_callback_list,&frame_callback->link);
462 void Surface::surface_set_opaque_region(struct wl_client *client, struct wl_resource *surfaceResource,
463 struct wl_resource *region)
466 Surface *surface = resolve<Surface>(surfaceResource);
467 surface->m_opaqueRegion = region ? resolve<Region>(region)->region() : QRegion();
470 void Surface::surface_set_input_region(struct wl_client *client, struct wl_resource *surfaceResource,
471 struct wl_resource *region)
474 Surface *surface = resolve<Surface>(surfaceResource);
475 surface->m_inputRegion = region ? resolve<Region>(region)->region() : QRegion(QRect(QPoint(), surface->size()));
478 } // namespace Wayland