os/textandloc/fontservices/fontstore/src/ShaperCache.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2006-2009 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 #include <fntstore.h>
    20 #include <gdi.h>
    21 #include "FNTSTD.H"
    22 #include <graphics/shapeimpl.h>
    23 #include "ShaperCache.H"
    24 
    25 //class COpenFontShaperCacheEntry;
    26 COpenFontShaperCacheEntry* COpenFontShaperCacheEntry::New(RHeap* aHeap)
    27 	{
    28 	COpenFontShaperCacheEntry* entry = (COpenFontShaperCacheEntry*)aHeap->Alloc(sizeof(COpenFontShaperCacheEntry));
    29 	if (!entry)
    30 		return NULL;
    31 	new(entry) COpenFontShaperCacheEntry();	
    32 	return entry;
    33 	}
    34 	
    35 COpenFontShaperCacheEntry* COpenFontShaperCacheEntry::New(RHeap* aHeap, CShaper::TInput aInput, TShapeHeader* aShapeHeader)
    36 	{
    37 	COpenFontShaperCacheEntry* entry = (COpenFontShaperCacheEntry*)aHeap->Alloc(sizeof(COpenFontShaperCacheEntry));
    38 	if (!entry)
    39 		return NULL;
    40 	new(entry) COpenFontShaperCacheEntry(aHeap,aInput);
    41 	TInt ret=entry->Construct(aInput,aShapeHeader);
    42 	if (ret!=KErrNone)
    43 		{
    44 		aHeap->Free(entry);
    45 		return NULL;
    46 		}
    47 	return entry;
    48 	}
    49 
    50 TInt COpenFontShaperCacheEntry::Construct(CShaper::TInput aInput,TShapeHeader* aShapeHeader)
    51 	{
    52 	// Get some memory for the shape header
    53 	iShapeHeader = (TShapeHeader*)( iHeap->Alloc(
    54 	sizeof(TShapeHeader) + (aShapeHeader->iGlyphCount * 10) + 4));
    55 	if (iShapeHeader==NULL)
    56 		{
    57 		return KErrNoMemory;
    58 		}
    59 	Mem::Copy(iShapeHeader, aShapeHeader,sizeof(TShapeHeader) + (aShapeHeader->iGlyphCount * 10) + 4);
    60 	
    61 	// Get some memory for the input text
    62 	iText = (TUint16*)(iHeap->Alloc(sizeof(TUint16)*aInput.iText->Length()));
    63 	if (iText==NULL)
    64 		{
    65 		iHeap->Free(iShapeHeader);
    66 		return KErrNoMemory;
    67 		}
    68 	TUint16* inputText = (TUint16*)aInput.iText->Ptr();
    69 	Mem::Copy(iText, inputText, sizeof(TUint16)*aInput.iText->Length());
    70 	return KErrNone;
    71 	}
    72 	
    73 /**
    74 Utility function used to free memory taken up by an entry to the cache
    75 @internalTechnology
    76 **/
    77 void COpenFontShaperCacheEntry::Delete(RHeap* aHeap,COpenFontShaperCacheEntry* aCacheEntry)
    78 	{
    79 	if (!aCacheEntry->IsSentinel())
    80 		{
    81 		aHeap->Free(aCacheEntry->iShapeHeader);
    82 		aHeap->Free(aCacheEntry->iText);
    83 		//now free the THandleCount list
    84 		if (aCacheEntry->iHandleRefList)
    85 			{
    86 			THandleCount* ptr=aCacheEntry->iHandleRefList;
    87 			while (ptr)
    88 				{
    89 				THandleCount* next=ptr->iNext;
    90 				aHeap->Free(ptr);
    91 				ptr=next;
    92 				}
    93 			aCacheEntry->iHandleRefList=NULL;
    94 			}
    95 		aHeap->Free(aCacheEntry);
    96 		}
    97 	}
    98 
    99 //function to update the ref count for a particular session handle	
   100 TInt COpenFontShaperCacheEntry::IncRefCount(TInt aSessionHandle)
   101 	{
   102 	//get pointer to start of the linked list
   103 	THandleCount* ptr=iHandleRefList;
   104 	THandleCount* last=NULL;
   105 	//loop through existing entries first
   106 	while (ptr)
   107 		{
   108 		if (ptr->iSessionHandle==aSessionHandle)
   109 			{
   110 			ptr->iRefCount++;
   111 			return KErrNone;			
   112 			}
   113 		last=ptr;
   114 		ptr=ptr->iNext;
   115 		}
   116 	// now we have reached the end of the list or we start with empty list
   117 	THandleCount* handle=(THandleCount*)iHeap->Alloc(sizeof(THandleCount));
   118 	if (!handle)
   119 		return KErrNoMemory;
   120 	new (handle)THandleCount(aSessionHandle);
   121 	//if the list is initially empty
   122 	if (!last)
   123 		iHandleRefList=handle;
   124 	else
   125 		last->iNext=handle;
   126 	iHandleRefCount++;
   127 	return KErrNone;	
   128 	}
   129 
   130 //function to decrement the ref count for a particular session handle
   131 //aReset here is used to immediately reset the handleCount reference to zero
   132 //and remove from the entry(this will be called in the case when a client session dies
   133 //Note that once the ref count reaches zero the THandleCount entry is automatically
   134 //removed from the list
   135 TInt COpenFontShaperCacheEntry::DecRefCount(TInt aSessionHandle,TBool aReset)
   136 	{
   137 	//get pointer to start of the linked list
   138 	THandleCount* ptr=iHandleRefList;
   139 	THandleCount* last=NULL;
   140 	//loop through existing entries first
   141 	while (ptr)
   142 		{
   143 		if (ptr->iSessionHandle==aSessionHandle)
   144 			{
   145 			if (aReset || ptr->iRefCount==1)
   146 				{
   147 				THandleCount* next=ptr->iNext;
   148 				//delete from first item
   149 				if (last==NULL)
   150 					iHandleRefList=next;
   151 				else
   152 					last->iNext=next;
   153 				iHeap->Free(ptr);
   154 				iHandleRefCount--;			
   155 				return KErrNone;
   156 				}
   157 			ptr->iRefCount--;
   158 			return KErrNone;			
   159 			}
   160 		last=ptr;	
   161 		ptr=ptr->iNext;
   162 		}
   163 	return KErrNotFound;	
   164 	}	
   165