os/graphics/graphicscomposition/openwfcompositionengine/adaptation/src/Platform/Graphics/symbian/owfdisplaycontext.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) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
//
sl@0
     3
// Permission is hereby granted, free of charge, to any person obtaining a
sl@0
     4
// copy of this software and/or associated documentation files (the
sl@0
     5
// "Materials"), to deal in the Materials without restriction, including
sl@0
     6
// without limitation the rights to use, copy, modify, merge, publish,
sl@0
     7
// distribute, sublicense, and/or sell copies of the Materials, and to
sl@0
     8
// permit persons to whom the Materials are furnished to do so, subject to
sl@0
     9
// the following conditions:
sl@0
    10
//
sl@0
    11
// The above copyright notice and this permission notice shall be included
sl@0
    12
// in all copies or substantial portions of the Materials.
sl@0
    13
//
sl@0
    14
// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
sl@0
    15
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
sl@0
    16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
sl@0
    17
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
sl@0
    18
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
sl@0
    19
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
sl@0
    20
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
sl@0
    21
//
sl@0
    22
// Description:
sl@0
    23
// 
sl@0
    24
// 
sl@0
    25
//
sl@0
    26
sl@0
    27
sl@0
    28
#include <WF/wfc.h>
sl@0
    29
#include <e32debug.h>
sl@0
    30
#include <e32std.h>
sl@0
    31
sl@0
    32
#include "wfcdevice.h"
sl@0
    33
#include "owfdisplaycontextgeneral.h"
sl@0
    34
#include "owfmutex.h"
sl@0
    35
#include "owfdisplaycontext.h"
sl@0
    36
#include <graphics/symbianstream.h>
sl@0
    37
sl@0
    38
static const TInt updateReceiverPriorityTable[] = {10, 9, 8, 7, 6, 5, 4, 3};
sl@0
    39
sl@0
    40
/* Arbitrary internal version number for SymbianStream registration. */
sl@0
    41
static const TInt KInternalVersion = 4;
sl@0
    42
sl@0
    43
OWF_DISPCTX OWF_DisplayContext_Create(TInt screenNum)
sl@0
    44
    {
sl@0
    45
    TInt priorityTableSize = sizeof(updateReceiverPriorityTable) / sizeof(updateReceiverPriorityTable[0]);
sl@0
    46
    TInt priority = 0;
sl@0
    47
    
sl@0
    48
    DPRINT(("++ OWF_DisplayContext_Create(screenNum = %d)", screenNum));
sl@0
    49
    OWFDisplayContext *pDispCtx = new OWFDisplayContext;
sl@0
    50
    if (!pDispCtx)
sl@0
    51
        {
sl@0
    52
        DPRINT(("-- Not enough memory to create OWFDisplayContext object"));
sl@0
    53
        return NULL;
sl@0
    54
        }
sl@0
    55
    pDispCtx->iEventServerUpdate  = 0;
sl@0
    56
    pDispCtx->iCompositionOngoing = WFC_FALSE;
sl@0
    57
    pDispCtx->iPreviousCommit     = WFC_FALSE;
sl@0
    58
    pDispCtx->iSerialNumber       = INITIAL_CONTEXT_SERIAL_NUMBER;
sl@0
    59
    pDispCtx->fastpathChecked     = WFC_FALSE;
sl@0
    60
    pDispCtx->fastpathStream      = OWF_INVALID_HANDLE;
sl@0
    61
    pDispCtx->iScreenContext      = NULL;
sl@0
    62
    pDispCtx->iInternalStreamAccessed = WFC_FALSE;    
sl@0
    63
    
sl@0
    64
    OWF_Semaphore_Init(&pDispCtx->iRendezvous, 0);
sl@0
    65
    
sl@0
    66
    if (screenNum != OWF_RESERVED_BAD_SCREEN_NUMBER)
sl@0
    67
        {
sl@0
    68
        if((screenNum < 0) || (screenNum >= priorityTableSize))
sl@0
    69
            {
sl@0
    70
            DPRINT(("   Can't retrieve backend priority for the screen number: %d\n", screenNum));
sl@0
    71
            }
sl@0
    72
        else
sl@0
    73
            {
sl@0
    74
            priority = updateReceiverPriorityTable[screenNum];
sl@0
    75
            }
sl@0
    76
		// False positive leaving function is in a TRAPD
sl@0
    77
		// coverity[leave_without_push]
sl@0
    78
        TInt err = SymbianStreamRegisterScreenNotifications(screenNum, priority, KInternalVersion);
sl@0
    79
        if (!(err == KErrNone || err == KErrNotReady || err == KErrAlreadyExists))
sl@0
    80
            {
sl@0
    81
            DPRINT(("-- Register with Surface Update Server failed for screen: %d err=%d\n", screenNum, err));
sl@0
    82
            delete pDispCtx;
sl@0
    83
            return NULL;
sl@0
    84
            }
sl@0
    85
        if (err == KErrNotReady)
sl@0
    86
            {
sl@0
    87
            DPRINT(("   Surface Update Server is not ready for screen: %d\n", screenNum));
sl@0
    88
            }
sl@0
    89
        if (err == KErrAlreadyExists)
sl@0
    90
            {
sl@0
    91
            DPRINT(("   Backend object already exists for screen: %d\n", screenNum));
sl@0
    92
            }
sl@0
    93
        }
sl@0
    94
    
sl@0
    95
    pDispCtx->iScreenContext = NULL;
sl@0
    96
    
sl@0
    97
    DPRINT(("-- OWF_DisplayContext_Create(screenNum = %d) : %p", screenNum, pDispCtx));
sl@0
    98
    return static_cast<OWF_DISPCTX>(pDispCtx);
sl@0
    99
    }
