fbs/fontandbitmapserver/sfbs/glyphatlas.cpp
author hgs
Thu, 09 Sep 2010 10:13:45 +0300
changeset 171 414d4b727fd9
parent 121 d72fc2aace31
permissions -rw-r--r--
201034
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
121
d72fc2aace31 201027_1
hgs
parents:
diff changeset
     1
// Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
d72fc2aace31 201027_1
hgs
parents:
diff changeset
     2
// All rights reserved.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
     3
// This component and the accompanying materials are made available
d72fc2aace31 201027_1
hgs
parents:
diff changeset
     4
// under the terms of "Eclipse Public License v1.0"
d72fc2aace31 201027_1
hgs
parents:
diff changeset
     5
// which accompanies this distribution, and is available
d72fc2aace31 201027_1
hgs
parents:
diff changeset
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
d72fc2aace31 201027_1
hgs
parents:
diff changeset
     7
//
d72fc2aace31 201027_1
hgs
parents:
diff changeset
     8
// Initial Contributors:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
     9
// Nokia Corporation - initial contribution.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    10
//
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    11
// Contributors:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    12
//
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    13
// Description:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    14
//
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    15
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    16
#include "glyphatlas.h"
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    17
#include "OstTraceDefinitions.h"
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    18
#ifdef OST_TRACE_COMPILER_IN_USE
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    19
#include "glyphatlasTraces.h"
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    20
#endif
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    21
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    22
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    23
extern void Panic(TFbsPanic aPanic);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    24
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    25
static TInt16 Load16(const TUint8* aPtr);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    26
static void DecodeBinaryData(const TSize& aGlyphSize, const TUint8* aEncodedData, TUint8* aByteData);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    27
static void DecodeBinaryDataExLarge(const TSize& aGlyphSize, const TUint8* aEncodedData, TUint8* aByteData);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    28
static void Convert1BppTo8Bpp(TUint32 aSrcData, TUint8*& aDestDataPtr, const TUint8* aDestDataPtrLimit);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    29
static void CopyCharLine(TUint8*& aByteDataPtr, TInt aWidthInBytes, const TUint8* aSrcData, TInt aBitShift, TInt16 aRepeatCount);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    30
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    31
// === CGlyphAtlas Functions ===
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    32
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    33
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    34
Glyph Atlas constructor.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    35
@param aMaxCacheSizeInBytes The maximum amount of specialised graphics memory 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    36
	that the glyph atlas should use. If this value is KGlyphAtlasNoCacheLimit,
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    37
	then there is no limit and the atlas will use as much memory as is available
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    38
	in the system.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    39
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    40
CGlyphAtlas::CGlyphAtlas(TInt aMaxCacheSizeInBytes)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    41
	:iLruPageList(_FOFF(CGlyphAtlasPage, iLink)),
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    42
	 iFontEntryArray(32, _FOFF(TFontEntryMap, iFont)),
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    43
	 iMaxCacheSizeInBytes(aMaxCacheSizeInBytes),
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    44
	 iMaxCacheSizeHigh(aMaxCacheSizeInBytes),
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    45
	 iGpuCacheSizeLimitIsMax(ETrue)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    46
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    47
    iMaxCacheSizeLow = ( KGlyphAtlasNoCacheLimit == aMaxCacheSizeInBytes )
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    48
                     ? KGlyphAtlasLowMemCacheLimitDefault
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    49
                     : ( aMaxCacheSizeInBytes / KGlyphAtlasLowMemCacheLimitDivisor );
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    50
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    51
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    52
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    53
Glyph Atlas destructor.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    54
Frees all the RSgImage handles, frees all the allocated system memory, and
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    55
closes the Graphics Resource driver. 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    56
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    57
CGlyphAtlas::~CGlyphAtlas()
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    58
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    59
	// cycle through all the font entries and destroy them
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    60
	for (TInt ii = iFontEntryArray.Count()-1; ii >= 0; --ii)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    61
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    62
		DeleteFontEntry(iFontEntryArray[ii].iEntry);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    63
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    64
	iFontEntryArray.Close();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    65
	__ASSERT_DEBUG(iLruPageList.IsEmpty(), Panic(EFbsPanicGlyphAtlasInconsistentState));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    66
	
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    67
	// there shouldn't be any remaining pages, but if there are, destroy them.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    68
	while (!iLruPageList.IsEmpty())
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    69
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    70
		delete iLruPageList.First();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    71
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    72
	iSgDriver.Close();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    73
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    74
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    75
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    76
Factory constructor method. Creates a new glyph atlas.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    77
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    78
@param aMaxCacheSizeInBytes The size in bytes, to use as the upper limit
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    79
	for the size of memory used by the glyph images in the atlas. If this
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    80
	value is KGlyphAtlasNoCacheLimit, then there is no limit and the atlas
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    81
	will use as much memory as is available in the system.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    82
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    83
@return A pointer to the newly-constructed atlas
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    84
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    85
@leave KErrNoMemory if there was insufficient memory to create the atlas, 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    86
	or a system wide error code if its RSgDriver failed to open.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    87
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    88
CGlyphAtlas* CGlyphAtlas::NewL(TInt aMaxCacheSizeInBytes)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    89
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    90
	CGlyphAtlas* self = new (ELeave) CGlyphAtlas(aMaxCacheSizeInBytes);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    91
	CleanupStack::PushL(self);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    92
	self->ConstructL();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    93
	CleanupStack::Pop(); // self;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    94
	return self;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    95
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    96
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    97
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    98
Two-phase constructor.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    99
@leave A system wide error code if RSgDriver failed to open.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   100
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   101
void CGlyphAtlas::ConstructL()
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   102
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   103
	User::LeaveIfError(iSgDriver.Open());
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   104
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   105
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   106
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   107
Retrieves a glyph from the atlas.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   108
If the glyph is found, the glyph data passed in is populated.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   109
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   110
@param[in] aFont The font the glyph belongs to.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   111
@param[in] aGlyphCode The glyph code for the glyph being requested.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   112
@param[out] aGlyphImageInfo The glyph image information if this function is successful.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   113
@return KErrNone if the glyph is found, KErrNotFound if not. 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   114
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   115
TInt CGlyphAtlas::GetGlyph(const CBitmapFont& aFont, TUint aGlyphCode, TGlyphImageInfo& aGlyphImageInfo)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   116
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   117
    OstTraceExt2( TRACE_NORMAL, CGLYPHATLAS_GETGLYPH, "> f=%x; gc=%04x",(TUint)&aFont, aGlyphCode);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   118
    
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   119
	CGlyphAtlasFontEntry* fontEntry = FindFontEntry(aFont);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   120
	if (!fontEntry)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   121
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   122
        OstTrace0( TRACE_NORMAL, CGLYPHATLAS_GETGLYPH_END2, "< KErrNotFound");
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   123
		return KErrNotFound;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   124
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   125
	TInt err = fontEntry->GetGlyph(aGlyphCode, aGlyphImageInfo);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   126
	
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   127
	OstTraceExt5( TRACE_NORMAL, CGLYPHATLAS_GETGLYPH_END1, "< id=%08x%08x; w=%u; h=%u; err=%d", 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   128
	        (TUint)I64HIGH(aGlyphImageInfo.iImageId.iId), (TUint)I64LOW(aGlyphImageInfo.iImageId.iId),
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   129
	        (TUint)aGlyphImageInfo.iMetrics.Width(), (TUint)aGlyphImageInfo.iMetrics.Height(), (TInt)err);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   130
	
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   131
	return err;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   132
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   133
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   134
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   135
Adds a glyph to the atlas from a bitmap glyph and retrieves the glyph data.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   136
If there is insufficient memory to create a RSgImage, then the least recently 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   137
used pages (and all the glyphs contained within) are removed until there is 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   138
enough memory to continue.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   139
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   140
@param[in] aFont The font the glyph belongs to.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   141
@param[in] aArgs The information needed to create a glyph.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   142
@param[out] aGlyphImageInfo Upon return contains all the glyph image information needed to use the 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   143
		glyph in another process.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   144
