src/hbcore/image/hbnvgrasterizer_p.cpp
changeset 28 b7da29130b0e
child 30 80e4d18b72f5
equal deleted inserted replaced
23:e6ad4ef83b23 28:b7da29130b0e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbCore module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hbnvgrasterizer_p.h"
       
    27 
       
    28 #if defined (HB_NVG_CS_ICON)
       
    29 #include <nvg.h>
       
    30 // Note: Cases of the following two directory names intentionally differ to
       
    31 //       match the cases of the corresponding directories in Symbian 4.
       
    32 #include <VG/openvg.h>
       
    33 #include <vg/vgcontext_symbian.h>
       
    34 
       
    35 struct HbNvgAspectRatioSettings
       
    36 {
       
    37     TNvgAlignStatusType nvgAlignStatusAndAspectRatio;
       
    38     TNvgMeetOrSliceType type;
       
    39 };
       
    40 #endif //HB_NVG_CS_ICON
       
    41 
       
    42 /*!
       
    43     \fn mapKeyAspectRatioToNvgAspectRatio()
       
    44     \a aspectRatio
       
    45  */
       
    46 HbNvgAspectRatioSettings mapKeyAspectRatioToNvgAspectRatio(
       
    47                                                 Qt::AspectRatioMode aspectRatio)
       
    48 {
       
    49     HbNvgAspectRatioSettings settings;
       
    50     switch(aspectRatio) {
       
    51 
       
    52     case Qt::IgnoreAspectRatio: {
       
    53         settings.nvgAlignStatusAndAspectRatio = ENvgPreserveAspectRatio_None;
       
    54         settings.type = ENvgMeet;
       
    55         break;
       
    56     }
       
    57     case Qt::KeepAspectRatio: {
       
    58         settings.nvgAlignStatusAndAspectRatio = ENvgPreserveAspectRatio_XmidYmid;
       
    59         settings.type = ENvgMeet;
       
    60         break;
       
    61         }
       
    62     case Qt::KeepAspectRatioByExpanding: {
       
    63         settings.nvgAlignStatusAndAspectRatio = ENvgPreserveAspectRatio_XmidYmid;
       
    64         settings.type = ENvgSlice;
       
    65         break;
       
    66         }
       
    67     default: {
       
    68         settings.nvgAlignStatusAndAspectRatio = ENvgPreserveAspectRatio_XmidYmid;
       
    69         settings.type = ENvgMeet;
       
    70         break;
       
    71         }
       
    72     }
       
    73     return settings;
       
    74 }
       
    75 
       
    76 /*!
       
    77     \fn mapToVgiDisplayFormat()
       
    78     \a imageFormat
       
    79  */
       
    80 VGIColorBufferFormat mapToVgiDisplayFormat(QImage::Format imageFormat)
       
    81 {
       
    82     VGIColorBufferFormat format = VGI_COLOR_BUFFER_FORMAT_ARGB8888_PRE;
       
    83     switch(imageFormat)
       
    84     {
       
    85     case QImage::Format_Mono:
       
    86     case QImage::Format_RGB32:
       
    87     case QImage::Format_ARGB32:
       
    88             format = VGI_COLOR_BUFFER_FORMAT_ARGB8888;
       
    89             break;
       
    90     case QImage::Format_ARGB32_Premultiplied:
       
    91             format = VGI_COLOR_BUFFER_FORMAT_ARGB8888_PRE;
       
    92             break;
       
    93     case QImage::Format_RGB16:
       
    94     case QImage::Format_ARGB8565_Premultiplied:
       
    95     case QImage::Format_RGB666:
       
    96     case QImage::Format_ARGB6666_Premultiplied:
       
    97     case QImage::Format_RGB555:
       
    98     case QImage::Format_ARGB8555_Premultiplied:
       
    99             break;
       
   100     case QImage::Format_RGB888:
       
   101             format = VGI_COLOR_BUFFER_FORMAT_RGB888;
       
   102             break;
       
   103     case QImage::Format_RGB444:
       
   104     case QImage::Format_ARGB4444_Premultiplied:
       
   105     case QImage::Format_Invalid:
       
   106         break;
       
   107     }
       
   108     return format;
       
   109 }
       
   110 
       
   111 static HbNvgRasterizer *nvgRasterizer = 0;
       
   112 
       
   113 HbNvgRasterizer* HbNvgRasterizer::global()
       
   114 {
       
   115     if (!nvgRasterizer) {
       
   116         nvgRasterizer = new HbNvgRasterizer();
       
   117     }
       
   118     return nvgRasterizer;
       
   119 }
       
   120 
       
   121 HbNvgRasterizer::HbNvgRasterizer() : vgiSymbianInit(false), nvgEngine(0)
       
   122 {
       
   123 }
       
   124 
       
   125 HbNvgRasterizer::~HbNvgRasterizer()
       
   126 {
       
   127     terminate();
       
   128 }
       
   129 
       
   130 bool HbNvgRasterizer::initialize(int width, int height)
       
   131 {
       
   132     vgiSymbianInit = false;
       
   133 
       
   134     TInt err =  VGISymbianInitialize( TSize(width, height), VGI_COLORSPACE_SRGB );
       
   135     if( err == KErrAlreadyExists || err == KErrNone) {
       
   136         vgiSymbianInit = true;
       
   137     }
       
   138 
       
   139     if (vgiSymbianInit) {
       
   140         TRAP(err, nvgEngine = CNvgEngine::NewL());
       
   141         if (err != KErrNone) {
       
   142             return (vgiSymbianInit = false);
       
   143         }
       
   144     }
       
   145 
       
   146     return vgiSymbianInit;
       
   147 }
       
   148 
       
   149 bool HbNvgRasterizer::terminate()
       
   150 {
       
   151     if (vgiSymbianInit) {
       
   152         VGISymbianTerminate();
       
   153         vgiSymbianInit = false;
       
   154     }
       
   155 
       
   156     return true;
       
   157 }
       
   158 
       
   159 bool HbNvgRasterizer::rasterize(const QByteArray &nvgData,
       
   160                    const QSizeF &renderSize,
       
   161                    Qt::AspectRatioMode aspectRatioMode,
       
   162                    QImage & destination,
       
   163                    QImage::Format imageFormat)
       
   164 {
       
   165     if (destination.isNull()) {
       
   166         destination = QImage(renderSize.toSize(), imageFormat);
       
   167     }
       
   168 
       
   169     TUint8 * imageData = destination.bits();
       
   170     TInt stride = destination.bytesPerLine();
       
   171 
       
   172     return rasterize(nvgData, renderSize, aspectRatioMode, imageData, stride, imageFormat);
       
   173 }
       
   174 
       
   175 bool HbNvgRasterizer::rasterize(const QByteArray &nvgData,
       
   176                const QSizeF &renderSize,
       
   177                Qt::AspectRatioMode aspectRatioMode,
       
   178                void * destination, int stride,
       
   179                QImage::Format imageFormat)
       
   180 {
       
   181     initialize(renderSize.width(), renderSize.height());
       
   182     bool isIconCreated = false;
       
   183     TSize surfaceSize(TSize(renderSize.width(), renderSize.height()));
       
   184 
       
   185     if (!vgiSymbianInit) {
       
   186         if (!initialize(surfaceSize.iWidth, surfaceSize.iHeight)) {
       
   187             return isIconCreated;
       
   188         }
       
   189     }
       
   190 
       
   191     TInt err = VGISymbianResize(surfaceSize);
       
   192     if(err != KErrNone) {
       
   193         return isIconCreated;
       
   194     }
       
   195 
       
   196     HbNvgAspectRatioSettings settings = mapKeyAspectRatioToNvgAspectRatio(aspectRatioMode);
       
   197     nvgEngine->SetPreserveAspectRatio(settings.nvgAlignStatusAndAspectRatio, settings.type);
       
   198 
       
   199 
       
   200     nvgEngine->SetBackgroundColor(0xFFFFFF00);
       
   201 
       
   202     TPtr8 data((unsigned char*)nvgData.data(), nvgData.length(), nvgData.length());
       
   203     err = nvgEngine->DrawNvg(data, surfaceSize, 0, 0);
       
   204     if(err !=KErrNone) {
       
   205         return isIconCreated;
       
   206     }
       
   207 
       
   208     //copy the data from the surface
       
   209     VGIColorBufferFormat format = (VGIColorBufferFormat)mapToVgiDisplayFormat(imageFormat);
       
   210 
       
   211     err = VGICopyToTarget(format, stride, destination, 0, NULL, VGI_COPY_TRANSPARENT_PIXELS);
       
   212     if (err == VGI_OK) {
       
   213         isIconCreated = true;
       
   214     }
       
   215 
       
   216     // destroy the previous surface,
       
   217     // as we don't have a way to clear the surface
       
   218     terminate();
       
   219 
       
   220     return isIconCreated;
       
   221 }
       
   222