sl@0
   100
sl@0
   101
void OWF_DisplayContext_Destroy(TInt screenNum, OWF_DISPCTX dc)
sl@0
   102
    {
sl@0
   103
    DPRINT(("++ OWF_DisplayContext_Destroy(screenNum = %d) : %p", screenNum, dc));
sl@0
   104
    if (dc != OWF_INVALID_HANDLE)
sl@0
   105
        {
sl@0
   106
        OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   107
        delete pDispCtx;
sl@0
   108
        }
sl@0
   109
    
sl@0
   110
    if (screenNum != OWF_RESERVED_BAD_SCREEN_NUMBER)
sl@0
   111
        {
sl@0
   112
        TInt err = SymbianStreamUnregisterScreenNotifications(screenNum);
sl@0
   113
        if (!(err == KErrNone || err == KErrNotReady))
sl@0
   114
            {
sl@0
   115
            DPRINT(("!! Unregister with Surface Update Server failed for screen: %d with %d\n", screenNum, err));
sl@0
   116
            }
sl@0
   117
        }
sl@0
   118
    DPRINT(("-- OWF_DisplayContext_Destroy()"));
sl@0
   119
    }
sl@0
   120
sl@0
   121
OWFDisplayContext_::~OWFDisplayContext_()
sl@0
   122
    {
sl@0
   123
    }
sl@0
   124
sl@0
   125
void* COwfScratchBuffers::Allocate(TInt aMinSize)
sl@0
   126
/**
sl@0
   127
Create a chunk for a new scratch buffer that is at least aMinSize bytes large.
sl@0
   128
@param  aMinSize    The minimum size of the buffer required.
sl@0
   129
@return A pointer to the base of the new buffer if successful; otherwise null is returned. 
sl@0
   130
*/
sl@0
   131
    {
sl@0
   132
    TBufferAddress buf;
sl@0
   133
    if (buf.iChunk.CreateLocal(aMinSize, aMinSize) != KErrNone) 
sl@0
   134
        {
sl@0
   135
        return 0;
sl@0
   136
        }    
sl@0
   137
    buf.iAddress = buf.iChunk.Base();
sl@0
   138
    buf.iOffset = 0;
sl@0
   139
    if(iBuffers.Append(buf) == KErrNone)
sl@0
   140
        {
sl@0
   141
        return buf.iAddress;
sl@0
   142
        }
sl@0
   143
    else
sl@0
   144
        {
sl@0
   145
        buf.iChunk.Close();
sl@0
   146
        return 0;
sl@0
   147
        }
sl@0
   148
    }
sl@0
   149
sl@0
   150
void COwfScratchBuffers::Destroy(void* aAddress)
sl@0
   151
