windowing/windowserver/nga/SERVER/REDRAWQ.CPP
changeset 0 5d03bc08d59c
child 33 25f95128741d
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 1996-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 //
       
    15 
       
    16 // Window server redraw queue handling
       
    17 //
       
    18 #include <e32std.h>
       
    19 #include "server.h"
       
    20 #include "rootwin.h"
       
    21 #include "windowgroup.h"
       
    22 #include "walkwindowtree.h"
       
    23 #include "EVQUEUE.H"
       
    24 #include "panics.h"
       
    25 #include "wstop.h"
       
    26 
       
    27 const TInt KGranularity = 10;
       
    28 
       
    29 void TWsRedrawEvent::SetHandle(TUint aHandle)
       
    30 	{
       
    31 	iHandle=aHandle;
       
    32 	}
       
    33 
       
    34 void TWsRedrawEvent::SetRect(TRect aRect)
       
    35 	{
       
    36 	iRect=aRect;
       
    37 	}
       
    38 
       
    39 TWsRedrawEvent CRedrawQueue::iNullRedrawEvent;
       
    40 
       
    41 CRedrawQueue::CRedrawQueue(CWsClient *aOwner) : CEventBase(aOwner)
       
    42 	{
       
    43 	__DECLARE_NAME(_S("CRedrawQueue"));
       
    44 	}
       
    45 
       
    46 void CRedrawQueue::ConstructL()
       
    47 	{
       
    48 	iRedrawTrigger=EFalse;
       
    49 	iRedraws=new(ELeave) CArrayFixSeg<TRedraw>(KGranularity);
       
    50 	iKeyPriority=new(ELeave) TKeyArrayFix(_FOFF(TRedraw,iPriority),ECmpTUint32);
       
    51 	iKeyWindow=new(ELeave) TKeyArrayFix(_FOFF(TRedraw,iRedraw),ECmpTUint32);
       
    52 	Mem::FillZ(&iNullRedrawEvent,sizeof(iNullRedrawEvent));
       
    53 	}
       
    54 
       
    55 CRedrawQueue::~CRedrawQueue()
       
    56 	{
       
    57 	delete iRedraws;
       
    58 	delete iKeyPriority;
       
    59 	delete iKeyWindow;
       
    60 	}
       
    61 
       
    62 void CRedrawQueue::ReCalcOrder()
       
    63 	{
       
    64 	const TInt redrawsCount=iRedraws->Count();
       
    65 	for(TInt index=0;index<redrawsCount;index++)
       
    66 		{
       
    67 		TRedraw *redraw=&iRedraws->At(index);
       
    68 		redraw->iPriority=static_cast<CWsClientWindow*>(redraw->iRedraw->WsWin())->RedrawPriority();
       
    69 		}
       
    70 	iRedraws->Sort(*iKeyPriority);
       
    71 	}
       
    72 
       
    73 void CRedrawQueue::AddInvalid(CWsWindowRedraw *aRedrawWin) 
       
    74 //
       
    75 // Add a window to the update list.
       
    76 //
       
    77 	{
       
    78 	TInt index;
       
    79 	TRedraw redraw;
       
    80 
       
    81 	redraw.iRedraw=aRedrawWin;
       
    82 	if (iRedraws->Find(redraw,*iKeyWindow,index)!=0)
       
    83 		{
       
    84 		redraw.iPriority=static_cast<CWsClientWindow*>(aRedrawWin->WsWin())->RedrawPriority();
       
    85 		TRAPD(err,iRedraws->InsertIsqAllowDuplicatesL(redraw,*iKeyPriority));
       
    86 		if (err!=KErrNone)
       
    87 			{
       
    88 			WS_ASSERT_DEBUG(err==KErrNoMemory,EWsPanicRedrawQueueError);
       
    89 			iAllocError=ETrue;
       
    90 			}
       
    91 		iRedrawTrigger=ETrue;
       
    92 		}
       
    93 	}
       
    94 
       
    95 void CRedrawQueue::DeleteFromQueue(TInt aIndex)
       
    96 	{
       
    97 	iRedraws->Delete(aIndex,1);
       
    98 
       
    99 	//We are certain we will need iRedraws again, so it would be silly to compress away the last KGranularity slots.
       
   100 	const TInt count = iRedraws->Count();
       
   101 	if((count >= KGranularity) && (count % KGranularity == 0))
       
   102 		{
       
   103 		iRedraws->Compress();
       
   104 		}
       
   105 	}
       
   106 
       
   107 void CRedrawQueue::RemoveInvalid(CWsWindowRedraw *aRedrawWin)
       
   108 //
       
   109 // remove the window from the invalid window list.
       
   110 // harmless to call if the window is not in the list.
       
   111 //
       
   112 	{
       
   113 	TInt index;
       
   114 	TRedraw redraw;
       
   115 
       
   116 	redraw.iRedraw=aRedrawWin;
       
   117 	redraw.iPriority=0;
       
   118 	if ((iRedraws->Find(redraw,*iKeyWindow,index))==KErrNone)
       
   119 		DeleteFromQueue(index);
       
   120 	}
       
   121 
       
   122 TBool CRedrawQueue::TriggerRedraw()
       
   123 //
       
   124 // Trigger any pending redraw messages in the queue
       
   125 // Returns ETrue if a redraw was sent, EFalse if not.
       
   126 //
       
   127 	{
       
   128 	TBool ret=EFalse;
       
   129 	if (iRedrawTrigger)
       
   130 		{
       
   131 		iRedrawTrigger=EFalse;
       
   132 		if (!iEventMsg.IsNull() && (iRedraws->Count()>0 || iAllocError))
       
   133 			{
       
   134 			SignalEvent();
       
   135 			ret=ETrue;
       
   136 			}
       
   137 		}
       
   138 	return(ret);
       
   139 	}
       
   140 
       
   141 void CRedrawQueue::EventReady(const RMessagePtr2& aEventMsg)
       
   142 //
       
   143 // Queue a read of an event from the queue
       
   144 //
       
   145 	{
       
   146 	CEventBase::EventReady(aEventMsg);
       
   147 	iRedrawTrigger=ETrue;
       
   148 	TriggerRedraw();
       
   149 	}
       
   150 
       
   151 TBool CRedrawQueue::FindOutstandingRedrawEvent(CWsWindowRedraw& aRedraw, TWsRedrawEvent& aEvent)
       
   152 	{
       
   153 	TRect rect;
       
   154 	if (aRedraw.GetRedrawRect(rect))
       
   155 		{
       
   156 		aEvent.SetRect(rect);
       
   157 		aEvent.SetHandle(aRedraw.WsWin()->ClientHandle());
       
   158 		CEventBase::GetData(&aEvent,sizeof(aEvent));
       
   159 		return ETrue;
       
   160 		}
       
   161 	return EFalse;
       
   162 	}
       
   163 
       
   164 TBool CRedrawQueue::FindWindowNeedingRedrawEvent(TWsRedrawEvent& aEvent)
       
   165 	{
       
   166 #if defined(_DEBUG)
       
   167 	CWsWindowRedraw* previousRedraw = NULL;
       
   168 #endif
       
   169 	// search all screens
       
   170 	TInt invalidWindows = 0;
       
   171 	for (TInt screenNo = 0; screenNo < CWsTop::NumberOfScreens(); ++screenNo)
       
   172 		{
       
   173 		CWsRootWindow* rootWindow = CWsTop::Screen(screenNo)->RootWindow();
       
   174 		for (CWsWindowGroup *groupWin = rootWindow->Child(); groupWin; groupWin = groupWin->NextSibling())
       
   175 			{
       
   176 			if (groupWin->WsOwner() == iWsOwner)
       
   177 				{
       
   178 				CWsWindowRedraw* redrawWin = NULL;
       
   179 				// use a window tree walk that can be resumed to find windows with an invalid region
       
   180 				TResumableWalkWindowTreeFindInvalid wwt(&redrawWin);
       
   181 				groupWin->WalkWindowTree(wwt, EWalkChildren, EFalse);
       
   182 				while (redrawWin != NULL)
       
   183 					{
       
   184 					WS_ASSERT_DEBUG(redrawWin != previousRedraw, EWsPanicRedrawQueueError);
       
   185 					// (the window may not actually need the client to redraw it, e.g. a CWsBlankWindow can redraw itself)
       
   186 					if (FindOutstandingRedrawEvent(*redrawWin, aEvent))
       
   187 						{
       
   188 						return ETrue;
       
   189 						}
       
   190 					else
       
   191 						{ // continue the Tree Walk
       
   192 #if defined(_DEBUG)
       
   193 						previousRedraw = redrawWin;
       
   194 #endif
       
   195 						if (redrawWin->NeedsRedraw())
       
   196 							{ // needs to be redrawn later?
       
   197 							++invalidWindows;
       
   198 							}
       
   199 						redrawWin = NULL;
       
   200 						groupWin->WalkWindowTree(wwt, EWalkChildren, ETrue);
       
   201 						}
       
   202 					}
       
   203 				}
       
   204 			}
       
   205 		}
       
   206 
       
   207 	if (invalidWindows == 0)
       
   208 		{ // error recovery is complete
       
   209 		iAllocError = 0;
       
   210 		}
       
   211 	return EFalse;
       
   212 	}
       
   213 
       
   214 void CRedrawQueue::GetData()
       
   215 //
       
   216 // If there is an outstanding redraw event in the queue, reply with it's data
       
   217 //
       
   218 	{
       
   219 	CWsWindowRedraw *redraw;
       
   220 	TWsRedrawEvent event;
       
   221 
       
   222 	while (iRedraws->Count()>0)
       
   223 		{
       
   224 		redraw=(*iRedraws)[0].iRedraw;
       
   225 		if (FindOutstandingRedrawEvent(*redraw,event))
       
   226 			{
       
   227 			return;
       
   228 			}
       
   229 		TInt toDelete=0;
       
   230 		if (redraw!=(*iRedraws)[0].iRedraw)
       
   231 			{		//In low memory conditions calls to FindOutstandingRedrawEvent can cause extra entries to be added to the array
       
   232 			TRedraw redrawFind;
       
   233 			redrawFind.iRedraw=redraw;
       
   234 			iRedraws->Find(redrawFind,*iKeyWindow,toDelete);
       
   235 			}
       
   236 		DeleteFromQueue(toDelete);
       
   237 		}
       
   238 
       
   239 	if (iAllocError && FindWindowNeedingRedrawEvent(event))
       
   240 		{
       
   241 		return;
       
   242 		}
       
   243 
       
   244 	CEventBase::GetData(&iNullRedrawEvent,sizeof(iNullRedrawEvent));
       
   245 	}
       
   246 
       
   247 TUint CRedrawQueue::RedrawPriority(CWsWindowRedraw *aRedrawWin)
       
   248 	{
       
   249 	TInt index;
       
   250 	TRedraw redraw;
       
   251 	TUint priority=0;
       
   252 	redraw.iRedraw=aRedrawWin;
       
   253 
       
   254 	if ((iRedraws->Find(redraw,*iKeyWindow,index))==KErrNone)
       
   255 		{
       
   256 		priority=iRedraws->At(index).iPriority;
       
   257 		}
       
   258 	return priority;
       
   259 	}