| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtOpenVG module 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 qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qpixmapdata_vg_p.h"
|
|---|
| 43 | #include "qpaintengine_vg_p.h"
|
|---|
| 44 | #include <QtGui/private/qdrawhelper_p.h>
|
|---|
| 45 | #include "qvg_p.h"
|
|---|
| 46 | #include "qvgimagepool_p.h"
|
|---|
| 47 |
|
|---|
| 48 | #ifdef QT_SYMBIAN_SUPPORTS_SGIMAGE
|
|---|
| 49 | #include <private/qt_s60_p.h>
|
|---|
| 50 | #include <fbs.h>
|
|---|
| 51 | #include <graphics/sgimage.h>
|
|---|
| 52 | typedef EGLImageKHR (*pfnEglCreateImageKHR)(EGLDisplay, EGLContext, EGLenum, EGLClientBuffer, EGLint*);
|
|---|
| 53 | typedef EGLBoolean (*pfnEglDestroyImageKHR)(EGLDisplay, EGLImageKHR);
|
|---|
| 54 | typedef VGImage (*pfnVgCreateEGLImageTargetKHR)(VGeglImageKHR);
|
|---|
| 55 | #endif // QT_SYMBIAN_SUPPORTS_SGIMAGE
|
|---|
| 56 |
|
|---|
| 57 | QT_BEGIN_NAMESPACE
|
|---|
| 58 |
|
|---|
| 59 | static int qt_vg_pixmap_serial = 0;
|
|---|
| 60 |
|
|---|
| 61 | QVGPixmapData::QVGPixmapData(PixelType type)
|
|---|
| 62 | : QPixmapData(type, OpenVGClass)
|
|---|
| 63 | {
|
|---|
| 64 | Q_ASSERT(type == QPixmapData::PixmapType);
|
|---|
| 65 | vgImage = VG_INVALID_HANDLE;
|
|---|
| 66 | vgImageOpacity = VG_INVALID_HANDLE;
|
|---|
| 67 | cachedOpacity = 1.0f;
|
|---|
| 68 | recreate = true;
|
|---|
| 69 | inImagePool = false;
|
|---|
| 70 | inLRU = false;
|
|---|
| 71 | #if !defined(QT_NO_EGL)
|
|---|
| 72 | context = 0;
|
|---|
| 73 | qt_vg_register_pixmap(this);
|
|---|
| 74 | #endif
|
|---|
| 75 | setSerialNumber(++qt_vg_pixmap_serial);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | QVGPixmapData::~QVGPixmapData()
|
|---|
| 79 | {
|
|---|
| 80 | destroyImageAndContext();
|
|---|
| 81 | #if !defined(QT_NO_EGL)
|
|---|
| 82 | qt_vg_unregister_pixmap(this);
|
|---|
| 83 | #endif
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | void QVGPixmapData::destroyImages()
|
|---|
| 87 | {
|
|---|
| 88 | if (inImagePool) {
|
|---|
| 89 | QVGImagePool *pool = QVGImagePool::instance();
|
|---|
| 90 | if (vgImage != VG_INVALID_HANDLE)
|
|---|
| 91 | pool->releaseImage(this, vgImage);
|
|---|
| 92 | if (vgImageOpacity != VG_INVALID_HANDLE)
|
|---|
| 93 | pool->releaseImage(this, vgImageOpacity);
|
|---|
| 94 | } else {
|
|---|
| 95 | if (vgImage != VG_INVALID_HANDLE)
|
|---|
| 96 | vgDestroyImage(vgImage);
|
|---|
| 97 | if (vgImageOpacity != VG_INVALID_HANDLE)
|
|---|
| 98 | vgDestroyImage(vgImageOpacity);
|
|---|
| 99 | }
|
|---|
| 100 | vgImage = VG_INVALID_HANDLE;
|
|---|
| 101 | vgImageOpacity = VG_INVALID_HANDLE;
|
|---|
| 102 | inImagePool = false;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | void QVGPixmapData::destroyImageAndContext()
|
|---|
| 106 | {
|
|---|
| 107 | if (vgImage != VG_INVALID_HANDLE) {
|
|---|
| 108 | // We need to have a context current to destroy the image.
|
|---|
| 109 | #if !defined(QT_NO_EGL)
|
|---|
| 110 | if (context->isCurrent()) {
|
|---|
| 111 | destroyImages();
|
|---|
| 112 | } else {
|
|---|
| 113 | // We don't currently have a widget surface active, but we
|
|---|
| 114 | // need a surface to make the context current. So use the
|
|---|
| 115 | // shared pbuffer surface instead.
|
|---|
| 116 | context->makeCurrent(qt_vg_shared_surface());
|
|---|
| 117 | destroyImages();
|
|---|
| 118 | context->lazyDoneCurrent();
|
|---|
| 119 | }
|
|---|
| 120 | #else
|
|---|
| 121 | destroyImages();
|
|---|
| 122 | #endif
|
|---|
| 123 | }
|
|---|
| 124 | #if !defined(QT_NO_EGL)
|
|---|
| 125 | if (context) {
|
|---|
| 126 | qt_vg_destroy_context(context, QInternal::Pixmap);
|
|---|
| 127 | context = 0;
|
|---|
| 128 | }
|
|---|
| 129 | #endif
|
|---|
| 130 | recreate = true;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | QPixmapData *QVGPixmapData::createCompatiblePixmapData() const
|
|---|
| 134 | {
|
|---|
| 135 | return new QVGPixmapData(pixelType());
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | bool QVGPixmapData::isValid() const
|
|---|
| 139 | {
|
|---|
| 140 | return (w > 0 && h > 0);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | void QVGPixmapData::resize(int wid, int ht)
|
|---|
| 144 | {
|
|---|
| 145 | if (w == wid && h == ht)
|
|---|
| 146 | return;
|
|---|
| 147 |
|
|---|
| 148 | w = wid;
|
|---|
| 149 | h = ht;
|
|---|
| 150 | d = 32; // We always use ARGB_Premultiplied for VG pixmaps.
|
|---|
| 151 | is_null = (w <= 0 || h <= 0);
|
|---|
| 152 | source = QImage();
|
|---|
| 153 | recreate = true;
|
|---|
| 154 |
|
|---|
| 155 | setSerialNumber(++qt_vg_pixmap_serial);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | void QVGPixmapData::fromImage
|
|---|
| 159 | (const QImage &image, Qt::ImageConversionFlags flags)
|
|---|
| 160 | {
|
|---|
| 161 | if (image.size() == QSize(w, h))
|
|---|
| 162 | setSerialNumber(++qt_vg_pixmap_serial);
|
|---|
| 163 | else
|
|---|
| 164 | resize(image.width(), image.height());
|
|---|
| 165 | source = image.convertToFormat(sourceFormat(), flags);
|
|---|
| 166 | recreate = true;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | void QVGPixmapData::fill(const QColor &color)
|
|---|
| 170 | {
|
|---|
| 171 | if (!isValid())
|
|---|
| 172 | return;
|
|---|
| 173 |
|
|---|
| 174 | if (source.isNull())
|
|---|
| 175 | source = QImage(w, h, sourceFormat());
|
|---|
| 176 |
|
|---|
| 177 | if (source.depth() == 1) {
|
|---|
| 178 | // Pick the best approximate color in the image's colortable.
|
|---|
| 179 | int gray = qGray(color.rgba());
|
|---|
| 180 | if (qAbs(qGray(source.color(0)) - gray) < qAbs(qGray(source.color(1)) - gray))
|
|---|
| 181 | source.fill(0);
|
|---|
| 182 | else
|
|---|
| 183 | source.fill(1);
|
|---|
| 184 | } else {
|
|---|
| 185 | source.fill(PREMUL(color.rgba()));
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | // Re-upload the image to VG the next time toVGImage() is called.
|
|---|
| 189 | recreate = true;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | bool QVGPixmapData::hasAlphaChannel() const
|
|---|
| 193 | {
|
|---|
| 194 | if (!source.isNull())
|
|---|
| 195 | return source.hasAlphaChannel();
|
|---|
| 196 | else
|
|---|
| 197 | return isValid();
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | void QVGPixmapData::setAlphaChannel(const QPixmap &alphaChannel)
|
|---|
| 201 | {
|
|---|
| 202 | forceToImage();
|
|---|
| 203 | source.setAlphaChannel(alphaChannel.toImage());
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | QImage QVGPixmapData::toImage() const
|
|---|
| 207 | {
|
|---|
| 208 | if (!isValid())
|
|---|
| 209 | return QImage();
|
|---|
| 210 |
|
|---|
| 211 | if (source.isNull()) {
|
|---|
| 212 | source = QImage(w, h, sourceFormat());
|
|---|
| 213 | recreate = true;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | return source;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | QImage *QVGPixmapData::buffer()
|
|---|
| 220 | {
|
|---|
| 221 | forceToImage();
|
|---|
| 222 | return &source;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | QPaintEngine* QVGPixmapData::paintEngine() const
|
|---|
| 226 | {
|
|---|
| 227 | // If the application wants to paint into the QPixmap, we first
|
|---|
| 228 | // force it to QImage format and then paint into that.
|
|---|
| 229 | // This is simpler than juggling multiple VG contexts.
|
|---|
| 230 | const_cast<QVGPixmapData *>(this)->forceToImage();
|
|---|
| 231 | return source.paintEngine();
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | // This function works around QImage::bits() making a deep copy if the
|
|---|
| 235 | // QImage is not const. We force it to be const and then get the bits.
|
|---|
| 236 | // XXX: Should add a QImage::constBits() in the future to replace this.
|
|---|
| 237 | const uchar *qt_vg_imageBits(const QImage& image)
|
|---|
| 238 | {
|
|---|
| 239 | return image.bits();
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | VGImage QVGPixmapData::toVGImage()
|
|---|
| 243 | {
|
|---|
| 244 | if (!isValid())
|
|---|
| 245 | return VG_INVALID_HANDLE;
|
|---|
| 246 |
|
|---|
| 247 | #if !defined(QT_NO_EGL)
|
|---|
| 248 | // Increase the reference count on the shared context.
|
|---|
| 249 | if (!context)
|
|---|
| 250 | context = qt_vg_create_context(0, QInternal::Pixmap);
|
|---|
| 251 | #endif
|
|---|
| 252 |
|
|---|
| 253 | if (recreate && prevSize != QSize(w, h))
|
|---|
| 254 | destroyImages();
|
|---|
| 255 | else if (recreate)
|
|---|
| 256 | cachedOpacity = -1.0f; // Force opacity image to be refreshed later.
|
|---|
| 257 |
|
|---|
| 258 | if (vgImage == VG_INVALID_HANDLE) {
|
|---|
| 259 | vgImage = QVGImagePool::instance()->createImageForPixmap
|
|---|
| 260 | (VG_sARGB_8888_PRE, w, h, VG_IMAGE_QUALITY_FASTER, this);
|
|---|
| 261 |
|
|---|
| 262 | // Bail out if we run out of GPU memory - try again next time.
|
|---|
| 263 | if (vgImage == VG_INVALID_HANDLE)
|
|---|
| 264 | return VG_INVALID_HANDLE;
|
|---|
| 265 |
|
|---|
| 266 | inImagePool = true;
|
|---|
| 267 | } else if (inImagePool) {
|
|---|
| 268 | QVGImagePool::instance()->useImage(this);
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | if (!source.isNull() && recreate) {
|
|---|
| 272 | vgImageSubData
|
|---|
| 273 | (vgImage,
|
|---|
| 274 | qt_vg_imageBits(source), source.bytesPerLine(),
|
|---|
| 275 | VG_sARGB_8888_PRE, 0, 0, w, h);
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | recreate = false;
|
|---|
| 279 | prevSize = QSize(w, h);
|
|---|
| 280 |
|
|---|
| 281 | return vgImage;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | VGImage QVGPixmapData::toVGImage(qreal opacity)
|
|---|
| 285 | {
|
|---|
| 286 | #if !defined(QT_SHIVAVG)
|
|---|
| 287 | // Force the primary VG image to be recreated if necessary.
|
|---|
| 288 | if (toVGImage() == VG_INVALID_HANDLE)
|
|---|
| 289 | return VG_INVALID_HANDLE;
|
|---|
| 290 |
|
|---|
| 291 | if (opacity == 1.0f)
|
|---|
| 292 | return vgImage;
|
|---|
| 293 |
|
|---|
| 294 | // Create an alternative image for the selected opacity.
|
|---|
| 295 | if (vgImageOpacity == VG_INVALID_HANDLE || cachedOpacity != opacity) {
|
|---|
| 296 | if (vgImageOpacity == VG_INVALID_HANDLE) {
|
|---|
| 297 | if (inImagePool) {
|
|---|
| 298 | vgImageOpacity = QVGImagePool::instance()->createImageForPixmap
|
|---|
| 299 | (VG_sARGB_8888_PRE, w, h, VG_IMAGE_QUALITY_FASTER, this);
|
|---|
| 300 | } else {
|
|---|
| 301 | vgImageOpacity = vgCreateImage
|
|---|
| 302 | (VG_sARGB_8888_PRE, w, h, VG_IMAGE_QUALITY_FASTER);
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | // Bail out if we run out of GPU memory - try again next time.
|
|---|
| 306 | if (vgImageOpacity == VG_INVALID_HANDLE)
|
|---|
| 307 | return VG_INVALID_HANDLE;
|
|---|
| 308 | }
|
|---|
| 309 | VGfloat matrix[20] = {
|
|---|
| 310 | 1.0f, 0.0f, 0.0f, 0.0f,
|
|---|
| 311 | 0.0f, 1.0f, 0.0f, 0.0f,
|
|---|
| 312 | 0.0f, 0.0f, 1.0f, 0.0f,
|
|---|
| 313 | 0.0f, 0.0f, 0.0f, opacity,
|
|---|
| 314 | 0.0f, 0.0f, 0.0f, 0.0f
|
|---|
| 315 | };
|
|---|
| 316 | vgColorMatrix(vgImageOpacity, vgImage, matrix);
|
|---|
| 317 | cachedOpacity = opacity;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | return vgImageOpacity;
|
|---|
| 321 | #else
|
|---|
| 322 | // vgColorMatrix() doesn't work with ShivaVG, so ignore the opacity.
|
|---|
| 323 | Q_UNUSED(opacity);
|
|---|
| 324 | return toVGImage();
|
|---|
| 325 | #endif
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | void QVGPixmapData::detachImageFromPool()
|
|---|
| 329 | {
|
|---|
| 330 | if (inImagePool) {
|
|---|
| 331 | QVGImagePool::instance()->detachImage(this);
|
|---|
| 332 | inImagePool = false;
|
|---|
| 333 | }
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | void QVGPixmapData::hibernate()
|
|---|
| 337 | {
|
|---|
| 338 | // If the image was imported (e.g, from an SgImage under Symbian),
|
|---|
| 339 | // then we cannot copy it back to main memory for storage.
|
|---|
| 340 | if (vgImage != VG_INVALID_HANDLE && source.isNull())
|
|---|
| 341 | return;
|
|---|
| 342 |
|
|---|
| 343 | forceToImage();
|
|---|
| 344 | destroyImageAndContext();
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | void QVGPixmapData::reclaimImages()
|
|---|
| 348 | {
|
|---|
| 349 | if (!inImagePool)
|
|---|
| 350 | return;
|
|---|
| 351 | forceToImage();
|
|---|
| 352 | destroyImages();
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | extern int qt_defaultDpiX();
|
|---|
| 356 | extern int qt_defaultDpiY();
|
|---|
| 357 |
|
|---|
| 358 | int QVGPixmapData::metric(QPaintDevice::PaintDeviceMetric metric) const
|
|---|
| 359 | {
|
|---|
| 360 | switch (metric) {
|
|---|
| 361 | case QPaintDevice::PdmWidth:
|
|---|
| 362 | return w;
|
|---|
| 363 | case QPaintDevice::PdmHeight:
|
|---|
| 364 | return h;
|
|---|
| 365 | case QPaintDevice::PdmNumColors:
|
|---|
| 366 | return 0;
|
|---|
| 367 | case QPaintDevice::PdmDepth:
|
|---|
| 368 | return d;
|
|---|
| 369 | case QPaintDevice::PdmWidthMM:
|
|---|
| 370 | return qRound(w * 25.4 / qt_defaultDpiX());
|
|---|
| 371 | case QPaintDevice::PdmHeightMM:
|
|---|
| 372 | return qRound(h * 25.4 / qt_defaultDpiY());
|
|---|
| 373 | case QPaintDevice::PdmDpiX:
|
|---|
| 374 | case QPaintDevice::PdmPhysicalDpiX:
|
|---|
| 375 | return qt_defaultDpiX();
|
|---|
| 376 | case QPaintDevice::PdmDpiY:
|
|---|
| 377 | case QPaintDevice::PdmPhysicalDpiY:
|
|---|
| 378 | return qt_defaultDpiY();
|
|---|
| 379 | default:
|
|---|
| 380 | qWarning("QVGPixmapData::metric(): Invalid metric");
|
|---|
| 381 | return 0;
|
|---|
| 382 | }
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | // Force the pixmap data to be in QImage format.
|
|---|
| 386 | void QVGPixmapData::forceToImage()
|
|---|
| 387 | {
|
|---|
| 388 | if (!isValid())
|
|---|
| 389 | return;
|
|---|
| 390 |
|
|---|
| 391 | if (source.isNull())
|
|---|
| 392 | source = QImage(w, h, sourceFormat());
|
|---|
| 393 |
|
|---|
| 394 | recreate = true;
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | QImage::Format QVGPixmapData::sourceFormat() const
|
|---|
| 398 | {
|
|---|
| 399 | return QImage::Format_ARGB32_Premultiplied;
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | /*
|
|---|
| 403 | \internal
|
|---|
| 404 |
|
|---|
| 405 | Returns the VGImage that is storing the contents of \a pixmap.
|
|---|
| 406 | Returns VG_INVALID_HANDLE if \a pixmap is not owned by the OpenVG
|
|---|
| 407 | graphics system or \a pixmap is invalid.
|
|---|
| 408 |
|
|---|
| 409 | This function is typically used to access the backing store
|
|---|
| 410 | for a pixmap when executing raw OpenVG calls. It must only
|
|---|
| 411 | be used when a QPainter is active and the OpenVG paint engine
|
|---|
| 412 | is in use by the QPainter.
|
|---|
| 413 |
|
|---|
| 414 | \sa {QtOpenVG Module}
|
|---|
| 415 | */
|
|---|
| 416 | Q_OPENVG_EXPORT VGImage qPixmapToVGImage(const QPixmap& pixmap)
|
|---|
| 417 | {
|
|---|
| 418 | QPixmapData *pd = pixmap.pixmapData();
|
|---|
| 419 | if (!pd)
|
|---|
| 420 | return VG_INVALID_HANDLE; // null QPixmap
|
|---|
| 421 | if (pd->classId() == QPixmapData::OpenVGClass) {
|
|---|
| 422 | QVGPixmapData *vgpd = static_cast<QVGPixmapData *>(pd);
|
|---|
| 423 | if (vgpd->isValid())
|
|---|
| 424 | return vgpd->toVGImage();
|
|---|
| 425 | }
|
|---|
| 426 | return VG_INVALID_HANDLE;
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 | #if defined(Q_OS_SYMBIAN)
|
|---|
| 430 |
|
|---|
| 431 | static CFbsBitmap* createBlitCopy(CFbsBitmap* bitmap)
|
|---|
| 432 | {
|
|---|
| 433 | CFbsBitmap *copy = q_check_ptr(new CFbsBitmap);
|
|---|
| 434 | if(!copy)
|
|---|
| 435 | return 0;
|
|---|
| 436 |
|
|---|
| 437 | if (copy->Create(bitmap->SizeInPixels(), bitmap->DisplayMode()) != KErrNone) {
|
|---|
| 438 | delete copy;
|
|---|
| 439 | copy = 0;
|
|---|
| 440 |
|
|---|
| 441 | return 0;
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | CFbsBitmapDevice* bitmapDevice = 0;
|
|---|
| 445 | CFbsBitGc *bitmapGc = 0;
|
|---|
| 446 | QT_TRAP_THROWING(bitmapDevice = CFbsBitmapDevice::NewL(copy));
|
|---|
| 447 | QT_TRAP_THROWING(bitmapGc = CFbsBitGc::NewL());
|
|---|
| 448 | bitmapGc->Activate(bitmapDevice);
|
|---|
| 449 |
|
|---|
| 450 | bitmapGc->BitBlt(TPoint(), bitmap);
|
|---|
| 451 |
|
|---|
| 452 | delete bitmapGc;
|
|---|
| 453 | delete bitmapDevice;
|
|---|
| 454 |
|
|---|
| 455 | return copy;
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | void QVGPixmapData::cleanup()
|
|---|
| 459 | {
|
|---|
| 460 | is_null = w = h = 0;
|
|---|
| 461 | recreate = false;
|
|---|
| 462 | source = QImage();
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | void QVGPixmapData::fromNativeType(void* pixmap, NativeType type)
|
|---|
| 466 | {
|
|---|
| 467 | #if defined(QT_SYMBIAN_SUPPORTS_SGIMAGE) && !defined(QT_NO_EGL)
|
|---|
| 468 | if (type == QPixmapData::SgImage && pixmap) {
|
|---|
| 469 | RSgImage *sgImage = reinterpret_cast<RSgImage*>(pixmap);
|
|---|
| 470 | // when "0" used as argument then
|
|---|
| 471 | // default display, context are used
|
|---|
| 472 | if (!context)
|
|---|
| 473 | context = qt_vg_create_context(0, QInternal::Pixmap);
|
|---|
| 474 |
|
|---|
| 475 | destroyImages();
|
|---|
| 476 | prevSize = QSize();
|
|---|
| 477 |
|
|---|
| 478 | TInt err = 0;
|
|---|
| 479 |
|
|---|
| 480 | err = SgDriver::Open();
|
|---|
| 481 | if(err != KErrNone) {
|
|---|
| 482 | cleanup();
|
|---|
| 483 | return;
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 | if(sgImage->IsNull()) {
|
|---|
| 487 | cleanup();
|
|---|
| 488 | SgDriver::Close();
|
|---|
| 489 | return;
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | TSgImageInfo sgImageInfo;
|
|---|
| 493 | err = sgImage->GetInfo(sgImageInfo);
|
|---|
| 494 | if(err != KErrNone) {
|
|---|
| 495 | cleanup();
|
|---|
| 496 | SgDriver::Close();
|
|---|
| 497 | return;
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | pfnEglCreateImageKHR eglCreateImageKHR = (pfnEglCreateImageKHR) eglGetProcAddress("eglCreateImageKHR");
|
|---|
| 501 | pfnEglDestroyImageKHR eglDestroyImageKHR = (pfnEglDestroyImageKHR) eglGetProcAddress("eglDestroyImageKHR");
|
|---|
| 502 | pfnVgCreateEGLImageTargetKHR vgCreateEGLImageTargetKHR = (pfnVgCreateEGLImageTargetKHR) eglGetProcAddress("vgCreateEGLImageTargetKHR");
|
|---|
| 503 |
|
|---|
| 504 | if(eglGetError() != EGL_SUCCESS || !eglCreateImageKHR || !eglDestroyImageKHR || !vgCreateEGLImageTargetKHR) {
|
|---|
| 505 | cleanup();
|
|---|
| 506 | SgDriver::Close();
|
|---|
| 507 | return;
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 | const EGLint KEglImageAttribs[] = {EGL_IMAGE_PRESERVED_SYMBIAN, EGL_TRUE, EGL_NONE};
|
|---|
| 511 | EGLImageKHR eglImage = eglCreateImageKHR(context->display(),
|
|---|
| 512 | EGL_NO_CONTEXT,
|
|---|
| 513 | EGL_NATIVE_PIXMAP_KHR,
|
|---|
| 514 | (EGLClientBuffer)sgImage,
|
|---|
| 515 | (EGLint*)KEglImageAttribs);
|
|---|
| 516 |
|
|---|
| 517 | if(eglGetError() != EGL_SUCCESS) {
|
|---|
| 518 | cleanup();
|
|---|
| 519 | SgDriver::Close();
|
|---|
| 520 | return;
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 | vgImage = vgCreateEGLImageTargetKHR(eglImage);
|
|---|
| 524 | if(vgGetError() != VG_NO_ERROR) {
|
|---|
| 525 | cleanup();
|
|---|
| 526 | eglDestroyImageKHR(context->display(), eglImage);
|
|---|
| 527 | SgDriver::Close();
|
|---|
| 528 | return;
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | w = sgImageInfo.iSizeInPixels.iWidth;
|
|---|
| 532 | h = sgImageInfo.iSizeInPixels.iHeight;
|
|---|
| 533 | d = 32; // We always use ARGB_Premultiplied for VG pixmaps.
|
|---|
| 534 | is_null = (w <= 0 || h <= 0);
|
|---|
| 535 | source = QImage();
|
|---|
| 536 | recreate = false;
|
|---|
| 537 | prevSize = QSize(w, h);
|
|---|
| 538 | setSerialNumber(++qt_vg_pixmap_serial);
|
|---|
| 539 | // release stuff
|
|---|
| 540 | eglDestroyImageKHR(context->display(), eglImage);
|
|---|
| 541 | SgDriver::Close();
|
|---|
| 542 | } else if (type == QPixmapData::FbsBitmap) {
|
|---|
| 543 | CFbsBitmap *bitmap = reinterpret_cast<CFbsBitmap*>(pixmap);
|
|---|
| 544 |
|
|---|
| 545 | bool deleteSourceBitmap = false;
|
|---|
| 546 |
|
|---|
| 547 | #ifdef Q_SYMBIAN_HAS_EXTENDED_BITMAP_TYPE
|
|---|
| 548 |
|
|---|
| 549 | // Rasterize extended bitmaps
|
|---|
| 550 |
|
|---|
| 551 | TUid extendedBitmapType = bitmap->ExtendedBitmapType();
|
|---|
| 552 | if (extendedBitmapType != KNullUid) {
|
|---|
| 553 | bitmap = createBlitCopy(bitmap);
|
|---|
| 554 | deleteSourceBitmap = true;
|
|---|
| 555 | }
|
|---|
| 556 | #endif
|
|---|
| 557 |
|
|---|
| 558 | if (bitmap->IsCompressedInRAM()) {
|
|---|
| 559 | bitmap = createBlitCopy(bitmap);
|
|---|
| 560 | deleteSourceBitmap = true;
|
|---|
| 561 | }
|
|---|
| 562 |
|
|---|
| 563 | TDisplayMode displayMode = bitmap->DisplayMode();
|
|---|
| 564 | QImage::Format format = qt_TDisplayMode2Format(displayMode);
|
|---|
| 565 |
|
|---|
| 566 | TSize size = bitmap->SizeInPixels();
|
|---|
| 567 |
|
|---|
| 568 | bitmap->BeginDataAccess();
|
|---|
| 569 | uchar *bytes = (uchar*)bitmap->DataAddress();
|
|---|
| 570 | QImage img = QImage(bytes, size.iWidth, size.iHeight, format);
|
|---|
| 571 | img = img.copy();
|
|---|
| 572 | bitmap->EndDataAccess();
|
|---|
| 573 |
|
|---|
| 574 | if(displayMode == EGray2) {
|
|---|
| 575 | //Symbian thinks set pixels are white/transparent, Qt thinks they are foreground/solid
|
|---|
| 576 | //So invert mono bitmaps so that masks work correctly.
|
|---|
| 577 | img.invertPixels();
|
|---|
| 578 | } else if(displayMode == EColor16M) {
|
|---|
| 579 | img = img.rgbSwapped(); // EColor16M is BGR
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | fromImage(img, Qt::AutoColor);
|
|---|
| 583 |
|
|---|
| 584 | if(deleteSourceBitmap)
|
|---|
| 585 | delete bitmap;
|
|---|
| 586 | }
|
|---|
| 587 | #else
|
|---|
| 588 | Q_UNUSED(pixmap);
|
|---|
| 589 | Q_UNUSED(type);
|
|---|
| 590 | #endif
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | void* QVGPixmapData::toNativeType(NativeType type)
|
|---|
| 594 | {
|
|---|
| 595 | #if defined(QT_SYMBIAN_SUPPORTS_SGIMAGE) && !defined(QT_NO_EGL)
|
|---|
| 596 | if (type == QPixmapData::SgImage) {
|
|---|
| 597 | toVGImage();
|
|---|
| 598 |
|
|---|
| 599 | if(!isValid() || vgImage == VG_INVALID_HANDLE)
|
|---|
| 600 | return 0;
|
|---|
| 601 |
|
|---|
| 602 | TInt err = 0;
|
|---|
| 603 |
|
|---|
| 604 | err = SgDriver::Open();
|
|---|
| 605 | if(err != KErrNone)
|
|---|
| 606 | return 0;
|
|---|
| 607 |
|
|---|
| 608 | TSgImageInfo sgInfo;
|
|---|
| 609 | sgInfo.iPixelFormat = EUidPixelFormatARGB_8888_PRE;
|
|---|
| 610 | sgInfo.iSizeInPixels.SetSize(w, h);
|
|---|
| 611 | sgInfo.iUsage = ESgUsageOpenVgImage | ESgUsageOpenVgTarget;
|
|---|
| 612 | sgInfo.iShareable = ETrue;
|
|---|
| 613 | sgInfo.iCpuAccess = ESgCpuAccessNone;
|
|---|
| 614 | sgInfo.iScreenId = KSgScreenIdMain; //KSgScreenIdAny;
|
|---|
| 615 | sgInfo.iUserAttributes = NULL;
|
|---|
| 616 | sgInfo.iUserAttributeCount = 0;
|
|---|
| 617 |
|
|---|
| 618 | RSgImage *sgImage = q_check_ptr(new RSgImage());
|
|---|
| 619 | err = sgImage->Create(sgInfo, NULL, NULL);
|
|---|
| 620 | if(err != KErrNone) {
|
|---|
| 621 | SgDriver::Close();
|
|---|
| 622 | return 0;
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | pfnEglCreateImageKHR eglCreateImageKHR = (pfnEglCreateImageKHR) eglGetProcAddress("eglCreateImageKHR");
|
|---|
| 626 | pfnEglDestroyImageKHR eglDestroyImageKHR = (pfnEglDestroyImageKHR) eglGetProcAddress("eglDestroyImageKHR");
|
|---|
| 627 | pfnVgCreateEGLImageTargetKHR vgCreateEGLImageTargetKHR = (pfnVgCreateEGLImageTargetKHR) eglGetProcAddress("vgCreateEGLImageTargetKHR");
|
|---|
| 628 |
|
|---|
| 629 | if(eglGetError() != EGL_SUCCESS || !eglCreateImageKHR || !eglDestroyImageKHR || !vgCreateEGLImageTargetKHR) {
|
|---|
| 630 | SgDriver::Close();
|
|---|
| 631 | return 0;
|
|---|
| 632 | }
|
|---|
| 633 |
|
|---|
| 634 | const EGLint KEglImageAttribs[] = {EGL_IMAGE_PRESERVED_SYMBIAN, EGL_TRUE, EGL_NONE};
|
|---|
| 635 | EGLImageKHR eglImage = eglCreateImageKHR(context->display(),
|
|---|
| 636 | EGL_NO_CONTEXT,
|
|---|
| 637 | EGL_NATIVE_PIXMAP_KHR,
|
|---|
| 638 | (EGLClientBuffer)sgImage,
|
|---|
| 639 | (EGLint*)KEglImageAttribs);
|
|---|
| 640 | if(eglGetError() != EGL_SUCCESS) {
|
|---|
| 641 | sgImage->Close();
|
|---|
| 642 | SgDriver::Close();
|
|---|
| 643 | return 0;
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | VGImage dstVgImage = vgCreateEGLImageTargetKHR(eglImage);
|
|---|
| 647 | if(vgGetError() != VG_NO_ERROR) {
|
|---|
| 648 | eglDestroyImageKHR(context->display(), eglImage);
|
|---|
| 649 | sgImage->Close();
|
|---|
| 650 | SgDriver::Close();
|
|---|
| 651 | return 0;
|
|---|
| 652 | }
|
|---|
| 653 |
|
|---|
| 654 | vgCopyImage(dstVgImage, 0, 0,
|
|---|
| 655 | vgImage, 0, 0,
|
|---|
| 656 | w, h, VG_FALSE);
|
|---|
| 657 |
|
|---|
| 658 | if(vgGetError() != VG_NO_ERROR) {
|
|---|
| 659 | sgImage->Close();
|
|---|
| 660 | sgImage = 0;
|
|---|
| 661 | }
|
|---|
| 662 | // release stuff
|
|---|
| 663 | vgDestroyImage(dstVgImage);
|
|---|
| 664 | eglDestroyImageKHR(context->display(), eglImage);
|
|---|
| 665 | SgDriver::Close();
|
|---|
| 666 | return reinterpret_cast<void*>(sgImage);
|
|---|
| 667 | } else if (type == QPixmapData::FbsBitmap) {
|
|---|
| 668 | CFbsBitmap *bitmap = q_check_ptr(new CFbsBitmap);
|
|---|
| 669 |
|
|---|
| 670 | if (bitmap) {
|
|---|
| 671 | if (bitmap->Create(TSize(source.width(), source.height()),
|
|---|
| 672 | EColor16MAP) == KErrNone) {
|
|---|
| 673 | const uchar *sptr = qt_vg_imageBits(source);
|
|---|
| 674 | bitmap->BeginDataAccess();
|
|---|
| 675 |
|
|---|
| 676 | uchar *dptr = (uchar*)bitmap->DataAddress();
|
|---|
| 677 | Mem::Copy(dptr, sptr, source.byteCount());
|
|---|
| 678 |
|
|---|
| 679 | bitmap->EndDataAccess();
|
|---|
| 680 | } else {
|
|---|
| 681 | delete bitmap;
|
|---|
| 682 | bitmap = 0;
|
|---|
| 683 | }
|
|---|
| 684 | }
|
|---|
| 685 |
|
|---|
| 686 | return reinterpret_cast<void*>(bitmap);
|
|---|
| 687 | }
|
|---|
| 688 | #else
|
|---|
| 689 | Q_UNUSED(type);
|
|---|
| 690 | return 0;
|
|---|
| 691 | #endif
|
|---|
| 692 | }
|
|---|
| 693 | #endif //Q_OS_SYMBIAN
|
|---|
| 694 |
|
|---|
| 695 | QT_END_NAMESPACE
|
|---|