Nokia N97 SDK
Example Applications Guide

TextureContainer.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : TextureContainer.cpp
00004  *  Part of     : OpenGLEx / Texture
00005  *
00006  *  Copyright (c) 2004-2006 Nokia Corporation.
00007  *  This material, including documentation and any related
00008  *  computer programs, is protected by copyright controlled by
00009  *  Nokia Corporation.
00010  * ==============================================================================
00011  */
00012 
00013 // INCLUDE FILES
00014 #include "TextureContainer.h"
00015 #include <AknUtils.h> // Pen support. 
00016 
00017 
00018 // ================= MEMBER FUNCTIONS =======================
00019 
00020 // ---------------------------------------------------------
00021 // CTextureContainer::ConstructL(const TRect& aRect)
00022 // EPOC two phased constructor
00023 // EGL code, e.g., OpenGL ES context creation, surface
00024 // creation etc. are located here.
00025 // ---------------------------------------------------------
00026 //
00027 void CTextureContainer::ConstructL(const TRect& aRect, CAknAppUi* aAppUi)
00028     {
00029     iAppUi = aAppUi;
00030     iOpenGlInitialized = EFalse;
00031     CreateWindowL();
00032 
00033     // Do not go to full-screen in touch devices.    
00034     if (AknLayoutUtils::PenEnabled())
00035     {
00036         SetRect(aRect);
00037     }
00038     else
00039     {
00040         SetExtentToWholeScreen();                // Take the whole screen into use
00041     }
00042     ActivateL();
00043 
00044     iFrame = 0;                              // Frame counter
00045 
00046     EGLConfig Config;                        // Describes the format, type and
00047                                              // size of the color buffers and
00048                                              // ancillary buffers for EGLSurface
00049 
00050     /* Get the display for drawing graphics*/
00051     iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
00052     if( iEglDisplay == NULL )
00053     {
00054             _LIT(KEglGetDisplay,    "eglGetDisplay failed");
00055         User::Panic( KEglGetDisplay, 0 );
00056     }
00057 
00058     /* Initialize display */
00059     if( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00060     {
00061             _LIT(KEglInitialize,    "eglInitialize failed");
00062         User::Panic( KEglInitialize, 0 );
00063     }
00064 
00065     EGLConfig *configList = NULL;            // Pointer for EGLConfigs
00066     TInt numOfConfigs = 0;
00067     TInt configSize   = 0;
00068 
00069     /* Get the number of possible EGLConfigs */
00070     if( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00071     {
00072             _LIT(KEglGetConfigs,    "eglGetconfigs failed");
00073         User::Panic( KEglGetConfigs, 0 );
00074     }
00075 
00076     configSize = numOfConfigs;
00077 
00078     /* Allocate memory for the configList */
00079     configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
00080     if( configList == NULL )
00081     {
00082             _LIT(KConfigAlloc,      "config alloc failed");
00083         User::Panic( KConfigAlloc, 0 );
00084     }
00085 
00086     /* Define properties for the wanted EGLSurface.
00087        To get the best possible performance, choose
00088        an EGLConfig with a buffersize matching
00089        the current window's display mode*/
00090     TDisplayMode DMode = Window().DisplayMode();
00091     TInt BufferSize = 0;
00092 
00093     switch(DMode)
00094     {
00095     case(EColor4K):
00096         BufferSize = 12;
00097         break;
00098     case(EColor64K):
00099         BufferSize = 16;
00100         break;
00101     case(EColor16M):
00102         BufferSize = 24;
00103         break;
00104     case(EColor16MU):
00105     case(EColor16MA):
00106         BufferSize = 32;
00107         break;
00108     default:
00109         _LIT(KDModeError,       "unsupported displaymode");
00110         User::Panic( KDModeError, 0 );
00111         break;
00112     }
00113 
00114     // Define properties for requesting a full-screen antialiased window surface
00115     const EGLint attrib_list_fsaa[] =
00116         {
00117                                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
00118         EGL_BUFFER_SIZE,  BufferSize,
00119         EGL_DEPTH_SIZE,   16,
00120                                 EGL_SAMPLE_BUFFERS, 1,
00121                                 EGL_SAMPLES,        4,
00122         EGL_NONE
00123         };
00124 
00125     // Define properties for requesting a non-antialiased window surface
00126     const EGLint attrib_list[] =
00127         {
00128                                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
00129         EGL_BUFFER_SIZE,  BufferSize,
00130         EGL_DEPTH_SIZE,   16,
00131         EGL_NONE
00132         };
00133 
00134     // Choose an EGLConfig that best matches to the properties in attrib_list_fsaa
00135     if ( eglChooseConfig( iEglDisplay, attrib_list_fsaa, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00136         {
00137         _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00138         User::Panic( KChooseConfigFailed, 0 );
00139         }
00140 
00141     // Check if no configurations were found
00142     if ( numOfConfigs == 0 )
00143         {
00144         // No configs with antialising were found. Try to get the non-antialiased config
00145                     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00146                               {
00147                   _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00148                   User::Panic( KChooseConfigFailed, 0 );
00149                               }
00150 
00151                     if ( numOfConfigs == 0 )
00152                               {
00153                               // No configs found without antialiasing
00154                   _LIT( KNoConfig, "Can't find the requested config." );
00155                   User::Panic( KNoConfig, 0 );
00156                   }
00157               }
00158 
00159     Config = configList[0];   // Choose the best EGLConfig. EGLConfigs
00160                               // returned by eglChooseConfig are sorted so
00161                               // that the best matching EGLConfig is first in
00162                               // the list.
00163     User::Free( configList ); // Free configList, not used anymore.
00164 
00165     /* Create a window where the graphics are blitted */
00166     iEglSurface = eglCreateWindowSurface( iEglDisplay, Config, &Window(), NULL );
00167     if( iEglSurface == NULL )
00168     {
00169             _LIT(KEglCreateWindow,  "eglCreateWindow failed");
00170         User::Panic( KEglCreateWindow, 0 );
00171     }
00172 
00173     /* Create a rendering context */
00174     iEglContext = eglCreateContext( iEglDisplay, Config, EGL_NO_CONTEXT, NULL );
00175     if( iEglContext == NULL )
00176     {
00177             _LIT(KEglCreateContext, "eglCreateContext failed");
00178         User::Panic( KEglCreateContext, 0 );
00179     }
00180 
00181     /* Make the context current. Binds context to the current rendering thread
00182        and surface.*/
00183     if( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
00184     {
00185             _LIT(KEglMakeCurrent,   "eglMakeCurrent failed");
00186         User::Panic( KEglMakeCurrent, 0 );
00187     }
00188 
00189     TSize size;
00190     size = this->Size();
00191 
00192     iTexture = CTexture::NewL( size.iWidth, size.iHeight );
00193     iTexture->AppInitL();
00194 
00195     iOpenGlInitialized = ETrue;
00196 
00197     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle ); // Create an active object for
00198                                                            // animating the scene
00199 
00200     iPeriodic->Start( 100, 100, TCallBack( CTextureContainer::DrawCallBack, this ) );
00201 }
00202 
00203 // Destructor
00204 CTextureContainer::~CTextureContainer()
00205 {
00206     delete iPeriodic;
00207 
00208     /* AppExit call is made to release any allocations made in AppInit.
00209      This call has to be made here before we destroy the rendering context. */
00210     if ( !(iTexture == NULL) )
00211         {
00212         iTexture->AppExit();
00213         delete iTexture;
00214         }
00215 
00216     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00217     eglDestroySurface( iEglDisplay, iEglSurface );
00218     eglDestroyContext( iEglDisplay, iEglContext );
00219     eglTerminate( iEglDisplay );                   // Release resources associated
00220                                                    // with EGL and OpenGL ES
00221 }
00222 
00223 // ---------------------------------------------------------
00224 // CTextureContainer::SizeChanged()
00225 // Called by framework when the view size is changed
00226 // ---------------------------------------------------------
00227 //
00228 void CTextureContainer::SizeChanged()
00229     {
00230     if( iOpenGlInitialized && iTexture )
00231         {
00232         TSize size;
00233         size = this->Size();
00234 
00235         iTexture->SetScreenSize( size.iWidth, size.iHeight );
00236         }
00237     }
00238 
00239 
00240 // ---------------------------------------------------------
00241 // CTextureContainer::HandleResourceChange(
00242 //     TInt aType)
00243 // Dynamic screen resize changes by calling the
00244 // SetExtentToWholeScreen() method again.
00245 // ---------------------------------------------------------
00246 //
00247  void CTextureContainer::HandleResourceChange(TInt aType)
00248     {
00249         switch( aType )
00250         {
00251             case KEikDynamicLayoutVariantSwitch:
00252                 // Do not go full-screen in touch devices.    
00253             if (AknLayoutUtils::PenEnabled())
00254             {
00255                 SetRect(iAppUi->ClientRect());
00256             }
00257             else
00258             {
00259                 SetExtentToWholeScreen();                // Take the whole screen into use
00260             }
00261                 break;
00262             }
00263 
00264 
00265     }
00266 
00267 // ---------------------------------------------------------
00268 // CTextureContainer::CountComponentControls() const
00269 // ---------------------------------------------------------
00270 //
00271 TInt CTextureContainer::CountComponentControls() const
00272 {
00273     return 0;
00274 }
00275 
00276 // ---------------------------------------------------------
00277 // CTextureContainer::ComponentControl(TInt aIndex) const
00278 // ---------------------------------------------------------
00279 //
00280 CCoeControl* CTextureContainer::ComponentControl(TInt /*aIndex*/) const
00281 {
00282     return NULL;
00283 }
00284 
00285 // ---------------------------------------------------------
00286 // CTextureContainer::Draw(const TRect& aRect) const
00287 // ---------------------------------------------------------
00288 //
00289 void CTextureContainer::Draw(const TRect& /*aRect*/ ) const
00290 {
00291     // Nothing to do here...
00292 }
00293 
00294 // ---------------------------------------------------------
00295 // CTextureContainer::DrawCallBack( TAny* aInstance )
00296 // Called by the CPeriodic in order to draw the graphics
00297 // ---------------------------------------------------------
00298 //
00299 TInt CTextureContainer::DrawCallBack( TAny* aInstance )
00300 {
00301     CTextureContainer* instance = (CTextureContainer*) aInstance;
00302     if ( instance->iTexture->GetState() == CTexture::ERunning )
00303                 {
00304             /* Check if the textures are loaded before doing any rendering. */
00305             instance->iFrame++;
00306 
00307             /* Call the main OpenGL ES Symbian rendering 'loop' */
00308             instance->iTexture->AppCycle( instance->iFrame );
00309 
00310             /* Call eglSwapBuffers, which blit the graphics to the window */
00311             eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00312 
00313             /* To keep the background light on */
00314             if(!(instance->iFrame%100))  User::ResetInactivityTime();
00315 
00316             /* Suspend the current thread for a short while. Give some time
00317                to other threads and AOs, avoids the ViewSrv error in ARMI and
00318                THUMB release builds. One may try to decrease the callback
00319                function instead of this. */
00320             if(!(instance->iFrame%50))   User::After(0);
00321                 }
00322     return 0;
00323 }
00324 
00325 // ---------------------------------------------------------
00326 // CTextureContainer::HandleControlEventL(
00327 //     CCoeControl* aControl,TCoeEvent aEventType)
00328 // ---------------------------------------------------------
00329 //
00330 void CTextureContainer::HandleControlEventL(
00331     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00332 {
00333 }
00334 
00335 // End of File

© Nokia 2009

Back to top