os/graphics/fbs/fontandbitmapserver/tfbs/tglyphatlas.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
/**
sl@0
    17
 @file
sl@0
    18
 @internalComponent - Internal Symbian test code
sl@0
    19
*/
sl@0
    20
sl@0
    21
#include "tglyphatlas.h"
sl@0
    22
#include "TFBS.H"
sl@0
    23
#include "glyphatlastestwrapper.h"
sl@0
    24
sl@0
    25
_LIT(KTypefaceName, "DejaVu Sans Condensed");
sl@0
    26
_LIT(KMonoTypefaceName, "DejaVu Sans Mono");
sl@0
    27
const TInt KNumGlyphCodesLatin = 96;
sl@0
    28
sl@0
    29
sl@0
    30
CTGlyphAtlas::CTGlyphAtlas(CTestStep* aStep):
sl@0
    31
	CTGraphicsBase(aStep),
sl@0
    32
	iFbs(NULL),
sl@0
    33
	iTs(NULL),
sl@0
    34
	iGlyphCodesLatin(NULL),
sl@0
    35
	iFont(NULL)
sl@0
    36
	{
sl@0
    37
	}
sl@0
    38
sl@0
    39
void CTGlyphAtlas::ConstructL()
sl@0
    40
	{
sl@0
    41
	iFbs = RFbsSession::GetSession();
sl@0
    42
	iTs = (CFbsTypefaceStore*)CFbsTypefaceStore::NewL(NULL);
sl@0
    43
	User::LeaveIfError(iTs->GetNearestFontToDesignHeightInPixels((CFont*&)iFont, TFontSpec(KTypefaceName, 15)));
sl@0
    44
	User::LeaveIfError(iTs->GetNearestFontToDesignHeightInPixels((CFont*&)iFont2, TFontSpec(KMonoTypefaceName, 8)));
sl@0
    45
	
sl@0
    46
	iGlyphCodesLatin = new(ELeave) TUint[KNumGlyphCodesLatin];
sl@0
    47
	for (TInt ii = 0; ii < KNumGlyphCodesLatin; ++ii)
sl@0
    48
		{
sl@0
    49
		iGlyphCodesLatin[ii] = ii+32; // ASCII characters from 0020 to 007F
sl@0
    50
		}
sl@0
    51
sl@0
    52
	INFO_PRINTF1(_L("FBSERV Glyph Atlas Testing"));
sl@0
    53
	}
sl@0
    54
sl@0
    55
CTGlyphAtlas::~CTGlyphAtlas()
sl@0
    56
	{
sl@0
    57
	if (iTs)
sl@0
    58
		{
sl@0
    59
		iTs->ReleaseFont(iFont);
sl@0
    60
		}
sl@0
    61
	delete iTs;
sl@0
    62
	delete[] iGlyphCodesLatin;
sl@0
    63
	}
sl@0
    64
sl@0
    65
void CTGlyphAtlas::RunTestCaseL(TInt aCurTestCase)
sl@0
    66
	{
sl@0
    67
	((CTGlyphAtlasStep*)iStep)->SetTestStepID(KUnknownSYMTestCaseIDName);
sl@0
    68
	switch(aCurTestCase)
sl@0
    69
		{
sl@0
    70
	case 1:
sl@0
    71
		((CTGlyphAtlasStep*)iStep)->SetTestStepID(_L("GRAPHICS-FBSERV-0663"));
sl@0
    72
		TestFullCache();
sl@0
    73
		break;
sl@0
    74
	case 2:
sl@0
    75
		((CTGlyphAtlasStep*)iStep)->SetTestStepID(_L("GRAPHICS-FBSERV-0664"));
sl@0
    76
		TestFontReleased();
sl@0
    77
		break;
sl@0
    78
	default:
sl@0
    79
		((CTGlyphAtlasStep*)iStep)->SetTestStepID(KNotATestSYMTestCaseIDName);
sl@0
    80
		((CTGlyphAtlasStep*)iStep)->CloseTMSGraphicsStep();
sl@0
    81
		TestComplete();		
sl@0
    82
		break;
sl@0
    83
		}
sl@0
    84
	((CTGlyphAtlasStep*)iStep)->RecordTestResultL();
sl@0
    85
	}
sl@0
    86
sl@0
    87
