graphicstest/uibench/s60/src/eglenvironment.cpp
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 #include "eglenvironment.h"
       
    18 #include "egldefs.h"
       
    19 #include "eglerror.h"
       
    20 
       
    21 #include <gdi.h>
       
    22 
       
    23 
       
    24 _LIT(KErrEgl, "EGL error");
       
    25 //_LIT(KErrDisplayMode, "Unsupported display mode");
       
    26 
       
    27 
       
    28 /** 
       
    29  * Report any error that occurs.
       
    30  */
       
    31 void ProcessEglError()
       
    32 	{
       
    33 	User::Panic(KErrEgl, eglGetError());
       
    34 	}
       
    35 
       
    36 CEglEnvironment* CEglEnvironment::NewL(RWindow& aWindow)
       
    37 	{
       
    38 	CEglEnvironment* self = new(ELeave) CEglEnvironment(aWindow);
       
    39 	CleanupStack::PushL(self);
       
    40 	self->ConstructL();
       
    41 	CleanupStack::Pop(self);
       
    42 	return self;
       
    43 	}
       
    44 
       
    45 CEglEnvironment::CEglEnvironment(RWindow& aWindow) : iWindow(aWindow)
       
    46 	{
       
    47 	// empty
       
    48 	}
       
    49 
       
    50 CEglEnvironment::~CEglEnvironment()
       
    51 	{
       
    52 	// Make sure that this egl status is active
       
    53     eglMakeCurrent(iDisplay, iSurface, iSurface, iContext);
       
    54     if (iContext != EGL_NO_CONTEXT)
       
    55         {
       
    56         eglDestroyContext(iDisplay,iContext);
       
    57         }
       
    58     if (iSurface != EGL_NO_SURFACE)
       
    59         {
       
    60         eglDestroySurface(iDisplay,iSurface);
       
    61         }   
       
    62     // Call eglMakeCurrent() to ensure the surfaces and contexts are truly destroyed. 
       
    63     eglMakeCurrent(iDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
       
    64     //eglTerminate(iDisplay);
       
    65     eglReleaseThread();
       
    66 	}
       
    67 
       
    68 void CEglEnvironment::ConstructL()
       
    69 	{
       
    70 	// Create display object
       
    71 	iDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
       
    72 	if (iDisplay == EGL_NO_DISPLAY)
       
    73 		{
       
    74 		ProcessEglError();
       
    75 		}
       
    76 
       
    77 	// Initialize display object
       
    78 	if (!eglInitialize(iDisplay, NULL, NULL))
       
    79 		{
       
    80 		ProcessEglError();
       
    81 		}
       
    82 
       
    83 	// Query number of configs available
       
    84 	EGLint numConfigs;
       
    85 	if (!eglGetConfigs(iDisplay, iConfig, KMaxConfigs, &numConfigs))
       
    86 		{
       
    87 		ProcessEglError();
       
    88 		}
       
    89 
       
    90 	const TDisplayMode dispMode = iWindow.DisplayMode();
       
    91 	const EGLint* attribList = NULL;
       
    92 	// Select attribs based on current display mode
       
    93 	switch (dispMode)
       
    94 		{
       
    95     case EColor4K:	
       
    96         attribList = KColor4KAttribList;
       
    97         break;
       
    98     case EColor64K:	
       
    99          attribList = KColor64KAttribList;
       
   100         break;
       
   101     case EColor16M:	
       
   102         attribList = KColor16MAttribList;
       
   103         break;
       
   104     case EColor16MA: 
       
   105         attribList = KColor16MAAttribList;
       
   106         break;
       
   107     default:
       
   108         attribList = KColor16MAAttribList;
       
   109         //todo: check why this can panic (which displaymode is used)
       
   110         //User::Panic(KErrDisplayMode, KErrNotSupported);
       
   111 		}
       
   112 	// Choose the config to use
       
   113 	if (!eglChooseConfig(iDisplay, attribList, iConfig, 1, &numConfigs))
       
   114 		{
       
   115 		ProcessEglError();
       
   116 		}
       
   117 	
       
   118 	// Create window surface to draw direct to
       
   119 	iSurface = eglCreateWindowSurface(iDisplay, iConfig[0], &iWindow, NULL);
       
   120 	if (iSurface == EGL_NO_SURFACE)
       
   121 		{
       
   122 		ProcessEglError();
       
   123 		}
       
   124 	
       
   125 	iContext = eglCreateContext(iDisplay, iConfig[0], EGL_NO_CONTEXT, NULL);
       
   126 	if(!iContext)
       
   127         {
       
   128         ProcessEglError();
       
   129         }
       
   130 	}
       
   131 
       
   132 void CEglEnvironment::DrawToWindow() const
       
   133     {
       
   134     if(!eglMakeCurrent(iDisplay, iSurface, iSurface, iContext))
       
   135         {
       
   136         ProcessEglError();
       
   137         }
       
   138     // Flush colour buffer to the window surface
       
   139     if(!eglSwapBuffers(iDisplay, iSurface))
       
   140         {
       
   141         ProcessEglError();
       
   142         }
       
   143     }
       
   144 
       
   145 EGLSurface CEglEnvironment::Surface() const
       
   146 	{
       
   147 	return iSurface;
       
   148 	}
       
   149 
       
   150 EGLDisplay CEglEnvironment::Display() const
       
   151 	{
       
   152 	return iDisplay;
       
   153 	}