@return KErrNone if the glyph was successfully added or other system-wide error.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   145
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   146
TInt CGlyphAtlas::AddGlyph(const CBitmapFont& aFont, const TAddGlyphArgs& aArgs, TGlyphImageInfo& aGlyphImageInfo)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   147
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   148
	OstTraceDefExt5( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, CGLYPHATLAS_ADDGLYPH, "> f=%x; bp=%08x; gc=%04x; w=%u; h=%u", 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   149
	        (TUint)&aFont, (TUint)aArgs.iBitmapPointer, aArgs.iGlyphCode, 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   150
	        aArgs.iMetrics->Width(), aArgs.iMetrics->Height());
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   151
	
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   152
    // Find font entry and create if none found
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   153
	CGlyphAtlasFontEntry* fontEntry = FindFontEntry(aFont);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   154
	TBool isNewFont = EFalse;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   155
	if (!fontEntry)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   156
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   157
		// Create a new font.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   158
		fontEntry = CreateFontEntry(aFont);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   159
		if (!fontEntry)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   160
			{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   161
            OstTraceDef0(OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, CGLYPHATLAS_ADDGLYPH_END2, "< KErrNoMemory");
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   162
			return KErrNoMemory;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   163
			}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   164
		isNewFont = ETrue;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   165
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   166
	TInt glyphSizeInBytes = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   167
	TInt err = fontEntry->AddGlyph(aArgs, aGlyphImageInfo, glyphSizeInBytes);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   168
	if (KErrNone != err)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   169
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   170
		if (isNewFont)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   171
			{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   172
			DeleteFontEntry(fontEntry);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   173
			}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   174
		OstTraceDef1(OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, CGLYPHATLAS_ADDGLYPH_END3, "< err=%d", err);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   175
		return err;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   176
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   177
	iCacheSizeInBytes += glyphSizeInBytes;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   178
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   179
	// If there is a cache limit and it is now exceeded, remove the least
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   180
	// recently used pages until the cache size is within the upper limit. Do 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   181
	// not remove the page relating to the glyph which is being added, which is 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   182
	// now at the head of the LRU array.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   183
	if (iMaxCacheSizeInBytes != KGlyphAtlasNoCacheLimit)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   184
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   185
		TBool morePagesToDelete = ETrue;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   186
		while ((iCacheSizeInBytes > iMaxCacheSizeInBytes) && morePagesToDelete)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   187
			{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   188
			morePagesToDelete = DeleteLeastRecentlyUsedPage(EFalse);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   189
			}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   190
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   191
	
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   192
	OstTraceDefExt2(OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, CGLYPHATLAS_ADDGLYPH_END1, "< id=%08x%08x", 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   193
	            I64HIGH(aGlyphImageInfo.iImageId.iId), I64LOW(aGlyphImageInfo.iImageId.iId));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   194
    
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   195
	return KErrNone;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   196
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   197
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   198
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   199
Releases all glyphs associated with a particular font when it has been
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   200
released by the font system.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   201
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   202
@param aFont The font which is released.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   203
 */
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   204
void CGlyphAtlas::FontReleased(const CBitmapFont& aFont)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   205
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   206
	OstTraceDef1(OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, CGLYPHATLAS_FONTRELEASED, "> f=%x", (TUint)&aFont);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   207
	
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   208
    TInt index = iFontEntryArray.FindInUnsignedKeyOrder(TFontEntryMap(&aFont, NULL));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   209
	if (KErrNotFound == index)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   210
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   211
        OstTraceDef0(OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, CGLYPHATLAS_FONTRELEASED_END2, "< KErrNotFound");
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   212
		return;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   213
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   214
	CGlyphAtlasFontEntry* fontEntry = iFontEntryArray[index].iEntry;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   215
	iCacheSizeInBytes -= fontEntry->SizeInBytes();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   216
	delete fontEntry;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   217
	iFontEntryArray.Remove(index);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   218
	OstTraceDef0(OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, CGLYPHATLAS_FONTRELEASED_END3, "< KErrNone");
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   219
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   220
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   221
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   222
Searches the array of font entries to find the entry for the given font.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   223
If the font entry is found, the entry is returned. If not a NULL pointer is 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   224
returned.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   225
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   226
@param aFont The font to match an entry with.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   227
@return The font entry if a match is found, NULL if not. 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   228
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   229
CGlyphAtlasFontEntry* CGlyphAtlas::FindFontEntry(const CBitmapFont& aFont) const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   230
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   231
	TFontEntryMap entryToMatch(&aFont, NULL);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   232
	TInt index = iFontEntryArray.FindInUnsignedKeyOrder(entryToMatch);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   233
	if (KErrNotFound == index)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   234
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   235
		return NULL;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   236
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   237
	return iFontEntryArray[index].iEntry;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   238
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   239
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   240
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   241
Deletes the given font entry.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   242
The mapping from the font to the font entry is removed.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   243
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   244
@param aFontEntry The entry to delete.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   245
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   246
void CGlyphAtlas::DeleteFontEntry(CGlyphAtlasFontEntry* aFontEntry)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   247
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   248
    __ASSERT_DEBUG(aFontEntry, Panic(EFbsPanicGlyphAtlasInconsistentState));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   249
    TInt index = iFontEntryArray.FindInUnsignedKeyOrder(TFontEntryMap(&aFontEntry->Font(), NULL));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   250
    __ASSERT_DEBUG(KErrNotFound != index, Panic(EFbsPanicGlyphAtlasInconsistentState));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   251
    if (KErrNotFound != index)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   252
        {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   253
        iFontEntryArray.Remove(index);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   254
        }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   255
    iCacheSizeInBytes -= aFontEntry->SizeInBytes();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   256
    delete aFontEntry;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   257
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   258
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   259
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   260
Moves the given page to the front (the position of the most recently used page) 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   261
of the usage order list.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   262
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   263
@param aPage The most recently used page.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   264
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   265
void CGlyphAtlas::MovePageToFront(CGlyphAtlasPage& aPage)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   266
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   267
	aPage.MoveToFirstInQueue(iLruPageList);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   268
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   269
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   270
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   271
Creates a font entry from the given font and adds a mapping from the font 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   272
to the font entry.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   273
If successful, the font entry is returned.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   274
If either the creation of the font entry or the adding the mapping fails, then 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   275
a NULL pointer is returned.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   276
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   277
@param aFont The font used to create a font entry from.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   278
@return A new font entry if successful, NULL if not.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   279
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   280
CGlyphAtlasFontEntry* CGlyphAtlas::CreateFontEntry(const CBitmapFont& aFont)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   281
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   282
	CGlyphAtlasFontEntry* fontEntry = new CGlyphAtlasFontEntry(aFont, *this);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   283
	if (!fontEntry)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   284
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   285
		return NULL;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   286
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   287
	// Add font entry to font entry array
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   288
	TFontEntryMap fontEntryMap(&aFont, fontEntry);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   289
	TInt err = iFontEntryArray.InsertInUnsignedKeyOrder(fontEntryMap);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   290
	__ASSERT_DEBUG(KErrAlreadyExists != err, Panic(EFbsPanicGlyphAtlasInconsistentState));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   291
	if (KErrNone != err)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   292
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   293
		delete fontEntry;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   294
		fontEntry = NULL;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   295
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   296
	return fontEntry;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   297
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   298
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   299
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   300
Deletes the least recently used page and removes it from the list of pages
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   301
held by the atlas.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   302
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   303
@param aAllowMruPageDeletion ETrue if the most recently used page can be deleted, 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   304
	EFalse otherwise.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   305
