uifw/ganes/src/HgVgImageCreator.cpp
changeset 47 2f0c06423c72
parent 46 0e1e0022bd03
child 53 3c67ea82fafc
equal deleted inserted replaced
46:0e1e0022bd03 47:2f0c06423c72
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:     
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 
       
    20 #include "HgVgImageCreator.h"
       
    21 #include "HgVgEGL.h"
       
    22 
       
    23 #include <e32math.h>
       
    24 #include <gulicon.h>
       
    25 #include <fbs.h>
       
    26 #include <nvg.h>
       
    27 #include <AknIconHeader.h>
       
    28 #include <AknIconUtils.h>
       
    29 
       
    30 
       
    31 static const TUid KUidMySingleton = { 0x100000E9 };
       
    32 
       
    33 
       
    34 static TAknIconHeader GetNvgIconHeader(HBufC8* aNVGData)
       
    35     {
       
    36     // Parse the icon header info from the extended data
       
    37     TPtr8 IconHeaderPtr((TUint8*)aNVGData->Des().Ptr(), KIconHeaderLength, KIconHeaderLength);
       
    38     TAknIconHeader iconheader(IconHeaderPtr);
       
    39     
       
    40     return iconheader;
       
    41     }
       
    42     
       
    43 static TPtr8 GetNvgDataWithoutHeader(HBufC8* aNVGData)
       
    44     {
       
    45     // The rest of the data (after the iconheader) are the OVG drawing instructions
       
    46     TInt lengthAfterHeader = aNVGData->Length() - KIconHeaderLength;
       
    47     TPtr8 nvgDataVoidIC((TUint8 *)aNVGData->Des().Ptr() + KIconHeaderLength, lengthAfterHeader, lengthAfterHeader);
       
    48     
       
    49     return nvgDataVoidIC;
       
    50     }
       
    51 
       
    52 static HBufC8* ReadNVGDataL(const CFbsBitmap& aBitmap)
       
    53     {
       
    54     // Fetch the extended data
       
    55     aBitmap.BeginDataAccess();
       
    56     const TUint32* data = aBitmap.DataAddress();
       
    57     TInt dataSize = aBitmap.DataSize();
       
    58     TUint8* compressedData = new (ELeave) TUint8[dataSize];
       
    59     CleanupStack::PushL(compressedData);
       
    60     Mem::Copy(compressedData, data, dataSize);
       
    61     aBitmap.EndDataAccess(ETrue);
       
    62     
       
    63     // Create a descriptor out of the extended bitmap data. The iNVGData
       
    64     // will now contain the direct OpenVG commands
       
    65     TPtr8 nvgDataPtr(compressedData, dataSize, dataSize);
       
    66     HBufC8* dataBuf = nvgDataPtr.AllocL();
       
    67     
       
    68     CleanupStack::PopAndDestroy(compressedData);
       
    69     return dataBuf;
       
    70     }
       
    71 
       
    72 VGImage CHgVgImageCreator::RenderImageFromIconL( const CFbsBitmap* aBitmap )
       
    73     {
       
    74     User::LeaveIfNull(aBitmap);
       
    75     User::LeaveIfNull(iEGL);
       
    76         
       
    77     HBufC8* nvgData = ReadNVGDataL(*aBitmap);
       
    78 
       
    79     TSize size = aBitmap->SizeInPixels();
       
    80 
       
    81     VGImage image = vgCreateImage(VG_sARGB_8888_PRE, size.iWidth, size.iHeight,VG_IMAGE_QUALITY_NONANTIALIASED);
       
    82     
       
    83     const EGLint    KColorAttribList[] =
       
    84             {
       
    85             EGL_NONE
       
    86             };
       
    87 
       
    88     CNvgEngine* nvgEngine = CNvgEngine::NewL();
       
    89     CleanupStack::PushL(nvgEngine);
       
    90     
       
    91     EGLSurface newSurface = eglCreatePbufferFromClientBuffer(
       
    92         iEGL->Display(), EGL_OPENVG_IMAGE,
       
    93         static_cast<EGLClientBuffer>(image),    // Use the image as buffer
       
    94         iEGL->CurrentConfig(), KColorAttribList );
       
    95     
       
    96     eglMakeCurrent( iEGL->Display(), newSurface, newSurface, iEGL->Context() );
       
    97     
       
    98     vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
       
    99     vgLoadIdentity();
       
   100     vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
       
   101     vgLoadIdentity();    
       
   102     vgSeti(VG_BLEND_MODE, VG_BLEND_SRC_OVER);
       
   103     vgSeti( VG_SCISSORING, VG_FALSE );
       
   104     
       
   105 
       
   106     TAknIconHeader iconheader = GetNvgIconHeader(nvgData);        
       
   107     // Set preserve aspect ratio according to the header info
       
   108     TNvgAlignStatusType alignTypeValue = ENvgPreserveAspectRatio_XmidYmid;
       
   109     TNvgMeetOrSliceType meetOrSliceTypeValue = ENvgMeet;
       
   110     if ( iconheader.GetScaleMode() == EAspectRatioPreserved )
       
   111         {
       
   112         
       
   113         }
       
   114     else if (iconheader.GetScaleMode() == EAspectRatioPreservedSlice)
       
   115         {
       
   116         meetOrSliceTypeValue = ENvgSlice;
       
   117         }
       
   118     else if (iconheader.GetScaleMode() == EAspectRatioPreservedAndUnusedSpaceRemoved || 
       
   119             iconheader.GetScaleMode() == EAspectRatioNotPreserved )
       
   120         {
       
   121         alignTypeValue = ENvgPreserveAspectRatio_None;
       
   122         }
       
   123 
       
   124     nvgEngine->SetPreserveAspectRatio(alignTypeValue, meetOrSliceTypeValue);
       
   125     nvgEngine->Rotate(iconheader.GetRotation(),size.iWidth >>1, size.iHeight >>1);
       
   126     
       
   127     nvgEngine->DrawNvg(GetNvgDataWithoutHeader(nvgData), size, NULL, NULL);
       
   128         
       
   129     // TODO: Error handling. DrawNvg mey fail icon rendering.
       
   130     
       
   131     delete nvgData;
       
   132 
       
   133     CleanupStack::PopAndDestroy(nvgEngine);
       
   134     
       
   135     // TODO: should we call explicitly vgFlush at this point to force all commands
       
   136     // complete before eglMakeCurrent?
       
   137     
       
   138     eglMakeCurrent(iEGL->Display(), iEGL->Surface(), iEGL->Surface(), iEGL->Context());
       
   139         
       
   140     if ( newSurface != EGL_NO_SURFACE )
       
   141         {
       
   142         eglDestroySurface( iEGL->Display(), newSurface );
       
   143         }
       
   144 
       
   145     // reset states, and matrices which nvg lefts dirty.    
       
   146     vgSeti( VG_BLEND_MODE, VG_BLEND_SRC_OVER );
       
   147     vgSeti( VG_SCISSORING, VG_FALSE );
       
   148     vgSeti( VG_MASKING, VG_FALSE );
       
   149     vgSeti( VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER );
       
   150     vgLoadIdentity();
       
   151     vgSeti( VG_MATRIX_MODE, VG_MATRIX_STROKE_PAINT_TO_USER );
       
   152     vgLoadIdentity();
       
   153     
       
   154     return image;
       
   155 
       
   156     }
       
   157 
       
   158 CHgVgImageCreator* CHgVgImageCreator::InstanceL()
       
   159     {
       
   160     CHgVgImageCreator* instance = static_cast<CHgVgImageCreator*>( CCoeEnv::Static( KUidMySingleton ) );
       
   161     
       
   162     if ( !instance )
       
   163         {
       
   164         instance = new ( ELeave ) CHgVgImageCreator;
       
   165         CleanupStack::PushL( instance );
       
   166         instance->ConstructL();
       
   167         CleanupStack::Pop();
       
   168         }
       
   169     
       
   170     return instance;
       
   171     }
       
   172 
       
   173     
       
   174 CHgVgImageCreator::CHgVgImageCreator() : CCoeStatic( KUidMySingleton )
       
   175      {
       
   176      }
       
   177 
       
   178 CHgVgImageCreator::~CHgVgImageCreator()
       
   179     {
       
   180     //delete iNvgEngine;
       
   181     }
       
   182 
       
   183 void CHgVgImageCreator::ConstructL()
       
   184     {
       
   185     //iNvgEngine = CNvgEngine::NewL();
       
   186     }
       
   187     
       
   188 void CHgVgImageCreator::Initialize(CHgVgEGL* aEGL)
       
   189     {
       
   190     iEGL = aEGL;
       
   191     }
       
   192 
       
   193 // End of File