/**
sl@0
    88
@SYMTestCaseID		GRAPHICS-FBSERV-0663
sl@0
    89
@SYMTestPriority	High
sl@0
    90
@SYMTestType		UT
sl@0
    91
@SYMTestStatus		Implemented
sl@0
    92
@SYMPREQ			PREQ2678
sl@0
    93
sl@0
    94
@SYMTestCaseDesc
sl@0
    95
	Shows that when the glyph atlas reaches its memory limit, any new added 
sl@0
    96
	glyphs will cause the eviction of the least recently used glyphs.
sl@0
    97
	The new glyph will be added successfully.
sl@0
    98
sl@0
    99
@SYMTestActions
sl@0
   100
	i. Create a glyph atlas with a memory limit of 1000 bytes.
sl@0
   101
	ii. Add glyphs to the atlas such that its memory consumption reaches the limit.
sl@0
   102
	iii. Add one more glyph.
sl@0
   103
	iv. Call CGlyphAtlas::GetGlyph() for the last glyph added.
sl@0
   104
	v. Call CGlyphAtlas::GetGlyph() for the least recently used glyphs to check 
sl@0
   105
		that it has been evicted.
sl@0
   106
	vi. Delete glyph atlas
sl@0
   107
sl@0
   108
@SYMTestExpectedResults
sl@0
   109
	Each glyph is added successfully.
sl@0
   110
	CGlyphAtlas::GetGlyph() returns KErrNone for the last glyph added.
sl@0
   111
	CGlyphAtlas::GetGlyph() returns KErrNotFound for the least recently used glyph.
sl@0
   112
*/
sl@0
   113
void CTGlyphAtlas::TestFullCache()
sl@0
   114
	{
sl@0
   115
	INFO_PRINTF1(_L("Test full cache eviction"));
sl@0
   116
sl@0
   117
	__UHEAP_MARK;
sl@0
   118
	// Fill cache up using expected size of glyph using AddGlyph.
sl@0
   119
	const TInt KMaxAtlasSizeInBytes = 1000;
sl@0
   120
	CGlyphAtlasTestWrapper* atlas = NULL;
sl@0
   121
	TRAPD(ret, atlas = CGlyphAtlasTestWrapper::NewL(KMaxAtlasSizeInBytes));
sl@0
   122
	TESTNOERROR(ret);
sl@0
   123
	if (KErrNone != ret)
sl@0
   124
		{
sl@0
   125
		return;
sl@0
   126
		}
sl@0
   127
	TOpenFontCharMetrics charMetrics;
sl@0
   128
	TGlyphImageInfo imageInfo;
sl@0
   129
	TSize bitmapSize;
sl@0
   130
	const TUint8* bitmapData = NULL;
sl@0
   131
	TInt glyphIndex = 0;
sl@0
   132
	TBool atlasFull = EFalse;
sl@0
   133
	CBitmapFont* bmFont = CTFbsFont::FontAddress(iFont);
sl@0
   134
	// Fill up atlas by adding glyphs.
sl@0
   135
	// Next glyph shold tip the atlas over the memory limit.
sl@0
   136
	// Glyphs are added in ascending glyph code order.
sl@0
   137
	// Leave at least one glyph so that we can guarantee that we can add one more unique glyph.
sl@0
   138
	while (glyphIndex < KNumGlyphCodesLatin-1 && !atlasFull)
sl@0
   139
		{
sl@0
   140
		iFont->GetCharacterData(iGlyphCodesLatin[glyphIndex], charMetrics, bitmapData, bitmapSize);
sl@0
   141
		TInt sizeInBytes = charMetrics.Height() * charMetrics.Width();
sl@0
   142
		if (atlas->SizeInBytes() + sizeInBytes <= KMaxAtlasSizeInBytes)
sl@0
   143
			{
sl@0
   144
			CGlyphAtlas::TAddGlyphArgs args(bitmapData, iGlyphCodesLatin[glyphIndex++], charMetrics);
sl@0
   145
			TESTNOERROR(atlas->AddGlyph(*bmFont, args, imageInfo));
sl@0
   146
			}
sl@0
   147
		else
sl@0
   148
			{
sl@0
   149
			atlasFull = ETrue;
sl@0
   150
			}
sl@0
   151
		}
sl@0
   152
	TEST(atlasFull);
sl@0
   153
sl@0
   154
	// check least recently used page contains the first glyph in glyph codes
sl@0
   155
	TUint leastUsedGlyphCode = iGlyphCodesLatin[0];
sl@0
   156
	TEST(atlas->LruPageContainsGlyph(leastUsedGlyphCode));
sl@0
   157
sl@0
   158
	// To ensure that the test does not pass if a FIFO eviction policy occurs,
sl@0
   159
	// get the least recently used glyph so that it is moved internally.
sl@0
   160
	TESTNOERROR(atlas->GetGlyph(*bmFont, leastUsedGlyphCode, imageInfo));
sl@0
   161
sl@0
   162
	// glyphIndex, bitmapData and charMetrics now current for next glyph which 
sl@0
   163
	// will take the atlas over the cache limit.
sl@0
   164
	CGlyphAtlas::TAddGlyphArgs args(bitmapData, iGlyphCodesLatin[glyphIndex], charMetrics);
sl@0
   165
	TESTNOERROR(atlas->AddGlyph(*bmFont, args, imageInfo));
sl@0
   166
sl@0
   167
	// check that searching for most recently added glyph is successful
sl@0
   168
	TGlyphImageInfo newInfo;
sl@0
   169
	TESTNOERROR(atlas->GetGlyph(*bmFont, iGlyphCodesLatin[glyphIndex], newInfo));
sl@0
   170
sl@0
   171
	// check atlas size is still under the limit
sl@0
   172
	TEST(atlas->SizeInBytes() <= KMaxAtlasSizeInBytes);
sl@0
   173
sl@0
   174
	// check that the pages which were evicted contained the least used glyphs
sl@0
   175
	// i.e. searching for these returns KErrNotFound
sl@0
   176
	TInt err = KErrNotFound;
sl@0
   177
	TInt index = 1;
sl@0
   178
	for (; KErrNotFound == err && index <= glyphIndex; ++index)
sl@0
   179
		{
sl@0
   180
		err = atlas->GetGlyph(*bmFont, iGlyphCodesLatin[index], newInfo);
sl@0
   181
		}
sl@0
   182
	TESTNOERROR(err);
sl@0
   183
	// first found glyph should be greater than glyph at index 1
sl@0
   184
	TEST(index-1 > 1);
sl@0
   185
	
sl@0
   186
	// check that the remaining pages contained the least used glyphs
sl@0
   187
	for (; index <= glyphIndex; ++index)
sl@0
   188
		{
sl@0
   189
		TESTNOERROR(atlas->GetGlyph(*bmFont, iGlyphCodesLatin[index], newInfo));
sl@0
   190
		}
sl@0
   191
sl@0
   192
	delete atlas;
sl@0
   193
	__UHEAP_MARKEND;
sl@0
   194
	}
