javauis/lcdui_akn/javalcdui/src.nokialcdui/CMIDDirectGraphics.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  Native side interface for DirectGraphics java class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <jutils.h> // MIDMakeHandle
       
    20 #include <gdi.h> // CWsBitmap
       
    21 
       
    22 #include "CMIDImage.h" // CMIDImage
       
    23 #include "CMIDGraphics.h" // CMIDGraphics
       
    24 #ifdef RD_JAVA_NGA_ENABLED
       
    25 #include "lcdui.h"
       
    26 #endif
       
    27 
       
    28 #include "CMIDDirectGraphics.h"
       
    29 #include "CMIDNativeConverter.h"
       
    30 #include "CMIDConvertFactory.h"
       
    31 #include "TMIDInternalARGB.h"
       
    32 #include "nativecolors.h"
       
    33 #include "TMIDBitmapParameters.h"
       
    34 
       
    35 namespace
       
    36 {
       
    37 const TUint8 KDivByEight = 3;
       
    38 const TInt KGray2Divider = 32;
       
    39 const TInt KGray4Divider = 16;
       
    40 const TInt KColor16Divider = 8;
       
    41 const TInt KColor256Divider = 4;
       
    42 const TInt KColor64KDivider = 2;
       
    43 const TInt KMaxInteger = 2147483647;
       
    44 }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CMIDDirectGraphics::NewL
       
    48 // Two-phased constructor.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CMIDDirectGraphics* CMIDDirectGraphics::NewL(CMIDGraphics* aGraphics,
       
    52         MMIDImage* aImage)
       
    53 {
       
    54     CMIDDirectGraphics* graphics = new(ELeave) CMIDDirectGraphics();
       
    55     CleanupStack::PushL(graphics);
       
    56     graphics->ConstructL(aGraphics, aImage);
       
    57     CleanupStack::Pop(graphics);
       
    58     return graphics;
       
    59 }
       
    60 
       
    61 CMIDDirectGraphics::CMIDDirectGraphics():
       
    62         iDottedLine(EFalse)
       
    63 {
       
    64 }
       
    65 
       
    66 CMIDDirectGraphics::~CMIDDirectGraphics()
       
    67 {
       
    68     delete iConverterFactory;
       
    69     delete iNativeConverter;
       
    70     delete iPolygonFiller;
       
    71 }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CMIDDirectGraphics::ConstructL
       
    75 // Symbian 2nd phase constructor can leave.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 void CMIDDirectGraphics::ConstructL(CMIDGraphics* aGraphics,
       
    79                                     MMIDImage *aImage)
       
    80 {
       
    81     User::LeaveIfNull(aGraphics);
       
    82     iGraphics = aGraphics;
       
    83 
       
    84 #ifdef RD_JAVA_NGA_ENABLED
       
    85     iCanvasTarget = iGraphics->GetTargetCanvas();
       
    86 #endif
       
    87 
       
    88     CFbsBitmap* bitmap = iGraphics->Bitmap();
       
    89     User::LeaveIfNull(bitmap);
       
    90 
       
    91     iScreenSize = bitmap->SizeInPixels();
       
    92 
       
    93     TMIDBitmapParameters params;
       
    94     params.iOffset = 0;
       
    95     params.iX = 0;
       
    96     params.iY = 0;
       
    97     params.iWidth = iScreenSize.iWidth;
       
    98     params.iHeight = iScreenSize.iHeight;
       
    99     params.iManipulation = 0;
       
   100     params.iClipRect = TRect(iScreenSize);
       
   101 
       
   102 
       
   103     TDisplayMode displayMode = bitmap->DisplayMode();
       
   104 
       
   105     params.iScanLength = CalculateScanLength(iScreenSize.iWidth,
       
   106                          displayMode);
       
   107 
       
   108 
       
   109     bitmap->LockHeap(ETrue);
       
   110     params.iPixels = bitmap->DataAddress();
       
   111     bitmap->UnlockHeap(ETrue);
       
   112 
       
   113     params.iPixelsSize = (params.iScanLength *
       
   114                           iScreenSize.iHeight *
       
   115                           bitmap->Header().iBitsPerPixel) >> KDivByEight;
       
   116 
       
   117     if (aImage)
       
   118     {
       
   119         // note: getting bitmap image increases its reference count
       
   120         MMIDBitmapImage* bitmapImage = aImage->BitmapImage();
       
   121         CFbsBitmap* alphaBitmap = bitmapImage->AlphaBitmap();
       
   122         if (alphaBitmap)
       
   123         {
       
   124             params.iTransparency = ETrue;
       
   125             alphaBitmap->LockHeap();
       
   126             params.iAlpha = alphaBitmap->DataAddress();
       
   127             params.iAlphaMode = alphaBitmap->DisplayMode();
       
   128             TSize bitmapSize(alphaBitmap->SizeInPixels());
       
   129             TInt scanLineLength =
       
   130                 CFbsBitmap::ScanLineLength(bitmapSize.iWidth,
       
   131                                            alphaBitmap->DisplayMode());
       
   132             TInt bitsPerPixel = alphaBitmap->Header().iBitsPerPixel;
       
   133             scanLineLength = (scanLineLength << KMIDShift3Bits) / bitsPerPixel;
       
   134             params.iAlphaSize = (scanLineLength * bitmapSize.iHeight *
       
   135                                  bitsPerPixel) >> KMIDShift3Bits;
       
   136             alphaBitmap->UnlockHeap();
       
   137         }
       
   138         bitmapImage->RemoveRef();
       
   139     }
       
   140 
       
   141     iNativePixelFormat = CMIDConvertFactory::GetPixelFormat(displayMode);
       
   142     params.iFormat = iNativePixelFormat;
       
   143 
       
   144     iNativeConverter = CMIDNativeConverter::NewL(params);
       
   145 
       
   146     iClipRect = params.iClipRect;
       
   147 
       
   148     iPolygonFiller = new(ELeave) CPolygonFiller();
       
   149 
       
   150     iConverterFactory = new(ELeave) CMIDConvertFactory();
       
   151 
       
   152 
       
   153 }
       
   154 
       
   155 
       
   156 void CMIDDirectGraphics::SetClippingRect(TRect aRect)
       
   157 {
       
   158     TRect screenRect(iScreenSize);
       
   159     screenRect.Intersection(aRect);
       
   160     iClipRect = screenRect;
       
   161 }
       
   162 
       
   163 inline TBool IsFormatARGB(TInt aFormat)
       
   164 {
       
   165     switch (aFormat)
       
   166     {
       
   167     case KMIDTypeInt8888ARGB:
       
   168     case KMIDTypeInt888ARGB:
       
   169     case KMIDTypeUshort4444ARGB:
       
   170     case KMIDTypeUshort1555ARGB:
       
   171         return ETrue;
       
   172 
       
   173     default:
       
   174         return EFalse;
       
   175     }
       
   176 }
       
   177 
       
   178 void CMIDDirectGraphics::SetColorL(TUint32 aColor, TInt aImageHandle)
       
   179 {
       
   180     TMIDInternalARGB internalColor;
       
   181     iNativeConverter->Convert(internalColor, aColor);
       
   182     iNativeConverter->SetDrawRect(iClipRect);
       
   183 
       
   184     // if native pixel format is ARGB or converted internal color is fully opaque
       
   185     // there is no need to create separate alpha bitmap, just plot
       
   186     if (IsFormatARGB(iNativePixelFormat) || KAlphaFullOpaque == internalColor.iA)
       
   187     {
       
   188         iGraphics->Bitmap()->LockHeap(ETrue);
       
   189 
       
   190         // bitmaps can change so have to re-install them every time
       
   191         iNativeConverter->SetBitmaps(iGraphics->Bitmap()->DataAddress());
       
   192 
       
   193         do
       
   194         {
       
   195             do
       
   196             {
       
   197                 iNativeConverter->PlotPixel(internalColor);
       
   198             }
       
   199             while (iNativeConverter->AdvanceX());
       
   200         }
       
   201         while (iNativeConverter->AdvanceY());
       
   202 
       
   203         iGraphics->Bitmap()->UnlockHeap(ETrue);
       
   204     }
       
   205     // otherwise create separate alpha bitmap and set possible alpha channel
       
   206     else
       
   207     {
       
   208         CMIDImage* image = MIDUnhandObject< CMIDImage >(aImageHandle);
       
   209 
       
   210         User::LeaveIfError(image->SetTransparencyType(MMIDImage::EAlpha));
       
   211 
       
   212         MMIDBitmapImage* bitmapImage = image->BitmapImage();
       
   213         CFbsBitmap* bitmap = bitmapImage->ColorBitmap();
       
   214         CFbsBitmap* alphaBitmap = bitmapImage->AlphaBitmap();
       
   215         __ASSERT_DEBUG(alphaBitmap->SizeInPixels() == bitmap->SizeInPixels(),
       
   216                        User::Invariant());
       
   217         TMIDBitmapParameters params;
       
   218         params.iOffset = 0;
       
   219         params.iX = 0;
       
   220         params.iY = 0;
       
   221         params.iWidth = iScreenSize.iWidth;
       
   222         params.iHeight = iScreenSize.iHeight;
       
   223         params.iManipulation = 0;
       
   224         params.iClipRect = TRect(iScreenSize);
       
   225 
       
   226         TDisplayMode displayMode = bitmap->DisplayMode();
       
   227 
       
   228         params.iScanLength = CalculateScanLength(iScreenSize.iWidth,
       
   229                              displayMode);
       
   230 
       
   231         bitmap->LockHeap(ETrue);
       
   232         params.iPixels = bitmap->DataAddress();
       
   233         bitmap->UnlockHeap(ETrue);
       
   234 
       
   235         params.iPixelsSize = (params.iScanLength *
       
   236                               iScreenSize.iHeight *
       
   237                               bitmap->Header().iBitsPerPixel) >> KDivByEight;
       
   238         if (alphaBitmap)
       
   239         {
       
   240             alphaBitmap->LockHeap(ETrue);
       
   241             params.iAlpha = alphaBitmap->DataAddress();
       
   242             alphaBitmap->UnlockHeap(ETrue);
       
   243             params.iAlphaMode = alphaBitmap->DisplayMode();
       
   244             params.iAlphaSize =
       
   245                 CalculateScanLength(iScreenSize.iWidth,
       
   246                                     params.iAlphaMode) *
       
   247                 iScreenSize.iHeight;
       
   248         }
       
   249 
       
   250         iNativePixelFormat = CMIDConvertFactory::GetPixelFormat(displayMode);
       
   251         params.iFormat = iNativePixelFormat;
       
   252 
       
   253         CMIDNativeConverter* nativeConverter =
       
   254             CMIDNativeConverter::NewL(params);
       
   255         delete iNativeConverter;
       
   256         iNativeConverter = nativeConverter;
       
   257         iNativeConverter->SetDrawRect(params.iClipRect);
       
   258         do
       
   259         {
       
   260             do
       
   261             {
       
   262                 iNativeConverter->PlotPixel(internalColor);
       
   263             }
       
   264             while (iNativeConverter->AdvanceX());
       
   265         }
       
   266         while (iNativeConverter->AdvanceY());
       
   267 
       
   268         // 256 level alpha bitmap is the only one we can currently handle
       
   269         if (params.iAlphaMode == EGray256)
       
   270         {
       
   271             for (TInt i = 0; i < params.iAlphaSize; i++)
       
   272             {
       
   273                 ((TUint8*)params.iAlpha)[ i ] = internalColor.iA;      // CSI: 2 Index already checked #
       
   274             }
       
   275         }
       
   276         bitmapImage->RemoveRef();
       
   277     }
       
   278 }
       
   279 
       
   280 // Looping through pixels, iNativeConverter takes care that looping is ended
       
   281 // notice that bitmapConverter's Advance methods returns always ETrue, but
       
   282 // they are located in while because advancing must not be done after native
       
   283 // converter is gone through. So do not mess with order.
       
   284 #define DRAWLOOP( get, plot ) \
       
   285     do \
       
   286         { \
       
   287         do \
       
   288             { \
       
   289             get; \
       
   290             plot; \
       
   291             } \
       
   292         while( iNativeConverter->AdvanceX() && bitmapConverter->AdvanceX() ); \
       
   293         } \
       
   294     while( iNativeConverter->AdvanceY() && bitmapConverter->AdvanceY() );
       
   295 
       
   296 
       
   297 void CMIDDirectGraphics::DrawBitmapL(TMIDBitmapParameters* aParams)
       
   298 {
       
   299     // Get converter for specified format (ownership will remain in factory)
       
   300     TMIDFormatConverter* bitmapConverter =
       
   301         iConverterFactory->ConverterL(aParams->iFormat);
       
   302 
       
   303     aParams->iClipRect = iClipRect;
       
   304 
       
   305     bitmapConverter->InitializeL(*aParams);
       
   306 
       
   307     if (bitmapConverter->iBitmapRect.IsEmpty())
       
   308     {
       
   309         //nothing to draw, probably outside cliprect
       
   310         return;
       
   311     }
       
   312     iNativeConverter->SetDrawRect(bitmapConverter->iBitmapRect);
       
   313 
       
   314 
       
   315     // bitmaps data address can change so have to re-install them every time
       
   316     iNativeConverter->SetBitmaps(iGraphics->Bitmap()->DataAddress());
       
   317 
       
   318     // choosing right operation for optimized drawing
       
   319 
       
   320     TMIDInternalARGB color;
       
   321 
       
   322     // are we ignoring all alpha or there is no transparency
       
   323     if (!(aParams->iTransparency) &&
       
   324             !(iNativeConverter->HasAlphaBitmap()))
       
   325     {
       
   326         do
       
   327         {
       
   328             do
       
   329             {
       
   330                 bitmapConverter->GetPixel(color);
       
   331                 color.iA = KAlphaFullOpaque;
       
   332                 iNativeConverter->PlotPixel(color);
       
   333             }
       
   334             while (iNativeConverter->AdvanceX() && bitmapConverter->AdvanceX());
       
   335         }
       
   336         while (iNativeConverter->AdvanceY() && bitmapConverter->AdvanceY());
       
   337         return;
       
   338     }
       
   339 
       
   340 
       
   341     // canvases and images no longer have a separate transparency bitmap
       
   342     if (aParams->iAlpha)
       
   343     {
       
   344         if (iNativeConverter->HasAlphaBitmap())
       
   345         {
       
   346             DRAWLOOP(bitmapConverter->GetPixelWithAlpha(color),
       
   347                      iNativeConverter->PlotPixelWithAlphaBitmap(color));
       
   348         }
       
   349         else
       
   350         {
       
   351             DRAWLOOP(bitmapConverter->GetPixelWithAlpha(color),
       
   352                      iNativeConverter->PlotPixelWithAlpha(color));
       
   353         }
       
   354     }
       
   355     else if (!aParams->iAlpha &&
       
   356              bitmapConverter->IsAlpha())
       
   357     {
       
   358         DRAWLOOP(bitmapConverter->GetPixel(color),
       
   359                  iNativeConverter->PlotPixelWithAlpha(color));
       
   360     }
       
   361     else if (!(bitmapConverter->IsAlpha()) &&
       
   362              (iNativeConverter->HasAlphaBitmap()))
       
   363     {
       
   364         do
       
   365         {
       
   366             do
       
   367             {
       
   368                 bitmapConverter->GetPixel(color);
       
   369                 color.iA = KAlphaFullOpaque;
       
   370                 iNativeConverter->PlotPixelWithAlphaBitmap(color);
       
   371             }
       
   372             while (iNativeConverter->AdvanceX() && bitmapConverter->AdvanceX());
       
   373         }
       
   374         while (iNativeConverter->AdvanceY() && bitmapConverter->AdvanceY());
       
   375     }
       
   376 
       
   377 #ifdef RD_JAVA_NGA_ENABLED
       
   378     if (iCanvasTarget && iCanvasTarget->IsEglAvailable())
       
   379     {
       
   380         iCanvasTarget->UpdateRect(bitmapConverter->iBitmapRect);
       
   381     }
       
   382 #endif
       
   383 }
       
   384 
       
   385 
       
   386 void CMIDDirectGraphics::GetBitmapL(TMIDBitmapParameters* aParams)
       
   387 {
       
   388 
       
   389     if ((aParams->iX < 0) ||
       
   390             (aParams->iY < 0) ||
       
   391             (aParams->iWidth < 0) ||
       
   392             (aParams->iHeight < 0))
       
   393     {
       
   394         // throws java.lang.IllegalArgumentException if x, y, width or height is
       
   395         // negative
       
   396         User::Leave(KErrIllegalArgumentException);
       
   397     }
       
   398     // Get converter for specified format (ownership will remain in factory)
       
   399     TMIDFormatConverter* bitmapConverter =
       
   400         iConverterFactory->ConverterL(aParams->iFormat);
       
   401 
       
   402     // Whole bitmap as clipping rect
       
   403     aParams->iClipRect = TRect(iScreenSize);
       
   404     bitmapConverter->InitializeL(*aParams);
       
   405 
       
   406 
       
   407     // Set drawing rectangle to the native converter.
       
   408     // Use intersection with the screen size in order to handle correctly
       
   409     // areas that are outside of the screen.
       
   410     TRect drawRect(aParams->iX, aParams->iY,
       
   411                    aParams->iX + aParams->iWidth,
       
   412                    aParams->iY + aParams->iHeight);
       
   413 
       
   414     drawRect.Intersection(iScreenSize);
       
   415 
       
   416     // Do nothing if the asked area is not on the screen.
       
   417     if (!drawRect.IsEmpty())
       
   418     {
       
   419         iNativeConverter->SetDrawRect(drawRect);
       
   420 
       
   421 #ifdef RD_JAVA_NGA_ENABLED
       
   422         if (iCanvasTarget && iCanvasTarget->IsEglAvailable())
       
   423         {
       
   424             iCanvasTarget->UpdateOffScreenBitmapL(ETrue);
       
   425         }
       
   426 #endif
       
   427         // bitmaps data address can change so have to re-install them every time
       
   428         iNativeConverter->SetBitmaps(iGraphics->Bitmap()->DataAddress());
       
   429 
       
   430         TMIDInternalARGB color;
       
   431         DRAWLOOP(iNativeConverter->GetPixel(color),
       
   432                  bitmapConverter->PlotPixelWithAlpha(color));
       
   433     }
       
   434 }
       
   435 
       
   436 void CMIDDirectGraphics::DrawPolygonL(TPolygonParameters* aParams)
       
   437 {
       
   438     // We don't have to test wheter xPoints or yPoints are null, as they
       
   439     // are already checked on the java side
       
   440     if (aParams->aNumberOfPoints < 0 ||
       
   441             aParams->aXPointsLength < (aParams->aXOffset + aParams->aNumberOfPoints) ||
       
   442             aParams->aYPointsLength < (aParams->aYOffset + aParams->aNumberOfPoints) ||
       
   443             aParams->aXOffset < 0 ||
       
   444             aParams->aYOffset < 0)
       
   445     {
       
   446         //ArrayIndexOutOfBoundsException - if requested to access
       
   447         //xPoints or yPoints beyond the
       
   448         //length of the arrays or with negative index
       
   449         User::Leave(KErrArrayIndexOutOfBoundsException);
       
   450     }
       
   451 
       
   452 
       
   453     // bitmaps can change so have to re-install them every time
       
   454     iNativeConverter->SetBitmaps(iGraphics->Bitmap()->DataAddress());
       
   455 
       
   456     TMIDInternalARGB colorNative;
       
   457     iNativeConverter->Convert(colorNative, aParams->aARGBColor);
       
   458 
       
   459     TInt* xPoints = aParams->aXPoints;
       
   460     TInt* yPoints = aParams->aYPoints;
       
   461     TInt numberOfPoints(aParams->aNumberOfPoints);
       
   462 
       
   463     xPoints += aParams->aXOffset; // move to first x point
       
   464     yPoints += aParams->aYOffset; // move to first y point
       
   465 
       
   466 #ifdef RD_JAVA_NGA_ENABLED
       
   467     TRect updateRect;
       
   468 #endif
       
   469 
       
   470     TInt i(0);
       
   471     for (; i < numberOfPoints; i++)
       
   472     {
       
   473         xPoints[ i ] += aParams->aTransX;   // CSI: 2 Index already checked #
       
   474         yPoints[ i ] += aParams->aTransY;   // CSI: 2 Index already checked #
       
   475 
       
   476 #ifdef RD_JAVA_NGA_ENABLED
       
   477         if (i == 0)
       
   478         {
       
   479             updateRect.iTl = TPoint(xPoints[i], yPoints[i]);
       
   480             updateRect.iBr = updateRect.iTl;
       
   481         }
       
   482         else
       
   483         {
       
   484             updateRect.iTl.iX = Min(xPoints[i], updateRect.iTl.iX);
       
   485             updateRect.iTl.iY = Min(yPoints[i], updateRect.iTl.iY);
       
   486             updateRect.iBr.iX = Max(xPoints[i], updateRect.iBr.iX);
       
   487             updateRect.iBr.iY = Max(yPoints[i], updateRect.iBr.iY);
       
   488         }
       
   489 #endif
       
   490     }
       
   491 
       
   492     for (i = 0; i < numberOfPoints - 1; i++)
       
   493     {
       
   494         DrawLine(TPoint(xPoints[ i ], yPoints[ i ]),     // CSI: 2 Index already checked #
       
   495                  TPoint(xPoints[ i + 1 ], yPoints[ i + 1 ]),    // CSI: 2 Index already checked #
       
   496                  colorNative);
       
   497     }
       
   498     // draw line from last point to first point
       
   499     DrawLine(TPoint(xPoints[ i ], yPoints[ i ]),     // CSI: 2 Index already checked #
       
   500              TPoint(xPoints[ 0 ], yPoints[ 0 ]),    // CSI: 2 Index already checked #
       
   501              colorNative);
       
   502 
       
   503     if (aParams->aFill)
       
   504     {
       
   505 
       
   506         TPoint* points = new(ELeave)TPoint[ numberOfPoints ];
       
   507 
       
   508         // CPolygonFiller chokes if coordinates are exactly 2147483647 (the integer
       
   509         // maximum value), so invalid coordinates are made smaller
       
   510         // api/com_nokia_mid/ui/DirectGraphics/index.html#FillPolygon wont pass
       
   511         // if this is not done.
       
   512         for (TInt i = 0; i < numberOfPoints; i++)
       
   513         {
       
   514             if ((xPoints[ i ] == KMaxInteger) ||    // CSI: 2 Index already checked #
       
   515                     (yPoints[ i ] == KMaxInteger))     // CSI: 2 Index already checked #
       
   516             {
       
   517                 points[ i ].SetXY(xPoints[ i ] - 1, yPoints[ i ] - 1);    // CSI: 2 Index already checked #
       
   518             }
       
   519             else
       
   520             {
       
   521                 points[ i ].SetXY(xPoints[ i ], yPoints[ i ]);    // CSI: 2 Index already checked #
       
   522             }
       
   523         }
       
   524         iPolygonFiller->Reset();
       
   525         iPolygonFiller->Construct(points, numberOfPoints,
       
   526                                   CGraphicsContext::EAlternate);
       
   527         TBool draw(ETrue);
       
   528         TPoint pos;
       
   529         TInt end;
       
   530         while (draw)
       
   531         {
       
   532             iPolygonFiller->GetNextPixelRun(draw, pos.iY, pos.iX, end);
       
   533             iNativeConverter->DrawScanLine(pos, end - pos.iX + 1, colorNative, iClipRect);
       
   534         }
       
   535         delete[] points;
       
   536     }
       
   537 
       
   538 #ifdef RD_JAVA_NGA_ENABLED
       
   539     if (iCanvasTarget && iCanvasTarget->IsEglAvailable())
       
   540     {
       
   541         // Grow by one pixel to include bottom right end point
       
   542         ++updateRect.iBr.iX;
       
   543         ++updateRect.iBr.iY;
       
   544         updateRect.Intersection(iClipRect);
       
   545         iCanvasTarget->UpdateRect(updateRect);
       
   546     }
       
   547 #endif
       
   548 }
       
   549 
       
   550 void CMIDDirectGraphics::DrawLine(const TPoint& aPointA,
       
   551                                   const TPoint& aPointB,
       
   552                                   const TMIDInternalARGB& aColor)
       
   553 {
       
   554     TLinearDDA linear;
       
   555     // TLinearDDA results a bit different line depending on starting point.
       
   556     // We do not have any problems with CPolygonFiller if we draw like this.
       
   557     if (aPointA.iY < aPointB.iY)
       
   558     {
       
   559         linear.Construct(aPointA, aPointB, TLinearDDA::ECenter);
       
   560     }
       
   561     else
       
   562     {
       
   563         linear.Construct(aPointB, aPointA, TLinearDDA::ECenter);
       
   564     }
       
   565 
       
   566     TPoint pos(0, 0);
       
   567     if (iDottedLine)
       
   568     {
       
   569         TInt dot(0);
       
   570         while (!linear.SingleStep(pos))
       
   571         {
       
   572             if (dot == KDottedLineStep ||
       
   573                     pos == aPointA ||
       
   574                     pos == aPointB)
       
   575             {
       
   576                 iNativeConverter->PlotPixel(pos, aColor, iClipRect);
       
   577                 dot = 0;
       
   578             }
       
   579             dot++;
       
   580         }
       
   581     }
       
   582     else
       
   583     {
       
   584         while (!linear.SingleStep(pos))
       
   585         {
       
   586             iNativeConverter->PlotPixel(pos, aColor, iClipRect);
       
   587         }
       
   588         // plot last pixel
       
   589         iNativeConverter->PlotPixel(pos, aColor, iClipRect);
       
   590     }
       
   591 }
       
   592 
       
   593 TInt CMIDDirectGraphics::CalculateScanLength(
       
   594     const TInt aWidth,
       
   595     const TDisplayMode aDisplayMode)
       
   596 {
       
   597     TInt retVal = 0;
       
   598 
       
   599     switch (aDisplayMode)
       
   600     {
       
   601     case EGray2:
       
   602     {
       
   603         retVal = ((aWidth + KGray2Divider - 1) / KGray2Divider) *
       
   604                  KGray2Divider;
       
   605         break;
       
   606     }
       
   607     case EGray4:
       
   608     {
       
   609         retVal = ((aWidth + KGray4Divider - 1) / KGray4Divider) *
       
   610                  KGray4Divider;
       
   611         break;
       
   612     }
       
   613     case EGray16:
       
   614     case EColor16:
       
   615     {
       
   616         retVal = ((aWidth + KColor16Divider - 1) / KColor16Divider) *
       
   617                  KColor16Divider;
       
   618         break;
       
   619     }
       
   620     case EGray256:
       
   621     case EColor256:
       
   622     {
       
   623         retVal = ((aWidth + KColor256Divider - 1) / KColor256Divider) *
       
   624                  KColor256Divider;
       
   625         break;
       
   626     }
       
   627     case EColor4K:
       
   628     case EColor64K:
       
   629     {
       
   630         retVal = ((aWidth + KColor64KDivider - 1) / KColor64KDivider) *
       
   631                  KColor64KDivider;
       
   632         break;
       
   633     }
       
   634     case EColor16M:
       
   635     {
       
   636         User::Invariant();
       
   637         break;
       
   638     }
       
   639     case EColor16MU:
       
   640     case ERgb:
       
   641     case EColor16MA:
       
   642     {
       
   643         retVal = aWidth;
       
   644         break;
       
   645     }
       
   646     default:
       
   647     {
       
   648         User::Invariant();
       
   649     }
       
   650     }
       
   651 
       
   652     return retVal;
       
   653 }
       
   654 
       
   655 #ifdef RD_JAVA_NGA_ENABLED
       
   656 #define UPDATE_TRIANGLE_RECT \
       
   657 if (iCanvasTarget && iCanvasTarget->IsEglAvailable()) \
       
   658 { \
       
   659     TRect updateRect(TPoint(Min(Min(aX1,aX2),aX3), Min(Min(aY1,aY2),aY3)), \
       
   660                      TPoint(Max(Max(aX1,aX2),aX3), Max(Max(aY1,aY2),aY3))); \
       
   661     ++updateRect.iBr.iX; \
       
   662     ++updateRect.iBr.iY; \
       
   663     updateRect.Intersection(iClipRect); \
       
   664     iCanvasTarget->UpdateRect(updateRect); \
       
   665 }
       
   666 #endif // RD_JAVA_NGA_ENABLED
       
   667 
       
   668 void CMIDDirectGraphics::DrawTriangle(const TInt aX1, const TInt aY1,
       
   669                                       const TInt aX2, const TInt aY2,
       
   670                                       const TInt aX3, const TInt aY3,
       
   671                                       const TUint32 aColor)
       
   672 {
       
   673     // bitmaps can change so have to re-install them every time
       
   674     iNativeConverter->SetBitmaps(iGraphics->Bitmap()->DataAddress());
       
   675 
       
   676     TMIDInternalARGB colorNative;
       
   677     iNativeConverter->Convert(colorNative, aColor);
       
   678 
       
   679     DrawLine(TPoint(aX1, aY1), TPoint(aX2, aY2), colorNative);
       
   680     DrawLine(TPoint(aX2, aY2), TPoint(aX3, aY3), colorNative);
       
   681     DrawLine(TPoint(aX3, aY3), TPoint(aX1, aY1), colorNative);
       
   682 
       
   683 #ifdef RD_JAVA_NGA_ENABLED
       
   684     UPDATE_TRIANGLE_RECT;
       
   685 #endif
       
   686 }
       
   687 
       
   688 void CMIDDirectGraphics::FillTriangle(const TInt aX1, const TInt aY1,
       
   689                                       const TInt aX2, const TInt aY2,
       
   690                                       const TInt aX3, const TInt aY3,
       
   691                                       const TUint32 aColor)
       
   692 {
       
   693     iNativeConverter->SetBitmaps(iGraphics->Bitmap()->DataAddress());
       
   694 
       
   695     TMIDInternalARGB colorNative;
       
   696     iNativeConverter->Convert(colorNative, aColor);
       
   697 
       
   698     TMIDTriangleFiller triangleFiller;
       
   699     triangleFiller.Construct(aX1, aY1,
       
   700                              aX2, aY2,
       
   701                              aX3, aY3);
       
   702     TBool draw(ETrue);
       
   703     TPoint pos;
       
   704     TInt end;
       
   705 
       
   706     while (draw)
       
   707     {
       
   708         triangleFiller.GetNextPixelRun(draw, pos.iY,
       
   709                                        pos.iX, end);
       
   710 
       
   711         iNativeConverter->DrawScanLine(pos, end - pos.iX + 1, colorNative, iClipRect);
       
   712     }
       
   713 
       
   714 #ifdef RD_JAVA_NGA_ENABLED
       
   715     UPDATE_TRIANGLE_RECT;
       
   716 #endif
       
   717 }
       
   718 
       
   719 
       
   720 
       
   721 
       
   722 
       
   723 
       
   724 
       
   725 
       
   726 
       
   727 
       
   728 
       
   729 
       
   730 
       
   731 
       
   732 
       
   733 
       
   734 
       
   735 
       
   736