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