os/graphics/windowing/windowserver/nga/remotegc/DrawableCache.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2008-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 #include "DrawableCache.h"
    17 #include <graphics/wsdrawablesourceprovider.h>
    18 #include <graphics/wsdrawresource.h>
    19 #include "graphicsresourcewrapper.h"
    20 
    21 
    22 const TAny* CDrawableCacheBase::Resolve(const TSgDrawableId& aDrawableId, TInt aScreenNumber) const
    23 	{
    24 	TCacheEntry entry(aDrawableId, aScreenNumber);
    25 	TInt index = iCachedItems.FindInOrder(entry, TLinearOrder<TCacheEntry>(CDrawableCacheBase::Compare));
    26 	if (index != KErrNotFound)
    27 		{
    28 		return iCachedItems[index].iCachedItem;
    29 		}
    30 	return NULL;
    31 	}
    32 
    33 /**
    34 Compare two cache entries by drawable ID and screen number.
    35 
    36 @return zero, if the two objects are equal, a negative value, if the first entry is less 
    37 than the second, a positive value, if the first entry is greater than the second.
    38 */
    39 TInt CDrawableCacheBase::Compare(const TCacheEntry &aFirstEntry, const TCacheEntry &aSecondEntry)
    40 	{
    41 	TInt delta = Mem::Compare(reinterpret_cast<const TUint8*>(&aFirstEntry.iDrawableId), sizeof(TSgDrawableId),
    42 	                          reinterpret_cast<const TUint8*>(&aSecondEntry.iDrawableId), sizeof(TSgDrawableId));
    43 	if (delta != 0)
    44 		{
    45 		return delta;
    46 		}
    47 	return aFirstEntry.iScreenNumber - aSecondEntry.iScreenNumber;
    48 	}
    49 
    50 
    51 CWindowDrawableCache::CWindowDrawableCache(RWsSession& aSession)
    52 	: iWsSession(aSession)
    53 	{}
    54 
    55 CWindowDrawableCache::~CWindowDrawableCache()
    56 	{
    57 	delete iGraphicsResource;
    58 	delete iGrwFactory;
    59 	for (TInt ii=0;ii<iCachedItems.Count();++ii)
    60 		{
    61 		RWsDrawableSource* cachedItem=static_cast<RWsDrawableSource*>(iCachedItems[ii].iCachedItem);
    62 		cachedItem->Close();
    63 		delete cachedItem;
    64 		}
    65 	iCachedItems.Close();
    66 	}
    67 
    68 struct TCloseDrawableData
    69 	{
    70 public:
    71 	TCloseDrawableData(CGraphicsResourceWrapper& aGraphicsResource, RSgDrawable& aDrawable)
    72 		: iGraphicsResource(aGraphicsResource), iDrawable(aDrawable) {}
    73 	CGraphicsResourceWrapper& iGraphicsResource;
    74 	RSgDrawable& iDrawable;
    75 	};
    76 
    77 void CloseDrawable(TAny* aCleanupData)
    78 	{
    79 	TCloseDrawableData* data = static_cast<TCloseDrawableData*>(aCleanupData);
    80 	data->iGraphicsResource.Close(data->iDrawable);
    81 	}
    82 
    83 TInt CWindowDrawableCache::UseL(const TSgDrawableId& aDrawableId, TInt aScreenNumber)
    84 	{
    85 	if(!iGraphicsResource)
    86 		{
    87 		if (!iGrwFactory)
    88 			iGrwFactory = new (ELeave) CGraphicsResourceWrapperFactory;
    89 		iGraphicsResource = iGrwFactory->NewGraphicsResourceWrapper();
    90 		if(!iGraphicsResource)
    91 			User::Leave(KErrNotSupported);
    92 		}
    93 	TCacheEntry entry(aDrawableId, aScreenNumber);
    94 	TInt index = iCachedItems.FindInOrder(entry, TLinearOrder<TCacheEntry>(CDrawableCacheBase::Compare));
    95 	if (index != KErrNotFound)
    96 		{
    97 		return KErrNone;
    98 		}
    99 	RSgDrawable* drawable = iGraphicsResource->NewDrawableL();
   100 	CleanupStack::PushL(drawable);
   101 	TInt err = iGraphicsResource->Open(*drawable, aDrawableId);
   102 	if (err != KErrNone)
   103 		{
   104 		if (err == KErrNoMemory)
   105 			{
   106 			User::LeaveNoMemory();
   107 			}
   108 		return err;
   109 		}
   110 	TCloseDrawableData cleanupData(*iGraphicsResource, *drawable);
   111 	CleanupStack::PushL(TCleanupItem(CloseDrawable, &cleanupData));
   112 	RWsDrawableSource* cachedItem = new(ELeave) RWsDrawableSource(iWsSession);
   113 	err = cachedItem->Create(*drawable, aScreenNumber);
   114 	CleanupStack::PopAndDestroy(); //CloseDrawable()
   115 	CleanupStack::PopAndDestroy(drawable);
   116 	if (err != KErrNone)
   117 		{
   118 		delete cachedItem;
   119 		if (err == KErrNoMemory)
   120 			{
   121 			User::LeaveNoMemory();
   122 			}
   123 		return err;
   124 		}
   125 	entry.iCachedItem = cachedItem;
   126 	err = iCachedItems.InsertInOrder(entry, TLinearOrder<TCacheEntry>(CDrawableCacheBase::Compare));
   127 	if (err != KErrNone)
   128 		{
   129 		cachedItem->Close();
   130 		delete cachedItem;
   131 		User::Leave(err);
   132 		}
   133 	return KErrNone;
   134 	}
   135 
   136 
   137 CRenderStageDrawableCache::CRenderStageDrawableCache(MWsDrawableSourceProvider* aDrawResource)
   138 	: iDrawResource(aDrawResource)
   139 	{
   140 	}
   141 
   142 CRenderStageDrawableCache::~CRenderStageDrawableCache()
   143 	{
   144 	for (TInt i = 0; i < iCachedItems.Count(); ++i)
   145 		{
   146 		iDrawResource->CloseDrawableSource(iCachedItems[i].iCachedItem);
   147 		}
   148 	iCachedItems.Close();
   149 	}
   150 
   151 TInt CRenderStageDrawableCache::UseL(const TSgDrawableId& aDrawableId, TInt aScreenNumber)
   152 	{
   153 	if (!iDrawResource)
   154 		{
   155 		return KErrGeneral;
   156 		}
   157 	TCacheEntry entry(aDrawableId, aScreenNumber);
   158 	TInt index = iCachedItems.FindInOrder(entry, TLinearOrder<TCacheEntry>(CDrawableCacheBase::Compare));
   159 	if (index != KErrNotFound)
   160 		{
   161 		return KErrNone;
   162 		}
   163 	TInt err = iDrawResource->CreateDrawableSource(aDrawableId, entry.iCachedItem);
   164 	if (err != KErrNone)
   165 		{
   166 		if (err == KErrNoMemory)
   167 			{
   168 			User::LeaveNoMemory();
   169 			}
   170 		return err;
   171 		}
   172 	err = iCachedItems.InsertInOrder(entry, TLinearOrder<TCacheEntry>(CDrawableCacheBase::Compare));
   173 	if (err != KErrNone)
   174 		{
   175 		iDrawResource->CloseDrawableSource(entry.iCachedItem);
   176 		User::Leave(err);
   177 		}
   178 	return KErrNone;
   179 	}