Nokia N97 SDK
Example Applications Guide

SimpleCubePbufferContainer.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : SimpleCubePbufferContainer.cpp
00004  *  Part of     : OpenGLEx / SimpleCubePbuffer
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 
00014 // INCLUDE FILES
00015 #include "SimpleCubePbufferContainer.h"
00016 #include <Eikenv.h>
00017 #include <w32std.h>
00018 #include <AknUtils.h> // Pen support. 
00019 
00020 // ================= MEMBER FUNCTIONS =======================
00021 
00022 // ---------------------------------------------------------
00023 // CSimpleCubePbufferContainer::ConstructL
00024 // EPOC two phased constructor
00025 // ---------------------------------------------------------
00026 //
00027 void CSimpleCubePbufferContainer::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;                              // Initialize frame counter
00045 
00046     // Get the display for drawing graphics
00047     iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
00048     if ( iEglDisplay == NULL )
00049         {
00050         _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00051         User::Panic( KGetDisplayFailed, 0 );
00052         }
00053 
00054     // Initialize display
00055     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00056         {
00057         _LIT(KInitializeFailed, "eglInitialize failed");
00058         User::Panic( KInitializeFailed, 0 );
00059         }
00060 
00061     EGLConfig *configList = NULL;            // Pointer for EGLConfigs
00062     EGLint numOfConfigs = 0;
00063     EGLint configSize   = 0;
00064 
00065     // Get the number of possible EGLConfigs
00066     if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs )
00067          == EGL_FALSE )
00068         {
00069         _LIT(KGetConfigsFailed, "eglGetConfigs failed");
00070         User::Panic( KGetConfigsFailed, 0 );
00071         }
00072 
00073     configSize = numOfConfigs;
00074 
00075     // Allocate memory for the configList
00076     configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
00077     if ( configList == NULL )
00078         {
00079         _LIT(KConfigAllocFailed, "config alloc failed");
00080         User::Panic( KConfigAllocFailed, 0 );
00081         }
00082 
00083     /* Define properties for the wanted EGLSurface
00084        To get the best possible performance, choose
00085        an EGLConfig with a buffersize matching
00086        the current window's display mode*/
00087     TDisplayMode DMode = Window().DisplayMode();
00088     TInt BufferSize = 0;
00089 
00090     switch ( DMode )
00091          {
00092          case(EColor4K):
00093              BufferSize = 12;
00094              break;
00095          case(EColor64K):
00096              BufferSize = 16;
00097              break;
00098          case(EColor16M):
00099              BufferSize = 24;
00100              break;
00101          case(EColor16MU):
00102                      case(EColor16MA):
00103              BufferSize = 32;
00104              break;
00105          default:
00106              _LIT(KDModeError,       "unsupported displaymode");
00107              User::Panic( KDModeError, 0 );
00108              break;
00109          }
00110 
00111     // Define properties for requesting a full-screen antialiased window surface
00112     const EGLint attrib_list_fsaa[] =
00113         {
00114                                 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
00115         EGL_BUFFER_SIZE,  BufferSize,
00116         EGL_DEPTH_SIZE,   16,
00117                                 EGL_SAMPLE_BUFFERS, 1,
00118                                 EGL_SAMPLES,        4,
00119         EGL_NONE
00120         };
00121 
00122     // Define properties for requesting a non-antialiased window surface
00123     const EGLint attrib_list[] =
00124         {
00125                                 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
00126         EGL_BUFFER_SIZE,  BufferSize,
00127         EGL_DEPTH_SIZE,   16,
00128         EGL_NONE
00129         };
00130 
00131     // Choose an EGLConfig that best matches to the properties in attrib_list_fsaa
00132     if ( eglChooseConfig( iEglDisplay, attrib_list_fsaa, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00133         {
00134         _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00135         User::Panic( KChooseConfigFailed, 0 );
00136         }
00137 
00138     // Check if no configurations were found
00139     if ( numOfConfigs == 0 )
00140         {
00141         // No configs with antialising were found. Try to get the non-antialiased config
00142                     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00143                               {
00144                   _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00145                   User::Panic( KChooseConfigFailed, 0 );
00146                               }
00147 
00148                     if ( numOfConfigs == 0 )
00149                               {
00150                               // No configs found without antialiasing
00151                   _LIT( KNoConfig, "Can't find the requested config." );
00152                   User::Panic( KNoConfig, 0 );
00153                   }
00154               }
00155 
00156     iConfig = configList[0];   // Choose the best EGLConfig. EGLConfigs
00157                               // returned by eglChooseConfig are sorted so
00158                               // that the best matching EGLConfig is first in
00159                               // the list.
00160     User::Free( configList ); // Free configList, not used anymore.
00161 
00162 
00163     TSize size;
00164     size = this->Size();
00165 
00166     // Create a Symbian bitmap where the graphics from the Pbuffer are copied
00167     iPixmap = new CWsBitmap( iEikonEnv->WsSession() );
00168     iPixmap->Create( Window().Size(), DMode);
00169 
00170     const EGLint attrib_list2[] = { EGL_WIDTH,
00171                                     size.iWidth,
00172                                     EGL_HEIGHT,
00173                                     size.iHeight,
00174                                     EGL_NONE };
00175 
00176     iEglSurface = eglCreatePbufferSurface( iEglDisplay, iConfig, attrib_list2 );
00177 
00178     if ( iEglSurface == NULL )
00179         {
00180         _LIT(KCreatePbufferSurfaceFailed, "eglCreatePbufferSurface failed");
00181         User::Panic( KCreatePbufferSurfaceFailed, 0 );
00182         }
00183 
00184     // Create a rendering context
00185     iEglContext = eglCreateContext( iEglDisplay, iConfig, EGL_NO_CONTEXT, NULL );
00186     if ( iEglContext == NULL )
00187         {
00188         _LIT(KCreateContextFailed, "eglCreateContext failed");
00189         User::Panic( KCreateContextFailed, 0 );
00190         }
00191 
00192     /* Make the context current. Binds context to the current rendering thread
00193        and surface.*/
00194     if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext )
00195         == EGL_FALSE )
00196         {
00197         _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00198         User::Panic( KMakeCurrentFailed, 0 );
00199         }
00200 
00201 
00202     // Create an instance of SimpleCubePbuffer
00203     iSimpleCubePbuffer = CSimpleCubePbuffer::NewL( size.iWidth, size.iHeight );
00204     iSimpleCubePbuffer->AppInit();                           // Initialize OpenGL ES
00205 
00206     iOpenGlInitialized = ETrue;
00207 
00208     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );  // Create an active object for
00209                                                             // animating the scene
00210     iPeriodic->Start( 100, 100, TCallBack( CSimpleCubePbufferContainer::DrawCallBack, this ) );
00211     }
00212 
00213 // Destructor
00214 CSimpleCubePbufferContainer::~CSimpleCubePbufferContainer()
00215     {
00216     delete iPeriodic;
00217 
00218     /* AppExit call is made to release
00219        any allocations made in AppInit. */
00220     if ( iSimpleCubePbuffer )
00221         {
00222         iSimpleCubePbuffer->AppExit();
00223         delete iSimpleCubePbuffer;
00224         }
00225 
00226     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00227     eglDestroySurface( iEglDisplay, iEglSurface );
00228     eglDestroyContext( iEglDisplay, iEglContext );
00229     eglTerminate( iEglDisplay );                   // Release resources associated
00230                                                    // with EGL and OpenGL ES
00231 
00232     delete iPixmap;
00233     }
00234 
00235 // ---------------------------------------------------------
00236 // CSimpleCubePbufferContainer::SizeChanged
00237 // Called by framework when the view size is changed
00238 // ---------------------------------------------------------
00239 //
00240 void CSimpleCubePbufferContainer::SizeChanged()
00241     {
00242     if( iOpenGlInitialized && iSimpleCubePbuffer )
00243         {
00244         TSize size;
00245         size = this->Size();
00246 
00247         // Delete previous context etc.
00248         eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00249         eglDestroySurface( iEglDisplay, iEglSurface );
00250         eglDestroyContext( iEglDisplay, iEglContext );
00251         if( iPixmap )
00252             {
00253             delete iPixmap;
00254             }
00255 
00256 
00257         // Recreate the Symbian bitmap where the graphics from the Pbuffer are copied
00258 
00259         iPixmap = new CWsBitmap( iEikonEnv->WsSession() );
00260         iPixmap->Create( Window().Size(), Window().DisplayMode() );
00261 
00262         // Recreate and bind the new surface and rendering context
00263         const EGLint attrib_list2[] = { EGL_WIDTH,
00264                                         size.iWidth,
00265                                         EGL_HEIGHT,
00266                                         size.iHeight,
00267                                         EGL_NONE };
00268 
00269         iEglSurface = eglCreatePbufferSurface( iEglDisplay, iConfig, attrib_list2 );
00270 
00271         if ( iEglSurface == NULL )
00272             {
00273             _LIT(KCreatePbufferSurfaceFailed, "eglCreatePbufferSurface failed");
00274             User::Panic( KCreatePbufferSurfaceFailed, 0 );
00275             }
00276 
00277         // Create a rendering context
00278         iEglContext = eglCreateContext( iEglDisplay, iConfig, EGL_NO_CONTEXT, NULL );
00279         if ( iEglContext == NULL )
00280             {
00281             _LIT(KCreateContextFailed, "eglCreateContext failed");
00282             User::Panic( KCreateContextFailed, 0 );
00283             }
00284 
00285         /* Make the context current. Binds context to the current rendering thread
00286            and surface.*/
00287         if ( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext )
00288             == EGL_FALSE )
00289             {
00290             _LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
00291             User::Panic( KMakeCurrentFailed, 0 );
00292             }
00293 
00294 
00295         // Notify the renderer class
00296         iSimpleCubePbuffer->SetScreenSize( size.iWidth, size.iHeight );
00297         }
00298     }
00299 
00300 
00301 // ---------------------------------------------------------
00302 // CSimpleCubePbufferContainer::HandleResourceChange(
00303 //     TInt aType)
00304 // Dynamic screen resize changes by calling the
00305 // SetExtentToWholeScreen() method again.
00306 // ---------------------------------------------------------
00307 //
00308  void CSimpleCubePbufferContainer::HandleResourceChange(TInt aType)
00309     {
00310         switch( aType )
00311         {
00312             case KEikDynamicLayoutVariantSwitch:
00313                 // Do not go full-screen in touch devices.    
00314             if (AknLayoutUtils::PenEnabled())
00315             {
00316                 SetRect(iAppUi->ClientRect());
00317             }
00318             else
00319             {
00320                 SetExtentToWholeScreen();                // Take the whole screen into use
00321             }
00322                 break;
00323             }
00324     }
00325 
00326 // ---------------------------------------------------------
00327 // CSimpleCubePbufferContainer::CountComponentControls
00328 // ---------------------------------------------------------
00329 //
00330 TInt CSimpleCubePbufferContainer::CountComponentControls() const
00331     {
00332     return 0;
00333     }
00334 
00335 // ---------------------------------------------------------
00336 // CSimpleCubePbufferContainer::ComponentControl
00337 // ---------------------------------------------------------
00338 //
00339 CCoeControl* CSimpleCubePbufferContainer::ComponentControl(TInt /*aIndex*/) const
00340     {
00341     return NULL;
00342     }
00343 // ---------------------------------------------------------
00344 // CSimpleCubePbufferContainer::Draw
00345 // ---------------------------------------------------------
00346 //
00347 void CSimpleCubePbufferContainer::Draw(const TRect& /*aRect*/ ) const
00348     {
00349     CWindowGc& gc = SystemGc();
00350     gc.BitBlt( TPoint( 0, 0 ), iPixmap );
00351     }
00352 
00353 // ---------------------------------------------------------
00354 // CSimpleCubePbufferContainer::DrawCallBack
00355 // Called by the CPeriodic in order to draw the graphics
00356 // ---------------------------------------------------------
00357 //
00358 int CSimpleCubePbufferContainer::DrawCallBack( TAny* aInstance )
00359     {
00360     CSimpleCubePbufferContainer* instance
00361       = (CSimpleCubePbufferContainer*) aInstance;
00362     instance->iFrame++;
00363 
00364     // Call the main OpenGL ES Symbian rendering 'loop'
00365     instance->iSimpleCubePbuffer->AppCycle( instance->iFrame );
00366 
00367     // Copy contents of Pbuffer surface to a native Pixmap
00368     eglCopyBuffers( instance->iEglDisplay, instance->iEglSurface, instance->iPixmap );
00369 
00370     /* eglSwapBuffers is not called when using Pixmap surface.
00371        Instead, standard Symbian BitBlt has to be used. */
00372     instance->DrawNow( );
00373 
00374     // To keep the background light on
00375     if ( !(instance->iFrame%100) )
00376         {
00377         User::ResetInactivityTime();
00378         }
00379 
00380     /* Suspend the current thread for a short while. Give some time
00381        to other threads and AOs, avoids the ViewSrv error in ARMI and
00382        THUMB release builds. One may try to decrease the callback
00383        function instead of this. */
00384     if ( !(instance->iFrame%50) )
00385         {
00386         User::After(0);
00387         }
00388 
00389     return 0;
00390     }
00391 
00392 // ---------------------------------------------------------
00393 // CSimpleCubePbufferContainer::HandleControlEventL
00394 // ---------------------------------------------------------
00395 //
00396 void CSimpleCubePbufferContainer::HandleControlEventL(
00397     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00398     {
00399     }
00400 
00401 
00402 // End of File

© Nokia 2009

Back to top