svgtopt/VGRenderer/HWVG/src/HWVGSurfaceImpl.cpp
changeset 0 d46562c3d99d
equal deleted inserted replaced
-1:000000000000 0:d46562c3d99d
       
     1 /*
       
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  CHWVGSurfaceImpl source file
       
    15  *
       
    16 */
       
    17 
       
    18 #include <eikenv.h>
       
    19 
       
    20 #ifdef DEBUG_RENDERER
       
    21 #include <e32debug.h>
       
    22 #endif
       
    23 
       
    24 #include "HWVGSurfaceImpl.h"
       
    25 
       
    26 CHWVGSurfaceImpl::CHWVGSurfaceImpl(TUint8 aOption)
       
    27     : iSurfaceType (aOption)
       
    28     {
       
    29 #ifdef HWRENDERER
       
    30     iEglDisplay                 = EGL_NO_DISPLAY;
       
    31     iEglSurface                 = EGL_NO_SURFACE;
       
    32     iEglPBufferSurface_Client   = EGL_NO_SURFACE;
       
    33     iEglContext                 = EGL_NO_CONTEXT;
       
    34 #endif
       
    35     }
       
    36 
       
    37 CHWVGSurfaceImpl::~CHWVGSurfaceImpl()
       
    38     {
       
    39     }
       
    40 
       
    41 inline void CHWVGSurfaceImpl::ConstructL()
       
    42     {
       
    43     }
       
    44 
       
    45 CHWVGSurfaceImpl* CHWVGSurfaceImpl::NewL(TUint8 aOption)
       
    46     {
       
    47     CHWVGSurfaceImpl* self = CHWVGSurfaceImpl::NewLC(aOption);
       
    48     CleanupStack::Pop();
       
    49     return self;
       
    50     }
       
    51 
       
    52 CHWVGSurfaceImpl* CHWVGSurfaceImpl::NewLC(TUint8 aOption)
       
    53     {
       
    54     CHWVGSurfaceImpl* self = new (ELeave)CHWVGSurfaceImpl(aOption);
       
    55     CleanupStack::PushL(self);
       
    56     self->ConstructL();
       
    57     return self;
       
    58     }
       
    59 
       
    60 TInt CHWVGSurfaceImpl::MapEGLErrorCodeToSymbian(TInt aErrorCode)
       
    61     {
       
    62     switch (aErrorCode)
       
    63         {
       
    64         case EGL_SUCCESS:
       
    65             return KErrNone;
       
    66         case EGL_NOT_INITIALIZED:
       
    67             return KErrNotReady;
       
    68         case EGL_BAD_ACCESS:
       
    69             return KErrPermissionDenied;
       
    70         case EGL_BAD_ALLOC:
       
    71             return KErrNoMemory;
       
    72         case EGL_BAD_CONTEXT:
       
    73         case EGL_BAD_CURRENT_SURFACE:
       
    74         case EGL_BAD_DISPLAY:
       
    75         case EGL_BAD_SURFACE:
       
    76         case EGL_BAD_NATIVE_PIXMAP:
       
    77         case EGL_BAD_NATIVE_WINDOW:
       
    78         case EGL_CONTEXT_LOST:
       
    79             return KErrBadHandle;
       
    80         case EGL_BAD_CONFIG:
       
    81         case EGL_BAD_PARAMETER:
       
    82         case EGL_BAD_MATCH:
       
    83         case EGL_BAD_ATTRIBUTE:
       
    84             return KErrArgument;
       
    85         default:
       
    86             return KErrUnknown;
       
    87         }
       
    88     }
       
    89 
       
    90 TInt CHWVGSurfaceImpl::InitializeSurface(TSize aSize, TInt /*aColorSpace*/)
       
    91     {
       
    92 #ifdef HWRENDERER
       
    93      if((aSize.iWidth==iSize.iWidth) && (aSize.iHeight==iSize.iHeight))
       
    94          {
       
    95          return 0;
       
    96          }
       
    97      
       
    98     if (iSurfaceType == SURFACE_PBUFFER)
       
    99         {
       
   100         iSize = aSize;
       
   101         iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
       
   102         if ( iEglDisplay == EGL_NO_DISPLAY )
       
   103             {
       
   104             return MapEGLErrorCodeToSymbian(eglGetError());
       
   105             }
       
   106         if ( eglInitialize( iEglDisplay, 0, 0 ) == EGL_FALSE )
       
   107             {
       
   108             return MapEGLErrorCodeToSymbian(eglGetError());
       
   109             }
       
   110         if ( eglBindAPI(EGL_OPENVG_API) == EGL_FALSE)
       
   111             {
       
   112             return MapEGLErrorCodeToSymbian(eglGetError());
       
   113             }
       
   114         }
       
   115     else
       
   116         {
       
   117         return KErrNotSupported;
       
   118         }
       
   119     return KErrNone;
       
   120 #else
       
   121     return KErrNotSupported;
       
   122 #endif
       
   123     }
       
   124 
       
   125 void CHWVGSurfaceImpl::SetConfiguration(TInt /*aOption*/, const TAny* /*aValue*/)
       
   126     {
       
   127     //TODO
       
   128     }
       
   129 
       
   130 TInt CHWVGSurfaceImpl::CreateSurface(TInt aDisplayMode, RWindow */*aSurface*/, CFbsBitmap *aBitmap)
       
   131     {
       
   132 #ifdef HWRENDERER
       
   133     if (iSurfaceType == SURFACE_PBUFFER)
       
   134         {
       
   135         EGLConfig *configList   = 0;
       
   136         EGLint numOfConfigs     = 0; 
       
   137         EGLint configSize       = 0;
       
   138         
       
   139         if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) == EGL_FALSE )
       
   140             {
       
   141             return MapEGLErrorCodeToSymbian(eglGetError());
       
   142             }
       
   143         
       
   144         configSize = numOfConfigs;
       
   145         
       
   146         configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
       
   147         if ( configList == NULL )
       
   148             {
       
   149             return MapEGLErrorCodeToSymbian(eglGetError());
       
   150             }
       
   151         
       
   152 
       
   153         TInt BufferSize = TDisplayModeUtils::NumDisplayModeBitsPerPixel(EColor16MA);// / 8;
       
   154 
       
   155 /*        const EGLint attribList[] =
       
   156             {
       
   157             EGL_BUFFER_SIZE,            BufferSize,
       
   158             EGL_RED_SIZE,             5,
       
   159             EGL_GREEN_SIZE,           6,
       
   160             EGL_BLUE_SIZE,            5,
       
   161             EGL_ALPHA_SIZE,           0,
       
   162             EGL_RENDERABLE_TYPE,        EGL_OPENVG_BIT,
       
   163             EGL_SURFACE_TYPE,           EGL_PBUFFER_BIT,
       
   164             EGL_NONE
       
   165             };
       
   166 */
       
   167 
       
   168         const EGLint attribList[] = { 
       
   169                  EGL_RENDERABLE_TYPE,EGL_OPENVG_BIT,
       
   170                  EGL_SURFACE_TYPE,         EGL_PBUFFER_BIT,
       
   171                  EGL_RED_SIZE, 8,
       
   172                  EGL_GREEN_SIZE, 8,
       
   173                  EGL_BLUE_SIZE, 8,
       
   174                  EGL_ALPHA_SIZE, 8,
       
   175                  EGL_NONE 
       
   176                 };
       
   177 
       
   178         if ( eglChooseConfig( iEglDisplay, attribList, configList, configSize, 
       
   179                               &numOfConfigs ) == EGL_FALSE )
       
   180             {
       
   181             User::Free( configList );
       
   182             return MapEGLErrorCodeToSymbian(eglGetError());
       
   183             }
       
   184 
       
   185         iConfig = configList[0]; 
       
   186         User::Free( configList ); 
       
   187 
       
   188         if ( aBitmap != 0 )
       
   189             {
       
   190             iSize = aBitmap->SizeInPixels();
       
   191             }
       
   192 
       
   193         return CreatePBufferSurface();
       
   194         }
       
   195     else
       
   196         {
       
   197         return KErrNotSupported;
       
   198         }
       
   199 #else
       
   200     return KErrNotSupported;
       
   201 #endif
       
   202     }
       
   203 
       
   204 TInt CHWVGSurfaceImpl::CreatePBufferSurface()
       
   205     {
       
   206 #ifdef HWRENDERER
       
   207     const EGLint attribList[] = { EGL_WIDTH,
       
   208                                         iSize.iWidth,
       
   209                                         EGL_HEIGHT,
       
   210                                         iSize.iHeight,
       
   211                                         EGL_NONE };
       
   212 
       
   213     iEglSurface = eglCreatePbufferSurface( iEglDisplay, iConfig, attribList );
       
   214     if (iEglSurface == EGL_NO_SURFACE)
       
   215         {
       
   216         return MapEGLErrorCodeToSymbian(eglGetError());
       
   217         }
       
   218 
       
   219     if (iEglContext == EGL_NO_CONTEXT)
       
   220         {
       
   221         iEglContext = eglCreateContext( iEglDisplay, iConfig, EGL_NO_CONTEXT, NULL );
       
   222         }
       
   223 
       
   224     if (iEglContext == EGL_NO_CONTEXT)
       
   225         {
       
   226         return MapEGLErrorCodeToSymbian(eglGetError());
       
   227         }
       
   228     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
       
   229         {
       
   230            return MapEGLErrorCodeToSymbian(eglGetError());
       
   231         }
       
   232     return KErrNone;
       
   233 #else
       
   234     return KErrNotSupported;
       
   235 #endif
       
   236     }
       
   237 
       
   238 TInt CHWVGSurfaceImpl::ResizeSurface(TSize aSize)
       
   239     {
       
   240 #ifdef HWRENDERER
       
   241     if((aSize.iWidth==iSize.iWidth) && (aSize.iHeight==iSize.iHeight))
       
   242         return KErrNone;
       
   243 
       
   244     if (iSurfaceType == SURFACE_PBUFFER)
       
   245         {
       
   246         if (iEglDisplay)
       
   247             {
       
   248             eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
       
   249             eglDestroySurface( iEglDisplay, iEglSurface ); 
       
   250             //eglDestroyContext( iEglDisplay, iEglContext );
       
   251             }
       
   252 
       
   253         iSize = aSize;
       
   254         return CreatePBufferSurface();
       
   255         }
       
   256     else
       
   257         {
       
   258         return KErrNotSupported;
       
   259         }
       
   260 #else
       
   261     return KErrNotSupported;
       
   262 #endif
       
   263     }
       
   264 
       
   265 TInt CHWVGSurfaceImpl::CopyBitmap(TInt /*aDisplayMode*/,TInt aMaskMode, CFbsBitmap *aBitmap, CFbsBitmap */*aMask*/, TSize /*BmpSize*/)
       
   266     {
       
   267 #ifdef HWRENDERER
       
   268     if (iSurfaceType == SURFACE_PBUFFER)
       
   269         {
       
   270         eglCopyBuffers( iEglDisplay, iEglSurface, aBitmap );
       
   271         }
       
   272 #endif
       
   273     }
       
   274 
       
   275 TInt CHWVGSurfaceImpl::PrepareToBindClientBuffer()
       
   276     {
       
   277     return KErrNone;
       
   278     }
       
   279 
       
   280 
       
   281 
       
   282 TInt CHWVGSurfaceImpl::BindClientBuffer(TInt buffer)
       
   283     {
       
   284 #ifdef HWRENDERER
       
   285     iEglPBufferSurface_Client = eglCreatePbufferFromClientBuffer(iEglDisplay, EGL_OPENVG_IMAGE, buffer, iConfig, 0);
       
   286 
       
   287     if (iEglPBufferSurface_Client == EGL_NO_SURFACE)
       
   288         {
       
   289 #ifdef DEBUG_RENDERER        
       
   290         RDebug::Print(_L("eglCreatePbufferFromClientBuffer Failed %x "), eglGetError());
       
   291 #endif
       
   292         return KErrGeneral;
       
   293         }
       
   294 
       
   295     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
       
   296     if ( eglMakeCurrent( iEglDisplay, iEglPBufferSurface_Client, iEglPBufferSurface_Client, iEglContext ) == EGL_FALSE )
       
   297         {
       
   298 
       
   299 #ifdef DEBUG_RENDERER        
       
   300         RDebug::Print(_L("eglMakeCurrent Failed %x "), eglGetError());
       
   301 #endif
       
   302         return KErrGeneral;
       
   303         }
       
   304 
       
   305 #endif
       
   306     return KErrNone;
       
   307     }
       
   308 
       
   309   
       
   310 
       
   311 TInt CHWVGSurfaceImpl::UnBindClientBuffer()
       
   312     {
       
   313 #ifdef HWRENDERER
       
   314 
       
   315     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
       
   316 
       
   317     if (iEglPBufferSurface_Client)
       
   318         {
       
   319         eglDestroySurface( iEglDisplay, iEglPBufferSurface_Client );
       
   320         iEglPBufferSurface_Client = EGL_NO_SURFACE;
       
   321         }
       
   322 
       
   323     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
       
   324         {
       
   325         return KErrGeneral;
       
   326         }
       
   327 #endif
       
   328     return KErrNone;
       
   329     }
       
   330 
       
   331 
       
   332 
       
   333 void CHWVGSurfaceImpl::TerminateSurface()
       
   334     {
       
   335 #ifdef HWRENDERER
       
   336     if (iEglDisplay != EGL_NO_DISPLAY)
       
   337         {
       
   338         eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
       
   339         if (iEglSurface)
       
   340             {
       
   341             eglDestroySurface( iEglDisplay, iEglSurface );
       
   342             }
       
   343 
       
   344         if (iEglContext)
       
   345             {
       
   346             eglDestroyContext( iEglDisplay, iEglContext );
       
   347             }
       
   348 
       
   349         eglTerminate( iEglDisplay );
       
   350         iEglDisplay = EGL_NO_DISPLAY;
       
   351         }
       
   352 #endif
       
   353     }