os/graphics/egl/egltest/endpointtestsuite/automated/tsrc/egltest_surface.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
/**
sl@0
    17
 @file
sl@0
    18
 @test
sl@0
    19
 @internalComponent - Internal Symbian test code
sl@0
    20
*/
sl@0
    21
sl@0
    22
sl@0
    23
#include "egltest_surface.h"
sl@0
    24
#include "egltest_endpoint_images.h"
sl@0
    25
#include <graphics/surfaceconfiguration.h>
sl@0
    26
#include <e32std.h>
sl@0
    27
#include <e32math.h>
sl@0
    28
#include <VG/vgu.h>
sl@0
    29
sl@0
    30
sl@0
    31
#define SURF_ASSERT(x) { if (!(x)) { RDebug::Printf("ASSERT(%s) failed at %s:%d", #x, __FILE__, __LINE__); User::Panic(_L("ASSERT SURF"), __LINE__); }}
sl@0
    32
sl@0
    33
// Macros for indicating what is what.
sl@0
    34
#define SIZE(x, y) x, y
sl@0
    35
#define Buffers(x) x
sl@0
    36
#define DefaultAlignment 32  // Pick some value that is valid.
sl@0
    37
#define Alignment(x) x
sl@0
    38
#define Stride(x) x
sl@0
    39
#define DefaultStride 0
sl@0
    40
// Note: Offset to first buffer.
sl@0
    41
#define Offset(x) x
sl@0
    42
#define WindowPos(x, y)  x, y
sl@0
    43
#define WindowMode(m) m
sl@0
    44
sl@0
    45
#define LARGEST_POSSIBLE_FLAG 0x80000000
sl@0
    46
sl@0
    47
static const TSurfaceParamsCommon KSurfaceParams[] =
sl@0
    48
{
sl@0
    49
    {
sl@0
    50
        EStandardSurface,
sl@0
    51
        SIZE(100, 100),
sl@0
    52
        Buffers(2),
sl@0
    53
        DefaultAlignment,
sl@0
    54
        DefaultStride,
sl@0
    55
        Offset(0),
sl@0
    56
        EUidPixelFormatARGB_8888_PRE,
sl@0
    57
        EFalse,
sl@0
    58
        { 0 },
sl@0
    59
        WindowPos(0, 0),
sl@0
    60
        WindowMode(EColor16MAP)
sl@0
    61
    },
sl@0
    62
    {
sl@0
    63
        EBadAttribSurface,
sl@0
    64
        SIZE(100, 100),
sl@0
    65
        Buffers(2),
sl@0
    66
        DefaultAlignment,
sl@0
    67
        DefaultStride,
sl@0
    68
        Offset(0),
sl@0
    69
        EUidPixelFormatARGB_8888_PRE,
sl@0
    70
        ETrue,
sl@0
    71
        { 1, 1, EGL_NONE },
sl@0
    72
        WindowPos(0, 0),
sl@0
    73
        WindowMode(EColor16MAP)
sl@0
    74
    },
sl@0
    75
    {
sl@0
    76
        EEmptyAttribSurface,
sl@0
    77
        SIZE(100, 100),
sl@0
    78
        Buffers(2),
sl@0
    79
        DefaultAlignment,
sl@0
    80
        DefaultStride,
sl@0
    81
        Offset(0),
sl@0
    82
        EUidPixelFormatARGB_8888_PRE,
sl@0
    83
        ETrue,
sl@0
    84
        { EGL_NONE },
sl@0
    85
        WindowPos(0, 0),
sl@0
    86
        WindowMode(EColor16MAP)
sl@0
    87
    },
sl@0
    88
    {
sl@0
    89
        EStandard128sqSurface,
sl@0
    90
        SIZE(128, 128),
sl@0
    91
        Buffers(3),
sl@0
    92
        DefaultAlignment,
sl@0
    93
        DefaultStride,
sl@0
    94
        Offset(0),
sl@0
    95
        EUidPixelFormatARGB_8888_PRE,
sl@0
    96
        EFalse,
sl@0
    97
        { 0 },
sl@0
    98
        WindowPos(20, 20),
sl@0
    99
        WindowMode(EColor16MAP)
sl@0
   100
    },
sl@0
   101
    {
sl@0
   102
        EUnusualStrideSurface,
sl@0
   103
        SIZE(167,263),
sl@0
   104
        Buffers(2),
sl@0
   105
        Alignment(8),
sl@0
   106
        Stride(167*4+64),
sl@0
   107
        Offset(200),
sl@0
   108
        EUidPixelFormatARGB_8888_PRE,
sl@0
   109
        EFalse,
sl@0
   110
        { 0 },
sl@0
   111
        WindowPos(0, 0),
sl@0
   112
        WindowMode(EColor16MAP)
sl@0
   113
    },
sl@0
   114
    {
sl@0
   115
        EUnalignedPixelSizeSurface,
sl@0
   116
        SIZE(103, 107),
sl@0
   117
        Buffers(2),
sl@0
   118
        Alignment(8),
sl@0
   119
        Stride(103*4),
sl@0
   120
        Offset(0),
sl@0
   121
        EUidPixelFormatARGB_8888_PRE,
sl@0
   122
        EFalse,
sl@0
   123
        { 0 },
sl@0
   124
        WindowPos(0, 0),
sl@0
   125
        WindowMode(EColor16MAP)
sl@0
   126
    },
sl@0
   127
    {
sl@0
   128
        ELargeSurface,
sl@0
   129
        SIZE(800, 600),
sl@0
   130
        Buffers(2),
sl@0
   131
        DefaultAlignment,
sl@0
   132
        DefaultStride,
sl@0
   133
        Offset(0),
sl@0
   134
        EUidPixelFormatARGB_8888_PRE,
sl@0
   135
        EFalse,
sl@0
   136
        { 0 },
sl@0
   137
        WindowPos(0, 0),
sl@0
   138
        WindowMode(EColor16MAP)
sl@0
   139
    },
sl@0
   140
    {
sl@0
   141
        ELargestPossibleSurface,
sl@0
   142
        SIZE(LARGEST_POSSIBLE_FLAG, LARGEST_POSSIBLE_FLAG),
sl@0
   143
        Buffers(2),
sl@0
   144
        DefaultAlignment,
sl@0
   145
        DefaultStride,
sl@0
   146
        Offset(0),
sl@0
   147
        EUidPixelFormatARGB_8888_PRE,
sl@0
   148
        EFalse,
sl@0
   149
        { 0 },
sl@0
   150
        WindowPos(0, 0),
sl@0
   151
        WindowMode(EColor16MAP)
sl@0
   152
    },    
sl@0
   153
    {
sl@0
   154
        ESmallSurface,
sl@0
   155
        SIZE(16, 16),
sl@0
   156
        Buffers(1),
sl@0
   157
        DefaultAlignment,
sl@0
   158
        DefaultStride,
sl@0
   159
        Offset(0),
sl@0
   160
        EUidPixelFormatARGB_8888_PRE,
sl@0
   161
        EFalse,
sl@0
   162
        { 0 },
sl@0
   163
        WindowPos(0, 0),
sl@0
   164
        WindowMode(EColor16MAP)
sl@0
   165
    },
sl@0
   166
    {
sl@0
   167
        ETinySurface,
sl@0
   168
        SIZE(8, 8),
sl@0
   169
        Buffers(1),
sl@0
   170
        DefaultAlignment,
sl@0
   171
        DefaultStride,
sl@0
   172
        Offset(0),
sl@0
   173
        EUidPixelFormatARGB_8888_PRE,
sl@0
   174
        EFalse,
sl@0
   175
        { 0 },
sl@0
   176
        WindowPos(0, 0),
sl@0
   177
        WindowMode(EColor16MAP)
sl@0
   178
    },
sl@0
   179
};
sl@0
   180