/**
sl@0
   152
Destroys the buffer with base address aAddress
sl@0
   153
@param  aAddress    The address of the buffer to destroy
sl@0
   154
*/
sl@0
   155
    {
sl@0
   156
    for (TInt i = iBuffers.Count() - 1; i >= 0; --i)
sl@0
   157
        {
sl@0
   158
        if (iBuffers[i].iAddress == static_cast<TUint8*>(aAddress))
sl@0
   159
            {
sl@0
   160
            iBuffers[i].iChunk.Close();
sl@0
   161
            iBuffers.Remove(i);
sl@0
   162
            break;
sl@0
   163
            }
sl@0
   164
        }
sl@0
   165
    }
sl@0
   166
    
sl@0
   167
COwfScratchBuffers::~COwfScratchBuffers()
sl@0
   168
/**
sl@0
   169
Destructor
sl@0
   170
Close all the chunks.
sl@0
   171
*/
sl@0
   172
    {
sl@0
   173
    for (TInt i = iBuffers.Count() - 1; i >= 0; --i)
sl@0
   174
        {
sl@0
   175
        iBuffers[i].iChunk.Close();
sl@0
   176
        }
sl@0
   177
    iBuffers.Close();
sl@0
   178
    }    
sl@0
   179
sl@0
   180
void* OWF_DisplayContext_ScratchBuffer_Allocate(OWF_DISPCTX dc, int size)
sl@0
   181
    {
sl@0
   182
    OWF_ASSERT(dc);
sl@0
   183
    
sl@0
   184
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   185
    return pDispCtx->iBuffers.Allocate(size);
sl@0
   186
    }
sl@0
   187
sl@0
   188
void OWF_DisplayContext_ScratchBuffer_Destroy(OWF_DISPCTX dc, void* buffer)
sl@0
   189
    {
sl@0
   190
    OWF_ASSERT(dc);
sl@0
   191
    
sl@0
   192
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   193
    pDispCtx->iBuffers.Destroy(buffer);
sl@0
   194
    }
sl@0
   195
sl@0
   196
void OWF_DisplayContext_IncrementSerialNumber(OWF_DISPCTX dc)
sl@0
   197
    {
sl@0
   198
    OWF_ASSERT(dc);
sl@0
   199
    
sl@0
   200
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   201
    pDispCtx->iSerialNumber++;
sl@0
   202
    if (pDispCtx->iSerialNumber == INITIAL_CONTEXT_SERIAL_NUMBER)
sl@0
   203
        {
sl@0
   204
        pDispCtx->iSerialNumber++;
sl@0
   205
        }
sl@0
   206
    }
sl@0
   207
sl@0
   208
void OWF_DisplayContext_SetCompositionOngoing(OWF_DISPCTX dc, WFCboolean val)
sl@0
   209
    {
sl@0
   210
    OWF_ASSERT(dc);
sl@0
   211
    
sl@0
   212
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   213
    pDispCtx->iCompositionOngoing = val;    
sl@0
   214
    }
sl@0
   215
sl@0
   216
void OWF_ComposerThread_Rendezvous(OWF_DISPCTX dc)
sl@0
   217
    {
sl@0
   218
    OWF_ASSERT(dc);
sl@0
   219
    
sl@0
   220
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   221
    OWF_Semaphore_Post(&pDispCtx->iRendezvous);
sl@0
   222
    }
sl@0
   223
sl@0
   224
void OWF_ComposerThread_RendezvousWait(OWF_DISPCTX dc)
sl@0
   225
    {
sl@0
   226
    OWF_ASSERT(dc);
sl@0
   227
    
sl@0
   228
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   229
    OWF_Semaphore_Wait(&pDispCtx->iRendezvous);
sl@0
   230
    }
sl@0
   231
sl@0
   232
void OWF_ComposerThread_RendezvousDestroy(OWF_DISPCTX dc)
sl@0
   233
    {
sl@0
   234
    OWF_ASSERT(dc);
sl@0
   235
    
sl@0
   236
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   237
    OWF_Semaphore_Destroy(&pDispCtx->iRendezvous);
sl@0
   238
    }
sl@0
   239
sl@0
   240
void OWF_DisplayContext_EnableFastpath(OWF_DISPCTX dc, OWFNativeStreamType fastpathStream)
sl@0
   241
    {
sl@0
   242
    DPRINT(("++ OWF_DisplayContext_EnableFastpath(%p, %d)", dc, fastpathStream));
sl@0
   243
    OWF_ASSERT(dc);
sl@0
   244
    
sl@0
   245
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   246
    pDispCtx->fastpathStream = fastpathStream;
sl@0
   247
    }
