Qt 4.8
qmeegolivepixmapdata.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 "qmeegolivepixmapdata.h"
43 #include "qmeegorasterpixmapdata.h"
44 #include <private/qimage_p.h>
45 #include <private/qwindowsurface_gl_p.h>
46 #include <private/qeglcontext_p.h>
47 #include <private/qapplication_p.h>
48 #include <private/qgraphicssystem_runtime_p.h>
49 #include <private/qpixmap_x11_p.h>
50 #include <stdio.h>
51 
53 
54 static EGLint lock_attribs[] = {
57  EGL_NONE
58 };
59 
60 static EGLint preserved_attribs[] = {
61  EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
62  EGL_NONE
63 };
64 
65 // as copied from qwindowsurface.cpp
66 void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset)
67 {
68  // make sure we don't detach
69  uchar *mem = const_cast<uchar*>(const_cast<const QImage &>(img).bits());
70 
71  int lineskip = img.bytesPerLine();
72  int depth = img.depth() >> 3;
73 
74  const QRect imageRect(0, 0, img.width(), img.height());
75  const QRect r = rect & imageRect & imageRect.translated(-offset);
76  const QPoint p = rect.topLeft() + offset;
77 
78  if (r.isEmpty())
79  return;
80 
81  const uchar *src;
82  uchar *dest;
83 
84  if (r.top() < p.y()) {
85  src = mem + r.bottom() * lineskip + r.left() * depth;
86  dest = mem + (p.y() + r.height() - 1) * lineskip + p.x() * depth;
87  lineskip = -lineskip;
88  } else {
89  src = mem + r.top() * lineskip + r.left() * depth;
90  dest = mem + p.y() * lineskip + p.x() * depth;
91  }
92 
93  const int w = r.width();
94  int h = r.height();
95  const int bytes = w * depth;
96 
97  // overlapping segments?
98  if (offset.y() == 0 && qAbs(offset.x()) < w) {
99  do {
100  ::memmove(dest, src, bytes);
101  dest += lineskip;
102  src += lineskip;
103  } while (--h);
104  } else {
105  do {
106  ::memcpy(dest, src, bytes);
107  dest += lineskip;
108  src += lineskip;
109  } while (--h);
110  }
111 }
112 
113 /* Public */
114 
116 {
117  QImage image(w, h, format);
119  pmd->fromImage(image, Qt::NoOpaqueDetection);
120  backingX11Pixmap = new QPixmap(pmd);
121 
123 
124  pos = all_live_pixmaps.insert(all_live_pixmaps.begin(), this);
125 }
126 
128 {
129  backingX11Pixmap = new QPixmap(QPixmap::fromX11Pixmap(h));
131 
132  pos = all_live_pixmaps.insert(all_live_pixmaps.begin(), this);
133 }
134 
136 {
137  delete backingX11Pixmap;
138  all_live_pixmaps.erase(pos);
139 }
140 
142 {
143  if (texture()->id != 0)
144  return;
145 
148 
149  EGLImageKHR eglImage = EGL_NO_IMAGE_KHR;
150  GLuint newTextureId = 0;
151 
152  eglImage = QEgl::eglCreateImageKHR(QEgl::display(), EGL_NO_CONTEXT, EGL_NATIVE_PIXMAP_KHR,
154 
155  if (eglImage == EGL_NO_IMAGE_KHR) {
156  qWarning("eglCreateImageKHR failed (live texture)!");
157  return;
158  }
159 
160  glGenTextures(1, &newTextureId);
161  glBindTexture(GL_TEXTURE_2D, newTextureId);
162 
164  if (glGetError() == GL_NO_ERROR) {
166  texture()->id = newTextureId;
167  texture()->options &= ~QGLContext::InvertedYBindOption;
169  } else {
170  qWarning("Failed to create a texture from an egl image (live texture)!");
171  glDeleteTextures(1, &newTextureId);
172  }
173 
175 }
176 
178 {
179  qWarning("Create compatible called on live pixmap! Expect fail soon...");
180  return new QMeeGoRasterPixmapData(pixelType());
181 }
182 
184 {
187 
188  if (fenceSync) {
190  fenceSync,
193  }
194 
195  void *data = 0;
196  int pitch = 0;
197  int surfaceWidth = 0;
198  int surfaceHeight = 0;
199  EGLSurface surface = 0;
201  lockedImage = QImage();
202 
203  surface = getSurfaceForBackingPixmap();
205  qWarning("Failed to lock surface (live texture)!");
206  return &lockedImage;
207  }
208 
209  eglQuerySurface(QEgl::display(), surface, EGL_BITMAP_POINTER_KHR, (EGLint*) &data);
210  eglQuerySurface(QEgl::display(), surface, EGL_BITMAP_PITCH_KHR, (EGLint*) &pitch);
211  eglQuerySurface(QEgl::display(), surface, EGL_WIDTH, (EGLint*) &surfaceWidth);
212  eglQuerySurface(QEgl::display(), surface, EGL_HEIGHT, (EGLint*) &surfaceHeight);
213 
214  // Ok, here we know we just support those two formats. Real solution would be:
215  // query also the format.
216  if (backingX11Pixmap->depth() > 16)
218  else
219  format = QImage::Format_RGB16;
220 
221  if (data == NULL || pitch == 0) {
222  qWarning("Failed to query the live texture!");
223  return &lockedImage;
224  }
225 
226  if (width() != surfaceWidth || height() != surfaceHeight) {
227  qWarning("Live texture dimensions don't match!");
229  return &lockedImage;
230  }
231 
232  lockedImage = QImage((uchar *) data, width(), height(), pitch, format);
233  return &lockedImage;
234 }
235 
237 {
240 
242  lockedImage = QImage();
243  return true;
244  } else {
245  lockedImage = QImage();
246  return false;
247  }
248 }
249 
251 {
252  return backingX11Pixmap->handle();
253 }
254 
255 bool QMeeGoLivePixmapData::scroll(int dx, int dy, const QRect &rect)
256 {
257  lock(NULL);
258 
259  if (!lockedImage.isNull())
260  qt_scrollRectInImage(lockedImage, rect, QPoint(dx, dy));
261 
263  return true;
264 }
265 
267 {
269 
270  // This code is a crative remix of the stuff that can be found in the
271  // Qt's TFP implementation in /src/opengl/qgl_x11egl.cpp ::bindiTextureFromNativePixmap
272  QX11PixmapData *pixmapData = static_cast<QX11PixmapData*>(backingX11Pixmap->data_ptr().data());
273  Q_ASSERT(pixmapData->classId() == QPixmapData::X11Class);
274  bool hasAlpha = pixmapData->hasAlphaChannel();
275 
276  if (pixmapData->gl_surface &&
277  hasAlpha == (pixmapData->flags & QX11PixmapData::GlSurfaceCreatedWithAlpha))
278  return pixmapData->gl_surface;
279 
280  // Check to see if the surface is still valid
281  if (pixmapData->gl_surface &&
282  hasAlpha != ((pixmapData->flags & QX11PixmapData::GlSurfaceCreatedWithAlpha) > 0)) {
283  // Surface is invalid!
284  destroySurfaceForPixmapData(pixmapData);
285  }
286 
287  if (pixmapData->gl_surface == 0) {
288  EGLConfig config = QEgl::defaultConfig(QInternal::Pixmap,
289  QEgl::OpenGL,
290  hasAlpha ? QEgl::Translucent : QEgl::NoOptions);
291 
292  pixmapData->gl_surface = (void*)QEgl::createSurface(backingX11Pixmap, config);
293 
294  if (hasAlpha)
296  else
297  pixmapData->flags &= ~QX11PixmapData::GlSurfaceCreatedWithAlpha;
298 
299  if (pixmapData->gl_surface == (void*)EGL_NO_SURFACE)
300  return NULL;
301  }
302 
303  return pixmapData->gl_surface;
304 }
305 
307 {
309  QX11PixmapData *pixmapData = static_cast<QX11PixmapData*>(pmd);
310  if (pixmapData->gl_surface) {
311  eglDestroySurface(QEgl::display(), (EGLSurface)pixmapData->gl_surface);
312  pixmapData->gl_surface = 0;
313  }
314 }
315 
317 {
318  foreach (QMeeGoLivePixmapData *data, all_live_pixmaps) {
319  QX11PixmapData *pixmapData = static_cast<QX11PixmapData*>(data->backingX11Pixmap->data_ptr().data());
320  *data->texture() = QGLTexture();
321  pixmapData->gl_surface = 0;
322  }
323 }
DataPtr & data_ptr()
Definition: qpixmap.h:297
#define EGL_WRITE_SURFACE_BIT_KHR
iterator erase(iterator pos)
Removes the item pointed to by the iterator pos from the list, and returns an iterator to the next it...
Definition: qlinkedlist.h:470
Format
The following image formats are available in Qt.
Definition: qimage.h:91
int width() const
Returns the width of the pixmap.
Definition: qpixmap.cpp:630
bool isNull() const
Returns true if it is a null image, otherwise returns false.
Definition: qimage.cpp:1542
Q_OPENGL_EXPORT QGLWidget * qt_gl_share_widget()
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlinkedlist.h:182
static EGLint preserved_attribs[]
int width() const
QRect translated(int dx, int dy) const
Returns a copy of the rectangle that is translated dx along the x axis and dy along the y axis...
Definition: qrect.h:328
#define EGL_NO_IMAGE_KHR
Definition: qegl_p.h:169
friend class QPixmap
Qt::HANDLE handle() const
Returns the pixmap&#39;s handle to the device context.
Definition: qpixmap.cpp:1299
#define EGL_FOREVER_KHR
int bytesPerLine() const
Returns the number of bytes per image scanline.
Definition: qimage.cpp:1812
int depth() const
Returns the depth of the pixmap.
Definition: qpixmap.cpp:695
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void fromImage(const QImage &image, Qt::ImageConversionFlags flags)
#define EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
static bool eglUnlockSurfaceKHR(EGLDisplay display, EGLSurface surface)
QGLContext::BindOptions options
Definition: qgl_p.h:611
#define GL_TEXTURE_2D
QImage * lock(EGLSyncKHR fenceSync)
QMeeGoLivePixmapDataList::Iterator pos
bool scroll(int dx, int dy, const QRect &rect)
PixelType pixelType() const
#define EGL_BITMAP_POINTER_KHR
The QLinkedList class is a template class that provides linked lists.
Definition: qdatastream.h:63
unsigned char uchar
Definition: qglobal.h:994
QGLTexture * texture() const
void destroySurfaceForPixmapData(QPixmapData *pmd)
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
Q_GUI_EXPORT EGLDisplay display()
Definition: qegl.cpp:589
QPixmapData * createCompatiblePixmapData() const
Q_GUI_EXPORT EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
Definition: qegl.cpp:638
GLuint id
Definition: qgl_p.h:608
The QGLContext class encapsulates an OpenGL rendering context.
Definition: qgl.h:310
#define EGL_MAP_PRESERVE_PIXELS_KHR
void resize(int width, int height)
#define EGL_READ_SURFACE_BIT_KHR
#define EGL_NATIVE_PIXMAP_KHR
Definition: qegl_p.h:180
EGLSurface getSurfaceForBackingPixmap()
Q_CORE_EXPORT void qWarning(const char *,...)
void * EGLClientBuffer
Definition: qegl_p.h:70
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)
void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset)
ClassId classId() const
friend class QX11PixmapData
int depth() const
Returns the depth of the image.
Definition: qimage.cpp:1620
void * HANDLE
Definition: qnamespace.h:1671
Q_GUI_EXPORT EGLConfig defaultConfig(int devType, API api, ConfigOptions options)
Definition: qegl.cpp:126
void * EGLImageKHR
Definition: qegl_p.h:165
#define EGL_IMAGE_PRESERVED_KHR
Definition: qegl_p.h:172
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
QMeeGoLivePixmapData(int w, int h, QImage::Format format)
static EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout)
Q_GUI_EXPORT EGLSurface createSurface(QPaintDevice *device, EGLConfig cfg, const QEglProperties *surfaceAttribs=0)
Definition: qegl_x11.cpp:333
static void ensureInitialized()
bool hasAlphaChannel() const
#define ctx
Definition: qgl.cpp:6094
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
void * EGLSyncKHR
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
#define EGL_LOCK_USAGE_HINT_KHR
int height() const
Returns the height of the pixmap.
Definition: qpixmap.cpp:645
int height() const
static QMeeGoLivePixmapDataList all_live_pixmaps
static bool eglLockSurfaceKHR(EGLDisplay display, EGLSurface surface, const EGLint *attrib_list)
static EGLint lock_attribs[]
iterator insert(iterator before, const T &t)
Inserts value in front of the item pointed to by the iterator before.
Definition: qlinkedlist.h:447
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
Q_GUI_EXPORT EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list)
Definition: qegl.cpp:625
#define EGL_BITMAP_PITCH_KHR
bool hasAlphaChannel() const
Returns true if the pixmap has a format that respects the alpha channel, otherwise returns false...
Definition: qpixmap.cpp:1965
#define GL_NO_ERROR
#define glEGLImageTargetTexture2DOES
QPoint topLeft() const
Returns the position of the rectangle&#39;s top-left corner.
Definition: qrect.h:288