Update contrib.
1 // Copyright (c) 2007-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.
18 #include "sgresourceadapter.h"
21 Function pointer type used to instantiate the Graphics Resource Adapter singleton.
23 typedef TInt (*TSgDriverCreateFunction)(MSgDriverAdapter*&);
25 XSgDriverPls::XSgDriverPls()
27 iError = iMutex.CreateLocal();
43 Opens the Graphics Resource driver in the context of the calling process.
45 This function must be called by any process using the Graphics Resource API before
46 any other function in the API. When finished the process should call Close(). If
47 Open() has already been called from the same process then calling it again just
48 increments a process-local open count. Each successful call to Open() must be
49 balanced by a corresponding call to Close() later on.
51 Note that, in a multi-threaded process, it is not necessary for each thread to call
52 this function. A possible strategy is therefore for the main thread to call
53 SgDriver::Open() at the beginning and SgDriver::Close() at the end, while the rest
54 of the threads do not call either of these but simply use the Graphics Resource API.
57 @post The Graphics Resource driver is initialised for use in the context of the
58 calling process and the process-local open count is incremented by one.
59 @return KErrNone if successful.
60 @return KErrNotSupported if no implementation of the Graphics Resource API is present.
61 @return KErrNoMemory if there is not enough system memory.
62 @see SgDriver::Close()
64 EXPORT_C TInt SgDriver::Open()
66 if (gPls.iError != KErrNone)
78 const TUidType KSgResourceAdapterLibraryUidType(KDynamicLibraryUid, KSharedLibraryUid, KSgResourceAdapterLibraryUid);
80 TInt err = lib.Load(KSgResourceAdapterLibraryName, KSgResourceAdapterLibraryUidType);
83 if (err == KErrNotFound)
85 err = KErrNotSupported;
91 err = gPls.iLibrary.Duplicate(RThread()); // Get a process-wide handle
98 TSgDriverCreateFunction create = reinterpret_cast<TSgDriverCreateFunction>(gPls.iLibrary.Lookup(1));
99 err = create(gPls.iDriver);
102 gPls.iLibrary.Close();
103 gPls.iMutex.Signal();
107 TInt err = MSgDriverAdapter::New(gPls.iDriver);
110 gPls.iMutex.Signal();
115 gPls.iMutex.Signal();
125 Closes the Graphics Resource driver in the context of the calling process.
127 This function must be called by a process when finished using the Graphics Resource
128 API. It decrements the process-local open count and, when the count becomes zero,
129 it carries out termination tasks needed to release the internal resources allocated
130 for the calling process.
132 Each call to Close() must correspond to a prior successful call to Open(). Note that,
133 in a multi-threaded process, it is not generally safe to call Close() regardless of
134 whether the corresponding call to Open() succeeded or failed, since too many calls to
135 Close() from a thread could have an unexpected effect on all the other threads in the
136 process. The following example demonstrates how to open and close the Graphics
137 Resource driver safely from a worker thread.
140 // Open Graphics Resource driver
141 TBool isDriverOpen = EFalse;
142 if (SgDriver::Open() == KErrNone)
144 isDriverOpen = ETrue;
146 // Do some work in this thread
148 // Close Graphics Resource driver
155 @pre If the process-local open count is one then there are no open handles to graphics
156 resources in the calling process.
157 @post The process-local open count is decremented by one if greater than zero. If the
158 count becomes zero, then the calling process is not able to use the Graphics
159 Resource API any longer.
160 @panic SGRES 1 in debug builds if there still are any open handles to graphics resources
161 in the calling process when the process termination tasks are carried out.
162 @see SgDriver::Open()
164 EXPORT_C void SgDriver::Close()
166 if (gPls.iError != KErrNone)
171 if (gPls.iOpenCount > 0 && --gPls.iOpenCount == 0)
173 gPls.iDriver->Delete();
176 gPls.iLibrary.Close();
179 gPls.iMutex.Signal();
187 EXPORT_C TInt SgDriver::ResourceCount()
191 __ASSERT_DEBUG(gPls.iDriver, Panic(ESgPanicNoDriver));
193 TInt count = gPls.iDriver->ResourceCount();
195 gPls.iMutex.Signal();
205 EXPORT_C void SgDriver::AllocMarkStart()
209 __ASSERT_DEBUG(gPls.iDriver, Panic(ESgPanicNoDriver));
211 gPls.iDriver->AllocMarkStart();
213 gPls.iMutex.Signal();
222 EXPORT_C void SgDriver::AllocMarkEnd(TInt aCount)
226 __ASSERT_DEBUG(gPls.iDriver, Panic(ESgPanicNoDriver));
228 gPls.iDriver->AllocMarkEnd(aCount);
230 gPls.iMutex.Signal();
239 EXPORT_C void SgDriver::SetAllocFail(RAllocator::TAllocFail aType, TInt aRate)
243 __ASSERT_DEBUG(gPls.iDriver, Panic(ESgPanicNoDriver));
245 gPls.iDriver->SetAllocFail(aType, aRate);
247 gPls.iMutex.Signal();