os/graphics/fbs/fontandbitmapserver/sfbs/BackGroundCompression.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/fbs/fontandbitmapserver/sfbs/BackGroundCompression.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,267 @@
     1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include "SERVER.H"
    1.20 +#include "BackGroundCompression.h"
    1.21 +
    1.22 +_LIT(KFbsBgCompThrdName, "FbsBgCompressThread");
    1.23 +const TInt KFbsBgCompThrdStackSize = 4096;
    1.24 +
    1.25 +
    1.26 +CFbsBackgroundCompressionQueueElement::CFbsBackgroundCompressionQueueElement(CBitmapObject* aSrcObj, TBitmapfileCompressionScheme aScheme):
    1.27 +	iSourceObject(aSrcObj),
    1.28 +	iCompressionScheme(aScheme)
    1.29 +	{
    1.30 +	__ASSERT_DEBUG(aSrcObj->iCompressionQueueElement == NULL, User::Invariant());
    1.31 +	aSrcObj->iCompressionQueueElement = this;
    1.32 +	}
    1.33 +
    1.34 +CFbsBackgroundCompressionQueueElement::~CFbsBackgroundCompressionQueueElement()
    1.35 +	{
    1.36 +	iLink.Deque();
    1.37 +	iSourceObject->iCompressionQueueElement = NULL;
    1.38 +	if (!iMessage.IsNull())
    1.39 +		iMessage.Complete(KErrNone);
    1.40 +	}
    1.41 +
    1.42 +void CFbsBackgroundCompressionQueueElement::CompleteOutstandingRequestAndDestroy(TInt aReason)
    1.43 +	{
    1.44 +	if (!iMessage.IsNull())
    1.45 +		iMessage.Complete(aReason);
    1.46 +	delete this;
    1.47 +	}
    1.48 +
    1.49 +void CFbsBackgroundCompressionQueueElement::CompleteOutstandingRequest(const CSession2* aSession)
    1.50 +	{
    1.51 +	if (!iMessage.IsNull() && iMessage.Session() == aSession)
    1.52 +		iMessage.Complete(KErrDisconnected);
    1.53 +	}
    1.54 +
    1.55 +
    1.56 +CFbsBackgroundCompression* CFbsBackgroundCompression::NewL(CFbTop& aFbTop)
    1.57 +	{
    1.58 +	CFbsBackgroundCompression* self = new(ELeave) CFbsBackgroundCompression(aFbTop);
    1.59 +	CleanupStack::PushL(self);
    1.60 +	self->ConstructL();
    1.61 +	CleanupStack::Pop(self);
    1.62 +	return self;
    1.63 +	}
    1.64 +
    1.65 +CFbsBackgroundCompression::CFbsBackgroundCompression(CFbTop& aFbTop):
    1.66 +	CActive(CActive::EPriorityIdle),
    1.67 +	iFbTop(aFbTop),
    1.68 +	iCompressionQueue(_FOFF(CFbsBackgroundCompressionQueueElement, iLink))
    1.69 +	{
    1.70 +	CActiveScheduler::Add(this);
    1.71 +	}
    1.72 +
    1.73 +void CFbsBackgroundCompression::ConstructL()
    1.74 +	{
    1.75 +	TThreadCreateInfo threadCreateInfo(KFbsBgCompThrdName, (TThreadFunction)ThreadFunction, KFbsBgCompThrdStackSize, this);
    1.76 +	threadCreateInfo.SetUseHeap(NULL);
    1.77 +	threadCreateInfo.SetOwner(EOwnerProcess);
    1.78 +	threadCreateInfo.SetPaging(TThreadCreateInfo::EUnspecified); //Use the creating process's paging attributes.
    1.79 +	User::LeaveIfError(iThread.Create(threadCreateInfo));
    1.80 +	iThreadCreated = ETrue;
    1.81 +	iThread.SetPriority(EPriorityAbsoluteVeryLow);
    1.82 +	User::LeaveIfError(iThreadGo.CreateLocal(0));
    1.83 +	User::LeaveIfError(iThreadMutex.CreateLocal());
    1.84 +	iThread.Resume();
    1.85 +	}
    1.86 +
    1.87 +CFbsBackgroundCompression::~CFbsBackgroundCompression()
    1.88 +	{
    1.89 +	// destructor should be called only after all sessions with the server have been closed
    1.90 +	// so there should not be any bitmap objects left
    1.91 +	__ASSERT_DEBUG(iCompressionQueue.IsEmpty(), User::Invariant());
    1.92 +	__ASSERT_DEBUG(iBitmapObject == NULL, User::Invariant());
    1.93 +	if (iThreadCreated)
    1.94 +		{
    1.95 +		iThread.Kill(KErrNone);
    1.96 +		iThread.Close();
    1.97 +		iThreadGo.Close();
    1.98 +		iThreadMutex.Close();
    1.99 +		}
   1.100 +	}
   1.101 +
   1.102 +TInt CFbsBackgroundCompression::AddToCompressionQueue(CBitmapObject* aSrcObj, TBitmapfileCompressionScheme aScheme, const RMessage2* aMessage)
   1.103 +	{
   1.104 +	__ASSERT_DEBUG(aSrcObj != NULL, User::Invariant());
   1.105 +	CFbsBackgroundCompressionQueueElement* element = new CFbsBackgroundCompressionQueueElement(aSrcObj, aScheme);
   1.106 +	if (element == NULL)
   1.107 +		return KErrNoMemory;
   1.108 +	iCompressionQueue.AddLast(*element);
   1.109 +	if (!IsActive())
   1.110 +		{
   1.111 +		TInt ret = PrepareCompression();
   1.112 +		if (ret != KErrNone)
   1.113 +			{
   1.114 +			delete element;
   1.115 +			return ret;
   1.116 +			}
   1.117 +		SetActive();
   1.118 +		iThread.Rendezvous(iStatus);
   1.119 +		iThreadGo.Signal();
   1.120 +		}
   1.121 +
   1.122 +	if (aMessage != NULL)
   1.123 +		element->iMessage = *aMessage;
   1.124 +
   1.125 +	return KErrNone;
   1.126 +	}
   1.127 +
   1.128 +void CFbsBackgroundCompression::RemoveFromCompressionQueue(CBitmapObject* aSrcObj)
   1.129 +	{
   1.130 +	__ASSERT_DEBUG(aSrcObj != NULL, User::Invariant());
   1.131 +	CFbsBackgroundCompressionQueueElement* element = aSrcObj->iCompressionQueueElement;
   1.132 +	if (element != NULL)
   1.133 +		{
   1.134 +		if (iBitmapObject != NULL && iCompressionQueue.IsFirst(element))
   1.135 +			{
   1.136 +			iThreadMutex.Wait();
   1.137 +			iBitmapObject->Close();
   1.138 +			iBitmapObject = NULL;
   1.139 +			iThreadMutex.Signal();
   1.140 +			}
   1.141 +		delete element;
   1.142 +		}
   1.143 +	}
   1.144 +
   1.145 +void CFbsBackgroundCompression::CompleteOutstandingRequests(const CSession2* aSession)
   1.146 +	{
   1.147 +	TDblQueIter<CFbsBackgroundCompressionQueueElement> iterator(iCompressionQueue);
   1.148 +	CFbsBackgroundCompressionQueueElement* element;
   1.149 +	while ((element = iterator++) != NULL)
   1.150 +		element->CompleteOutstandingRequest(aSession);
   1.151 +	}
   1.152 +
   1.153 +void CFbsBackgroundCompression::CompressAll()
   1.154 +	{
   1.155 +	if (iBitmapObject != NULL) // check if a compression is in progress
   1.156 +		{
   1.157 +		User::WaitForRequest(iStatus);
   1.158 +		FinalizeCompression(iStatus);
   1.159 +		TRequestStatus* status = &iStatus;
   1.160 +		User::RequestComplete(status, KErrCompletion);
   1.161 +		}
   1.162 +	while (!iCompressionQueue.IsEmpty())
   1.163 +		{
   1.164 +		TInt ret = PrepareCompression();
   1.165 +		if (ret != KErrNone)
   1.166 +			{
   1.167 +			iCompressionQueue.First()->CompleteOutstandingRequestAndDestroy(ret);
   1.168 +			continue;
   1.169 +			}
   1.170 +		TRequestStatus status;
   1.171 +		iThread.Rendezvous(status);
   1.172 +		iThreadGo.Signal();
   1.173 +		User::WaitForRequest(status);
   1.174 +		FinalizeCompression(status);
   1.175 +		}
   1.176 +	}
   1.177 +
   1.178 +TInt CFbsBackgroundCompression::PrepareCompression()
   1.179 +	{
   1.180 +	__ASSERT_DEBUG(!iCompressionQueue.IsEmpty(), User::Invariant());
   1.181 +	__ASSERT_DEBUG(iBitmapObject == NULL, User::Invariant());
   1.182 +	CFbsBackgroundCompressionQueueElement* element = iCompressionQueue.First();
   1.183 +	CBitwiseBitmap* srcBmp = element->iSourceObject->Address();
   1.184 +	CBitmapObject* bmpObj = NULL;
   1.185 +	TRAPD(ret, bmpObj = iFbTop.CreateBitmapL(srcBmp->SizeInPixels(), srcBmp->DisplayMode(), KUidCFbsBitmapCreation, ETrue));
   1.186 +	if (ret != KErrNone)
   1.187 +		return ret;
   1.188 +	ret = bmpObj->Address()->CopyData(*srcBmp);
   1.189 +	if (ret != KErrNone)
   1.190 +		{
   1.191 +		bmpObj->Close();
   1.192 +		return ret;
   1.193 +		}
   1.194 +	iThreadMutex.Wait();
   1.195 +	iBitmapObject = bmpObj;
   1.196 +	iCompressionScheme = element->iCompressionScheme;
   1.197 +	iThreadMutex.Signal();
   1.198 +	return KErrNone;
   1.199 +	}
   1.200 +
   1.201 +void CFbsBackgroundCompression::FinalizeCompression(const TRequestStatus& aStatus)
   1.202 +	{
   1.203 +	__ASSERT_DEBUG(!iCompressionQueue.IsEmpty(), User::Invariant());
   1.204 +	__ASSERT_DEBUG(iBitmapObject != NULL, User::Invariant());
   1.205 +	iThreadMutex.Wait();
   1.206 +	CFbsBackgroundCompressionQueueElement* element = iCompressionQueue.First();
   1.207 +	if (aStatus == KErrNone)
   1.208 +		{
   1.209 +		element->iSourceObject->SetCleanBitmap(iBitmapObject);
   1.210 +		iFbTop.NotifyDirtyBitmap(*element->iSourceObject, NULL);
   1.211 +		}
   1.212 +	else
   1.213 +		{
   1.214 +		iBitmapObject->Close();
   1.215 +		}
   1.216 +	iBitmapObject = NULL;
   1.217 +	element->CompleteOutstandingRequestAndDestroy(aStatus.Int());
   1.218 +	iThreadMutex.Signal();
   1.219 +	}
   1.220 +
   1.221 +void CFbsBackgroundCompression::RunL()
   1.222 +	{
   1.223 +	// check if CompressAll() or RemoveFromCompressionQueue() have been called before
   1.224 +	if (iBitmapObject != NULL)
   1.225 +		{
   1.226 +		FinalizeCompression(iStatus);
   1.227 +		}
   1.228 +	while (!iCompressionQueue.IsEmpty())
   1.229 +		{
   1.230 +		TInt ret = PrepareCompression();
   1.231 +		if (ret == KErrNone)
   1.232 +			{
   1.233 +			SetActive();
   1.234 +			iThread.Rendezvous(iStatus);
   1.235 +			iThreadGo.Signal();
   1.236 +			return;
   1.237 +			}
   1.238 +		iCompressionQueue.First()->CompleteOutstandingRequestAndDestroy(ret);
   1.239 +		}
   1.240 +	}
   1.241 +
   1.242 +void CFbsBackgroundCompression::DoCancel()
   1.243 +	{
   1.244 +	iThread.RendezvousCancel(iStatus);
   1.245 +	}
   1.246 +
   1.247 +TInt CFbsBackgroundCompression::ThreadFunction(CFbsBackgroundCompression* aSelf)
   1.248 +	{
   1.249 +	TBool notDone = ETrue; // to suppress compiler warning
   1.250 +	// coverity[dead_error_condition]
   1.251 +	// coverity[loop_condition]
   1.252 +	while (notDone)
   1.253 +		{
   1.254 +		aSelf->iThreadGo.Wait();
   1.255 +		aSelf->iThreadMutex.Wait();
   1.256 +		if (aSelf->iBitmapObject != NULL)
   1.257 +			{
   1.258 +			TInt ret = aSelf->iBitmapObject->Address()->CompressData(aSelf->iCompressionScheme);
   1.259 +			RThread::Rendezvous(ret);
   1.260 +			}
   1.261 +		else
   1.262 +			{
   1.263 +			// bitmap removed from queue before background thread had a chance to compress it
   1.264 +			RThread::Rendezvous(KErrAbort);
   1.265 +			}
   1.266 +		aSelf->iThreadMutex.Signal();
   1.267 +		}
   1.268 +	// coverity[dead_error_line]
   1.269 +	return KErrNone;
   1.270 +	}