sl@0
   181
const TInt KSurfaceMaxIndex = sizeof(KSurfaceParams) / sizeof(KSurfaceParams[0]);
sl@0
   182
sl@0
   183
struct TSurfaceSize
sl@0
   184
    {
sl@0
   185
    TInt iWidth;
sl@0
   186
    TInt iHeight;
sl@0
   187
    };
sl@0
   188
sl@0
   189
static const TSurfaceSize KSurfaceSizes[] =
sl@0
   190
    {
sl@0
   191
        {  320,  240 },
sl@0
   192
        {  640,  480 },
sl@0
   193
        {  720,  480 },
sl@0
   194
        {  854,  480 },
sl@0
   195
        {  720,  576 },
sl@0
   196
        {  854,  576 },
sl@0
   197
        { 1280,  720 },
sl@0
   198
        { 1024,  768 },
sl@0
   199
        { 1280, 1024 },
sl@0
   200
        { 1920, 1080 },
sl@0
   201
        { 1600, 1200 },
sl@0
   202
#if 0
sl@0
   203
        { 2048, 1536 },
sl@0
   204
        { 2560, 1920 },
sl@0
   205
        { 3648, 2736 },
sl@0
   206
        { 4216, 2638 },
sl@0
   207
        { 4000, 3000 },
sl@0
   208
        { 4616, 2600 },
sl@0
   209
#endif
sl@0
   210
    };
sl@0
   211
sl@0
   212
const TInt KMaxSurfaceSizes =  sizeof(KSurfaceSizes) / sizeof(KSurfaceSizes[0]);
sl@0
   213
sl@0
   214
LOCAL_C TUint RandomNumberInRange(TUint aLow, TUint aHigh)
sl@0
   215
    {
sl@0
   216
    TReal32 rand = Math::Random();
sl@0
   217
    rand /= KMaxTUint;
sl@0
   218
    rand *= aHigh - aLow;
sl@0
   219
    rand += aLow;
sl@0
   220
    return TUint(rand);
sl@0
   221
    }
sl@0
   222
sl@0
   223
sl@0
   224
void CSurface::CreateL(TInt aIndex)
sl@0
   225
    {
sl@0
   226
    CreateL(aIndex, TPoint(0, 0));
sl@0
   227
    }
sl@0
   228
sl@0
   229
sl@0
   230
TSize CSurface::Size()
sl@0
   231
    {
sl@0
   232
    return iActualSize;
sl@0
   233
    }
sl@0
   234
sl@0
   235
sl@0
   236
TInt CSurface::SizeInBytes() const
sl@0
   237
    {
sl@0
   238
    RSurfaceManager::TInfoBuf infoBuf;
sl@0
   239
    RSurfaceManager surfMgr;
sl@0
   240
    TInt err = surfMgr.Open();
sl@0
   241
    if (err != KErrNone)
sl@0
   242
        {
sl@0
   243
        RDebug::Printf("Error opening surface manager... Err=%d", err);
sl@0
   244
        return 0;
sl@0
   245
        }
sl@0
   246
    err = surfMgr.SurfaceInfo(SurfaceId(), infoBuf);
sl@0
   247
    if (err != KErrNone)
sl@0
   248
        {
sl@0
   249
        RDebug::Printf("Could not get surface info - err = %d", err);
sl@0
   250
        return 0;
sl@0
   251
        }
sl@0
   252
    TInt size = infoBuf().iBuffers * infoBuf().iSize.iHeight * infoBuf().iStride;
sl@0
   253
    surfMgr.Close();
sl@0
   254
    return size;
sl@0
   255
    }
sl@0
   256
sl@0
   257
sl@0
   258
CRawSurface* CRawSurface::NewL()
sl@0
   259
    {
sl@0
   260
    CRawSurface* obj = new (ELeave) CRawSurface();
sl@0
   261
    CleanupStack::PushL(obj);
sl@0
   262
    obj->ConstructL();
sl@0
   263
    CleanupStack::Pop(obj);
sl@0
   264
    return obj;
sl@0
   265
    }
sl@0
   266
sl@0
   267
sl@0
   268
sl@0
   269
CRawSurface::CRawSurface() : iDrawBuffer(0), iBuffers(0)
sl@0
   270
    {
sl@0
   271
    }
sl@0
   272
sl@0
   273
sl@0
   274
void CRawSurface::ConstructL()
sl@0
   275
    {
sl@0
   276
    iSurfaceId = TSurfaceId::CreateNullId();
sl@0
   277
    User::LeaveIfError(iSurfaceManager.Open());
sl@0
   278
    User::LeaveIfError(iSurfaceUpdate.Connect());
sl@0
   279
    }
sl@0
   280
sl@0
   281
sl@0
   282
CRawSurface::~CRawSurface()
sl@0
   283
    {
sl@0
   284
    iSurfaceUpdate.Close();
sl@0
   285
    if(!iSurfaceId.IsNull())
sl@0
   286
        {
sl@0
   287
        iSurfaceManager.CloseSurface(iSurfaceId);
sl@0
   288
        }
sl@0
   289
    iSurfaceManager.Close();
sl@0
   290
    }
sl@0
   291
sl@0
   292
sl@0
   293
TInt CRawSurface::PixelSize(TUidPixelFormat aPixelFormat)
sl@0
   294
    {
sl@0
   295
    switch(aPixelFormat)
sl@0
   296
        {
sl@0
   297
        case EUidPixelFormatARGB_8888_PRE:
sl@0
   298
        case EUidPixelFormatARGB_8888:
sl@0
   299
        case EUidPixelFormatABGR_8888:
sl@0
   300
        case EUidPixelFormatABGR_8888_PRE:
sl@0
   301
            return 4;
sl@0
   302
sl@0
   303
        case EUidPixelFormatARGB_4444:
sl@0
   304
        case EUidPixelFormatRGB_565:
sl@0
   305
            return 2;
sl@0
   306
sl@0
   307
        default:
sl@0
   308
            SURF_ASSERT(0);
sl@0
   309
            break;
sl@0
   310
        }
sl@0
   311
    return 0; // Make sure no compiler moans about "not all paths return a value".
sl@0
   312
    }
sl@0
   313
sl@0
   314
sl@0
   315
void CRawSurface::GetSurfAttribs(RSurfaceManager::TSurfaceCreationAttributesBuf &aSurfaceAttribs, 
sl@0
   316
        TInt aIndex, TInt aSizeIndex)
