fbs/fontandbitmapserver/sfbs/glyphatlas.h
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
// Font & Bitmap Server Glyph Atlas
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    15
//
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    16
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    17
#ifndef GLYPHATLAS_H
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    18
#define GLYPHATLAS_H
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    19
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    20
#include <e32base.h>
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    21
#include <sgresource/sgimage.h>
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    22
#include <fbs.h>
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    23
#include "UTILS.H"
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    24
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    25
class CGlyphAtlasFontEntry;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    26
class CGlyphAtlasPage;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    27
class TGlyphCacheMetrics;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    28
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    29
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    30
The Glyph Atlas manages the storing and retrieval of glyphs stored in GPU 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    31
memory as RSgImages.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    32
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    33
CGlyphAtlas is the main class which processes requests to add and retrieve
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    34
glyphs. Only one instance of this class will exist throughout the lifetime of 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    35
the Font and Bitmap Server.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    36
@internalComponent
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    37
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    38
NONSHARABLE_CLASS(CGlyphAtlas) : public CBase
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    39
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    40
friend class CGlyphAtlasTestWrapper;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    41
public:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    42
	/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    43
	Structure used to pass information required to create a new glyph so that it can 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    44
	be added to the glyph atlas.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    45
	@internalComponent 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    46
	*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    47
	class TAddGlyphArgs
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    48
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    49
	public:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    50
		inline TAddGlyphArgs(const TUint8* aBitmapPointer, TUint aGlyphCode, const TOpenFontCharMetrics& aMetrics)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    51
			: iBitmapPointer(aBitmapPointer), iGlyphCode(aGlyphCode), iMetrics(&aMetrics) {}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    52
	public:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    53
		const TUint8* iBitmapPointer;	/**< The address of the bitmap glyph. No ownership.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    54
		TUint iGlyphCode;	/**< The glyph code for the glyph being added. No ownership.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    55
		const TOpenFontCharMetrics* iMetrics;	/**< The metrics for the glyph being added. No ownership.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    56
		};
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    57
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    58
public:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    59
	~CGlyphAtlas();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    60
	static CGlyphAtlas* NewL(TInt aMaxCacheSizeInBytes);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    61
	TInt GetGlyph(const CBitmapFont& aFont, TUint aGlyphCode, TGlyphImageInfo& aGlyphImageInfo);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    62
	TInt AddGlyph(const CBitmapFont& aFont, const TAddGlyphArgs& aArgs, TGlyphImageInfo& aGlyphImageInfo);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    63
	void FontReleased(const CBitmapFont& aFont);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    64
	void MovePageToFront(CGlyphAtlasPage& aPage);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    65
	TBool DeleteLeastRecentlyUsedPage(TBool aAllowMruPageDeletion);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    66
	TInt FontCount() const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    67
	TInt GlyphCount() const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    68
	TInt GlyphCount(const CBitmapFont& aFont) const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    69
	void GlyphCacheMetrics(TInt& aSizeInBytes, TInt& aMaxSizeInBytes, TBool& aGpuCacheSizeLimitIsMax);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    70
	void ReleaseGpuMemory( TInt /*aBytes*/, TInt /*aFlags*/ );
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    71
	void InstateGpuMemory( TInt /*aFlags*/ );
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    72
	void GetGlyphCacheMetrics( TGlyphCacheMetrics& aGlyphCacheMetrics );
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    73
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    74
private:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    75
	CGlyphAtlas(TInt aMaxCacheSizeInBytes);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    76
	void ConstructL();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    77
	CGlyphAtlasFontEntry* CreateFontEntry(const CBitmapFont& aFont);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    78
	CGlyphAtlasFontEntry* FindFontEntry(const CBitmapFont& aFont) const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    79
	void DeleteFontEntry(CGlyphAtlasFontEntry* aFontEntry);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    80
    void SwitchGpuCacheSizeLimit();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    81
    TBool GpuCacheSizeLimitIsMax() const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    82
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    83
private:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    84
	class TFontEntryMap
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    85
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    86
	public:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    87
		inline TFontEntryMap(const CBitmapFont* aFont, CGlyphAtlasFontEntry* aEntry)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    88
				: iFont(aFont),iEntry(aEntry) {}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    89
	public:			
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    90
		const CBitmapFont* iFont;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    91
		CGlyphAtlasFontEntry* iEntry;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    92
		};
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    93
private:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    94
	RSgDriver iSgDriver;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    95
	TDblQue<CGlyphAtlasPage> iLruPageList; /**< Least-recently used ordered queue (most-recently used at head).*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    96
	RArray<TFontEntryMap> iFontEntryArray; /**< Array of font entries maintained in unsigned ordered by font pointer.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    97
	TUint iMaxCacheSizeInBytes;	/**< Maximum specialised graphics memory the cache should use. If zero, there is no limit.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    98
	TInt iMaxCacheSizeHigh;                /**< The high cach-size threshold.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
    99
	TInt iMaxCacheSizeLow;                 /**< The low cach-size threshold.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   100
	TUint iCacheSizeInBytes; 	/**< Actual amount of specialised graphics memory used by the entire atlas.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   101
	TBool iGpuCacheSizeLimitIsMax;         /**< Is the cache-size limit set to it's maximum value? */
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   102
	};
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   103
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   104
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   105
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   106
Each font entry manages the storing and retrieval of glyphs belonging to a font.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   107
@internalComponent
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   108
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   109
NONSHARABLE_CLASS(CGlyphAtlasFontEntry): public CBase
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   110
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   111
public:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   112
	CGlyphAtlasFontEntry(const CBitmapFont& aFont, CGlyphAtlas& aGlyphAtlas);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   113
	~CGlyphAtlasFontEntry();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   114
	const CBitmapFont& Font() const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   115
	void DeletePage(CGlyphAtlasPage* aPage);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   116
	TInt GetGlyph(TUint aGlyphCode, TGlyphImageInfo& aGlyphImageInfo);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   117
	TInt AddGlyph(const CGlyphAtlas::TAddGlyphArgs& aArgs, TGlyphImageInfo& aGlyphImageInfo, TInt& aSizeInBytes);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   118
	TInt SizeInBytes() const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   119
	TBool IsEmpty() const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   120
	CGlyphAtlas& GlyphAtlas() const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   121
	TInt GlyphCount() const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   122
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   123
private:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   124
	// Class that handles association between glyph code and page the 
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   125
	// image that glyph code resides on. Used in pointer arrays.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   126
	class TPageMap
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   127
		{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   128
	public:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   129
		inline TPageMap(TUint aGlyphCode, CGlyphAtlasPage* aPage)
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   130
				: iGlyphCode(aGlyphCode),iPage(aPage) {}
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   131
	public:			
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   132
		TUint iGlyphCode;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   133
		CGlyphAtlasPage* iPage;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   134
		};
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   135
private:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   136
	const CBitmapFont& iFont;	/**< The font which the stored glyph belongs to.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   137
	RArray<TPageMap> iPageArray;	/**< Array of pages maintained in unsigned ordered by glyph code.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   138
	CGlyphAtlas& iAtlas;		/**< The glyph atlas the font entry belongs to.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   139
	TInt iSizeInBytes;			/**< The amount of specialised graphics memory used by all owned pages.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   140
	};
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   141
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   142
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   143
/**
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   144
Each page in the glyph atlas stores one glyph in one RSgImage.
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   145
@internalComponent
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   146
*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   147
NONSHARABLE_CLASS(CGlyphAtlasPage): public CBase
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   148
	{
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   149
friend CGlyphAtlas::CGlyphAtlas(TInt);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   150
public:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   151
	CGlyphAtlasPage(CGlyphAtlasFontEntry& aFontEntry);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   152
	~CGlyphAtlasPage();
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   153
	TInt AddGlyph(const CGlyphAtlas::TAddGlyphArgs& aArgs, TGlyphImageInfo& aGlyphImageInfo, TInt& aSizeInBytes);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   154
	void GetGlyph(TUint aGlyphCode, TGlyphImageInfo& aGlyphImageInfo) const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   155
	TUint GlyphCodeAt(TInt aIndex) const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   156
	TInt GlyphCount() const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   157
	CGlyphAtlasFontEntry& FontEntry() const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   158
	TInt SizeInBytes() const;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   159
	void MoveToFirstInQueue(TDblQue<CGlyphAtlasPage>& aList);
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   160
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   161
private:
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   162
	TDblQueLink iLink;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   163
	// Data for the stored glyph
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   164
	CGlyphAtlasFontEntry& iFontEntry;	/**< The font entry asssociated with the page.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   165
	RSgImage iGlyphImage;		/**< The image which contains the stored glyph.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   166
	TInt16 iPosX;				/**< The x-coord of the position of the stored glyph within the image.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   167
	TInt16 iPosY;				/**< The y-coord of the position of the stored glyph within the image.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   168
	TOpenFontCharMetrics iMetrics;	/**< The character metrics for the stored glyph.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   169
	TUint iGlyphCode;			/**< The glyph code for the stored glyph.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   170
	TInt iSizeInBytes;			/**< The amount of specialised graphics memory used by this page.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   171
	TInt iNumGlyphs;			/**< The number of glyphs contained in the page.*/
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   172
	};
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   173
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   174
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   175
// If no limit to the size of the glyph atlas cache is desired, this
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   176
// constant must be passed to CGlyphCache::NewL()
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   177
// In this case the cache lower maximum limit, deployed in response to GooM,
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   178
// will be set to KGlyphAtlasLowMemCacheLimitDefault
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   179
const TInt KGlyphAtlasNoCacheLimit = 0;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   180
const TInt KGlyphAtlasLowMemCacheLimitDivisor = 2;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   181
const TInt KGlyphAtlasLowMemCacheLimitDefault = 0x40000;
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   182
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   183
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   184
#endif // GLYPHATLAS_H
d72fc2aace31 201027_1
hgs
parents:
diff changeset
   185