uifw/ganes/src/HgVgHelper.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 "HgVgHelper.h"
       
    21 #include "HgVgDrawBuffer.h"
       
    22 #include "HgVgImageCreator.h"
       
    23 
       
    24 #include <e32math.h>
       
    25 #include <gulicon.h>
       
    26 #include <fbs.h>
       
    27 #include <nvg.h>
       
    28 #include <AknIconHeader.h>
       
    29 #include <AknIconUtils.h>
       
    30 
       
    31 const TInt KVertexX(0);
       
    32 const TInt KVertexY(1);
       
    33 const TInt KVertexZ(2);
       
    34 const TInt KNumColorValues(4);
       
    35 const TInt KNumColorTransformValues(8);
       
    36 const VGfloat KColorByteToFloatFactor(255.0f);
       
    37 
       
    38 
       
    39 namespace HgVgHelper
       
    40     {
       
    41 
       
    42 static VGImage CreateNonMaskedVgImageL( const CFbsBitmap& aBitmap )
       
    43     {
       
    44     TSize size = aBitmap.SizeInPixels();    
       
    45     VGImage image = vgCreateImage(VG_sRGB_565, size.iWidth, size.iHeight,VG_IMAGE_QUALITY_NONANTIALIASED);        
       
    46     VGErrorCode error = vgGetError();
       
    47     if (image == VG_INVALID_HANDLE || error == VG_OUT_OF_MEMORY_ERROR)
       
    48         {
       
    49         User::Leave(KErrNoMemory);
       
    50         }
       
    51     if (aBitmap.DisplayMode() == EColor64K && !aBitmap.IsCompressedInRAM())
       
    52         {
       
    53         aBitmap.BeginDataAccess();
       
    54         TInt stride = aBitmap.DataStride();
       
    55         TUint8* ptr = (TUint8*)aBitmap.DataAddress();
       
    56         vgImageSubData (image, ptr, stride, VG_sRGB_565, 0, 0, size.iWidth, size.iHeight );        
       
    57         aBitmap.EndDataAccess();
       
    58         }
       
    59     else
       
    60         {
       
    61         TSize size = aBitmap.SizeInPixels();
       
    62         CHgVgDrawBuffer* temp = CHgVgDrawBuffer::NewL(size, EColor64K);
       
    63         temp->Gc().BitBlt(TPoint(0,0), &aBitmap);
       
    64         temp->GetDrawBufferToVgImage(size, TPoint(0,0), image, 
       
    65                 VG_sRGB_565);
       
    66         delete temp;        
       
    67         }
       
    68     return image;
       
    69     }
       
    70 
       
    71 static VGImage CreateMaskedVgImageL( CFbsBitmap* aBitmap, CFbsBitmap* aMask )
       
    72     {
       
    73     TSize size = aBitmap->SizeInPixels();
       
    74     CHgVgDrawBuffer* temp = CHgVgDrawBuffer::NewL(size, EColor16MA);
       
    75     temp->Gc().SetDrawMode(CGraphicsContext::EDrawModeWriteAlpha);
       
    76     temp->Gc().SetBrushColor(TRgb(0,0,0,0));
       
    77     temp->Gc().Clear();
       
    78     temp->Gc().BitBltMasked(TPoint(0,0), aBitmap, size, aMask, EFalse);
       
    79     
       
    80     VGImage image = vgCreateImage(VG_sRGBA_8888, size.iWidth, size.iHeight,VG_IMAGE_QUALITY_NONANTIALIASED);
       
    81     temp->GetDrawBufferToVgImage(size, TPoint(0,0), image, 
       
    82             VG_sARGB_8888);
       
    83     
       
    84     delete temp;
       
    85     return image;
       
    86     }
       
    87     
       
    88 // ---------------------------------------------------------------------------
       
    89 // HgVgHelper::CreateVgImageFromIconL()
       
    90 // ---------------------------------------------------------------------------
       
    91 //     
       
    92 VGImage CreateVgImageFromIconL(const CGulIcon& aIcon)
       
    93     {
       
    94         
       
    95     CFbsBitmap* bitmap = aIcon.Bitmap();
       
    96     User::LeaveIfNull(bitmap);
       
    97     CFbsBitmap* mask = aIcon.Mask();
       
    98     
       
    99     // if this is NVG image, rasterize it using 
       
   100     // nvg engine
       
   101     if (bitmap->ExtendedBitmapType() != KNullUid)
       
   102         {
       
   103         return CHgVgImageCreator::InstanceL()->RenderImageFromIconL(bitmap);
       
   104         }
       
   105     else
       
   106         {
       
   107         // otherwise just blit/copy to vg image.
       
   108         if (mask)
       
   109             {
       
   110             return CreateMaskedVgImageL(bitmap, mask);
       
   111             }
       
   112         else
       
   113             {
       
   114             return CreateNonMaskedVgImageL(*bitmap); 
       
   115             }
       
   116         }
       
   117     
       
   118     }
       
   119         
       
   120 // ---------------------------------------------------------------------------
       
   121 // HgVgHelper::Clamp()
       
   122 // ---------------------------------------------------------------------------
       
   123 //     
       
   124 VGfloat Clamp(VGfloat value, VGfloat min, VGfloat max)
       
   125     {
       
   126     VGfloat result = (value < min) ? min : value;
       
   127     return (result > max) ? max : result;
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 // HgVgHelper::Lerp()
       
   132 // ---------------------------------------------------------------------------
       
   133 //     
       
   134 VGfloat Lerp(VGfloat start, VGfloat end, VGfloat t)
       
   135     {
       
   136     return start * (1.0f - t) + end * t;
       
   137     }
       
   138 
       
   139 // ---------------------------------------------------------------------------
       
   140 // HgVgHelper::Abs()
       
   141 // ---------------------------------------------------------------------------
       
   142 //     
       
   143 VGfloat Abs(VGfloat value)
       
   144     {
       
   145     return (value < 0) ? -value : value;
       
   146     }
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 // HgVgHelper::CalculateBoudingRect()
       
   150 // ---------------------------------------------------------------------------
       
   151 //     
       
   152 void CalculateBoundingRect(TRect& aRect, VGfloat* aPoints, TInt aNumPoints, 
       
   153         const TRect& aWindowRect)
       
   154     {
       
   155     TInt height = aWindowRect.Height();
       
   156     
       
   157     TPoint min((TInt)aPoints[0], height - (TInt)aPoints[1]);
       
   158     TPoint max((TInt)aPoints[0], height - (TInt)aPoints[1]);
       
   159     
       
   160     for (TInt i = 0; i < aNumPoints; i++)
       
   161         {
       
   162         
       
   163         TPoint temp((TInt)aPoints[i*2+0], height - (TInt)aPoints[i*2+1]);
       
   164         
       
   165         min.iX = (temp.iX < min.iX) ? temp.iX : min.iX;
       
   166         max.iX = (temp.iX > max.iX) ? temp.iX : max.iX;
       
   167 
       
   168         min.iY = (temp.iY < min.iY) ? temp.iY : min.iY;
       
   169         max.iY = (temp.iY > max.iY) ? temp.iY : max.iY;
       
   170 
       
   171         }
       
   172     
       
   173     // Top Left
       
   174     aRect.iTl = min;
       
   175     
       
   176     // Bottom Right
       
   177     aRect.iBr = max;        
       
   178 
       
   179     }
       
   180 
       
   181 void TVertex::ProjectPoint(VGfloat aScreenWidth, VGfloat aScreenHeight, VGfloat aFov)
       
   182     {
       
   183     VGfloat hw = aScreenWidth * 0.5f;
       
   184     VGfloat hh = aScreenHeight * 0.5f;
       
   185     VGfloat alpha = aFov / 2.0f;
       
   186     double tanAlpha;
       
   187     Math::Tan(tanAlpha, alpha);
       
   188     VGfloat d = hw / tanAlpha;    
       
   189     iScreenX = (VGfloat)(hw + d * iTx / iTz);
       
   190     iScreenY = (VGfloat)(hh + d * iTy / iTz);    
       
   191     }
       
   192 
       
   193 void TVertex::TransformPoint(const TMatrix& aMatrix)
       
   194     {
       
   195     iTx = iX * aMatrix.iM[0][0] + iY * aMatrix.iM[1][0] + iZ * aMatrix.iM[2][0] + aMatrix.iM[3][0];
       
   196     iTy = iX * aMatrix.iM[0][1] + iY * aMatrix.iM[1][1] + iZ * aMatrix.iM[2][1] + aMatrix.iM[3][1];
       
   197     iTz = iX * aMatrix.iM[0][2] + iY * aMatrix.iM[1][2] + iZ * aMatrix.iM[2][2] + aMatrix.iM[3][2];
       
   198     }
       
   199 
       
   200 TMatrix::TMatrix()
       
   201     {
       
   202     }
       
   203 
       
   204 TMatrix::TMatrix(const TMatrix& rhs)
       
   205     {
       
   206             (*this) = rhs;
       
   207     }
       
   208 
       
   209 TMatrix::~TMatrix()
       
   210     {
       
   211     }
       
   212 
       
   213 TMatrix& TMatrix::operator=(const TMatrix& rhs)
       
   214     {
       
   215         for (int i = 0; i < 4; ++i)
       
   216         {
       
   217                 for (int j = 0; j < 4; ++j)
       
   218                 {
       
   219                         iM[i][j] = rhs.iM[i][j];
       
   220                 }
       
   221         }
       
   222         return *this;
       
   223         }
       
   224             
       
   225 void TMatrix::Identity()
       
   226     {
       
   227             iM[0][0] = 1.0f;
       
   228             iM[0][1] = 0.0f;
       
   229             iM[0][2] = 0.0f;
       
   230             iM[0][3] = 0.0f;
       
   231 
       
   232             iM[1][0] = 0.0f;
       
   233             iM[1][1] = 1.0f;
       
   234             iM[1][2] = 0.0f;
       
   235             iM[1][3] = 0.0f;
       
   236 
       
   237             iM[2][0] = 0.0f;
       
   238             iM[2][1] = 0.0f;
       
   239             iM[2][2] = 1.0f;
       
   240             iM[2][3] = 0.0f;
       
   241 
       
   242             iM[3][0] = 0.0f;
       
   243             iM[3][1] = 0.0f;
       
   244             iM[3][2] = 0.0f;
       
   245             iM[3][3] = 1.0f;
       
   246     }
       
   247    
       
   248        
       
   249 void TMatrix::Multiply(const TMatrix& rhs)
       
   250     {
       
   251         VGfloat temp[4][4];
       
   252         for (int i = 0; i < 4; ++i)
       
   253         {
       
   254                 for (int j = 0; j < 4; ++j)
       
   255                 {
       
   256                         temp[i][j] =
       
   257                         iM[i][0] * rhs.iM[0][j] +
       
   258                         iM[i][1] * rhs.iM[1][j] +
       
   259                         iM[i][2] * rhs.iM[2][j] +
       
   260                         iM[i][3] * rhs.iM[3][j];
       
   261                 }
       
   262         }
       
   263         for (int i = 0; i < 4; ++i)
       
   264         {
       
   265                 for (int j = 0; j < 4; ++j)
       
   266                 {
       
   267                 iM[i][j] = temp[i][j];
       
   268                 }
       
   269         }
       
   270     }
       
   271     
       
   272     
       
   273 void TMatrix::RotationX(VGfloat angle)
       
   274     {
       
   275     double sa, ca;
       
   276     Math::Sin(sa, angle);
       
   277     Math::Cos(ca, angle);
       
   278 
       
   279     iM[0][0] = 1.0f;
       
   280     iM[0][1] = 0.0f;
       
   281     iM[0][2] = 0.0f;
       
   282     iM[0][3] = 0.0f;
       
   283 
       
   284     iM[1][0] = 0.0f;
       
   285     iM[1][1] = ca;
       
   286     iM[1][2] = sa;
       
   287     iM[1][3] = 0.0f;
       
   288 
       
   289     iM[2][0] = 0.0f;
       
   290     iM[2][1] = -sa;
       
   291     iM[2][2] = ca;
       
   292     iM[2][3] = 0.0f;
       
   293 
       
   294     iM[3][0] = 0.0f;
       
   295     iM[3][1] = 0.0f;
       
   296     iM[3][2] = 0.0f;
       
   297     iM[3][3] = 1.0f;
       
   298     }
       
   299 
       
   300 
       
   301 void TMatrix::RotationY(float angle)
       
   302     {
       
   303     double sa, ca;
       
   304     Math::Sin(sa, angle);
       
   305     Math::Cos(ca, angle);
       
   306 
       
   307     iM[0][0] = ca;
       
   308     iM[0][1] = 0.0f;
       
   309     iM[0][2] = -sa;
       
   310     iM[0][3] = 0.0f;
       
   311 
       
   312     iM[1][0] = 0.0f;
       
   313     iM[1][1] = 1.0f;
       
   314     iM[1][2] = 0.0f;
       
   315     iM[1][3] = 0.0f;
       
   316 
       
   317     iM[2][0] = sa;
       
   318     iM[2][1] = 0.0f;
       
   319     iM[2][2] = ca;
       
   320     iM[2][3] = 0.0f;
       
   321 
       
   322     iM[3][0] = 0.0f;
       
   323     iM[3][1] = 0.0f;
       
   324     iM[3][2] = 0.0f;
       
   325     iM[3][3] = 1.0f;
       
   326     }
       
   327 
       
   328 void TMatrix::Scale(VGfloat aSx, VGfloat aSy, VGfloat aSz)
       
   329     {
       
   330     Identity();
       
   331     iM[0][0] = aSx;
       
   332     iM[1][1] = aSy;
       
   333     iM[2][2] = aSz;
       
   334     }
       
   335 
       
   336 void TMatrix::Translation(VGfloat aX, VGfloat aY, VGfloat aZ)
       
   337 {
       
   338     Identity();
       
   339 
       
   340     iM[3][0] = aX;
       
   341     iM[3][1] = aY;
       
   342     iM[3][2] = aZ;
       
   343 }
       
   344 
       
   345 
       
   346 // ---------------------------------------------------------------------------
       
   347 // HgVgHelper::ComputeRotationMatrixByY
       
   348 // ---------------------------------------------------------------------------
       
   349 //     
       
   350 void ComputeRotationMatrixByY(VGfloat* matrix, VGfloat angle)
       
   351     {
       
   352     // Build rotation matrix around Y-axis
       
   353     double sinrad, cosrad;
       
   354     Math::Sin (sinrad, angle );
       
   355     Math::Cos (cosrad, angle );
       
   356     VGfloat sa = sinrad;
       
   357     VGfloat ca = cosrad;
       
   358 
       
   359     VGfloat rotMatrix[] =
       
   360         {
       
   361         ca, 0, sa, 
       
   362         0, 1, 0, 
       
   363         sa, 0, -ca
       
   364         };
       
   365 
       
   366     // copy to result.
       
   367     for (int i = 0; i < 9; i++ )
       
   368         matrix[i] = rotMatrix[i];    
       
   369     }
       
   370 
       
   371 
       
   372 // ---------------------------------------------------------------------------
       
   373 // HgVgHelper::MultiplyMatrix()
       
   374 // ---------------------------------------------------------------------------
       
   375 //     
       
   376 void MultiplyMatrix(VGfloat* aResult, VGfloat* aA, VGfloat* aB)
       
   377     {
       
   378     for (VGint i = 0; i < 3; ++i)
       
   379         {
       
   380             for (VGint j = 0; j < 3; ++j)
       
   381             {
       
   382                     aResult[i*3+j] =
       
   383                     aA[j*3+0] * aB[i*3+0] +
       
   384                     aA[j*3+1] * aB[i*3+1] +
       
   385                     aA[j*3+2] * aB[i*3+2];
       
   386             }
       
   387         }
       
   388     }
       
   389 
       
   390 
       
   391 // ---------------------------------------------------------------------------
       
   392 // HgVgHelper::TransformVertex
       
   393 // ---------------------------------------------------------------------------
       
   394 //         
       
   395 void TransformVertex(const VGfloat* inVertex, VGfloat* outVertex, 
       
   396         const VGfloat* matrix)
       
   397     {
       
   398     VGfloat x = inVertex[KVertexX];
       
   399     VGfloat y = inVertex[KVertexY];
       
   400     VGfloat z = inVertex[KVertexZ];
       
   401 
       
   402     outVertex[KVertexX] = x * matrix[0] + y * matrix[3] + z * matrix[6];
       
   403     outVertex[KVertexY] = x * matrix[1] + y * matrix[4] + z * matrix[7];
       
   404     outVertex[KVertexZ] = x * matrix[2] + y * matrix[5] + z * matrix[8];
       
   405     }
       
   406 
       
   407 // ---------------------------------------------------------------------------
       
   408 // HgVgHelper::PerspectiveTransformVertex
       
   409 // ---------------------------------------------------------------------------
       
   410 //         
       
   411 void PerspectiveTransformVertex(VGfloat* aInVertex, VGfloat* aOutVertex,
       
   412         VGfloat aScreenWidth, VGfloat aScreenHeight, VGfloat aFov)
       
   413     {
       
   414 /*    VGfloat x = inVertex[KVertexX];
       
   415     VGfloat y = inVertex[KVertexY];
       
   416     VGfloat z = inVertex[KVertexZ];
       
   417 
       
   418     outVertex[KVertexX] = screenWidth * 0.5f + fov * x / z;
       
   419     outVertex[KVertexY] = screenHeight * 0.5f + fov * y / z;
       
   420 */    
       
   421     VGfloat hw = aScreenWidth * 0.5f;
       
   422     VGfloat hh = aScreenHeight * 0.5f;
       
   423     VGfloat alpha = aFov / 2.0f;
       
   424     double tanAlpha;
       
   425     Math::Tan(tanAlpha, alpha);
       
   426     VGfloat d = hw / tanAlpha;    
       
   427     aOutVertex[KVertexX] = (VGfloat)(hw + d * aInVertex[KVertexX] / aInVertex[KVertexZ]);
       
   428     aOutVertex[KVertexY] = (VGfloat)(hh + d * aInVertex[KVertexY] / aInVertex[KVertexZ]);    
       
   429         
       
   430     }
       
   431 
       
   432 // ---------------------------------------------------------------------------
       
   433 // HgVgHelper::DrawAlphaImage()
       
   434 // ---------------------------------------------------------------------------
       
   435 //         
       
   436 void DrawImage(VGImage aImage, 
       
   437         const TPoint& aPos, const TRect& aWindowRect, TBool aCentered)
       
   438     {
       
   439             
       
   440     vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
       
   441     vgLoadIdentity();
       
   442 
       
   443     if (!aCentered)
       
   444         {
       
   445         vgScale(1, -1);
       
   446         vgTranslate(aPos.iX, -(aWindowRect.Height() - aPos.iY));
       
   447         }
       
   448     else
       
   449         {
       
   450         VGint w = vgGetParameteri(aImage, VG_IMAGE_WIDTH);
       
   451         VGint h = vgGetParameteri(aImage, VG_IMAGE_HEIGHT);        
       
   452         vgTranslate(aPos.iX, (aWindowRect.Height() - aPos.iY));
       
   453         vgScale(1, -1);        
       
   454         vgTranslate(-(VGfloat)w/2,-(VGfloat)h/2);
       
   455         }
       
   456     
       
   457     vgDrawImage(aImage);        
       
   458 
       
   459     }
       
   460 
       
   461 // ---------------------------------------------------------------------------
       
   462 // HgVgHelper::DrawImageColorized
       
   463 // ---------------------------------------------------------------------------
       
   464 //         
       
   465 void DrawImageColorized(VGImage aImage, const TRgb& aColor, 
       
   466         const TPoint& aPos, const TRect& aWindowRect, TBool aCentered)
       
   467     {
       
   468     
       
   469     VGfloat values[] = { 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 };
       
   470     
       
   471     values[0] = (VGfloat)aColor.Red() / KColorByteToFloatFactor;
       
   472     values[1] = (VGfloat)aColor.Green() / KColorByteToFloatFactor;
       
   473     values[2] = (VGfloat)aColor.Blue() / KColorByteToFloatFactor;
       
   474     values[3] = (VGfloat)aColor.Alpha() / KColorByteToFloatFactor;
       
   475             
       
   476     vgSetfv(VG_COLOR_TRANSFORM_VALUES, KNumColorTransformValues, values);
       
   477     vgSeti(VG_COLOR_TRANSFORM, VG_TRUE);
       
   478     
       
   479     HgVgHelper::DrawImage(aImage, aPos, aWindowRect, aCentered);
       
   480 
       
   481     vgSeti(VG_COLOR_TRANSFORM, VG_FALSE);        
       
   482     }
       
   483 
       
   484 
       
   485 
       
   486 // ---------------------------------------------------------------------------
       
   487 // HgVgHelper::CreatePath()
       
   488 // ---------------------------------------------------------------------------
       
   489 //         
       
   490 VGPath CreatePath(VGuint aNumSegments, const VGubyte* aSegments, const VGfloat* aPoints)
       
   491     {
       
   492     VGPath path = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 
       
   493             1.0f, 0.0f, 4, 4, (unsigned int)VG_PATH_CAPABILITY_ALL);
       
   494           
       
   495     vgAppendPathData(path, aNumSegments, aSegments, aPoints);
       
   496     
       
   497     return path;
       
   498     }
       
   499     
       
   500 // ---------------------------------------------------------------------------
       
   501 // HgVgHelper::CreateColorPaint()
       
   502 // ---------------------------------------------------------------------------
       
   503 //         
       
   504 VGPaint CreateColorPaint(const VGfloat* aColor)
       
   505     {
       
   506     VGPaint paint = vgCreatePaint();
       
   507     
       
   508     vgSetParameteri(paint, VG_PAINT_TYPE, 
       
   509         VG_PAINT_TYPE_COLOR);
       
   510     
       
   511     vgSetParameterfv(paint, VG_PAINT_COLOR,
       
   512         KNumColorValues, aColor);        
       
   513     
       
   514     return paint;
       
   515     }
       
   516 
       
   517 // ---------------------------------------------------------------------------
       
   518 // HgVgHelper::CreateColorPaint()
       
   519 // ---------------------------------------------------------------------------
       
   520 //         
       
   521 VGPaint CreateColorPaint(const TRgb& aColor)
       
   522     {
       
   523     VGfloat values[4];
       
   524     
       
   525     values[0] = (VGfloat)aColor.Red() / KColorByteToFloatFactor;
       
   526     values[1] = (VGfloat)aColor.Green() / KColorByteToFloatFactor;
       
   527     values[2] = (VGfloat)aColor.Blue() / KColorByteToFloatFactor;
       
   528     values[3] = (VGfloat)aColor.Alpha() / KColorByteToFloatFactor;
       
   529 
       
   530     return CreateColorPaint(values);
       
   531     }
       
   532 
       
   533     }
       
   534 
       
   535 // End of File