Qt 4.8
qdirectfbblitter.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 
42 #include "qdirectfbblitter.h"
43 #include "qdirectfbconvenience.h"
44 
45 #include <QtGui/private/qpixmap_blitter_p.h>
46 
47 #include <QDebug>
48 #include <QFile>
49 
50 #include <directfb.h>
51 
53 
54 static QBlittable::Capabilities dfb_blitter_capabilities()
55 {
56  return QBlittable::Capabilities(QBlittable::SolidRectCapability
62 }
63 
64 QDirectFbBlitter::QDirectFbBlitter(const QSize &rect, IDirectFBSurface *surface)
66  , m_surface(surface)
67 {
68  m_surface->AddRef(m_surface.data());
69 
70  DFBSurfaceCapabilities surfaceCaps;
71  m_surface->GetCapabilities(m_surface.data(), &surfaceCaps);
72  m_premult = (surfaceCaps & DSCAPS_PREMULTIPLIED);
73 }
74 
75 QDirectFbBlitter::QDirectFbBlitter(const QSize &rect, bool alpha)
77 {
78  DFBSurfaceDescription surfaceDesc;
79  memset(&surfaceDesc,0,sizeof(DFBSurfaceDescription));
80  surfaceDesc.width = rect.width();
81  surfaceDesc.height = rect.height();
82 
83  // force alpha format to get AlphaFillRectCapability and ExtendedPixmapCapability support
84  alpha = true;
85 
86  if (alpha) {
87  m_premult = true;
88  surfaceDesc.caps = DSCAPS_PREMULTIPLIED;
89  surfaceDesc.pixelformat = QDirectFbBlitter::alphaPixmapFormat();
90  surfaceDesc.flags = DFBSurfaceDescriptionFlags(DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_CAPS | DSDESC_PIXELFORMAT);
91  } else {
92  surfaceDesc.flags = DFBSurfaceDescriptionFlags(DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT);
93  surfaceDesc.pixelformat = QDirectFbBlitter::pixmapFormat();
94  }
95 
96  IDirectFB *dfb = QDirectFbConvenience::dfbInterface();
97  dfb->CreateSurface(dfb , &surfaceDesc, m_surface.outPtr());
98  m_surface->Clear(m_surface.data(), 0, 0, 0, 0);
99 }
100 
102 {
103  unlock();
104 }
105 
106 DFBSurfacePixelFormat QDirectFbBlitter::alphaPixmapFormat()
107 {
108  return DSPF_ARGB;
109 }
110 
111 DFBSurfacePixelFormat QDirectFbBlitter::pixmapFormat()
112 {
113  return DSPF_RGB32;
114 }
115 
116 DFBSurfacePixelFormat QDirectFbBlitter::selectPixmapFormat(bool withAlpha)
117 {
118  return withAlpha ? alphaPixmapFormat() : pixmapFormat();
119 }
120 
121 void QDirectFbBlitter::fillRect(const QRectF &rect, const QColor &color)
122 {
124 }
125 
126 void QDirectFbBlitter::drawPixmap(const QRectF &rect, const QPixmap &pixmap, const QRectF &srcRect)
127 {
128  drawPixmapOpacity(rect, pixmap, srcRect, QPainter::CompositionMode_SourceOver, 1.0);
129 }
130 
132 {
133  int x, y, w, h;
134  DFBResult result;
135 
136  // check paramters
137  rect.toRect().getRect(&x, &y ,&w, &h);
138  if ((w <= 0) || (h <= 0)) return;
139 
140  if ((cmode == QPainter::CompositionMode_Source) || (color.alpha() == 255)) {
141  // CompositionMode_Source case or CompositionMode_SourceOver with opaque color
142 
143  m_surface->SetDrawingFlags(m_surface.data(),
144  DFBSurfaceDrawingFlags(m_premult ? (DSDRAW_NOFX | DSDRAW_SRC_PREMULTIPLY) : DSDRAW_NOFX));
145  m_surface->SetPorterDuff(m_surface.data(), DSPD_SRC);
146 
147  } else {
148  // CompositionMode_SourceOver case
149 
150  // check if operation is useless
151  if (color.alpha() == 0)
152  return;
153 
154  m_surface->SetDrawingFlags(m_surface.data(),
155  DFBSurfaceDrawingFlags(m_premult ? (DSDRAW_BLEND | DSDRAW_SRC_PREMULTIPLY) : DSDRAW_BLEND));
156  m_surface->SetPorterDuff(m_surface.data(), DSPD_SRC_OVER);
157  }
158 
159  // set color
160  m_surface->SetColor(m_surface.data(), color.red(), color.green(), color.blue(), color.alpha());
161 
162  // perform fill
163  result = m_surface->FillRectangle(m_surface.data(), x, y, w, h);
164  if (result != DFB_OK)
165  DirectFBError("QDirectFBBlitter::alphaFillRect()", result);
166 }
167 
168 void QDirectFbBlitter::drawPixmapOpacity(const QRectF &rect, const QPixmap &pixmap, const QRectF &subrect, QPainter::CompositionMode cmode, qreal opacity)
169 {
170  QRect sQRect = subrect.toRect();
171  QRect dQRect = rect.toRect();
172  DFBRectangle sRect = { sQRect.x(), sQRect.y(), sQRect.width(), sQRect.height() };
173  DFBRectangle dRect = { dQRect.x(), dQRect.y(), dQRect.width(), dQRect.height() };
174  DFBResult result;
175 
176  // skip if dst too small
177  if ((dRect.w <= 0) || (dRect.h <= 0)) return;
178 
179  // correct roundings if needed
180  if (sRect.w <= 0) sRect.w = 1;
181  if (sRect.h <= 0) sRect.h = 1;
182 
183  QBlittablePixmapData *blitPm = static_cast<QBlittablePixmapData*>(pixmap.pixmapData());
184  QDirectFbBlitter *dfbBlitter = static_cast<QDirectFbBlitter *>(blitPm->blittable());
185  dfbBlitter->unlock();
186 
187  IDirectFBSurface *s = dfbBlitter->m_surface.data();
188 
189  DFBSurfaceBlittingFlags blittingFlags = DFBSurfaceBlittingFlags(DSBLIT_BLEND_ALPHACHANNEL);
190  DFBSurfacePorterDuffRule porterDuff = (cmode == QPainter::CompositionMode_SourceOver) ? DSPD_SRC_OVER : DSPD_SRC;
191 
192  if (opacity != 1.0)
193  {
194  blittingFlags = DFBSurfaceBlittingFlags(blittingFlags | DSBLIT_BLEND_COLORALPHA | (m_premult ? DSBLIT_SRC_PREMULTCOLOR : 0));
195  m_surface->SetColor(m_surface.data(), 0xff, 0xff, 0xff, (u8) (opacity * 255.0));
196  }
197 
198  m_surface->SetBlittingFlags(m_surface.data(), DFBSurfaceBlittingFlags(blittingFlags));
199  m_surface->SetPorterDuff(m_surface.data(), porterDuff);
200 
202  m_surface->SetDstBlendFunction(m_surface.data(), DSBF_INVSRCALPHA);
203 
204  if ((sRect.w == dRect.w) && (sRect.h == dRect.h))
205  result = m_surface->Blit(m_surface.data(), s, &sRect, dRect.x, dRect.y);
206  else
207  result = m_surface->StretchBlit(m_surface.data(), s, &sRect, &dRect);
208 
209  if (result != DFB_OK)
210  DirectFBError("QDirectFBBlitter::drawPixmapExtended()", result);
211 }
212 
214 {
216  Q_ASSERT(size().isValid());
217 
218  void *mem;
219  int bpl;
220  const DFBResult result = m_surface->Lock(m_surface.data(), DFBSurfaceLockFlags(DSLF_WRITE|DSLF_READ), static_cast<void**>(&mem), &bpl);
221  if (result == DFB_OK) {
222  DFBSurfacePixelFormat dfbFormat;
223  DFBSurfaceCapabilities dfbCaps;
224  m_surface->GetPixelFormat(m_surface.data(), &dfbFormat);
225  m_surface->GetCapabilities(m_surface.data(), &dfbCaps);
227  int w, h;
228  m_surface->GetSize(m_surface.data(), &w, &h);
229  m_image = QImage(static_cast<uchar *>(mem),w,h,bpl,format);
230  } else {
231  DirectFBError("Failed to lock image", result);
232  }
233 
234  return &m_image;
235 }
236 
237 bool QDirectFbBlitterPlatformPixmap::fromDataBufferDescription(const DFBDataBufferDescription &dataBufferDescription)
238 {
239  DFBResult result;
240  IDirectFB *dfb = QDirectFbConvenience::dfbInterface();
241 
242  // Create a data buffer
244  result = dfb->CreateDataBuffer(dfb, &dataBufferDescription, dataBuffer.outPtr());
245  if (result != DFB_OK) {
246  DirectFBError(QDFB_PRETTY, result);
247  return false;
248  }
249 
250  // Create the image provider
252  result = dataBuffer->CreateImageProvider(dataBuffer.data(), provider.outPtr());
253  if (result != DFB_OK) {
254  DirectFBError(QDFB_PRETTY, result);
255  return false;
256  }
257 
258  // Extract image information
259  DFBImageDescription imageDescription;
260  result = provider->GetImageDescription(provider.data(), &imageDescription);
261  if (result != DFB_OK) {
262  DirectFBError(QDFB_PRETTY, result);
263  return false;
264  }
265 
266  // Can we handle this directlu?
267  if (imageDescription.caps & DICAPS_COLORKEY)
268  return false;
269 
270  DFBSurfaceDescription surfaceDescription;
271  result = provider->GetSurfaceDescription(provider.data(), &surfaceDescription);
272  if (result != DFB_OK) {
273  DirectFBError(QDFB_PRETTY, result);
274  return false;
275  }
276 
277  m_alpha = imageDescription.caps & DICAPS_ALPHACHANNEL;
278  resize(surfaceDescription.width, surfaceDescription.height);
279  // TODO: FIXME; update d
280 
281 
282  result = provider->RenderTo(provider.data(), dfbBlitter()->dfbSurface(), 0);
283  if (result != DFB_OK) {
284  DirectFBError(QDFB_PRETTY, result);
285  return false;
286  }
287 
288  return true;
289 }
290 
291 bool QDirectFbBlitterPlatformPixmap::fromFile(const QString &filename, const char *format,
292  Qt::ImageConversionFlags flags)
293 {
294  // If we can't find the file, pass it on to the base class as it is
295  // trying harder by appending various extensions to the path.
296  if (!QFile::exists(filename))
297  return QBlittablePixmapData::fromFile(filename, format, flags);
298 
299  // Stop if there is a requirement for colors
300  if (flags != Qt::AutoColor)
301  return QBlittablePixmapData::fromFile(filename, format, flags);
302 
303  // Deal with resources
304  if (filename.startsWith(QLatin1Char(':')))
305  return QBlittablePixmapData::fromFile(filename, format, flags);
306 
307  // Try to use directfb to load it.
308  DFBDataBufferDescription description;
309  description.flags = DBDESC_FILE;
310  const QByteArray fileNameData = filename.toLocal8Bit();
311  description.file = fileNameData.constData();
312  if (fromDataBufferDescription(description))
313  return true;
314 
315  // Fallback
316  return QBlittablePixmapData::fromFile(filename, format, flags);
317 }
318 
320 {
321  m_surface->Unlock(m_surface.data());
322 }
323 
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
virtual bool fromFile(const QString &filename, const char *format, Qt::ImageConversionFlags flags)
Format
The following image formats are available in Qt.
Definition: qimage.h:91
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
CompositionMode
Defines the modes supported for digital image compositing.
Definition: qpainter.h:138
T * data() const
Returns the value of the pointer referenced by this object.
virtual QImage * doLock()
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static QBlittable::Capabilities dfb_blitter_capabilities()
void drawPixmapOpacity(const QRectF &rect, const QPixmap &pixmap, const QRectF &subrect, QPainter::CompositionMode cmode, qreal opacity)
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
#define QDFB_PRETTY
ushort red
Returns the red color component of this color.
Definition: qcolor.h:243
static QImage::Format imageFormatFromSurfaceFormat(const DFBSurfacePixelFormat format, const DFBSurfaceCapabilities caps)
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
virtual bool fromFile(const QString &filename, const char *format, Qt::ImageConversionFlags flags)
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual void fillRect(const QRectF &rect, const QColor &color)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
int width() const
Returns the width.
Definition: qsize.h:126
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
QDirectFbBlitter(const QSize &size, IDirectFBSurface *surface)
virtual void drawPixmap(const QRectF &rect, const QPixmap &pixmap, const QRectF &subrect)
static DFBSurfacePixelFormat pixmapFormat()
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
QRect toRect() const
Returns a QRect based on the values of this rectangle.
Definition: qrect.h:845
void alphaFillRect(const QRectF &rect, const QColor &color, QPainter::CompositionMode cmode)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
virtual void doUnlock()
ushort blue
Returns the blue color component of this color.
Definition: qcolor.h:245
ushort alpha
Returns the alpha color component of this color.
Definition: qcolor.h:242
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
void unlock()
Definition: qblittable.cpp:100
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
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
static IDirectFB * dfbInterface()
void getRect(int *x, int *y, int *w, int *h) const
Extracts the position of the rectangle&#39;s top-left corner to *x and *y, and its dimensions to *width a...
Definition: qrect.h:392
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
QSize size() const
Definition: qblittable.cpp:77
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
bool fromDataBufferDescription(const DFBDataBufferDescription &)
QBlittable * blittable() const
static DFBSurfacePixelFormat selectPixmapFormat(bool withAlpha)
static DFBSurfacePixelFormat alphaPixmapFormat()
ushort green
Returns the green color component of this color.
Definition: qcolor.h:244
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QDirectFBPointer< IDirectFBSurface > m_surface
QPixmapData * pixmapData() const
Definition: qpixmap.cpp:2277