Nokia N97 SDK
Example Applications Guide

CSimpleCubePbufferContainer Class Reference

#include <SimpleCubePbufferContainer.h>

Inheritance diagram for CSimpleCubePbufferContainer:

List of all members.

Detailed Description

Container control class that handles the OpenGL ES initialization and deinitializations. Also uses the CSimpleCubePbuffer class to do the actual OpenGL ES rendering.

Definition at line 29 of file SimpleCubePbufferContainer.h.


Public Member Functions

void  ConstructL (const TRect &aRect, CAknAppUi *aAppUi)
virtual  ~CSimpleCubePbufferContainer ()

Static Public Member Functions

static TInt  DrawCallBack (TAny *aInstance)

Public Attributes

TInt  iFrame
CSimpleCubePbuffer *  iSimpleCubePbuffer

Private Member Functions

void  SizeChanged ()
void  HandleResourceChange (TInt aType)
TInt  CountComponentControls () const
CCoeControl *  ComponentControl (TInt aIndex) const
void  Draw (const TRect &aRect) const
void  HandleControlEventL (CCoeControl *aControl, TCoeEvent aEventType)

Private Attributes

EGLDisplay  iEglDisplay
EGLSurface  iEglSurface
EGLConfig  iConfig
EGLContext  iEglContext
CPeriodic *  iPeriodic
CWsBitmap *  iPixmap
TBool  iOpenGlInitialized
CAknAppUi *  iAppUi


Constructor & Destructor Documentation

CSimpleCubePbufferContainer::~CSimpleCubePbufferContainer (   )  [virtual]
 

Destructor. Destroys the CPeriodic, CSimpleCubePbuffer and uninitializes OpenGL ES.

Definition at line 214 of file SimpleCubePbufferContainer.cpp.

References CSimpleCubePbuffer::AppExit(), iEglContext, iEglDisplay, iEglSurface, iPeriodic, iPixmap, and iSimpleCubePbuffer.

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     }


Member Function Documentation

void CSimpleCubePbufferContainer::ConstructL (  const TRect &  aRect,
CAknAppUi *  aAppUi
) 
 

EPOC default constructor. Initializes the OpenGL ES for rendering to the PBuffer surface.

Parameters:
aRect  Screen rectangle for container.

Definition at line 27 of file SimpleCubePbufferContainer.cpp.

References CSimpleCubePbuffer::AppInit(), DrawCallBack(), iAppUi, iConfig, iEglContext, iEglDisplay, iEglSurface, iFrame, iOpenGlInitialized, iPeriodic, iPixmap, iSimpleCubePbuffer, and CSimpleCubePbuffer::NewL().

Referenced by CSimpleCubePbufferAppUi::ConstructL().

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     }

int CSimpleCubePbufferContainer::DrawCallBack (  TAny *  aInstance  )  [static]
 

Callback function for the CPeriodic. Calculates the current frame, keeps the background light from turning off and orders the CSimpleCubePbuffer to do the rendering for each frame.

Parameters:
aInstance  Pointer to this instance of CSimpleCubePbufferContainer.

Definition at line 358 of file SimpleCubePbufferContainer.cpp.

References CSimpleCubePbuffer::AppCycle(), iEglDisplay, iEglSurface, iFrame, iPixmap, and iSimpleCubePbuffer.

Referenced by ConstructL().

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     }

void CSimpleCubePbufferContainer::SizeChanged (   )  [private]
 

Method from CoeControl that gets called when the display size changes. If OpenGL has been initialized, notifies the renderer class that the screen size has changed.

Definition at line 240 of file SimpleCubePbufferContainer.cpp.

References iConfig, iEglContext, iEglDisplay, iEglSurface, iOpenGlInitialized, iPixmap, iSimpleCubePbuffer, and CSimpleCubePbuffer::SetScreenSize().

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     }

void CSimpleCubePbufferContainer::HandleResourceChange (  TInt  aType  )  [private]
 

Handles a change to the control's resources. This method reacts to the KEikDynamicLayoutVariantSwitch event (that notifies of screen size change) by calling the SetExtentToWholeScreen() again so that this control fills the new screen size. This will then trigger a call to the SizeChanged() method.

Parameters:
aType  Message UID value, only KEikDynamicLayoutVariantSwitch is handled by this method.

