Qt 4.8
qbbbuffer.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 - 2012 Research In Motion <blackberry-qt@qnx.com>
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module 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 
42 //#define QBBBUFFER_DEBUG
43 
44 
45 #include "qbbbuffer.h"
46 
47 #include <QDebug>
48 
49 #include <errno.h>
50 #include <sys/mman.h>
51 
53 
55  : mBuffer(NULL)
56 {
57 #if defined(QBBBUFFER_DEBUG)
58  qDebug() << "QBBBuffer::QBBBuffer - empty";
59 #endif
60 }
61 
62 QBBBuffer::QBBBuffer(screen_buffer_t buffer)
63  : mBuffer(buffer)
64 {
65 #if defined(QBBBUFFER_DEBUG)
66  qDebug() << "QBBBuffer::QBBBuffer - normal";
67 #endif
68 
69  // get size of buffer
70  errno = 0;
71  int size[2];
72  int result = screen_get_buffer_property_iv(buffer, SCREEN_PROPERTY_BUFFER_SIZE, size);
73  if (result != 0) {
74  qFatal("QBB: failed to query buffer size, errno=%d", errno);
75  }
76 
77  // get stride of buffer
78  errno = 0;
79  int stride;
80  result = screen_get_buffer_property_iv(buffer, SCREEN_PROPERTY_STRIDE, &stride);
81  if (result != 0) {
82  qFatal("QBB: failed to query buffer stride, errno=%d", errno);
83  }
84 
85  // get access to buffer's data
86  errno = 0;
87  uchar* dataPtr = NULL;
88  result = screen_get_buffer_property_pv(buffer, SCREEN_PROPERTY_POINTER, (void **)&dataPtr);
89  if (result != 0) {
90  qFatal("QBB: failed to query buffer pointer, errno=%d", errno);
91  }
92  if (dataPtr == NULL) {
93  qFatal("QBB: buffer pointer is NULL, errno=%d", errno);
94  }
95 
96  // get format of buffer
97  errno = 0;
98  int screenFormat;
99  result = screen_get_buffer_property_iv(buffer, SCREEN_PROPERTY_FORMAT, &screenFormat);
100  if (result != 0) {
101  qFatal("QBB: failed to query buffer format, errno=%d", errno);
102  }
103 
104  // convert screen format to QImage format
106  switch (screenFormat) {
107  case SCREEN_FORMAT_RGBX4444:
108  imageFormat = QImage::Format_RGB444;
109  break;
110  case SCREEN_FORMAT_RGBA4444:
112  break;
113  case SCREEN_FORMAT_RGBX5551:
114  imageFormat = QImage::Format_RGB555;
115  break;
116  case SCREEN_FORMAT_RGB565:
117  imageFormat = QImage::Format_RGB16;
118  break;
119  case SCREEN_FORMAT_RGBX8888:
120  imageFormat = QImage::Format_RGB32;
121  break;
122  case SCREEN_FORMAT_RGBA8888:
124  break;
125  default:
126  qFatal("QBB: unsupported buffer format, format=%d", screenFormat);
127  }
128 
129  // wrap buffer in an image
130  mImage = QImage(dataPtr, size[0], size[1], stride, imageFormat);
131 }
132 
134  : mBuffer(other.mBuffer),
135  mImage(other.mImage)
136 {
137 #if defined(QBBBUFFER_DEBUG)
138  qDebug() << "QBBBuffer::QBBBuffer - copy";
139 #endif
140 }
141 
143 {
144 #if defined(QBBBUFFER_DEBUG)
145  qDebug() << "QBBBuffer::~QBBBuffer";
146 #endif
147 }
148 
150 {
151 #if defined(QBBBUFFER_DEBUG)
152  qDebug() << "QBBBuffer::invalidateInCache";
153 #endif
154 
155  // verify native buffer exists
156  if (mBuffer == NULL) {
157  qFatal("QBB: can't invalidate cache for null buffer");
158  }
159 
160  // evict buffer's data from cache
161  errno = 0;
162  int result = msync(mImage.bits(), mImage.height() * mImage.bytesPerLine(), MS_INVALIDATE | MS_CACHE_ONLY);
163  if (result != 0) {
164  qFatal("QBB: failed to invalidate cache, errno=%d", errno);
165  }
166 }
167 
Format
The following image formats are available in Qt.
Definition: qimage.h:91
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int bytesPerLine() const
Returns the number of bytes per image scanline.
Definition: qimage.cpp:1812
virtual ~QBBBuffer()
Definition: qbbbuffer.cpp:142
Q_CORE_EXPORT void qDebug(const char *,...)
unsigned char uchar
Definition: qglobal.h:994
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QImage mImage
Definition: qbbbuffer.h:69
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
screen_buffer_t mBuffer
Definition: qbbbuffer.h:68
uchar * bits()
Returns a pointer to the first pixel data.
Definition: qimage.cpp:1946
Q_CORE_EXPORT void qFatal(const char *,...)
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
void invalidateInCache()
Definition: qbbbuffer.cpp:149
int errno