os/graphics/graphicstest/uibench/s60/src/eglrendering.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1  // Copyright (c) 2006-2009 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 
    17 #include "eglrendering.h"
    18 #include "engine.h"
    19 #include "openvgengine.h"
    20 
    21 #include <hal.h>
    22 #include <string.h>
    23 
    24 
    25 _LIT(KErrEgl, "EGL error");
    26 _LIT(KErrOpenVg, "OpenVG error");
    27 _LIT(KErrEglReturn, "EGL return error");
    28 
    29 
    30 /** Attributes to be passed into eglChooseConfig */
    31 const EGLint	KColorRGB565AttribList[] =
    32 		{
    33 		EGL_RED_SIZE,			5,
    34 		EGL_GREEN_SIZE,			6,
    35 		EGL_BLUE_SIZE,			5,
    36 		EGL_SURFACE_TYPE,		EGL_WINDOW_BIT,
    37 		EGL_RENDERABLE_TYPE, 	EGL_OPENVG_BIT,
    38 		EGL_NONE
    39 		};
    40 
    41 
    42 CEGLRendering* CEGLRendering::NewL(RWindow& aWindow)
    43 	{
    44 	CEGLRendering* self = CEGLRendering::NewLC(aWindow);
    45 	CleanupStack::Pop(self);
    46 	return self;
    47 	}
    48 
    49 CEGLRendering* CEGLRendering::NewLC(RWindow& aWindow)
    50 	{
    51 	CEGLRendering* self = new(ELeave) CEGLRendering(aWindow);
    52 	CleanupStack::PushL(self);
    53 	self->ConstructL();
    54 	return self;
    55 	}
    56 
    57 CEGLRendering::CEGLRendering(RWindow& aWindow) : iWindow(aWindow),iCount(0)
    58     {
    59     // empty
    60     }
    61 
    62 void CEGLRendering::ConstructL()
    63     {
    64     const TDisplayMode dispMode = iWindow.DisplayMode();
    65     const TSize windowSize(iWindow.Size());
    66 
    67     // Create display object
    68     iDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    69     EGLCheckError();
    70 
    71     // Initialize display object
    72     EGLCheckReturnError(eglInitialize(iDisplay, NULL, NULL));
    73     
    74     // Check that EGL provides the capabilities for this app.
    75     if (NULL == strstr(eglQueryString(iDisplay, EGL_CLIENT_APIS), "OpenVG")) 
    76         {
    77         RDebug::Printf("OpenVG not listed in supported client APIs %s", eglQueryString(iDisplay, EGL_CLIENT_APIS));
    78         User::Leave(KErrNotSupported);
    79         }
    80     if (NULL == strstr(eglQueryString(iDisplay, EGL_EXTENSIONS), "EGL_SYMBIAN_COMPOSITION")) 
    81         {
    82         RDebug::Printf("EGL_SYMBIAN_COMPOSITION not listed in extension string %s", eglQueryString(iDisplay, EGL_EXTENSIONS));
    83         User::Leave(KErrNotSupported);
    84         }
    85     
    86     EGLint numConfigs;
    87     EGLConfig chosenConfig = 0;
    88 
    89     // Choose the config to use
    90     EGLCheckReturnError(eglChooseConfig(iDisplay, KColorRGB565AttribList, &chosenConfig, 1, &numConfigs));
    91     if (numConfigs == 0)
    92         {
    93         RDebug::Printf("No matching configs found", eglQueryString(iDisplay, EGL_EXTENSIONS));
    94         User::Leave(KErrNotSupported);
    95         }
    96     
    97     // Create window surface to draw direct to.
    98     EGLCheckReturnError(eglBindAPI(EGL_OPENVG_API));
    99     iSurface = eglCreateWindowSurface(iDisplay, chosenConfig, &iWindow, NULL);
   100     EGLCheckError();
   101 
   102     TInt redSize, greenSize, blueSize, alphaSize;
   103     EGLCheckReturnError(eglGetConfigAttrib(iDisplay, chosenConfig, EGL_ALPHA_SIZE, &alphaSize));
   104     EGLCheckReturnError(eglGetConfigAttrib(iDisplay, chosenConfig, EGL_RED_SIZE, &redSize));
   105     EGLCheckReturnError(eglGetConfigAttrib(iDisplay, chosenConfig, EGL_GREEN_SIZE, &greenSize));
   106     EGLCheckReturnError(eglGetConfigAttrib(iDisplay, chosenConfig, EGL_BLUE_SIZE, &blueSize));
   107 
   108     // Create context to store surface settings
   109     iContextVG = eglCreateContext(iDisplay, chosenConfig, EGL_NO_CONTEXT, NULL);
   110     EGLCheckError();
   111     
   112     iCurrentDemo = COpenVGEngine::NewL(iWindow,iDisplay,iSurface,iContextVG);
   113     iCurrentDemo->ActivateL();
   114     }
   115 
   116 CEGLRendering::~CEGLRendering()
   117 	{
   118 	delete iCurrentDemo;
   119 	CEGLRendering::EGLCheckReturnError(eglMakeCurrent(iDisplay, iSurface, iSurface, iContextVG));
   120 	if (iContextVG != EGL_NO_CONTEXT)
   121 		{
   122 		EGLCheckReturnError(eglDestroyContext(iDisplay,iContextVG));
   123 		}
   124 	if (iSurface != EGL_NO_SURFACE)
   125 		{
   126 		EGLCheckReturnError(eglDestroySurface(iDisplay,iSurface));
   127 		}
   128 	// Call eglMakeCurrent() to ensure the surfaces and contexts are truly destroyed. 
   129 	EGLCheckReturnError(eglMakeCurrent(iDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
   130 	//EGLCheckReturnError(eglTerminate(iDisplay));
   131 	eglReleaseThread();
   132 	}
   133 
   134 void CEGLRendering::EGLCheckError()
   135 	{
   136 	EGLint error = eglGetError();
   137 	if(error != EGL_SUCCESS)
   138 		{
   139 		User::Panic(KErrEgl, error);
   140 		}
   141 	}
   142 
   143 void CEGLRendering::VGCheckError()
   144 	{
   145 	VGint error = vgGetError();
   146 	if(error != VG_NO_ERROR)
   147 		{
   148 		User::Panic(KErrOpenVg, error);
   149 		}
   150 	}
   151 
   152 void CEGLRendering::EGLCheckReturnError(EGLBoolean aBool)
   153 	{
   154 	if (!aBool)
   155 		{
   156 		User::Panic(KErrEglReturn, eglGetError());
   157  		}
   158 	}
   159 
   160 /**
   161  * Update the display.
   162  */
   163 void CEGLRendering::UpdateDisplay()
   164 	{
   165 	// Guard against unexpected re-entrant problem. Use this flag until the issue is resolved.
   166 	if (iBusySwapping)
   167 		{
   168 		return;
   169 		}
   170 
   171 	if (iCurrentDemo->IsPending() || (!iCurrentDemo->IsPending() && iShowMirrorToggled))
   172 		{	
   173 		// needed to make sure the correct status is active
   174 		CEGLRendering::EGLCheckReturnError(eglMakeCurrent(iDisplay, iSurface, iSurface, iContextVG));
   175 		iShowMirrorToggled = EFalse;
   176 		iCurrentDemo->Step();
   177 		iBusySwapping = ETrue;
   178 		CEGLRendering::EGLCheckReturnError(eglSwapBuffers(iDisplay, iSurface));
   179 		iBusySwapping = EFalse;
   180 		}	
   181 	}