Qt 4.8
qwaylandshmsurface.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include "qwaylandshmsurface.h"
42 
43 #include <QtCore/qdebug.h>
44 #include <QtGui/private/qapplication_p.h>
45 
46 #include "qwaylanddisplay.h"
47 #include "qwaylandshmwindow.h"
48 #include "qwaylandscreen.h"
49 
50 #include <wayland-client.h>
51 #include <unistd.h>
52 #include <fcntl.h>
53 #include <errno.h>
54 #include <sys/mman.h>
55 
57 
59  const QSize &size, QImage::Format format)
60 {
61  int stride = size.width() * 4;
62  int alloc = stride * size.height();
63  char filename[] = "/tmp/wayland-shm-XXXXXX";
64  int fd = mkstemp(filename);
65  if (fd < 0)
66  qWarning("open %s failed: %s", filename, strerror(errno));
67  if (ftruncate(fd, alloc) < 0) {
68  qWarning("ftruncate failed: %s", strerror(errno));
69  close(fd);
70  return;
71  }
72  uchar *data = (uchar *)
73  mmap(NULL, alloc, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
74  unlink(filename);
75 
76  if (data == (uchar *) MAP_FAILED) {
77  qWarning("mmap /dev/zero failed: %s", strerror(errno));
78  close(fd);
79  return;
80  }
81 
82  mImage = QImage(data, size.width(), size.height(), stride, format);
83  mBuffer = display->createShmBuffer(fd, size.width(), size.height(),
84  stride, display->argbVisual());
85  close(fd);
86 }
87 
89 {
90  munmap((void *) mImage.constBits(), mImage.byteCount());
91  wl_buffer_destroy(mBuffer);
92 }
93 
95  : QWindowSurface(window)
96  , mBuffer(0)
97  , mDisplay(QWaylandScreen::waylandScreenFromWidget(window)->display())
98 {
99 }
100 
102 {
103 }
104 
106 {
107  return mBuffer->image();
108 }
109 
111 {
112  QWaylandShmWindow *waylandWindow = static_cast<QWaylandShmWindow *>(window()->platformWindow());
113  Q_ASSERT(waylandWindow->windowType() == QWaylandWindow::Shm);
114  waylandWindow->waitForFrameSync();
115 }
116 
118 {
119  Q_UNUSED(widget);
120  Q_UNUSED(offset);
121  QWaylandShmWindow *waylandWindow = static_cast<QWaylandShmWindow *>(window()->platformWindow());
122  Q_ASSERT(waylandWindow->windowType() == QWaylandWindow::Shm);
123  QVector<QRect> rects = region.rects();
124  for (int i = 0; i < rects.size(); i++) {
125  const QRect rect = rects.at(i);
126  wl_buffer_damage(mBuffer->buffer(),rect.x(),rect.y(),rect.width(),rect.height());
127  waylandWindow->damage(rect);
128  }
129 }
130 
132 {
133  QWaylandShmWindow *waylandWindow = static_cast<QWaylandShmWindow *>(window()->platformWindow());
134  Q_ASSERT(waylandWindow->windowType() == QWaylandWindow::Shm);
135 
136  QWindowSurface::resize(size);
138 
139  if (mBuffer != NULL && mBuffer->size() == size)
140  return;
141 
142  if (mBuffer != NULL)
143  delete mBuffer;
144 
145  mBuffer = new QWaylandShmBuffer(mDisplay, size, format);
146 
147  waylandWindow->attach(mBuffer);
148 }
149 
Format
The following image formats are available in Qt.
Definition: qimage.h:91
#define MAP_FAILED
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QPointer< QWidget > widget
QRect rect(const QWidget *widget) const
Returns the rectangle for widget in the coordinates of this window surface.
QWaylandShmBuffer * mBuffer
struct wl_visual * argbVisual()
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
int byteCount() const
Returns the number of bytes occupied by the image data.
Definition: qimage.cpp:1800
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
virtual QImage::Format format() const =0
Reimplement in subclass to return the image format which corresponds to the screen format...
virtual QPoint offset(const QWidget *widget) const
Returns the offset of widget in the coordinates of this window surface.
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
QWaylandShmBuffer(QWaylandDisplay *display, const QSize &size, QImage::Format format)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void flush(QWidget *widget, const QRegion &region, const QPoint &offset)
Flushes the given region from the specified widget onto the screen.
unsigned char uchar
Definition: qglobal.h:994
NSWindow * window
int width() const
Returns the width.
Definition: qsize.h:126
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Q_GUI_EXPORT EGLDisplay display()
Definition: qegl.cpp:589
Q_CORE_EXPORT void qWarning(const char *,...)
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
static const char * data(const QByteArray &arr)
static QPlatformScreen * platformScreenForWidget(const QWidget *widget)
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
WindowType windowType() const
const uchar * constBits() const
Returns a pointer to the first pixel data.
Definition: qimage.cpp:1985
void attach(QWaylandBuffer *buffer)
The QWindowSurface class provides the drawing area for top-level windows.
QPaintDevice * paintDevice()
Implement this function to return the appropriate paint device.
void resize(const QSize &size)
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QVector< QRect > rects() const
Returns an array of non-overlapping rectangles that make up the region.
Definition: qregion.cpp:4412
wl_buffer * buffer()
QWaylandShmWindowSurface(QWidget *window)
int height() const
Returns the height.
Definition: qsize.h:129
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
QWaylandDisplay * mDisplay
struct wl_buffer * mBuffer
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
void damage(const QRect &rect)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
void beginPaint(const QRegion &)
This function is called before painting onto the surface begins, with the region in which the paintin...
QWidget * window() const
Returns a pointer to the top-level window associated with this surface.
struct wl_buffer * createShmBuffer(int fd, int width, int height, uint32_t stride, struct wl_visual *visual)
int errno