examples/Graphics/coverflow/src/eglrendering.cpp

Go to the documentation of this file.
00001 // eglrendering.cpp
00002 //
00003 // Copyright (c) 2005-2008 Symbian Ltd.  All rights reserved.
00004 //
00009 #include <eikenv.h>
00010 //#include <techview/eiklbx.h>
00011 #include <string.h>
00012 #include "eglrendering.h"
00013 #include "engine.h"
00014 #include "openvgengine.h"
00015 #include <hal.h>
00016 
00020 const EGLint    KColorRGB565AttribList[] =
00021                 {
00022                 EGL_RED_SIZE,                   5,
00023                 EGL_GREEN_SIZE,                 6,
00024                 EGL_BLUE_SIZE,                  5,
00025                 EGL_SURFACE_TYPE,               EGL_WINDOW_BIT,
00026                 EGL_RENDERABLE_TYPE,    EGL_OPENVG_BIT,
00027                 EGL_NONE
00028                 };
00029 
00035 CEGLRendering* CEGLRendering::NewL(RWindow& aWindow)
00036         {
00037         CEGLRendering* self = CEGLRendering::NewLC(aWindow);
00038         CleanupStack::Pop(self);
00039         return self;
00040         }
00041 
00047 CEGLRendering* CEGLRendering::NewLC(RWindow& aWindow)
00048         {
00049         CEGLRendering* self = new(ELeave) CEGLRendering(aWindow);
00050         CleanupStack::PushL(self);
00051         self->ConstructL();
00052         return self;
00053         }
00054 
00058 CEGLRendering::~CEGLRendering()
00059         {
00060         // Stop the timer object.
00061         Stop();
00062         delete iTimer;
00063 
00064         if(iCurrentDemo)
00065                 {
00066                 // Deactivate the active drawing surface.
00067                 iCurrentDemo->Deactivate();
00068                 }
00069         delete iCurrentDemo;
00070 
00071         if (iContextVG!=EGL_NO_CONTEXT)
00072                 {
00073                 EGLCheckReturnError(eglDestroyContext(iDisplay,iContextVG));
00074                 }
00075 
00076         if (iSurface!=EGL_NO_SURFACE)
00077                 {
00078                 EGLCheckReturnError(eglDestroySurface(iDisplay,iSurface));
00079                 }
00080 
00081         EGLCheckReturnError(eglTerminate(iDisplay));
00082         EGLCheckReturnError(eglReleaseThread());
00083 
00084         delete iBitmap;
00085         }
00086 
00090 void CEGLRendering::Start()
00091         {
00092         // Start drawing the screen periodically
00093         iTimer->Start(0, KTimerDelay, TCallBack(TimerCallBack,this));
00094         }
00095 
00099 void CEGLRendering::Stop()
00100         {
00101         if(iTimer)
00102                 {
00103                 iTimer->Cancel();
00104                 }
00105         }
00106 
00110 void CEGLRendering::EGLCheckError()
00111         {
00112         EGLint error = eglGetError();
00113         if(error != EGL_SUCCESS)
00114                 {
00115                 User::Panic(_L("EGL error"), error);
00116                 }
00117         }
00118 
00122 void CEGLRendering::VGCheckError()
00123         {
00124         VGint error = vgGetError();
00125         if(error != VG_NO_ERROR)
00126                 {
00127                 User::Panic(_L("OpenVG error"), error);
00128                 }
00129         }
00130 
00135 void CEGLRendering::EGLCheckReturnError(EGLBoolean aBool)
00136         {
00137         if (!aBool)
00138                 {
00139                 User::Panic(_L("EGL return error"),eglGetError());
00140                 }
00141         }
00142 
00147 CEGLRendering::CEGLRendering(RWindow& aWindow)
00148         : iWindow(aWindow)
00149         {
00150         }
00151 
00157 TKeyResponse CEGLRendering::HandleKeyEventL(const TKeyEvent& aKeyEvent)
00158         {
00159         TKeyResponse response = EKeyWasConsumed;
00160         switch (aKeyEvent.iCode)
00161                 {
00162         case EKeySpace:
00163         // Handle the space bar key press event.
00164         // Stop the timer if it is active.
00165         // Start the timer if it is not active.
00166                 iTimer->IsActive()? Stop() : Start();
00167                 break;
00168         // Do nothing for tab, up arrow and down arrow key press events.
00169         case EKeyTab:
00170                 break;
00171         case EKeyUpArrow:
00172                 break;
00173         case EKeyDownArrow:
00174                 break;
00175         case EKeyBackspace:
00176         // Stop displaying mirror images of the cover if backspace key is pressed.
00177                 iShowMirrorToggled = ETrue;
00178         default:
00179                 response = iCurrentDemo->HandleKeyEventL(aKeyEvent);
00180                 break;
00181                 };
00182         return response;
00183         }
00184 
00188 void CEGLRendering::ConstructL()
00189     {
00190         // Refresh the timer.
00191         iTimer = CPeriodic::NewL(CActive::EPriorityIdle);
00192 
00193         const TSize windowSize(iWindow.Size());
00194 
00195         // Create the display object.
00196         iDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
00197         EGLCheckError();
00198 
00199         // Initialise the display object.
00200         EGLint iMajor, iMinor;
00201         // initialise the EGL display connection.
00202         EGLCheckReturnError(eglInitialize(iDisplay, &iMajor, &iMinor));
00203         RDebug::Printf("Vendor string %s", eglQueryString(iDisplay, EGL_VENDOR));
00204         RDebug::Printf("Version string %s", eglQueryString(iDisplay, EGL_VERSION));
00205 
00206         if ( NULL == strstr(eglQueryString(iDisplay, EGL_CLIENT_APIS), "OpenVG") )
00207                 {
00208                 RDebug::Printf("OpenVG not listed in supported client APIs %s", eglQueryString(iDisplay, EGL_CLIENT_APIS));
00209                 }
00210         if ( NULL == strstr(eglQueryString(iDisplay, EGL_EXTENSIONS), "EGL_SYMBIAN_COMPOSITION") )
00211                 {
00212                 CEikonEnv::Static()->InfoMsg(_L("Graphics Composition is not supported in this SymbianOS version"));
00213                 RDebug::Printf("EGL_SYMBIAN_COMPOSITION not listed in extension string %s", eglQueryString(iDisplay, EGL_EXTENSIONS));
00214                 User::Leave(KErrNotSupported);
00215                 }
00216 
00217         // Query the available number of configurations .
00218         EGLint numConfigs;
00219         // Get a list of all EGL frame buffer configurations for iDisplay.
00220         EGLCheckReturnError(eglGetConfigs(iDisplay, iConfig, KMaxConfigs, &numConfigs));
00221 
00222         // Choose the configuration to use
00223         EGLCheckReturnError(eglChooseConfig(iDisplay, KColorRGB565AttribList, iConfig, 1, &numConfigs));
00224         EGLCheckReturnError(eglBindAPI(EGL_OPENVG_API));
00225 
00226         // Create window surface to draw direct to.
00227         // Contents drawn on the iSurface will be directed to iWindow.
00228         iSurface = eglCreateWindowSurface(iDisplay, iConfig[0], &iWindow, NULL);
00229         EGLCheckError();
00230 
00231         TInt redSize, greenSize, blueSize, alphaSize;
00232         // Gets the information about the number of bits of alpha stored in the colour buffer.
00233         EGLCheckReturnError(eglGetConfigAttrib(iDisplay, iConfig[0], EGL_ALPHA_SIZE, &alphaSize));
00234         EGLCheckReturnError(eglGetConfigAttrib(iDisplay, iConfig[0], EGL_RED_SIZE, &redSize));
00235         EGLCheckReturnError(eglGetConfigAttrib(iDisplay, iConfig[0], EGL_GREEN_SIZE, &greenSize));
00236         EGLCheckReturnError(eglGetConfigAttrib(iDisplay, iConfig[0], EGL_BLUE_SIZE, &blueSize));
00237         RDebug::Print(_L("EGLConfig id:%d alpha:%d red:%d green:%d blue:%d"), iConfig[0],
00238                         alphaSize, redSize, greenSize, blueSize);
00239 
00240         // Create context to store surface settings
00241         iContextVG = eglCreateContext(iDisplay, iConfig[0], NULL, NULL);
00242         EGLCheckError();
00243 
00244         CEGLRendering::EGLCheckReturnError(eglMakeCurrent(iDisplay, iSurface, iSurface, iContextVG));
00245         iCurrentDemo = COpenVGEngine::NewL(windowSize,iDisplay,iSurface,iContextVG);
00246         iCurrentDemo->ActivateL();
00247 
00248         User::LeaveIfError(HAL::Get(HAL::EFastCounterFrequency, iFastCounterFrequency));
00249         }
00250 
00251 
00255 void CEGLRendering::UpdateDisplay()
00256         {
00257         // Guard against unexpected re-entrant problem.
00258         if (iBusySwapping)
00259                 {
00260                 return;
00261                 }
00262 
00263         if (iCurrentDemo->IsPending() || (!iCurrentDemo->IsPending() && iShowMirrorToggled))
00264                 {
00265                 iShowMirrorToggled = EFalse;
00266 
00267                 // Updates the contents of the iDisplay.
00268                 iCurrentDemo->Step();
00269                 iBusySwapping = ETrue;
00270 
00271                 // Flush colour buffer to the window surface.
00272                 CEGLRendering::EGLCheckReturnError(eglSwapBuffers(iDisplay, iSurface));
00273                 iBusySwapping = EFalse;
00274 
00275                 TUint currentTimeStamp = User::FastCounter();
00276                 TReal delta = currentTimeStamp - iLastFrameTimeStamp;
00277 
00278                 TReal measuredFrameRate = static_cast<TReal>(iFastCounterFrequency/delta);
00279 
00280                 iLastFrameTimeStamp = currentTimeStamp;
00281                 RDebug::Printf("[Timestamp: %d FrameRate: %f]", iLastFrameTimeStamp, measuredFrameRate);
00282                 }
00283         }
00284 
00290 TInt CEGLRendering::TimerCallBack(TAny* aDemo)
00291         {
00292         static_cast<CEGLRendering*>(aDemo)->UpdateDisplay();
00293         return KErrNone;
00294         }

Generated by  doxygen 1.6.2