os/graphics/graphicsresourceservices/graphicsresourceimplementation/src/sgchannel.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) 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
// Graphics Resource - logical channel implementation
sl@0
    15
//
sl@0
    16
sl@0
    17
#include <kernel/kern_priv.h>
sl@0
    18
#include <sgresource/sgextension.h>
sl@0
    19
#include "sgdeviceimpl.h"
sl@0
    20
sl@0
    21
TInt DSgChannel::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& aVersion)
sl@0
    22
	{
sl@0
    23
	TVersion version = SgExtension::Version();
sl@0
    24
	if (aVersion.iMajor != version.iMajor || aVersion.iMinor > version.iMinor)
sl@0
    25
		{
sl@0
    26
		return KErrNotSupported;
sl@0
    27
		}
sl@0
    28
	return Kern::MutexCreate(iMutex, KNullDesC, KMutexOrdGeneral7);
sl@0
    29
	}
sl@0
    30
sl@0
    31
DSgChannel::~DSgChannel()
sl@0
    32
	{
sl@0
    33
	if (iMutex)
sl@0
    34
		{
sl@0
    35
		iMutex->Close(NULL);
sl@0
    36
		}
sl@0
    37
	for (TInt i = 0; i < iResources.Count(); ++i)
sl@0
    38
		{
sl@0
    39
		iResources[i].iResource->Close();
sl@0
    40
		}
sl@0
    41
	iResources.Close();
sl@0
    42
	}
sl@0
    43
sl@0
    44
TInt DSgChannel::Request(TInt aFunction, TAny* a1, TAny* a2)
sl@0
    45
	{
sl@0
    46
	switch (aFunction)
sl@0
    47
		{
sl@0
    48
	case RSgDevice::EControlCreateResource:
sl@0
    49
		{
sl@0
    50
		RSgDevice::TCreateResourceArgs args;
sl@0
    51
		kumemget32(&args, a1, sizeof(RSgDevice::TCreateResourceArgs));
sl@0
    52
		TInt metaDataSize, maxMetaDataSize;
sl@0
    53
		Kern::KUDesInfo(*args.iMetaData, metaDataSize, maxMetaDataSize);
sl@0
    54
		__ASSERT_ALWAYS(metaDataSize <= KSgMaxMetaDataSize, Panic(ESgPanicMetaDataSizeTooBig));
sl@0
    55
		__ASSERT_ALWAYS(args.iDataSize >= 0, Panic(ESgPanicDataSizeNegative));
sl@0
    56
		TBuf8<KSgMaxMetaDataSize> metaData;
sl@0
    57
		Kern::KUDesGet(metaData, *args.iMetaData);
sl@0
    58
		TUint64 id;
sl@0
    59
		TInt err = CreateResource(args.iAttributes, metaData, args.iDataSize, id);
sl@0
    60
		kumemput32(a2, &id, sizeof(TUint64));
sl@0
    61
		return err;
sl@0
    62
		}
sl@0
    63
	case RSgDevice::EControlOpenResource:
sl@0
    64
		{
sl@0
    65
		TUint64 id;
sl@0
    66
		kumemget32(&id, a1, sizeof(TUint64));
sl@0
    67
		return OpenResource(id);
sl@0
    68
		}
sl@0
    69
	case RSgDevice::EControlCloseResource:
sl@0
    70
		{
sl@0
    71
		TUint64 id;
sl@0
    72
		kumemget32(&id, a1, sizeof(TUint64));
sl@0
    73
		return CloseResource(id);
sl@0
    74
		}
sl@0
    75
	case RSgDevice::EControlResourceAttributes:
sl@0
    76
		{
sl@0
    77
		TUint64 id;
sl@0
    78
		kumemget32(&id, a1, sizeof(TUint64));
sl@0
    79
		return static_cast<TInt>(ResourceAttributes(id));
sl@0
    80
		}
sl@0
    81
	case RSgDevice::EControlGetResourceMetaData:
sl@0
    82
		{
sl@0
    83
		TUint64 id;
sl@0
    84
		kumemget32(&id, a1, sizeof(TUint64));
sl@0
    85
		TBuf8<KSgMaxMetaDataSize> metaData;
sl@0
    86
		TInt err = GetResourceMetaData(id, metaData);
sl@0
    87
		if (err == KErrNone)
sl@0
    88
			{
sl@0
    89
			Kern::InfoCopy(*static_cast<TDes8*>(a2), metaData);
sl@0
    90
			}
sl@0
    91
		return err;
sl@0
    92
		}
sl@0
    93
	case RSgDevice::EControlResourceDataAddress:
sl@0
    94
		{
sl@0
    95
		TUint64 id;
sl@0
    96
		kumemget32(&id, a1, sizeof(TUint64));
sl@0
    97
		return reinterpret_cast<TInt>(ResourceDataAddress(id));
sl@0
    98
		}
sl@0
    99
	case RSgDevice::EControlResourceDataSize:
sl@0
   100
		{
sl@0
   101
		TUint64 id;
sl@0
   102
		kumemget32(&id, a1, sizeof(TUint64));
sl@0
   103
		return ResourceDataSize(id);
sl@0
   104
		}
sl@0
   105
	case RSgDevice::EControlGlobalResourceCount:
sl@0
   106
		return GlobalResourceCount();
sl@0
   107
	case RSgDevice::EControlLocalGraphicsMemoryUsed:
sl@0
   108
		return LocalGraphicsMemoryUsed();
sl@0
   109
	case RSgDevice::EControlGlobalGraphicsMemoryUsed:
sl@0
   110
		return GlobalGraphicsMemoryUsed();
sl@0
   111
	default:
sl@0
   112
		return KErrNotSupported;
sl@0
   113
		}
sl@0
   114
	}
