vghwinterface/vghwapiwrapper/src/driverapiwrapper.cpp
branchbug235_bringup_0
changeset 53 c2ef9095503a
parent 30 f204b762818d
child 71 243bbc1d70db
equal deleted inserted replaced
52:39e5f73667ba 53:c2ef9095503a
       
     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 // 
       
    15 
       
    16 #include <stdio.h>
       
    17 #include <stdlib.h>
       
    18 #include <EGL/egl.h>//EGL operations will be called a lot from here
       
    19 #include <vg/openvg.h>
       
    20 
       
    21 #include "driverapiwrapper.h"
       
    22 #include "KhronosAPIWrapper.h"
       
    23 
       
    24 
       
    25 bool DriverAPIWrapper::IsSgBackingVGImage( VGImage aImage )
       
    26 	{
       
    27 		for( std::list<VGImage>::iterator cur = m_SgImageVGImages.begin();
       
    28 			 cur != m_SgImageVGImages.end();
       
    29 			 ++cur )
       
    30 		{
       
    31 			if( (*cur) == aImage )
       
    32 			{
       
    33 				return true;
       
    34 			}
       
    35 		}
       
    36 		return false;
       
    37 	}
       
    38 
       
    39 bool DriverAPIWrapper::IsSgBackingPBuffer( EGLSurface aSurface )
       
    40 	{
       
    41 		for( std::list<EGLSurface>::iterator cur = m_SgImagePbuffers.begin();
       
    42 			 cur != m_SgImagePbuffers.end();
       
    43 			 ++cur )
       
    44 		{
       
    45 			if( (*cur) == aSurface )
       
    46 			{
       
    47 				return true;
       
    48 			}
       
    49 		}
       
    50 		return false;
       
    51 	}
       
    52 
       
    53 int DriverAPIWrapper::CreatePoolContext()
       
    54 	{
       
    55 	//Initialize EGL on the first run, as per 5.1 in the "Implementing SgImage" design doc
       
    56 	if( m_initialized ){ return 0; }
       
    57 	TRACE("DriverAPIWrapper::CreatePoolContext() ->\n");
       
    58     EGLDisplay display(0);
       
    59 
       
    60     EGLConfig config(0);
       
    61 	EGLint num_config;
       
    62 
       
    63 	EGLContext PoolContext(0);//for sharing
       
    64     EGLContext ContextWithFormatARGB_8888_PRE(0);//for sharing
       
    65     EGLContext ContextWithFormatARGB_8888(0);
       
    66     EGLContext ContextWithFormatXRGB_8888(0);
       
    67     EGLContext ContextWithFormatRGB_565(0);
       
    68     EGLContext ContextWithFormatA_8(0);
       
    69     EGLSurface DudSurface(0);
       
    70 
       
    71 	int red(0);
       
    72 	int green(0);
       
    73 	int blue(0);
       
    74 	int alpha(0);
       
    75     
       
    76 	display = ::eglGetDisplay( EGL_DEFAULT_DISPLAY );//Notice: there is no support for multiple displays here
       
    77 
       
    78 	if( !::eglInitialize(display, NULL, NULL) )
       
    79 	{
       
    80 		TRACE("DriverAPIWrapper::CreatePoolContext() <- (eglInitialize failed)\n");
       
    81 		return 0;
       
    82 	}
       
    83 
       
    84 	m_Dpy = display;
       
    85 	//Pool Context
       
    86     //get a Configuration, then get the context
       
    87 	if( ::eglChooseConfig(	display,
       
    88 							getColorAttributes( EUidPixelFormatARGB_8888, red, green, blue, alpha ),
       
    89 							&config,
       
    90 							1,
       
    91 							&num_config ) == EGL_TRUE )
       
    92 	{
       
    93 		/* create an EGL rendering context */
       
    94 		PoolContext = ContextWithFormatARGB_8888 = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
       
    95 		m_PoolConfig = config;
       
    96 	}
       
    97 
       
    98 
       
    99     /* get an appropriate EGL surface buffer configuration */
       
   100 	if( ::eglChooseConfig(	display,
       
   101 							getColorAttributes( EUidPixelFormatXRGB_8888, red, green, blue, alpha ),
       
   102 							&config,
       
   103 							1,
       
   104 							&num_config ) == EGL_TRUE )
       
   105 	{
       
   106 		/* create an EGL rendering context */
       
   107 		PoolContext = ContextWithFormatXRGB_8888 = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
       
   108 	}
       
   109 
       
   110 
       
   111     //get a Configuration, then get the context
       
   112 	if( ::eglChooseConfig(	display,
       
   113 							getColorAttributes( EUidPixelFormatARGB_8888_PRE, red, green, blue, alpha ),
       
   114 							&config,
       
   115 							1,
       
   116 							&num_config ) == EGL_TRUE )
       
   117 	{
       
   118 		/* create an EGL rendering context */
       
   119 		ContextWithFormatARGB_8888_PRE = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
       
   120 	}
       
   121 	
       
   122 
       
   123     //get a Configuration, then get the context
       
   124 	if( ::eglChooseConfig(	display,
       
   125 							getColorAttributes( EUidPixelFormatRGB_565, red, green, blue, alpha ),
       
   126 							&config,
       
   127 							1,
       
   128 							&num_config ) == EGL_TRUE )
       
   129 	{
       
   130 		/* create an EGL rendering context */
       
   131 		ContextWithFormatRGB_565 = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
       
   132 	}
       
   133 	
       
   134 
       
   135     //get a Configuration, then get the context
       
   136 	if( ::eglChooseConfig(	display,
       
   137 							getColorAttributes( EUidPixelFormatA_8, red, green, blue, alpha ),
       
   138 							&config,
       
   139 							1,
       
   140 							&num_config ) == EGL_TRUE )
       
   141 	{
       
   142 		/* create an EGL rendering context */
       
   143 		ContextWithFormatA_8 = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
       
   144 	}
       
   145 
       
   146 
       
   147 	EGLint attribs[] = { EGL_NONE };
       
   148 	DudSurface = ::eglCreatePbufferSurface( display, PoolContext, attribs );
       
   149 
       
   150 
       
   151 	TRACE( "PoolContext = %u\n", PoolContext );
       
   152 	TRACE( "ContextWithFormatARGB_8888_PRE = %u\n", ContextWithFormatARGB_8888_PRE );
       
   153 	TRACE( "ContextWithFormatARGB_8888 = %u\n", ContextWithFormatARGB_8888 );
       
   154 	TRACE( "ContextWithFormatXRGB_8888 = %u\n", ContextWithFormatXRGB_8888 );
       
   155 	TRACE( "ContextWithFormatRGB_565 = %u\n", ContextWithFormatRGB_565 );
       
   156 	TRACE( "ContextWithFormatA_8 = %u\n", ContextWithFormatA_8 );
       
   157 	TRACE( "DummySurface = %u\n", DudSurface );
       
   158 
       
   159 	m_Dpy								= display;
       
   160 	m_PoolContext						= PoolContext;
       
   161     m_ContextWithFormatARGB_8888_PRE	= ContextWithFormatARGB_8888_PRE;
       
   162     m_ContextWithFormatARGB_8888		= ContextWithFormatARGB_8888;
       
   163     m_ContextWithFormatXRGB_8888		= ContextWithFormatXRGB_8888;
       
   164     m_ContextWithFormatRGB_565			= ContextWithFormatRGB_565;
       
   165     m_ContextWithFormatA_8				= ContextWithFormatA_8;
       
   166     m_DudSurface						= DudSurface;
       
   167 
       
   168 	//m_currentFunctionCall.AppendEGLContext( PoolContext );//for sharing
       
   169     //m_currentFunctionCall.AppendEGLContext( ContextWithFormatARGB_8888_PRE );//for sharing
       
   170     //m_currentFunctionCall.AppendEGLContext( ContextWithFormatARGB_8888 );
       
   171     //m_currentFunctionCall.AppendEGLContext( ContextWithFormatXRGB_8888 );
       
   172     //m_currentFunctionCall.AppendEGLContext( ContextWithFormatRGB_565 );
       
   173     //m_currentFunctionCall.AppendEGLContext( ContextWithFormatA_8 );
       
   174 	//m_currentFunctionCall.AppendEGLSurface( DudSurface );
       
   175 
       
   176 	TRACE("DriverAPIWrapper::CreatePoolContext() <-\n");
       
   177 	m_initialized = true;
       
   178 	return 1;
       
   179 	}
       
   180 
       
   181 
       
   182 
       
   183 
       
   184 /**
       
   185 * This code is supposed to create a pbuffer to back an sgImage
       
   186 * TODO: not tested at all
       
   187 */
       
   188 int DriverAPIWrapper::CreatePbufferSg(  )
       
   189 	{
       
   190 	TRACE("DriverAPIWrapper::CreatePbufferSg ->\n");
       
   191 	EGLint w;
       
   192 	EGLint h;
       
   193 
       
   194 	if( !m_initialized )
       
   195 		{
       
   196 		CreatePoolContext();
       
   197 		}
       
   198 	
       
   199 	m_currentFunctionCall.GetEGLint( w, 0 );
       
   200 	m_currentFunctionCall.GetEGLint( h, 1 );
       
   201 
       
   202 	//eglCreatePbufferSurface
       
   203 	EGLint pbuffer_surface_attribs[] =
       
   204 	{
       
   205       EGL_WIDTH,    w,
       
   206 	  EGL_HEIGHT,   h,
       
   207       EGL_NONE
       
   208 	};
       
   209 	
       
   210     EGLSurface surface = ::eglCreatePbufferSurface( m_Dpy, m_PoolConfig, pbuffer_surface_attribs );
       
   211 	if( surface != EGL_NO_SURFACE )
       
   212 	{
       
   213 		m_SgImagePbuffers.push_back( surface );
       
   214 	}
       
   215 	TRACE("DriverAPIWrapper::CreatePbufferSg surface=%u \n", surface );
       
   216     m_currentFunctionCall.SetReturnValue( (TUint32)surface );
       
   217     TRACE("DriverAPIWrapper::CreatePbufferSg <-\n");
       
   218     return WriteReply();
       
   219 	}
       
   220 
       
   221 
       
   222 /**
       
   223 * This code is supposed to create a pbuffer to back an sgImage
       
   224 * TODO: not tested at all
       
   225 */
       
   226 int DriverAPIWrapper::CreateVGImageSg(  )
       
   227 	{
       
   228 	TRACE("DriverAPIWrapper::CreateVGImageSg ->\n");
       
   229 	EGLint w;
       
   230 	EGLint h;
       
   231 	EGLint pixelformat;
       
   232 	VGImage img;
       
   233 
       
   234 	if( !m_initialized )
       
   235 	{
       
   236 		CreatePoolContext();
       
   237 	}
       
   238 	
       
   239 	EGLenum previousApi = ::eglQueryAPI();
       
   240 	::eglBindAPI( EGL_OPENVG_API );
       
   241 
       
   242 	m_currentFunctionCall.GetEGLint( w, 0 );
       
   243 	m_currentFunctionCall.GetEGLint( h, 1 );
       
   244 	m_currentFunctionCall.GetEGLint( pixelformat, 2 );
       
   245 
       
   246 	::eglMakeCurrent( m_Dpy, m_DudSurface, m_DudSurface, m_PoolContext );
       
   247 
       
   248 	//eglCreatePbufferSurface
       
   249 	EGLint pbuffer_surface_attribs[] =
       
   250 	{
       
   251       EGL_WIDTH,    w,
       
   252 	  EGL_HEIGHT,   h,
       
   253       EGL_NONE
       
   254 	};
       
   255 	VGImageFormat format = getVGColorFormat( pixelformat );
       
   256 	img = ::vgCreateImage(format, w, h, VG_IMAGE_QUALITY_NONANTIALIASED);//todo: quality; how to decide (TSgImageInfo only provides w,h, pixelformat)?
       
   257 		if( img != VG_INVALID_HANDLE )
       
   258 	{
       
   259 		m_SgImageVGImages.push_back( img );
       
   260 	}
       
   261 
       
   262     m_currentFunctionCall.SetReturnValue( (TUint32)img );
       
   263 	::eglBindAPI( previousApi );
       
   264     TRACE("DriverAPIWrapper::CreateVGImageSg <-\n");
       
   265     return WriteReply();
       
   266 	}
       
   267 
       
   268 int DriverAPIWrapper::DeleteSgImage()
       
   269 {
       
   270 	EGLSurface surface;
       
   271 	VGImage image;
       
   272 	m_currentFunctionCall.GetEGLSurface( surface, 0 );
       
   273 	m_currentFunctionCall.GetVGParamValue( image, 1 );
       
   274 
       
   275 	eglDestroySurface( m_Dpy, surface );
       
   276 	vgDestroyImage( image );
       
   277 
       
   278 	return WriteReply();
       
   279 }
       
   280 
       
   281 void DriverAPIWrapper::SyncVGImageFromPBuffer( EGLSurface aPbuffer, VGImage aVGImage )
       
   282 {
       
   283 	TRACE("DriverAPIWrapper::SyncVGImageFromPBuffer ->\n");
       
   284 	//Store the previous current
       
   285 	EGLDisplay disp = ::eglGetCurrentDisplay();
       
   286 	EGLSurface draw = ::eglGetCurrentSurface( EGL_DRAW );
       
   287 	EGLSurface read = ::eglGetCurrentSurface( EGL_READ );
       
   288 	EGLContext ctx  = ::eglGetCurrentContext();
       
   289 
       
   290 	EGLint width(0);
       
   291 	EGLint height(0);
       
   292 	eglSurfaceAttrib( disp, aPbuffer, EGL_WIDTH, width );
       
   293 	eglSurfaceAttrib( disp, aPbuffer, EGL_HEIGHT, height );
       
   294 
       
   295 	::eglMakeCurrent( m_Dpy, aPbuffer, aPbuffer, m_PoolContext );
       
   296 	::vgGetPixels( aVGImage, 0, 0, 0, 0, width, height );
       
   297 	//Set the previous current
       
   298 	::eglMakeCurrent( disp, draw, read, ctx );
       
   299 	TRACE("DriverAPIWrapper::SyncVGImageFromPBuffer <-\n");
       
   300 }
       
   301 
       
   302 
       
   303 int DriverAPIWrapper::SyncPBufferFromVGImage( VGImage aSrc, VGint aWidth, VGint aHeight )
       
   304 {
       
   305 	TRACE("DriverAPIWrapper::SyncPBufferFromVGImage ->\n");
       
   306 	if( ((VGboolean)vgGeti( VG_SCISSORING )) == VG_TRUE )
       
   307 	{
       
   308 		::vgSeti( VG_SCISSORING, VG_FALSE );
       
   309 		//The surface is the same size, as the vgimage
       
   310 		::vgSetPixels( 0, 0, aSrc, 0, 0, aWidth, aHeight );
       
   311 		::vgSeti( VG_SCISSORING, VG_TRUE );
       
   312 	}
       
   313 	else
       
   314 	{
       
   315 		::vgSetPixels( 0, 0, aSrc, 0, 0, aWidth, aHeight );
       
   316 	}
       
   317 	TRACE("DriverAPIWrapper::SyncPBufferFromVGImage <-\n");
       
   318 	return 0;
       
   319 }
       
   320 
       
   321 
       
   322 VGImageFormat DriverAPIWrapper::getVGColorFormat( int aPixelFormat )
       
   323 {
       
   324 	switch( aPixelFormat )
       
   325 		{
       
   326 		case  EUidPixelFormatXRGB_8888:
       
   327 			{
       
   328 			return VG_sXRGB_8888;
       
   329 			}
       
   330 		case EUidPixelFormatARGB_8888:
       
   331 			{
       
   332 			return VG_sARGB_8888;
       
   333 			}
       
   334 		case EUidPixelFormatARGB_8888_PRE:
       
   335 			{
       
   336 			return VG_sARGB_8888_PRE;
       
   337 			}
       
   338 		case EUidPixelFormatRGB_565:
       
   339 			{
       
   340 			return VG_sRGB_565;
       
   341 			}
       
   342 		case EUidPixelFormatA_8:
       
   343 			{
       
   344 			return VG_A_8;
       
   345 			}
       
   346 		default:
       
   347 			{
       
   348 			return VG_sARGB_8888;
       
   349 			}
       
   350 		}
       
   351 }
       
   352 
       
   353 EGLint* DriverAPIWrapper::getColorAttributes( int aPixelFormat, int& aRed, int& aGreen, int& aBlue, int& aAlpha )
       
   354 	{
       
   355 	switch( aPixelFormat )
       
   356 		{
       
   357 		case  EUidPixelFormatXRGB_8888:
       
   358 			{
       
   359 			aRed = aGreen = aBlue = aAlpha = 8;
       
   360 			break;
       
   361 			}
       
   362 		case EUidPixelFormatARGB_8888:
       
   363 			{
       
   364 			aRed = aGreen = aBlue = aAlpha = 8;
       
   365 			break;
       
   366 			}
       
   367 		case EUidPixelFormatARGB_8888_PRE:
       
   368 			{
       
   369 			aRed = aGreen = aBlue = aAlpha = 8;
       
   370 			break;
       
   371 			}
       
   372 		case EUidPixelFormatRGB_565:
       
   373 			{
       
   374 			aRed = 5; aGreen = 6; aBlue = 5; aAlpha = 0;
       
   375 			break;
       
   376 			}
       
   377 		case EUidPixelFormatA_8:
       
   378 			{
       
   379 			aRed = aGreen = aBlue = 0; aAlpha = 8;
       
   380 			break;
       
   381 			}
       
   382 		default:
       
   383 			{
       
   384 			aRed = aGreen = aBlue = aAlpha = 0;
       
   385 			break;
       
   386 			}
       
   387 		}
       
   388 		static EGLint attribs[] =
       
   389 		{
       
   390 		  EGL_RED_SIZE,   aRed,
       
   391 		  EGL_GREEN_SIZE, aGreen,
       
   392 		  EGL_BLUE_SIZE,  aBlue,
       
   393 		  EGL_ALPHA_SIZE, aAlpha,
       
   394 	      EGL_RENDERABLE_TYPE, EGL_OPENVG_BIT,
       
   395 		  EGL_SURFACE_TYPE,   EGL_PBUFFER_BIT,
       
   396 		  EGL_NONE
       
   397 		};
       
   398 	return attribs;
       
   399 	}
       
   400