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