sl@0: // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Reference EGL implementation to support EGL sync objects and OpenWF extensions sl@0: sl@0: #include "eglprivate.h" sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: // OpenWF Composition library sl@0: // sl@0: _LIT(KOwfDllName, "libWFC.dll"); sl@0: const TUid KOwfDllUid1 = {KDynamicLibraryUidValue}; sl@0: const TUid KOwfDllUid2 = {KUidSharedDllUidValue}; sl@0: const TUid KOwfDllUid3 = {KUidOpenWfcDllUidValue}; sl@0: const TUidType KOwfDllUidType = TUidType(KOwfDllUid1, KOwfDllUid2, KOwfDllUid3); sl@0: sl@0: // EGL_KHR_reusable_sync extensions API sl@0: // Declare here so their addresses can be used to build extensions table sl@0: // sl@0: EGLSyncKHR eglCreateSyncKHR(EGLDisplay aDisplay, EGLenum aType, const EGLint *aAttribList); sl@0: EGLBoolean eglDestroySyncKHR(EGLDisplay aDisplay, EGLSyncKHR aSync); sl@0: EGLint eglClientWaitSyncKHR(EGLDisplay aDisplay, EGLSyncKHR aSync, EGLint aFlags, EGLTimeKHR aTimeout); sl@0: EGLBoolean eglSignalSyncKHR(EGLDisplay aDisplay, EGLSyncKHR aSync, EGLenum aMode); sl@0: EGLBoolean eglGetSyncAttribKHR(EGLDisplay aDisplay, EGLSyncKHR aSync, EGLint aAttribute, EGLint *aValue); sl@0: sl@0: // Private extensions sl@0: // sl@0: EGLint egl_Private_SignalSyncNOK(EGLDisplay aDisplay, EGLSyncKHR aSync, EGLenum aMode); sl@0: sl@0: // Extensions for shared heap OOM test sl@0: #ifdef _DEBUG sl@0: void egliDebugHeapMarkStart(); sl@0: EGLint egliDebugHeapMarkEnd(EGLint aCount); sl@0: void egliDebugSetBurstAllocFail(EGLenum aType, EGLint aRate, EGLint aBurst); sl@0: #endif //_DEBUG sl@0: sl@0: struct TEglNameProcRecord sl@0: { sl@0: char* iName; sl@0: TFuncPtrEglProc iProc; sl@0: }; sl@0: sl@0: #define MAKE_NAME_PROC_RECORD(f) {#f,reinterpret_cast(f)} sl@0: sl@0: const TEglNameProcRecord KEglExtensionFuncs[] = { sl@0: MAKE_NAME_PROC_RECORD(eglCreateSyncKHR), sl@0: MAKE_NAME_PROC_RECORD(eglDestroySyncKHR), sl@0: MAKE_NAME_PROC_RECORD(eglClientWaitSyncKHR), sl@0: MAKE_NAME_PROC_RECORD(eglSignalSyncKHR), sl@0: MAKE_NAME_PROC_RECORD(eglGetSyncAttribKHR), sl@0: MAKE_NAME_PROC_RECORD(egl_Private_SignalSyncNOK) sl@0: #ifdef _DEBUG sl@0: , sl@0: MAKE_NAME_PROC_RECORD(egliDebugHeapMarkStart), sl@0: MAKE_NAME_PROC_RECORD(egliDebugHeapMarkEnd), sl@0: MAKE_NAME_PROC_RECORD(egliDebugSetBurstAllocFail) sl@0: #endif //_DEBUG sl@0: }; sl@0: sl@0: // Use common EglPls() function to hide difference between WINS and target in dealing with PLS sl@0: // sl@0: #ifdef __WINS__ sl@0: #include sl@0: static inline XEglPls* EglPls() sl@0: { sl@0: const TUid KUidEglDll = {KUidEGLDllUidValue}; sl@0: return Pls(KUidEglDll); sl@0: } sl@0: #else sl@0: static XEglPls TheEglPls; sl@0: static inline XEglPls* EglPls() sl@0: { sl@0: return &TheEglPls; sl@0: } sl@0: #endif sl@0: sl@0: sl@0: // Macros for placement new and delete of shared objects sl@0: // sl@0: #define EGL_PLACEMENT_NEW(heap, obj, klass) klass* obj = NULL; \ sl@0: TAny* buf = (heap).AllocZ(sizeof(klass)); \ sl@0: if (buf) \ sl@0: { \ sl@0: obj = new(buf) klass((heap)); \ sl@0: } sl@0: sl@0: #define EGL_PLACEMENT_DELETE(heap, obj, klass) obj->~klass(); \ sl@0: (heap).Free(obj); sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // PLS sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: XEglPls::XEglPls() sl@0: { sl@0: iError = iLock.CreateLocal(); sl@0: iDriver = NULL; sl@0: } sl@0: sl@0: XEglPls::~XEglPls() sl@0: { sl@0: iLock.Close(); sl@0: } sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // EGL driver sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: CEglDriver::CEglDriver(RHeap& aHeap): sl@0: iHeap(aHeap) sl@0: { sl@0: } sl@0: sl@0: CEglDriver::~CEglDriver() sl@0: { sl@0: // EGL display was allocated on shared heap using placement new sl@0: if (iDisplay) sl@0: { sl@0: EGL_PLACEMENT_DELETE(iHeap, iDisplay, CEglDisplay) sl@0: } sl@0: sl@0: iOwfLib.Close(); sl@0: iLock.Close(); sl@0: } sl@0: sl@0: TInt CEglDriver::Open() sl@0: { sl@0: // we're in trouble if mutex creation failed during DLL initialisation sl@0: XEglPls& pls = *EglPls(); sl@0: if (pls.iError != KErrNone) sl@0: { sl@0: return pls.iError; sl@0: } sl@0: sl@0: pls.Lock(); sl@0: sl@0: if (pls.iDriver) sl@0: { sl@0: ++(pls.iDriver->iRefCount); sl@0: pls.Unlock(); sl@0: return KErrNone; sl@0: } sl@0: sl@0: // create shared heap in a local chunk to allow access to shared objects from any threads within Driver sl@0: // allow heap to grow from 256 bytes up to 1MB sl@0: const TInt KMaxHeapSize = 0x100000; sl@0: const TInt KWordAlign = 4; sl@0: RHeap* heap = User::ChunkHeap(NULL, KMinHeapSize, KMaxHeapSize, KMinHeapGrowBy, KWordAlign); sl@0: if (!heap) sl@0: { sl@0: pls.Unlock(); sl@0: return KErrNoMemory; sl@0: } sl@0: sl@0: EGL_PLACEMENT_NEW(*heap, drv, CEglDriver) sl@0: if (!drv) sl@0: { sl@0: heap->Close(); sl@0: pls.Unlock(); sl@0: return KErrNoMemory; sl@0: } sl@0: sl@0: const TInt err = drv->Construct(); sl@0: if (err != KErrNone) sl@0: { sl@0: EGL_PLACEMENT_DELETE(*heap, drv, CEglDriver) sl@0: heap->Close(); sl@0: pls.Unlock(); sl@0: sl@0: return err; sl@0: } sl@0: sl@0: pls.iDriver = drv; sl@0: pls.iDriver->iRefCount = 1; sl@0: sl@0: pls.Unlock(); sl@0: sl@0: return KErrNone; sl@0: } sl@0: sl@0: void CEglDriver::Close() sl@0: { sl@0: XEglPls& pls = *EglPls(); sl@0: // must never happen if CEglDriver::Open() succeed sl@0: __ASSERT_DEBUG(pls.iError == KErrNone, User::Panic(KEglPanicCategory, EEglPanicPlsMutexError)); sl@0: sl@0: pls.Lock(); sl@0: sl@0: if (pls.iDriver) sl@0: { sl@0: if (--(pls.iDriver->iRefCount) == 0) sl@0: { sl@0: // copy shared heap pointer out as we're about to destroy the driver sl@0: RHeap& heap = pls.iDriver->iHeap; sl@0: sl@0: // driver was allocated on shared heap using placement new sl@0: EGL_PLACEMENT_DELETE(heap, pls.iDriver, CEglDriver) sl@0: pls.iDriver = NULL; sl@0: heap.Close(); sl@0: } sl@0: } sl@0: sl@0: pls.Unlock(); sl@0: } sl@0: sl@0: CEglDriver* CEglDriver::GetDriver() sl@0: { sl@0: XEglPls& pls = *EglPls(); sl@0: return pls.iDriver; sl@0: } sl@0: sl@0: // called when lock is held sl@0: // sl@0: TInt CEglDriver::Construct() sl@0: { sl@0: TInt err = iLock.CreateLocal(); sl@0: if (err != KErrNone) sl@0: { sl@0: return err; sl@0: } sl@0: sl@0: // create default EGL display as part of driver initialisation, it will be destroyed when the driver is destroyed sl@0: // and recreated when driver is reinitialised sl@0: EGL_PLACEMENT_NEW(iHeap, disp, CEglDisplay) sl@0: if (!disp) sl@0: { sl@0: return KErrNoMemory; sl@0: } sl@0: sl@0: iDisplay = disp; sl@0: sl@0: // for security reason, use TUidType when loading OWF library to make sure it is not a spoofed DLL with the same name sl@0: err = iOwfLib.Load(KOwfDllName, KOwfDllUidType); sl@0: // allow initialisation to proceed even if OWF library is not found sl@0: if (err == KErrNone) sl@0: { sl@0: // promote library handle from thread-relative to process-wide to allow closing from any thread sl@0: RLibrary dup = iOwfLib; sl@0: err = iOwfLib.Duplicate(RThread()); sl@0: if (err != KErrNone) sl@0: { sl@0: return err; sl@0: } sl@0: // if Duplicate succeed, it will overwrite iOwfLib with process-wide handle sl@0: // we need to close the original thread-relative handle sl@0: dup.Close(); sl@0: sl@0: // In SymbianOS implementation of Khronos API, ordinal #1 is always reserved for internal implementation use as defined in sl@0: // SymbianOS standard DEF files for that API. This DEF file ensures BC among different implementations of that API. sl@0: // In OWF case, ordinal #1 refers to GetExtensionFunctionTable, a function which will return a table of function pointer. sl@0: // One of them is a function that implement GetProcAddress to retrieve OWF extension functions. sl@0: // sl@0: const TInt KOwfGetFuncTableOrdinalNum = 1; sl@0: typedef void (*TFuncPtrOwfGetFuncTable)(TOwfFuncTable*); sl@0: sl@0: TFuncPtrOwfGetFuncTable owfGetFuncTable = reinterpret_cast(iOwfLib.Lookup(KOwfGetFuncTableOrdinalNum)); sl@0: if (!owfGetFuncTable) sl@0: { sl@0: return KErrNotFound; sl@0: } sl@0: sl@0: owfGetFuncTable(&iOwfFuncTable); sl@0: sl@0: if (!iOwfFuncTable.iOwfGetProcAddress) sl@0: { sl@0: return KErrNotSupported; sl@0: } sl@0: } sl@0: sl@0: return KErrNone; sl@0: } sl@0: sl@0: TFuncPtrEglProc CEglDriver::GetProcAddress(const char* aName) const sl@0: { sl@0: // start with EGL extension functions sl@0: // sl@0: TPtrC8 procName(reinterpret_cast(aName)); sl@0: const TInt nExts = sizeof(KEglExtensionFuncs) / sizeof(KEglExtensionFuncs[0]); sl@0: for (TInt idx=0; idx(KEglExtensionFuncs[idx].iName))) sl@0: { sl@0: return KEglExtensionFuncs[idx].iProc; sl@0: } sl@0: } sl@0: sl@0: // not an EGL extension, pass it on to OWF sl@0: // sl@0: return iOwfFuncTable.iOwfGetProcAddress ? iOwfFuncTable.iOwfGetProcAddress(aName) : NULL; sl@0: }