src/hbservers/hbthemeserver/hbpixmapiconprocessor_p.cpp
changeset 34 ed14f46c0e55
parent 7 923ff622b8b9
equal deleted inserted replaced
31:7516d6d86cf5 34:ed14f46c0e55
    90     \fn HbPixmapIconProcessor::sharedIconData()
    90     \fn HbPixmapIconProcessor::sharedIconData()
    91     Copies the pixmap raster data to the shared memory
    91     Copies the pixmap raster data to the shared memory
    92  */
    92  */
    93 HbSharedIconInfo HbPixmapIconProcessor::sharedIconData()const
    93 HbSharedIconInfo HbPixmapIconProcessor::sharedIconData()const
    94 {
    94 {
    95     HbSharedIconInfo data;
       
    96     data.type = INVALID_FORMAT;
       
    97     const QImage image = pixmap.toImage();
    95     const QImage image = pixmap.toImage();
    98 
    96 
    99     if (!image.isNull()) {
    97     if (!image.isNull() && (data.type == INVALID_FORMAT)) {
   100         GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
    98         GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
   101         try {
    99         try {
   102             //alloc function of memory manager can throw
   100             //alloc function of memory manager can throw
   103             data.pixmapData.offset =  manager->alloc(image.numBytes());
   101             data.pixmapData.offset =  manager->alloc(image.numBytes());
   104             data.type = OTHER_SUPPORTED_FORMATS;
   102             data.type = OTHER_SUPPORTED_FORMATS;
   125     if (iconType == "SVG") {
   123     if (iconType == "SVG") {
   126         isIconCreated = renderSvgToPixmap(iconPath);
   124         isIconCreated = renderSvgToPixmap(iconPath);
   127     } else if (iconType == "PIC") {
   125     } else if (iconType == "PIC") {
   128         isIconCreated = renderPicToPixmap(iconPath);
   126         isIconCreated = renderPicToPixmap(iconPath);
   129     } else if (iconType == "NVG") {
   127     } else if (iconType == "NVG") {
   130 #if defined (HB_NVG_CS_ICON)    
   128 #if defined (HB_NVG_CS_ICON)
   131 		isIconCreated = renderNvgToPixmap(iconPath);
   129         isIconCreated = renderNvgToPixmap(iconPath);
   132 #endif //HB_NVG_CS_ICON
   130 #endif //HB_NVG_CS_ICON
   133     } else {
   131     } else {
   134         isIconCreated = renderOtherFormatsToPixmap(iconPath);
   132         isIconCreated = renderOtherFormatsToPixmap(iconPath);
   135     }
   133     }
   136     if (isIconCreated) {
   134     if (isIconCreated) {
   139             QTransform t;
   137             QTransform t;
   140             t.scale(-1, 1);
   138             t.scale(-1, 1);
   141             pixmap = pixmap.transformed(t);
   139             pixmap = pixmap.transformed(t);
   142         }
   140         }
   143 
   141 
       
   142         // Apply color if valid
       
   143         if (iconKey.color.isValid()) {
       
   144             if (!pixmap.isNull()) {
       
   145                 QPixmap mask = pixmap.alphaChannel();
       
   146                 pixmap.fill(iconKey.color);
       
   147                 pixmap.setAlphaChannel(mask);
       
   148             }
       
   149         }
   144         // Apply mode
   150         // Apply mode
   145         if (iconKey.mode != QIcon::Normal) {
   151         if (iconKey.mode != QIcon::Normal) {
   146             QStyleOption opt(0);
   152             QStyleOption opt(0);
   147             opt.palette = QApplication::palette();
   153             opt.palette = QApplication::palette();
   148             pixmap = QApplication::style()->generatedIconPixmap(iconKey.mode, pixmap, &opt);
   154             pixmap = QApplication::style()->generatedIconPixmap(iconKey.mode, pixmap, &opt);
   149         }
   155         }
   150         /* Apply color if valid*/
       
   151 
       
   152         if (iconKey.color.isValid() && (iconKey.mode != QIcon::Disabled)) {
       
   153             if (!pixmap.isNull()) {
       
   154                 QPixmap mask = pixmap.alphaChannel();
       
   155                 pixmap.fill(iconKey.color);
       
   156                 pixmap.setAlphaChannel(mask);
       
   157             }
       
   158         }
       
   159     }
   156     }
   160 
   157 
   161     return isIconCreated;
   158     return isIconCreated;
   162 }
   159 }
   163 
   160 
       
   161 #if defined (HB_NVG_CS_ICON)
       
   162 
       
   163 bool HbPixmapIconProcessor::renderNvgToPixmap(const QString& iconPath)
       
   164 {
       
   165     bool success = false;
       
   166     HbNvgRasterizer * nvgRasterizer = HbNvgRasterizer::global();
       
   167 
       
   168     bool isDefaultSize =  iconKey.size.isNull();
       
   169     HbIconSource *source = HbThemeServerUtils::getIconSource(iconPath);
       
   170     if (!source) {
       
   171         return success;
       
   172     }
       
   173     QByteArray *sourceByteArray = source->byteArray();
       
   174     if( !sourceByteArray ) {
       
   175         return success;
       
   176     }
       
   177     QByteArray nvgArray = *sourceByteArray;
       
   178     QSizeF renderSize = source->defaultSize();
       
   179     defaultSize = renderSize.toSize();
       
   180     if (!isDefaultSize) {
       
   181         renderSize.scale(iconKey.size,iconKey.aspectRatioMode);
       
   182     }
       
   183 
       
   184     QSize iconSize = renderSize.toSize();
       
   185 
       
   186     QImage image(iconSize, QImage::Format_ARGB32_Premultiplied);
       
   187     QImage::Format imageFormat = image.format();
       
   188     image.fill(0);
       
   189 
       
   190     int stride = image.bytesPerLine();
       
   191     void * rasterizedData = image.bits();
       
   192     success = nvgRasterizer->rasterize(nvgArray, iconSize,
       
   193                                         iconKey.aspectRatioMode,
       
   194                                         rasterizedData, stride,imageFormat);
       
   195 
       
   196     pixmap = QPixmap::fromImage(image);
       
   197     return success;
       
   198 }
       
   199 
       
   200 #endif
   164 /*!
   201 /*!
   165     \fn HbPixmapIconProcessor::renderSvgToPixmap()
   202     \fn HbPixmapIconProcessor::renderSvgToPixmap()
   166     \a iconpath
   203     \a iconpath
   167  */
   204  */
   168 bool HbPixmapIconProcessor::renderSvgToPixmap(const QString& iconPath)
   205 bool HbPixmapIconProcessor::renderSvgToPixmap(const QString& iconPath)
   194         painter.begin(&pixmap);
   231         painter.begin(&pixmap);
   195         svgRenderer->render(&painter, QRectF(QPointF(), renderSize.toSize()));
   232         svgRenderer->render(&painter, QRectF(QPointF(), renderSize.toSize()));
   196         painter.end();
   233         painter.end();
   197         isIconCreated = true;
   234         isIconCreated = true;
   198     }
   235     }
   199     
   236 
   200     source->releaseSvgRenderer();
   237     source->releaseSvgRenderer();
   201     return isIconCreated;
   238     return isIconCreated;
   202 }
   239 }
   203 
   240 
   204 /*!
   241 /*!
   266     if (!iconKey.size.isValid()) {
   303     if (!iconKey.size.isValid()) {
   267         return isIconCreated;
   304         return isIconCreated;
   268     }
   305     }
   269 
   306 
   270     HbIconSource *source = HbThemeServerUtils::getIconSource(iconPath);
   307     HbIconSource *source = HbThemeServerUtils::getIconSource(iconPath);
   271     pixmap = *source->pixmap();
   308     pixmap = QPixmap::fromImage(*source->image());
   272 
   309 
   273     if (pixmap.isNull()) {
   310     if (pixmap.isNull()) {
   274         return isIconCreated;
   311         return isIconCreated;
   275     }
   312     }
   276 
   313 
   307         }
   344         }
   308     }
   345     }
   309     isIconCreated = true;
   346     isIconCreated = true;
   310 
   347 
   311     // Delete original pixmap if its size is large
   348     // Delete original pixmap if its size is large
   312     source->deletePixmapIfLargerThan(PIXMAP_SIZE_LIMIT);
   349     source->deleteImageIfLargerThan(IMAGE_SIZE_LIMIT);
   313 
   350 
   314     return isIconCreated;
   351     return isIconCreated;
   315 }
   352 }
   316 
   353 
   317 
   354 
   337     QPainter painter;
   374     QPainter painter;
   338     painter.begin(&finalPixmap);
   375     painter.begin(&finalPixmap);
   339 
   376 
   340 
   377 
   341     for (int i = 0; i < multiPieceIconParams.multiPartIconList.count(); i++) {
   378     for (int i = 0; i < multiPieceIconParams.multiPartIconList.count(); i++) {
   342 
   379 #if defined (HB_NVG_CS_ICON)
   343         if ((multiPieceIconInfo[i].type != INVALID_FORMAT) && multiPieceIconInfo[i].type == OTHER_SUPPORTED_FORMATS) {
   380         if ((multiPieceIconInfo[i].type != INVALID_FORMAT) && multiPieceIconInfo[i].type == NVG) {
   344             GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
   381                 
   345             QImage image((const uchar*)
   382                 GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
       
   383                 const QByteArray nvgData  = QByteArray::fromRawData((char*)manager->base() + multiPieceIconInfo[i].nvgData.offset,
       
   384                         multiPieceIconInfo[i].nvgData.dataSize);
       
   385                 QSize contentSize = QSize(multiPieceIconInfo[i].nvgData.width, multiPieceIconInfo[i].nvgData.height);
       
   386                 HbNvgRasterizer * nvgRasterizer = HbNvgRasterizer::global();
       
   387 
       
   388                 QImage image(contentSize, QImage::Format_ARGB32_Premultiplied);
       
   389                 if(nvgRasterizer->rasterize(nvgData, contentSize,
       
   390                                     (Qt::AspectRatioMode)multiPieceIconParams.aspectRatioMode,
       
   391                                     image)) {
       
   392                     QPixmap tempPixmap = QPixmap::fromImage(image);
       
   393                     painter.drawPixmap(
       
   394                             multiPieceIconParams.multiPartIconData.targets[i].topLeft(),
       
   395                             tempPixmap,
       
   396                             tempPixmap.rect());
       
   397                 } else {
       
   398                     // consolidated pixmap creation falied.
       
   399                     painter.end();
       
   400                     return false;
       
   401                 }
       
   402             } else {
       
   403 #endif
       
   404             if ((multiPieceIconInfo[i].type != INVALID_FORMAT) && multiPieceIconInfo[i].type == OTHER_SUPPORTED_FORMATS) {
       
   405                 GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
       
   406                 QImage image((const uchar*)
   346                          ((char *)manager->base() + multiPieceIconInfo[i].pixmapData.offset),
   407                          ((char *)manager->base() + multiPieceIconInfo[i].pixmapData.offset),
   347                          multiPieceIconParams.multiPartIconData.pixmapSizes[i].width(), 
   408                          multiPieceIconParams.multiPartIconData.pixmapSizes[i].width(),
   348                          multiPieceIconParams.multiPartIconData.pixmapSizes[i].height(),
   409                          multiPieceIconParams.multiPartIconData.pixmapSizes[i].height(),
   349                          multiPieceIconInfo[i].pixmapData.format);
   410                          multiPieceIconInfo[i].pixmapData.format);
   350 
   411 
   351             QPixmap pixmap = QPixmap::fromImage((image));            
   412                 QPixmap pixmap = QPixmap::fromImage((image));
   352             painter.drawPixmap(multiPieceIconParams.multiPartIconData.targets[i].topLeft(), pixmap, pixmap.rect());
   413                 painter.drawPixmap(multiPieceIconParams.multiPartIconData.targets[i].topLeft(), pixmap, pixmap.rect());
   353         }
   414             }
       
   415 #if defined (HB_NVG_CS_ICON)
       
   416         }
       
   417 #endif
   354     }
   418     }
   355 
   419 
   356     painter.end();
   420     painter.end();
   357     if (multiPieceIconParams.mirrored) {
   421     if (multiPieceIconParams.mirrored) {
   358         QTransform t;
   422         QTransform t;
   359         t.scale(-1, 1);
   423         t.scale(-1, 1);
   360         finalPixmap = finalPixmap.transformed(t);
   424         finalPixmap = finalPixmap.transformed(t);
   361     }
   425     }
   362     setPixmap(finalPixmap);
   426     setPixmap(finalPixmap);
       
   427     
   363     return true;
   428     return true;
   364 }
   429 }
   365 
   430 
   366 #if defined (HB_NVG_CS_ICON)
   431 
   367 
   432 
   368 VGIColorBufferFormat HbPixmapIconProcessor::mapToVgiDisplayFormat( QImage::Format imageFormat ) const
   433 
   369 { 
       
   370     VGIColorBufferFormat format; 
       
   371     switch(imageFormat) 
       
   372     { 
       
   373     case QImage::Format_Mono:                  
       
   374     case QImage::Format_RGB32:                 
       
   375     case QImage::Format_ARGB32:       
       
   376             format = VGI_COLOR_BUFFER_FORMAT_ARGB8888;
       
   377             break;
       
   378     case QImage::Format_ARGB32_Premultiplied: 
       
   379             format = VGI_COLOR_BUFFER_FORMAT_ARGB8888_PRE;
       
   380             break;
       
   381     case QImage::Format_RGB16:                 
       
   382     case QImage::Format_ARGB8565_Premultiplied: 
       
   383     case QImage::Format_RGB666:                
       
   384     case QImage::Format_ARGB6666_Premultiplied: 
       
   385     case QImage::Format_RGB555:                
       
   386     case QImage::Format_ARGB8555_Premultiplied: 
       
   387             break; 
       
   388     case QImage::Format_RGB888: 
       
   389             format = VGI_COLOR_BUFFER_FORMAT_RGB888; 
       
   390             break;                
       
   391     case QImage::Format_RGB444:                
       
   392     case QImage::Format_ARGB4444_Premultiplied: 
       
   393     case QImage::Format_Invalid:               
       
   394         break; 
       
   395     } 
       
   396     return format; 
       
   397 }
       
   398 
       
   399 /**
       
   400  * HbNvgIconProcessor::renderNvgToPixmap()
       
   401  * This is used to render NVG data to a pixmap using the Software OpenVG
       
   402  * \a iconPath
       
   403  */
       
   404 bool HbPixmapIconProcessor::renderNvgToPixmap(const QString& iconPath)
       
   405 {
       
   406     bool isIconCreated = false;
       
   407     
       
   408     CNvgEngine* nvgengine = 0;
       
   409     TRAPD(error, nvgengine = CNvgEngine::NewL());
       
   410     if (error != KErrNone) {
       
   411         return isIconCreated;
       
   412     }
       
   413     QScopedPointer<CNvgEngine> nvgEngine(nvgengine); 
       
   414     
       
   415     bool isDefaultSize =  iconKey.size.isNull();
       
   416     HbIconSource *source = HbThemeServerUtils::getIconSource(iconPath);
       
   417     QByteArray *sourceByteArray = source->byteArray();
       
   418     if( !sourceByteArray ) {
       
   419         return false;
       
   420     }
       
   421     QByteArray byteArray = *sourceByteArray;
       
   422     QSizeF renderSize = source->defaultSize();
       
   423     defaultSize = renderSize.toSize();
       
   424     if (!isDefaultSize) {
       
   425         renderSize.scale(iconKey.size,iconKey.aspectRatioMode);
       
   426     }
       
   427     size = renderSize.toSize();          
       
   428     TSize surfaceSize(TSize(size.width(), size.height()));
       
   429 
       
   430 /*    QImage img(size,QImage::Format_ARGB32_Premultiplied);
       
   431     
       
   432     VGIColorBufferFormat format;
       
   433     TInt stride = img.bytesPerLine(); 
       
   434     TUint8* imageBuffer = img.bits(); // get the pointer to image buffer. 
       
   435     // Map Qimage display modes to the VGI display modes. 
       
   436     format = mapToVgiDisplayFormat(img.format()); 
       
   437     qDebug()<<"Format = " <<format; */
       
   438     
       
   439     QScopedPointer<CFbsBitmap> bitmapData(new CFbsBitmap());    
       
   440     
       
   441     TInt err = bitmapData.data()->Create(surfaceSize, EColor16MA);    
       
   442     if(err != KErrNone) {      
       
   443         return isIconCreated;
       
   444     }
       
   445     
       
   446     
       
   447     //Reset the surface incase already present
       
   448     VGISymbianTerminate();
       
   449     
       
   450     // Surface creation
       
   451     /*TInt*/ err =  VGISymbianInitialize( surfaceSize, VGI_COLORSPACE_SRGB );
       
   452     if( err != KErrNone) {
       
   453         return isIconCreated;
       
   454     }
       
   455     
       
   456     HbNvgAspectRatioSettings settings = mapKeyAspectRatioToNvgAspectRatio(iconKey.aspectRatioMode);
       
   457     nvgEngine.data()->SetPreserveAspectRatio(settings.nvgAlignStatusAndAspectRatio, settings.type);
       
   458     // Rendering onto active surface
       
   459     TPtr8 data ((unsigned char*)byteArray.data(), byteArray.length(), byteArray.length());
       
   460     err = nvgEngine.data()->DrawNvg(data, surfaceSize, 0, 0);    
       
   461     if(err !=KErrNone) {  
       
   462         return isIconCreated;
       
   463     }
       
   464     
       
   465     //Copy the data from the surface
       
   466 /*    err = VGICopyToTarget(format, stride, imageBuffer, 0, NULL, VGI_COPY_TRANSPARENT_PIXELS); 
       
   467 #ifdef __DEBUG    
       
   468 	qDebug() << "error code for VGICopyToTarget()"<< err;
       
   469 #endif
       
   470     //Get Pixmap from the Qimage.
       
   471     pixmap = QPixmap::fromImage(img); */
       
   472 
       
   473     err = VGISymbianCopyToBitmap(bitmapData.data(), 0, VGI_COPY_TRANSPARENT_PIXELS);
       
   474     if(err !=KErrNone) { 
       
   475         return isIconCreated;
       
   476     }
       
   477     //Get Pixmap from the Symbian Native format.
       
   478     pixmap = QPixmap::fromSymbianCFbsBitmap(bitmapData.data());
       
   479     
       
   480     isIconCreated = true;    
       
   481         
       
   482     //Clean Up
       
   483     VGISymbianTerminate();
       
   484     return isIconCreated;
       
   485 }
       
   486 
       
   487 /*!
       
   488     \fn HbPixmapIconProcessor::mapKeyAspectRatioToNvgAspectRatio()
       
   489     \a aspectRatio
       
   490  */
       
   491 HbNvgAspectRatioSettings HbPixmapIconProcessor::mapKeyAspectRatioToNvgAspectRatio(
       
   492                                                 Qt::AspectRatioMode aspectRatio) const
       
   493 {
       
   494     HbNvgAspectRatioSettings settings;
       
   495     switch(aspectRatio) {  
       
   496     
       
   497     case Qt::IgnoreAspectRatio: {
       
   498         settings.nvgAlignStatusAndAspectRatio = ENvgPreserveAspectRatio_None;
       
   499         settings.type = ENvgMeet; 
       
   500         break;
       
   501     }
       
   502     case Qt::KeepAspectRatio: {
       
   503         settings.nvgAlignStatusAndAspectRatio = ENvgPreserveAspectRatio_XmidYmid;
       
   504         settings.type = ENvgMeet;
       
   505         break;
       
   506         }
       
   507     case Qt::KeepAspectRatioByExpanding: {
       
   508         settings.nvgAlignStatusAndAspectRatio = ENvgPreserveAspectRatio_XmidYmid;
       
   509         settings.type = ENvgSlice;
       
   510         break;
       
   511         } 
       
   512     default: {
       
   513         settings.nvgAlignStatusAndAspectRatio = ENvgPreserveAspectRatio_XmidYmid;
       
   514         settings.type = ENvgMeet;
       
   515         break;
       
   516         }        
       
   517     }
       
   518     return settings;
       
   519 }
       
   520 #endif //HB_NVG_CS_ICON
       
   521