os/graphics/windowing/windowserver/nga/SERVER/openwfc/Direct.CPP
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2000-2009 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 // Direct Screen Access class, to allow the client to draw directly to the screen
    15 // 
    16 //
    17 
    18 #include <e32base.h>
    19 #include "Direct.H"
    20 #include "server.h"
    21 #include "rootwin.h"
    22 #include "wstop.h"
    23 #include "panics.h"
    24 #include <e32msgqueue.h>
    25 
    26 const TInt KMsgQueueLength = 1;
    27 
    28 CDsaMsgQueue* CDsaMsgQueue::NewL(CWsDirectScreenAccess* aDirect)
    29 	{
    30 	CDsaMsgQueue* self = new(ELeave)CDsaMsgQueue;
    31 	CleanupStack::PushL(self);
    32 	self->ConstructL(aDirect);
    33 	CleanupStack::Pop(self);
    34 	return self;
    35 	}
    36 
    37 CDsaMsgQueue::CDsaMsgQueue()
    38 {}
    39 
    40 CDsaMsgQueue::~CDsaMsgQueue()
    41 	{
    42 	delete iAborted;
    43 	iSendQueue.Close();
    44 	iRecQueue.Close();
    45 	}
    46 
    47 void CDsaMsgQueue::ConstructL(CWsDirectScreenAccess* aDirect)
    48 	{
    49 	iAborted=new(ELeave) CWsAbortDirect(aDirect,this);	
    50 	}
    51 
    52 void CDsaMsgQueue::Cancel()
    53 	{
    54 	//purge the queue of data
    55 	TInt ret = KErrNone;
    56 	do
    57 		{
    58 		TInt data = 0;
    59 		ret = iRecQueue.Receive(&data,sizeof(TInt));
    60 		}while(ret == KErrNone);
    61 	iAborted->Cancel();
    62 	}
    63 
    64 TInt CDsaMsgQueue::Send(TInt aData)
    65 	{
    66 	return iSendQueue.Send(&aData,sizeof(TInt));
    67 	}
    68 
    69 TInt CDsaMsgQueue::CreateSendQueue()
    70 	{
    71 	if(iSendQueue.Handle() == 0)
    72 		{
    73 		return iSendQueue.CreateGlobal(KNullDesC,KMsgQueueLength,sizeof(TInt), EOwnerProcess);
    74 		}
    75 	else
    76 		{
    77 		return 0;
    78 		}
    79 	}
    80 
    81 TInt CDsaMsgQueue::CreateRecQueue()
    82 	{
    83 	if(iRecQueue.Handle() == 0)
    84 		{
    85 		return iRecQueue.CreateGlobal(KNullDesC,KMsgQueueLength,sizeof(TInt), EOwnerProcess);
    86 		}
    87 	else
    88 		{
    89 		return 0;
    90 		}
    91 	}
    92 
    93 RMsgQueueBase* CDsaMsgQueue::SendQueue()
    94 	{
    95 	return &iSendQueue;
    96 	}
    97 
    98 RMsgQueueBase* CDsaMsgQueue::RecQueue()
    99 	{
   100 	return &iRecQueue;
   101 	}
   102 
   103 void CDsaMsgQueue::Started()
   104 	{
   105 	iAborted->Started();
   106 	}
   107 	
   108 void CDsaMsgQueue::Complete()
   109 	{
   110 	iAborted->Complete(RDirectScreenAccess::ETerminateCancel);	
   111 	}
   112 
   113 void CDsaMsgQueue::CompleteAbort()
   114 	{
   115 	iAborted->Complete(KErrNone);	
   116 	}
   117 
   118 TInt CDsaMsgQueue::ReceiveData()
   119 	{
   120 	TInt data = 0;
   121 	iRecQueue.Receive(&data,sizeof(TInt));
   122 	return data;
   123 	}
   124 
   125 /*CWsAbortDirect*/
   126 
   127 CWsAbortDirect::CWsAbortDirect(CWsDirectScreenAccess* aDirect,CDsaMsgQueue* aParent) : CActive(EDirectAbort), iDirect(aDirect),iParent(aParent)
   128 	{
   129 	CActiveScheduler::Add(this);
   130 	}
   131 
   132 CWsAbortDirect::~CWsAbortDirect()
   133 	{
   134 	Cancel();
   135 	}
   136 
   137 void CWsAbortDirect::Started()
   138 	{
   139 	iStatus=KRequestPending;
   140 	TRequestStatus& status = iStatus;	
   141 	iParent->RecQueue()->NotifyDataAvailable(status);
   142 	SetActive();
   143 	}
   144 
   145 void CWsAbortDirect::RunL()
   146 	{
   147 	iParent->ReceiveData();
   148 	iParent->RecQueue()->CancelDataAvailable();
   149 	iDirect->Aborted();
   150 	}
   151 
   152 void CWsAbortDirect::DoCancel()
   153 	{
   154 	iParent->RecQueue()->CancelDataAvailable();
   155 	if (iStatus==KRequestPending)
   156 		{
   157 		Complete(KErrNone);
   158 		}
   159 	}
   160 
   161 void CWsAbortDirect::Complete(TInt aReason)
   162 	{
   163 	if(IsActive())
   164 		{
   165 		TRequestStatus* status=&iStatus;
   166 		iParent->RecQueue()->CancelDataAvailable();
   167 		RThread().RequestComplete(status,aReason);
   168 		}
   169 	}
   170 
   171 
   172 /*CWsDirectScreenAccess*/
   173 
   174 CWsDirectScreenAccess* CWsDirectScreenAccess::NewL(CWsClient* aOwner,TBool aRegionTrackingOnly)
   175 	{
   176 	CWsDirectScreenAccess* self = new(ELeave) CWsDirectScreenAccess(aOwner);
   177 	CleanupStack::PushL(self);
   178 	self->ConstructL(aRegionTrackingOnly);
   179 	CleanupStack::Pop(self);
   180 	return self;
   181 	}
   182 
   183 CWsDirectScreenAccess::~CWsDirectScreenAccess()
   184 	{
   185 	iVisible.Close();
   186 	if (iStatus!=EDirectStatusTimeNotCreated)
   187 		{
   188 		if (iStatus==EDirectStatusRunning)
   189 			AbortNow();
   190 		if (iStatus>=EDirectStatusAborted)
   191 			CorrectScreen();
   192 		}
   193 	WS_ASSERT_DEBUG(!OnQueue(), EWsPanicDirectScreenAccess);
   194 	if(!iRegionTrackingOnly)
   195 		{
   196 		CScreen* screen = Screen();
   197 		__ASSERT_DEBUG(screen,Panic(EWsPanicNoScreen));
   198 		screen->ReleaseDsaScreenDevice();
   199 		}
   200 	delete iMsgQueue;
   201 	}
   202 
   203 void CWsDirectScreenAccess::ConstructL(TBool aRegionTrackingOnly)
   204 	{
   205 	NewObjL();
   206 	iMsgQueue = CDsaMsgQueue::NewL(this);
   207 	iStatus=EDirectStatusNone;
   208 	iRegionTrackingOnly = aRegionTrackingOnly;
   209 	if(!iRegionTrackingOnly)
   210 		{
   211 		//The Direct Screen Access object is going to be used to draw to the screen so we need to:
   212 		//1)allocate the buffer
   213 		//2)initialize the DSA device and surface
   214 		CScreen* screen = Screen();
   215 		__ASSERT_DEBUG(screen,Panic(EWsPanicNoScreen));
   216 		screen->AcquireDsaScreenDeviceL();	
   217 		}
   218 	}
   219 
   220 void CWsDirectScreenAccess::Request(TInt handle)
   221 	{
   222 	if (iStatus!=EDirectStatusNone)
   223 		{
   224 		if (iStatus==EDirectStatusCompleted)
   225 			{
   226 			iMsgQueue->Cancel();
   227 			}
   228 		else
   229 			{
   230 			iWsOwner->PPanic(EWservPanicDirectMisuse);
   231 			}
   232 		}
   233 	iWsOwner->HandleToClientWindow(handle,&iWin);
   234 	STACK_REGION region;
   235 	iWin->GenerateTopRegion(region);
   236 	
   237 	//clip the draw region with DSA buffer
   238 	TRect dsaBuffer(TPoint(0,0), iScreen->DSASizeInPixels());
   239 	MWsDisplayMapping *dispMap = iScreen->DisplayMapping();
   240 	TRect dsaBufferInAppSpace;
   241 	if(dispMap)
   242 		{
   243 		dispMap->MapCoordinates(EDirectScreenAccessSpace,dsaBuffer,EApplicationSpace,dsaBufferInAppSpace);
   244 		}
   245 	else
   246 		{
   247 		dsaBufferInAppSpace = dsaBuffer;
   248 		}
   249 	region.ClipRect(dsaBufferInAppSpace);
   250 	const TInt regionCount=region.Count();
   251 	region.Close();
   252 	SetReply(regionCount);
   253 	iStatus=EDirectStatusInitialising;
   254 	}
   255 
   256 void CWsDirectScreenAccess::GetRegion(TInt aNumRects)
   257 	{
   258 #if defined(_DEBUG)
   259 	if (iStatus!=EDirectStatusInitialising)
   260 		iWsOwner->PPanic(EWservPanicDirectMisuse);
   261 #endif
   262 	STACK_REGION winVisibleRegion, region;
   263 	iWin->GenerateTopRegion(winVisibleRegion);
   264 	//clip the draw region with DSA buffer
   265 	TRect dsaBuffer(TPoint(0,0), iScreen->DSASizeInPixels());
   266 	MWsDisplayMapping *dispMap = iScreen->DisplayMapping();
   267 	TRect dsaBufferInAppSpace;
   268 	if(dispMap)
   269 		{
   270 		dispMap->MapCoordinates(EDirectScreenAccessSpace,dsaBuffer,EApplicationSpace,dsaBufferInAppSpace);
   271 		}
   272 	else
   273 		{
   274 		dsaBufferInAppSpace = dsaBuffer;
   275 		}
   276 	region.Copy(winVisibleRegion);
   277 	region.ClipRect(dsaBufferInAppSpace);
   278 	
   279 	const TInt regionCount=region.Count();
   280 	if (region.Count()==aNumRects)
   281 		{
   282 		iVisible.Copy(winVisibleRegion);
   283 		if (iVisible.CheckError())
   284 			{
   285 			iStatus=EDirectStatusNone;
   286 			SetReply(KErrNotReady);
   287 			}
   288 		else
   289 			{
   290 			TInt error = Initiate();
   291 			
   292 			if (!error)
   293 				{
   294 				TPtrC8 rectList(REINTERPRET_CAST(const TUint8*,region.RectangleList()),region.Count()*sizeof(TRect));
   295 				CWsClient::ReplyBuf(rectList);
   296 				iStatus=EDirectStatusRunning;
   297 				iMsgQueue->Started();
   298 				SetReply(KMaxTInt);
   299 				}
   300 			else
   301 				{
   302 				iStatus=EDirectStatusNone;
   303 				SetReply(error);
   304 				}
   305 			}
   306 		}
   307 	else
   308 		{
   309 		SetReply(region.Count());
   310 		}
   311 	region.Close();
   312 	winVisibleRegion.Close();
   313 	}
   314 
   315 void CWsDirectScreenAccess::Cancel()
   316 	{
   317 	TInt ret = 0;
   318 	switch (iStatus)
   319 		{
   320 #if defined(_DEBUG)
   321 	case EDirectStatusInitialising:
   322 		iWsOwner->PPanic(EWservPanicDirectMisuse);
   323 #endif
   324 	case EDirectStatusNone:
   325 		break;
   326 	case EDirectStatusRunning:
   327 		Terminate1();
   328 		Terminate2(); 
   329 		/*Fall through*/
   330 	case EDirectStatusCanceling:
   331 		ret = 1;
   332 		iMsgQueue->Send(RDirectScreenAccess::ETerminateCancel);	
   333 		break;
   334 	case EDirectStatusAbortedWindow:
   335 	case EDirectStatusAbortedGlobal:
   336 		CorrectScreen();
   337 		break;
   338 	case EDirectStatusCompleted:
   339 		break;
   340 	default:
   341 			{
   342 			}
   343 		}
   344 	SetReply(ret);
   345 	iStatus=EDirectStatusNone;
   346 	}
   347 
   348 void CWsDirectScreenAccess::Aborted()
   349 	{
   350 	switch (iStatus)
   351 		{
   352 	case EDirectStatusRunning:
   353 		Terminate1();
   354 		Terminate2();
   355 		iStatus=EDirectStatusCanceling;
   356 		break;
   357 	case EDirectStatusAbortedWindow:
   358 	case EDirectStatusAbortedGlobal:
   359 		CorrectScreen(); 
   360 		/*Fall Through*/ 
   361 	case EDirectStatusCompleted:
   362 		iStatus=EDirectStatusNone;
   363 		break;
   364 	default:
   365 		iWsOwner->PPanic(EWservPanicDirectMisuse);
   366 		}
   367 	}
   368 
   369 void CWsDirectScreenAccess::AbortNow()
   370 	{
   371 	if (iStatus!=EDirectStatusRunning)
   372 		{
   373 		iWsOwner->PPanic(EWservPanicDirectMisuse);
   374 		}
   375 	SignalAbort(RDirectScreenAccess::ETerminateRegion);
   376 	TRequestStatus timerStatus;
   377 	RTimer& timer=CWsTop::Timer();
   378 	timer.After(timerStatus,400000);		//0.4secs
   379 	User::WaitForRequest(iMsgQueue->Status(),timerStatus);
   380 	if (timerStatus!=KRequestPending)
   381 		{
   382 		Abort();
   383 		}
   384 	else
   385 		{
   386 		CancelAbortObject();
   387 		timer.Cancel();
   388 		User::WaitForRequest(timerStatus);
   389 		}
   390 	}
   391 
   392 void CWsDirectScreenAccess::SignalAbort(RDirectScreenAccess::TTerminationReasons aReason)
   393 	{
   394 	iAbortReason=aReason;
   395 	if (iStatus==EDirectStatusAbortedWindow)
   396 		{
   397 		WS_ASSERT_DEBUG(iAbortReason>RDirectScreenAccess::ETerminateRegion, EWsPanicDirectScreenAccess);
   398 		Terminate2();
   399 		return;
   400 		}
   401 	if (iStatus!=EDirectStatusRunning)
   402 		{
   403 		iWsOwner->PPanic(EWservPanicDirectMisuse);
   404 		}
   405 	iMsgQueue->Send(aReason);
   406 	}
   407 
   408 void CWsDirectScreenAccess::CancelAbortObject()
   409 	{
   410 	iMsgQueue->Complete();
   411 	iMsgQueue->Cancel();
   412 	iStatus=EDirectStatusNone;
   413 	Terminate1();
   414 	Terminate2();
   415 	}
   416 
   417 void CWsDirectScreenAccess::Abort()
   418 	{
   419 	if ( iStatus == EDirectStatusRunning ) 	
   420 		{
   421 		Terminate1();
   422 		if (iMsgQueue->Status()==KRequestPending)
   423 			iStatus=(iAbortReason<=RDirectScreenAccess::ETerminateRegion ? EDirectStatusAbortedWindow:EDirectStatusAbortedGlobal);
   424 		else
   425 			iStatus=EDirectStatusCompleted;
   426 		if (iStatus!=EDirectStatusAbortedWindow)
   427 			Terminate2();
   428 		}
   429 	}
   430 
   431 TInt CWsDirectScreenAccess::GetSendQueue()
   432 	{
   433 	TInt ret = iMsgQueue->CreateSendQueue();
   434 	if(ret == KErrNone)
   435 		{
   436 		iWsOwner->SetResponseHandle(iMsgQueue->SendQueue());	
   437 		}
   438 	return ret;
   439 	}
   440 
   441 TInt CWsDirectScreenAccess::GetRecQueue()
   442 	{
   443 	TInt ret = iMsgQueue->CreateRecQueue();
   444 	if(ret == KErrNone)
   445 		{
   446 		iWsOwner->SetResponseHandle(iMsgQueue->RecQueue());	
   447 		}
   448 	return ret;
   449 	}
   450 
   451 void CWsDirectScreenAccess::CommandL(TInt aOpcode,const TAny* aCmdData)
   452 	{
   453 	TWsDirectCmdUnion pData;
   454 	pData.any=aCmdData;
   455 	switch(aOpcode)
   456 		{
   457 		case EWsDirectOpFree:
   458 			delete this;
   459 			break;
   460 		case EWsDirectOpRequest:
   461 			Request(*pData.Int);
   462 			break;
   463 		case EWsDirectOpInitFailed:
   464 	#if defined(_DEBUG)
   465 			if (iStatus!=EDirectStatusInitialising)
   466 				{
   467 				iWsOwner->PPanic(EWservPanicDirectMisuse);
   468 				}
   469 	#endif
   470 			iStatus=EDirectStatusNone;
   471 			break;
   472 		case EWsDirectOpGetRegion:
   473 			GetRegion(*pData.Int);
   474 			break;
   475 		case EWsDirectOpCancel:
   476 			Cancel();
   477 			break;
   478 		case EWsDirectOpGetSendQueue:
   479 			GetSendQueue();
   480 			break;
   481 		case EWsDirectOpGetRecQueue:
   482 			GetRecQueue();
   483 			break;
   484 		default:
   485 			OwnerPanic(EWservPanicOpcode);
   486 			break;
   487 		}
   488 	}
   489 
   490 TInt CWsDirectScreenAccess::Initiate()
   491 	{
   492 	const TInt err = iWin->AddDSA(*this);
   493 	if(err)
   494 		return err;
   495 
   496 	iScreen->AddDirect(*this);
   497 	return KErrNone;
   498 	}
   499 
   500 static TBool RegionsMatch(const TRegion & aOne, const TRegion & aTwo)
   501 	{
   502 	// Check if the regions have equal areas.
   503 	const TRect* rect1 = aOne.RectangleList();
   504 	TUint area1 = 0;
   505 	for(TInt i = 0; i < aOne.Count(); ++i)
   506 		{
   507 		area1 += (rect1->Width() * rect1->Height());
   508 		rect1++;
   509 		}
   510 
   511 	const TRect* rect2 = aTwo.RectangleList();
   512 	TUint area2 = 0;
   513 	for(TInt i = 0; i < aTwo.Count(); ++i)
   514 		{
   515 		area2 += (rect2->Width() * rect2->Height());
   516 		rect2++;
   517 		}
   518 	
   519 	if(area1 != area2)
   520 		{
   521 		return EFalse;
   522 		}
   523 
   524 	// Check if one region is completely contained within the other.
   525 	STACK_REGION tempRegion;
   526 	tempRegion.Copy(aOne);	
   527 	tempRegion.SubRegion(aTwo);
   528 
   529 	const TBool ret(tempRegion.IsEmpty());
   530 	tempRegion.Close();
   531 
   532 	return ret;
   533 	}
   534 
   535 TBool CWsDirectScreenAccess::IsAbortRequired(const TRegion& aTopVisibleRegion) const
   536 	{
   537 	return ( iStatus == EDirectStatusRunning ) && !RegionsMatch(aTopVisibleRegion, iVisible);
   538 	}
   539 
   540 void CWsDirectScreenAccess::Terminate1()
   541 	{
   542 	WS_ASSERT_DEBUG(!iWin->DSAs().IsEmpty(), EWsPanicDirectScreenAccess);
   543 	iWin->RemoveDSA(*this);
   544 	}
   545 
   546 void CWsDirectScreenAccess::Terminate2()
   547 	{
   548 	iScreen->RemoveDirect(*this);
   549 	WS_ASSERT_DEBUG(!OnQueue(), EWsPanicDirectScreenAccess);
   550 	}
   551 
   552 void CWsDirectScreenAccess::CorrectScreen()
   553 	{
   554 	if (iAbortReason<=RDirectScreenAccess::ETerminateRegion)
   555 		{
   556 		Terminate2();
   557 		RootWindow()->Invalidate(&iVisible);
   558 		}
   559 	else
   560 		RootWindow()->InvalidateWholeScreen();
   561 
   562 	WS_ASSERT_DEBUG(!OnQueue(), EWsPanicDirectScreenAccess);
   563 	iScreen->UpdateDsa();
   564 	}
   565 
   566 #if defined(_DEBUG)
   567 TBool CWsDirectScreenAccess::OnQueue()
   568 	{
   569 	return iScreen->IsDirectOnQueue(this);
   570 	}
   571 #endif