sl@0
   317
    {
sl@0
   318
    SURF_ASSERT(aIndex < KSurfaceMaxIndex);
sl@0
   319
    SURF_ASSERT(aIndex == KSurfaceParams[aIndex].iIndex);
sl@0
   320
    iParamIndex = aIndex;
sl@0
   321
    if (KSurfaceParams[aIndex].iXSize & LARGEST_POSSIBLE_FLAG)
sl@0
   322
        {
sl@0
   323
        
sl@0
   324
        aSurfaceAttribs().iSize = 
sl@0
   325
                TSize(KSurfaceSizes[aSizeIndex].iWidth, KSurfaceSizes[aSizeIndex].iHeight);
sl@0
   326
        }
sl@0
   327
    else
sl@0
   328
        {
sl@0
   329
        aSurfaceAttribs().iSize = 
sl@0
   330
                TSize(KSurfaceParams[aIndex].iXSize, KSurfaceParams[aIndex].iYSize);
sl@0
   331
        }
sl@0
   332
    iBuffers = KSurfaceParams[aIndex].iBuffers;
sl@0
   333
    aSurfaceAttribs().iBuffers = iBuffers;
sl@0
   334
    aSurfaceAttribs().iPixelFormat = KSurfaceParams[aIndex].iPixelFormat;
sl@0
   335
    TInt stride = KSurfaceParams[aIndex].iStrideInBytes;
sl@0
   336
    if (stride == 0)
sl@0
   337
        {
sl@0
   338
        stride = aSurfaceAttribs().iSize.iWidth * PixelSize(KSurfaceParams[aIndex].iPixelFormat);
sl@0
   339
        }
sl@0
   340
    aSurfaceAttribs().iStride = stride;
sl@0
   341
    aSurfaceAttribs().iOffsetToFirstBuffer = KSurfaceParams[aIndex].iOffsetToFirstBuffer;
sl@0
   342
    aSurfaceAttribs().iAlignment = KSurfaceParams[aIndex].iAlignment;
sl@0
   343
    aSurfaceAttribs().iContiguous = EFalse;
sl@0
   344
    aSurfaceAttribs().iCacheAttrib = RSurfaceManager::ECached;
sl@0
   345
    aSurfaceAttribs().iOffsetBetweenBuffers = 0;
sl@0
   346
    aSurfaceAttribs().iSurfaceHints = NULL;
sl@0
   347
    aSurfaceAttribs().iHintCount = 0;
sl@0
   348
    aSurfaceAttribs().iMappable = ETrue;
sl@0
   349
    }
sl@0
   350
sl@0
   351
sl@0
   352
void CRawSurface::CreateL(TInt aIndex, const TPoint &/* aPoint */)
sl@0
   353
    {
sl@0
   354
    RSurfaceManager::TSurfaceCreationAttributesBuf surfaceAttribs;
sl@0
   355
    SURF_ASSERT(aIndex < KSurfaceMaxIndex);
sl@0
   356
    TInt sizeIndex = 0;
sl@0
   357
    if (KSurfaceParams[aIndex].iXSize & LARGEST_POSSIBLE_FLAG)
sl@0
   358
        {
sl@0
   359
        sizeIndex = KMaxSurfaceSizes-1;
sl@0
   360
        
sl@0
   361
        }
sl@0
   362
    TInt err = KErrNone;
sl@0
   363
    do
sl@0
   364
        {
sl@0
   365
        GetSurfAttribs(surfaceAttribs, aIndex, sizeIndex);
sl@0
   366
        err = iSurfaceManager.CreateSurface(surfaceAttribs, iSurfaceId);
sl@0
   367
        iActualSize = surfaceAttribs().iSize;
sl@0
   368
        sizeIndex--;
sl@0
   369
        }
sl@0
   370
    while(err != KErrNone && sizeIndex >= 0);
sl@0
   371
    User::LeaveIfError(err);
sl@0
   372
    }
sl@0
   373
sl@0
   374
sl@0
   375
TUint8* CRawSurface::MapSurfaceAndGetInfoLC(RSurfaceManager::TSurfaceInfoV01& aInfo)
sl@0
   376
    {
sl@0
   377
    SURF_ASSERT(!iSurfaceId.IsNull());
sl@0
   378
    User::LeaveIfError(iSurfaceManager.MapSurface(iSurfaceId, iSurfaceChunk));
sl@0
   379
    CleanupClosePushL(iSurfaceChunk);
sl@0
   380
    RSurfaceManager::TInfoBuf infoBuf;
sl@0
   381
    User::LeaveIfError(iSurfaceManager.SurfaceInfo(iSurfaceId, infoBuf));
sl@0
   382
    aInfo = infoBuf();
sl@0
   383
    TInt offset = -1000;  // So we hopefully detect when it goes horribly wrong.
sl@0
   384
    User::LeaveIfError(iSurfaceManager.GetBufferOffset(iSurfaceId, iDrawBuffer, offset));
sl@0
   385
    SURF_ASSERT(offset >= 0);
sl@0
   386
    return iSurfaceChunk.Base() + offset;
sl@0
   387
    }
sl@0
   388
sl@0
   389
sl@0
   390
void CRawSurface::DrawContentL(TInt aImageIndex)
sl@0
   391
    {
sl@0
   392
    CTestCFbsImage *image = CTestCFbsImage::NewL(aImageIndex);
sl@0
   393
    CleanupStack::PushL(image);
sl@0
   394
sl@0
   395
    RSurfaceManager::TSurfaceInfoV01 info;
sl@0
   396
    TUint8 *dataAddress = MapSurfaceAndGetInfoLC(info);
sl@0
   397
    TInt stride = info.iStride;
sl@0
   398
sl@0
   399
    CFbsBitmap *bitmap = image->Bitmap();
sl@0
   400
    TDisplayMode displaymode = bitmap->DisplayMode();
sl@0
   401
    TInt pixelStride = stride / CFbsBitmap::ScanLineLength(1, displaymode);
sl@0
   402
    for(TInt y = 0; y < image->Size().iHeight; y++)
sl@0
   403
        {
sl@0
   404
        TPtr8 buf(dataAddress + y * stride, stride);
sl@0
   405
sl@0
   406
        // TODO: We need to check that the bitsperpixel matches between the surface and bitmap.
sl@0
   407
        bitmap->GetScanLine(buf, TPoint(0, y), pixelStride, displaymode);
sl@0
   408
        }
sl@0
   409
sl@0
   410
    CleanupStack::PopAndDestroy(2, image);
sl@0
   411
    }
sl@0
   412
sl@0
   413
void CRawSurface::DrawContentL(const TRgb& aColour)
sl@0
   414
    {
sl@0
   415
    //Map the surface and get its info.
sl@0
   416
    RSurfaceManager::TSurfaceInfoV01 surfaceInfo;
sl@0
   417
    TUint32* buffer = (TUint32*)MapSurfaceAndGetInfoLC(surfaceInfo);
sl@0
   418
sl@0
   419
    //Currently this function only supports drawing into ARGB_8888_PRE surfaces.
sl@0
   420
    //This is because the only test that uses this function uses this type of surface.
sl@0
   421
    //If this functionallity needs expanding, you must correctly convert the TRgb colour
sl@0
   422
    //and pack it into the surface buffer correctly.
sl@0
   423
    SURF_ASSERT(surfaceInfo.iPixelFormat == EUidPixelFormatARGB_8888_PRE);
sl@0
   424
sl@0
   425
    TUint32 fillColour = aColour._Color16MAP();
sl@0
   426
sl@0
   427
    //Loop over each pixel in the surface and colour it.
sl@0
   428
    //This is deliberately slow since it is only used for the tearing test
sl@0
   429
    //and we want to spend most of our time drawing so that the chances of the other thread
sl@0
   430
    //picking up a buffer in the middle of drawing is increased.
sl@0
   431
    for(TInt y=0; y < surfaceInfo.iSize.iHeight; ++y)
sl@0
   432
        {
sl@0
   433
        for(TInt x=0; x < surfaceInfo.iSize.iWidth; ++x)
sl@0
   434
            {
sl@0
   435
            buffer[x] = fillColour;
sl@0
   436
            }
sl@0
   437
        buffer += surfaceInfo.iStride >> 2;
sl@0
   438
        }
sl@0
   439
sl@0
   440
    CleanupStack::PopAndDestroy();
sl@0
   441
    }