sl@0
   195
sl@0
   196
sl@0
   197
/**
sl@0
   198
@SYMTestCaseID		GRAPHICS-FBSERV-0664
sl@0
   199
@SYMTestPriority	High
sl@0
   200
@SYMTestType		UT
sl@0
   201
@SYMTestStatus		Implemented
sl@0
   202
@SYMPREQ			PREQ2678
sl@0
   203
sl@0
   204
@SYMTestCaseDesc
sl@0
   205
	Shows that CGlyphAtlas::FontReleased() does actually delete all the glyphs 
sl@0
   206
	associated with the released font and leaves glyphs associated with other 
sl@0
   207
	fonts untouched.
sl@0
   208
sl@0
   209
@SYMTestActions
sl@0
   210
	i. Create a glyph atlas with no memory limit.
sl@0
   211
	ii. Add glyphs for two different fonts to the atlas.
sl@0
   212
	iii. Check all glyphs for both fonts were successfully added.
sl@0
   213
	iv. Call CGlyphAtlas::ReleaseFont for one of the fonts.
sl@0
   214
	v. Check that there are no glyphs associated with the released font.
sl@0
   215
	vi. Call CGlyphAtlas::ReleaseFont for the remaining font.
sl@0
   216
	vii Check that there are no glyphs associated with the released font and 
sl@0
   217
		that the atlas is empty.
sl@0
   218
	viii Delete the glyph atlas.
sl@0
   219
	
sl@0
   220
@SYMTestExpectedResults
sl@0
   221
	After each font is released, there are no glyphs associated with that font 
sl@0
   222
	left in the atlas.
sl@0
   223
*/
sl@0
   224