@return ETrue, if there are pages remaining in the atlas after the deletion, EFalse
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   306
	otherwise. If there is only one page in the atlas and aAllowMruPageDeletion is EFalse,
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   307
	EFalse is returned. 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   308
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   309
TBool CGlyphAtlas::DeleteLeastRecentlyUsedPage(TBool aAllowMruPageDeletion)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   310
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   311
    OstTraceDef1(OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, CGLYPHATLAS_DELETELEASTRECENTLYUSEDPAGE, "> mru=%d", aAllowMruPageDeletion );
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   312
    
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   313
	CGlyphAtlasPage* lruPage = NULL;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   314
	if (!iLruPageList.IsEmpty())
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   315
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   316
		lruPage = iLruPageList.Last();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   317
		if (!aAllowMruPageDeletion && (lruPage == iLruPageList.First()))
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   318
			{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   319
			lruPage = NULL;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   320
			}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   321
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   322
	TBool canDeleteMorePages = EFalse;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   323
	if (lruPage)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   324
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   325
		iCacheSizeInBytes -= lruPage->SizeInBytes();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   326
		CGlyphAtlasFontEntry& fontEntry = lruPage->FontEntry();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   327
		fontEntry.DeletePage(lruPage);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   328
		lruPage = NULL;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   329
		if (fontEntry.IsEmpty())
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   330
			{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   331
			DeleteFontEntry(&fontEntry);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   332
			}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   333
		canDeleteMorePages = !iLruPageList.IsEmpty();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   334
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   335
	OstTraceDefExt2(OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, CGLYPHATLAS_DELETELEASTRECENTLYUSEDPAGE_END, "< more=%u; size=%u", (TUint)canDeleteMorePages, iCacheSizeInBytes);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   336
	return canDeleteMorePages;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   337
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   338
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   339
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   340
Utility function that calculates the number of unique fonts associated with the atlas.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   341
@return Number of fonts in the atlas.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   342
 */
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   343
TInt CGlyphAtlas::FontCount() const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   344
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   345
	return iFontEntryArray.Count();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   346
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   347
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   348
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   349
Utility function that calculates the number of glyphs across all fonts stored in
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   350
the atlas.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   351
@return Number of glyphs in the atlas.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   352
 */
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   353
TInt CGlyphAtlas::GlyphCount() const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   354
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   355
	TInt glyphCount = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   356
	for (TInt ii = iFontEntryArray.Count() - 1; ii >= 0; --ii)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   357
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   358
		glyphCount += iFontEntryArray[ii].iEntry->GlyphCount();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   359
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   360
	return glyphCount;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   361
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   362
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   363
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   364
Utility function that calculates the number of glyphs for a given font in the atlas.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   365
@param The font to return the number of glyphs for.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   366
@return Number of glyphs in the atlas.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   367
 */
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   368
TInt CGlyphAtlas::GlyphCount(const CBitmapFont& aFont) const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   369
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   370
	CGlyphAtlasFontEntry* fontEntry = FindFontEntry(aFont);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   371
	return (fontEntry) ? fontEntry->GlyphCount() : 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   372
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   373
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   374
void CGlyphAtlas::GetGlyphCacheMetrics( TGlyphCacheMetrics& aGlyphCacheMetrics )
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   375
    {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   376
    aGlyphCacheMetrics.iMaxCacheSizeInBytes = iMaxCacheSizeInBytes;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   377
    aGlyphCacheMetrics.iMaxCacheSizeHigh = iMaxCacheSizeHigh;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   378
    aGlyphCacheMetrics.iMaxCacheSizeLow = iMaxCacheSizeLow;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   379
    aGlyphCacheMetrics.iCacheSizeInBytes = iCacheSizeInBytes;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   380
    aGlyphCacheMetrics.iGpuCacheSizeLimitIsMax = iGpuCacheSizeLimitIsMax;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   381
    }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   382
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   383
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   384
 Function to release the GPU cache. Called in response to the GoomMonitor's
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   385
 requirement to reduce GPU memory use.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   386
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   387
 @param aBytes. The amount of memory the GOoM framework would like us to relinquish.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   388
 @param aFlags. The flags conveyed from the GOoM monitor framework.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   389
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   390
void CGlyphAtlas::ReleaseGpuMemory( TInt /*aBytes*/, TInt /*aFlags*/ )
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   391
    {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   392
    OstTraceDefExt2( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, CGLYPHATLAS_RELEASEGPUMEMORY, "> max=%d; size=%d", iMaxCacheSizeInBytes, iCacheSizeInBytes);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   393
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   394
    if ( iCacheSizeInBytes > 0 )
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   395
        {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   396
        while( DeleteLeastRecentlyUsedPage(ETrue) )
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   397
            {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   398
            // Do nothing
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   399
            }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   400
        }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   401
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   402
    // If appropriate, reduce the cache-size limit.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   403
    if ( GpuCacheSizeLimitIsMax() )
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   404
        {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   405
        SwitchGpuCacheSizeLimit();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   406
        }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   407
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   408
    OstTraceDefExt2( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, CGLYPHATLAS_RELEASEGPUMEMORY_EXIT, "< max=%d; size=%d", iMaxCacheSizeInBytes, iCacheSizeInBytes);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   409
    }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   410
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   411
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   412
 Function to establish GPU memory use. Called in response to the GoomMonitor's
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   413
 notification that GPU memory may once more be utilised in the usual manner.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   414
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   415
 @param aFlags. The flags conveyed from the GOoM monitor framework.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   416
 */
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   417
void CGlyphAtlas::InstateGpuMemory( TInt /*aFlags*/ )
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   418
    {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   419
    OstTraceDef1(OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, CGLYPHATLAS_INSTATEGPUMEMORY, "> max=%d", iMaxCacheSizeInBytes );
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   420
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   421
    // If appropriate, reinstate the full cache-size limit.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   422
    if ( !GpuCacheSizeLimitIsMax() )
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   423
        {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   424
        SwitchGpuCacheSizeLimit();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   425
        }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   426
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   427
    OstTraceDef1(OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, CGLYPHATLAS_INSTATEGPUMEMORY_EXIT, "< max=%d", iMaxCacheSizeInBytes );
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   428
    }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   429
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   430
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   431
 Utility function to toggle between full and reduced GPU cache-size limits.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   432
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   433
 Use in conjunction with TBool GpuCacheSizeLimitIsMax()
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   434
 */
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   435
void CGlyphAtlas::SwitchGpuCacheSizeLimit()
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   436
    {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   437
    if ( GpuCacheSizeLimitIsMax() )
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   438
        {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   439
        // The short-circuit operator obviates tautological conditionals.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   440
        while ( (iCacheSizeInBytes >= iMaxCacheSizeLow) && DeleteLeastRecentlyUsedPage(ETrue) )
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   441
            {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   442
            // Do Nothing...
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   443
            }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   444
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   445
        iMaxCacheSizeInBytes = iMaxCacheSizeLow;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   446
        iGpuCacheSizeLimitIsMax = EFalse;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   447
        }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   448
    else
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   449
        {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   450
        iMaxCacheSizeInBytes = iMaxCacheSizeHigh;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   451
        iGpuCacheSizeLimitIsMax = ETrue;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   452
        }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   453
    }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   454
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   455
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   456
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   457
 Utility function to report whether the GPU cache-size limit is set to the maximum permissible
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   458
 level, or to its reduced level.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   459
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   460
 @return ETrue if the cache-size is set to the maximum permissible limit.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   461
         EFalse if it is set to the reduced limit.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   462
 */
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   463
TBool CGlyphAtlas::GpuCacheSizeLimitIsMax() const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   464
    {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   465
    return iGpuCacheSizeLimitIsMax;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   466
    }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   467
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   468
// === CGlyphAtlasFontEntry Functions ===
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   469
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   470
Font entry constructor.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   471
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   472
@param aFont The font to which this font entry should be associated.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   473
@param aGlyphAtlas The glyph atlas to which this font entry belongs.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   474
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   475
CGlyphAtlasFontEntry::CGlyphAtlasFontEntry(const CBitmapFont& aFont, CGlyphAtlas& aGlyphAtlas)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   476
	:iFont(aFont),
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   477
	 iPageArray(32, _FOFF(TPageMap, iGlyphCode)),
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   478
	 iAtlas(aGlyphAtlas)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   479
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   480
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   481
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   482
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   483
Font entry destructor.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   484
Destroys the pages owned by the font entry.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   485
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   486
CGlyphAtlasFontEntry::~CGlyphAtlasFontEntry()
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   487
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   488
	// cycle through all the font entries and destroy them
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   489
	for (TInt ii = iPageArray.Count()-1; ii >= 0; --ii)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   490
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   491
		DeletePage(iPageArray[ii].iPage);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   492
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   493
	__ASSERT_DEBUG(iSizeInBytes == 0, Panic(EFbsPanicGlyphAtlasInconsistentState));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   494
	iPageArray.Close();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   495
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   496
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   497
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   498
Deletes the given page.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   499
The page mapping is removed and the page is deleted.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   500
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   501
@param aPage The page to delete.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   502
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   503
void CGlyphAtlasFontEntry::DeletePage(CGlyphAtlasPage* aPage)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   504
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   505
    __ASSERT_DEBUG(aPage, Panic(EFbsPanicGlyphAtlasInconsistentState));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   506
    __ASSERT_DEBUG(iPageArray.Count() > 0, Panic(EFbsPanicGlyphAtlasInconsistentState));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   507
		
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   508
    TInt numGlyphsInPage = aPage->GlyphCount();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   509
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   510
    for (TInt ii = 0; ii < numGlyphsInPage; ++ii)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   511
        {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   512
        TInt index = iPageArray.FindInUnsignedKeyOrder(TPageMap(aPage->GlyphCodeAt(ii), NULL));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   513
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   514
        __ASSERT_DEBUG(KErrNotFound != index, Panic(EFbsPanicGlyphAtlasInconsistentState));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   515
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   516
        if (KErrNotFound != index)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   517
            {
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   518
            iPageArray.Remove(index);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   519
            iPageArray.GranularCompress();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   520
            }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   521
        }
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   522
    iSizeInBytes -= aPage->SizeInBytes();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   523
    delete aPage;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   524
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   525
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   526
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   527
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   528
Adds a glyph to the font from a bitmap glyph and gets the glyph image info back.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   529
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   530
@param[in] aArgs The information needed to create a glyph.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   531
@param[out] aGlyphImageInfo Upon return contains all the glyph image information needed to use the 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   532
		glyph in another process.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   533