sl@0
   442
sl@0
   443
sl@0
   444
void CRawSurface::DrawComplexL(const TRgb& aColour)
sl@0
   445
    {
sl@0
   446
    DrawContentL(aColour);
sl@0
   447
    }
sl@0
   448
sl@0
   449
sl@0
   450
TInt CRawSurface::SubmitContent(TBool aShouldWaitForDisplay, TInt /* aRectsIndex */)
sl@0
   451
    {
sl@0
   452
    TRequestStatus displayNotify = KRequestPending;
sl@0
   453
    TTimeStamp timeStamp;
sl@0
   454
sl@0
   455
    if(aShouldWaitForDisplay)
sl@0
   456
        {
sl@0
   457
        Notify(ENotifyWhenDisplayed, displayNotify, 0);
sl@0
   458
        }
sl@0
   459
sl@0
   460
    TInt err = iSurfaceUpdate.SubmitUpdate(KAllScreens, iSurfaceId, iDrawBuffer, NULL);
sl@0
   461
    if (err != KErrNone)
sl@0
   462
        {
sl@0
   463
        if (err != KErrNone)
sl@0
   464
            {
sl@0
   465
            RDebug::Printf("%s:%d: SubmitUpdate gave unexpected error %d", __FILE__, __LINE__, err);
sl@0
   466
            }
sl@0
   467
        return err;
sl@0
   468
        }
sl@0
   469
    iDrawBuffer = (iDrawBuffer + 1) % iBuffers;
sl@0
   470
sl@0
   471
    if(aShouldWaitForDisplay)
sl@0
   472
        {
sl@0
   473
        TUint32 dummy;
sl@0
   474
        err = WaitFor(ENotifyWhenDisplayed, displayNotify, 100 * 1000, dummy);
sl@0
   475
        if (err != KErrNone && err != KErrNotVisible && err != KErrOverflow)
sl@0
   476
            {
sl@0
   477
//            RDebug::Printf("%s:%d: NotifyWhenDisplayed gave unexpected error %d", __FILE__, __LINE__, err);
sl@0
   478
            return err;
sl@0
   479
            }
sl@0
   480
        }
sl@0
   481
    return KErrNone;
sl@0
   482
    }
sl@0
   483
sl@0
   484
sl@0
   485
TSurfaceId CRawSurface::SurfaceId() const
sl@0
   486
    {
sl@0
   487
    return iSurfaceId;
sl@0
   488
    }
sl@0
   489
sl@0
   490
sl@0
   491
void CRawSurface::GetSurfaceParamsL(TSurfaceParamsRemote &aParams)
sl@0
   492
    {
sl@0
   493
    aParams.iCommonParams = KSurfaceParams[iParamIndex];
sl@0
   494
    aParams.iCommonParams.iBuffers = iBuffers; // May have been changed if it's a single buffered surface...
sl@0
   495
    aParams.iSurfaceId = SurfaceId();
sl@0
   496
    }
sl@0
   497
sl@0
   498
const TText *CRawSurface::GetSurfaceTypeStr() const
sl@0
   499
    {
sl@0
   500
    return _S("CRawSurface");
sl@0
   501
    }
sl@0
   502
sl@0
   503
TInt CRawSurface::Notify(TNotification aWhen, TRequestStatus& aStatus, TUint32 aXTimes)
sl@0
   504
    {
sl@0
   505
    aStatus = KRequestPending;
sl@0
   506
    switch(aWhen)
sl@0
   507
        {
sl@0
   508
        case ENotifyWhenAvailable:
sl@0
   509
            iSurfaceUpdate.NotifyWhenAvailable(aStatus);
sl@0
   510
            break;
sl@0
   511
        case ENotifyWhenDisplayed:
sl@0
   512
            iSurfaceUpdate.NotifyWhenDisplayed(aStatus, iTimeStamp);
sl@0
   513
            break;
sl@0
   514
        case ENotifyWhenDispXTimes:
sl@0
   515
            iSurfaceUpdate.NotifyWhenDisplayedXTimes(aXTimes, aStatus);
sl@0
   516
            break;
sl@0
   517
        default:
sl@0
   518
            RDebug::Printf("%s:%d: Invalid notification: %d. Panicking...", __FILE__, __LINE__, aWhen);
sl@0
   519
            User::Panic(_L("CRawSurface::Notify()"), __LINE__);
sl@0
   520
            break;
sl@0
   521
        }
sl@0
   522
    return KErrNone;
sl@0
   523
    }
sl@0
   524
sl@0
   525
sl@0
   526
TInt CRawSurface::WaitFor(TNotification aWhen, TRequestStatus& aStatus, TInt aTimeoutInMicroSeconds, TUint32& aTimeStamp)
sl@0
   527
    {
sl@0
   528
    RTimer timer;
sl@0
   529
    TInt err = timer.CreateLocal();
sl@0
   530
    if (err != KErrNone)
sl@0
   531
        {
sl@0
   532
        RDebug::Printf("%s:%d: Could not create timer... err= %d", __FILE__, __LINE__, err);
sl@0
   533
        return err;
sl@0
   534
        }
sl@0
   535
    TRequestStatus timerStatus = KRequestPending;
sl@0
   536
#if __WINS__
sl@0
   537
    // Windows timer isn't very precise - add some "fuzz" to the timeout to ensure we do not wait "too little".
sl@0
   538
    const TInt KTimeOutExtra = 20000;
sl@0
   539
#else
sl@0
   540
    // On hardware, we should be able to run with less "fuzz".
sl@0
   541
    const TInt KTimeOutExtra = 10000;
sl@0
   542
#endif
sl@0
   543
    timer.HighRes(timerStatus, aTimeoutInMicroSeconds + KTimeOutExtra);
sl@0
   544
    User::WaitForRequest(timerStatus, aStatus);
sl@0
   545
    if (aStatus == KRequestPending)
sl@0
   546
        {
sl@0
   547
        if (aWhen == ENotifyWhenDisplayed)
sl@0
   548
            {
sl@0
   549
            aTimeStamp = User::FastCounter();
sl@0
   550
            }
sl@0
   551
        return KErrTimedOut;
sl@0
   552
        }
sl@0
   553
    if (aWhen == ENotifyWhenDisplayed)
sl@0
   554
        {
sl@0
   555
        aTimeStamp = iTimeStamp();
sl@0
   556
        }
sl@0
   557
    timer.Cancel();
sl@0
   558
    timer.Close();
sl@0
   559
    TInt result = aStatus.Int();
sl@0
   560
    aStatus = KRequestPending;
sl@0
   561
    return result;
sl@0
   562
    }
sl@0
   563
sl@0
   564
const TText *CRawSingleBufferSurface::GetSurfaceTypeStr() const
sl@0
   565
    {
sl@0
   566
    return _S("CRawSingleBufferedSurface");
sl@0
   567
    }
sl@0
   568
sl@0
   569