sl@0
   248
sl@0
   249
void OWF_DisplayContext_DisableFastpath(OWF_DISPCTX dc)
sl@0
   250
    {
sl@0
   251
    DPRINT(("++ OWF_DisplayContext_DisableFastpath(%p)", dc));
sl@0
   252
    OWF_ASSERT(dc);
sl@0
   253
    
sl@0
   254
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   255
    pDispCtx->fastpathStream = OWF_INVALID_HANDLE;
sl@0
   256
    }
sl@0
   257
sl@0
   258
WFCboolean OWF_DisplayContext_FastpathEnabled(OWF_DISPCTX dc)
sl@0
   259
    {
sl@0
   260
    OWF_ASSERT(dc);
sl@0
   261
    
sl@0
   262
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   263
    DPRINT(("++ OWF_DisplayContext_FastpathEnabled(%p) : %d", dc, (pDispCtx->fastpathStream != OWF_INVALID_HANDLE)));
sl@0
   264
    return (pDispCtx->fastpathStream != OWF_INVALID_HANDLE) ? WFC_TRUE : WFC_FALSE;
sl@0
   265
    }
sl@0
   266
sl@0
   267
OWFNativeStreamType OWF_DisplayContext_FastpathStream(OWF_DISPCTX dc)
sl@0
   268
    {
sl@0
   269
    OWF_ASSERT(dc);
sl@0
   270
    
sl@0
   271
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   272
    DPRINT(("++ OWF_DisplayContext_FastpathStream(%p) : %p", dc, pDispCtx->fastpathStream));
sl@0
   273
    return pDispCtx->fastpathStream;
sl@0
   274
    }
sl@0
   275
sl@0
   276
WFCboolean OWF_DisplayContext_FastpathChecked(OWF_DISPCTX dc)
sl@0
   277
    {
sl@0
   278
    OWF_ASSERT(dc);
sl@0
   279
    
sl@0
   280
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   281
    DPRINT(("++ OWF_DisplayContext_FastpathChecked(%p) : %d", dc, pDispCtx->fastpathChecked));
sl@0
   282
    return pDispCtx->fastpathChecked;
sl@0
   283
    }
sl@0
   284
sl@0
   285
void OWF_DisplayContext_ResetFastpathCheck(OWF_DISPCTX dc)
sl@0
   286
    {
sl@0
   287
    DPRINT(("++ OWF_DisplayContext_ResetFastpathCheck(%p)", dc));
sl@0
   288
    OWF_ASSERT(dc);
sl@0
   289
    
sl@0
   290
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   291
    pDispCtx->fastpathChecked = WFC_FALSE;
sl@0
   292
    }
sl@0
   293
sl@0
   294
void OWF_DisplayContext_SetFastpathChecked(OWF_DISPCTX dc)
sl@0
   295
    {
sl@0
   296
    DPRINT(("++ OWF_DisplayContext_SetFastpathCheck(%p)", dc));
sl@0
   297
    OWF_ASSERT(dc);
sl@0
   298
    
sl@0
   299
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   300
    pDispCtx->fastpathChecked = WFC_TRUE;
sl@0
   301
    }
sl@0
   302
sl@0
   303
void OWF_DisplayContext_FlagInternalStreamAccessed(OWF_DISPCTX dc)
sl@0
   304
    {
sl@0
   305
    DPRINT(("++ OWF_DisplayContext_FlagInternalStreamAccessed(%p)", dc));
sl@0
   306
    OWF_ASSERT(dc);
sl@0
   307
    
sl@0
   308
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   309
    pDispCtx->iInternalStreamAccessed = WFC_TRUE;
sl@0
   310
    }
sl@0
   311
sl@0
   312
WFCboolean OWF_DisplayContext_InternalStreamAccessed(OWF_DISPCTX dc)
sl@0
   313
    {
sl@0
   314
    OWF_ASSERT(dc);
sl@0
   315
    
sl@0
   316
    OWFDisplayContext *pDispCtx = _OWF_DISPLAYCONTEXT(dc);
sl@0
   317
    DPRINT(("++ OWF_DisplayContext_InternalStreamAccessed(%p) : ", dc, pDispCtx->iInternalStreamAccessed));
sl@0
   318
    return pDispCtx->iInternalStreamAccessed;
sl@0
   319
    }