@param[out] aSizeInBytes Upon return contains the size of the added glyph's image data in bytes.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   534
@return KErrNone if the glyph was successfully added or other system-wide error.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   535
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   536
TInt CGlyphAtlasFontEntry::AddGlyph(const CGlyphAtlas::TAddGlyphArgs& aArgs, TGlyphImageInfo& aGlyphImageInfo, TInt& aSizeInBytes)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   537
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   538
    CGlyphAtlasPage* newPage = new CGlyphAtlasPage(*this);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   539
	if (!newPage)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   540
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   541
		return KErrNoMemory;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   542
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   543
	
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   544
	TInt err = newPage->AddGlyph(aArgs, aGlyphImageInfo, aSizeInBytes);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   545
	if (KErrNone != err)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   546
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   547
		delete newPage;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   548
		return err;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   549
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   550
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   551
	err = iPageArray.InsertInUnsignedKeyOrder(TPageMap(aArgs.iGlyphCode, newPage));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   552
	__ASSERT_DEBUG(KErrAlreadyExists != err, Panic(EFbsPanicGlyphAtlasInconsistentState));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   553
	if (KErrNone != err)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   554
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   555
		delete newPage;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   556
		return err;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   557
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   558
	iSizeInBytes += aSizeInBytes;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   559
	iAtlas.MovePageToFront(*newPage);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   560
	return err;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   561
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   562
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   563
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   564
Searches the array of pages to find the page containing the given glyph.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   565
If the page is found, the glyph image info is populated and the page is moved 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   566
to the front of the Glyph Atlas' LRU page array.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   567
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   568
@param[in] aGlyphCode The glyph code for the glyph being requested.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   569
@param[out] aGlyphImageInfo Upon return contains all the glyph image information needed to use the 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   570
		glyph in another process.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   571
