examples/Graphics/coverflow/src/eglrendering.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008 � list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010 � this list of conditions and the following disclaimer in the documentation
00011 � and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013 � may be used to endorse or promote products derived from this software
00014 � without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:� 
00028 */
00033 #include <eikenv.h>
00034 #include <string.h>
00035 #include "eglrendering.h"
00036 #include "engine.h"
00037 #include "openvgengine.h"
00038 #include <hal.h>
00039 
00043 const EGLint    KColorRGB565AttribList[] =
00044                 {
00045                 EGL_RED_SIZE,                   5,
00046                 EGL_GREEN_SIZE,                 6,
00047                 EGL_BLUE_SIZE,                  5,
00048                 EGL_SURFACE_TYPE,               EGL_WINDOW_BIT,
00049                 EGL_RENDERABLE_TYPE,    EGL_OPENVG_BIT,
00050                 EGL_NONE
00051                 };
00052 
00058 CEGLRendering* CEGLRendering::NewL(RWindow& aWindow)
00059         {
00060         CEGLRendering* self = CEGLRendering::NewLC(aWindow);
00061         CleanupStack::Pop(self);
00062         return self;
00063         }
00064 
00070 CEGLRendering* CEGLRendering::NewLC(RWindow& aWindow)
00071         {
00072         CEGLRendering* self = new(ELeave) CEGLRendering(aWindow);
00073         CleanupStack::PushL(self);
00074         self->ConstructL();
00075         return self;
00076         }
00077 
00081 CEGLRendering::~CEGLRendering()
00082         {
00083         // Stop the timer object.
00084         Stop();
00085         delete iTimer;
00086 
00087         if(iCurrentDemo)
00088                 {
00089                 // Deactivate the active drawing surface.
00090                 iCurrentDemo->Deactivate();
00091                 }
00092         delete iCurrentDemo;
00093 
00094         if (iContextVG!=EGL_NO_CONTEXT)
00095                 {
00096                 EGLCheckReturnError(eglDestroyContext(iDisplay,iContextVG));
00097                 }
00098 
00099         if (iSurface!=EGL_NO_SURFACE)
00100                 {
00101                 EGLCheckReturnError(eglDestroySurface(iDisplay,iSurface));
00102                 }
00103 
00104         EGLCheckReturnError(eglTerminate(iDisplay));
00105         EGLCheckReturnError(eglReleaseThread());
00106 
00107         delete iBitmap;
00108         }
00109 
00113 void CEGLRendering::Start()
00114         {
00115         // Start drawing the screen periodically
00116         iTimer->Start(0, KTimerDelay, TCallBack(TimerCallBack,this));
00117         }
00118 
00122 void CEGLRendering::Stop()
00123         {
00124         if(iTimer)
00125                 {
00126                 iTimer->Cancel();
00127                 }
00128         }
00129 
00133 void CEGLRendering::EGLCheckError()
00134         {
00135         EGLint error = eglGetError();
00136         if(error != EGL_SUCCESS)
00137                 {
00138                 User::Panic(_L("EGL error"), error);
00139                 }
00140         }
00141 
00145 void CEGLRendering::VGCheckError()
00146         {
00147         VGint error = vgGetError();
00148         if(error != VG_NO_ERROR)
00149                 {
00150                 User::Panic(_L("OpenVG error"), error);
00151                 }
00152         }
00153 
00158 void CEGLRendering::EGLCheckReturnError(EGLBoolean aBool)
00159         {
00160         if (!aBool)
00161                 {
00162                 User::Panic(_L("EGL return error"),eglGetError());
00163                 }
00164         }
00165 
00170 CEGLRendering::CEGLRendering(RWindow& aWindow)
00171         : iWindow(aWindow)
00172         {
00173         }
00174 
00180 TKeyResponse CEGLRendering::HandleKeyEventL(const TKeyEvent& aKeyEvent)
00181         {
00182         TKeyResponse response = EKeyWasConsumed;
00183         switch (aKeyEvent.iCode)
00184                 {
00185         case EKeySpace:
00186         // Handle the space bar key press event.
00187         // Stop the timer if it is active.
00188         // Start the timer if it is not active.
00189                 iTimer->IsActive()? Stop() : Start();
00190                 break;
00191         // Do nothing for tab, up arrow and down arrow key press events.
00192         case EKeyTab:
00193                 break;
00194         case EKeyUpArrow:
00195                 break;
00196         case EKeyDownArrow:
00197                 break;
00198         case EKeyBackspace:
00199         // Stop displaying mirror images of the cover if backspace key is pressed.
00200                 iShowMirrorToggled = ETrue;
00201         default:
00202                 response = iCurrentDemo->HandleKeyEventL(aKeyEvent);
00203                 break;
00204                 };
00205         return response;
00206         }
00207 
00211 void CEGLRendering::ConstructL()
00212     {
00213         // Refresh the timer.
00214         iTimer = CPeriodic::NewL(CActive::EPriorityIdle);
00215 
00216         const TSize windowSize(iWindow.Size());
00217 
00218         // Create the display object.
00219         iDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
00220         EGLCheckError();
00221 
00222         // Initialise the display object.
00223         EGLint iMajor, iMinor;
00224         // initialise the EGL display connection.
00225         EGLCheckReturnError(eglInitialize(iDisplay, &iMajor, &iMinor));
00226         RDebug::Printf("Vendor string %s", eglQueryString(iDisplay, EGL_VENDOR));
00227         RDebug::Printf("Version string %s", eglQueryString(iDisplay, EGL_VERSION));
00228 
00229         if ( NULL == strstr(eglQueryString(iDisplay, EGL_CLIENT_APIS), "OpenVG") )
00230                 {
00231                 RDebug::Printf("OpenVG not listed in supported client APIs %s", eglQueryString(iDisplay, EGL_CLIENT_APIS));
00232                 }
00233         if ( NULL == strstr(eglQueryString(iDisplay, EGL_EXTENSIONS), "EGL_SYMBIAN_COMPOSITION") )
00234                 {
00235                 CEikonEnv::Static()->InfoMsg(_L("Graphics Composition is not supported in this SymbianOS version"));
00236                 RDebug::Printf("EGL_SYMBIAN_COMPOSITION not listed in extension string %s", eglQueryString(iDisplay, EGL_EXTENSIONS));
00237                 User::Leave(KErrNotSupported);
00238                 }
00239 
00240         // Query the available number of configurations .
00241         EGLint numConfigs;
00242         // Get a list of all EGL frame buffer configurations for iDisplay.
00243         EGLCheckReturnError(eglGetConfigs(iDisplay, iConfig, KMaxConfigs, &numConfigs));
00244 
00245         // Choose the configuration to use
00246         EGLCheckReturnError(eglChooseConfig(iDisplay, KColorRGB565AttribList, iConfig, 1, &numConfigs));
00247         EGLCheckReturnError(eglBindAPI(EGL_OPENVG_API));
00248 
00249         // Create window surface to draw direct to.
00250         // Contents drawn on the iSurface will be directed to iWindow.
00251         iSurface = eglCreateWindowSurface(iDisplay, iConfig[0], &iWindow, NULL);
00252         EGLCheckError();
00253 
00254         TInt redSize, greenSize, blueSize, alphaSize;
00255         // Gets the information about the number of bits of alpha stored in the colour buffer.
00256         EGLCheckReturnError(eglGetConfigAttrib(iDisplay, iConfig[0], EGL_ALPHA_SIZE, &alphaSize));
00257         EGLCheckReturnError(eglGetConfigAttrib(iDisplay, iConfig[0], EGL_RED_SIZE, &redSize));
00258         EGLCheckReturnError(eglGetConfigAttrib(iDisplay, iConfig[0], EGL_GREEN_SIZE, &greenSize));
00259         EGLCheckReturnError(eglGetConfigAttrib(iDisplay, iConfig[0], EGL_BLUE_SIZE, &blueSize));
00260         RDebug::Print(_L("EGLConfig id:%d alpha:%d red:%d green:%d blue:%d"), iConfig[0],
00261                         alphaSize, redSize, greenSize, blueSize);
00262 
00263         // Create context to store surface settings
00264         iContextVG = eglCreateContext(iDisplay, iConfig[0], NULL, NULL);
00265         EGLCheckError();
00266 
00267         CEGLRendering::EGLCheckReturnError(eglMakeCurrent(iDisplay, iSurface, iSurface, iContextVG));
00268         iCurrentDemo = COpenVGEngine::NewL(windowSize,iDisplay,iSurface,iContextVG);
00269         iCurrentDemo->ActivateL();
00270 
00271         User::LeaveIfError(HAL::Get(HAL::EFastCounterFrequency, iFastCounterFrequency));
00272         }
00273 
00274 
00278 void CEGLRendering::UpdateDisplay()
00279         {
00280         // Guard against unexpected re-entrant problem.
00281         if (iBusySwapping)
00282                 {
00283                 return;
00284                 }
00285 
00286         if (iCurrentDemo->IsPending() || (!iCurrentDemo->IsPending() && iShowMirrorToggled))
00287                 {
00288                 iShowMirrorToggled = EFalse;
00289 
00290                 // Updates the contents of the iDisplay.
00291                 iCurrentDemo->Step();
00292                 iBusySwapping = ETrue;
00293 
00294                 // Flush colour buffer to the window surface.
00295                 CEGLRendering::EGLCheckReturnError(eglSwapBuffers(iDisplay, iSurface));
00296                 iBusySwapping = EFalse;
00297 
00298                 TUint currentTimeStamp = User::FastCounter();
00299                 TReal delta = currentTimeStamp - iLastFrameTimeStamp;
00300 
00301                 TReal measuredFrameRate = static_cast<TReal>(iFastCounterFrequency/delta);
00302 
00303                 iLastFrameTimeStamp = currentTimeStamp;
00304                 RDebug::Printf("[Timestamp: %d FrameRate: %f]", iLastFrameTimeStamp, measuredFrameRate);
00305                 }
00306         }
00307 
00313 TInt CEGLRendering::TimerCallBack(TAny* aDemo)
00314         {
00315         static_cast<CEGLRendering*>(aDemo)->UpdateDisplay();
00316         return KErrNone;
00317         }

Generated by  doxygen 1.6.2