os/graphics/graphicsresourceservices/graphicsresourceimplementation/src/sgchannel.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/graphics/graphicsresourceservices/graphicsresourceimplementation/src/sgchannel.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,336 @@
1.4 +// Copyright (c) 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 +// Graphics Resource - logical channel implementation
1.18 +//
1.19 +
1.20 +#include <kernel/kern_priv.h>
1.21 +#include <sgresource/sgextension.h>
1.22 +#include "sgdeviceimpl.h"
1.23 +
1.24 +TInt DSgChannel::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& aVersion)
1.25 + {
1.26 + TVersion version = SgExtension::Version();
1.27 + if (aVersion.iMajor != version.iMajor || aVersion.iMinor > version.iMinor)
1.28 + {
1.29 + return KErrNotSupported;
1.30 + }
1.31 + return Kern::MutexCreate(iMutex, KNullDesC, KMutexOrdGeneral7);
1.32 + }
1.33 +
1.34 +DSgChannel::~DSgChannel()
1.35 + {
1.36 + if (iMutex)
1.37 + {
1.38 + iMutex->Close(NULL);
1.39 + }
1.40 + for (TInt i = 0; i < iResources.Count(); ++i)
1.41 + {
1.42 + iResources[i].iResource->Close();
1.43 + }
1.44 + iResources.Close();
1.45 + }
1.46 +
1.47 +TInt DSgChannel::Request(TInt aFunction, TAny* a1, TAny* a2)
1.48 + {
1.49 + switch (aFunction)
1.50 + {
1.51 + case RSgDevice::EControlCreateResource:
1.52 + {
1.53 + RSgDevice::TCreateResourceArgs args;
1.54 + kumemget32(&args, a1, sizeof(RSgDevice::TCreateResourceArgs));
1.55 + TInt metaDataSize, maxMetaDataSize;
1.56 + Kern::KUDesInfo(*args.iMetaData, metaDataSize, maxMetaDataSize);
1.57 + __ASSERT_ALWAYS(metaDataSize <= KSgMaxMetaDataSize, Panic(ESgPanicMetaDataSizeTooBig));
1.58 + __ASSERT_ALWAYS(args.iDataSize >= 0, Panic(ESgPanicDataSizeNegative));
1.59 + TBuf8<KSgMaxMetaDataSize> metaData;
1.60 + Kern::KUDesGet(metaData, *args.iMetaData);
1.61 + TUint64 id;
1.62 + TInt err = CreateResource(args.iAttributes, metaData, args.iDataSize, id);
1.63 + kumemput32(a2, &id, sizeof(TUint64));
1.64 + return err;
1.65 + }
1.66 + case RSgDevice::EControlOpenResource:
1.67 + {
1.68 + TUint64 id;
1.69 + kumemget32(&id, a1, sizeof(TUint64));
1.70 + return OpenResource(id);
1.71 + }
1.72 + case RSgDevice::EControlCloseResource:
1.73 + {
1.74 + TUint64 id;
1.75 + kumemget32(&id, a1, sizeof(TUint64));
1.76 + return CloseResource(id);
1.77 + }
1.78 + case RSgDevice::EControlResourceAttributes:
1.79 + {
1.80 + TUint64 id;
1.81 + kumemget32(&id, a1, sizeof(TUint64));
1.82 + return static_cast<TInt>(ResourceAttributes(id));
1.83 + }
1.84 + case RSgDevice::EControlGetResourceMetaData:
1.85 + {
1.86 + TUint64 id;
1.87 + kumemget32(&id, a1, sizeof(TUint64));
1.88 + TBuf8<KSgMaxMetaDataSize> metaData;
1.89 + TInt err = GetResourceMetaData(id, metaData);
1.90 + if (err == KErrNone)
1.91 + {
1.92 + Kern::InfoCopy(*static_cast<TDes8*>(a2), metaData);
1.93 + }
1.94 + return err;
1.95 + }
1.96 + case RSgDevice::EControlResourceDataAddress:
1.97 + {
1.98 + TUint64 id;
1.99 + kumemget32(&id, a1, sizeof(TUint64));
1.100 + return reinterpret_cast<TInt>(ResourceDataAddress(id));
1.101 + }
1.102 + case RSgDevice::EControlResourceDataSize:
1.103 + {
1.104 + TUint64 id;
1.105 + kumemget32(&id, a1, sizeof(TUint64));
1.106 + return ResourceDataSize(id);
1.107 + }
1.108 + case RSgDevice::EControlGlobalResourceCount:
1.109 + return GlobalResourceCount();
1.110 + case RSgDevice::EControlLocalGraphicsMemoryUsed:
1.111 + return LocalGraphicsMemoryUsed();
1.112 + case RSgDevice::EControlGlobalGraphicsMemoryUsed:
1.113 + return GlobalGraphicsMemoryUsed();
1.114 + default:
1.115 + return KErrNotSupported;
1.116 + }
1.117 + }
1.118 +
1.119 +TInt DSgChannel::CreateResource(TUint32 aAttribs, const TDesC8& aMetaData, TInt aDataSize, TUint64& aId)
1.120 + {
1.121 + aId = 0;
1.122 + NKern::ThreadEnterCS();
1.123 + DSgResource* resource;
1.124 + TInt err = SgExtension::CreateResource(aAttribs, aMetaData, aDataSize, resource);
1.125 + if (err != KErrNone)
1.126 + {
1.127 + NKern::ThreadLeaveCS();
1.128 + return err;
1.129 + }
1.130 + TInt handle = Kern::MakeHandleAndOpen(NULL, resource->DataChunk(), EOwnerProcess);
1.131 + if (handle < 0)
1.132 + {
1.133 + resource->Close();
1.134 + NKern::ThreadLeaveCS();
1.135 + return handle;
1.136 + }
1.137 + Kern::MutexWait(*iMutex);
1.138 + err = iResources.InsertInOrder(TResourceListItem(resource, handle), Compare);
1.139 + Kern::MutexSignal(*iMutex);
1.140 + if (err != KErrNone)
1.141 + {
1.142 + (void)Kern::CloseHandle(NULL, handle);
1.143 + resource->Close();
1.144 + NKern::ThreadLeaveCS();
1.145 + return err;
1.146 + }
1.147 + NKern::ThreadLeaveCS();
1.148 + aId = resource->Id();
1.149 + return KErrNone;
1.150 + }
1.151 +
1.152 +TInt DSgChannel::OpenResource(TUint64 aId)
1.153 + {
1.154 + if (aId == 0)
1.155 + {
1.156 + return KErrArgument;
1.157 + }
1.158 + NKern::ThreadEnterCS();
1.159 + Kern::MutexWait(*iMutex);
1.160 + TInt i = iResources.FindInOrder(aId, Compare);
1.161 + if (i >= 0)
1.162 + {
1.163 + Kern::MutexSignal(*iMutex);
1.164 + NKern::ThreadLeaveCS();
1.165 + return KErrAlreadyExists;
1.166 + }
1.167 + DSgResource* resource;
1.168 + TInt err = SgExtension::FindAndOpenResource(aId, resource);
1.169 + if (err != KErrNone)
1.170 + {
1.171 + Kern::MutexSignal(*iMutex);
1.172 + NKern::ThreadLeaveCS();
1.173 + return err;
1.174 + }
1.175 + TInt handle = Kern::MakeHandleAndOpen(NULL, resource->DataChunk(), EOwnerProcess);
1.176 + if (handle < 0)
1.177 + {
1.178 + resource->Close();
1.179 + Kern::MutexSignal(*iMutex);
1.180 + NKern::ThreadLeaveCS();
1.181 + return handle;
1.182 + }
1.183 + err = iResources.InsertInOrder(TResourceListItem(resource, handle), Compare);
1.184 + if (err != KErrNone)
1.185 + {
1.186 + (void)Kern::CloseHandle(NULL, handle);
1.187 + resource->Close();
1.188 + Kern::MutexSignal(*iMutex);
1.189 + NKern::ThreadLeaveCS();
1.190 + return err;
1.191 + }
1.192 + Kern::MutexSignal(*iMutex);
1.193 + NKern::ThreadLeaveCS();
1.194 + return KErrNone;
1.195 + }
1.196 +
1.197 +TInt DSgChannel::CloseResource(TUint64 aId)
1.198 + {
1.199 + if (aId == 0)
1.200 + {
1.201 + return KErrArgument;
1.202 + }
1.203 + TInt err = KErrNotFound;
1.204 + NKern::ThreadEnterCS();
1.205 + Kern::MutexWait(*iMutex);
1.206 + TInt i = iResources.FindInOrder(aId, Compare);
1.207 + if (i >= 0)
1.208 + {
1.209 + (void)Kern::CloseHandle(NULL, iResources[i].iChunkHandle);
1.210 + iResources[i].iResource->Close();
1.211 + iResources.Remove(i);
1.212 + err = KErrNone;
1.213 + }
1.214 + Kern::MutexSignal(*iMutex);
1.215 + NKern::ThreadLeaveCS();
1.216 + return err;
1.217 + }
1.218 +
1.219 +TUint32 DSgChannel::ResourceAttributes(TUint64 aId) const
1.220 + {
1.221 + if (aId == 0)
1.222 + {
1.223 + return 0;
1.224 + }
1.225 + TUint32 attribs = 0;
1.226 + NKern::ThreadEnterCS();
1.227 + Kern::MutexWait(*iMutex);
1.228 + TInt i = iResources.FindInOrder(aId, Compare);
1.229 + if (i >= 0)
1.230 + {
1.231 + attribs = iResources[i].iResource->Attributes();
1.232 + }
1.233 + Kern::MutexSignal(*iMutex);
1.234 + NKern::ThreadLeaveCS();
1.235 + return attribs;
1.236 + }
1.237 +
1.238 +TInt DSgChannel::GetResourceMetaData(TUint64 aId, TDes8& aMetaData) const
1.239 + {
1.240 + if (aId == 0)
1.241 + {
1.242 + return KErrArgument;
1.243 + }
1.244 + TInt err = KErrNotFound;
1.245 + NKern::ThreadEnterCS();
1.246 + Kern::MutexWait(*iMutex);
1.247 + TInt i = iResources.FindInOrder(aId, Compare);
1.248 + if (i >= 0)
1.249 + {
1.250 + err = iResources[i].iResource->GetMetaData(aMetaData);
1.251 + }
1.252 + Kern::MutexSignal(*iMutex);
1.253 + NKern::ThreadLeaveCS();
1.254 + return err;
1.255 + }
1.256 +
1.257 +TAny* DSgChannel::ResourceDataAddress(TUint64 aId) const
1.258 + {
1.259 + if (aId == 0)
1.260 + {
1.261 + return NULL;
1.262 + }
1.263 + TAny* addr = NULL;
1.264 + NKern::ThreadEnterCS();
1.265 + Kern::MutexWait(*iMutex);
1.266 + TInt i = iResources.FindInOrder(aId, Compare);
1.267 + if (i >= 0)
1.268 + {
1.269 + addr = Kern::ChunkUserBase(iResources[i].iResource->DataChunk(), &Kern::CurrentThread());
1.270 + }
1.271 + Kern::MutexSignal(*iMutex);
1.272 + NKern::ThreadLeaveCS();
1.273 + return addr;
1.274 + }
1.275 +
1.276 +TInt DSgChannel::ResourceDataSize(TUint64 aId) const
1.277 + {
1.278 + if (aId == 0)
1.279 + {
1.280 + return KErrArgument;
1.281 + }
1.282 + TInt ret = KErrNotFound;
1.283 + NKern::ThreadEnterCS();
1.284 + Kern::MutexWait(*iMutex);
1.285 + TInt i = iResources.FindInOrder(aId, Compare);
1.286 + if (i >= 0)
1.287 + {
1.288 + ret = iResources[i].iResource->DataSize();
1.289 + }
1.290 + Kern::MutexSignal(*iMutex);
1.291 + NKern::ThreadLeaveCS();
1.292 + return ret;
1.293 + }
1.294 +
1.295 +TInt DSgChannel::GlobalResourceCount() const
1.296 + {
1.297 + NKern::ThreadEnterCS();
1.298 + TInt ret = SgExtension::GlobalResourceCount();
1.299 + NKern::ThreadLeaveCS();
1.300 + return ret;
1.301 + }
1.302 +
1.303 +TInt DSgChannel::LocalGraphicsMemoryUsed() const
1.304 + {
1.305 + TInt ret = 0;
1.306 + NKern::ThreadEnterCS();
1.307 + Kern::MutexWait(*iMutex);
1.308 + TInt n = iResources.Count();
1.309 + for (TInt i = 0; i < n; ++i)
1.310 + {
1.311 + ret += iResources[i].iResource->DataChunk()->Size();
1.312 + }
1.313 + Kern::MutexSignal(*iMutex);
1.314 + NKern::ThreadLeaveCS();
1.315 + return ret;
1.316 + }
1.317 +
1.318 +TInt DSgChannel::GlobalGraphicsMemoryUsed() const
1.319 + {
1.320 + NKern::ThreadEnterCS();
1.321 + TInt ret = SgExtension::GlobalGraphicsMemoryUsed();
1.322 + NKern::ThreadLeaveCS();
1.323 + return ret;
1.324 + }
1.325 +
1.326 +DSgChannel::TResourceListItem::TResourceListItem(DSgResource* aResource, TInt aChunkHandle)
1.327 + : iResource(aResource), iChunkHandle(aChunkHandle)
1.328 + {
1.329 + }
1.330 +
1.331 +TInt DSgChannel::Compare(const TUint64* aId, const TResourceListItem& aResourceListItem)
1.332 + {
1.333 + return DSgResource::Compare(aId, *aResourceListItem.iResource);
1.334 + }
1.335 +
1.336 +TInt DSgChannel::Compare(const TResourceListItem& aResourceListItem1, const TResourceListItem& aResourceListItem2)
1.337 + {
1.338 + return DSgResource::Compare(*aResourceListItem1.iResource, *aResourceListItem2.iResource);
1.339 + }