@return KErrNone if the glyph was found, KErrNotFound if not. 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   572
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   573
TInt CGlyphAtlasFontEntry::GetGlyph(TUint aGlyphCode, TGlyphImageInfo& aGlyphImageInfo)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   574
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   575
	TInt index = iPageArray.FindInUnsignedKeyOrder(TPageMap(aGlyphCode, NULL));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   576
	if (KErrNotFound == index)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   577
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   578
		return KErrNotFound;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   579
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   580
	CGlyphAtlasPage* page = iPageArray[index].iPage;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   581
	page->GetGlyph(aGlyphCode, aGlyphImageInfo);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   582
	iAtlas.MovePageToFront(*page);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   583
	return KErrNone;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   584
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   585
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   586
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   587
Gets the font associated with the font entry.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   588
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   589
@return The font associated with this entry. 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   590
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   591
const CBitmapFont& CGlyphAtlasFontEntry::Font() const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   592
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   593
	return iFont;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   594
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   595
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   596
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   597
Gets the amount of memory allocated for all the image data for this font.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   598
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   599
@return The size of the font's image data in bytes.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   600
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   601
TInt CGlyphAtlasFontEntry::SizeInBytes() const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   602
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   603
	return iSizeInBytes;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   604
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   605
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   606
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   607
Tests whether the font entry has any pages.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   608
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   609
@return ETrue if the font entry does not contain any pages, EFalse if it does..
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   610
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   611
TBool CGlyphAtlasFontEntry::IsEmpty() const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   612
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   613
	if (iPageArray.Count() == 0)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   614
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   615
		__ASSERT_DEBUG(iSizeInBytes == 0, Panic(EFbsPanicGlyphAtlasInconsistentState));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   616
		return ETrue;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   617
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   618
	return EFalse;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   619
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   620
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   621
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   622
Gets the glyph atlas the font entry belongs to.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   623
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   624
@return The font entry's glyph atlas.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   625
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   626
CGlyphAtlas& CGlyphAtlasFontEntry::GlyphAtlas() const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   627
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   628
	return iAtlas;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   629
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   630
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   631
/** 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   632
@return The number of glyphs this font entry has.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   633
 */
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   634
TInt CGlyphAtlasFontEntry::GlyphCount() const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   635
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   636
	TInt glyphCount = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   637
	for (TInt ii = iPageArray.Count() - 1; ii >= 0; --ii)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   638
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   639
		glyphCount += iPageArray[ii].iPage->GlyphCount();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   640
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   641
	return glyphCount;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   642
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   643
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   644
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   645
// === CGlyphAtlasPage Functions ===
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   646
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   647
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   648
Page constructor.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   649
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   650
@param aFontEntry The font entry to which the page is associated.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   651
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   652
CGlyphAtlasPage::CGlyphAtlasPage(CGlyphAtlasFontEntry& aFontEntry)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   653
	:iFontEntry(aFontEntry)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   654
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   655
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   656
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   657
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   658
Page destructor.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   659
Releases the RSgImage handles held by the page.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   660
Removes the page from the Glyph Atlas' LRU page array. 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   661
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   662
CGlyphAtlasPage::~CGlyphAtlasPage()
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   663
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   664
	iLink.Deque();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   665
	iGlyphImage.Close();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   666
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   667
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   668
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   669
Adds a glyph to the page from a bitmap glyph and gets the glyph image info back.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   670
An RSgImage handle is acquired for the glyph. 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   671
If there is not enough specialised graphics memory to create a RSgImage, then the
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   672
least recently used pages are deleted until there there is either enough memory 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   673
for the creation to be successful or if there are no more pages to delete (in 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   674
which case an appropriate out of memory error message is returned). 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   675
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   676
@param[in] aArgs The information needed to create a glyph.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   677
@param[out] aGlyphImageInfo Upon return contains all the glyph image information needed to use the 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   678
		glyph in another process.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   679