CRawSingleBufferSurface *CRawSingleBufferSurface::NewL()
sl@0
   570
    {
sl@0
   571
    CRawSingleBufferSurface* obj = new (ELeave) CRawSingleBufferSurface();
sl@0
   572
    CleanupStack::PushL(obj);
sl@0
   573
    obj->ConstructL();
sl@0
   574
    CleanupStack::Pop(obj);
sl@0
   575
    return obj;
sl@0
   576
    }
sl@0
   577
sl@0
   578
sl@0
   579
void CRawSingleBufferSurface::CreateL(TInt aIndex, const TPoint & /*aPoint */)
sl@0
   580
    {
sl@0
   581
    RSurfaceManager::TSurfaceCreationAttributesBuf surfaceAttribs;
sl@0
   582
sl@0
   583
    TInt sizeIndex = 0;
sl@0
   584
    if (KSurfaceParams[aIndex].iXSize & LARGEST_POSSIBLE_FLAG)
sl@0
   585
        {
sl@0
   586
        sizeIndex = KMaxSurfaceSizes-1;
sl@0
   587
        }
sl@0
   588
    TInt err = KErrNone;
sl@0
   589
    do
sl@0
   590
        {
sl@0
   591
        GetSurfAttribs(surfaceAttribs, aIndex, sizeIndex);
sl@0
   592
        iBuffers = 1;
sl@0
   593
        surfaceAttribs().iBuffers = 1;
sl@0
   594
        err = iSurfaceManager.CreateSurface(surfaceAttribs, iSurfaceId);
sl@0
   595
        iActualSize = surfaceAttribs().iSize;
sl@0
   596
        sizeIndex--;
sl@0
   597
        }
sl@0
   598
    while(err != KErrNone && sizeIndex >= 0);
sl@0
   599
    }
sl@0
   600
sl@0
   601
CRawSingleBufferSurface::~CRawSingleBufferSurface()
sl@0
   602
    {
sl@0
   603
    }
sl@0
   604
sl@0
   605
sl@0
   606
TInt CEglSurfaceBase::Activate()
sl@0
   607
    {
sl@0
   608
    if (!eglMakeCurrent(iDisplay, iSurface, iSurface, iContext))
sl@0
   609
        {
sl@0
   610
        EGLint err = eglGetError();
sl@0
   611
        RDebug::Printf("%s:%d: eglMakeCurrent gave error 0x%x", __FILE__, __LINE__, err);
sl@0
   612
        return KErrBadHandle;
sl@0
   613
        }
sl@0
   614
    return KErrNone;
sl@0
   615
    }
sl@0
   616
sl@0
   617
void CEglSurfaceBase::ActivateL()
sl@0
   618
    {
sl@0
   619
    User::LeaveIfError(Activate());
sl@0
   620
    }
sl@0
   621
sl@0
   622
void CEglSurfaceBase::DrawComplexL(const TRgb& aColour)
sl@0
   623
    {
sl@0
   624
    ActivateL();
sl@0
   625
sl@0
   626
    TSize size;
sl@0
   627
    eglQuerySurface(iDisplay, iSurface, EGL_WIDTH, &size.iWidth);
sl@0
   628
    eglQuerySurface(iDisplay, iSurface, EGL_HEIGHT, &size.iHeight);
sl@0
   629
    
sl@0
   630
    //Paint lots of random circles to keep the GPU busy.
sl@0
   631
    for(TInt i=0; i < 300; i++)
sl@0
   632
        {
sl@0
   633
        VGPaint paint = vgCreatePaint();
sl@0
   634
        VGPath path = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 0, 0, VG_PATH_CAPABILITY_APPEND_TO);
sl@0
   635
        
sl@0
   636
        TInt minDim = Min(size.iWidth, size.iHeight);
sl@0
   637
        VGfloat cx = RandomNumberInRange(0, size.iWidth);
sl@0
   638
        VGfloat cy = RandomNumberInRange(0, size.iHeight);
sl@0
   639
        VGfloat diameter = RandomNumberInRange(minDim / 20, minDim / 3);
sl@0
   640
        TRgb fillColour(RandomNumberInRange(0, 255), RandomNumberInRange(0, 255), RandomNumberInRange(0, 255), RandomNumberInRange(0, 255));
sl@0
   641
        
sl@0
   642
        vguEllipse(path, cx, cy, diameter, diameter);
sl@0
   643
        vgSetPaint(paint, VG_FILL_PATH);
sl@0
   644
        vgSetColor(paint, fillColour.Value());
sl@0
   645
        vgDrawPath(path, VG_FILL_PATH);
sl@0
   646
        
sl@0
   647
        vgDestroyPath(path);
sl@0
   648
        vgDestroyPaint(paint);
sl@0
   649
        }
sl@0
   650
    
sl@0
   651
    //Paint the top corner with aColour so we can identify the drawing.
sl@0
   652
    VGfloat fillColour[4];
sl@0
   653
    fillColour[0] = (VGfloat)aColour.Red() / 255.0f;
sl@0
   654
    fillColour[1] = (VGfloat)aColour.Green() / 255.0f;
sl@0
   655
    fillColour[2] = (VGfloat)aColour.Blue() / 255.0f;
sl@0
   656
    fillColour[3] = (VGfloat)aColour.Alpha() / 255.0f;
sl@0
   657
    
sl@0
   658
    vgSetfv(VG_CLEAR_COLOR, 4, fillColour);
sl@0
   659
    vgClear(0, 0, 20, size.iHeight);
sl@0
   660
    }
sl@0
   661
sl@0
   662
void CEglSurfaceBase::DrawContentL(const TRgb& aColour)
sl@0
   663
    {
sl@0
   664
    ActivateL();
sl@0
   665
sl@0
   666
    TSize size;
sl@0
   667
    eglQuerySurface(iDisplay, iSurface, EGL_WIDTH, &size.iWidth);
sl@0
   668
    eglQuerySurface(iDisplay, iSurface, EGL_HEIGHT, &size.iHeight);
sl@0
   669
sl@0
   670
    VGfloat fillColour[4];
sl@0
   671
    fillColour[0] = (VGfloat)aColour.Red() / 255.0f;
sl@0
   672
    fillColour[1] = (VGfloat)aColour.Green() / 255.0f;
sl@0
   673
    fillColour[2] = (VGfloat)aColour.Blue() / 255.0f;
sl@0
   674
    fillColour[3] = (VGfloat)aColour.Alpha() / 255.0f;
sl@0
   675
sl@0
   676
    vgSetfv(VG_CLEAR_COLOR, 4, fillColour);
sl@0
   677
    vgClear(0, 0, size.iWidth, size.iHeight);
sl@0
   678
    }
sl@0
   679
sl@0
   680
void CEglSurfaceBase::CreateL(TInt aIndex, const TPoint &aOffset)
sl@0
   681
    {
sl@0
   682
    SURF_ASSERT(aIndex < KSurfaceMaxIndex);
sl@0
   683
    SURF_ASSERT(aIndex == KSurfaceParams[aIndex].iIndex);
sl@0
   684
sl@0
   685
    TInt sizeIndex = 0;
sl@0
   686
    if (KSurfaceParams[aIndex].iXSize & LARGEST_POSSIBLE_FLAG)
sl@0
   687
        {
sl@0
   688
        sizeIndex = KMaxSurfaceSizes-1;
sl@0
   689
        }
sl@0
   690
    TInt err = KErrNone;
sl@0
   691
    do
sl@0
   692
        {
sl@0
   693
        TRAP(err, DoCreateL(aIndex, aOffset, sizeIndex));
sl@0
   694
        sizeIndex--;
sl@0
   695
        }
sl@0
   696
    while(err != KErrNone && sizeIndex >= 0);
sl@0
   697
    if (err != KErrNone)
sl@0
   698
        {
sl@0
   699
//        RDebug::Printf("%s:%d: err=%d (%d x %d)", __FILE__, __LINE__, err, iActualSize.iWidth, iActualSize.iHeight);
sl@0
   700
        User::Leave(err);
sl@0
   701
        }
sl@0
   702
    }
