os/graphics/graphicsresourceservices/graphicsresource/src/sgdriver.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include <e32uid.h>
    17 #include "sgdriver.h"
    18 #include "sgresourceadapter.h"
    19 
    20 /**
    21 Function pointer type used to instantiate the Graphics Resource Adapter singleton.
    22 */
    23 typedef TInt (*TSgDriverCreateFunction)(MSgDriverAdapter*&);
    24 
    25 XSgDriverPls::XSgDriverPls()
    26 	{
    27 	iError = iMutex.CreateLocal();
    28 	iOpenCount = 0;
    29 	iDriver = NULL;
    30 	}
    31 
    32 
    33 #ifndef __WINS__
    34 XSgDriverPls gPls;
    35 #endif
    36 
    37 
    38 /**
    39 @publishedPartner
    40 @prototype
    41 @deprecated
    42 
    43 Opens the Graphics Resource driver in the context of the calling process.
    44 
    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.
    50 
    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.
    55 
    56 @pre None.
    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()
    63 */
    64 EXPORT_C TInt SgDriver::Open()
    65 	{
    66 	if (gPls.iError != KErrNone)
    67 		{
    68 		return gPls.iError;
    69 		}
    70 	gPls.iMutex.Wait();
    71 	if (gPls.iDriver)
    72 		{
    73 		++gPls.iOpenCount;
    74 		gPls.iMutex.Signal();
    75 		return KErrNone;
    76 		}
    77 #ifdef __WINS__
    78 	const TUidType KSgResourceAdapterLibraryUidType(KDynamicLibraryUid, KSharedLibraryUid, KSgResourceAdapterLibraryUid);
    79 	RLibrary lib;
    80 	TInt err = lib.Load(KSgResourceAdapterLibraryName, KSgResourceAdapterLibraryUidType);
    81 	if (err != KErrNone)
    82 		{
    83 		if (err == KErrNotFound)
    84 			{
    85 			err = KErrNotSupported;
    86 			}
    87 		gPls.iMutex.Signal();
    88 		return err;
    89 		}
    90 	gPls.iLibrary = lib;
    91 	err = gPls.iLibrary.Duplicate(RThread()); // Get a process-wide handle
    92 	lib.Close();
    93 	if (err != KErrNone)
    94 		{
    95 		gPls.iMutex.Signal();
    96 		return err;
    97 		}
    98 	TSgDriverCreateFunction create = reinterpret_cast<TSgDriverCreateFunction>(gPls.iLibrary.Lookup(1));
    99 	err = create(gPls.iDriver);
   100 	if (err != KErrNone)
   101 		{
   102 		gPls.iLibrary.Close();
   103 		gPls.iMutex.Signal();
   104 		return err;
   105 		}
   106 #else
   107 	TInt err = MSgDriverAdapter::New(gPls.iDriver);
   108 	if (err != KErrNone)
   109 		{
   110 		gPls.iMutex.Signal();
   111 		return err;
   112 		}
   113 #endif
   114 	gPls.iOpenCount = 1;
   115 	gPls.iMutex.Signal();
   116 	return KErrNone;
   117 	}
   118 
   119 
   120 /**
   121 @publishedPartner
   122 @prototype
   123 @deprecated
   124 
   125 Closes the Graphics Resource driver in the context of the calling process.
   126 
   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.
   131 
   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.
   138 
   139 @code
   140 	// Open Graphics Resource driver
   141 	TBool isDriverOpen = EFalse;
   142 	if (SgDriver::Open() == KErrNone)
   143 		{
   144 		isDriverOpen = ETrue;
   145 		}
   146 	// Do some work in this thread
   147 	DoThreadWork();
   148 	// Close Graphics Resource driver
   149 	if (isDriverOpen)
   150 		{
   151 		SgDriver::Close();
   152 		}
   153 @endcode
   154 
   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()
   163 */
   164 EXPORT_C void SgDriver::Close()
   165 	{
   166 	if (gPls.iError != KErrNone)
   167 		{
   168 		return;
   169 		}
   170 	gPls.iMutex.Wait();
   171 	if (gPls.iOpenCount > 0 && --gPls.iOpenCount == 0)
   172 		{
   173 		gPls.iDriver->Delete();
   174 		gPls.iDriver = NULL;
   175 #ifdef __WINS__
   176 		gPls.iLibrary.Close();
   177 #endif
   178 		}
   179 	gPls.iMutex.Signal();
   180 	}
   181 
   182 
   183 /**
   184 @internalComponent
   185 @test
   186 */
   187 EXPORT_C TInt SgDriver::ResourceCount()
   188 	{
   189 #ifdef _DEBUG
   190 	gPls.iMutex.Wait();
   191 	__ASSERT_DEBUG(gPls.iDriver, Panic(ESgPanicNoDriver));
   192 #endif
   193 	TInt count = gPls.iDriver->ResourceCount();
   194 #ifdef _DEBUG
   195 	gPls.iMutex.Signal();
   196 #endif
   197 	return count;
   198 	}
   199 
   200 
   201 /**
   202 @internalComponent
   203 @test
   204 */
   205 EXPORT_C void SgDriver::AllocMarkStart()
   206 	{
   207 #ifdef _DEBUG
   208 	gPls.iMutex.Wait();
   209 	__ASSERT_DEBUG(gPls.iDriver, Panic(ESgPanicNoDriver));
   210 #endif
   211 	gPls.iDriver->AllocMarkStart();
   212 #ifdef _DEBUG
   213 	gPls.iMutex.Signal();
   214 #endif
   215 	}
   216 
   217 
   218 /**
   219 @internalComponent
   220 @test
   221 */
   222 EXPORT_C void SgDriver::AllocMarkEnd(TInt aCount)
   223 	{
   224 #ifdef _DEBUG
   225 	gPls.iMutex.Wait();
   226 	__ASSERT_DEBUG(gPls.iDriver, Panic(ESgPanicNoDriver));
   227 #endif
   228 	gPls.iDriver->AllocMarkEnd(aCount);
   229 #ifdef _DEBUG
   230 	gPls.iMutex.Signal();
   231 #endif
   232 	}
   233 
   234 
   235 /**
   236 @internalComponent
   237 @test
   238 */
   239 EXPORT_C void SgDriver::SetAllocFail(RAllocator::TAllocFail aType, TInt aRate)
   240 	{
   241 #ifdef _DEBUG
   242 	gPls.iMutex.Wait();
   243 	__ASSERT_DEBUG(gPls.iDriver, Panic(ESgPanicNoDriver));
   244 #endif
   245 	gPls.iDriver->SetAllocFail(aType, aRate);
   246 #ifdef _DEBUG
   247 	gPls.iMutex.Signal();
   248 #endif
   249 	}