@param[out] aSizeInBytes Upon return contains the size of the added glyph's image data in bytes.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   680
@return KErrNone if the glyph was successfully added; 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   681
	KErrNoMemory if there is not enough system memory available;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   682
	KErrNoGraphicsMemory if there is not enough specialised graphics memory available.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   683
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   684
TInt CGlyphAtlasPage::AddGlyph(const CGlyphAtlas::TAddGlyphArgs& aArgs, TGlyphImageInfo& aGlyphImageInfo, TInt& aSizeInBytes)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   685
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   686
	const TSize glyphSize(aArgs.iMetrics->Width(), aArgs.iMetrics->Height());
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   687
	// If glyph has zero size (e.g. space), set glyph data and return 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   688
	if (glyphSize.iWidth == 0 || glyphSize.iHeight == 0)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   689
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   690
		iPosX = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   691
		iPosY = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   692
		iMetrics = *aArgs.iMetrics;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   693
		iGlyphCode = aArgs.iGlyphCode;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   694
		iSizeInBytes = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   695
		iNumGlyphs++;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   696
		aGlyphImageInfo.iImageId = KSgNullDrawableId;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   697
		aGlyphImageInfo.iPosX = iPosX;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   698
		aGlyphImageInfo.iPosY = iPosY;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   699
		aGlyphImageInfo.iMetrics = iMetrics;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   700
		return KErrNone;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   701
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   702
	TUint8* buf = NULL;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   703
	TSgImageInfo info;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   704
	info.iSizeInPixels = glyphSize;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   705
	info.iUsage = ESgUsageBitOpenVgImage;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   706
	info.iPixelFormat = EUidPixelFormatA_8;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   707
	TInt dataStride = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   708
	const TInt KDataArraySize = 256;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   709
	TUint8 byteDataArray[KDataArraySize];
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   710
	TUint8* tempBuf = NULL;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   711
	TUint8* byteDataBuf = NULL;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   712
	TGlyphBitmapType glyphBitmapType = iFontEntry.Font().GlyphBitmapType(); 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   713
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   714
	switch (glyphBitmapType)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   715
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   716
		case EMonochromeGlyphBitmap:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   717
			// Decompress to 8bpp buffer
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   718
			dataStride = glyphSize.iWidth;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   719
			byteDataBuf = byteDataArray;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   720
			// If data too big to fit in byteDataArray, allocate memory on the heap
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   721
			if (glyphSize.iHeight * glyphSize.iWidth > KDataArraySize)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   722
				{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   723
				tempBuf = (TUint8*) User::AllocZ(dataStride * glyphSize.iHeight);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   724
				if (!tempBuf)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   725
					{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   726
					return KErrNoMemory;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   727
					}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   728
				byteDataBuf = tempBuf;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   729
				}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   730
			else
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   731
				{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   732
				// fill array with zeros.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   733
				Mem::FillZ(&byteDataArray, KDataArraySize);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   734
				}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   735
			
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   736
			if (glyphSize.iWidth >32)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   737
				{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   738
				DecodeBinaryDataExLarge(glyphSize, aArgs.iBitmapPointer, byteDataBuf);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   739
				}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   740
			else
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   741
				{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   742
				DecodeBinaryData(glyphSize, aArgs.iBitmapPointer, byteDataBuf);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   743
				}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   744
			buf = byteDataBuf;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   745
			break;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   746
		case EAntiAliasedGlyphBitmap:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   747
			buf = const_cast<TUint8*>(aArgs.iBitmapPointer);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   748
			dataStride = glyphSize.iWidth;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   749
			break;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   750
		default:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   751
			return KErrNotSupported;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   752
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   753
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   754
	TInt err = iGlyphImage.Create(info, buf, dataStride);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   755
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   756
	// If RSgImage creation fails due to out of memory, delete the least
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   757
	// recently used pages to free up memory until either creation succeeds or 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   758
	// there are no more pages to remove.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   759
	TBool morePagesToDelete = ETrue;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   760
	while ((KErrNoGraphicsMemory == err || KErrNoMemory == err) && morePagesToDelete)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   761
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   762
		// Delete least used page.  Can delete all pages if necessary as this  
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   763
		// page has not been added to the LRU array yet.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   764
		morePagesToDelete = iFontEntry.GlyphAtlas().DeleteLeastRecentlyUsedPage(ETrue);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   765
		err = iGlyphImage.Create(info, buf, dataStride);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   766
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   767
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   768
	User::Free(tempBuf);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   769
	
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   770
	if (KErrNone != err)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   771
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   772
		return err;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   773
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   774
	aSizeInBytes = glyphSize.iHeight * glyphSize.iWidth;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   775
	iGlyphCode = aArgs.iGlyphCode;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   776
	iPosX = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   777
	iPosY = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   778
	iMetrics = *aArgs.iMetrics;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   779
	// As the image is stored as one byte per pixel, the size in bytes is 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   780
	// just the number of pixels.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   781
	// TODO: Replace estimating size with call to SgImage/SgDriver to get accurate size.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   782
	iSizeInBytes += aSizeInBytes;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   783
	iNumGlyphs++;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   784
	aGlyphImageInfo.iPosX = iPosX;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   785
	aGlyphImageInfo.iPosY = iPosY;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   786
	aGlyphImageInfo.iImageId = iGlyphImage.Id();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   787
	aGlyphImageInfo.iMetrics = iMetrics;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   788
	return err;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   789
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   790
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   791
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   792
Retrieves the glyph image information for the given glyph code necessary to be 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   793
able to use the glyph in another process.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   794
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   795
@param aGlyphCode The glyph code for the glyph being requested
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   796
@param aGlyphImageInfo Upon return contains all the glyph image information needed to use the 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   797
		glyph in another process if the glyph is contained in the page.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   798
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   799
void CGlyphAtlasPage::GetGlyph(TUint aGlyphCode, TGlyphImageInfo& aGlyphImageInfo) const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   800
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   801
	__ASSERT_DEBUG(iGlyphCode == aGlyphCode, Panic(EFbsPanicGlyphAtlasInconsistentState));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   802
	aGlyphImageInfo.iMetrics = iMetrics;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   803
	aGlyphImageInfo.iPosX = iPosX;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   804
	aGlyphImageInfo.iPosY = iPosY;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   805
	aGlyphImageInfo.iImageId = iGlyphImage.Id();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   806
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   807
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   808
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   809
Gets the amount of memory allocated for the image data for this page.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   810
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   811
@return The size of the page's image data in bytes.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   812
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   813
TInt CGlyphAtlasPage::SizeInBytes() const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   814
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   815
	return iSizeInBytes;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   816
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   817
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   818
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   819
Gets the glyph code at the given index associated with the page.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   820
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   821
@param aIndex The index of the glyph code within the page.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   822
@return The glyph code at the given index.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   823
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   824
TUint CGlyphAtlasPage::GlyphCodeAt(TInt aIndex) const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   825
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   826
	__ASSERT_DEBUG(0 == aIndex, Panic(EFbsPanicGlyphAtlasInconsistentState));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   827
	return iGlyphCode;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   828
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   829
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   830
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   831
Gets the number of glyphs stored in the page.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   832
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   833
@return The number of glyphs in the page.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   834
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   835
TInt CGlyphAtlasPage::GlyphCount() const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   836
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   837
	return iNumGlyphs;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   838
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   839
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   840
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   841
Gets the font entry which owns the page.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   842
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   843
@return The font entry which owns the page. 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   844
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   845
CGlyphAtlasFontEntry& CGlyphAtlasPage::FontEntry() const
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   846
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   847
	return iFontEntry;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   848
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   849
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   850
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   851
void CGlyphAtlasPage::MoveToFirstInQueue(TDblQue<CGlyphAtlasPage>& aList)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   852
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   853
	if(!aList.IsFirst(this))
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   854
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   855
		iLink.Deque();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   856
		aList.AddFirst(*this);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   857
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   858
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   859
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   860
// === Static Utility Functions ===
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   861
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   862
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   863
Combines 2 8-bit unsigned integers into a 16-bit integer.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   864
@param aPtr A pointer to a source buffer of 2 8-bit unsigned integers.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   865
@return The two 8-bit integers combined into a 16-bit integer.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   866
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   867
static TInt16 Load16(const TUint8* aPtr)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   868
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   869
	return TInt16(aPtr[0]+(aPtr[1]<<8)); 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   870
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   871
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   872
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   873
Decodes binary data for monochrome glyph bitmap.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   874
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   875
@param aGlyphSize size of glyph in pixels.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   876
@param aEncodedData Pointer to an encoded source buffer.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   877
@param aByteData Pointer to a destination buffer (8 bits per pixel).
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   878
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   879
void DecodeBinaryData(const TSize& aGlyphSize, const TUint8* aEncodedData, TUint8* aByteData)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   880
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   881
	const TInt dataHeight = aGlyphSize.iHeight;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   882
	const TInt dataWidth = aGlyphSize.iWidth;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   883
	TUint32 binaryData = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   884
	
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   885
	// The data is encoded as follows:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   886
	// 1 bit for a multiple lines flag (1=yes)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   887
	// 4 bits for a repeat count which represents:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   888
	// -if the multiple line flag is 0 the number of lines whose data is repeated
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   889
	// -if the flag is 1, the number of lines which differ from line to line.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   890
	// n bits representing the data at 1 bit per pixel, where:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   891
	// -if the multiple line flag is 0, n is the width of the glyph.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   892
	// -if the flag is 1, n is width of glyph multiplied by the repeat count for this block of data.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   893
	// This information presented in continuous packed blocks of:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   894
	// [data][reps][multiLineFlag]
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   895
	TInt bitIndex = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   896
	TInt16 repeatCount = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   897
	TUint8* byteDataPtr = aByteData;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   898
	TUint8* byteDataPtrLimit = NULL;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   899
	for (TInt charLine = 0; charLine < dataHeight; charLine += repeatCount) // for lines in the character...
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   900
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   901
		// Get first 5 bits of block
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   902
		repeatCount = Load16(aEncodedData + (bitIndex >> 3));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   903
		repeatCount >>= bitIndex & 7;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   904
		// strip out multiple line flag (1st bit)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   905
		TInt multiLineFlag = repeatCount & 1;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   906
		// Get repeat count (last 4 bits)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   907
		repeatCount >>= 1;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   908
		repeatCount &= 0xf;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   909
		// move bit index to point to first bit of image data
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   910
		bitIndex += 5;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   911
		// end pointer of destination buffer for this block of data to fill 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   912
		byteDataPtrLimit = aByteData + dataWidth * (charLine + repeatCount);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   913
		if (multiLineFlag)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   914
			{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   915
			while (byteDataPtr < byteDataPtrLimit)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   916
				{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   917
				// Pointer to beginning of data in source buffer for current scanline
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   918
				TInt charDataOffsetPtr = TInt(aEncodedData) + (bitIndex >> 3);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   919
				// Pointer to beginning of current word.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   920
				TUint32* charDataWord = (TUint32*)(charDataOffsetPtr &~ 3);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   921
				// Number of bits to shift in current word to get to beginning of scanline
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   922
				TInt bitShift = bitIndex & 7;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   923
				bitShift += (charDataOffsetPtr & 3) << 3;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   924
				// Copy scanline data into temporary buffer
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   925
				binaryData = (*charDataWord++) >> bitShift;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   926
				// If data crosses a word boundary, get the rest of the data from next word.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   927
				if (bitShift)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   928
					{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   929
					binaryData |= (*charDataWord << (32-bitShift));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   930
					}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   931
				Convert1BppTo8Bpp(binaryData, byteDataPtr, byteDataPtr + dataWidth);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   932
				// Move bit index to beginning of next block
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   933
				bitIndex += dataWidth;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   934
				}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   935
			}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   936
		else
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   937
			{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   938
			TInt charDataOffsetPtr = TInt(aEncodedData) + (bitIndex >> 3);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   939
			TUint32* charDataWord = (TUint32*)(charDataOffsetPtr &~ 3);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   940
			TInt bitShift = bitIndex & 7;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   941
			bitShift += (charDataOffsetPtr & 3) << 3;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   942
			binaryData = (*charDataWord++) >> bitShift;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   943
			if (bitShift)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   944
				{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   945
				binaryData |= (*charDataWord << (32-bitShift));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   946
				}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   947
			TUint8* startByteDataPtr = byteDataPtr;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   948
			Convert1BppTo8Bpp(binaryData, byteDataPtr, byteDataPtr + dataWidth);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   949
			
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   950
			while (byteDataPtr < byteDataPtrLimit)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   951
				{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   952
				Mem::Copy(byteDataPtr, startByteDataPtr, dataWidth);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   953
				byteDataPtr += dataWidth;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   954
				}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   955
			bitIndex += dataWidth;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   956
			}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   957
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   958
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   959
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   960
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   961
Converts binary data in 1 bit per pixel format to 8 bits per pixel format, where
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   962
0 is converted to 0x00 and 1 is converted to 0xFF.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   963
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   964
@param aSrcData Pointer to a 1bpp source buffer.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   965
@param aDestDataPtr Pointer to a 8bpp destination buffer.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   966
@param aDestDataPtrLimit Pointer to the end position in destination buffer to convert to.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   967
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   968
void Convert1BppTo8Bpp(TUint32 aSrcData, TUint8*& aDestDataPtr, const TUint8* aDestDataPtrLimit)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   969
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   970
	for (; aDestDataPtr < aDestDataPtrLimit; ++aDestDataPtr, aSrcData >>= 1)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   971
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   972
		if (aSrcData&1)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   973
			{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   974
			*aDestDataPtr = 0xFF;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   975
			}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   976
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   977
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   978
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   979
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   980
Decodes binary data for extra large monochrome glyph bitmap.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   981
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   982
@param aGlyphSize Size of glyph in pixels.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   983
@param aEncodedData Pointer to an encoded source buffer.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   984
@param aByteData Pointer to a destination buffer (8 bits per pixel).
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   985
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   986
void DecodeBinaryDataExLarge(const TSize& aGlyphSize, const TUint8* aEncodedData, TUint8* aByteData)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   987
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   988
	const TInt dataWidth = aGlyphSize.iWidth;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   989
	const TInt dataHeight = aGlyphSize.iHeight;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   990
	TInt bitIndex = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   991
	TInt16 repeatCount = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   992
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   993
	for (TInt charLine = 0; charLine < dataHeight; charLine += repeatCount) // for lines in the character...
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   994
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   995
		repeatCount = Load16(aEncodedData + (bitIndex >> 3));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   996
		repeatCount >>= bitIndex & 7;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   997
		const TInt multiLineFlag = repeatCount & 1;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   998
		repeatCount >>= 1;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   999
		repeatCount &= 0xf;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1000
		bitIndex += 5;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1001
		if (multiLineFlag)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1002
			{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1003
			for (TInt currentline = 0; currentline < repeatCount; currentline++)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1004
				{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1005
				CopyCharLine(aByteData, dataWidth, aEncodedData + (bitIndex >> 3), bitIndex & 7, 1);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1006
				bitIndex += dataWidth;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1007
				}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1008
			}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1009
		else
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1010
			{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1011
			CopyCharLine(aByteData, dataWidth, aEncodedData + (bitIndex >> 3), bitIndex & 7, repeatCount);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1012
			bitIndex += dataWidth;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1013
			}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1014
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1015
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1016
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1017
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1018
Copies glyph image data line(s)(1 bit per pixel) to an 8 bit per pixel
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1019
destination buffer.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1020
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1021
@param aByteDataPtr Pointer to a destination buffer (8bpp).
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1022
@param aWidthInBytes Stride of the image.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1023
@param aSrcData Pointer to a source buffer (1bpp).
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1024
@param aBitShift Number of bits the source data pointer will be shifted. 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1025
@param aRepeatCount Number of lines to copy.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1026
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1027
void CopyCharLine(TUint8*& aByteDataPtr, TInt aWidthInBytes, const TUint8* aSrcData, TInt aBitShift, TInt16 aRepeatCount)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1028
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1029
	aBitShift &= 7;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1030
	TUint8* ptrLimit = aByteDataPtr + aWidthInBytes;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1031
	TUint32* dataWord = (TUint32*)(TInt(aSrcData) &~ 3);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1032
	aBitShift += (TInt(aSrcData) - TInt(dataWord)) << 3;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1033
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1034
	TUint8* startByteDataPtr = aByteDataPtr;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1035
	TUint32 binaryData = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1036
	while (aByteDataPtr < ptrLimit)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1037
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1038
		binaryData = *dataWord++;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1039
		binaryData >>= aBitShift;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1040
		if (aBitShift)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1041
			{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1042
			binaryData |= (*dataWord << (32-aBitShift));
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1043
			}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1044
		TUint8* wordLimit = aByteDataPtr + 32;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1045
		if (wordLimit > ptrLimit)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1046
			{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1047
			wordLimit = ptrLimit;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1048
			}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1049
		Convert1BppTo8Bpp(binaryData, aByteDataPtr, wordLimit);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1050
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1051
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1052
	while (aRepeatCount > 1)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1053
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1054
		Mem::Copy(aByteDataPtr, startByteDataPtr, aWidthInBytes);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1055
		aByteDataPtr += aWidthInBytes;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1056
		--aRepeatCount;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1057
		}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1058
	}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1059
d72fc2aace31 201027_1
hgs
parents:
diff changeset
  1060