| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the plugins of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 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 |
|
|---|
| 58 | QHash <void*, QMeeGoImageInfo*> QMeeGoPixmapData::sharedImagesMap;
|
|---|
| 59 |
|
|---|
| 60 | /* Public */
|
|---|
| 61 |
|
|---|
| 62 | QMeeGoPixmapData::QMeeGoPixmapData() : QGLPixmapData(QPixmapData::PixmapType)
|
|---|
| 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 |
|
|---|
| 74 | QImage QMeeGoPixmapData::toImage() const
|
|---|
| 75 | {
|
|---|
| 76 | return softImage;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | void QMeeGoPixmapData::fromImage(const QImage &image,
|
|---|
| 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 |
|
|---|
| 96 | void QMeeGoPixmapData::fromEGLSharedImage(Qt::HANDLE handle, const QImage &si)
|
|---|
| 97 | {
|
|---|
| 98 | if (si.isNull())
|
|---|
| 99 | qFatal("Trying to build pixmap with an empty/null softimage!");
|
|---|
| 100 |
|
|---|
| 101 | QGLShareContextScope ctx(qt_gl_share_widget()->context());
|
|---|
| 102 |
|
|---|
| 103 | QMeeGoExtensions::ensureInitialized();
|
|---|
| 104 |
|
|---|
| 105 | bool textureIsBound = false;
|
|---|
| 106 | GLuint newTextureId;
|
|---|
| 107 | GLint newWidth, newHeight;
|
|---|
| 108 |
|
|---|
| 109 | glGenTextures(1, &newTextureId);
|
|---|
| 110 | glBindTexture(GL_TEXTURE_2D, newTextureId);
|
|---|
| 111 |
|
|---|
| 112 | EGLImageKHR image = QEgl::eglCreateImageKHR(QEgl::display(), EGL_NO_CONTEXT, EGL_SHARED_IMAGE_NOK,
|
|---|
| 113 | (EGLClientBuffer)handle, preserved_image_attribs);
|
|---|
| 114 |
|
|---|
| 115 | if (image != EGL_NO_IMAGE_KHR) {
|
|---|
| 116 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
|
|---|
| 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 |
|
|---|
| 124 | QEgl::eglDestroyImageKHR(QEgl::display(), image);
|
|---|
| 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;
|
|---|
| 132 | QMeeGoPixmapData::registerSharedImage(handle, softImage);
|
|---|
| 133 | } else {
|
|---|
| 134 | qWarning("Failed to create a texture from a shared image!");
|
|---|
| 135 | glDeleteTextures(1, &newTextureId);
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | Qt::HANDLE QMeeGoPixmapData::imageToEGLSharedImage(const QImage &image)
|
|---|
| 140 | {
|
|---|
| 141 | QGLShareContextScope ctx(qt_gl_share_widget()->context());
|
|---|
| 142 |
|
|---|
| 143 | QMeeGoExtensions::ensureInitialized();
|
|---|
| 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 |
|
|---|
| 159 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|---|
| 160 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|---|
| 161 |
|
|---|
| 162 | glBindTexture(GL_TEXTURE_2D, textureId);
|
|---|
| 163 | EGLImageKHR eglimage = QEgl::eglCreateImageKHR(QEgl::display(), QEglContext::currentContext(QEgl::OpenGL)->context(),
|
|---|
| 164 | EGL_GL_TEXTURE_2D_KHR,
|
|---|
| 165 | (EGLClientBuffer) textureId,
|
|---|
| 166 | preserved_image_attribs);
|
|---|
| 167 | glDeleteTextures(1, &textureId);
|
|---|
| 168 | if (eglimage) {
|
|---|
| 169 | EGLNativeSharedImageTypeNOK handle = QMeeGoExtensions::eglCreateSharedImageNOK(QEgl::display(), eglimage, NULL);
|
|---|
| 170 | QEgl::eglDestroyImageKHR(QEgl::display(), eglimage);
|
|---|
| 171 | return (Qt::HANDLE) handle;
|
|---|
| 172 | } else {
|
|---|
| 173 | qWarning("Failed to create shared image from pixmap/texture!");
|
|---|
| 174 | return 0;
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | void QMeeGoPixmapData::updateFromSoftImage()
|
|---|
| 179 | {
|
|---|
| 180 | // FIXME That's broken with recent 16bit textures changes.
|
|---|
| 181 | m_dirty = true;
|
|---|
| 182 | m_source = softImage;
|
|---|
| 183 | ensureCreated();
|
|---|
| 184 |
|
|---|
| 185 | if (softImage.width() != w || softImage.height() != h)
|
|---|
| 186 | qWarning("Ooops, looks like softImage changed dimensions since last updated! Corruption ahead?!");
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | bool QMeeGoPixmapData::destroyEGLSharedImage(Qt::HANDLE h)
|
|---|
| 190 | {
|
|---|
| 191 | QGLShareContextScope ctx(qt_gl_share_widget()->context());
|
|---|
| 192 | QMeeGoExtensions::ensureInitialized();
|
|---|
| 193 |
|
|---|
| 194 | QMutableHashIterator <void*, QMeeGoImageInfo*> i(sharedImagesMap);
|
|---|
| 195 | while (i.hasNext()) {
|
|---|
| 196 | i.next();
|
|---|
| 197 | if (i.value()->handle == h)
|
|---|
| 198 | i.remove();
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | return QMeeGoExtensions::eglDestroySharedImageNOK(QEgl::display(), (EGLNativeSharedImageTypeNOK) h);
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | void QMeeGoPixmapData::registerSharedImage(Qt::HANDLE handle, const QImage &si)
|
|---|
| 205 | {
|
|---|
| 206 | void *raw = static_cast <void *> (((QImage) si).data_ptr()->data);
|
|---|
| 207 | QMeeGoImageInfo *info;
|
|---|
| 208 |
|
|---|
| 209 | if (! sharedImagesMap.contains(raw)) {
|
|---|
| 210 | info = new QMeeGoImageInfo;
|
|---|
| 211 | info->handle = handle;
|
|---|
| 212 | info->rawFormat = si.format();
|
|---|
| 213 | sharedImagesMap.insert(raw, info);
|
|---|
| 214 | } else {
|
|---|
| 215 | info = sharedImagesMap.value(raw);
|
|---|
| 216 | if (info->handle != handle || info->rawFormat != si.format())
|
|---|
| 217 | qWarning("Inconsistency detected: overwriting entry in sharedImagesMap but handle/format different");
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | QPixmapData *QMeeGoPixmapData::createCompatiblePixmapData() const
|
|---|
| 222 | {
|
|---|
| 223 | return new QMeeGoRasterPixmapData(pixelType());
|
|---|
| 224 | }
|
|---|