uiacceltk/hitchcock/coretoolkit/inc/huicanvastexturecache.h
changeset 0 15bf7259bb7c
child 6 10534483575f
equal deleted inserted replaced
-1:000000000000 0:15bf7259bb7c
       
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #ifndef __HUICANVASTEXTURECACHE_H__
       
    21 #define __HUICANVASTEXTURECACHE_H__
       
    22 
       
    23 
       
    24 #include <e32base.h>
       
    25 #include <uiacceltk/HuiCanvasCmdBufferReader.h>
       
    26 #include <uiacceltk/HuiImage.h>
       
    27 #include <uiacceltk/HuiTexture.h>
       
    28 #include <uiacceltk/HuiCanvasVisual.h>
       
    29 #include <uiacceltk/huilowmemoryobserver.h>
       
    30 
       
    31 class CHuiTexture;
       
    32 class CHuiCanvasVisual;
       
    33 class CHuiCanvasRenderBuffer;
       
    34 class CHuiCanvasTextImage;
       
    35 class CHuiCanvasGraphicImage;
       
    36 class CHuiCanvasRenderBufferImage;
       
    37 class CHuiCanvasTextImageRasterizer;
       
    38 class CHuiCanvasGraphicImageRasterizer;
       
    39 class CHuiCanvasCombinedGraphicImageRasterizer;
       
    40 class CHuiCanvasTextureCache;
       
    41 
       
    42 NONSHARABLE_CLASS( THuiCachedGcParams )  
       
    43     {
       
    44 public:
       
    45     THuiCachedGcParams() :    
       
    46         iPenColor(KHuiWsDefaultPenColor),
       
    47         iPenStyle(KHuiWsDefaultPenStyle),
       
    48         iDrawMode(KHuiWsDefaultDrawMode),
       
    49         iBrushColor(KHuiWsDefaultBrushColor),
       
    50         iBrushStyle(KHuiWsDefaultBrushStyle),
       
    51         iShadowMode(KHuiWsDefaultShadowMode),
       
    52         iStrikethrough(KHuiWsDefaultStrikethrough),
       
    53         iUnderline(KHuiWsDefaultUnderline),
       
    54         iUserDisplayMode(KHuiWsDefaultUserDisplayMode),
       
    55         iShadowColor(KHuiWsDefaultShadowColor)
       
    56             {
       
    57             }
       
    58 
       
    59     TRgb  iPenColor;
       
    60     TInt  iPenStyle;
       
    61     TInt  iDrawMode;
       
    62     TRgb  iBrushColor;
       
    63     TInt  iBrushStyle;
       
    64     TInt  iShadowMode;
       
    65     TInt  iStrikethrough;
       
    66     TInt  iUnderline;
       
    67     TInt  iUserDisplayMode;
       
    68     TRgb  iShadowColor;        
       
    69     };
       
    70 
       
    71 
       
    72 NONSHARABLE_CLASS( THuiCachedImageParams )  
       
    73     {
       
    74 public:
       
    75     THuiCachedImageParams() :    
       
    76         iBitmapHandle(0),
       
    77         iMaskHandle(0),
       
    78         iInvertedMask(EFalse),
       
    79         iMaskOriginPoint(TPoint(0,0))
       
    80             {
       
    81             }
       
    82 
       
    83     TInt iBitmapHandle;
       
    84     TInt iMaskHandle;
       
    85     TBool iInvertedMask;
       
    86     TPoint iMaskOriginPoint;
       
    87     THuiCachedGcParams iGcParams;
       
    88     };
       
    89 
       
    90 NONSHARABLE_CLASS( THuiCachedCombinedImageParams )  
       
    91     {
       
    92 public:
       
    93     THuiCachedCombinedImageParams() :    
       
    94         iBitmapHandle(0),
       
    95         iMaskHandle(0),
       
    96         iCombinedBitmapPoint(TPoint(0,0))
       
    97             {
       
    98             }
       
    99 
       
   100     TInt iBitmapHandle;
       
   101     TInt iMaskHandle;
       
   102     TPoint iCombinedBitmapPoint;
       
   103     };
       
   104 
       
   105 NONSHARABLE_CLASS( THuiCachedTextParams )
       
   106     {      
       
   107 public:
       
   108     THuiCachedTextParams() :
       
   109         iText(NULL),
       
   110         iFontHandle(0),
       
   111         iRasterizationOffset(TPoint(0,0)),
       
   112         iTextParams(THuiCanvasTextParameters()),
       
   113         iTextBoxMaxSize(TSize(KMinTInt,KMinTInt)),
       
   114         iTextWidth(KMinTInt),
       
   115         iBaseLineOffset(KMinTInt),
       
   116         iTextAlign(CGraphicsContext::ELeft),
       
   117         iMargin(0),
       
   118         iAngle(0)
       
   119             {
       
   120             }
       
   121     
       
   122     HBufC* iText;
       
   123     TPtr* iFindTextPtr;
       
   124     TInt iFontHandle;
       
   125     TPoint iRasterizationOffset;
       
   126     THuiCanvasTextParameters iTextParams;
       
   127     TSize iTextBoxMaxSize;        
       
   128     TInt iTextWidth;
       
   129     TInt iBaseLineOffset;
       
   130     TInt iTextAlign;
       
   131     TInt iMargin;
       
   132     TInt iAngle;
       
   133     THuiCachedGcParams iGcParams;
       
   134     };
       
   135 
       
   136 NONSHARABLE_CLASS( THuiCachedBitmap )  
       
   137     {
       
   138 public:
       
   139     THuiCachedBitmap() :    
       
   140         iBitmapHandle(0),
       
   141         iMaskHandle(0),
       
   142         iBitmap(0),
       
   143         iMask(0),
       
   144         iBitmapTouchCount(-1),
       
   145         iMaskTouchCount(-1),
       
   146         iInvertedMask(EFalse),
       
   147         iMaskOriginPoint(TPoint(0,0)),
       
   148         iCombinedBitmapPoint(TPoint(0,0))       
       
   149             {
       
   150             }
       
   151 
       
   152     TInt iBitmapHandle;
       
   153     TInt iMaskHandle;
       
   154     CFbsBitmap* iBitmap;
       
   155     CFbsBitmap* iMask;    
       
   156     TInt iBitmapTouchCount;
       
   157     TInt iMaskTouchCount;
       
   158     TBool iInvertedMask;
       
   159     TPoint iMaskOriginPoint;
       
   160     TPoint iCombinedBitmapPoint;
       
   161     };
       
   162 
       
   163 
       
   164 NONSHARABLE_CLASS( CHuiCanvasImage ) : public CBase
       
   165     {
       
   166 public:
       
   167     
       
   168     CHuiCanvasImage();
       
   169 
       
   170     ~CHuiCanvasImage();
       
   171 
       
   172     void RefreshUser(const CHuiCanvasVisual& aUser);
       
   173 
       
   174     void RemoveUser(const CHuiCanvasVisual& aUser);
       
   175 
       
   176     TBool IsUser(const CHuiCanvasVisual& aUser) const;
       
   177 
       
   178     TBool IsAnyUser() const;
       
   179         
       
   180     CHuiTexture* Texture() const;
       
   181         
       
   182     virtual void CopyAttributes(CHuiCanvasImage& aSrc);
       
   183 
       
   184     virtual void Reset();
       
   185 
       
   186 public:
       
   187 
       
   188     CHuiTexture* iTexture;
       
   189     THuiCachedGcParams iGcParams;
       
   190     RPointerArray<CHuiCanvasVisual> iActiveUsers;
       
   191     TTime iLastUsed;
       
   192     CHuiCanvasTextureCache* iCache;
       
   193     };
       
   194 
       
   195 
       
   196 NONSHARABLE_CLASS( CHuiCanvasGraphicImage ) : public CHuiCanvasImage
       
   197     {
       
   198 public:
       
   199     
       
   200     CHuiCanvasGraphicImage();
       
   201 
       
   202     ~CHuiCanvasGraphicImage();
       
   203 
       
   204     virtual void CopyAttributes(CHuiCanvasGraphicImage& aSrc);
       
   205 
       
   206     virtual void Reset();
       
   207 
       
   208 public:
       
   209 
       
   210     TInt iBitmapHandle;
       
   211     TInt iMaskHandle;
       
   212     CFbsBitmap* iBitmap;
       
   213     CFbsBitmap* iMask;    
       
   214     TBool iInvertedMask;
       
   215     TPoint iMaskOriginPoint;
       
   216     TInt iBitmapTouchCount;        
       
   217     TInt iMaskTouchCount;
       
   218     
       
   219     // Combined image
       
   220     RArray<THuiCachedBitmap> iSubImages;
       
   221     TSize iImageSize; 
       
   222     };
       
   223 
       
   224 
       
   225 NONSHARABLE_CLASS( CHuiCanvasTextImage ) : public CHuiCanvasImage
       
   226     {
       
   227 public:
       
   228     
       
   229     CHuiCanvasTextImage();
       
   230 
       
   231     ~CHuiCanvasTextImage();
       
   232 
       
   233     TBool UseColorModulation() const;
       
   234     
       
   235     virtual void CopyAttributes(CHuiCanvasTextImage& aSrc);
       
   236 
       
   237     virtual void Reset();
       
   238 
       
   239 public:
       
   240 
       
   241     HBufC* iText;
       
   242     TPtr* iFindTextPtr;
       
   243     TInt iFontHandle;
       
   244     TPoint iRasterizationOffset;
       
   245     THuiCanvasTextParameters iTextParams;
       
   246     TSize iTextBoxMaxSize;        
       
   247     TInt iTextWidth;
       
   248     TInt iBaseLineOffset;
       
   249     TInt iTextAlign;
       
   250     TInt iMargin;
       
   251     TInt iAngle;
       
   252     TBool iUseColorModulation;
       
   253     // not owned
       
   254     CFbsBitGcFont* iFont;
       
   255     };
       
   256 
       
   257 
       
   258 class CHuiCanvasTextureCache : public CBase, public MHuiLowMemoryObserver, public MHuiMemoryLevelObserver
       
   259     {
       
   260 public:
       
   261 
       
   262 	/**
       
   263 	 * Constructor.
       
   264 	 */
       
   265 	CHuiCanvasTextureCache();
       
   266 
       
   267 
       
   268 	/**
       
   269 	 * Destructor.
       
   270 	 */ 
       
   271 	~CHuiCanvasTextureCache();
       
   272 
       
   273 	/**
       
   274 	 * Constructor.
       
   275 	 */
       
   276 	static CHuiCanvasTextureCache* NewL();
       
   277 
       
   278 	/**
       
   279 	 * Constructor.
       
   280 	 */
       
   281     static CHuiCanvasTextureCache* NewLC();
       
   282     
       
   283     /**
       
   284      * Second-phase constructor.
       
   285      */
       
   286     void ConstructL();
       
   287 
       
   288     /**
       
   289      * Enables (disables) low memory state.
       
   290      * Deprecated
       
   291      */
       
   292     void EnableLowMemoryState(TBool aEnable);
       
   293 
       
   294     /**
       
   295      * Sets memory level. Alternative for EnableLowMemoryState().
       
   296      */
       
   297     void SetMemoryLevel(THuiMemoryLevel aLevel);
       
   298 
       
   299     /**
       
   300      * Gets memory level.
       
   301      */
       
   302     THuiMemoryLevel MemoryLevel();
       
   303     
       
   304 	/**
       
   305 	 * Finds cached combined image and updates it if needed. If not found creates new one into cache.
       
   306 	 */ 
       
   307     const CHuiCanvasGraphicImage* CreateCombinedCachedImageL(const RArray<THuiCachedCombinedImageParams>& aCachedImageParams, 
       
   308         const THuiCachedGcParams& aGcParams,
       
   309         const TSize& aImageSize, 
       
   310         const CHuiCanvasVisual& aUser);
       
   311 
       
   312 	/**
       
   313 	 * Finds cached image and updates it if needed. If not found creates new one into cache.
       
   314 	 */ 
       
   315     const CHuiCanvasGraphicImage* CreateCachedImageL(const THuiCachedImageParams& aCachedImageParams, const CHuiCanvasVisual& aUser);
       
   316 
       
   317 	/**
       
   318 	 * Finds cached image, but does not update it. If not found, leave happens.
       
   319 	 */ 
       
   320     const CHuiCanvasGraphicImage* FindCachedImageL(const THuiCachedImageParams& aCachedImageParams, const CHuiCanvasVisual& aUser);
       
   321 
       
   322 	/**
       
   323 	 * Finds cached text and updates it if needed. If not found creates new one into cache.
       
   324 	 */ 
       
   325     const CHuiCanvasTextImage* CreateCachedTextL(const THuiCachedTextParams& aCachedTextParams, const CHuiCanvasVisual& aUser);
       
   326     
       
   327     /**
       
   328      * Finds cached render buffer, if not found it is created. Ownership is not transferred to the caller.  
       
   329      * 
       
   330      * If current renderer does not support canvas render buffers, NULL is returned. 
       
   331      * In unexpected error situations leave happens. 
       
   332      */ 
       
   333     CHuiCanvasRenderBuffer* CreateCachedRenderBufferL(const CHuiCanvasVisual& aUser, TSize aSizeHint = TSize(0,0));
       
   334 
       
   335     /**
       
   336      * Finds cached render buffer. Ownership is not transferred to the caller.  
       
   337      */ 
       
   338     CHuiCanvasRenderBuffer* FindCachedRenderBuffer(const CHuiCanvasVisual& aUser);
       
   339 
       
   340     /**
       
   341      * Releases render buffer of the given user. Render buffer is not
       
   342      * neccessarely deleted immediately, thus it may continue to be available in the cache.
       
   343      */ 
       
   344     void ReleaseCachedRenderBuffer(const CHuiCanvasVisual& aUser);
       
   345     
       
   346 	/**
       
   347 	 * This method releases all images and texts that are used by the given user. Images are not
       
   348 	 * neccessarely deleted immediately,thus they may continue to be available in the cache.
       
   349 	 */ 
       
   350     void ReleaseAllCachedEntries(const CHuiCanvasVisual& aUser);
       
   351 
       
   352 	/**
       
   353 	 * Called by the env when frame has been completed. 
       
   354 	 */ 
       
   355     void AdvanceTime();
       
   356 
       
   357 	/**
       
   358 	 * Called when texture memory has run out. This method tries to release more texture memory.
       
   359 	 */ 
       
   360     void HandleOutOfTextureMemory();
       
   361 
       
   362 	/**
       
   363 	 * Utility to create new (or reuse recylced) texture.
       
   364 	 */ 
       
   365     CHuiTexture* CreateRecycledTextureL();
       
   366 
       
   367 	/**
       
   368 	 * Deletes (or moves to recycling) given texture.
       
   369 	 */ 
       
   370     void DeleteRecycledTexture(CHuiTexture* aTexture);
       
   371 
       
   372     void EnableTouchCountCheck(TBool aEnable = ETrue);
       
   373     TBool IsTouchCountCheckEnabled();
       
   374 
       
   375 private:
       
   376     
       
   377     void DeleteAllReleasedEntries(TBool aAllowKeepCached);        
       
   378     
       
   379     void FindUnusedImageEntries(RPointerArray<CHuiCanvasGraphicImage>& aIndexEntries);    
       
   380     void FindUnusedTextEntries(RPointerArray<CHuiCanvasTextImage>& aIndexEntries);
       
   381     void FindUnusedRenderBufferEntries(RPointerArray<CHuiCanvasRenderBufferImage>& aIndexEntries);
       
   382     
       
   383     void SelectPreservedUnusedImageEntries(RPointerArray<CHuiCanvasGraphicImage>& aIndexEntries);    
       
   384     void SelectPreservedUnusedTextEntries(RPointerArray<CHuiCanvasTextImage>& aIndexEntries);
       
   385     void SelectPreservedUnusedRenderBufferEntries(RPointerArray<CHuiCanvasRenderBufferImage>& aIndexEntries);
       
   386     
       
   387     void DeleteUnusedImageEntries(RPointerArray<CHuiCanvasGraphicImage>& aIndexEntries);
       
   388     void DeleteUnusedTextEntries(RPointerArray<CHuiCanvasTextImage>& aIndexEntries);
       
   389     void DeleteUnusedRenderBufferEntries(RPointerArray<CHuiCanvasRenderBufferImage>& aIndexEntries);
       
   390     
       
   391     void CalculateGraphicsMemoryUsage();    
       
   392 
       
   393     TInt CalculateUnusedCanvasTextureUsageInKbytes();
       
   394     TInt CalculateUnusedCanvasRenderBufferUsageInKbytes();
       
   395 
       
   396     TInt CalculateTotalCanvasRenderBufferUsageInKbytes();
       
   397     
       
   398     CHuiCanvasRenderBufferImage* ReUseReleasedRenderBuffer(TSize aSizehint);
       
   399     
       
   400     TBool IsLowMemoryStateEnabled() const;
       
   401     
       
   402 private:
       
   403 
       
   404     /** Temp entry for doing quick search op */
       
   405     CHuiCanvasTextImage* iSearchedTextImageEntry;
       
   406 
       
   407     /** Temp entry for doing quick search op */
       
   408     CHuiCanvasGraphicImage* iSearchedGraphicImageEntry;
       
   409 
       
   410     /** Temp entry for doing quick search op */
       
   411     CHuiCanvasRenderBufferImage* iSearchedRenderBufferImageEntry;
       
   412     
       
   413     /** Cached images */
       
   414     RPointerArray<CHuiCanvasGraphicImage> iCachedImages;
       
   415 
       
   416     /** Cached texts */
       
   417     RPointerArray<CHuiCanvasTextImage>  iCachedTexts;
       
   418 
       
   419     /** Cached render buffers */
       
   420     RPointerArray<CHuiCanvasRenderBufferImage>  iCachedRenderBuffers;
       
   421     
       
   422     /** Flag to tell that there are unused images which should be possibly deleted */
       
   423     TBool iHasReleasedImages;
       
   424 
       
   425     /** Flag to tell that there are unused texts which should be possibly deleted */
       
   426     TBool iHasReleasedTexts;
       
   427 
       
   428     /** Flag to tell that there are unused render buffers which should be possibly deleted */
       
   429     TBool iHasReleasedRenderBuffers;
       
   430     
       
   431     /** Amount of texture memory reserved for unused texts */
       
   432     TInt iUnusedCanvasTextTextureCacheSizeInKBytes;
       
   433     
       
   434     /** Amount of texture memory reserved for unused images */
       
   435     TInt iUnusedCanvasImageTextureCacheSizeInKBytes;
       
   436 
       
   437     /** Amount of texture memory reserved for unused render buffers */
       
   438     TInt iUnusedCanvasRenderBufferCacheSizeInKBytes;
       
   439     
       
   440     /** Total amount of all hui textures are attempted (but not promised) to keep below this limit */
       
   441     TInt iMaxTextureMemoryInKBytes;
       
   442     
       
   443     /** Total amount of all render buffers are attempted (but not promised) to keep below this limit */
       
   444     TInt iMaxRenderBufferMemoryInKBytes;
       
   445 
       
   446     /** Texture recycling array */
       
   447     RPointerArray<CHuiTexture> iRecycledTextures;
       
   448 
       
   449     /** Rasterizer for texts */
       
   450     CHuiCanvasTextImageRasterizer* iTextImageRasterizer;
       
   451 
       
   452     /** Rasterizer for outline texts */
       
   453     CHuiCanvasTextImageRasterizer* iOutLineTextImageRasterizer;
       
   454 
       
   455     /** Rasterizer for image */
       
   456     CHuiCanvasGraphicImageRasterizer* iGraphicImageRasterizer;
       
   457 
       
   458     /** Rasterizer for combined image */
       
   459     CHuiCanvasCombinedGraphicImageRasterizer* iCombinedGraphicImageRasterizer;
       
   460 
       
   461     /** Canvas gc instance for internal use */
       
   462     CHuiCanvasGc* iCanvasGc;
       
   463     
       
   464     /** This controls the amount of cached textures, buffers, etc. */
       
   465     THuiMemoryLevel iMemoryLevel;
       
   466     
       
   467     /** RnD utils */
       
   468     TInt iEglProfiling;
       
   469 	
       
   470 	TBool iTouchCountCheckEnabled;
       
   471     };
       
   472 
       
   473 #endif        
       
   474