sl@0
   115
sl@0
   116
TInt DSgChannel::CreateResource(TUint32 aAttribs, const TDesC8& aMetaData, TInt aDataSize, TUint64& aId)
sl@0
   117
	{
sl@0
   118
	aId = 0;
sl@0
   119
	NKern::ThreadEnterCS();
sl@0
   120
	DSgResource* resource;
sl@0
   121
	TInt err = SgExtension::CreateResource(aAttribs, aMetaData, aDataSize, resource);
sl@0
   122
	if (err != KErrNone)
sl@0
   123
		{
sl@0
   124
		NKern::ThreadLeaveCS();
sl@0
   125
		return err;
sl@0
   126
		}
sl@0
   127
	TInt handle = Kern::MakeHandleAndOpen(NULL, resource->DataChunk(), EOwnerProcess);
sl@0
   128
	if (handle < 0)
sl@0
   129
		{
sl@0
   130
		resource->Close();
sl@0
   131
		NKern::ThreadLeaveCS();
sl@0
   132
		return handle;
sl@0
   133
		}
sl@0
   134
	Kern::MutexWait(*iMutex);
sl@0
   135
	err = iResources.InsertInOrder(TResourceListItem(resource, handle), Compare);
sl@0
   136
	Kern::MutexSignal(*iMutex);
sl@0
   137
	if (err != KErrNone)
sl@0
   138
		{
sl@0
   139
		(void)Kern::CloseHandle(NULL, handle);
sl@0
   140
		resource->Close();
sl@0
   141
		NKern::ThreadLeaveCS();
sl@0
   142
		return err;
sl@0
   143
		}
sl@0
   144
	NKern::ThreadLeaveCS();
sl@0
   145
	aId = resource->Id();
sl@0
   146
	return KErrNone;
sl@0
   147
	}
sl@0
   148
sl@0
   149
