os/graphics/graphicsresourceservices/graphicsresourceimplementation/src/sgchannel.cpp
Update contrib.
1 // Copyright (c) 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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Graphics Resource - logical channel implementation
17 #include <kernel/kern_priv.h>
18 #include <sgresource/sgextension.h>
19 #include "sgdeviceimpl.h"
21 TInt DSgChannel::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& aVersion)
23 TVersion version = SgExtension::Version();
24 if (aVersion.iMajor != version.iMajor || aVersion.iMinor > version.iMinor)
26 return KErrNotSupported;
28 return Kern::MutexCreate(iMutex, KNullDesC, KMutexOrdGeneral7);
31 DSgChannel::~DSgChannel()
37 for (TInt i = 0; i < iResources.Count(); ++i)
39 iResources[i].iResource->Close();
44 TInt DSgChannel::Request(TInt aFunction, TAny* a1, TAny* a2)
48 case RSgDevice::EControlCreateResource:
50 RSgDevice::TCreateResourceArgs args;
51 kumemget32(&args, a1, sizeof(RSgDevice::TCreateResourceArgs));
52 TInt metaDataSize, maxMetaDataSize;
53 Kern::KUDesInfo(*args.iMetaData, metaDataSize, maxMetaDataSize);
54 __ASSERT_ALWAYS(metaDataSize <= KSgMaxMetaDataSize, Panic(ESgPanicMetaDataSizeTooBig));
55 __ASSERT_ALWAYS(args.iDataSize >= 0, Panic(ESgPanicDataSizeNegative));
56 TBuf8<KSgMaxMetaDataSize> metaData;
57 Kern::KUDesGet(metaData, *args.iMetaData);
59 TInt err = CreateResource(args.iAttributes, metaData, args.iDataSize, id);
60 kumemput32(a2, &id, sizeof(TUint64));
63 case RSgDevice::EControlOpenResource:
66 kumemget32(&id, a1, sizeof(TUint64));
67 return OpenResource(id);
69 case RSgDevice::EControlCloseResource:
72 kumemget32(&id, a1, sizeof(TUint64));
73 return CloseResource(id);
75 case RSgDevice::EControlResourceAttributes:
78 kumemget32(&id, a1, sizeof(TUint64));
79 return static_cast<TInt>(ResourceAttributes(id));
81 case RSgDevice::EControlGetResourceMetaData:
84 kumemget32(&id, a1, sizeof(TUint64));
85 TBuf8<KSgMaxMetaDataSize> metaData;
86 TInt err = GetResourceMetaData(id, metaData);
89 Kern::InfoCopy(*static_cast<TDes8*>(a2), metaData);
93 case RSgDevice::EControlResourceDataAddress:
96 kumemget32(&id, a1, sizeof(TUint64));
97 return reinterpret_cast<TInt>(ResourceDataAddress(id));
99 case RSgDevice::EControlResourceDataSize:
102 kumemget32(&id, a1, sizeof(TUint64));
103 return ResourceDataSize(id);
105 case RSgDevice::EControlGlobalResourceCount:
106 return GlobalResourceCount();
107 case RSgDevice::EControlLocalGraphicsMemoryUsed:
108 return LocalGraphicsMemoryUsed();
109 case RSgDevice::EControlGlobalGraphicsMemoryUsed:
110 return GlobalGraphicsMemoryUsed();
112 return KErrNotSupported;
116 TInt DSgChannel::CreateResource(TUint32 aAttribs, const TDesC8& aMetaData, TInt aDataSize, TUint64& aId)
119 NKern::ThreadEnterCS();
120 DSgResource* resource;
121 TInt err = SgExtension::CreateResource(aAttribs, aMetaData, aDataSize, resource);
124 NKern::ThreadLeaveCS();
127 TInt handle = Kern::MakeHandleAndOpen(NULL, resource->DataChunk(), EOwnerProcess);
131 NKern::ThreadLeaveCS();
134 Kern::MutexWait(*iMutex);
135 err = iResources.InsertInOrder(TResourceListItem(resource, handle), Compare);
136 Kern::MutexSignal(*iMutex);
139 (void)Kern::CloseHandle(NULL, handle);
141 NKern::ThreadLeaveCS();
144 NKern::ThreadLeaveCS();
145 aId = resource->Id();
149 TInt DSgChannel::OpenResource(TUint64 aId)
155 NKern::ThreadEnterCS();
156 Kern::MutexWait(*iMutex);
157 TInt i = iResources.FindInOrder(aId, Compare);
160 Kern::MutexSignal(*iMutex);
161 NKern::ThreadLeaveCS();
162 return KErrAlreadyExists;
164 DSgResource* resource;
165 TInt err = SgExtension::FindAndOpenResource(aId, resource);
168 Kern::MutexSignal(*iMutex);
169 NKern::ThreadLeaveCS();
172 TInt handle = Kern::MakeHandleAndOpen(NULL, resource->DataChunk(), EOwnerProcess);
176 Kern::MutexSignal(*iMutex);
177 NKern::ThreadLeaveCS();
180 err = iResources.InsertInOrder(TResourceListItem(resource, handle), Compare);
183 (void)Kern::CloseHandle(NULL, handle);
185 Kern::MutexSignal(*iMutex);
186 NKern::ThreadLeaveCS();
189 Kern::MutexSignal(*iMutex);
190 NKern::ThreadLeaveCS();
194 TInt DSgChannel::CloseResource(TUint64 aId)
200 TInt err = KErrNotFound;
201 NKern::ThreadEnterCS();
202 Kern::MutexWait(*iMutex);
203 TInt i = iResources.FindInOrder(aId, Compare);
206 (void)Kern::CloseHandle(NULL, iResources[i].iChunkHandle);
207 iResources[i].iResource->Close();
208 iResources.Remove(i);
211 Kern::MutexSignal(*iMutex);
212 NKern::ThreadLeaveCS();
216 TUint32 DSgChannel::ResourceAttributes(TUint64 aId) const
223 NKern::ThreadEnterCS();
224 Kern::MutexWait(*iMutex);
225 TInt i = iResources.FindInOrder(aId, Compare);
228 attribs = iResources[i].iResource->Attributes();
230 Kern::MutexSignal(*iMutex);
231 NKern::ThreadLeaveCS();
235 TInt DSgChannel::GetResourceMetaData(TUint64 aId, TDes8& aMetaData) const
241 TInt err = KErrNotFound;
242 NKern::ThreadEnterCS();
243 Kern::MutexWait(*iMutex);
244 TInt i = iResources.FindInOrder(aId, Compare);
247 err = iResources[i].iResource->GetMetaData(aMetaData);
249 Kern::MutexSignal(*iMutex);
250 NKern::ThreadLeaveCS();
254 TAny* DSgChannel::ResourceDataAddress(TUint64 aId) const
261 NKern::ThreadEnterCS();
262 Kern::MutexWait(*iMutex);
263 TInt i = iResources.FindInOrder(aId, Compare);
266 addr = Kern::ChunkUserBase(iResources[i].iResource->DataChunk(), &Kern::CurrentThread());
268 Kern::MutexSignal(*iMutex);
269 NKern::ThreadLeaveCS();
273 TInt DSgChannel::ResourceDataSize(TUint64 aId) const
279 TInt ret = KErrNotFound;
280 NKern::ThreadEnterCS();
281 Kern::MutexWait(*iMutex);
282 TInt i = iResources.FindInOrder(aId, Compare);
285 ret = iResources[i].iResource->DataSize();
287 Kern::MutexSignal(*iMutex);
288 NKern::ThreadLeaveCS();
292 TInt DSgChannel::GlobalResourceCount() const
294 NKern::ThreadEnterCS();
295 TInt ret = SgExtension::GlobalResourceCount();
296 NKern::ThreadLeaveCS();
300 TInt DSgChannel::LocalGraphicsMemoryUsed() const
303 NKern::ThreadEnterCS();
304 Kern::MutexWait(*iMutex);
305 TInt n = iResources.Count();
306 for (TInt i = 0; i < n; ++i)
308 ret += iResources[i].iResource->DataChunk()->Size();
310 Kern::MutexSignal(*iMutex);
311 NKern::ThreadLeaveCS();
315 TInt DSgChannel::GlobalGraphicsMemoryUsed() const
317 NKern::ThreadEnterCS();
318 TInt ret = SgExtension::GlobalGraphicsMemoryUsed();
319 NKern::ThreadLeaveCS();
323 DSgChannel::TResourceListItem::TResourceListItem(DSgResource* aResource, TInt aChunkHandle)
324 : iResource(aResource), iChunkHandle(aChunkHandle)
328 TInt DSgChannel::Compare(const TUint64* aId, const TResourceListItem& aResourceListItem)
330 return DSgResource::Compare(aId, *aResourceListItem.iResource);
333 TInt DSgChannel::Compare(const TResourceListItem& aResourceListItem1, const TResourceListItem& aResourceListItem2)
335 return DSgResource::Compare(*aResourceListItem1.iResource, *aResourceListItem2.iResource);