sl@0
   320
sl@0
   321
OWFboolean OWF_DisplayContext_Check_Fastpathed_Stream(OWF_DISPCTX dc, OWFNativeStreamType fastpathedStream)
sl@0
   322
    {
sl@0
   323
    if (!dc)
sl@0
   324
        {
sl@0
   325
        return OWF_FALSE;
sl@0
   326
        }
sl@0
   327
    
sl@0
   328
    khronos_int32_t pixelFormat = 0;
sl@0
   329
    SymbianStreamGetHeader((SymbianStreamType)fastpathedStream, NULL, NULL, NULL, &pixelFormat, NULL);
sl@0
   330
    
sl@0
   331
    return ((EUidPixelFormatARGB_8888_PRE == pixelFormat) || (EUidPixelFormatARGB_8888 == pixelFormat) || (EUidPixelFormatXRGB_8888 == pixelFormat)) ? OWF_TRUE : OWF_FALSE;
sl@0
   332
    }
sl@0
   333
sl@0
   334
OWFboolean OWF_DisplayContext_CopyFastpathedStreamToTargetStream(void* ctxt)
sl@0
   335
    {
sl@0
   336
    DPRINT(("++ OWF_DisplayContext_CopyFastpathedStreamToTargetStream(%p)", ctxt));
sl@0
   337
    WFC_CONTEXT* context = CONTEXT(ctxt);
sl@0
   338
    if (!context || !context->displayContext)
sl@0
   339
        {
sl@0
   340
        DPRINT(("!! OWF_DisplayContext_CopyFastpathedStreamToTargetStream(%p) invalid", ctxt));
sl@0
   341
        return OWF_FALSE;
sl@0
   342
        }
sl@0
   343
    khronos_int32_t dstStride = context->state.targetImage->stride;
sl@0
   344
    TInt height = context->state.targetImage->height;
sl@0
   345
    void* dstPtr     = context->state.targetPixels;
sl@0
   346
    OWFDisplayContext* pDispCtx = _OWF_DISPLAYCONTEXT(context->displayContext);
sl@0
   347
    SymbianStreamType fastpathedStream = (SymbianStreamType)pDispCtx->fastpathStream;
sl@0
   348
    if (!OWF_DisplayContext_Check_Fastpathed_Stream(pDispCtx,pDispCtx->fastpathStream))
sl@0
   349
        {   //The format does not lend itself to quick copying.
sl@0
   350
            //Clients listening for update callbacks will be disappointed.
sl@0
   351
        return OWF_FALSE;
sl@0
   352
        }
sl@0
   353
    void* srcPtr     = NULL;
sl@0
   354
    khronos_int32_t srcStride = 0;
sl@0
   355
    SymbianStreamBuffer frontBuffer;
sl@0
   356
    /* Stride may include unaddressable memory, so only copy valid pixels */
sl@0
   357
    khronos_int32_t copyLen = context->state.targetImage->width * context->state.targetImage->pixelSize;
sl@0
   358
    
sl@0
   359
    SymbianStreamAcquireReadBuffer(fastpathedStream, &frontBuffer);
sl@0
   360
    SymbianStreamGetBufferPointer(fastpathedStream, frontBuffer, &srcPtr);
sl@0
   361
    
sl@0
   362
    SymbianStreamGetHeader(fastpathedStream, NULL, NULL, &srcStride, NULL, NULL);
sl@0
   363
    
sl@0
   364
    for (TInt i = 0; i < height; i++)
sl@0
   365
        {
sl@0
   366
        Mem::Copy(dstPtr, srcPtr, copyLen);
sl@0
   367
        dstPtr = (khronos_uint8_t*)dstPtr + dstStride;
sl@0
   368
        srcPtr = (khronos_uint8_t*)srcPtr + srcStride;
sl@0
   369
        }
sl@0
   370
    
sl@0
   371
    SymbianStreamReleaseReadBuffer(fastpathedStream, frontBuffer);
sl@0
   372
    DPRINT(("-- OWF_DisplayContext_CopyFastpathedStreamToTargetStream(%p)", ctxt));
sl@0
   373
    return OWF_TRUE;
sl@0
   374
    }
sl@0
   375