TInt DSgChannel::OpenResource(TUint64 aId)
sl@0
   150
	{
sl@0
   151
	if (aId == 0)
sl@0
   152
		{
sl@0
   153
		return KErrArgument;
sl@0
   154
		}
sl@0
   155
	NKern::ThreadEnterCS();
sl@0
   156
	Kern::MutexWait(*iMutex);
sl@0
   157
	TInt i = iResources.FindInOrder(aId, Compare);
sl@0
   158
	if (i >= 0)
sl@0
   159
		{
sl@0
   160
		Kern::MutexSignal(*iMutex);
sl@0
   161
		NKern::ThreadLeaveCS();
sl@0
   162
		return KErrAlreadyExists;
sl@0
   163
		}
sl@0
   164
	DSgResource* resource;
sl@0
   165
	TInt err = SgExtension::FindAndOpenResource(aId, resource);
sl@0
   166
	if (err != KErrNone)
sl@0
   167
		{
sl@0
   168
		Kern::MutexSignal(*iMutex);
sl@0
   169
		NKern::ThreadLeaveCS();
sl@0
   170
		return err;
sl@0
   171
		}
sl@0
   172
	TInt handle = Kern::MakeHandleAndOpen(NULL, resource->DataChunk(), EOwnerProcess);
sl@0
   173
	if (handle < 0)
sl@0
   174
		{
sl@0
   175
		resource->Close();
sl@0
   176
		Kern::MutexSignal(*iMutex);
sl@0
   177
		NKern::ThreadLeaveCS();
sl@0
   178
		return handle;
sl@0
   179
		}
sl@0
   180
	err = iResources.InsertInOrder(TResourceListItem(resource, handle), Compare);
sl@0
   181
	if (err != KErrNone)
sl@0
   182
		{
sl@0
   183
		(void)Kern::CloseHandle(NULL, handle);
sl@0
   184
		resource->Close();
sl@0
   185
		Kern::MutexSignal(*iMutex);
sl@0
   186
		NKern::ThreadLeaveCS();
sl@0
   187
		return err;
sl@0
   188
		}
sl@0
   189
	Kern::MutexSignal(*iMutex);
sl@0
   190
	NKern::ThreadLeaveCS();
sl@0
   191
	return KErrNone;
sl@0
   192
	}
sl@0
   193
sl@0
   194
TInt DSgChannel::CloseResource(TUint64 aId)
sl@0
   195
	{
sl@0
   196
	if (aId == 0)
sl@0
   197
		{
sl@0
   198
		return KErrArgument;
sl@0
   199
		}
sl@0
   200
	TInt err = KErrNotFound;
sl@0
   201
	NKern::ThreadEnterCS();
sl@0
   202
	Kern::MutexWait(*iMutex);
sl@0
   203
	TInt i = iResources.FindInOrder(aId, Compare);
sl@0
   204
	if (i >= 0)
sl@0
   205
		{
sl@0
   206
		(void)Kern::CloseHandle(NULL, iResources[i].iChunkHandle);
sl@0
   207
		iResources[i].iResource->Close();
sl@0
   208
		iResources.Remove(i);
sl@0
   209
		err = KErrNone;
sl@0
   210
		}
sl@0
   211
	Kern::MutexSignal(*iMutex);
sl@0
   212
	NKern::ThreadLeaveCS();
sl@0
   213
	return err;
sl@0
   214
	}
sl@0
   215
sl@0
   216
TUint32 DSgChannel::ResourceAttributes(TUint64 aId) const
sl@0
   217
	{
sl@0
   218
	if (aId == 0)
sl@0
   219
		{
sl@0
   220
		return 0;
sl@0
   221
		}
sl@0
   222
	TUint32 attribs = 0;
sl@0
   223
	NKern::ThreadEnterCS();
sl@0
   224
	Kern::MutexWait(*iMutex);
sl@0
   225
	TInt i = iResources.FindInOrder(aId, Compare);
sl@0
   226
	if (i >= 0)
sl@0
   227
		{
sl@0
   228
		attribs = iResources[i].iResource->Attributes();
sl@0
   229
		}
sl@0
   230
	Kern::MutexSignal(*iMutex);
sl@0
   231
	NKern::ThreadLeaveCS();
sl@0
   232
	return attribs;
sl@0
   233
	}
sl@0
   234
sl@0
   235
TInt DSgChannel::GetResourceMetaData(TUint64 aId, TDes8& aMetaData) const
sl@0
   236
	{
sl@0
   237
	if (aId == 0)
sl@0
   238
		{
sl@0
   239
		return KErrArgument;
sl@0
   240
		}
sl@0
   241
	TInt err = KErrNotFound;
sl@0
   242
	NKern::ThreadEnterCS();
sl@0
   243
	Kern::MutexWait(*iMutex);
sl@0
   244
	TInt i = iResources.FindInOrder(aId, Compare);
sl@0
   245
	if (i >= 0)
sl@0
   246
		{
sl@0
   247
		err = iResources[i].iResource->GetMetaData(aMetaData);
sl@0
   248
		}
sl@0
   249
	Kern::MutexSignal(*iMutex);
sl@0
   250
	NKern::ThreadLeaveCS();
sl@0
   251
	return err;
sl@0
   252
	}
sl@0
   253
sl@0
   254
