Qt 4.8
qmeegopixmapdata.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 "qmeegopixmapdata.h"
43 #include "qmeegoextensions.h"
44 #include "qmeegorasterpixmapdata.h"
45 #include <private/qimage_p.h>
46 #include <private/qwindowsurface_gl_p.h>
47 #include <private/qeglcontext_p.h>
48 #include <private/qapplication_p.h>
49 #include <private/qgraphicssystem_runtime_p.h>
50 
51 // from dithering.cpp
52 extern unsigned short* convertRGB32_to_RGB565(const unsigned char *in, int width, int height, int stride);
53 extern unsigned short* convertARGB32_to_RGBA4444(const unsigned char *in, int width, int height, int stride);
54 extern unsigned char* convertBGRA32_to_RGBA32(const unsigned char *in, int width, int height, int stride);
55 
56 static EGLint preserved_image_attribs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
57 
59 
60 /* Public */
61 
63 {
64 }
65 
66 void QMeeGoPixmapData::fromTexture(GLuint textureId, int w, int h, bool alpha)
67 {
68  resize(w, h);
69  texture()->id = textureId;
70  m_hasAlpha = alpha;
71  softImage = QImage();
72 }
73 
75 {
76  return softImage;
77 }
78 
80  Qt::ImageConversionFlags flags)
81 {
82  void *rawResource = static_cast <void *> (((QImage &) image).data_ptr()->data);
83 
84  if (sharedImagesMap.contains(rawResource)) {
85  QMeeGoImageInfo *info = sharedImagesMap.value(rawResource);
86  fromEGLSharedImage(info->handle, image);
87  } else {
88  // This should *never* happen since the graphics system should never
89  // create a QMeeGoPixmapData for an origin that doesn't contain a raster
90  // image we know about. But...
91  qWarning("QMeeGoPixmapData::fromImage called on non-know resource. Falling back...");
92  QGLPixmapData::fromImage(image, flags);
93  }
94 }
95 
97 {
98  if (si.isNull())
99  qFatal("Trying to build pixmap with an empty/null softimage!");
100 
102 
104 
105  bool textureIsBound = false;
106  GLuint newTextureId;
107  GLint newWidth, newHeight;
108 
109  glGenTextures(1, &newTextureId);
110  glBindTexture(GL_TEXTURE_2D, newTextureId);
111 
114 
115  if (image != EGL_NO_IMAGE_KHR) {
117  GLint err = glGetError();
118  if (err == GL_NO_ERROR)
119  textureIsBound = true;
120 
121  QMeeGoExtensions::eglQueryImageNOK(QEgl::display(), image, EGL_WIDTH, &newWidth);
122  QMeeGoExtensions::eglQueryImageNOK(QEgl::display(), image, EGL_HEIGHT, &newHeight);
123 
125  }
126 
127  if (textureIsBound) {
128  fromTexture(newTextureId, newWidth, newHeight,
129  (si.hasAlphaChannel() && const_cast<QImage &>(si).data_ptr()->checkForAlphaPixels()));
130  texture()->options &= ~QGLContext::InvertedYBindOption;
131  softImage = si;
133  } else {
134  qWarning("Failed to create a texture from a shared image!");
135  glDeleteTextures(1, &newTextureId);
136  }
137 }
138 
140 {
142 
144 
145  GLuint textureId;
146 
147  glGenTextures(1, &textureId);
148  glBindTexture(GL_TEXTURE_2D, textureId);
149  if (image.hasAlphaChannel() && const_cast<QImage &>(image).data_ptr()->checkForAlphaPixels()) {
150  void *converted = convertBGRA32_to_RGBA32(image.bits(), image.width(), image.height(), image.bytesPerLine());
151  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, converted);
152  free(converted);
153  } else {
154  void *converted = convertRGB32_to_RGB565(image.bits(), image.width(), image.height(), image.bytesPerLine());
155  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width(), image.height(), 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, converted);
156  free(converted);
157  }
158 
161 
164 
167  (EGLClientBuffer) textureId,
169  glDeleteTextures(1, &textureId);
170  if (eglimage) {
173  return (Qt::HANDLE) handle;
174  } else {
175  qWarning("Failed to create shared image from pixmap/texture!");
176  return 0;
177  }
178 }
179 
181 {
182  // FIXME That's broken with recent 16bit textures changes.
183  m_dirty = true;
185  ensureCreated();
186 
187  if (softImage.width() != w || softImage.height() != h)
188  qWarning("Ooops, looks like softImage changed dimensions since last updated! Corruption ahead?!");
189 }
190 
192 {
195 
196  QMutableHashIterator <void*, QMeeGoImageInfo*> i(sharedImagesMap);
197  while (i.hasNext()) {
198  i.next();
199  if (i.value()->handle == h)
200  i.remove();
201  }
202 
204 }
205 
207 {
208  void *raw = static_cast <void *> (((QImage) si).data_ptr()->data);
210 
211  if (! sharedImagesMap.contains(raw)) {
212  info = new QMeeGoImageInfo;
213  info->handle = handle;
214  info->rawFormat = si.format();
215  sharedImagesMap.insert(raw, info);
216  } else {
217  info = sharedImagesMap.value(raw);
218  if (info->handle != handle || info->rawFormat != si.format())
219  qWarning("Inconsistency detected: overwriting entry in sharedImagesMap but handle/format different");
220  }
221 }
222 
224 {
225  return new QMeeGoRasterPixmapData(pixelType());
226 }
static void registerSharedImage(Qt::HANDLE handle, const QImage &si)
#define GL_RGB
#define GL_CLAMP_TO_EDGE
Definition: glfunctions.h:62
#define GL_TEXTURE_MIN_FILTER
virtual void fromImage(const QImage &image, Qt::ImageConversionFlags flags)
static mach_timebase_info_data_t info
unsigned short * convertARGB32_to_RGBA4444(const unsigned char *in, int width, int height, int stride)
Definition: dithering.cpp:152
static bool eglDestroySharedImageNOK(EGLDisplay dpy, EGLNativeSharedImageTypeNOK img)
static EGLint preserved_image_attribs[]
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()
unsigned short * convertRGB32_to_RGB565(const unsigned char *in, int width, int height, int stride)
Definition: dithering.cpp:84
#define EGL_NO_IMAGE_KHR
Definition: qegl_p.h:169
#define GL_TEXTURE_WRAP_S
bool hasAlphaChannel() const
Returns true if the image has a format that respects the alpha channel, otherwise returns false...
Definition: qimage.cpp:6495
int bytesPerLine() const
Returns the number of bytes per image scanline.
Definition: qimage.cpp:1812
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
void fromTexture(GLuint textureId, int w, int h, bool alpha)
QGLContext::BindOptions options
Definition: qgl_p.h:611
#define GL_TEXTURE_2D
Format format() const
Returns the format of the image.
Definition: qimage.cpp:2305
void * EGLNativeSharedImageTypeNOK
PixelType pixelType() const
QGLTexture * texture() const
#define GL_UNSIGNED_SHORT_5_6_5
static bool destroyEGLSharedImage(Qt::HANDLE h)
Q_GUI_EXPORT EGLDisplay display()
Definition: qegl.cpp:589
QPixmapData * createCompatiblePixmapData() const
void ensureCreated() const
static bool eglQueryImageNOK(EGLDisplay dpy, EGLImageKHR image, EGLint prop, EGLint *v)
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
void resize(int width, int height)
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 QHash< void *, QMeeGoImageInfo * > sharedImagesMap
void * HANDLE
Definition: qnamespace.h:1671
unsigned char * convertBGRA32_to_RGBA32(const unsigned char *in, int width, int height, int stride)
Definition: dithering.cpp:250
void * EGLImageKHR
Definition: qegl_p.h:165
#define GL_TEXTURE_WRAP_T
uchar * bits()
Returns a pointer to the first pixel data.
Definition: qimage.cpp:1946
Q_CORE_EXPORT void qFatal(const char *,...)
#define EGL_IMAGE_PRESERVED_KHR
Definition: qegl_p.h:172
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
static QEglContext * currentContext(QEgl::API api)
Definition: qegl.cpp:799
static Qt::HANDLE imageToEGLSharedImage(const QImage &image)
static void ensureInitialized()
#define GL_UNSIGNED_BYTE
void * data_ptr(const QTransform &t)
Definition: qpainter_p.h:81
#define ctx
Definition: qgl.cpp:6094
virtual QImage toImage() const
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
typedef GLint
Definition: glfunctions.h:67
virtual void fromEGLSharedImage(Qt::HANDLE handle, const QImage &softImage)
#define GL_RGBA
Q_GUI_EXPORT EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list)
Definition: qegl.cpp:625
#define GL_TEXTURE_MAG_FILTER
void fromImage(const QImage &image, Qt::ImageConversionFlags flags)
virtual void updateFromSoftImage()
static EGLNativeSharedImageTypeNOK eglCreateSharedImageNOK(EGLDisplay dpy, EGLImageKHR image, EGLint *props)
#define EGL_SHARED_IMAGE_NOK
#define GL_NO_ERROR
#define EGL_GL_TEXTURE_2D_KHR
#define glEGLImageTargetTexture2DOES
#define GL_NEAREST