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