sl@0
   703
sl@0
   704
TInt CEglSurfaceBase::SubmitContent(TBool aShouldWaitForDisplay, TInt /* aRectsIndex */)
sl@0
   705
    {
sl@0
   706
    TInt err = Activate();
sl@0
   707
    if (err != KErrNone)
sl@0
   708
        {
sl@0
   709
        return err;
sl@0
   710
        }
sl@0
   711
    if (!eglSwapBuffers(iDisplay, iSurface))
sl@0
   712
        {
sl@0
   713
        EGLint err = eglGetError();
sl@0
   714
        RDebug::Printf("%s:%d: eglSwapBuffers gave error 0x%x", __FILE__, __LINE__, err);
sl@0
   715
        return KErrBadHandle;
sl@0
   716
        }
sl@0
   717
    if (aShouldWaitForDisplay)
sl@0
   718
        {
sl@0
   719
        // We are cheating: We just wait for a bit to ensure that the swapbuffer is actually finished.
sl@0
   720
        // There is no way to determine how long this takes, so we just grab a number that should be
sl@0
   721
        // large enough...
sl@0
   722
        User::After(100 * 1000);  // Wait 100ms.
sl@0
   723
        }
sl@0
   724
    return KErrNone;
sl@0
   725
    }
sl@0
   726
sl@0
   727
void CEglSurfaceBase::DrawContentL(TInt aIndex)
sl@0
   728
    {
sl@0
   729
    ActivateL();
sl@0
   730
    CTestVgImage *vgImage = CTestVgImage::NewL(aIndex);
sl@0
   731
    CleanupStack::PushL(vgImage);
sl@0
   732
    vgDrawImage(vgImage->VGImage());
sl@0
   733
    CleanupStack::PopAndDestroy(vgImage);
sl@0
   734
    }
sl@0
   735
sl@0
   736
void CEglSurfaceBase::GetSurfaceParamsL(TSurfaceParamsRemote &aParams)
sl@0
   737
    {
sl@0
   738
    RSurfaceManager surfaceManager;
sl@0
   739
    User::LeaveIfError(surfaceManager.Open());
sl@0
   740
    RSurfaceManager::TInfoBuf infoBuf;
sl@0
   741
    TInt err = surfaceManager.SurfaceInfo(SurfaceId(), infoBuf);
sl@0
   742
    User::LeaveIfError(err);
sl@0
   743
    surfaceManager.Close();
sl@0
   744
    RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
sl@0
   745
    aParams.iSurfaceId = SurfaceId();
sl@0
   746
    aParams.iCommonParams.iAlignment = -1;  // N/A
sl@0
   747
    aParams.iCommonParams.iBuffers = info.iBuffers;
sl@0
   748
    aParams.iCommonParams.iOffsetToFirstBuffer = -1;
sl@0
   749
    aParams.iCommonParams.iPixelFormat = info.iPixelFormat;
sl@0
   750
    aParams.iCommonParams.iStrideInBytes = info.iStride;
sl@0
   751
    aParams.iCommonParams.iXSize = info.iSize.iWidth;
sl@0
   752
    aParams.iCommonParams.iYSize = info.iSize.iHeight;
sl@0
   753
    aParams.iCommonParams.iUseAttribList = KSurfaceParams[iParamIndex].iUseAttribList;
sl@0
   754
    for(TInt i = 0; i < KNumAttribs; i++)
sl@0
   755
        {
sl@0
   756
        aParams.iCommonParams.iAttribs[i] = KSurfaceParams[iParamIndex].iAttribs[i];
sl@0
   757
        }
sl@0
   758
    }
sl@0
   759
sl@0
   760
sl@0
   761
TInt CEglSurfaceBase::Notify(TNotification /*aWhen*/, TRequestStatus& /*aStatus*/, TUint32 /*aXTImes*/)
sl@0
   762
    {
sl@0
   763
    return KErrNotSupported;
sl@0
   764
    }
sl@0
   765
sl@0
   766
TInt CEglSurfaceBase::WaitFor(TNotification /*aWhen*/, TRequestStatus& /*aStatus*/,
sl@0
   767
        TInt /*aTimeoutinMicroseconds*/, TUint32 & /*aTimeStamp*/)
sl@0
   768
    {
sl@0
   769
    return KErrNotSupported;
sl@0
   770
    }
sl@0
   771
sl@0
   772
void CEglSurfaceBase::BaseCreateL(TInt aIndex, EGLint aSurfaceType)
sl@0
   773
    {
sl@0
   774
    iParamIndex = aIndex;
sl@0
   775
    iDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
sl@0
   776
    
sl@0
   777
    EGLint err;
sl@0
   778
    if (iDisplay == EGL_NO_DISPLAY)
sl@0
   779
        {
sl@0
   780
        err = eglGetError();
sl@0
   781
        RDebug::Printf("%s:%d: err = 0x%x", __FILE__, __LINE__, err);
sl@0
   782
        User::Leave(KErrNotSupported);
sl@0
   783
        }
sl@0
   784
    
sl@0
   785
    EGLint nConfigs = 0;
sl@0
   786
    
sl@0
   787
    // TODO: Need to use differnet config attribs based on aIndex.
sl@0
   788
    EGLint configAttribs[] =
sl@0
   789
    {
sl@0
   790
        EGL_BUFFER_SIZE,    32,
sl@0
   791
        EGL_RED_SIZE,       8,
sl@0
   792
        EGL_GREEN_SIZE,     8,
sl@0
   793
        EGL_BLUE_SIZE,      8,
sl@0
   794
        EGL_ALPHA_SIZE,     8,
sl@0
   795
        EGL_SURFACE_TYPE,   EGL_WINDOW_BIT,
sl@0
   796
        EGL_RENDERABLE_TYPE,EGL_OPENVG_BIT,
sl@0
   797
        EGL_NONE
sl@0
   798
    };
sl@0
   799
    
sl@0
   800
    // Update surfacetype type to match 
sl@0
   801
    for(TInt i = 0; configAttribs[i] != EGL_NONE; i += 2)
sl@0
   802
        {
sl@0
   803
        if (configAttribs[i] == EGL_SURFACE_TYPE)
sl@0
   804
            {
sl@0
   805
            configAttribs[i+1] = aSurfaceType;
sl@0
   806
            }
sl@0
   807
        }
sl@0
   808
    // Need some way to configure the attribs ...
sl@0
   809
    eglChooseConfig(iDisplay, configAttribs, &iConfig, 1, &nConfigs);
sl@0
   810
    if (!nConfigs)
sl@0
   811
        {
sl@0
   812
        err = eglGetError();
sl@0
   813
        RDebug::Printf("%s:%d: err = %d", __FILE__, __LINE__, err);
sl@0
   814
        User::Leave(KErrNotSupported);
sl@0
   815
        }
sl@0
   816
    
sl@0
   817
    if (!eglBindAPI(EGL_OPENVG_API))
sl@0
   818
        {
sl@0
   819
        err = eglGetError();
sl@0
   820
        RDebug::Printf("%s:%d: err = %d", __FILE__, __LINE__, err);
sl@0
   821
        User::Leave(KErrNotSupported);
sl@0
   822
        }
sl@0
   823
    iContext = eglCreateContext(iDisplay, iConfig, 0, NULL);
sl@0
   824
    if (iContext == EGL_NO_CONTEXT)
sl@0
   825
        {
sl@0
   826
        err = eglGetError();
sl@0
   827
        //RDebug::Printf("%s:%d: err = %d", __FILE__, __LINE__, err);
sl@0
   828
        User::Leave(KErrNotSupported);
sl@0
   829
        }
sl@0
   830
    }