void CTGlyphAtlas::TestFontReleased()
sl@0
   225
	{
sl@0
   226
	INFO_PRINTF1(_L("Test behaviour of CGlyphAtlas::FontReleased()"));
sl@0
   227
sl@0
   228
	__UHEAP_MARK;
sl@0
   229
	CGlyphAtlasTestWrapper* atlas = NULL;
sl@0
   230
	TRAPD(ret, atlas = CGlyphAtlasTestWrapper::NewL(KGlyphAtlasNoCacheLimit));
sl@0
   231
	TESTNOERROR(ret);
sl@0
   232
	if (KErrNone != ret)
sl@0
   233
		{
sl@0
   234
		return;
sl@0
   235
		}
sl@0
   236
	TOpenFontCharMetrics charMetrics;
sl@0
   237
	TGlyphImageInfo imageInfo;
sl@0
   238
	TSize bitmapSize;
sl@0
   239
	const TUint8* bitmapData = NULL;
sl@0
   240
	CBitmapFont* bmFont = CTFbsFont::FontAddress(iFont);
sl@0
   241
	CBitmapFont* bmFont2 = CTFbsFont::FontAddress(iFont2);
sl@0
   242
	for (TInt glyphIndex = 0; glyphIndex < KNumGlyphCodesLatin; ++glyphIndex)
sl@0
   243
		{
sl@0
   244
		iFont->GetCharacterData(iGlyphCodesLatin[glyphIndex], charMetrics, bitmapData, bitmapSize);
sl@0
   245
		CGlyphAtlas::TAddGlyphArgs args(bitmapData, iGlyphCodesLatin[glyphIndex], charMetrics);
sl@0
   246
		TESTNOERROR(atlas->AddGlyph(*bmFont, args, imageInfo));
sl@0
   247
sl@0
   248
		iFont2->GetCharacterData(iGlyphCodesLatin[KNumGlyphCodesLatin-1-glyphIndex], charMetrics, bitmapData, bitmapSize);
sl@0
   249
		CGlyphAtlas::TAddGlyphArgs args2(bitmapData, iGlyphCodesLatin[glyphIndex], charMetrics);
sl@0
   250
		TESTNOERROR(atlas->AddGlyph(*bmFont2, args2, imageInfo));
sl@0
   251
		}
sl@0
   252
	// check there are font entries for these 2 fonts
sl@0
   253
	TEST(2 == atlas->FontCount());
sl@0
   254
sl@0
   255
	// check actual number of glyphs in atlas for each font is as expected
sl@0
   256
	TEST(KNumGlyphCodesLatin == atlas->GlyphCountByFont(bmFont));
sl@0
   257
	TEST(KNumGlyphCodesLatin == atlas->GlyphCountByFont(bmFont2));
sl@0
   258
	TEST(2*KNumGlyphCodesLatin == atlas->GlyphCount());
sl@0
   259
sl@0
   260
	// release one font and check number of glyphs in atlas for each font 
sl@0
   261
	// is as expected
sl@0
   262
	atlas->FontReleased(*bmFont);
sl@0
   263
	TEST(1 == atlas->FontCount());
sl@0
   264
	TEST(0 == atlas->GlyphCountByFont(bmFont));
sl@0
   265
	TEST(KNumGlyphCodesLatin == atlas->GlyphCountByFont(bmFont2));
sl@0
   266
	TEST(KNumGlyphCodesLatin == atlas->GlyphCount());
sl@0
   267
sl@0
   268
	// release one font and check number of glyphs in atlas for each font 
sl@0
   269
	// is as expected
sl@0
   270
	atlas->FontReleased(*bmFont2);
sl@0
   271
	TEST(0 == atlas->FontCount());
sl@0
   272
	TEST(0 == atlas->GlyphCountByFont(bmFont));
sl@0
   273
	TEST(0 == atlas->GlyphCountByFont(bmFont2));
sl@0
   274
	TEST(0 == atlas->GlyphCount());
sl@0
   275
sl@0
   276
	delete atlas;
sl@0
   277
sl@0
   278
	__UHEAP_MARKEND;
sl@0
   279
	}
sl@0
   280
sl@0
   281
sl@0
   282
sl@0
   283
//--------------
sl@0
   284
__CONSTRUCT_STEP__(GlyphAtlas)