TAny* DSgChannel::ResourceDataAddress(TUint64 aId) const
sl@0
   255
	{
sl@0
   256
	if (aId == 0)
sl@0
   257
		{
sl@0
   258
		return NULL;
sl@0
   259
		}
sl@0
   260
	TAny* addr = NULL;
sl@0
   261
	NKern::ThreadEnterCS();
sl@0
   262
	Kern::MutexWait(*iMutex);
sl@0
   263
	TInt i = iResources.FindInOrder(aId, Compare);
sl@0
   264
	if (i >= 0)
sl@0
   265
		{
sl@0
   266
		addr = Kern::ChunkUserBase(iResources[i].iResource->DataChunk(), &Kern::CurrentThread());
sl@0
   267
		}
sl@0
   268
	Kern::MutexSignal(*iMutex);
sl@0
   269
	NKern::ThreadLeaveCS();
sl@0
   270
	return addr;
sl@0
   271
	}
sl@0
   272
sl@0
   273
TInt DSgChannel::ResourceDataSize(TUint64 aId) const
sl@0
   274
	{
sl@0
   275
	if (aId == 0)
sl@0
   276
		{
sl@0
   277
		return KErrArgument;
sl@0
   278
		}
sl@0
   279
	TInt ret = KErrNotFound;
sl@0
   280
	NKern::ThreadEnterCS();
sl@0
   281
	Kern::MutexWait(*iMutex);
sl@0
   282
	TInt i = iResources.FindInOrder(aId, Compare);
sl@0
   283
	if (i >= 0)
sl@0
   284
		{
sl@0
   285
		ret = iResources[i].iResource->DataSize();
sl@0
   286
		}
sl@0
   287
	Kern::MutexSignal(*iMutex);
sl@0
   288
	NKern::ThreadLeaveCS();
sl@0
   289
	return ret;
sl@0
   290
	}
sl@0
   291
sl@0
   292
TInt DSgChannel::GlobalResourceCount() const
sl@0
   293
	{
sl@0
   294
	NKern::ThreadEnterCS();
sl@0
   295
	TInt ret = SgExtension::GlobalResourceCount();
sl@0
   296
	NKern::ThreadLeaveCS();
sl@0
   297
	return ret;
sl@0
   298
	}
sl@0
   299
sl@0
   300
TInt DSgChannel::LocalGraphicsMemoryUsed() const
sl@0
   301
	{
sl@0
   302
	TInt ret = 0;
sl@0
   303
	NKern::ThreadEnterCS();
sl@0
   304
	Kern::MutexWait(*iMutex);
sl@0
   305
	TInt n = iResources.Count();
sl@0
   306
	for (TInt i = 0; i < n; ++i)
sl@0
   307
		{
sl@0
   308
		ret += iResources[i].iResource->DataChunk()->Size();
sl@0
   309
		}
sl@0
   310
	Kern::MutexSignal(*iMutex);
sl@0
   311
	NKern::ThreadLeaveCS();
sl@0
   312
	return ret;
sl@0
   313
	}
sl@0
   314
sl@0
   315
TInt DSgChannel::GlobalGraphicsMemoryUsed() const
sl@0
   316
	{
sl@0
   317
	NKern::ThreadEnterCS();
sl@0
   318
	TInt ret = SgExtension::GlobalGraphicsMemoryUsed();
sl@0
   319
	NKern::ThreadLeaveCS();
sl@0
   320
	return ret;
sl@0
   321
	}
sl@0
   322
sl@0
   323
DSgChannel::TResourceListItem::TResourceListItem(DSgResource* aResource, TInt aChunkHandle)
sl@0
   324
	: iResource(aResource), iChunkHandle(aChunkHandle)
sl@0
   325
	{
sl@0
   326
	}
sl@0
   327
sl@0
   328
TInt DSgChannel::Compare(const TUint64* aId, const TResourceListItem& aResourceListItem)
sl@0
   329
	{
sl@0
   330
	return DSgResource::Compare(aId, *aResourceListItem.iResource);
sl@0
   331
	}
sl@0
   332
sl@0
   333
TInt DSgChannel::Compare(const TResourceListItem& aResourceListItem1, const TResourceListItem& aResourceListItem2)
sl@0
   334
	{
sl@0
   335
	return DSgResource::Compare(*aResourceListItem1.iResource, *aResourceListItem2.iResource);
sl@0
   336
	}