sl@0
   831
sl@0
   832
void CEglSurfaceBase::Destroy()
sl@0
   833
    {
sl@0
   834
    eglMakeCurrent(iDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
sl@0
   835
    if (iSurface != EGL_NO_SURFACE)
sl@0
   836
        {
sl@0
   837
        eglDestroySurface(iDisplay, iSurface);
sl@0
   838
        iSurface = EGL_NO_SURFACE;
sl@0
   839
        }
sl@0
   840
sl@0
   841
    if (iDisplay != EGL_NO_DISPLAY)
sl@0
   842
        {
sl@0
   843
        eglDestroyContext(iDisplay, iContext);
sl@0
   844
        }
sl@0
   845
    }
sl@0
   846
sl@0
   847
sl@0
   848
class CWindow: public CBase
sl@0
   849
    {
sl@0
   850
public:
sl@0
   851
    static CWindow *NewL(TInt aIndex, const TPoint &aOffset, TInt aSizeIndex);
sl@0
   852
    RWindow& Window();
sl@0
   853
    ~CWindow();
sl@0
   854
private:
sl@0
   855
    void ConstructL(TInt aIndex, const TPoint &aOffset, TInt aSizeIndex);
sl@0
   856
    CWindow();
sl@0
   857
sl@0
   858
private:
sl@0
   859
    RWindow iWindow;
sl@0
   860
    RWindowGroup iWindowGroup;
sl@0
   861
    RWsSession iWsSession;
sl@0
   862
    };
sl@0
   863
sl@0
   864
sl@0
   865
CWindow* CWindow::NewL(TInt aIndex, const TPoint &aOffset, TInt aSizeIndex)
sl@0
   866
    {
sl@0
   867
    CWindow *self = new (ELeave) CWindow;
sl@0
   868
    CleanupStack::PushL(self);
sl@0
   869
    self->ConstructL(aIndex, aOffset, aSizeIndex);
sl@0
   870
    CleanupStack::Pop(self);
sl@0
   871
    return self;
sl@0
   872
    }
sl@0
   873
sl@0
   874
sl@0
   875
void CWindow::ConstructL(TInt aIndex, const TPoint &aOffset, TInt aSizeIndex)
sl@0
   876
    {
sl@0
   877
    RFbsSession::Connect();
sl@0
   878
    if (aIndex >= KSurfaceMaxIndex)
sl@0
   879
        {
sl@0
   880
        User::Leave(KErrOverflow);
sl@0
   881
        }
sl@0
   882
    User::LeaveIfError(iWsSession.Connect());
sl@0
   883
    iWindowGroup = RWindowGroup(iWsSession);
sl@0
   884
    User::LeaveIfError(iWindowGroup.Construct((TUint32)this));
sl@0
   885
    iWindow = RWindow(iWsSession);
sl@0
   886
    User::LeaveIfError(iWindow.Construct(iWindowGroup, -1U));
sl@0
   887
    TSurfaceParamsCommon winAttrib = KSurfaceParams[aIndex];
sl@0
   888
    TSize winSize;
sl@0
   889
    if (winAttrib.iXSize & LARGEST_POSSIBLE_FLAG)
sl@0
   890
        {
sl@0
   891
        winSize = TSize(KSurfaceSizes[aSizeIndex].iWidth, KSurfaceSizes[aSizeIndex].iHeight);
sl@0
   892
        }
sl@0
   893
    else
sl@0
   894
        {
sl@0
   895
        winSize = TSize(winAttrib.iXSize, winAttrib.iYSize);
sl@0
   896
        }
sl@0
   897
    iWindow.SetExtent(TPoint(winAttrib.iXPos + aOffset.iX, winAttrib.iYPos + aOffset.iY), winSize);
sl@0
   898
    iWindow.SetRequiredDisplayMode(winAttrib.iDisplayMode);
sl@0
   899
    iWindow.Activate();
sl@0
   900
    }
sl@0
   901
sl@0
   902
sl@0
   903
CWindow::~CWindow()
sl@0
   904
    {
sl@0
   905
    iWindow.Close();
sl@0
   906
    iWindowGroup.Close();
sl@0
   907
    iWsSession.Close();
sl@0
   908
    RFbsSession::Disconnect();
sl@0
   909
    }
sl@0
   910
sl@0
   911
sl@0
   912
CWindow::CWindow()
sl@0
   913
    {
sl@0
   914
    }
sl@0
   915
sl@0
   916
sl@0
   917
RWindow& CWindow::Window()
sl@0
   918
    {
sl@0
   919
    return iWindow;
sl@0
   920
    }
sl@0
   921
sl@0
   922
sl@0
   923
CEglWindowSurface* CEglWindowSurface::NewL()
sl@0
   924
    {
sl@0
   925
    CEglWindowSurface* self = new (ELeave) CEglWindowSurface;
sl@0
   926
    CleanupStack::PushL(self);
sl@0
   927
    self->ConstructL();
sl@0
   928
    CleanupStack::Pop(self);
sl@0
   929
    return self;
sl@0
   930
    }
sl@0
   931
sl@0
   932
sl@0
   933
void CEglWindowSurface::ConstructL()
sl@0
   934
    {
sl@0
   935
    }
sl@0
   936
sl@0
   937
sl@0
   938
CEglWindowSurface::CEglWindowSurface()
sl@0
   939
    {
sl@0
   940
    }
sl@0
   941
sl@0
   942
sl@0
   943
void CEglWindowSurface::DoCreateL(TInt aIndex, const TPoint &aOffset, TInt aSizeIndex)
sl@0
   944
    {
sl@0
   945
    iParamIndex = aIndex;
sl@0
   946
    iWindow = CWindow::NewL(aIndex, aOffset, aSizeIndex);
sl@0
   947
    iActualSize = iWindow->Window().Size();
sl@0
   948
    
sl@0
   949
    CEglSurfaceBase::BaseCreateL(aIndex, EGL_WINDOW_BIT);
sl@0
   950
    
sl@0
   951
    iSurface = eglCreateWindowSurface(iDisplay, iConfig, &iWindow->Window(), NULL);
sl@0
   952
    if (iSurface == EGL_NO_SURFACE)
sl@0
   953
        {
sl@0
   954
        EGLint err = eglGetError();
sl@0
   955
        RDebug::Printf("%s:%d: err = %x (%d x %d)", __FILE__, __LINE__, err, iActualSize.iWidth, iActualSize.iHeight);
sl@0
   956
        User::Leave(KErrNotSupported);
sl@0
   957
        }
sl@0
   958
    }
sl@0
   959
sl@0
   960
sl@0
   961
CEglWindowSurface::~CEglWindowSurface()
sl@0
   962
    {
sl@0
   963
    Destroy();
sl@0
   964
	eglReleaseThread();
sl@0
   965
    delete iWindow;
sl@0
   966
    }
sl@0
   967
sl@0
   968
sl@0
   969
TSurfaceId CEglWindowSurface::SurfaceId() const
sl@0
   970
    {
sl@0
   971
    // Default constructor for id sets it to a NULL-value, so if no window is created, we get
sl@0
   972
    // a defined surface id value that is invalid.
sl@0
   973
    TSurfaceId id;
sl@0
   974
    if (iWindow)
sl@0
   975
        {
sl@0
   976
        TSurfaceConfiguration surfConfig;
sl@0
   977
        iWindow->Window().GetBackgroundSurface(surfConfig);
sl@0
   978
        surfConfig.GetSurfaceId(id);
sl@0
   979
        }
sl@0
   980
    return id;
sl@0
   981
    }
sl@0
   982
sl@0
   983
sl@0
   984
const TText *CEglWindowSurface::GetSurfaceTypeStr() const
sl@0
   985
    {
sl@0
   986
    return _S("CEglWindowSurface");
sl@0
   987
    }
sl@0
   988
sl@0
   989
sl@0
   990
CEglPBufferSurface::CEglPBufferSurface()
sl@0
   991
    {
sl@0
   992
    }
sl@0
   993
sl@0
   994
sl@0
   995
CEglPBufferSurface::~CEglPBufferSurface()
sl@0
   996
    {
sl@0
   997
    Destroy();
sl@0
   998
	eglReleaseThread();
sl@0
   999
    }
sl@0
  1000
    
sl@0
  1001
sl@0
  1002
CEglPBufferSurface* CEglPBufferSurface::NewL()
sl@0
  1003
    {
sl@0
  1004
    CEglPBufferSurface* self = new (ELeave) CEglPBufferSurface;
sl@0
  1005
    CleanupStack::PushL(self);
sl@0
  1006
    self->ConstructL();
sl@0
  1007
    CleanupStack::Pop(self);
sl@0
  1008
    return self;
sl@0
  1009
    }
sl@0
  1010
sl@0
  1011
sl@0
  1012
void CEglPBufferSurface::ConstructL()
sl@0
  1013
    {
sl@0
  1014
    }
sl@0
  1015
    
sl@0
  1016
sl@0
  1017
const TText *CEglPBufferSurface::GetSurfaceTypeStr() const
sl@0
  1018
    {
sl@0
  1019
    return _S("CEglPBufferSurface");
sl@0
  1020
    }
sl@0
  1021
sl@0
  1022
sl@0
  1023
void CEglPBufferSurface::DoCreateL(TInt aIndex, const TPoint &/*aOffset*/, TInt aSizeIndex)
sl@0
  1024
    {
sl@0
  1025
    CEglSurfaceBase::BaseCreateL(aIndex, EGL_PBUFFER_BIT);
sl@0
  1026
sl@0
  1027
    EGLint attribs[] = 
sl@0
  1028
            {
sl@0
  1029
                EGL_WIDTH, 0,
sl@0
  1030
                EGL_HEIGHT, 0,
sl@0
  1031
                EGL_NONE,
sl@0
  1032
            };
sl@0
  1033
    if (KSurfaceParams[aIndex].iXSize & ELargestPossibleSurface)
sl@0
  1034
        {
sl@0
  1035
        iActualSize.iWidth = KSurfaceSizes[aSizeIndex].iWidth;
sl@0
  1036
        iActualSize.iHeight = KSurfaceSizes[aSizeIndex].iHeight;
sl@0
  1037
        }
sl@0
  1038
    else
sl@0
  1039
        {
sl@0
  1040
        iActualSize.iWidth = KSurfaceParams[aIndex].iXSize;
sl@0
  1041
        iActualSize.iHeight = KSurfaceParams[aIndex].iYSize;
sl@0
  1042
        }
sl@0
  1043
    for(TInt i = 0; attribs[i] != EGL_NONE; i += 2)
sl@0
  1044
        {
sl@0
  1045
        switch(attribs[i])
sl@0
  1046
            {
sl@0
  1047
            case EGL_HEIGHT:
sl@0
  1048
                attribs[i+1] = iActualSize.iHeight;
sl@0
  1049
                break;
sl@0
  1050
            case EGL_WIDTH:
sl@0
  1051
                attribs[i+1] = iActualSize.iWidth;
sl@0
  1052
                break;
sl@0
  1053
            }
sl@0
  1054
        }
sl@0
  1055
    
sl@0
  1056
    iSurface = eglCreatePbufferSurface(iDisplay, iConfig, attribs);
sl@0
  1057
    if (iSurface == EGL_NO_SURFACE)
sl@0
  1058
        {
sl@0
  1059
        EGLint err = eglGetError();
sl@0
  1060
        User::Leave(KErrNotSupported);
sl@0
  1061
        }
sl@0
  1062
    }
sl@0
  1063
sl@0
  1064
sl@0
  1065
TSurfaceId CEglPBufferSurface::SurfaceId() const
sl@0
  1066
    {
sl@0
  1067
    SURF_ASSERT(0);  // We shouldn't call this!
sl@0
  1068
    return TSurfaceId::CreateNullId();
sl@0
  1069
    }
sl@0
  1070
sl@0
  1071
TInt CEglPBufferSurface::SizeInBytes() const
sl@0
  1072
    {
sl@0
  1073
    // size of a pixel in bits. 
sl@0
  1074
    EGLint size;
sl@0
  1075
    if (!eglGetConfigAttrib(iDisplay, iConfig, EGL_BUFFER_SIZE, &size))
sl@0
  1076
        {
sl@0
  1077
        RDebug::Printf("Unable to get EGL_BUFFER_SIZE from config %d, err = %04x", iConfig, eglGetError());
sl@0
  1078
        return 0;
sl@0
  1079
        }
sl@0
  1080
    
sl@0
  1081
    return (KSurfaceParams[iParamIndex].iXSize * KSurfaceParams[iParamIndex].iYSize * size) / 8;
sl@0
  1082
    }
sl@0
  1083
sl@0
  1084
sl@0
  1085
// Factory function.
sl@0
  1086
CSurface *CSurface::SurfaceFactoryL(TSurfaceType aSurfaceType)
sl@0
  1087
    {
sl@0
  1088
    switch(aSurfaceType)
sl@0
  1089
        {
sl@0
  1090
        case ESurfTypeRaw:
sl@0
  1091
            return CRawSurface::NewL();
sl@0
  1092
            
sl@0
  1093
        case ESurfTypeEglWindow:
sl@0
  1094
            return CEglWindowSurface::NewL();
sl@0
  1095
            
sl@0
  1096
        case ESurfTypeRawSingleBuffered:
sl@0
  1097
            return CRawSingleBufferSurface::NewL();
sl@0
  1098
            
sl@0
  1099
        case ESurfTypePBuffer:
sl@0
  1100
            return CEglPBufferSurface::NewL();
sl@0
  1101
            
sl@0
  1102
        default:
sl@0
  1103
            SURF_ASSERT(0);
sl@0
  1104
            return NULL;
sl@0
  1105
        }
sl@0
  1106
    }