Definition at line 308 of file SimpleCubePbufferContainer.cpp.

References iAppUi.

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     }

TInt CSimpleCubePbufferContainer::CountComponentControls (   )  const [private]
 

Method from CoeControl. Does nothing in this implementation.

Definition at line 330 of file SimpleCubePbufferContainer.cpp.

00331     {
00332     return 0;
00333     }

CCoeControl * CSimpleCubePbufferContainer::ComponentControl (  TInt  aIndex  )  const [private]
 

Method from CCoeControl. Does nothing in this implementation.

Definition at line 339 of file SimpleCubePbufferContainer.cpp.

00340     {
00341     return NULL;
00342     }

void CSimpleCubePbufferContainer::Draw (  const TRect &  aRect  )  const [private]
 

Method from CCoeControl. Does nothing in this implementation. All rendering is done in the DrawCallBack() method.

Definition at line 347 of file SimpleCubePbufferContainer.cpp.

References iPixmap.

00348     {
00349     CWindowGc& gc = SystemGc();
00350     gc.BitBlt( TPoint( 0, 0 ), iPixmap );
00351     }

void CSimpleCubePbufferContainer::HandleControlEventL (  CCoeControl *  aControl,
TCoeEvent  aEventType
)  [private]
 

Method from MCoeControlObserver that handles an event from the observed control. Does nothing in this implementation.

Parameters:
aControl  Control changing its state.
aEventType  Type of the control event.

Definition at line 396 of file SimpleCubePbufferContainer.cpp.

00398     {
00399     }


Member Data Documentation

EGLDisplay CSimpleCubePbufferContainer::iEglDisplay [private]
 

Display where the OpenGL ES surface resides.

Definition at line 101 of file SimpleCubePbufferContainer.h.

Referenced by ConstructL(), DrawCallBack(), SizeChanged(), and ~CSimpleCubePbufferContainer().

EGLSurface CSimpleCubePbufferContainer::iEglSurface [private]
 

PBuffer surface that is the target of OpenGL ES graphics rendering.

Definition at line 104 of file SimpleCubePbufferContainer.h.

Referenced by ConstructL(), DrawCallBack(), SizeChanged(), and ~CSimpleCubePbufferContainer().

EGLConfig CSimpleCubePbufferContainer::iConfig [private]
 

Describes the format, type and size of the color buffers and ancillary buffers for EGLSurface.

Definition at line 107 of file SimpleCubePbufferContainer.h.

Referenced by ConstructL(), and SizeChanged().

EGLContext CSimpleCubePbufferContainer::iEglContext [private]
 

OpenGL ES rendering context.

Definition at line 110 of file SimpleCubePbufferContainer.h.

Referenced by ConstructL(), SizeChanged(), and ~CSimpleCubePbufferContainer().

CPeriodic* CSimpleCubePbufferContainer::iPeriodic [private]
 

Active object that is the timing source for the animation.

Definition at line 113 of file SimpleCubePbufferContainer.h.

Referenced by ConstructL(), and ~CSimpleCubePbufferContainer().

TBool CSimpleCubePbufferContainer::iOpenGlInitialized [private]
 

Flag that indicates if OpenGL ES has been initialized or not. Used to check if SizeChanged() can react to incoming notifications.

Definition at line 122 of file SimpleCubePbufferContainer.h.

Referenced by ConstructL(), and SizeChanged().

CAknAppUi* CSimpleCubePbufferContainer::iAppUi [private]
 

Application UI class for querying the client rectangle size.

Definition at line 125 of file SimpleCubePbufferContainer.h.

Referenced by ConstructL(), and HandleResourceChange().

TInt CSimpleCubePbufferContainer::iFrame
 

Frame counter variable, used in the animation.

Definition at line 130 of file SimpleCubePbufferContainer.h.

Referenced by ConstructL(), and DrawCallBack().

CSimpleCubePbuffer* CSimpleCubePbufferContainer::iSimpleCubePbuffer
 

Used in DrawCallBack() method to do the actual OpenGL ES rendering.

Definition at line 133 of file SimpleCubePbufferContainer.h.

Referenced by ConstructL(), DrawCallBack(), CSimpleCubePbufferAppUi::HandleCommandL(), SizeChanged(), and ~CSimpleCubePbufferContainer().


The documentation for this class was generated from the following files:

© Nokia 2009

Back to top