First public contribution.
1 // Copyright (c) 1995-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
20 #include "BackGroundCompression.h"
21 #include "linkedfonts.h"
22 #include "BitwiseBitmap.inl"
24 const TInt KFbsPriority = 0;
25 const TInt KFbsGoomMonitorSid = 0x10207218;
26 // Setup security policies
27 const TInt KRanges[] =
32 EFbsSetSystemDefaultTypefaceName,
33 EFbsGetAllBitmapHandles,
36 EFbsMessRegisterLinkedTypeface,
37 EFbsMessFetchLinkedTypeface,
38 EFbsMessUpdateLinkedTypeface,
40 EFbsMessOogmNotification,
41 EFbsMessGetGlyphCacheMetrics,
44 const TUint KRangeCount = sizeof(KRanges)/sizeof(TInt);
45 const TUint8 KElementsIndex[KRangeCount] =
48 3, // For EFbsMessShutdown
50 1, // For EFbsSetSystemDefaultTypefaceName
51 2, // For EFbsGetAllBitmapHandles
52 0, // ECapability_None for EFbsMessUnused1
53 0, // ECapability_None for EFbsMessUnused2 and beyond
54 1, // ECapabilityWriteDeviceData for EFbsMessRegisterLinkedTypeface
55 2, // ECapabilityReadDeviceData for EFbsMessFetchLinkedTypeface
56 1, // ECapabilityWriteDeviceData for EFbsMessUpdateLinkedTypeface
57 0, // ECapability_None for EFbsMessGetFontTable and beyond
58 4, // SID for EFbsMessOogmNotification.
59 0, // For EFbsMessGetGlyphCacheMetrics and on.
62 const CPolicyServer::TPolicyElement KElements[] =
64 {_INIT_SECURITY_POLICY_C1(ECapability_None), CPolicyServer::EFailClient},
65 {_INIT_SECURITY_POLICY_C1(ECapabilityWriteDeviceData), CPolicyServer::EFailClient},
66 {_INIT_SECURITY_POLICY_C1(ECapabilityReadDeviceData), CPolicyServer::EFailClient},
67 {_INIT_SECURITY_POLICY_C1(ECapabilityPowerMgmt), CPolicyServer::EFailClient},
68 {_INIT_SECURITY_POLICY_S0(KFbsGoomMonitorSid), CPolicyServer::EFailClient}
71 const CPolicyServer::TPolicy KFbsPolicy =
73 CPolicyServer::EAlwaysPass, //specifies all connect attempts should pass
80 CFontBitmapServer::CFontBitmapServer():
81 CPolicyServer(KFbsPriority, KFbsPolicy)
85 CFontBitmapServer::~CFontBitmapServer()
87 delete iTopLevelStore;
88 iTopLevelStore = NULL; // so that CFBClient::~CFbClient can avoid using this pointer;
89 // it may be called inside CFontBitmapServer::~CFontBitmapServer
92 CFontBitmapServer* CFontBitmapServer::NewL()
94 CFontBitmapServer* self = new(ELeave) CFontBitmapServer;
95 CleanupStack::PushL(self);
97 CleanupStack::Pop(); //self
102 Two-phase constructor.
104 void CFontBitmapServer::ConstructL()
106 iTopLevelStore=CFbTop::NewL();
108 // If fbserv data paging is configured as unpaged, automatically pin client descriptors
109 if(!RProcess().DefaultDataPaged())
111 SetPinClientDescriptors(ETrue);
113 StartL(KFBSERVGlobalThreadName);
116 CFbTop* CFontBitmapServer::TopLevelStore()
118 return(iTopLevelStore);
121 CSession2* CFontBitmapServer::NewSessionL(const TVersion& aVersion,const RMessage2& /*aMessage*/ ) const
123 TVersion v(KFbsMajorVersionNumber,KFbsMinorVersionNumber,KFbsBuildVersionNumber);
124 if(!User::QueryVersionSupported(v,aVersion))
125 User::Leave(KErrNotSupported);
127 CSession2* pSession2 = CFbClient::NewL(iTopLevelStore->Heap());
131 TInt CFontBitmapServer::Init()
133 return(iConnectionId++);
137 TInt CFontBitmapServer::HandleMesgTypefaceSupport(const RMessage2& aMessage, TBool& aClientPanicRequired)
139 TTypefaceSupport tfi;
140 TPckgBuf<TTypefaceSupport> ttipckg(tfi);
141 TPckgBuf<TSize> pixelSize;
142 TInt index=aMessage.Int0();
143 TInt limit=iTopLevelStore->FontStore()->NumTypefaces();
144 if(index < 0 || index > limit)
148 TInt ret = aMessage.Read(2, pixelSize);
153 iTopLevelStore->FontStore()->iKPixelHeightInTwips = pixelSize().iHeight;
154 iTopLevelStore->FontStore()->iKPixelWidthInTwips = pixelSize().iWidth;
155 iTopLevelStore->FontStore()->TypefaceSupport(ttipckg(),index);
156 ret = aMessage.Write(1,ttipckg);
159 aClientPanicRequired = ETrue;
165 TInt CFontBitmapServer::HandleMesgFontHeight(const RMessage2& aMessage, TBool aInTwips)
167 TInt typefaceindex = aMessage.Int0();
168 TInt fontsize = aMessage.Int1();
170 if(typefaceindex < 0)
172 aMessage.Panic(KFBSERVPanicCategory,KErrArgument);
176 // pixel size (used to be set in a separate call)
177 TPckgBuf<TSize> size;
178 TInt ret = aMessage.Read(2, size);
184 if(size().iHeight <= 0 || size().iWidth < 0)
186 aMessage.Panic(KFBSERVPanicCategory,KErrArgument);
190 iTopLevelStore->FontStore()->iKPixelHeightInTwips = size().iHeight;
191 iTopLevelStore->FontStore()->iKPixelWidthInTwips = size().iWidth;
194 return iTopLevelStore->FontStore()->FontHeightInTwips(typefaceindex,fontsize);
198 return iTopLevelStore->FontStore()->FontHeightInPixels(typefaceindex,fontsize);
204 //aRet is used in debug builds to allow the server to know if the call was successful.
205 //On success there should be no panics from out of memory testing, since memory
206 //may have been genuinely allocated.
207 void CFontBitmapServer::ProcMessage(const RMessage2& aMessage, TInt aSession, TInt& aRet)
209 void CFontBitmapServer::ProcMessage(const RMessage2& aMessage, TInt aSession)
213 TBool clientPanicRequired = EFalse;
217 switch (aMessage.Function())
219 case EFbsMessShutdown:
220 CActiveScheduler::Stop();
222 case EFbsMessNumTypefaces:
223 ret=iTopLevelStore->FontStore()->NumTypefaces();
225 case EFbsMessTypefaceSupport:
226 ret = HandleMesgTypefaceSupport(aMessage, clientPanicRequired);
229 case EFbsMessFontHeightInTwips:
230 ret = HandleMesgFontHeight(aMessage, ETrue);
233 case EFbsMessFontHeightInPixels:
234 ret = HandleMesgFontHeight(aMessage, EFalse);
237 case EFbsMessSetPixelHeight:
238 iTopLevelStore->FontStore()->iKPixelWidthInTwips=aMessage.Int0();
239 iTopLevelStore->FontStore()->iKPixelHeightInTwips=aMessage.Int1();
242 case EFbsMessDefaultAllocFail:
244 if(num) __UHEAP_FAILNEXT(num);
247 case EFbsMessDefaultMark:
250 case EFbsMessDefaultMarkEnd:
252 if(num) __UHEAP_MARKENDC(num);
253 else __UHEAP_MARKEND;
255 case EFbsMessUserAllocFail:
257 if(num) __RHEAP_FAILNEXT(iTopLevelStore->Heap(),num);
258 else __RHEAP_RESET(iTopLevelStore->Heap());
260 case EFbsMessUserMark:
261 __RHEAP_MARK(iTopLevelStore->Heap());
263 case EFbsMessUserMarkEnd:
265 if(num) __RHEAP_MARKENDC(iTopLevelStore->Heap(),num);
266 else __RHEAP_MARKEND(iTopLevelStore->Heap());
269 case EFbsMessHeapCheck:
270 iTopLevelStore->Heap()->Check();
271 User::Heap().Check();
273 case EFbsMessSetDefaultGlyphBitmapType:
274 iTopLevelStore->FontStore()->SetDefaultBitmapType((TGlyphBitmapType)aMessage.Int0());
276 case EFbsMessGetDefaultGlyphBitmapType:
277 ret = iTopLevelStore->FontStore()->DefaultBitmapType();
279 case EFbsMessFontNameAlias:
280 TRAP(ret, iTopLevelStore->SetFontNameAliasL(aMessage));
282 case EFbsMessGetHeapSizes:
283 TRAP(ret, GetHeapSizesL(aMessage));
285 case EFbsMessDefaultLanguageForMetrics:
286 iTopLevelStore->SetDefaultLanguageForMetrics(aMessage);
289 iTopLevelStore->BackgroundCompression()->CompressAll();
291 case EFbsMessRegisterLinkedTypeface:
293 TPckgBuf<TLinkedTypefaceSpecificationArgs> buf;
294 ret=aMessage.Read(0,buf);
297 //Id used by original font linking; now unused.
299 ret=iTopLevelStore->FontStore()->CreateLinkedTypeface(buf(),aSession,id);
303 case EFbsMessFetchLinkedTypeface:
305 TBuf<KMaxTypefaceNameLength> name;
306 ret = aMessage.GetDesLength(0);
311 if (ret > KMaxTypefaceNameLength)
313 clientPanicRequired = ETrue;
318 ret = aMessage.Read(0,name);
320 TLinkedTypefaceSpecificationArgs spec;
326 TRAP(ret, iTopLevelStore->FontStore()->GetLinkedTypefaceL(spec));
330 TPckgBuf<TLinkedTypefaceSpecificationArgs> specArgs = spec;
331 ret = aMessage.Write(2,specArgs);
336 case EFbsMessUpdateLinkedTypeface:
338 TPckgBuf<TLinkedTypefaceSpecificationArgs> buf;
339 ret=aMessage.Read(0,buf);
342 TRAP(ret, iTopLevelStore->FontStore()->UpdateLinkedTypefaceL(buf()));
348 if (clientPanicRequired)
350 aMessage.Panic(KFBSERVPanicCategory,ret);
354 if(!aMessage.IsNull())
356 aMessage.Complete(ret);
367 Returns the current sizes of the FBServ default heap, the heap for large bitmaps,
368 and the heap for small bitmaps.
370 Not supported in release builds.
374 @param aMessage Encapsulates references to integers supplied by the caller, which will on return contain the sizes of the default heap, heap for small bitmaps, and heap for large bitmaps.
375 @leave KErrNotSupported when used in release mode.
378 void CFontBitmapServer::GetHeapSizesL(const RMessage2& aMessage)
380 TPckgBuf<THeapSizes> data;
381 TInt defaultHeapSize = 0;
382 TInt smallBmpHeapSize = 0;
383 TInt bigBmpHeapSize = 0;
385 User::Heap().AllocSize(defaultHeapSize);
386 bigBmpHeapSize = iTopLevelStore->iLargeBitmapChunk.Size();
387 iTopLevelStore->Heap()->AllocSize(smallBmpHeapSize);
389 data().iDefault = defaultHeapSize;
390 data().iBig = bigBmpHeapSize;
391 data().iSmall = smallBmpHeapSize;
393 aMessage.WriteL(0, data);
396 void CFontBitmapServer::GetHeapSizesL(const RMessage2&)
398 User::Leave(KErrNotSupported);
403 CFontObject::CFontObject(CFontStore* aFontStore, CGlyphAtlas* aGlyphAtlas):
405 iFontStore(aFontStore),
406 iGlyphAtlas(aGlyphAtlas)
410 CFontObject::~CFontObject()
412 if (AccessCount()==1)
416 iFontStore->ReleaseFont(iAddressPointer);
419 iGlyphAtlas->FontReleased(*iAddressPointer);
423 // CBitmapObject constructor - takes ownership of aBmp
424 CBitmapObject::CBitmapObject(CFbTop& aFbTop, CBitwiseBitmap* aBmp):
427 iAddressPointer(aBmp)
429 __ASSERT_DEBUG(iAddressPointer, User::Invariant());
432 /** Second-phase constructor of reference counting objects for bitmaps.
433 Adds the CBitmapObject to the bitmap object container and, if it is not a
434 replacement for a dirty bitmap, assigns a server-wide handle to it and adds
435 it to the server's bitmap index. When a replacement bitmap is attached to
436 the corresponding dirty bitmap with a call to SetCleanBitmap(), then it is
437 assigned the same server-wide handle as the bitmap to be replaced.
439 @param aReplacement If ETrue the bitmap is being created as a replacement
440 for a bitmap being made dirty by a resize or compress operation.
443 void CBitmapObject::ConstructL(TBool aReplacement)
445 iFbTop->iBitmapCon->AddL(this);
448 iHandle = reinterpret_cast<TInt>(this);
449 while (iFbTop->iBitmapObjectIndex.FindInOrder(this, Compare) != KErrNotFound)
453 User::LeaveIfError(iFbTop->iBitmapObjectIndex.InsertInOrder(this, Compare));
457 void CleanupBitmapObject(TAny* aPtr)
459 CBitmapObject* ptr = static_cast<CBitmapObject*>(aPtr);
463 /** Create a reference counting object for a new bitmap.
465 @param aFbTop Reference to the CFbTop singleton.
466 @param aBmp Bitmap to be attached to the new reference counting object.
467 The bitmap must have been pushed on the clean-up stack and it will be
468 popped out of the stack by this function. Ownership will be transferred
469 to the reference counting object.
470 @param aReplacement If ETrue the bitmap is being created as a replacement
471 for a bitmap being made dirty by a resize or compress operation.
474 CBitmapObject* CBitmapObject::NewL(CFbTop& aFbTop, CBitwiseBitmap* aBmp, TBool aReplacement)
476 CBitmapObject* bmpObj = new(ELeave) CBitmapObject(aFbTop, aBmp);
477 CleanupStack::Pop(aBmp); // aBmp is owned now by bmpObj
478 TCleanupItem bitmapObjectCleanupItem(CleanupBitmapObject, bmpObj);
479 CleanupStack::PushL(bitmapObjectCleanupItem);
480 bmpObj->ConstructL(aReplacement);
481 CleanupStack::Pop(bmpObj);
485 CBitmapObject::~CBitmapObject()
487 iFbTop->BackgroundCompression()->RemoveFromCompressionQueue(this);
488 // the associated clean bitmap object cannot possibly be now in the background compression queue
493 TInt index = iFbTop->iBitmapObjectIndex.FindInOrder(this, Compare);
494 if (index != KErrNotFound)
498 iFbTop->iBitmapObjectIndex[index] = iCleanBitmap;
502 iFbTop->iBitmapObjectIndex.Remove(index);
506 if (iCleanBitmap != NULL)
508 iCleanBitmap->SetOwner(NULL);
509 iCleanBitmap->Close();
512 delete iAddressPointer;
515 void CBitmapObject::SetCleanBitmap(CBitmapObject* aNewBmpObj)
517 __ASSERT_DEBUG(iHandle, User::Invariant());
518 __ASSERT_DEBUG(iCleanBitmap == NULL, User::Invariant());
519 __ASSERT_DEBUG(aNewBmpObj->Owner() == NULL, User::Invariant());
520 __ASSERT_DEBUG(aNewBmpObj->iHandle == 0, User::Invariant());
521 __ASSERT_DEBUG(aNewBmpObj->iCleanBitmap == NULL, User::Invariant());
522 iCleanBitmap = aNewBmpObj;
523 aNewBmpObj->SetOwner(this);
524 aNewBmpObj->iHandle = iHandle;
525 iAddressPointer->iSettings.SetDirtyBitmap();
526 aNewBmpObj->iAddressPointer->Extra()->iSerialNumber = iAddressPointer->Extra()->iSerialNumber;
527 aNewBmpObj->iAddressPointer->Extra()->iTouchCount = iAddressPointer->Extra()->iTouchCount + 1;
530 void CBitmapObject::Close()
532 if ((iCleanBitmap != NULL) && (Owner() != NULL) && (AccessCount() == 2))
534 static_cast<CBitmapObject*>(Owner())->iCleanBitmap = iCleanBitmap;
535 iCleanBitmap->SetOwner(Owner());
542 TInt CBitmapObject::Compare(const CBitmapObject& aBmpObj1, const CBitmapObject& aBmpObj2)
544 return aBmpObj1.iHandle - aBmpObj2.iHandle;
547 TInt CBitmapObject::Compare(const TInt* aHandle, const CBitmapObject& aBmpObj)
549 return *aHandle - aBmpObj.iHandle;
552 // CSharedBitmapObject constructor - takes ownership of aBmp and aKey
553 CSharedBitmapObject::CSharedBitmapObject(CFbTop& aFbTop, CBitwiseBitmap* aBmp, TDesC* aKey):
554 CBitmapObject(aFbTop, aBmp),
557 __ASSERT_DEBUG(iKey, User::Invariant());
560 /** Second-phase constructor of reference counting objects for bitmaps
561 loaded to share from a file. Adds the CSharedBitmapObject to the bitmap
562 object container, assigns a server-wide handle to it and adds it to the
563 server's bitmap index and to the server's shared bitmap hash map.
565 @param aHash Hash value of the key associated with the bitmap.
568 void CSharedBitmapObject::ConstructL(TUint aHash)
570 CBitmapObject::ConstructL(EFalse);
571 iFbTop->iSharedBitmapObjectHashMap.Insert(*this, aHash);
574 /** Create a reference counting object for a bitmap loaded to share from a file.
576 @param aFbTop Reference to the CFbTop singleton.
577 @param aBmp Bitmap to be attached to the new reference counting object.
578 The bitmap must have been pushed on the clean-up stack and it will be
579 popped out of the stack by this function. Ownership will be transferred
580 to the reference counting object.
581 @param aKey Key associated with the bitmap. The key must have been pushed on
582 the clean-up stack and it will be popped out of the stack by this function.
583 Ownership will be transferred to the reference counting object.
584 @param aHash Hash value of the key associated with the bitmap.
587 CSharedBitmapObject* CSharedBitmapObject::NewL(CFbTop& aFbTop, CBitwiseBitmap* aBmp, TDesC* aKey, TUint aHash)
589 CSharedBitmapObject* bmpObj = new(ELeave) CSharedBitmapObject(aFbTop, aBmp, aKey);
590 CleanupStack::Pop(2, aKey); // aBmp and aKey are owned now by bmpObj
591 TCleanupItem bitmapObjectCleanupItem(CleanupBitmapObject, bmpObj);
592 CleanupStack::PushL(bitmapObjectCleanupItem);
593 bmpObj->ConstructL(aHash);
594 CleanupStack::Pop(bmpObj);
598 CSharedBitmapObject::~CSharedBitmapObject()
600 iFbTop->iSharedBitmapObjectHashMap.Remove(*this);
604 // Constructs a single key object from constituent parts that uniquely identify a bitmap
605 HBufC* CSharedBitmapObject::KeyLC(const TDesC& aFileName, TInt aId, const TTime& aModTime)
607 static const TInt K16BitSizeOfTInt = sizeof(TInt) /sizeof(TUint16);
608 static const TInt K16BitSizeOfTTime = sizeof(TTime)/sizeof(TUint16);
610 HBufC* key = HBufC::NewLC(K16BitSizeOfTInt + K16BitSizeOfTTime + aFileName.Length());
612 TPtr keyPtr = key->Des();
614 keyPtr.Append(reinterpret_cast<const TUint16*>(&aId), K16BitSizeOfTInt);
615 keyPtr.Append(reinterpret_cast<const TUint16*>(&aModTime), K16BitSizeOfTTime);
616 keyPtr.Append(aFileName);