Update contrib.
1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
18 #include "TAlphaBlend.h"
21 const TInt KWidth = 10;//Used in alpha blending tests
22 const TInt KHeight = 3;//Used in alpha blending tests
23 const TInt KMaximumAttempts = 2; // Allow retries on some tests, due to spurious InfoPrints
24 TBool iExtraLogging1=EFalse; //Used to trigger logging at times the test fails
25 TBool iExtraLogging2=EFalse; //Used to trigger logging at times the test fails
27 CTAlphaBlending::CTAlphaBlending(CTestStep* aStep):
28 CTGraphicsBase(aStep),
34 CTAlphaBlending::~CTAlphaBlending()
36 DeleteGraphicsContext();
40 void CTAlphaBlending::RunTestCaseL(TInt aCurTestCase)
42 ((CTAlphaBlendingStep*)iStep)->SetTestStepID(KUnknownSYMTestCaseIDName);
46 ((CTAlphaBlendingStep*)iStep)->SetTestStepID(_L("GRAPHICS-BITGDI-0024"));
47 INFO_PRINTF1(_L("Alpha blending"));
52 @SYMTestCaseID GRAPHICS-BITGDI-0114
54 ((CTAlphaBlendingStep*)iStep)->SetTestStepID(_L("GRAPHICS-BITGDI-0114"));
55 INFO_PRINTF1(_L("Alpha blending 2"));
56 TestAlphaBlending2L();
59 ((CTAlphaBlendingStep*)iStep)->SetTestStepID(_L("GRAPHICS-BITGDI-0085"));
60 INFO_PRINTF1(_L("Alpha blending Correctness test 16MU 16MA"));
61 TestAlphaBlendCorrect(EColor16MU, EColor16MU);
64 ((CTAlphaBlendingStep*)iStep)->SetTestStepID(_L("GRAPHICS-BITGDI-0097"));
65 INFO_PRINTF1(_L("Alpha plot test"));
66 TestAlphaBlendingPlotL();
70 @SYMTestCaseID GRAPHICS-BITGDI-0115
72 ((CTAlphaBlendingStep*)iStep)->SetTestStepID(_L("GRAPHICS-BITGDI-0115"));
73 INFO_PRINTF1(_L("Draw bitmap blending"));
78 ((CTAlphaBlendingStep*)iStep)->SetTestStepID(_L("GRAPHICS-BITGDI-0085"));
79 INFO_PRINTF1(_L("Alpha blending Correctness test 16M 16MA"));
80 TestAlphaBlendCorrect(EColor16M, EColor16MA);
83 ((CTAlphaBlendingStep*)iStep)->SetTestStepID(KNotATestSYMTestCaseIDName);
84 ((CTAlphaBlendingStep*)iStep)->CloseTMSGraphicsStep();
88 ((CTAlphaBlendingStep*)iStep)->RecordTestResultL();
91 TInt CTAlphaBlending::CreateScreenDevice(TDisplayMode aDisplayMode, CFbsBitGc::TGraphicsOrientation aOrientation)
93 DeleteGraphicsContext();
95 TRAPD(err, iDevice = CFbsScreenDevice::NewL(_L("scdv"), aDisplayMode));
96 if(err == KErrNotSupported)
100 TEST2(err, KErrNone);
101 err = iDevice->CreateContext((CGraphicsContext*&)iGc);
102 TEST2(err, KErrNone);
103 if (!iGc->SetOrientation(aOrientation))
105 return(KErrNotSupported);
107 iGc->SetUserDisplayMode(aDisplayMode);
108 iDevice->ChangeScreenDevice(NULL);
109 iDevice->SetAutoUpdate(EFalse);
113 void CTAlphaBlending::DeleteScreenDevice()
119 void CTAlphaBlending::DeleteGraphicsContext()
125 // returns the pixel colour from the provided bitmap in aarrggbb format
126 // if pixel is outside the bitmaps limits return top left pixel
127 TUint32 CTAlphaBlending::GetRawPixel(CFbsBitmap* aBitmap, TPoint aPos)
129 TBitmapUtil bmpUtil(aBitmap);
131 ASSERT(aPos.iX>=0 && aPos.iY>=0);
132 ASSERT(aPos.iX<aBitmap->SizeInPixels().iWidth && aPos.iY<aBitmap->SizeInPixels().iHeight);
134 value = bmpUtil.GetPixel();
140 @SYMTestCaseID GRAPHICS-BITGDI-0097
144 @SYMTestCaseDesc CDrawThirtyTwoBppBitmapCommon::WriteRgb did not change the dest alpha value to 255 when
145 the dest alpha was >0 and <255 and the a soure pen had an alpha of 255
147 @SYMTestPriority Normal
149 @SYMTestStatus Implemented
151 @SYMTestActions Creates a bitmap, clears it to black (destination) 50% opaque then plots a series of points
152 with a 100% opaque pen on it. Tests the resultant alpha value.
154 @SYMTestExpectedResults Final alpha value should be 255
156 void CTAlphaBlending::TestAlphaBlendingPlotL()
158 const TSize KRectSize(100,100);
159 const TRect KTargetRect(TPoint(0,0), KRectSize);
161 // create the target bitmap
162 CFbsBitmap* destBmp = new (ELeave) CFbsBitmap;
163 CleanupStack::PushL(destBmp);
164 User::LeaveIfError(destBmp->Create(KRectSize, EColor16MA));
165 destBmp->SetSizeInTwips(KRectSize);
167 // create bitmap device and graphics context
168 CFbsBitmapDevice* destBmpDevice = CFbsBitmapDevice::NewL(destBmp);
169 CleanupStack::PushL(destBmpDevice);
170 CFbsBitGc* destGc = NULL;
171 User::LeaveIfError(destBmpDevice->CreateContext(destGc));
172 CleanupStack::PushL(destGc);
173 destGc->SetPenStyle(CGraphicsContext::ENullPen);
174 destGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
176 TDisplayMode screenMode = EColor16MA;
177 TInt err = CreateScreenDevice(screenMode);
180 screenMode = EColor64K;
181 err = CreateScreenDevice(screenMode);
186 const TInt KSqrMin=45;
187 const TInt KSqrMax=55;
189 iGc->SetUserDisplayMode(screenMode);
190 destGc->SetBrushColor(TRgb(0,0,0,127));
191 destGc->SetDrawMode(CGraphicsContext::EDrawModeWriteAlpha);
192 destGc->Clear(KTargetRect);
194 // copy over to screen dc for anyone watching
195 iGc->BitBlt(TPoint(0,0), destBmp, KTargetRect);
198 // set the pen colour to white and plot some points
199 destGc->SetDrawMode(CGraphicsContext::EDrawModePEN);
200 destGc->SetPenStyle(CGraphicsContext::ESolidPen);
201 destGc->SetPenColor(TRgb(0,70,130,255));
202 for( TInt y=KSqrMin;y<=KSqrMax;y++)
204 for( TInt x=KSqrMin;x<=KSqrMax;x++)
206 destGc->Plot(TPoint(x,y));
210 // copy over to screen dc for anyone watching
211 iGc->BitBlt(TPoint(0,0), destBmp, KTargetRect);
214 TUint32 actualValue=0;
215 // check the resulting values alpha values are 0xFF in the square we drew
216 for( TInt y=KSqrMin;y<=KSqrMax;y++)
218 for( TInt x=KSqrMin;x<=KSqrMax;x++)
220 actualValue = GetRawPixel(destBmp, TPoint(x,y));
221 if( (actualValue&0xFF000000) != 0xFF000000 )
224 INFO_PRINTF2(_L("TestAlphaBlendingPlotL() ***FAILED*** - expected alpha value 0xFF got %d "),((actualValue&0xFF000000)>>24));
229 CleanupStack::PopAndDestroy(3); //destGc,destBmpDevice,destBmp
233 @SYMTestCaseID GRAPHICS-BITGDI-0024
239 System, GT0173 System Libraries, BITGDI support required for semi-transparent windows
240 DEF039083 - Using BitBltMasked to do alpha-blending only works if your brush style is "null"
241 DEF039409 - Wrong part of Alpha Bitmap is used when there is a clipping region
242 DEF039669 - The UserDisplayMode is not honoured during the AlphaBlendBitmap function.
243 REQ3413 Second overload of CFbsBitGc's AlphaBlendBitmaps - "Supply a second overload of
244 CFbsBitGc's AlphaBlendBitmaps function, but with one of the source bitmaps being passed
245 as a CFbsBitGc object, so that the screen can be used as a source."
247 @SYMTestPriority High
249 @SYMTestStatus Implemented
253 Tests alpha blending - for all display modes, some brush styles, user display modes,
254 different positions on the screen.
256 Shadow/fade mode is no more tested, because the existing BitBltMasked() and the new
257 AlphaBlendBitmaps() methods treat it a different way - see MAlphaBlend interface in
258 ScreenDriver component.
260 @SYMTestExpectedResults Test should perform graphics operations succesfully.
263 void CTAlphaBlending::TestAlphaBlendingL()
265 TPoint originPt(0, 0);
267 TRect scrRc1(0, 0, KWidth, KHeight);
269 TPoint alphaPt(0, 0);
271 //If we compare CFbsBitGc::BitBltMasked() aguments with CFbsBitGc::AlphaBlending() arguments,
272 //we will see that AlphaBlending() has more arguments than BitBltMasked() -
273 //srcPt2, alphaPt. To make it possible - the comparison between these two methods,
274 //we have to change aAlphaPt and aSrcPt2 values accordingly with the changes of scrRc1 value.
276 //test 1 - the origin is moved
277 originPt = TPoint(97, 33);
278 DoAlphaBlendingTestsL(originPt, destPt, scrRc1, srcPt2, alphaPt);
279 #if !defined(__X86GCC__) //These test take too long to run in X86GCC
280 //test 2 - the origin is (0, 0)
281 originPt = TPoint(0, 0);
282 DoAlphaBlendingTestsL(originPt, destPt, scrRc1, srcPt2, alphaPt);
283 //test 3 - scrRect1 is not (0, 0, KWidth, KHeight)
284 scrRc1 = TRect(3, 1, KWidth, KHeight);
285 alphaPt = TPoint(3, 1);
286 DoAlphaBlendingTestsL(originPt, destPt, scrRc1, srcPt2, alphaPt);
287 //test 4 - restore scrRc1 and alphaPt, move the destination point
288 scrRc1 = TRect(0, 0, KWidth, KHeight);
289 alphaPt = TPoint(0, 0);
290 destPt = TPoint(13, 17);
291 iExtraLogging1=ETrue;
292 DoAlphaBlendingTestsL(originPt, destPt, scrRc1, srcPt2, alphaPt);
293 iExtraLogging1=EFalse;
297 void CTAlphaBlending::DoDrawBitmapTestsL()
299 TDisplayMode modes[] = {EColor16MA, EColor16MAP, EColor16MU, EColor16M, EColor256, EColor4K, EColor64K,
300 EGray256, EGray16, EGray4, EGray2, EColor16};
301 const TInt KNumTestDisplayModes=sizeof(modes)/sizeof(modes[0]);
302 for(TInt modeIndex=0;modeIndex<KNumTestDisplayModes;modeIndex++)
304 TDisplayMode screenMode=modes[modeIndex];
305 if (CreateScreenDevice(screenMode)!=KErrNone)
307 DoDrawBitmapTestL(screenMode);
311 void CTAlphaBlending::DoDrawBitmapTestL(TDisplayMode aTestDisplayMode)
314 TBool alphaSupported=(aTestDisplayMode==EColor16MA || aTestDisplayMode==EColor16MAP);
315 TSize screenSize=iDevice->SizeInPixels();
317 const TInt KNumTestSrcSizes=4;
318 const TSize testSrcSizes[KNumTestSrcSizes]={TSize(2,2),TSize(20,10),TSize(200,5),TSize(55,555)};
319 for(TInt srcSizeIndex=0;srcSizeIndex<KNumTestSrcSizes;srcSizeIndex++)
321 TSize srcSize(testSrcSizes[srcSizeIndex]);
323 CFbsBitmap* srcBmp=new(ELeave) CFbsBitmap;
324 CleanupStack::PushL(srcBmp);
325 User::LeaveIfError(srcBmp->Create(srcSize,aTestDisplayMode));
326 CFbsBitmapDevice* srcDevice = CFbsBitmapDevice::NewL(srcBmp);
327 CleanupStack::PushL(srcDevice);
328 CFbsBitGc* srcGc=NULL;
329 User::LeaveIfError(srcDevice->CreateContext(srcGc));
330 CleanupStack::PushL(srcGc);
332 CFbsBitmap* srcAlpha=new(ELeave) CFbsBitmap;
333 CleanupStack::PushL(srcAlpha);
334 User::LeaveIfError(srcAlpha->Create(srcSize,aTestDisplayMode));
335 CFbsBitmapDevice* srcAlphaDevice = CFbsBitmapDevice::NewL(srcAlpha);
336 CleanupStack::PushL(srcAlphaDevice);
337 CFbsBitGc* srcAlphaGc=NULL;
338 User::LeaveIfError(srcAlphaDevice->CreateContext(srcAlphaGc));
339 CleanupStack::PushL(srcAlphaGc);
340 srcAlphaGc->SetDrawMode(CGraphicsContext::EDrawModeWriteAlpha);
342 CFbsBitmap* srcMask=new(ELeave) CFbsBitmap;
343 CleanupStack::PushL(srcMask);
344 User::LeaveIfError(srcMask->Create(srcSize,EGray256));
345 CFbsBitmapDevice* srcMaskDevice = CFbsBitmapDevice::NewL(srcMask);
346 CleanupStack::PushL(srcMaskDevice);
347 CFbsBitGc* srcMaskGc=NULL;
348 User::LeaveIfError(srcMaskDevice->CreateContext(srcMaskGc));
349 CleanupStack::PushL(srcMaskGc);
351 TInt maxX=srcSize.iWidth-1;
352 TInt maxY=srcSize.iHeight-1;
353 for(TInt yLoop=0;yLoop<srcSize.iHeight;yLoop++)
355 for(TInt xLoop=0;xLoop<srcSize.iWidth;xLoop++)
357 TPoint plotPos(xLoop,yLoop);
358 TRgb pen(xLoop*255/maxX,yLoop*255/maxY,(xLoop+yLoop)*128/(maxX+maxY));
359 srcGc->SetPenColor(pen);
360 srcGc->Plot(plotPos);
361 TInt alpha=(xLoop+yLoop)*255/(maxX+maxY);
363 srcAlphaGc->SetBrushColor(pen);
364 srcAlphaGc->Clear(TRect(plotPos,TSize(1,1)));
365 srcMaskGc->SetPenColor(TRgb::Gray256(alpha));
366 srcMaskGc->Plot(plotPos);
369 const TInt KNumTestTargSizes=5;
370 const TSize testTargSizes[KNumTestTargSizes]={TSize(0,0), TSize(20,1),TSize(30,20),TSize(200,31),TSize(55,5)};
371 for(TInt targSizeIndex=0;targSizeIndex<KNumTestSrcSizes;targSizeIndex++)
373 TSize targSize(testTargSizes[targSizeIndex]);
374 if (targSizeIndex==0)
376 targSize=srcSize; // Special case with no scaling
377 if (targSize.iWidth>screenSize.iWidth)
378 targSize.iWidth=screenSize.iWidth;
379 TInt maxHeight=screenSize.iHeight/3;
380 if (targSize.iHeight>maxHeight)
381 targSize.iHeight=maxHeight;
384 CFbsBitmap* targBmp=new(ELeave) CFbsBitmap;
385 CleanupStack::PushL(targBmp);
386 User::LeaveIfError(targBmp->Create(targSize,aTestDisplayMode));
387 CFbsBitmapDevice* targBmpDevice = CFbsBitmapDevice::NewL(targBmp);
388 CleanupStack::PushL(targBmpDevice);
389 CFbsBitGc* targGc=NULL;
390 User::LeaveIfError(targBmpDevice->CreateContext(targGc));
391 CleanupStack::PushL(targGc);
393 CFbsBitmap* targMask=new(ELeave) CFbsBitmap;
394 CleanupStack::PushL(targMask);
395 User::LeaveIfError(targMask->Create(targSize,EGray256));
396 CFbsBitmapDevice* targMaskDevice = CFbsBitmapDevice::NewL(targMask);
397 CleanupStack::PushL(targMaskDevice);
398 CFbsBitGc* targMaskGc=NULL;
399 User::LeaveIfError(targMaskDevice->CreateContext(targMaskGc));
400 CleanupStack::PushL(targMaskGc);
403 TRect testRect1(targSize);
405 // First we pre-stretch the source and mask bitmaps into temp bitmaps
406 targGc->DrawBitmap(TRect(targSize),srcBmp);
407 targMaskGc->DrawBitmap(TRect(targSize),srcMask);
408 // Then blend them onto the screen with a call to BitBltMasked
409 iGc->BitBltMasked(drawPos,targBmp,TRect(targSize),targMask,EFalse);
410 drawPos.iY+=targSize.iHeight;
411 TRect testRect2(drawPos,targSize);
412 // Next we combine the stretching and masking with one call to DrawBitmapMasked,
413 // this should give the same end result.
414 iGc->DrawBitmapMasked(testRect2,srcBmp,TRect(srcSize),srcMask,EFalse);
418 // Finally if alpha blending supported we stretch and blend, again to achieve the exact same end result
419 // as the two previous calls. This was specificially done to catch DEF116427.
420 drawPos.iY+=targSize.iHeight;
421 testRect3=TRect(drawPos,targSize);
422 iGc->DrawBitmap(testRect3,srcAlpha);
424 //Use this just to check what we've put in the test bitmaps
426 drawPos.iY+=targSize.iHeight+1;
427 iGc->BitBlt(drawPos,srcBmp);
428 drawPos.iY+=srcSize.iHeight+1;
429 iGc->BitBlt(drawPos,srcMask);
430 drawPos.iY+=srcSize.iHeight+1;
432 iGc->BitBlt(drawPos,srcAlpha);
435 TBool ret1=iDevice->RectCompare(testRect1,*iDevice,testRect2);
436 TBool ret2=alphaSupported?iDevice->RectCompare(testRect1,*iDevice,testRect3):ETrue;
439 INFO_PRINTF4(_L("DrawBitmapTest, ret1=%d, ret2=%d, Screen mode=%d"),ret1,ret2,aTestDisplayMode);
442 CleanupStack::PopAndDestroy(3,targMask);
443 CleanupStack::PopAndDestroy(3,targBmp);
445 CleanupStack::PopAndDestroy(3,srcMask);
446 CleanupStack::PopAndDestroy(3,srcAlpha);
447 CleanupStack::PopAndDestroy(3,srcBmp);
451 //Tests alpha blending - for all display modes, some brush styles, user display modes,
452 //different positions on the screen.
453 void CTAlphaBlending::DoAlphaBlendingTestsL(const TPoint& aOrigin,
454 const TPoint& aDestPt,
455 const TRect& aSrcRc1,
456 const TPoint& aScrPt2,
457 const TPoint& aAlphaPt)
460 _LIT(KLog,"Origin=(%d,%d) DestPt=(%d,%d) SrcRect=(%d,%d,%d,%d) ScrPt=(%d,%d) AlphaPt=(%d,%d)");
461 buf.Format(KLog,aOrigin.iX,aOrigin.iY,aDestPt.iX,aDestPt.iY,aSrcRc1.iTl.iX,aSrcRc1.iTl.iY
462 ,aSrcRc1.iBr.iX,aSrcRc1.iBr.iY,aScrPt2.iX,aScrPt2.iY,aAlphaPt.iX,aAlphaPt.iY);
464 TDisplayMode mode[] = {EColor16MA, EColor16MAP, EColor16MU, EColor16M, EColor256, EColor4K, EColor64K,
465 EGray256, EGray16, EGray4, EGray2, EColor16};
466 CFbsBitmap* screenBmp = NULL;
467 CFbsBitmap* srcBmp = NULL;
468 CFbsBitmap* alphaBmp = NULL;
469 CGraphicsContext::TBrushStyle brushStyle[] = {CGraphicsContext::ENullBrush, CGraphicsContext::ESolidBrush};
470 for(TInt i=0;i<TInt(sizeof(mode)/sizeof(mode[0]));i++)
472 for(TInt orientation=0;orientation<=CFbsBitGc::EGraphicsOrientationRotated270;orientation++)
474 if (CreateScreenDevice(mode[i],(CFbsBitGc::TGraphicsOrientation)orientation) != KErrNone)
478 iExtraLogging2=(iExtraLogging1 && mode[i]==11);
479 INFO_PRINTF3(_L("Mode=%d, Orientation=%d"), mode[i], orientation);
480 CreateAlphaBlendingBitmapsLC(screenBmp, srcBmp, alphaBmp, mode[i]);
481 for(TInt j=0;j<TInt(sizeof(brushStyle)/sizeof(brushStyle[0]));j++)
483 if(mode[i] == EGray4 && brushStyle[j] == CGraphicsContext::ESolidBrush)
485 //When the display mode is EGray4 - Flicker-free blitting is not enabled.
486 //The screen will be filled with 0xFF color, which is not the same color used by
487 //the test - 0x00. And BitBltMasked and AlphaBlendBitmaps will produce different results.
490 iGc->SetOrigin(aOrigin);
491 iGc->SetBrushStyle(brushStyle[j]);
492 TDisplayMode userDisplayMode[] = {EColor16MA, EColor16MAP, EColor16MU, EColor16M, EColor256,
493 EColor4K, EColor64K, EGray256,
494 EGray16, EGray4, EGray2, EColor16};
495 for(TInt l=0;l<TInt(sizeof(userDisplayMode)/sizeof(userDisplayMode[0]));l++)
497 iGc->SetUserDisplayMode(userDisplayMode[l]);
498 TSize scrDevSize = iDevice->SizeInPixels();
499 TRect clipRc[] = {TRect(0, 0, scrDevSize.iWidth, scrDevSize.iHeight),
500 TRect(0, 1, scrDevSize.iWidth, scrDevSize.iHeight), // Tests Y clipping only
501 TRect(5, 0, scrDevSize.iWidth, scrDevSize.iHeight), // Tests X clipping only
502 TRect(5, 1, scrDevSize.iWidth, scrDevSize.iHeight),
503 TRect(3, 0, 14, 23)};//14 and 23 values are not accidental!
504 //Sometimes the method is called with aDestPt(13, 17).
505 for(TInt k=0;k<TInt(sizeof(clipRc)/sizeof(clipRc[0]));k++)
509 _LIT(KLog," BrushStyle=%d UserDisplayMode=%d ClipRect=(%d,%d,%d,%d)");
510 INFO_PRINTF7(KLog,j,l,clipRc[k].iTl.iX,clipRc[k].iTl.iY,clipRc[k].iBr.iX,clipRc[k].iBr.iY);
513 iGc->SetClippingRect(clipRc[k]);
514 DoAlphaBlendingTestL(screenBmp, srcBmp, alphaBmp, aDestPt, aSrcRc1, aScrPt2, aAlphaPt);
515 iGc->CancelClippingRect();
517 }//end of - for(TInt l=0;l<TInt(sizeof(userDisplayMode)/sizeof(userDisplayMode[0]));l++)
518 iGc->SetOrientation(CFbsBitGc::EGraphicsOrientationNormal);
520 DestroyAlphaBlendingBitmaps(screenBmp, srcBmp, alphaBmp);
521 }//end of - for(TInt j=0;j<TInt(sizeof(brushStyle)/sizeof(brushStyle[0]));j++)
522 }//end of - for(TInt i=0;i<TInt(sizeof(mode)/sizeof(mode[0]));i++)
525 //The method compares results of two alpha blending methods:
526 //iGc->BitBltMasked() and iGc->AlphaBlendBitmaps().
527 //To make that possible, aScreenBmp is copied to the screen before the call of
528 //iGc->BitBltMasked().
529 void CTAlphaBlending::DoAlphaBlendingTestL(CFbsBitmap* aScreenBmp,
530 const CFbsBitmap* aSrcBmp,
531 const CFbsBitmap* aAlphaBmp,
532 const TPoint& aDestPt,
533 const TRect& aSrcRc1,
534 const TPoint& aSrcPt2,
535 const TPoint& aAlphaPt)
537 _LIT(KScreenBmpFile, "C:\\BMP_DATA.DAT");
538 iGc->SetShadowMode(EFalse);
541 TSize scrDevSize = iDevice->SizeInPixels();
542 TInt allocatedSize = scrDevSize.iWidth * scrDevSize.iHeight * 2;
544 //The screen alpha blended data after calling of BitBltMasked() - will be filled after the call
545 TUint8* screenBmpDestData1 = new (ELeave) TUint8[allocatedSize];
546 CleanupStack::PushL(screenBmpDestData1);
547 //The screen alpha blended data after calling of AlphaBlendingBitmaps() - will be filled after the call
548 TUint8* screenBmpDestData2 = new (ELeave) TUint8[allocatedSize];
549 CleanupStack::PushL(screenBmpDestData2);
550 // Allow an effective restart of the test, since sometimes there are spurious
551 // InfoPrints that affect the comparisons.
552 for (TInt attempt = 0; res && (attempt < KMaximumAttempts); attempt++)
554 //Fill the blocks with some default value
555 Mem::Fill(screenBmpDestData1, allocatedSize, 0xCA);
556 Mem::Fill(screenBmpDestData2, allocatedSize, 0xCA);
557 //Check screen bitmap size
558 TSize screenBmpSize = aScreenBmp->SizeInPixels();
559 if ((screenBmpSize.iWidth != KWidth) || (screenBmpSize.iHeight != KHeight))
561 _LIT(KScreenErr,"DoAlphaBlendingTestL test: w:%d!=%d || h:%d!=%d");
562 INFO_PRINTF5(KScreenErr, screenBmpSize.iWidth, KWidth, screenBmpSize.iHeight, KHeight);
565 // Alpha blending using CFbsBitGc::BitBltMasked //
568 _LIT(KLog1," CFbsBitGc::BitBltMasked test");
571 //Screen bitmap rectangle
572 screenBmpRc.SetRect(aDestPt, TSize(aSrcRc1.Width(), aSrcRc1.Height()));
575 TInt saveAttempts = 5;
576 TInt err = aScreenBmp->Save(KScreenBmpFile);
577 while ((err != KErrNone) && saveAttempts--)
580 _LIT(KSaveRetry,"DoAlphaBlendingTestL: Bitmap save failed, retrying.");
581 INFO_PRINTF1(KSaveRetry);
583 err = aScreenBmp->Save(KScreenBmpFile);
585 User::LeaveIfError(err);
587 User::LeaveIfError(aScreenBmp->Resize(TSize(aSrcRc1.Width(), aSrcRc1.Height())));
590 iGc->SetDrawMode(CGraphicsContext::EDrawModeWriteAlpha);
591 iGc->DrawBitmap(screenBmpRc, aScreenBmp);
594 iGc->BitBltMasked(aDestPt, aSrcBmp, aSrcRc1, aAlphaBmp, EFalse);
596 //Get screen data and write the data to screenBmpDestData1.
597 for(i=0;i<scrDevSize.iHeight;i++)
599 TPtr8 p(screenBmpDestData1 + i * scrDevSize.iWidth * 2, scrDevSize.iWidth * 2, scrDevSize.iWidth * 2);
600 iDevice->GetScanLine(p, TPoint(0, i), scrDevSize.iWidth, EColor64K);
602 // Alpha blending using explicit form of CFbsBitGc::AlphaBlendBitmaps //
605 _LIT(KLog2," CFbsBitGc::AlphaBlendBitmaps explicit test");
612 User::LeaveIfError(aScreenBmp->Load(KScreenBmpFile));
613 //Do AlphaBlendBitmaps()
614 User::LeaveIfError(iGc->AlphaBlendBitmaps(aDestPt, aSrcBmp, aScreenBmp, aSrcRc1,
615 aSrcPt2, aAlphaBmp, aAlphaPt));
617 //Get screen data and write the data to screenBmpDestData2.
618 for(i=0;i<scrDevSize.iHeight;i++)
620 TPtr8 p(screenBmpDestData2 + i * scrDevSize.iWidth * 2, scrDevSize.iWidth * 2, scrDevSize.iWidth * 2);
621 iDevice->GetScanLine(p, TPoint(0, i), scrDevSize.iWidth, EColor64K);
623 //Compare screen bitmaps //
624 res = Mem::Compare(screenBmpDestData1, allocatedSize, screenBmpDestData2, allocatedSize);
626 // colour comparison tolerance between RGB565 components.
627 const TInt KColourComparisonTolerance = 2;
630 TBool testPassed = ETrue;
631 // strict byte-for-byte comparison of the pixel maps could have failed because blending algorithms may
632 // differ slightly, but actual resulting colours may be close enough
633 for (int byteNumber = 0; byteNumber < allocatedSize; byteNumber+=2)
635 // find any RGB565 value that doesn't match and examine each component
636 if ( *(TUint16*)(screenBmpDestData1 + byteNumber) != *(TUint16*)(screenBmpDestData2 + byteNumber))
638 TUint16 Rgb1 = *(TUint16*)(screenBmpDestData1 + byteNumber);
639 TUint16 Rgb2 = *(TUint16*)(screenBmpDestData2 + byteNumber);
641 TInt16 Red1 = (Rgb1 & 0xF800) >> 11;
642 TInt16 Red2 = (Rgb2 & 0xF800) >> 11;
643 TInt16 DiffRed = Abs(Red1 - Red2);
645 TInt16 Green1 = (Rgb1 & 0x07E0) >> 5;
646 TInt16 Green2 = (Rgb2 & 0x07E0) >> 5;
647 TInt16 DiffGreen = Abs(Green1 - Green2);
649 TInt16 Blue1 = (Rgb1 & 0x001F);
650 TInt16 Blue2 = (Rgb2 & 0x001F);
651 TInt16 DiffBlue = Abs(Blue1 - Blue2);
653 // is any difference is outside the tolerance break out signaling test failure
654 if (DiffRed > KColourComparisonTolerance ||
655 DiffGreen > KColourComparisonTolerance ||
656 DiffBlue > KColourComparisonTolerance)
669 if (res && (attempt < KMaximumAttempts - 1))
671 INFO_PRINTF1(_L("Memory comparison 1 failed, retrying"));
672 // Skip to next attempt
677 // Alpha blending using implicit form of CFbsBitGc::AlphaBlendBitmaps //
680 _LIT(KLog3," CFbsBitGc::AlphaBlendBitmaps implicit test");
686 //Draw screen bitmap (it's already loaded)
687 User::LeaveIfError(aScreenBmp->Resize(TSize(aSrcRc1.Width(), aSrcRc1.Height())));
688 iGc->SetDrawMode(CGraphicsContext::EDrawModeWriteAlpha);
689 iGc->DrawBitmap(screenBmpRc, aScreenBmp);
691 //Do AlphaBlendBitmaps()
692 User::LeaveIfError(iGc->AlphaBlendBitmaps(aDestPt, aSrcBmp, aSrcRc1, aAlphaBmp, aSrcRc1.iTl));
694 //Get screen data and write the data to screenBmpDestData2.
695 for(i=0;i<scrDevSize.iHeight;i++)
697 TPtr8 p(screenBmpDestData2 + i * scrDevSize.iWidth * 2, scrDevSize.iWidth * 2, scrDevSize.iWidth * 2);
698 iDevice->GetScanLine(p, TPoint(0, i), scrDevSize.iWidth, EColor64K);
700 //Compare screen bitmaps //
703 _LIT(KLog4," Compare Screen Bitmaps");
706 res = Mem::Compare(screenBmpDestData1, allocatedSize, screenBmpDestData2, allocatedSize);
708 if (res && (attempt < KMaximumAttempts - 1))
710 INFO_PRINTF1(_L("Memory comparison 2 failed, retrying"));
713 // Reload the screen bitmap as it was before:
714 User::LeaveIfError(aScreenBmp->Load(KScreenBmpFile));
718 //Destroy the allocated memory blocks
719 CleanupStack::PopAndDestroy(2);//screenBmpDestData1 & screenBmpDestData2
722 void CTAlphaBlending::CreateAlphaBlendingBitmapsLC(CFbsBitmap*& aScreenBmp,
723 CFbsBitmap*& aSrcBmp,
724 CFbsBitmap*& aAlphaBmp,
728 TSize size(KWidth, KHeight);
730 TUint8 screenBmpSrcData[KHeight][KWidth * 3 + 2] = // "+ 2" - every row is aligned to a 32 bit boundary
731 {//0 1 2 3 4 5 6 7 8 9
732 {0x00, 0x00, 0x00, 0x20, 0x21, 0x22, 0x30, 0x31, 0x32, 0x40, 0x41, 0x42, 0x50, 0x51, 0x51, 0x60, 0x61, 0x62, 0x70, 0x71, 0x72, 0x80, 0x81, 0x82, 0x90, 0x91, 0x92, 0xA0, 0xA1, 0xA2, 0x00, 0x00},
733 {0x19, 0x18, 0x17, 0x29, 0x28, 0x27, 0x39, 0x38, 0x37, 0x49, 0x48, 0x47, 0x59, 0x58, 0x57, 0x69, 0x68, 0x67, 0x79, 0x78, 0x77, 0x89, 0x88, 0x87, 0x99, 0x98, 0x97, 0xA9, 0xA8, 0xA7, 0x00, 0x00},
734 {0x1F, 0x1E, 0x1D, 0x2F, 0x2E, 0x2D, 0x3F, 0x3E, 0x3D, 0x4F, 0x4E, 0x4D, 0x5F, 0x5E, 0x5D, 0x6F, 0x6E, 0x6D, 0x7F, 0x7E, 0x7D, 0x8F, 0x8E, 0x8D, 0x9F, 0x9E, 0x9D, 0xAF, 0xAE, 0xAD, 0x00, 0x00}
736 //The source bitmap data
737 TUint8 srcBmpData[KHeight][KWidth * 3 + 2] = // "+ 2" - every row is aligned to a 32 bit boundary
738 {//0 1 2 3 4 5 6 7 8 9
739 {0x32, 0x67, 0xA2, 0x11, 0x34, 0x67, 0xC5, 0xA3, 0x91, 0x01, 0xB3, 0xA8, 0xF3, 0x3F, 0x1E, 0x88, 0x11, 0x12, 0xAE, 0xEE, 0x9A, 0x56, 0x12, 0x81, 0xB4, 0xCA, 0x91, 0xFF, 0x1A, 0x2A, 0x00, 0x00},
740 {0x02, 0xB1, 0xE2, 0xAA, 0xBB, 0x13, 0x22, 0xA8, 0xC3, 0x75, 0x8D, 0xFF, 0xA4, 0xAB, 0x00, 0xC5, 0xA6, 0x22, 0xBB, 0x09, 0xC1, 0x97, 0x25, 0xC6, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0x00, 0x00},
741 {0x32, 0x50, 0x76, 0x27, 0xCC, 0x45, 0x81, 0xE5, 0xE9, 0xB7, 0xCD, 0x11, 0x32, 0xB1, 0x23, 0xFF, 0x71, 0x11, 0xCC, 0xAA, 0xF2, 0x98, 0x13, 0x8C, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x00, 0x00}
743 //The alpha bitmap data
744 TUint8 alphaBmpData[KHeight][KWidth + 2] = // "+ 2" - every row is aligned to a 32 bit boundary
745 {//0 1 2 3 4 5 6 7 8 9
746 {0x68, 0x68, 0x68, 0xAC, 0xD9, 0xB2, 0x8F, 0x11, 0xA0, 0xC1, 0x00, 0x00},
747 {0x71, 0x5A, 0xF6, 0xEE, 0xF9, 0xE5, 0x06, 0x4C, 0xBB, 0x7B, 0x00, 0x00},
748 {0x9F, 0x99, 0x45, 0x17, 0xA8, 0xF5, 0xFF, 0xD2, 0x22, 0x1D, 0x00, 0x00}
751 if(aMode == EColor16MU)
754 TInt buffer_size = KWidth * 4 ;
755 TUint8* buffer = new(ELeave) TUint8[buffer_size];
756 TPtr8 source_ptr(buffer, buffer_size, buffer_size);
759 aScreenBmp = new (ELeave) CFbsBitmap;
760 CleanupStack::PushL(aScreenBmp);
761 User::LeaveIfError(aScreenBmp->Create(size, aMode));
762 for(i=0; i<KHeight; i++)
764 TUint8* bufCur = buffer;
765 TUint8* bufSrcCur = screenBmpSrcData[i];
766 TUint8* bufSrcEnd = bufSrcCur + KWidth * 3;
767 while(bufSrcCur < bufSrcEnd)
769 *bufCur++ = *bufSrcCur++;
770 *bufCur++ = *bufSrcCur++;
771 *bufCur++ = *bufSrcCur++;
774 aScreenBmp->SetScanLine(source_ptr, i);
778 aSrcBmp = new (ELeave) CFbsBitmap;
779 CleanupStack::PushL(aSrcBmp);
780 User::LeaveIfError(aSrcBmp->Create(size, aMode));
781 for(i=0; i<KHeight; i++)
783 TUint8* bufCur = buffer;
784 TUint8* bufSrcCur = srcBmpData[i];
785 TUint8* bufSrcEnd = bufSrcCur + KWidth * 3;
786 while(bufSrcCur < bufSrcEnd)
788 *bufCur++ = *bufSrcCur++;
789 *bufCur++ = *bufSrcCur++;
790 *bufCur++ = *bufSrcCur++;
793 aSrcBmp->SetScanLine(source_ptr, i);
798 aAlphaBmp = new (ELeave) CFbsBitmap;
799 CleanupStack::PushL(aAlphaBmp);
800 User::LeaveIfError(aAlphaBmp->Create(size, EGray256));
801 for(i=0;i<KHeight;i++)
803 TPtr8 p(alphaBmpData[i], KWidth * 3, KWidth * 3);
804 aAlphaBmp->SetScanLine(p, i);
812 aScreenBmp = new (ELeave) CFbsBitmap;
813 CleanupStack::PushL(aScreenBmp);
814 User::LeaveIfError(aScreenBmp->Create(size, aMode));
815 for(i=0;i<KHeight;i++)
817 TPtr8 p(screenBmpSrcData[i], KWidth * 3, KWidth * 3);
818 aScreenBmp->SetScanLine(p, i);
821 aSrcBmp = new (ELeave) CFbsBitmap;
822 CleanupStack::PushL(aSrcBmp);
823 User::LeaveIfError(aSrcBmp->Create(size, aMode));
824 for(i=0;i<KHeight;i++)
826 TPtr8 p(srcBmpData[i], KWidth * 3, KWidth * 3);
827 aSrcBmp->SetScanLine(p, i);
830 aAlphaBmp = new (ELeave) CFbsBitmap;
831 CleanupStack::PushL(aAlphaBmp);
832 User::LeaveIfError(aAlphaBmp->Create(size, EGray256));
833 for(i=0;i<KHeight;i++)
835 TPtr8 p(alphaBmpData[i], KWidth * 3, KWidth * 3);
836 aAlphaBmp->SetScanLine(p, i);
841 void CTAlphaBlending::DestroyAlphaBlendingBitmaps(CFbsBitmap*& aScreenBmp,
842 CFbsBitmap*& aSrcBmp,
843 CFbsBitmap*& aAlphaBmp)
845 CleanupStack::PopAndDestroy(aAlphaBmp);
847 CleanupStack::PopAndDestroy(aSrcBmp);
849 CleanupStack::PopAndDestroy(aScreenBmp);
853 //The test doesn't check anything. It is for debugging only.
854 void CTAlphaBlending::TestAlphaBlending2L()
856 static const TDisplayMode modeList[] = {
857 EColor64K, EColor256, EColor16MAP, EColor16MA, EColor16MU, EColor4K
859 const TInt KNumTestDisplayModes=sizeof(modeList)/sizeof(modeList[0]);
860 for(TInt orientation=0;orientation<=CFbsBitGc::EGraphicsOrientationRotated270;orientation++)
862 CFbsBitmap* srcBmp = NULL;
863 CFbsBitmap* alphaBmp = NULL;
864 TInt err = KErrNotSupported;
866 for (TInt modeIndex = 0; (err != KErrNone) && modeIndex < KNumTestDisplayModes; modeIndex++)
868 err = CreateScreenDevice(modeList[modeIndex],(CFbsBitGc::TGraphicsOrientation)orientation);
870 if (err == KErrNotSupported)
872 // Orientation not supported, try next one
876 //The source bitmap data
877 TUint8 srcBmpData[100];
880 srcBmpData[j] = 0xAA;
882 //The alpha bitmap data
883 TUint8 alphaBmpData[20];
886 alphaBmpData[j] = TUint8(j);
889 srcBmp = new (ELeave) CFbsBitmap;
890 CleanupStack::PushL(srcBmp);
891 User::LeaveIfError(srcBmp->Create(TSize(100, 1), EColor256));
892 TPtr8 p1(srcBmpData, 100, 100);
893 srcBmp->SetScanLine(p1, 0);
895 alphaBmp = new (ELeave) CFbsBitmap;
896 CleanupStack::PushL(alphaBmp);
897 User::LeaveIfError(alphaBmp->Create(TSize(20, 1), EGray256));
898 TPtr8 p2(alphaBmpData, 20, 20);
899 alphaBmp->SetScanLine(p2, 0);
901 iGc->BitBltMasked(TPoint(20, 20), srcBmp, TRect(10, 0, 100, 1), alphaBmp, EFalse);
903 iGc->SetOrientation(CFbsBitGc::EGraphicsOrientationNormal);
905 CleanupStack::PopAndDestroy(alphaBmp);
906 CleanupStack::PopAndDestroy(srcBmp);
910 TUint32 AlphaBlend(TUint32 aDestPixel, TUint32 aSrcPixel, TUint8 aMask)
912 TUint32 dr = (aDestPixel & 0x00FF0000) >> 16;
913 TUint32 dg = (aDestPixel & 0x0000FF00) >> 8;
914 TUint32 db = aDestPixel & 0x000000FF;
916 TUint32 sr = (aSrcPixel & 0x00FF0000) >> 16;
917 TUint32 sg = (aSrcPixel & 0x0000FF00) >> 8;
918 TUint32 sb = aSrcPixel & 0x000000FF;
920 TUint32 rr = (aMask * sr)/255 + ((0xFF - aMask) * dr)/255;
921 TUint32 rg = (aMask * sg)/255 + ((0xFF - aMask) * dg)/255;
922 TUint32 rb = (aMask * sb)/255 + ((0xFF - aMask) * db)/255;
924 return(rr << 16 | rg << 8 | rb | 0xff000000);
927 inline TUint32 OptimizedBlend32(TInt aPrimaryRed,TInt aPrimaryGreen,TInt aPrimaryBlue,TUint32 aSecondary,TUint8 aAlphaValue)
930 if(aAlphaValue == 0xff)
932 return (aPrimaryBlue + (aPrimaryGreen<<8) + (aPrimaryRed<<16)) | 0xff000000;
936 const TUint32 alphaValue = (aAlphaValue << 8) + aAlphaValue;
938 const TInt r2 = (aSecondary & 0x00ff0000) >> 16;
939 const TInt g2 = (aSecondary & 0x0000ff00) >> 8;
940 const TInt b2 = aSecondary & 0x000000ff;
942 const TInt r3 = ((alphaValue * (aPrimaryRed - r2)) >> 16) + r2;
943 const TInt g3 = ((alphaValue * (aPrimaryGreen - g2)) >> 16) + g2;
944 const TInt b3 = ((alphaValue * (aPrimaryBlue - b2)) >> 16) + b2;
946 return (b3 & 0xFF) | ((g3<<8) & 0xFF00) | ((r3<<16) & 0xFF0000) | 0xFF000000;
952 @SYMTestCaseID GRAPHICS-BITGDI-0085
958 @SYMTestPriority High
960 @SYMTestStatus Implemented
964 @SYMTestExpectedResults
966 // This tests the correctness of the results of alpha-blending with EColor16MA
967 void CTAlphaBlending::TestAlphaBlendCorrect(TDisplayMode /* aScreenMode */, TDisplayMode /* aBitmapMode */)
970 TUint32 top = 0xFFCCEE55;
971 TUint32 beneath = 0xFFEEAA66;
976 for(TInt i = 0; i < 100000; i++)
978 top = Math::Random();
979 beneath = Math::Random();
980 alpha = Math::Random();
981 TUint32 res1 = AlphaBlend(beneath, top, alpha);
982 TUint32 res2 = OptimizedBlend32((top >> 16) & 0xFF,(top >>8) & 0xFF,(top & 0xFF),beneath, alpha);
987 TInt diff1 = res1 & 0xFF;
988 TInt diff2 = res2 & 0xFF;
990 diff = diff1 - diff2;
998 diff1 = (res1 >> 8) & 0xFF;
999 diff2 = (res2 >> 8) & 0xFF;
1001 diff = diff1 - diff2;
1010 diff1 = (res1 >> 16) & 0xFF;
1011 diff2 = (res2 >> 16) & 0xFF;
1013 diff = diff1 - diff2;
1024 INFO_PRINTF1(_L("Results:"));
1027 INFO_PRINTF2(_L("Max Diff = %i"), maxDiff);
1029 INFO_PRINTF1(_L("Results are identical"));
1033 __CONSTRUCT_STEP__(AlphaBlending)
1035 void CTAlphaBlendingStep::TestSetupL()