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