sl@0: /*
sl@0: * Copyright (c) 2009 Symbian Foundation Ltd
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of the License "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: * Symbian Foundation Ltd - initial contribution.
sl@0: * 
sl@0: * Contributors:
sl@0: *
sl@0: * Description:
sl@0: * Implementation of VGI interface
sl@0: */
sl@0: 
sl@0: #include <e32std.h>
sl@0: #include <vg\vgcontext.h>
sl@0: #include <vg\vgcontext_symbian.h>
sl@0: #include <egl.h>
sl@0: 
sl@0: #define MAX_WIDTH 		640 //*4 // in bytes
sl@0: #define MAX_HEIGHT 		640 //*4 // in bytes
sl@0: class TEgl
sl@0: 	{
sl@0: public:
sl@0: 	TEgl() :
sl@0: 	iEgldisplay(0),
sl@0: 	iEglsurface(0),
sl@0: 	iEglcontext(0),
sl@0: 	iInitialised(EFalse),
sl@0: 	iPixmap(NULL)
sl@0: 	{};
sl@0: 
sl@0: public: //data
sl@0: 	EGLDisplay iEgldisplay;
sl@0: 	EGLSurface iEglsurface;
sl@0: 	EGLContext iEglcontext;
sl@0: 	TBool iInitialised;
sl@0: 	CFbsBitmap* iPixmap;
sl@0: 	};
sl@0: 
sl@0: 
sl@0: TEgl& GetEglInstance()
sl@0: 	{
sl@0: 	//use TLS
sl@0: 	//use TLS to store static global egl
sl@0: 	TEgl* pEgl=NULL; 
sl@0: 	if((pEgl = static_cast<TEgl*>(Dll::Tls()))==NULL)
sl@0: 		{
sl@0: 		//create TLS instance
sl@0: 		pEgl = new(ELeave)TEgl;
sl@0: 		Dll::SetTls(pEgl);
sl@0: 		}
sl@0: 	return *pEgl;
sl@0: 	}
sl@0: 
sl@0: void ReleaseTls()
sl@0: 	{
sl@0: 	TEgl* pEgl = static_cast<TEgl*>(Dll::Tls());
sl@0: 	if (pEgl)
sl@0: 		delete pEgl; 
sl@0: 	Dll::SetTls(NULL);
sl@0: 	}
sl@0: 
sl@0: VGI_API_CALL int VGIInitialize( int /*width*/, int /*height*/, VGIColorSpace /*colorSpace*/ )
sl@0: 	{
sl@0: 	return VGI_OK;
sl@0: 	}
sl@0: 
sl@0: VGI_API_CALL int VGIInitializeEx( int /*width*/, int /*height*/, VGIColorSpace /*colorSpace*/, int /*premultiplied*/, int /*conformant*/ )
sl@0: 	{
sl@0: 	return VGI_OK;
sl@0: 	}
sl@0: 
sl@0: VGI_API_CALL int VGICopyToTarget( VGIColorBufferFormat /*format*/, int /*bufferStride*/, void */*buffer*/, int /*maskStride*/, void */*mask*/, VGICopyToTargetHint /*hint*/ )
sl@0: 	{
sl@0: 	return VGI_OK;
sl@0: 	}
sl@0: 
sl@0: VGI_API_CALL void VGITerminate( void )
sl@0: 	{
sl@0: 
sl@0: 	}
sl@0: 
sl@0: VGI_API_CALL int VGIResize( int /*width*/, int /*height*/ )
sl@0: 	{
sl@0: 	return VGI_OK;
sl@0: 	}
sl@0: 
sl@0: VGI_API_CALL int VGIBindToImage( VGImage /*image*/ )
sl@0: 	{
sl@0: 	return VGI_OK;
sl@0: 	}	
sl@0: 
sl@0: VGI_API_CALL int VGIUnBindImage( void )
sl@0: 	{
sl@0: 	return VGI_OK;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: 
sl@0: VGI_API_CALL TInt VGISymbianInitialize( TSize aSize, VGIColorSpace /*aColorSpace*/ )
sl@0: 	{
sl@0: 	TEgl& egl = GetEglInstance();
sl@0: 	//only init once
sl@0: 	if(!egl.iInitialised)
sl@0: 		{
sl@0: 		egl.iInitialised = ETrue;
sl@0: 		static const EGLint s_configAttribs[] =
sl@0: 			{
sl@0: 					EGL_RED_SIZE,
sl@0: 					8,
sl@0: 					EGL_GREEN_SIZE,
sl@0: 					8,
sl@0: 					EGL_BLUE_SIZE,
sl@0: 					8,
sl@0: 					EGL_ALPHA_SIZE,
sl@0: 					8,
sl@0: 					EGL_LUMINANCE_SIZE,
sl@0: 					EGL_DONT_CARE, //EGL_DONT_CARE
sl@0: 					EGL_SURFACE_TYPE,
sl@0: 					EGL_WINDOW_BIT,
sl@0: 					EGL_SAMPLES,
sl@0: 					1,
sl@0: 					EGL_NONE
sl@0: 			};
sl@0: 		EGLint numconfigs;
sl@0: 		
sl@0: 		egl.iEgldisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
sl@0: 		eglInitialize(egl.iEgldisplay, NULL, NULL);
sl@0: 		__ASSERT_ALWAYS(eglGetError() == EGL_SUCCESS,User::Invariant());
sl@0: 		eglBindAPI(EGL_OPENVG_API);
sl@0: 		
sl@0: 		EGLConfig  eglconfig;
sl@0: 		
sl@0: 		eglChooseConfig(egl.iEgldisplay, s_configAttribs, &eglconfig, 1, &numconfigs);
sl@0: 		__ASSERT_ALWAYS(eglGetError() == EGL_SUCCESS,User::Invariant());
sl@0: 		__ASSERT_ALWAYS(numconfigs == 1,User::Invariant());
sl@0: 		
sl@0: 		TSize maxSize(MAX_WIDTH,MAX_HEIGHT);
sl@0: 		egl.iPixmap = new(ELeave) CFbsBitmap();
sl@0: 		egl.iPixmap->Create( maxSize, EColor16MA );
sl@0: 		 
sl@0: 		egl.iEglsurface = eglCreatePixmapSurface(egl.iEgldisplay, eglconfig, (EGLNativePixmapType)egl.iPixmap, NULL);
sl@0: 		__ASSERT_ALWAYS(eglGetError() == EGL_SUCCESS,User::Invariant());
sl@0: 				
sl@0: 		egl.iEglcontext = eglCreateContext(egl.iEgldisplay, eglconfig, NULL, NULL);
sl@0: 		__ASSERT_ALWAYS(eglGetError() == EGL_SUCCESS,User::Invariant());
sl@0: 		eglMakeCurrent(egl.iEgldisplay, egl.iEglsurface, egl.iEglsurface, egl.iEglcontext);
sl@0: 		__ASSERT_ALWAYS(eglGetError() == EGL_SUCCESS,User::Invariant());
sl@0: 	
sl@0: 		}
sl@0: 		return VGI_OK;
sl@0: 	}
sl@0: 
sl@0: VGI_API_CALL TInt VGISymbianInitializeEx( TSize /*aSize*/, VGIColorSpace /*aColorSpace*/, TBool /*aPremultiplied*/, TBool /*aConformant*/ )
sl@0: 	{
sl@0: 	return VGI_OK;
sl@0: 	}
sl@0: 
sl@0: VGI_API_CALL TInt VGISymbianCopyToBitmap( CFbsBitmap *aBitmap, CFbsBitmap *aMaskBitmap, VGICopyToTargetHint /*aHint*/ )
sl@0: 	{
sl@0: 	TEgl& egl = GetEglInstance();	
sl@0: 	
sl@0: 	eglCopyBuffers(egl.iEgldisplay, egl.iEglsurface,(EGLNativePixmapType)aBitmap);
sl@0: 	
sl@0: 	if(aMaskBitmap)
sl@0: 		{
sl@0: 		eglCopyBuffers(egl.iEgldisplay, egl.iEglsurface,(EGLNativePixmapType)aMaskBitmap);		
sl@0: 		}
sl@0: 	
sl@0: 	__ASSERT_ALWAYS(eglGetError() == EGL_SUCCESS,User::Invariant());
sl@0: 	return VGI_OK;
sl@0: 	}
sl@0: 
sl@0: VGI_API_CALL void VGISymbianTerminate()
sl@0: {
sl@0: 	TEgl& egl = GetEglInstance();
sl@0: 	eglDestroyContext(egl.iEgldisplay, egl.iEglcontext);
sl@0: 	eglDestroySurface(egl.iEgldisplay, egl.iEglsurface);
sl@0: 	delete egl.iPixmap;
sl@0: 	ReleaseTls();
sl@0: }
sl@0: 
sl@0: VGI_API_CALL TInt VGISymbianResize( TSize aSize )
sl@0: 	{
sl@0: 	return VGI_OK;
sl@0: 	}
sl@0: 
sl@0: VGI_API_CALL TInt VGISymbianBindToImage( VGImage /*aImage*/ )
sl@0: 	{
sl@0: 	return VGI_OK;
sl@0: 	}
sl@0: 
sl@0: VGI_API_CALL TInt VGISymbianUnBindImage()
sl@0: 	{
sl@0: 	return VGI_OK;
sl@0: 	}
sl@0: