guestrendering/guestegl/src/eglsgimage.cpp
branchbug235_bringup_0
changeset 13 220791dae4c4
child 17 c9d1eb0bfac8
equal deleted inserted replaced
12:a4c94be9fb92 13:220791dae4c4
       
     1 // Copyright (c) 2010 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 // Implementation of SgImage functions exported through eglGetProcAddress
       
    15 
       
    16 
       
    17 #include "eglapi.h"
       
    18 
       
    19 
       
    20 ////////////////////////////////////////////////////////////////////////////////////////////////////
       
    21 // CEgLImage
       
    22 ////////////////////////////////////////////////////////////////////////////////////////////////////
       
    23 
       
    24 #ifdef FAISALMEMON_S4_SGIMAGE
       
    25 CEglImage::CEglImage() : iSgHandle(), iDisplay(0), iSgImageInfo(), iPbufferHandle(0), iVgHandle(VG_INVALID_HANDLE),
       
    26 		iCreateCount(1), iOpenCount(0), iIsDestroyed(EFalse)
       
    27 	{
       
    28 	EGL_TRACE("CEglImage::CEglImage(0x%x)", this);
       
    29 	}
       
    30 
       
    31 CEglImage::~CEglImage()
       
    32 	{
       
    33 	EGL_TRACE("CEglImage::~CEglImage(0x%x) iCreateCount=%d, iOpenCount=%d", this, iCreateCount, iOpenCount);
       
    34 	EGLPANIC_ASSERT_DEBUG( (iCreateCount == 0) && (iOpenCount == 0), EEglPanicEglImageRefCountNonZero);
       
    35 	iSgHandle.Close();
       
    36 	}
       
    37 
       
    38 // initialise member data
       
    39 void CEglImage::Create(TSgDrawableId aSgImageId, EGLDisplay aDisplay, TSgImageInfo& aSgImageInfo, TInt aPbufferHandle,
       
    40 		VGHandle aVgHandle)
       
    41 	{
       
    42 	EGL_TRACE("CEglImage::Create iCreateCount=%d, iOpenCount=%d", iCreateCount, iOpenCount);
       
    43 	EGLPANIC_ASSERT_DEBUG(iCreateCount == 0, EEglPanicEglImageRefCountNonZero);
       
    44 	EGLPANIC_ASSERT_DEBUG(!iIsDestroyed, EEglPanicEglImageIsDestroyed);
       
    45 	EGLPANIC_ASSERT_DEBUG(aSgImageId != KSgNullDrawableId, EEglPanicTemp);
       
    46 	++iCreateCount;
       
    47 	TInt err = iSgHandle.Open(aSgImageId);
       
    48 	EGLPANIC_ASSERT_DEBUG(err == KErrNone, EEglPanicTemp);
       
    49 	iDisplay = aDisplay;
       
    50 	iSgImageInfo = aSgImageInfo;
       
    51 	iVgHandle = aVgHandle;
       
    52 	iPbufferHandle = aPbufferHandle;
       
    53 	}
       
    54 
       
    55 // Duplicate open
       
    56 void CEglImage::Duplicate()
       
    57 	{
       
    58 	EGL_TRACE("CEglImage::Duplicate iCreateCount=%d, iOpenCount=%d", iCreateCount, iOpenCount);
       
    59 	EGLPANIC_ASSERT_DEBUG( (iCreateCount > 0) && !iIsDestroyed, EEglPanicEglImageIsDestroyed);
       
    60 	++iCreateCount;
       
    61 	}
       
    62 
       
    63 
       
    64 // If successful increments reference count & returns ETrue. (Ultimtately called from e.g. vgCreateEGLImageTargetKHR.) 
       
    65 TBool CEglImage::OpenForVgImage(TSize& aSize, VGHandle& aVgHandle, TUint64& aSgImageId)
       
    66 	{
       
    67 	EGL_TRACE("CEglImage::OpenForVgImage iCreateCount=%d, iOpenCount=%d", iCreateCount, iOpenCount);
       
    68 	if ( (iCreateCount > 0) && (iSgImageInfo.iUsage & ESgUsageBitOpenVgImage) )
       
    69 		{
       
    70 		++iOpenCount;
       
    71 		aSize = iSgImageInfo.iSizeInPixels;
       
    72 		aVgHandle = iVgHandle;
       
    73 		aSgImageId = iSgHandle.Id().iId;
       
    74 		return ETrue;
       
    75 		}
       
    76 	return EFalse;
       
    77 	}
       
    78 
       
    79 // If successful decrements reference count & returns ETrue. (Ultimtately called from e.g. vgDestroyImage.) 
       
    80 TBool CEglImage::Close()
       
    81 	{
       
    82 	EGL_TRACE("CEglImage::Close iCreateCount=%d, iOpenCount=%d", iCreateCount, iOpenCount);
       
    83 	if (iOpenCount > 0)
       
    84 		{
       
    85 		--iOpenCount;
       
    86 		if ( RefCount() == 0 )
       
    87 			{
       
    88 			iSgHandle.Close();
       
    89 			}
       
    90 		return ETrue;
       
    91 		}
       
    92 	return EFalse;
       
    93 	}
       
    94 
       
    95 // If not already destroyed: mark EglImage as destroyed, dec reference count, return ETrue. (Called from eglDestroyImageKHR.) 
       
    96 TBool CEglImage::Destroy()
       
    97 	{
       
    98 	EGL_TRACE("CEglImage::Destroy iCreateCount=%d, iOpenCount=%d", iCreateCount, iOpenCount);
       
    99 	if (!iIsDestroyed)
       
   100 		{
       
   101 		--iCreateCount;
       
   102 		if (iCreateCount == 0)
       
   103 			{
       
   104 			iIsDestroyed = ETrue;
       
   105 			if ( RefCount() == 0 )
       
   106 				{
       
   107 				iSgHandle.Close();
       
   108 				}
       
   109 			return ETrue;
       
   110 			}
       
   111 		}
       
   112 	return EFalse;
       
   113 	}
       
   114 
       
   115 // Reference Count: count of unbalanced Create() plus Open() calls.
       
   116 TInt CEglImage::RefCount()
       
   117 	{
       
   118 	return iOpenCount + iCreateCount;
       
   119 	}
       
   120 
       
   121 TSgDrawableId CEglImage::SgImageId() const
       
   122 	{
       
   123 	return iSgHandle.Id();
       
   124 	}
       
   125 
       
   126 EGLDisplay CEglImage::Display() const
       
   127 	{
       
   128 	return iDisplay;
       
   129 	}
       
   130 
       
   131 TBool CEglImage::IsDestroyed() const
       
   132 	{
       
   133 	return iIsDestroyed;
       
   134 	}
       
   135 #else
       
   136 void CEglImage::Duplicate() {}
       
   137 TBool CEglImage::OpenForVgImage(TSize& aSize, VGHandle& aVgHandle, TUint64& aSgImageId) {}
       
   138 TBool CEglImage::Close() {}
       
   139 TBool CEglImage::Destroy() {}
       
   140 TBool CEglImage::IsDestroyed() const {}
       
   141 TInt CEglImage::RefCount() {}
       
   142 EGLDisplay CEglImage::Display() const {}
       
   143 #endif
       
   144 
       
   145 
       
   146 ////////////////////////////////////////////////////////////////////////////////////////////////////
       
   147 // SgImage stuff in CGuestEGL
       
   148 ////////////////////////////////////////////////////////////////////////////////////////////////////
       
   149 
       
   150 
       
   151 EGLint CGuestEGL::ValidateEglImageTarget(EGLDisplay aDisplay, EGLContext aContext, EGLenum aTarget,
       
   152 		EGLClientBuffer aBuffer, const EGLint *aAttribList, TSymbianPixmapTypeId aTargetPixmapType)
       
   153 	{
       
   154 	if (!IsDisplayInitialized(aDisplay)) // ToDo just check display is valid, don't care if it is initialised
       
   155 		{
       
   156 		return EGL_BAD_DISPLAY;
       
   157 		}
       
   158 	if ( (aContext != EGL_NO_CONTEXT) || (aTarget != EGL_NATIVE_PIXMAP_KHR) )
       
   159 		{
       
   160 		return EGL_BAD_PARAMETER;
       
   161 		}
       
   162 	aTargetPixmapType = EglInternalFunction_GetNativePixmapType((EGLNativePixmapType) aBuffer);
       
   163 	if ( (aTargetPixmapType != EPixmapTypeSgImage) || !EglInternalFunction_IsValidNativePixmap((EGLNativePixmapType) aBuffer, aTargetPixmapType) )
       
   164 		{
       
   165 		return EGL_BAD_PARAMETER;
       
   166 		}
       
   167 	// ToDo check Attrib List is valid
       
   168 	return EGL_SUCCESS;
       
   169 	}
       
   170 
       
   171 #ifdef FAISALMEMON_S4_SGIMAGE
       
   172 EGLImageKHR CGuestEGL::DuplicateEglImageIfItExists(EGLDisplay aDisplay, TSgDrawableId aSgId)
       
   173 	{ // ToDo something smarter with a Hash Map ?
       
   174 	TInt imageCount = iEglImageArray.Count();
       
   175 	for (TInt index = 0; index < imageCount; ++index)
       
   176 		{
       
   177 		CEglImage* imagePtr = iEglImageArray[index];
       
   178 		if ( !imagePtr->IsDestroyed() && (imagePtr->SgImageId() == aSgId)  && (imagePtr->Display() == aDisplay) )
       
   179 			{
       
   180 			imagePtr->Duplicate();
       
   181 			EGL_TRACE("CGuestEGL::DuplicateEglImageIfItExists SgImage already used for eglImage=0x%x", imagePtr);
       
   182 			return (EGLImageKHR)imagePtr;
       
   183 			}
       
   184 		}
       
   185 	return EGL_NO_IMAGE_KHR;
       
   186 	}
       
   187 
       
   188 
       
   189 
       
   190 EGLImageKHR CGuestEGL::CreateNewEglImage(TEglThreadState& aThreadState, EGLDisplay aDisplay, TSgDrawableId aSgId, TSgImageInfo aSgImageInfo)
       
   191 	{
       
   192 	RHeap* oldHeap = CVghwUtils::SwitchToVghwHeap();
       
   193 	CEglImage* newImage = new CEglImage();
       
   194 	if (newImage)
       
   195 		{
       
   196 		if (KErrNone != iEglImageArray.InsertInAddressOrder(newImage))
       
   197 			{
       
   198 			delete newImage;
       
   199 			newImage = NULL;
       
   200 			}
       
   201 		}
       
   202 	CVghwUtils::SwitchFromVghwHeap(oldHeap);
       
   203 	if (newImage == NULL)
       
   204 		{
       
   205 		EGL_TRACE("CGuestEGL::CreateNewEglImage 1.a error creating new CEglImage");
       
   206 		return EGL_NO_IMAGE_KHR;
       
   207 		}
       
   208 
       
   209 	// success ...
       
   210 	EGL_TRACE("CGuestEGL::CreateNewEglImage 1.b Created new eglImage=0x%x", newImage);
       
   211 	TInt pbufferHandle;
       
   212 	TInt vgImageHandle;
       
   213 	TUint64 sgHandles;
       
   214     EGLint hostResult = CVghwUtils::EglGetSgHandles(aSgId.iId, &sgHandles);
       
   215     pbufferHandle = (TInt)(sgHandles&0xFFFFFFFF);
       
   216     vgImageHandle = (TInt)((sgHandles>>32)&0xFFFFFFFF);
       
   217 	EGL_TRACE("CGuestEGL::CreateNewEglImage 2. CVghwUtils::EglGetSgHandles result=%d, pbufferHandle=0x%x, vgImageHandle=0x%x", 
       
   218 			hostResult, pbufferHandle, vgImageHandle);
       
   219 	EGLPANIC_ASSERT(KErrNone == hostResult, EEglPanicTemp);
       
   220 
       
   221 	newImage->Create(aSgId, aDisplay, aSgImageInfo, pbufferHandle, vgImageHandle);
       
   222 
       
   223 	return (EGLImageKHR)newImage;
       
   224 	}
       
   225 
       
   226 #endif // FAISALMEMON_S4_SGIMAGE
       
   227 
       
   228 
       
   229 /*
       
   230  * eglCreateImageKHR supports Khronos EGL extension #8, "KHR_image_base"
       
   231  *
       
   232  * Supported values for target parameter:
       
   233  * EGL_NATIVE_PIXMAP_KHR for Khronos EGL extension #9, "KHR_image_pixmap"
       
   234  */
       
   235 EGLImageKHR CGuestEGL::eglCreateImageKHR(TEglThreadState& aThreadState, EGLDisplay aDisplay, EGLContext aContext, EGLenum aTarget,
       
   236 		EGLClientBuffer aBuffer, const EGLint *aAttribList)
       
   237 	{
       
   238 	// ToDo lock aDisplay
       
   239 	EGLImageKHR eglImage = EGL_NO_IMAGE_KHR;
       
   240 	iEglImageLock.Wait();
       
   241 	TSymbianPixmapTypeId targetPixmapType = EPixmapTypeNone;
       
   242 	EGLint error = ValidateEglImageTarget(aDisplay, aContext, aTarget, aBuffer, aAttribList, targetPixmapType);
       
   243 	EGL_TRACE("CGuestEGL::eglCreateImageKHR 1. validate error=%d, targetPixmapType=%d", error, targetPixmapType);
       
   244 
       
   245 	if (error == EGL_SUCCESS)
       
   246 		{
       
   247 		switch(targetPixmapType)
       
   248 			{
       
   249 			case EPixmapTypeSgImage:
       
   250 				{
       
   251 #ifdef FAISALMEMON_S4_SGIMAGE
       
   252 				// try to lock SgImage from disappearing by opening a temporary handle
       
   253 				RSgImage* sgImagePtr = reinterpret_cast<RSgImage*>(aBuffer);
       
   254 				TSgDrawableId sgId = sgImagePtr->Id();
       
   255 				RSgDrawable sgHandle;
       
   256 				TSgImageInfo imgInfo;
       
   257 				// ToDo ensure SgImage has correct suitable usage bits 
       
   258 				if ( (sgId != KSgNullDrawableId) && (KErrNone == sgHandle.Open(sgId)) && (KErrNone == sgImagePtr->GetInfo(imgInfo)) )
       
   259 					{
       
   260 					EGL_TRACE("CGuestEGL::eglCreateImageKHR 2. SgImage PixelFormat=%d; size=%d,%d; Usage=0x%x",
       
   261 							imgInfo.iPixelFormat, imgInfo.iSizeInPixels.iWidth, imgInfo.iSizeInPixels.iHeight, imgInfo.iUsage);
       
   262 
       
   263 					// find / create CEglImage
       
   264 					if (error == EGL_SUCCESS)
       
   265 						{ // SgImage may already be used for an EglImage
       
   266 						eglImage = DuplicateEglImageIfItExists(aDisplay, sgId);
       
   267 						if (eglImage == EGL_NO_IMAGE_KHR)
       
   268 							{ // first use
       
   269 							eglImage = CreateNewEglImage(aThreadState, aDisplay, sgId, imgInfo);
       
   270 							if (eglImage == EGL_NO_IMAGE_KHR)
       
   271 								{
       
   272 								error = EGL_BAD_ALLOC;
       
   273 								}
       
   274 							}
       
   275 						}
       
   276 					}
       
   277 				else
       
   278 					{
       
   279 					error = EGL_BAD_PARAMETER;
       
   280 					}
       
   281 				sgHandle.Close();
       
   282 #endif
       
   283 				}
       
   284 				break;
       
   285 
       
   286 			case EPixmapTypeFbsBitmap:
       
   287 			case EPixmapTypeNone:
       
   288 			default: // pixmap type not supported by this API
       
   289 				error = EGL_BAD_PARAMETER;
       
   290 				break;
       
   291 			}
       
   292 		}
       
   293 
       
   294 	iEglImageLock.Signal();
       
   295 	aThreadState.SetEglError(error);
       
   296 
       
   297 	// we either failed or should have a non-NULL handle
       
   298 	EGLPANIC_ASSERT_DEBUG( (error != EGL_SUCCESS) || (eglImage != EGL_NO_IMAGE_KHR), EEglPanicTemp);
       
   299 
       
   300 	return eglImage;
       
   301 	}
       
   302 
       
   303 /*
       
   304  * eglDestroyImageKHR supports Khronos EGL extension #8, "KHR_image_base"
       
   305  */
       
   306 EGLBoolean CGuestEGL::eglDestroyImageKHR(TEglThreadState& aThreadState, EGLDisplay aDisplay, EGLImageKHR aImage)
       
   307 	{
       
   308 	// ToDo lock aDisplay
       
   309 	EGLBoolean result = EGL_FALSE;
       
   310 	iEglImageLock.Wait();
       
   311 	TInt index = iEglImageArray.FindInAddressOrder((CEglImage*)aImage);
       
   312 	if (index >= 0)
       
   313 		{
       
   314 		CEglImage* eglImage = iEglImageArray[index];
       
   315 		if ( (eglImage->Display() && aDisplay) && eglImage->Destroy())
       
   316 			{
       
   317 			result = EGL_TRUE;
       
   318 			if (eglImage->RefCount() == 0)
       
   319 				{
       
   320 				iEglImageArray.Remove(index);
       
   321 				delete eglImage;
       
   322 				}
       
   323 			}
       
   324 		}
       
   325 	iEglImageLock.Signal();
       
   326 
       
   327 	// set EGL Error appropriately
       
   328 	aThreadState.SetEglError( result ? EGL_SUCCESS : EGL_BAD_PARAMETER);
       
   329 	return result;
       
   330 	}
       
   331 
       
   332 
       
   333 TBool CGuestEGL::EglImageOpenForVgImage(EGLImageKHR aImage, TSize& aSize, VGHandle& aVgHandle, TUint64& aSgImageId)
       
   334 	{
       
   335 	EGLBoolean result = EGL_FALSE;
       
   336 	iEglImageLock.Wait();
       
   337 	TInt index = iEglImageArray.FindInAddressOrder((CEglImage*)aImage);
       
   338 	if (index >= 0)
       
   339 		{
       
   340 		CEglImage* eglImage = iEglImageArray[index];
       
   341 		if (eglImage->OpenForVgImage(aSize, aVgHandle, aSgImageId))
       
   342 			{
       
   343 			result = EGL_TRUE;
       
   344 			}
       
   345 		}
       
   346 	iEglImageLock.Signal();
       
   347 	return result;
       
   348 	}
       
   349 
       
   350 void CGuestEGL::EglImageClose(EGLImageKHR aImage)
       
   351 	{
       
   352 	iEglImageLock.Wait();
       
   353 	TInt index = iEglImageArray.FindInAddressOrder((CEglImage*)aImage);
       
   354 	if (index >= 0)
       
   355 		{
       
   356 		CEglImage* eglImage = iEglImageArray[index];
       
   357 		if (eglImage->Close())
       
   358 			{
       
   359 			if (eglImage->RefCount() == 0)
       
   360 				{
       
   361 				iEglImageArray.Remove(index);
       
   362 				delete eglImage;
       
   363 				}
       
   364 			}
       
   365 		}
       
   366 	iEglImageLock.Signal();
       
   367 	}
       
   368 
       
   369 // end of file eglsgimage.cpp