1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/graphics/windowing/windowserver/nga/remotegc/DrawableCache.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,179 @@
1.4 +// Copyright (c) 2008-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 "DrawableCache.h"
1.20 +#include <graphics/wsdrawablesourceprovider.h>
1.21 +#include <graphics/wsdrawresource.h>
1.22 +#include "graphicsresourcewrapper.h"
1.23 +
1.24 +
1.25 +const TAny* CDrawableCacheBase::Resolve(const TSgDrawableId& aDrawableId, TInt aScreenNumber) const
1.26 + {
1.27 + TCacheEntry entry(aDrawableId, aScreenNumber);
1.28 + TInt index = iCachedItems.FindInOrder(entry, TLinearOrder<TCacheEntry>(CDrawableCacheBase::Compare));
1.29 + if (index != KErrNotFound)
1.30 + {
1.31 + return iCachedItems[index].iCachedItem;
1.32 + }
1.33 + return NULL;
1.34 + }
1.35 +
1.36 +/**
1.37 +Compare two cache entries by drawable ID and screen number.
1.38 +
1.39 +@return zero, if the two objects are equal, a negative value, if the first entry is less
1.40 +than the second, a positive value, if the first entry is greater than the second.
1.41 +*/
1.42 +TInt CDrawableCacheBase::Compare(const TCacheEntry &aFirstEntry, const TCacheEntry &aSecondEntry)
1.43 + {
1.44 + TInt delta = Mem::Compare(reinterpret_cast<const TUint8*>(&aFirstEntry.iDrawableId), sizeof(TSgDrawableId),
1.45 + reinterpret_cast<const TUint8*>(&aSecondEntry.iDrawableId), sizeof(TSgDrawableId));
1.46 + if (delta != 0)
1.47 + {
1.48 + return delta;
1.49 + }
1.50 + return aFirstEntry.iScreenNumber - aSecondEntry.iScreenNumber;
1.51 + }
1.52 +
1.53 +
1.54 +CWindowDrawableCache::CWindowDrawableCache(RWsSession& aSession)
1.55 + : iWsSession(aSession)
1.56 + {}
1.57 +
1.58 +CWindowDrawableCache::~CWindowDrawableCache()
1.59 + {
1.60 + delete iGraphicsResource;
1.61 + delete iGrwFactory;
1.62 + for (TInt ii=0;ii<iCachedItems.Count();++ii)
1.63 + {
1.64 + RWsDrawableSource* cachedItem=static_cast<RWsDrawableSource*>(iCachedItems[ii].iCachedItem);
1.65 + cachedItem->Close();
1.66 + delete cachedItem;
1.67 + }
1.68 + iCachedItems.Close();
1.69 + }
1.70 +
1.71 +struct TCloseDrawableData
1.72 + {
1.73 +public:
1.74 + TCloseDrawableData(CGraphicsResourceWrapper& aGraphicsResource, RSgDrawable& aDrawable)
1.75 + : iGraphicsResource(aGraphicsResource), iDrawable(aDrawable) {}
1.76 + CGraphicsResourceWrapper& iGraphicsResource;
1.77 + RSgDrawable& iDrawable;
1.78 + };
1.79 +
1.80 +void CloseDrawable(TAny* aCleanupData)
1.81 + {
1.82 + TCloseDrawableData* data = static_cast<TCloseDrawableData*>(aCleanupData);
1.83 + data->iGraphicsResource.Close(data->iDrawable);
1.84 + }
1.85 +
1.86 +TInt CWindowDrawableCache::UseL(const TSgDrawableId& aDrawableId, TInt aScreenNumber)
1.87 + {
1.88 + if(!iGraphicsResource)
1.89 + {
1.90 + if (!iGrwFactory)
1.91 + iGrwFactory = new (ELeave) CGraphicsResourceWrapperFactory;
1.92 + iGraphicsResource = iGrwFactory->NewGraphicsResourceWrapper();
1.93 + if(!iGraphicsResource)
1.94 + User::Leave(KErrNotSupported);
1.95 + }
1.96 + TCacheEntry entry(aDrawableId, aScreenNumber);
1.97 + TInt index = iCachedItems.FindInOrder(entry, TLinearOrder<TCacheEntry>(CDrawableCacheBase::Compare));
1.98 + if (index != KErrNotFound)
1.99 + {
1.100 + return KErrNone;
1.101 + }
1.102 + RSgDrawable* drawable = iGraphicsResource->NewDrawableL();
1.103 + CleanupStack::PushL(drawable);
1.104 + TInt err = iGraphicsResource->Open(*drawable, aDrawableId);
1.105 + if (err != KErrNone)
1.106 + {
1.107 + if (err == KErrNoMemory)
1.108 + {
1.109 + User::LeaveNoMemory();
1.110 + }
1.111 + return err;
1.112 + }
1.113 + TCloseDrawableData cleanupData(*iGraphicsResource, *drawable);
1.114 + CleanupStack::PushL(TCleanupItem(CloseDrawable, &cleanupData));
1.115 + RWsDrawableSource* cachedItem = new(ELeave) RWsDrawableSource(iWsSession);
1.116 + err = cachedItem->Create(*drawable, aScreenNumber);
1.117 + CleanupStack::PopAndDestroy(); //CloseDrawable()
1.118 + CleanupStack::PopAndDestroy(drawable);
1.119 + if (err != KErrNone)
1.120 + {
1.121 + delete cachedItem;
1.122 + if (err == KErrNoMemory)
1.123 + {
1.124 + User::LeaveNoMemory();
1.125 + }
1.126 + return err;
1.127 + }
1.128 + entry.iCachedItem = cachedItem;
1.129 + err = iCachedItems.InsertInOrder(entry, TLinearOrder<TCacheEntry>(CDrawableCacheBase::Compare));
1.130 + if (err != KErrNone)
1.131 + {
1.132 + cachedItem->Close();
1.133 + delete cachedItem;
1.134 + User::Leave(err);
1.135 + }
1.136 + return KErrNone;
1.137 + }
1.138 +
1.139 +
1.140 +CRenderStageDrawableCache::CRenderStageDrawableCache(MWsDrawableSourceProvider* aDrawResource)
1.141 + : iDrawResource(aDrawResource)
1.142 + {
1.143 + }
1.144 +
1.145 +CRenderStageDrawableCache::~CRenderStageDrawableCache()
1.146 + {
1.147 + for (TInt i = 0; i < iCachedItems.Count(); ++i)
1.148 + {
1.149 + iDrawResource->CloseDrawableSource(iCachedItems[i].iCachedItem);
1.150 + }
1.151 + iCachedItems.Close();
1.152 + }
1.153 +
1.154 +TInt CRenderStageDrawableCache::UseL(const TSgDrawableId& aDrawableId, TInt aScreenNumber)
1.155 + {
1.156 + if (!iDrawResource)
1.157 + {
1.158 + return KErrGeneral;
1.159 + }
1.160 + TCacheEntry entry(aDrawableId, aScreenNumber);
1.161 + TInt index = iCachedItems.FindInOrder(entry, TLinearOrder<TCacheEntry>(CDrawableCacheBase::Compare));
1.162 + if (index != KErrNotFound)
1.163 + {
1.164 + return KErrNone;
1.165 + }
1.166 + TInt err = iDrawResource->CreateDrawableSource(aDrawableId, entry.iCachedItem);
1.167 + if (err != KErrNone)
1.168 + {
1.169 + if (err == KErrNoMemory)
1.170 + {
1.171 + User::LeaveNoMemory();
1.172 + }
1.173 + return err;
1.174 + }
1.175 + err = iCachedItems.InsertInOrder(entry, TLinearOrder<TCacheEntry>(CDrawableCacheBase::Compare));
1.176 + if (err != KErrNone)
1.177 + {
1.178 + iDrawResource->CloseDrawableSource(entry.iCachedItem);
1.179 + User::Leave(err);
1.180 + }
1.181 + return KErrNone;
1.182 + }