os/graphics/graphicscomposition/openwfcompositionengine/composition/src/wfccontext.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/graphicscomposition/openwfcompositionengine/composition/src/wfccontext.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,2026 @@
     1.4 +/* Copyright (c) 2009 The Khronos Group Inc.
     1.5 + * Portions copyright (c) 2009-2010  Nokia Corporation and/or its subsidiary(-ies)
     1.6 + *
     1.7 + * Permission is hereby granted, free of charge, to any person obtaining a
     1.8 + * copy of this software and/or associated documentation files (the
     1.9 + * "Materials"), to deal in the Materials without restriction, including
    1.10 + * without limitation the rights to use, copy, modify, merge, publish,
    1.11 + * distribute, sublicense, and/or sell copies of the Materials, and to
    1.12 + * permit persons to whom the Materials are furnished to do so, subject to
    1.13 + * the following conditions:
    1.14 + *
    1.15 + * The above copyright notice and this permission notice shall be included
    1.16 + * in all copies or substantial portions of the Materials.
    1.17 + *
    1.18 + * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    1.19 + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    1.20 + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    1.21 + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    1.22 + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    1.23 + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    1.24 + * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
    1.25 + */
    1.26 +
    1.27 +/*! \ingroup wfc
    1.28 + *  \file wfccontext.c
    1.29 + *
    1.30 + *  \brief SI Context handling
    1.31 + */
    1.32 +
    1.33 +#include <string.h>
    1.34 +#include <stdio.h>
    1.35 +#include <time.h>
    1.36 +#include <stdlib.h>
    1.37 +#include <math.h>
    1.38 +
    1.39 +#include <WF/wfc.h>
    1.40 +#include <EGL/egl.h>
    1.41 +
    1.42 +#include "wfccontext.h"
    1.43 +#include "wfcpipeline.h"
    1.44 +
    1.45 +#include "owfscreen.h"
    1.46 +#include "owfdisplaycontextgeneral.h"
    1.47 +#include "owfnotifications.h"
    1.48 +
    1.49 +
    1.50 +/*! maximum number of elements per scene */
    1.51 +#define MAX_ELEMENTS            512
    1.52 +/*! maximum number of scenes per context */
    1.53 +#define MAX_SCENES              3
    1.54 +
    1.55 +
    1.56 +#define CONTEXT_SCENE_POOL_SIZE     MAX_SCENES
    1.57 +#define CONTEXT_ELEMENT_POOL_SIZE   MAX_SCENES * MAX_ELEMENTS
    1.58 +#define CONTEXT_NODE_POOL_SIZE      2 * CONTEXT_ELEMENT_POOL_SIZE
    1.59 +
    1.60 +/*! almost 2^31 */
    1.61 +#define MAX_DELAY               2100000000
    1.62 +
    1.63 +/*! 15ms */
    1.64 +#define AUTO_COMPOSE_DELAY      15000
    1.65 +#define FIRST_CONTEXT_HANDLE    2000
    1.66 +
    1.67 +#ifdef __cplusplus
    1.68 +extern "C" {
    1.69 +#endif
    1.70 +
    1.71 +static WFCHandle                nextContextHandle = FIRST_CONTEXT_HANDLE;
    1.72 +
    1.73 +typedef enum
    1.74 +{
    1.75 +    WFC_MESSAGE_NONE,
    1.76 +    WFC_MESSAGE_QUIT,
    1.77 +    WFC_MESSAGE_COMPOSE,
    1.78 +    WFC_MESSAGE_COMMIT,
    1.79 +    WFC_MESSAGE_FENCE_1_DISPLAY,
    1.80 +    WFC_MESSAGE_FENCE_2_SYNCOBJECT,
    1.81 +    WFC_MESSAGE_ACTIVATE,
    1.82 +    WFC_MESSAGE_DEACTIVATE,
    1.83 +    WFC_MESSAGE_START_COUNTDOWN,
    1.84 +    WFC_MESSAGE_CANCEL
    1.85 +} WFC_MESSAGES;
    1.86 +
    1.87 +static void*
    1.88 +WFC_Context_ComposerThread(void* data);
    1.89 +
    1.90 +
    1.91 +/*---------------------------------------------------------------------------
    1.92 + *
    1.93 + *----------------------------------------------------------------------------*/
    1.94 +void WFC_CONTEXT_Ctor(void* self)
    1.95 +{
    1.96 +    self = self;
    1.97 +}
    1.98 +
    1.99 +/*---------------------------------------------------------------------------*/
   1.100 +static WFCboolean
   1.101 +WFC_Context_CreateState(WFC_CONTEXT* context)
   1.102 +    {   /* Must be called late enough that scratch buffers can be mapped and hardware rotation capability queried */
   1.103 +    OWF_IMAGE_FORMAT        fInt,
   1.104 +                            fExt;
   1.105 +    WFCint                  stride = 0;
   1.106 +    OWF_ASSERT(context);
   1.107 +
   1.108 +    DPRINT(("WFC_Context_CreateContextState"));
   1.109 +
   1.110 +    owfNativeStreamGetHeader(context->stream,
   1.111 +                             NULL, NULL, &stride, &fExt, NULL);
   1.112 +    /* internal image used as intermediate target */
   1.113 +    fInt.pixelFormat    = OWF_IMAGE_ARGB_INTERNAL;
   1.114 +    fInt.linear         = fExt.linear;
   1.115 +    fInt.premultiplied  = fExt.premultiplied;
   1.116 +    fInt.rowPadding     = 1;
   1.117 +     
   1.118 +    if (context->type == WFC_CONTEXT_TYPE_ON_SCREEN)
   1.119 +        {
   1.120 +        /* The unrotated target buffer: Can't get real address without locking for writing!  NO STRIDE */
   1.121 +        context->state.unrotatedTargetImage=OWF_Image_Create(context->targetWidth,context->targetHeight,&fExt,context->scratchBuffer[2],0);
   1.122 +        /* The rotated version of the target buffer for hardware rotation
   1.123 +         * or a de-rotated version of the internal buffer into another scratch buffer for software rotation
   1.124 +         */ 
   1.125 +        if (OWF_Screen_Rotation_Supported(context->displayContext))
   1.126 +            {   /* The rotated version of the target buffer for hardware rotation */
   1.127 +            context->state.rotatedTargetImage=OWF_Image_Create(context->targetHeight,context->targetWidth,&fExt,context->scratchBuffer[2],0);
   1.128 +            }
   1.129 +        else
   1.130 +            {   /* a de-rotated version of the internal buffer into another scratch buffer for software rotation */
   1.131 +            context->state.rotatedTargetImage=OWF_Image_Create(context->targetWidth,context->targetHeight,&fInt,context->scratchBuffer[1],0);
   1.132 +            }
   1.133 +        }
   1.134 +    else
   1.135 +        {
   1.136 +        /* The unrotated target buffer: Can't get real address without locking for writing!  STRIDE HONOURED */
   1.137 +        context->state.unrotatedTargetImage=OWF_Image_Create(context->targetWidth,context->targetHeight,&fExt,context->scratchBuffer[2],stride);
   1.138 +        /* a de-rotated version of the internal buffer into another scratch buffer for software rotation */
   1.139 +        context->state.rotatedTargetImage=OWF_Image_Create(context->targetWidth,context->targetHeight,&fInt,context->scratchBuffer[1],0);
   1.140 +        }
   1.141 +    /* The internal target buffer composed to for 0 and 180 degree rotation */
   1.142 +    context->state.unrotatedInternalTargetImage=OWF_Image_Create(context->targetWidth,context->targetHeight,&fInt,context->scratchBuffer[0],stride);
   1.143 +    /* The internal target buffer composed to for 90 and 270 degree rotation */
   1.144 +    context->state.rotatedInternalTargetImage=OWF_Image_Create(context->targetHeight,context->targetWidth,&fInt,context->scratchBuffer[0],stride);
   1.145 +
   1.146 +    if (context->state.unrotatedTargetImage && context->state.rotatedTargetImage && context->state.unrotatedInternalTargetImage && context->state.rotatedInternalTargetImage)
   1.147 +        {
   1.148 +        return WFC_TRUE;
   1.149 +        }
   1.150 +    else
   1.151 +        {
   1.152 +        return WFC_FALSE;
   1.153 +        }
   1.154 +    }
   1.155 +/*---------------------------------------------------------------------------*/
   1.156 +static void
   1.157 +WFC_Context_DestroyState(WFC_CONTEXT* context)
   1.158 +    {
   1.159 +    /* The unrotated target buffer */ 
   1.160 +    OWF_Image_Destroy(context->state.unrotatedTargetImage);
   1.161 +    /* The rotated version of the target buffer for hardware rotation, 
   1.162 +     * or a de-rotated version of the internal buffer into another scratch buffer for software rotation
   1.163 +     */ 
   1.164 +    OWF_Image_Destroy(context->state.rotatedTargetImage);
   1.165 +    /* The internal target buffer composed to for 0 and 180 degree rotation */
   1.166 +    OWF_Image_Destroy(context->state.unrotatedInternalTargetImage);
   1.167 +    /* The internal target buffer composed to for 90 and 270 degree rotation */
   1.168 +    OWF_Image_Destroy(context->state.rotatedInternalTargetImage);
   1.169 +    
   1.170 +    }
   1.171 +/*---------------------------------------------------------------------------
   1.172 + * Should only be accessed indirectly by calls to WFC_Device_DestroyContext or
   1.173 + * WFC_Device_DestroyContexts
   1.174 + *----------------------------------------------------------------------------*/
   1.175 +void WFC_CONTEXT_Dtor(void* self)
   1.176 +{
   1.177 +    OWFint                  ii = 0;
   1.178 +    WFC_CONTEXT*            context = NULL;
   1.179 +
   1.180 +    OWF_ASSERT(self);
   1.181 +    DPRINT(("WFC_CONTEXT_Dtor(%p)", self));
   1.182 +
   1.183 +    context = CONTEXT(self);
   1.184 +
   1.185 +    OWF_ASSERT(context);
   1.186 +    
   1.187 +    WFC_Pipeline_DestroyState(context);
   1.188 +    WFC_Context_DestroyState(context);
   1.189 +    
   1.190 +    OWF_MessageQueue_Destroy(&context->composerQueue);
   1.191 +
   1.192 +    /* make the stream destroyable */
   1.193 +    if (context->stream != OWF_INVALID_HANDLE)
   1.194 +        {
   1.195 +        owfNativeStreamSetProtectionFlag(context->stream, OWF_FALSE);
   1.196 +        owfNativeStreamDestroy(context->stream);
   1.197 +        }
   1.198 +    context->stream = OWF_INVALID_HANDLE;
   1.199 +
   1.200 +    OWF_AttributeList_Destroy(&context->attributes);
   1.201 +
   1.202 +    for (ii = 0; ii < SCRATCH_BUFFER_COUNT; ii++)
   1.203 +    {
   1.204 +        OWF_Image_FreeData(context->displayContext, &context->scratchBuffer[ii]);
   1.205 +        context->scratchBuffer[ii] = 0;
   1.206 +    }
   1.207 +
   1.208 +    OWF_DisplayContext_Destroy(context->screenNumber, context->displayContext);
   1.209 +    context->displayContext = NULL;    
   1.210 +
   1.211 +    OWF_Pool_Destroy(context->scenePool);
   1.212 +    OWF_Pool_Destroy(context->elementPool);
   1.213 +    OWF_Pool_Destroy(context->nodePool);
   1.214 +
   1.215 +    OWF_Semaphore_Destroy(&context->compositionSemaphore);
   1.216 +    OWF_Semaphore_Destroy(&context->commitSemaphore);
   1.217 +    OWF_Mutex_Destroy(&context->updateFlagMutex);
   1.218 +    OWF_Mutex_Destroy(&context->sceneMutex);
   1.219 +}
   1.220 +
   1.221 +/*---------------------------------------------------------------------------
   1.222 + *
   1.223 + *----------------------------------------------------------------------------*/
   1.224 +OWF_API_CALL void
   1.225 +WFC_Context_Shutdown(WFC_CONTEXT* context)
   1.226 +{
   1.227 +    OWF_ASSERT(context);
   1.228 +    DPRINT(("WFC_Context_Shutdown(context = %d)", context->handle));
   1.229 +
   1.230 +    DPRINT(("Waiting for composer thread termination"));
   1.231 +    if (context->composerThread)
   1.232 +        {
   1.233 +        OWF_Message_Send(&context->composerQueue, WFC_MESSAGE_QUIT, 0);
   1.234 +        OWF_Thread_Join(context->composerThread, NULL);
   1.235 +        OWF_Thread_Destroy(context->composerThread);
   1.236 +        }
   1.237 +    
   1.238 +    context->composerThread = NULL;
   1.239 +
   1.240 +    if (context->device)
   1.241 +    {
   1.242 +        /* #4604: added guard condition */
   1.243 +        WFC_Device_DestroyContextElements(context->device, context);
   1.244 +        WFC_Device_DestroyContextImageProviders(context->device, context);
   1.245 +    }
   1.246 +
   1.247 +    WFC_Scene_Destroy(context->workScene);
   1.248 +    WFC_Scene_Destroy(context->snapshotScene);
   1.249 +    WFC_Scene_Destroy(context->committedScene);
   1.250 +    context->workScene = NULL;
   1.251 +    context->snapshotScene = NULL;
   1.252 +    context->committedScene = NULL;    
   1.253 +}
   1.254 +
   1.255 +/*---------------------------------------------------------------------------
   1.256 + *  Initialize context attributes
   1.257 + *----------------------------------------------------------------------------*/
   1.258 +OWF_API_CALL OWF_ATTRIBUTE_LIST_STATUS
   1.259 +WFC_Context_InitializeAttributes(WFC_CONTEXT* context,
   1.260 +                                 WFCContextType type)
   1.261 +{
   1.262 +    OWF_ATTRIBUTE_LIST_STATUS attribError=ATTR_ERROR_NONE;
   1.263 +    OWF_ASSERT(context);
   1.264 +    /* initialize attributes/properties */
   1.265 +    if (context->stream)
   1.266 +    {
   1.267 +        owfNativeStreamGetHeader(context->stream,
   1.268 +                                 &context->targetWidth,
   1.269 +                                 &context->targetHeight,
   1.270 +                                 NULL, NULL, NULL);
   1.271 +    }
   1.272 +    context->type               = type;
   1.273 +    context->rotation           = WFC_ROTATION_0;
   1.274 +    context->backgroundColor    = 0x000000FF;
   1.275 +    context->lowestElement      = WFC_INVALID_HANDLE;
   1.276 +
   1.277 +
   1.278 +    OWF_AttributeList_Create(&context->attributes,
   1.279 +                             WFC_CONTEXT_TYPE,
   1.280 +                             WFC_CONTEXT_BG_COLOR);
   1.281 +    attribError=OWF_AttributeList_GetError(&context->attributes);
   1.282 +    if (attribError!=ATTR_ERROR_NONE)
   1.283 +        {
   1.284 +        OWF_ASSERT(attribError==ATTR_ERROR_NO_MEMORY);
   1.285 +        return attribError;
   1.286 +        }
   1.287 +
   1.288 +  
   1.289 +    /* The composition code reads the member variables directly, 
   1.290 +     * not via the attribute engine.
   1.291 +     */
   1.292 +    OWF_Attribute_Initi(&context->attributes,
   1.293 +                        WFC_CONTEXT_TYPE,
   1.294 +                        (WFCint*) &context->type,
   1.295 +                        OWF_TRUE);
   1.296 +
   1.297 +    OWF_Attribute_Initi(&context->attributes,
   1.298 +                        WFC_CONTEXT_TARGET_WIDTH,
   1.299 +                        &context->targetWidth,
   1.300 +                        OWF_TRUE);
   1.301 +
   1.302 +    OWF_Attribute_Initi(&context->attributes,
   1.303 +                        WFC_CONTEXT_TARGET_HEIGHT,
   1.304 +                        &context->targetHeight,
   1.305 +                        OWF_TRUE);
   1.306 +
   1.307 +    OWF_Attribute_Initi(&context->attributes,
   1.308 +                        WFC_CONTEXT_ROTATION,
   1.309 +                        (WFCint*) &context->rotation,
   1.310 +                        OWF_FALSE);
   1.311 +
   1.312 +    OWF_Attribute_Initi(&context->attributes,
   1.313 +                        WFC_CONTEXT_BG_COLOR,
   1.314 +                        (WFCint*) &context->backgroundColor,
   1.315 +                        OWF_FALSE);
   1.316 +
   1.317 +    OWF_Attribute_Initi(&context->attributes,
   1.318 +                        WFC_CONTEXT_LOWEST_ELEMENT,
   1.319 +                        (OWFint*) &context->lowestElement,
   1.320 +                        OWF_TRUE);
   1.321 +    attribError=OWF_AttributeList_GetError(&context->attributes);
   1.322 +	
   1.323 +	/* After commit to working, writable attribute abstracted variables
   1.324 +	must not be written to directly. */
   1.325 +    OWF_AttributeList_Commit(&context->attributes,
   1.326 +                             WFC_CONTEXT_TYPE,
   1.327 +                             WFC_CONTEXT_BG_COLOR,
   1.328 +		             WORKING_ATTR_VALUE_INDEX );
   1.329 +	
   1.330 +    return attribError;
   1.331 +}
   1.332 +
   1.333 +
   1.334 +/*---------------------------------------------------------------------------
   1.335 + *
   1.336 + *----------------------------------------------------------------------------*/
   1.337 +static WFC_CONTEXT*
   1.338 +WFC_Context_Initialize(WFC_CONTEXT* context,
   1.339 +                       WFC_DEVICE* device,
   1.340 +                       WFCNativeStreamType stream,
   1.341 +                       WFCContextType type,
   1.342 +                       WFCint screenNum)
   1.343 +{
   1.344 +    void*                   scratch[SCRATCH_BUFFER_COUNT];
   1.345 +    OWFint                  err2    = 0;
   1.346 +    OWFint                  ii      = 0,
   1.347 +                            nbufs   = 0;
   1.348 +    OWFint                  fail    = 0;
   1.349 +    OWF_ATTRIBUTE_LIST_STATUS attribStatus = ATTR_ERROR_NONE;
   1.350 +    OWF_ASSERT(context);
   1.351 +    OWF_ASSERT(device);
   1.352 +
   1.353 +    DPRINT(("WFC_Context_Initialize(%p,%p,%d,%d)", context, device, type, screenNum));
   1.354 +
   1.355 +    context->type               = type;
   1.356 +    context->device             = device;
   1.357 +    context->handle             = nextContextHandle;
   1.358 +    context->screenNumber       = screenNum;
   1.359 +    context->activationState    = WFC_CONTEXT_STATE_PASSIVE;
   1.360 +    context->sourceUpdateCount  = 0;
   1.361 +    context->clientElementCount = 0;
   1.362 +    ++nextContextHandle;
   1.363 +
   1.364 +    context->displayContext = OWF_DisplayContext_Create(screenNum);
   1.365 +    if (context->displayContext == NULL)
   1.366 +        {
   1.367 +        DPRINT(("WFC_Context_Initialize(): Could not create display Context"));
   1.368 +        return NULL;
   1.369 +        }
   1.370 +    
   1.371 +    context->eglPrivateSignalSync = (TFPtrEglPrivateSignalSyncNok) eglGetProcAddress("egl_Private_SignalSyncNOK");
   1.372 +    if (! context->eglPrivateSignalSync)
   1.373 +        {
   1.374 +        DPRINT(("WFC_Context_Initialize(): Missing EGL extension - egl_Private_SignalSyncNOK extension"));   
   1.375 +        return NULL;
   1.376 +        }
   1.377 +
   1.378 +    err2 =OWF_MessageQueue_Init(&context->composerQueue);
   1.379 +    if (err2 != 0)
   1.380 +        {
   1.381 +        DPRINT(("WFC_Context_Initialize(): Cannot initialise the message queue err(%d)", err2));   
   1.382 +        return NULL;
   1.383 +        }
   1.384 +    
   1.385 +    context->composerThread = OWF_Thread_Create(WFC_Context_ComposerThread, context);
   1.386 +    if (!(context->composerThread))
   1.387 +        {
   1.388 +        /* must call these to remove references to context */
   1.389 +        DPRINT(("WFC_Context_Initialize(): Failed to create thread!"));
   1.390 +        return NULL;
   1.391 +        }
   1.392 +
   1.393 +    OWF_ComposerThread_RendezvousWait(context->displayContext);
   1.394 +    
   1.395 +    /*the following section of the code could be pushed to adaptation in future*/
   1.396 +    if (type == WFC_CONTEXT_TYPE_ON_SCREEN)
   1.397 +    {
   1.398 +       OWF_IMAGE_FORMAT        imageFormat;
   1.399 +       OWF_SCREEN              screen;
   1.400 +       WFCint width = 0;
   1.401 +       WFCint height = 0;
   1.402 +       WFCint normalSize = 0;
   1.403 +       WFCint flippedSize = 0;
   1.404 +       WFCNativeStreamType stream;
   1.405 +    
   1.406 +       /* Set up stream for sending data to screen */
   1.407 +       
   1.408 +       if (!OWF_Screen_GetHeader(context->displayContext, &screen))
   1.409 +       {
   1.410 +           DPRINT(("WFC_Context_Initialize(): Could not retrieve the screen parameters"));
   1.411 +           WFC_Context_Shutdown(context);
   1.412 +           return NULL;
   1.413 +       }
   1.414 +    
   1.415 +       /* Set on-screen pixel format */
   1.416 +       imageFormat.pixelFormat     = OWF_SURFACE_PIXEL_FORMAT;
   1.417 +       imageFormat.premultiplied   = OWF_SURFACE_PREMULTIPLIED;
   1.418 +       imageFormat.linear          = OWF_SURFACE_LINEAR;
   1.419 +       imageFormat.rowPadding      = OWF_SURFACE_ROWPADDING;
   1.420 +    
   1.421 +       width = screen.normal.width;
   1.422 +       height = screen.normal.height;
   1.423 +       
   1.424 +       normalSize = screen.normal.height * screen.normal.stride;
   1.425 +       flippedSize = screen.flipped.height * screen.flipped.stride;
   1.426 +       
   1.427 +       if (flippedSize > normalSize)
   1.428 +           {
   1.429 +           width = screen.flipped.width;
   1.430 +           height = screen.flipped.height;
   1.431 +           }
   1.432 +       
   1.433 +       stream = owfNativeStreamCreateImageStream(width,
   1.434 +                                                 height,
   1.435 +                                                 &imageFormat,
   1.436 +                                                 1);
   1.437 +    
   1.438 +       if (stream)
   1.439 +       {
   1.440 +           WFC_Context_SetTargetStream(context, stream);
   1.441 +           
   1.442 +           /* At this point the stream's refcount is 2 - we must decrement
   1.443 +            * it by one to ensure that the stream is destroyed when the
   1.444 +            * context (that "owns" it) is destroyed.
   1.445 +            */
   1.446 +           owfNativeStreamRemoveReference(stream);
   1.447 +       }
   1.448 +       else
   1.449 +       {
   1.450 +           DPRINT(("WFC_Context_Initialize(): cannot create internal target stream"));
   1.451 +           WFC_Context_Shutdown(context);
   1.452 +           return NULL;
   1.453 +       }
   1.454 +    }
   1.455 +    else
   1.456 +    {
   1.457 +       WFC_Context_SetTargetStream(context, stream);
   1.458 +    }
   1.459 +    
   1.460 +    nbufs = SCRATCH_BUFFER_COUNT-1;
   1.461 +    for (ii = 0; ii < nbufs; ii++)
   1.462 +    {
   1.463 +        scratch[ii] = OWF_Image_AllocData(context->displayContext, MAX_SOURCE_WIDTH,
   1.464 +                MAX_SOURCE_HEIGHT,
   1.465 +                OWF_IMAGE_ARGB_INTERNAL);
   1.466 +        fail = fail || (scratch[ii] == NULL);
   1.467 +    }
   1.468 +
   1.469 +    /*
   1.470 +     * allocate one-channel buffer for alpha
   1.471 +     * obs! this assumes sizeof(OWFsubpixel) is 4.
   1.472 +     */
   1.473 +    scratch[nbufs] = OWF_Image_AllocData(context->displayContext, MAX_SOURCE_WIDTH,
   1.474 +                                         MAX_SOURCE_HEIGHT,
   1.475 +                                         OWF_IMAGE_L32);
   1.476 +    fail = fail || (scratch[nbufs] == NULL);
   1.477 +    
   1.478 +    if (fail)
   1.479 +    {
   1.480 +        for (ii = 0; ii < SCRATCH_BUFFER_COUNT; ii++)
   1.481 +        {
   1.482 +            OWF_Image_FreeData(context->displayContext, &scratch[ii]);
   1.483 +        }
   1.484 +        WFC_Context_Shutdown(context);
   1.485 +        return NULL;
   1.486 +    }
   1.487 +
   1.488 +    for (ii = 0; ii < SCRATCH_BUFFER_COUNT; ii++)
   1.489 +    {
   1.490 +        context->scratchBuffer[ii] = scratch[ii];
   1.491 +    }
   1.492 +    
   1.493 +    if (    OWF_Semaphore_Init(&context->compositionSemaphore, 1)
   1.494 +        ||  OWF_Semaphore_Init(&context->commitSemaphore, 1)
   1.495 +        ||  OWF_Mutex_Init(&context->updateFlagMutex)
   1.496 +        ||  OWF_Mutex_Init(&context->sceneMutex)
   1.497 +        
   1.498 +        )
   1.499 +        {
   1.500 +        DPRINT(("WFC_Context_Initialize(): Could not create mutexes and semaphores!"));
   1.501 +        WFC_Context_Shutdown(context);
   1.502 +        return NULL;
   1.503 +        }
   1.504 +
   1.505 +    if (!WFC_Pipeline_CreateState(context) || !WFC_Context_CreateState(context))
   1.506 +         {
   1.507 +         DPRINT(("WFC_Context_Initialize(): Could not create pipeline state object"));
   1.508 +         WFC_Context_Shutdown(context);
   1.509 +         return NULL;
   1.510 +         }
   1.511 +    
   1.512 +
   1.513 +    attribStatus= WFC_Context_InitializeAttributes(context, type);
   1.514 +
   1.515 +    if (attribStatus!=ATTR_ERROR_NONE)
   1.516 +        {
   1.517 +        WFC_Context_Shutdown(context);
   1.518 +        return NULL;
   1.519 +        }
   1.520 +    
   1.521 +    
   1.522 +    context->scenePool = OWF_Pool_Create(sizeof(WFC_SCENE),
   1.523 +                                         CONTEXT_SCENE_POOL_SIZE);
   1.524 +    context->elementPool = OWF_Pool_Create(sizeof(WFC_ELEMENT),
   1.525 +                                           CONTEXT_ELEMENT_POOL_SIZE);
   1.526 +    context->nodePool = OWF_Pool_Create(sizeof(OWF_NODE),
   1.527 +                                        CONTEXT_NODE_POOL_SIZE);
   1.528 +    if (!( context->scenePool &&
   1.529 +          context->nodePool && context->elementPool))
   1.530 +    {
   1.531 +        WFC_Context_Shutdown(context);
   1.532 +        return NULL;
   1.533 +    }
   1.534 +
   1.535 +    DPRINT(("  Creating scenes"));
   1.536 +    context->workScene = WFC_Scene_Create(context);
   1.537 +    context->committedScene = WFC_Scene_Create(context);
   1.538 +    context->snapshotScene = NULL;
   1.539 +
   1.540 +    /* snapshotScene is initialized in InvokeCommit */
   1.541 +
   1.542 +    /* context's refcount is now 3 */
   1.543 +
   1.544 +    if (!(context->workScene && context->committedScene &&
   1.545 +          context->nodePool && context->elementPool))
   1.546 +    {
   1.547 +        WFC_Context_Shutdown(context);
   1.548 +        return NULL;
   1.549 +    }
   1.550 +
   1.551 +    return context;
   1.552 +}
   1.553 +
   1.554 +/*---------------------------------------------------------------------------
   1.555 + *  Create new context on device
   1.556 + *
   1.557 + *  \param device Device on which the context should be created
   1.558 + *  \param type Context type (on- or off-screen)
   1.559 + *
   1.560 + *  \return New context object or NULL in case of failure
   1.561 + *----------------------------------------------------------------------------*/
   1.562 +OWF_API_CALL WFC_CONTEXT*
   1.563 +WFC_Context_Create(WFC_DEVICE* device,
   1.564 +                   WFCNativeStreamType stream,
   1.565 +                   WFCContextType type,
   1.566 +                   WFCint screenNum)
   1.567 +{
   1.568 +    WFC_CONTEXT*            context = NULL;
   1.569 +
   1.570 +    OWF_ASSERT(device);
   1.571 +    context = CREATE(WFC_CONTEXT);
   1.572 +
   1.573 +    if (context)
   1.574 +    {
   1.575 +        if (!WFC_Context_Initialize(context, device, stream, type, screenNum))
   1.576 +        {
   1.577 +            DESTROY(context);
   1.578 +        }
   1.579 +    }
   1.580 +    return context;
   1.581 +}
   1.582 +
   1.583 +/*---------------------------------------------------------------------------
   1.584 + *  Setup context rendering target
   1.585 + *
   1.586 + *  \param context Context
   1.587 + *  \param stream Target stream to use for rendering
   1.588 + *----------------------------------------------------------------------------*/
   1.589 +OWF_API_CALL void
   1.590 +WFC_Context_SetTargetStream(WFC_CONTEXT* context,
   1.591 +                            OWFNativeStreamType stream)
   1.592 +{
   1.593 +    OWF_ASSERT(context);
   1.594 +    context->stream = stream;
   1.595 +
   1.596 +    owfNativeStreamAddReference(stream);
   1.597 +
   1.598 +    owfNativeStreamGetHeader(stream,
   1.599 +                             &context->targetWidth, &context->targetHeight,
   1.600 +                             NULL, NULL, NULL);
   1.601 +}
   1.602 +
   1.603 +static OWFboolean
   1.604 +WFC_FastpathCheckTransparency(WFCbitfield transparencyTypes, WFCfloat globalAlpha, OWF_PIXEL_FORMAT sourceFormat)
   1.605 +    {
   1.606 +    if ((transparencyTypes & WFC_TRANSPARENCY_ELEMENT_GLOBAL_ALPHA) && (globalAlpha != 255.0f))
   1.607 +        {
   1.608 +        DPRINT(("=== WFC_FastpathCheckTransparency - Failed global alfa(%f) check", globalAlpha));
   1.609 +        return OWF_FALSE;
   1.610 +        }
   1.611 +
   1.612 +    if ((transparencyTypes & WFC_TRANSPARENCY_SOURCE) && (sourceFormat != OWF_IMAGE_XRGB8888))
   1.613 +        {
   1.614 +        DPRINT(("=== WFC_FastpathCheckTransparency - Failed transparency check types=0x%x format=0x%x", 
   1.615 +                transparencyTypes, sourceFormat));
   1.616 +        return OWF_FALSE;
   1.617 +        }
   1.618 +    
   1.619 +
   1.620 +    return OWF_TRUE;
   1.621 +    }
   1.622 +
   1.623 +static OWFboolean
   1.624 +WFC_FastpathCheckGeometry(WFC_CONTEXT* context, WFC_ELEMENT* element)
   1.625 +    {
   1.626 +    OWFint sourceWidth = 0;
   1.627 +    OWFint sourceHeight = 0;
   1.628 +    OWFint destWidth = 0;
   1.629 +    OWFint destHeight = 0;
   1.630 +    OWFint targetWidth = 0;
   1.631 +    OWFint targetHeight = 0;
   1.632 +    
   1.633 +    OWF_ASSERT(context);
   1.634 +    OWF_ASSERT(element);
   1.635 +
   1.636 +    if ((element->srcRect[0] != 0) || (element->srcRect[1] != 0))
   1.637 +        {
   1.638 +        DPRINT(("=== WFC_FastpathCheckGeometry(context = %p) - FAILED Source Position Check", context));
   1.639 +        return OWF_FALSE;
   1.640 +        }
   1.641 +    
   1.642 +    if ((element->dstRect[0] != 0) || (element->dstRect[1] != 0))
   1.643 +        {
   1.644 +        DPRINT(("=== WFC_FastpathCheckGeometry(context = %p) - FAILED Destination Position Check", context));
   1.645 +        return OWF_FALSE;
   1.646 +        }
   1.647 +    
   1.648 +    if(element->sourceFlip)
   1.649 +        {
   1.650 +        DPRINT(("=== WFC_FastpathCheckGeometry(context = %p) - FAILED Source Flip Check", context));
   1.651 +        return OWF_FALSE;
   1.652 +        }
   1.653 +    
   1.654 +    if(element->sourceRotation == WFC_ROTATION_0)
   1.655 +        {
   1.656 +        sourceWidth = element->srcRect[2];
   1.657 +        sourceHeight = element->srcRect[3];
   1.658 +        }
   1.659 +    else
   1.660 +        {
   1.661 +        DPRINT(("=== WFC_FastpathCheckGeometry(context = %p) - FAILED Source Rotation (0x%x) Check", 
   1.662 +                context, element->sourceRotation));
   1.663 +        return OWF_FALSE;
   1.664 +        }
   1.665 +    
   1.666 +    destWidth = element->dstRect[2];
   1.667 +    destHeight = element->dstRect[3];
   1.668 +    
   1.669 +    if ((sourceWidth != destWidth) || (sourceHeight != destHeight))
   1.670 +       {
   1.671 +       DPRINT(("=== WFC_FastpathCheckGeometry(context = %p) - FAILED Non-scaling Check", context));
   1.672 +       return OWF_FALSE;
   1.673 +       }
   1.674 +
   1.675 +    if (context->rotation == WFC_ROTATION_0 || OWF_Screen_Rotation_Supported(context->displayContext))
   1.676 +        {
   1.677 +        if (context->rotation == WFC_ROTATION_0 || context->rotation == WFC_ROTATION_180)
   1.678 +            {
   1.679 +            targetWidth = context->targetWidth;
   1.680 +            targetHeight = context->targetHeight;
   1.681 +            }
   1.682 +        else
   1.683 +            {
   1.684 +            targetWidth = context->targetHeight;
   1.685 +            targetHeight = context->targetWidth;
   1.686 +            }
   1.687 +        
   1.688 +        if (destWidth == targetWidth && destHeight == targetHeight)
   1.689 +            {
   1.690 +            return OWF_TRUE;
   1.691 +            }
   1.692 +        else
   1.693 +            {
   1.694 +            DPRINT(("=== WFC_FastpathCheckGeometry(context = %p) - Failed Dimensions Check", context));
   1.695 +            }
   1.696 +        }
   1.697 +    else
   1.698 +        {
   1.699 +        DPRINT(("=== WFC_FastpathCheckGeometry(context = %p) - Failed Supported Rotations Check", context));
   1.700 +        }
   1.701 +    
   1.702 +    return OWF_FALSE;
   1.703 +    }
   1.704 +
   1.705 +/**
   1.706 + * Check if the current scene is candidate for fastpath optimisation.
   1.707 + * Fastpath optimisation means the topmost visible layer will be passed direct to the 
   1.708 + * lower level for presentation on the display without being composed.
   1.709 + * There are two questions:
   1.710 + * - Is the scene itself suitable for fastpathing?
   1.711 + * - Can the surface selected for fastpathing be presented directly by the display?
   1.712 + * This function will check the scene (eg reject if the top stream is non-opaque or smaller than the screen)
   1.713 + *
   1.714 + * @param context context object containing the scene to be checked.
   1.715 + **/
   1.716 +
   1.717 +/* [Not in doxygen]
   1.718 + * The first time MOpenWFC_RI_Display_Update::SetTopLayerSurface (or SetLayerSurface) is called 
   1.719 + * with a different stream handle, it can fail indicating the display cannot accept the stream.
   1.720 + * The compositor will then have to immediately compose that frame as normal, and should continue
   1.721 + * to perform normal composition until the scene changes to present a different stream as fastpath candidate.  
   1.722 + * 
   1.723 + * There is a middle ground, e.g. can the hardware handle over-sized images, or do scaling or do rotation?
   1.724 + * SetTopLayerSurface accepts an optional list of imperfect attributes to be checked by the adaptation.
   1.725 + * By WFC_Context_CheckFastpath only listing the attributes that are considered imperfect,
   1.726 + * and SetLayerSurface rejecting fastpath for any attribute IDs that it doesn't recognise,
   1.727 + * safe independent extensibility is assured. 
   1.728 + */
   1.729 +static void
   1.730 +WFC_Context_CheckFastpath(WFC_CONTEXT* context)
   1.731 +    {
   1.732 +    WFC_ELEMENT* element = NULL;
   1.733 +    OWF_ASSERT(context);
   1.734 +    
   1.735 +    DPRINT(("WFC_Context_CheckFastpath(context = %p) - Check Fastpath", context));
   1.736 +    if ((context->type != WFC_CONTEXT_TYPE_ON_SCREEN) ||
   1.737 +        OWF_DisplayContext_FastpathChecked(context->displayContext))
   1.738 +        {
   1.739 +        return;
   1.740 +        }
   1.741 +    
   1.742 +    // Simple case-fast path top most layer
   1.743 +    // More complex case, fast-path top most VISIBLE, OPAQUE layer.
   1.744 +    OWF_DisplayContext_DisableFastpath(context->displayContext);
   1.745 +    OWF_DisplayContext_SetFastpathChecked(context->displayContext);
   1.746 +    // Find top layer
   1.747 +    element = WFC_Scene_TopMostElement(context->committedScene);
   1.748 +    if (element && element->source && element->skipCompose == WFC_FALSE)
   1.749 +        {
   1.750 +    
   1.751 +        if (element->mask)
   1.752 +            {
   1.753 +            DPRINT(("=== WFC_Context_CheckFastpath(context = %p) - FAILED elemenent includes mask", context));
   1.754 +            return;
   1.755 +            }
   1.756 +        
   1.757 +        OWF_ASSERT(element->source->lockedStream.image);
   1.758 +        
   1.759 +        OWF_ASSERT(element->source->streamHandle != OWF_INVALID_HANDLE);
   1.760 +        
   1.761 +        if (!WFC_FastpathCheckGeometry(context, element))
   1.762 +            {
   1.763 +            return;
   1.764 +            }
   1.765 +        
   1.766 +        if (!WFC_FastpathCheckTransparency(element->transparencyTypes, 
   1.767 +                                           element->globalAlpha, 
   1.768 +                                           element->source->lockedStream.image->format.pixelFormat))
   1.769 +            {
   1.770 +            return;
   1.771 +            }
   1.772 +        
   1.773 +        OWF_DisplayContext_EnableFastpath(context->displayContext, element->source->streamHandle);
   1.774 +        DPRINT(("=== WFC_Context_CheckFastpath(context = %p) - Fastpath Enabled", context));
   1.775 +        }
   1.776 +    }
   1.777 +
   1.778 +/*---------------------------------------------------------------------------
   1.779 + * Checks if the given stream would be valid as an off-screen context target.
   1.780 + * 
   1.781 + * Checks that the format can be rendered TO.
   1.782 + * Also checks that the image size is acceptable (within the scratch buffers).
   1.783 + * This is called before the context is created so is effectively a "static" context method.
   1.784 + * 
   1.785 + *  \param stream Target stream to use for rendering
   1.786 + ---------------------------------------------------------------------------*/
   1.787 +
   1.788 +OWF_API_CALL WFCboolean
   1.789 +WFC_Context_IsValidTargetStream(OWFNativeStreamType stream)
   1.790 +{
   1.791 +    OWFint      width,height;
   1.792 +    OWF_IMAGE_FORMAT    format;
   1.793 +    owfNativeStreamGetHeader(stream,
   1.794 +                         &width, &height,
   1.795 +                         NULL, &format, NULL);
   1.796 +    if (width>MAX_SOURCE_WIDTH)
   1.797 +        {
   1.798 +        return WFC_FALSE;
   1.799 +        }
   1.800 +    if (height>MAX_SOURCE_HEIGHT)
   1.801 +        {
   1.802 +        return WFC_FALSE;
   1.803 +        }
   1.804 +    return (WFCboolean)OWF_Image_IsValidDestinationFormat(&format);
   1.805 +}
   1.806 +
   1.807 +/*---------------------------------------------------------------------------
   1.808 + * Checks that the image size is acceptable (within the scratch buffers).
   1.809 + * This is called before the source is created.
   1.810 + * 
   1.811 + *  \param stream source stream to use for rendering.
   1.812 + ---------------------------------------------------------------------------*/
   1.813 +
   1.814 +OWF_API_CALL WFCboolean
   1.815 +WFC_Context_IsValidSourceStream(OWFNativeStreamType stream)
   1.816 +{
   1.817 +    OWFint      width,height;
   1.818 +    owfNativeStreamGetHeader(stream,
   1.819 +                         &width, &height,
   1.820 +                         NULL, NULL, NULL);
   1.821 +    if ((width+2) * (height+2) > MAX_SOURCE_WIDTH * MAX_SOURCE_HEIGHT)
   1.822 +        {
   1.823 +        return WFC_FALSE;
   1.824 +        }
   1.825 +    return WFC_TRUE;
   1.826 +}
   1.827 +
   1.828 +/*---------------------------------------------------------------------------
   1.829 + *  Find element from current scene
   1.830 + *
   1.831 + *  \param context Context object
   1.832 + *  \param element Element to find
   1.833 + *
   1.834 + *  \return Element object or NULL if element hasn't been inserted
   1.835 + *  into current scene.
   1.836 + *----------------------------------------------------------------------------*/
   1.837 +OWF_API_CALL WFC_ELEMENT*
   1.838 +WFC_Context_FindElement(WFC_CONTEXT* context,
   1.839 +                        WFCElement element)
   1.840 +{
   1.841 +    OWF_ASSERT(context);
   1.842 +    return WFC_Scene_FindElement(context->workScene, element);
   1.843 +}
   1.844 +
   1.845 +/*---------------------------------------------------------------------------
   1.846 + *  Commit context scene graph changes
   1.847 + *
   1.848 + *  \param context Context to commit
   1.849 + *----------------------------------------------------------------------------*/
   1.850 +static void
   1.851 +WFC_Context_DoCommit(WFC_CONTEXT* context)
   1.852 +{
   1.853 +    OWF_ASSERT(context);
   1.854 +    DPRINT(("WFC_Context_DoCommit(context = %p)", context));
   1.855 +
   1.856 +    OWF_ASSERT(context->snapshotScene);
   1.857 +
   1.858 +
   1.859 +    DPRINT(("COMMIT: Committing attribute list changes"));
   1.860 +
   1.861 +    DPRINT(("COMMIT: Acquiring mutex"));
   1.862 +    OWF_Mutex_Lock(&context->sceneMutex);
   1.863 +
   1.864 +    /* comitting scene attribute changes */
   1.865 +    DPRINT(("COMMIT: Committing scene attribute changes"));
   1.866 +    OWF_AttributeList_Commit(&context->attributes,
   1.867 +                             WFC_CONTEXT_TYPE,
   1.868 +                             WFC_CONTEXT_BG_COLOR,COMMITTED_ATTR_VALUE_INDEX);
   1.869 +
   1.870 +
   1.871 +    /* resolve sources and masks */
   1.872 +    DPRINT(("COMMIT: Committing scene changes"));
   1.873 +    WFC_Scene_Commit(context->snapshotScene);
   1.874 +    DPRINT(("COMMIT: Destroying old committed scene"));
   1.875 +    WFC_Scene_Destroy(context->committedScene);
   1.876 +    DPRINT(("COMMIT: Setting new snapshot scene as committed one."));
   1.877 +    context->committedScene = context->snapshotScene;
   1.878 +    context->snapshotScene = NULL;
   1.879 +
   1.880 +    // reset the visibility flags
   1.881 +    owfSymDeviceResetVisibilityState(context);
   1.882 +    
   1.883 +    DPRINT(("COMMIT: Unlocking mutex"));
   1.884 +    OWF_Mutex_Unlock(&context->sceneMutex);
   1.885 +
   1.886 +    DPRINT(("COMMIT: Signaling commit semaphore"));
   1.887 +    /* signal we're ready */
   1.888 +    OWF_Semaphore_Post(&context->commitSemaphore);
   1.889 +}
   1.890 +
   1.891 +/*---------------------------------------------------------------------------
   1.892 + *
   1.893 + *----------------------------------------------------------------------------*/
   1.894 +static OWFboolean
   1.895 +WFC_Context_LockTargetForWriting(WFC_CONTEXT* context)
   1.896 +{
   1.897 +    OWF_ASSERT(context);
   1.898 +
   1.899 +    DPRINT(("WFC_Context_LockTargetForWriting"));
   1.900 +
   1.901 +    context->state.targetBuffer =
   1.902 +        owfNativeStreamAcquireWriteBuffer(context->stream);
   1.903 +    
   1.904 +    if (!context->state.targetBuffer)
   1.905 +        {
   1.906 +        DPRINT(("Failed to WFC_Context_LockTargetForWriting owfNativeStreamAcquireWriteBuffer"));
   1.907 +        return OWF_FALSE;
   1.908 +        }
   1.909 +    context->state.targetPixels =
   1.910 +        owfNativeStreamGetBufferPtr(context->stream,
   1.911 +                                    context->state.targetBuffer);
   1.912 +
   1.913 +    if ((WFC_ROTATION_0   == context->rotation || WFC_ROTATION_180 == context->rotation) ||
   1.914 +        !OWF_Screen_Rotation_Supported(context->displayContext))
   1.915 +    {
   1.916 +        /* final target, in target format */
   1.917 +        context->state.targetImage =context->state.unrotatedTargetImage;
   1.918 +    }
   1.919 +    else
   1.920 +    {
   1.921 +        /* final target, in target format */
   1.922 +        /* fExt stride/padding value is the rotated value */
   1.923 +        context->state.targetImage = context->state.rotatedTargetImage;
   1.924 +    }
   1.925 +    OWF_Image_SetPixelBuffer(context->state.targetImage,context->state.targetPixels);
   1.926 +    
   1.927 +    if (context->state.targetImage==NULL)
   1.928 +        {
   1.929 +        OWF_ASSERT(WFC_FALSE);
   1.930 +        }
   1.931 +
   1.932 +    /* take context rotation into account. */
   1.933 +    if (WFC_ROTATION_0   == context->rotation ||
   1.934 +        WFC_ROTATION_180 == context->rotation)
   1.935 +    {
   1.936 +        context->state.internalTargetImage = context->state.unrotatedInternalTargetImage;
   1.937 +    }
   1.938 +    else
   1.939 +    {
   1.940 +        context->state.internalTargetImage = context->state.rotatedInternalTargetImage;
   1.941 +    }
   1.942 +
   1.943 +    if (context->state.internalTargetImage==NULL)
   1.944 +        {
   1.945 +        OWF_ASSERT(WFC_FALSE);
   1.946 +        }
   1.947 +    return OWF_TRUE;
   1.948 +}
   1.949 +
   1.950 +/*---------------------------------------------------------------------------
   1.951 + *
   1.952 + *----------------------------------------------------------------------------*/
   1.953 +static void
   1.954 +WFC_Context_UnlockTarget(WFC_CONTEXT* context,OWFboolean aDoPost)
   1.955 +{
   1.956 +    OWF_ROTATION rotation = OWF_ROTATION_0;
   1.957 +
   1.958 +    OWF_ASSERT(context);
   1.959 +    DPRINT(("WFC_Context_UnlockTarget"));
   1.960 +    DPRINT(("  Unlocking target stream=%d, buffer=%d",
   1.961 +            context->stream, context->state.targetBuffer));
   1.962 +
   1.963 +    owfNativeStreamReleaseWriteBuffer(context->stream,
   1.964 +                                      context->state.targetBuffer,
   1.965 +                                      EGL_NO_DISPLAY,
   1.966 +                                      NULL);
   1.967 +
   1.968 +    
   1.969 +    if (aDoPost)
   1.970 +    {
   1.971 +        switch (context->rotation)
   1.972 +        {
   1.973 +            case WFC_ROTATION_0:
   1.974 +            {
   1.975 +                rotation = OWF_ROTATION_0;
   1.976 +                break;
   1.977 +            }
   1.978 +            case WFC_ROTATION_90:
   1.979 +            {
   1.980 +                rotation = OWF_ROTATION_90;
   1.981 +                break;
   1.982 +            }
   1.983 +            case WFC_ROTATION_180:
   1.984 +            {
   1.985 +                rotation = OWF_ROTATION_180;
   1.986 +                break;
   1.987 +            }
   1.988 +            case WFC_ROTATION_270:
   1.989 +            {
   1.990 +                rotation = OWF_ROTATION_270;
   1.991 +                break;
   1.992 +            }
   1.993 +            default:
   1.994 +            {
   1.995 +                OWF_ASSERT(0);
   1.996 +            }
   1.997 +        }
   1.998 +        
   1.999 +        if (!OWF_Screen_Post_Topmost_Layer(context->displayContext, context->stream, rotation))
  1.1000 +            {   //getting a fail here is bad... display did not accept the composition buffer.
  1.1001 +            DPRINT(("WFC_Context_UnlockTarget - OWF_Screen_Post_Topmost_Layer failed for composition stream"));
  1.1002 +           OWF_ASSERT(0);
  1.1003 +            }
  1.1004 +    }
  1.1005 +}
  1.1006 +
  1.1007 +/*---------------------------------------------------------------------------
  1.1008 + *
  1.1009 + *----------------------------------------------------------------------------*/
  1.1010 +static void
  1.1011 +WFC_Context_PrepareComposition(WFC_CONTEXT* context)
  1.1012 +{
  1.1013 +    OWFsubpixel             r, g, b, a;
  1.1014 +
  1.1015 +    OWF_ASSERT(context);
  1.1016 +
  1.1017 +    /* the first thing to do is to lock target stream and fetch
  1.1018 +       write buffer to it. fetching blocks until one is available,
  1.1019 +       but since only one stream can be target to only one context
  1.1020 +       at the time, no stalls should occur */
  1.1021 +    WFC_Context_LockTargetForWriting(context);
  1.1022 +
  1.1023 +    /* prepare for composition by "clearing the table" with
  1.1024 +       background color.  */
  1.1025 +
  1.1026 +    r = (OWFsubpixel) OWF_RED_MAX_VALUE * ((context->backgroundColor >> 24) &
  1.1027 +        0xFF) / OWF_BYTE_MAX_VALUE;
  1.1028 +    g = (OWFsubpixel) OWF_GREEN_MAX_VALUE * ((context->backgroundColor >> 16) &
  1.1029 +        0xFF) / OWF_BYTE_MAX_VALUE;
  1.1030 +    b = (OWFsubpixel) OWF_BLUE_MAX_VALUE * ((context->backgroundColor >> 8) &
  1.1031 +        0xFF) / OWF_BYTE_MAX_VALUE;
  1.1032 +    a = (OWFsubpixel) OWF_ALPHA_MAX_VALUE * (context->backgroundColor & 0xFF) /
  1.1033 +        OWF_BYTE_MAX_VALUE;
  1.1034 +
  1.1035 +    r = r * a / OWF_ALPHA_MAX_VALUE;
  1.1036 +    g = g * a / OWF_ALPHA_MAX_VALUE;
  1.1037 +    b = b * a / OWF_ALPHA_MAX_VALUE;
  1.1038 +
  1.1039 +    OWF_Image_Clear(context->state.internalTargetImage, r, g, b, a);
  1.1040 +}
  1.1041 +
  1.1042 +
  1.1043 +
  1.1044 +/*---------------------------------------------------------------------------
  1.1045 + *
  1.1046 + *----------------------------------------------------------------------------*/
  1.1047 +static void
  1.1048 +WFC_Context_FinishComposition(WFC_CONTEXT* context)
  1.1049 +{
  1.1050 +    OWF_ROTATION            rotation = OWF_ROTATION_0;
  1.1051 +    OWFboolean              screenRotation;
  1.1052 +
  1.1053 +    OWF_ASSERT(context);
  1.1054 +
  1.1055 +    screenRotation = OWF_Screen_Rotation_Supported(context->displayContext);
  1.1056 +    /* re-use scratch buffer 1 for context rotation */
  1.1057 +    if (WFC_ROTATION_0   == context->rotation || screenRotation)
  1.1058 +    {
  1.1059 + 
  1.1060 +        if (screenRotation)
  1.1061 +        {
  1.1062 +            if (WFC_ROTATION_90   == context->rotation || WFC_ROTATION_270   == context->rotation)
  1.1063 +                {
  1.1064 +                owfSetStreamFlipState(context->stream, OWF_TRUE);
  1.1065 +                }
  1.1066 +            else
  1.1067 +                {
  1.1068 +                owfSetStreamFlipState(context->stream, OWF_FALSE);
  1.1069 +                }
  1.1070 +        }
  1.1071 +        OWF_Image_DestinationFormatConversion(context->state.targetImage, context->state.internalTargetImage);
  1.1072 +    }
  1.1073 +    else
  1.1074 +    {
  1.1075 +        switch (context->rotation)
  1.1076 +        {
  1.1077 +            case WFC_ROTATION_0:
  1.1078 +            {
  1.1079 +                rotation = OWF_ROTATION_0;
  1.1080 +                break;
  1.1081 +            }
  1.1082 +            case WFC_ROTATION_90:
  1.1083 +            {
  1.1084 +                rotation = OWF_ROTATION_90;
  1.1085 +                break;
  1.1086 +            }
  1.1087 +            case WFC_ROTATION_180:
  1.1088 +            {
  1.1089 +                rotation = OWF_ROTATION_180;
  1.1090 +                break;
  1.1091 +            }
  1.1092 +            case WFC_ROTATION_270:
  1.1093 +            {
  1.1094 +                rotation = OWF_ROTATION_270;
  1.1095 +                break;
  1.1096 +            }
  1.1097 +            default:
  1.1098 +            {
  1.1099 +                OWF_ASSERT(0);
  1.1100 +            }
  1.1101 +        }
  1.1102 +     
  1.1103 +        /* rotate */
  1.1104 +        OWF_Image_Rotate(context->state.rotatedTargetImage,
  1.1105 +                         context->state.internalTargetImage,
  1.1106 +                         rotation);
  1.1107 +
  1.1108 +        /* Note: support of different target formats  can be put here */
  1.1109 +
  1.1110 +        OWF_Image_DestinationFormatConversion(context->state.targetImage, context->state.rotatedTargetImage);
  1.1111 +    }
  1.1112 +    WFC_Context_UnlockTarget(context,(context->type==WFC_CONTEXT_TYPE_ON_SCREEN)?OWF_TRUE:OWF_FALSE);
  1.1113 +}
  1.1114 +
  1.1115 +/*!---------------------------------------------------------------------------
  1.1116 + * \brief Actual composition routine.
  1.1117 + *  Mainly just calls other functions that executes different stages of
  1.1118 + *  the composition pipeline.
  1.1119 + *  \param context Context to compose.
  1.1120 + *----------------------------------------------------------------------------*/
  1.1121 +static void
  1.1122 +WFC_Context_DoCompose(WFC_CONTEXT* context)
  1.1123 +{
  1.1124 +    WFC_SCENE*              scene   = NULL;
  1.1125 +    OWF_NODE*               node    = NULL;
  1.1126 +
  1.1127 +    OWF_ASSERT(context);
  1.1128 +
  1.1129 +    OWF_Mutex_Lock(&context->updateFlagMutex);
  1.1130 +
  1.1131 +    OWF_DisplayContext_IncrementSerialNumber(context->displayContext);
  1.1132 +    
  1.1133 +    OWF_DisplayContext_SetCompositionOngoing(context->displayContext, WFC_TRUE);
  1.1134 +    context->sourceUpdateCount = 0;
  1.1135 +    OWF_Mutex_Unlock(&context->updateFlagMutex);
  1.1136 +    
  1.1137 +    DPRINT(("WFC_Context_Compose"));
  1.1138 +    /* Composition always uses the committed version
  1.1139 +     * of the scene.
  1.1140 +     */
  1.1141 +
  1.1142 +    WFC_Scene_LockSourcesAndMasks(context->committedScene);
  1.1143 +    
  1.1144 +    OWF_Mutex_Lock(&context->sceneMutex);
  1.1145 +    
  1.1146 +    WFC_Context_CheckFastpath(context);
  1.1147 +    if (OWF_DisplayContext_FastpathEnabled(context->displayContext))
  1.1148 +        {
  1.1149 +        WFCboolean targetStreamAccessed;
  1.1150 +        OWFboolean screenRotation;
  1.1151 +        screenRotation = OWF_Screen_Rotation_Supported(context->displayContext);
  1.1152 +        if (WFC_Context_Active(context))
  1.1153 +            {   //Full fastpath is only supported for autonomous composition 
  1.1154 +            OWFNativeStreamType stream = OWF_INVALID_HANDLE;
  1.1155 +            OWF_ROTATION rotation = OWF_ROTATION_0;
  1.1156 +    
  1.1157 +            DPRINT(("== WFC_Context_DoCompose(context = %p) - Fastpathing", context));
  1.1158 +    
  1.1159 +            stream = OWF_DisplayContext_FastpathStream(context->displayContext);
  1.1160 +            if (screenRotation)
  1.1161 +                {
  1.1162 +                switch (context->rotation)
  1.1163 +                    {
  1.1164 +                    case WFC_ROTATION_0:
  1.1165 +                        {
  1.1166 +                        rotation = OWF_ROTATION_0;
  1.1167 +                        break;
  1.1168 +                        }
  1.1169 +                    case WFC_ROTATION_90:
  1.1170 +                        {
  1.1171 +                        rotation = OWF_ROTATION_90;
  1.1172 +                        break;
  1.1173 +                        }
  1.1174 +                    case WFC_ROTATION_180:
  1.1175 +                        {
  1.1176 +                        rotation = OWF_ROTATION_180;
  1.1177 +                        break;
  1.1178 +                        }
  1.1179 +                    case WFC_ROTATION_270:
  1.1180 +                        {
  1.1181 +                        rotation = OWF_ROTATION_270;
  1.1182 +                        break;
  1.1183 +                        }
  1.1184 +                    default:
  1.1185 +                        {
  1.1186 +                        OWF_ASSERT(0);
  1.1187 +                        rotation = OWF_ROTATION_0;
  1.1188 +                        }
  1.1189 +                    }
  1.1190 +                }
  1.1191 +    
  1.1192 +            if (!OWF_Screen_Post_Topmost_Layer(context->displayContext, stream, rotation))
  1.1193 +                {
  1.1194 +            
  1.1195 +                DPRINT(("WFC_Context_Compose calls OWF_DisplayContext_DisableFastpath because !OWF_Screen_Post_Topmost_Layer()"));
  1.1196 +                OWF_DisplayContext_DisableFastpath(context->displayContext);
  1.1197 +                //If fastpath is disabled here then we need to compose properly this cycle
  1.1198 +                }
  1.1199 +            }
  1.1200 +        targetStreamAccessed = OWF_DisplayContext_InternalStreamAccessed(context->displayContext);
  1.1201 +        if (OWF_DisplayContext_FastpathEnabled(context->displayContext) && ( targetStreamAccessed || !WFC_Context_Active(context) ))
  1.1202 +            {   //Fastpath in non-autonomous composition just does a simple copy and post.
  1.1203 +            DPRINT(("== WFC_Context_DoCompose(context = %p) -   fastpath copy target", context));
  1.1204 +            if (WFC_Context_LockTargetForWriting(context))
  1.1205 +                {
  1.1206 +                OWFboolean copy;
  1.1207 +                if (screenRotation)
  1.1208 +                    {
  1.1209 +                    if (WFC_ROTATION_90 == context->rotation || WFC_ROTATION_270 == context->rotation)
  1.1210 +                        {
  1.1211 +                        owfSetStreamFlipState(context->stream, OWF_TRUE);
  1.1212 +                        }
  1.1213 +                    else
  1.1214 +                        {
  1.1215 +                        owfSetStreamFlipState(context->stream, OWF_FALSE);
  1.1216 +                        }
  1.1217 +                    }
  1.1218 +                copy=OWF_DisplayContext_CopyFastpathedStreamToTargetStream(context);
  1.1219 +                if (!WFC_Context_Active(context))
  1.1220 +                    {
  1.1221 +                    if (!copy)
  1.1222 +                        {
  1.1223 +                        DPRINT(("WFC_Context_Compose calls OWF_DisplayContext_DisableFastpath because !OWF_DisplayContext_CopyFastpathedStreamToTargetStream()"));
  1.1224 +                        OWF_DisplayContext_DisableFastpath(context->displayContext);
  1.1225 +                        //If fastpath is disabled here then we need to compose properly this cycle
  1.1226 +                        }
  1.1227 +                    }
  1.1228 +                else
  1.1229 +                    {
  1.1230 +                    copy=OWF_FALSE;
  1.1231 +                    }
  1.1232 +              
  1.1233 +                WFC_Context_UnlockTarget(context,copy);
  1.1234 +                }
  1.1235 +            else
  1.1236 +                {
  1.1237 +                //If non-autonomous, then the lock target is required.
  1.1238 +                OWF_ASSERT(WFC_Context_Active(context));
  1.1239 +                }
  1.1240 +
  1.1241 +            }
  1.1242 +        if (OWF_DisplayContext_FastpathEnabled(context->displayContext))
  1.1243 +            {
  1.1244 +            WFC_ELEMENT* topmostElement = NULL;
  1.1245 +            topmostElement = WFC_Scene_TopMostElement(context->committedScene);
  1.1246 +            owfSymElementNotifications(context, topmostElement);
  1.1247 +            }
  1.1248 +        }
  1.1249 +    if (!OWF_DisplayContext_FastpathEnabled(context->displayContext))
  1.1250 +        {
  1.1251 +        DPRINT(("== WFC_Context_DoCompose(context = %p) -   Composing", context));
  1.1252 +        WFC_Context_PrepareComposition(context);
  1.1253 +
  1.1254 +        scene = context->committedScene;
  1.1255 +        OWF_ASSERT(scene);
  1.1256 +    
  1.1257 +        for (node = scene->elements; NULL != node; node = node->next)
  1.1258 +            {
  1.1259 +            
  1.1260 +            WFC_ELEMENT*            element = NULL;
  1.1261 +            WFC_ELEMENT_STATE*      elementState = NULL;
  1.1262 +            element = ELEMENT(node->data);
  1.1263 +    
  1.1264 +            if (element->skipCompose)
  1.1265 +                {
  1.1266 +                 /* this element is somehow degraded, its source is missing or
  1.1267 +                  * something else; skip to next element */
  1.1268 +                DPRINT(("  *** Skipping element %d", element->handle));
  1.1269 +                continue;
  1.1270 +                }
  1.1271 +    
  1.1272 +            DPRINT(("  Composing element %d", element->handle));
  1.1273 +    
  1.1274 +            /* BeginComposition may fail e.g. if the element's destination
  1.1275 +             * rectangle is something bizarre, i.e. causes overflows or
  1.1276 +             * something.
  1.1277 +             */
  1.1278 +            if ((elementState=WFC_Pipeline_BeginComposition(context, element))!=NULL)
  1.1279 +                {
  1.1280 +                owfSymElementNotifications(context, element);
  1.1281 +                
  1.1282 +                WFC_Pipeline_ExecuteSourceConversionStage(context, elementState);
  1.1283 +                WFC_Pipeline_ExecuteCropStage(context, elementState);
  1.1284 +                WFC_Pipeline_ExecuteFlipStage(context, elementState);
  1.1285 +                WFC_Pipeline_ExecuteRotationStage(context, elementState);
  1.1286 +                WFC_Pipeline_ExecuteScalingStage(context, elementState);
  1.1287 +                WFC_Pipeline_ExecuteBlendingStage(context, elementState);
  1.1288 +                    
  1.1289 +                WFC_Pipeline_EndComposition(context, element,elementState);
  1.1290 +                }
  1.1291 +            }
  1.1292 +    
  1.1293 +        WFC_Context_FinishComposition(context);
  1.1294 +        DPRINT(("=== WFC_Context_DoCompose(context = %p) - Diplayed Composition", context));
  1.1295 +        }
  1.1296 +    
  1.1297 +    WFC_Scene_UnlockSourcesAndMasks(context->committedScene);
  1.1298 +    owfSymProcessAllNotifications(context);
  1.1299 +    OWF_Mutex_Unlock(&context->sceneMutex);
  1.1300 +    
  1.1301 +    OWF_Semaphore_Post(&context->compositionSemaphore);
  1.1302 +}
  1.1303 +
  1.1304 +/*---------------------------------------------------------------------------
  1.1305 + *  Activate/deactivate auto-composition on context
  1.1306 + *
  1.1307 + *  \param context Context
  1.1308 + *  \param act Auto-composition enable/disable
  1.1309 + *----------------------------------------------------------------------------*/
  1.1310 +OWF_API_CALL void
  1.1311 +WFC_Context_Activate(WFC_CONTEXT* context,
  1.1312 +                     WFCboolean act)
  1.1313 +{
  1.1314 +    OWF_ASSERT(context);
  1.1315 +
  1.1316 +    DPRINT(("WFC_Context_Activate: %s", (act) ? "activate" : "deactivate"));
  1.1317 +
  1.1318 +    if (act && !WFC_Context_Active(context))
  1.1319 +    {
  1.1320 +        DPRINT(("WFC_Context_Activate: WFC_CONTEXT_STATE_PASSIVE: activating"));
  1.1321 +        context->activationState = WFC_CONTEXT_STATE_ACTIVATING;
  1.1322 +
  1.1323 +        /* moved from composer loop - updates must be allowed
  1.1324 +         * immediately after activation
  1.1325 +         */
  1.1326 +        WFC_Device_EnableContentNotifications(context->device,
  1.1327 +                                              context,
  1.1328 +                                              WFC_TRUE);
  1.1329 +
  1.1330 +        OWF_Message_Send(&context->composerQueue,
  1.1331 +                         WFC_MESSAGE_ACTIVATE,
  1.1332 +                         0);
  1.1333 +    }
  1.1334 +    else if (!act && WFC_Context_Active(context))
  1.1335 +    {
  1.1336 +        DPRINT(("WFC_Context_Activate: WFC_CONTEXT_STATE_ACTIVE: deactivating"));
  1.1337 +        context->activationState = WFC_CONTEXT_STATE_DEACTIVATING;
  1.1338 +        OWF_Message_Send(&context->composerQueue,
  1.1339 +                         WFC_MESSAGE_DEACTIVATE,
  1.1340 +                         0);
  1.1341 +    }
  1.1342 +}
  1.1343 +
  1.1344 +/*---------------------------------------------------------------------------
  1.1345 + *
  1.1346 + *----------------------------------------------------------------------------*/
  1.1347 +OWF_API_CALL WFCboolean
  1.1348 +WFC_Context_InvokeComposition(WFC_DEVICE* device,
  1.1349 +                              WFC_CONTEXT* context,
  1.1350 +                              WFCboolean wait)
  1.1351 +{
  1.1352 +    WFCint              status = 0;
  1.1353 +
  1.1354 +    OWF_ASSERT(context);
  1.1355 +    OWF_ASSERT(device);
  1.1356 +
  1.1357 +    device = device; /* suppress the compiler warning */
  1.1358 +
  1.1359 +    status = OWF_Semaphore_TryWait(&context->compositionSemaphore);
  1.1360 +    if (status)
  1.1361 +    {
  1.1362 +        if (!wait)
  1.1363 +        {
  1.1364 +            /* busy; processing last request */
  1.1365 +            return WFC_FALSE;
  1.1366 +        }
  1.1367 +        /* wait previous frame composition to finish */
  1.1368 +        OWF_Semaphore_Wait(&context->compositionSemaphore);
  1.1369 +    }
  1.1370 +
  1.1371 +    /* compositionSemaphore is posted/signaled in WFC_Context_Compose()
  1.1372 +    after frame has been successfully composed */
  1.1373 +    OWF_Message_Send(&context->composerQueue, WFC_MESSAGE_COMPOSE, 0);
  1.1374 +
  1.1375 +    return WFC_TRUE;
  1.1376 +}
  1.1377 +
  1.1378 +/*---------------------------------------------------------------------------
  1.1379 + *
  1.1380 + *----------------------------------------------------------------------------*/
  1.1381 +OWF_API_CALL WFCErrorCode
  1.1382 +WFC_Context_InvokeCommit(WFC_DEVICE* device,
  1.1383 +                         WFC_CONTEXT* context,
  1.1384 +                         WFCboolean wait)
  1.1385 +{
  1.1386 +    WFCint              status = 0;
  1.1387 +
  1.1388 +    OWF_ASSERT(context);
  1.1389 +    OWF_ASSERT(device);
  1.1390 +
  1.1391 +    device = device; /* suppress the compiler warning */
  1.1392 +
  1.1393 +    /* first check if there're inconsistensies in the scene */
  1.1394 +    if (WFC_Scene_HasConflicts(context->workScene))
  1.1395 +    {
  1.1396 +        DPRINT(("WFC_Context_InvokeCommit: scene has inconsistensies"));
  1.1397 +        return WFC_ERROR_INCONSISTENCY;
  1.1398 +    }
  1.1399 +
  1.1400 +    /* then commit - always asynchronously */
  1.1401 +    status = OWF_Semaphore_TryWait(&context->commitSemaphore);
  1.1402 +    DPRINT(("COMMIT: Commit semaphore status = %d", status));
  1.1403 +    if (status)
  1.1404 +    {
  1.1405 +        if (!wait)
  1.1406 +        {
  1.1407 +            DPRINT(("COMMIT: Busy; exiting."));
  1.1408 +            /* busy; processing last commit */
  1.1409 +            return WFC_ERROR_BUSY;
  1.1410 +        }
  1.1411 +
  1.1412 +        DPRINT(("COMMIT: Waiting for previous commit to finish."));
  1.1413 +        /* wait previous commit to finish */
  1.1414 +        OWF_Semaphore_Wait(&context->commitSemaphore);
  1.1415 +    }
  1.1416 +
  1.1417 +    /* comitting scene attribute changes */
  1.1418 +    DPRINT(("COMMIT: Cloning scene attribute changes"));
  1.1419 +    OWF_AttributeList_Commit(&context->attributes,
  1.1420 +                             WFC_CONTEXT_TYPE,
  1.1421 +                             WFC_CONTEXT_BG_COLOR,SNAPSHOT_ATTR_VALUE_INDEX);
  1.1422 +
  1.1423 +    DPRINT(("COMMIT: Cloning scene"));
  1.1424 +    /* take snapshot of the current working copy - it will
  1.1425 +     * be the new committed scene */
  1.1426 +    context->snapshotScene = WFC_Scene_Clone(context->workScene);
  1.1427 +
  1.1428 +    DPRINT(("COMMIT: Sending commit request"));
  1.1429 +    /* invoke async commit */
  1.1430 +    OWF_Message_Send(&context->composerQueue, WFC_MESSAGE_COMMIT, 0);
  1.1431 +    return WFC_ERROR_NONE;
  1.1432 +}
  1.1433 +
  1.1434 +/*---------------------------------------------------------------------------
  1.1435 + *  \param context
  1.1436 + *  \param dpy
  1.1437 + *  \param sync
  1.1438 + *----------------------------------------------------------------------------*/
  1.1439 +OWF_API_CALL void
  1.1440 +WFC_Context_InsertFence(WFC_CONTEXT* context,
  1.1441 +                        WFCEGLDisplay dpy,
  1.1442 +                        WFCEGLSync sync)
  1.1443 +{
  1.1444 +    OWF_ASSERT(context);
  1.1445 +    OWF_ASSERT(sync);
  1.1446 +
  1.1447 +    DPRINT(("WFC_Context_InsertFence: Sending fence sync: 0x%08x", sync));
  1.1448 +    
  1.1449 +    OWF_Message_Send(&context->composerQueue, WFC_MESSAGE_FENCE_1_DISPLAY, (void*) dpy);
  1.1450 +    OWF_Message_Send(&context->composerQueue, WFC_MESSAGE_FENCE_2_SYNCOBJECT, sync);
  1.1451 +}
  1.1452 +
  1.1453 +/*---------------------------------------------------------------------------
  1.1454 + *  Insert element into context's scene
  1.1455 + *
  1.1456 + *  \param context
  1.1457 + *  \param element
  1.1458 + *  \param subordinate
  1.1459 + *
  1.1460 + *  \return WFCErrorCode
  1.1461 + *----------------------------------------------------------------------------*/
  1.1462 +OWF_API_CALL WFCErrorCode
  1.1463 +WFC_Context_InsertElement(WFC_CONTEXT* context,
  1.1464 +                          WFCElement element,
  1.1465 +                          WFCElement subordinate)
  1.1466 +{
  1.1467 +    WFC_ELEMENT*            object = NULL;
  1.1468 +    WFCErrorCode            result = WFC_ERROR_BAD_HANDLE;
  1.1469 +
  1.1470 +    OWF_ASSERT(context);
  1.1471 +
  1.1472 +    object = WFC_Device_FindElement(context->device, element);
  1.1473 +
  1.1474 +    if (NULL != object && CONTEXT(object->context) == context)
  1.1475 +    {
  1.1476 +        /* set the sharing flag as the element will be
  1.1477 +         * shared between the device and working copy scene.
  1.1478 +         * this is to tell the scene that it must not destroy
  1.1479 +         * this element.
  1.1480 +         */
  1.1481 +        object->shared = WFC_TRUE;
  1.1482 +        result = WFC_Scene_InsertElement(context->workScene,
  1.1483 +                                         object,
  1.1484 +                                         subordinate);
  1.1485 +
  1.1486 +        context->lowestElement = WFC_Scene_LowestElement(context->workScene);
  1.1487 +    }
  1.1488 +    return result;
  1.1489 +}
  1.1490 +
  1.1491 +/*---------------------------------------------------------------------------
  1.1492 + *  Remove element from context's scene
  1.1493 + *
  1.1494 + *  \param context
  1.1495 + *  \param element
  1.1496 + *
  1.1497 + *  \return WFCErrorCode
  1.1498 + *----------------------------------------------------------------------------*/
  1.1499 +OWF_API_CALL WFCErrorCode
  1.1500 +WFC_Context_RemoveElement(WFC_CONTEXT* context,
  1.1501 +                          WFCElement element)
  1.1502 +{
  1.1503 +    WFC_ELEMENT*            elemento = NULL;
  1.1504 +
  1.1505 +    OWF_ASSERT(context);
  1.1506 +
  1.1507 +    elemento = WFC_Context_FindElement(context, element);
  1.1508 +
  1.1509 +    if (elemento)
  1.1510 +    {
  1.1511 +        WFC_Scene_RemoveElement(context->workScene, element);
  1.1512 +        /* the element is no longer shared, as it only resides
  1.1513 +         * in device from this point on
  1.1514 +         */
  1.1515 +        elemento->shared = WFC_FALSE;
  1.1516 +        context->lowestElement = WFC_Scene_LowestElement(context->workScene);
  1.1517 +    }
  1.1518 +
  1.1519 +    return WFC_ERROR_NONE;
  1.1520 +}
  1.1521 +
  1.1522 +/*!
  1.1523 + *  \brief IncreaseClientElementCount
  1.1524 + *
  1.1525 + *  \param context
  1.1526 + *  \return positive increased current element count; negative or zero indicating the count can't be increased
  1.1527 + */
  1.1528 +OWF_API_CALL WFCint
  1.1529 +WFC_Context_IncreaseClientElementCount(WFC_CONTEXT* context)
  1.1530 +    {
  1.1531 +    /* This implementation simply caps the number of elements allocated to the client
  1.1532 +     * to 1/3 of the total elements.
  1.1533 +     * A cleverer strategy would allow the client to create more elements 
  1.1534 +     * so long as the number added to the scene *2 did not exceed the spare pool size.
  1.1535 +     * These failure points are also a good place to consider increasing the pool size
  1.1536 +     */
  1.1537 +    if (context->clientElementCount>=MAX_ELEMENTS)
  1.1538 +        {
  1.1539 +        return -context->clientElementCount;
  1.1540 +        }
  1.1541 +    else
  1.1542 +        {
  1.1543 +        return ++context->clientElementCount;
  1.1544 +        }
  1.1545 +    }
  1.1546 +
  1.1547 +/*!
  1.1548 + *  \brief DecreaseClientElementCount
  1.1549 + *
  1.1550 + *  \param context
  1.1551 + *  \return positive or zero decreased current element count; negative indicating the count can't be decreased - p0robably already zero
  1.1552 + */
  1.1553 +OWF_API_CALL WFCint
  1.1554 +WFC_Context_DecreaseClientElementCount(WFC_CONTEXT* context)
  1.1555 +    {
  1.1556 +    /* note that a negative return indicates that decrements are not matched with increments */ 
  1.1557 +    return --context->clientElementCount;
  1.1558 +    }
  1.1559 +
  1.1560 +/*---------------------------------------------------------------------------
  1.1561 + *
  1.1562 + *----------------------------------------------------------------------------*/
  1.1563 +OWF_API_CALL WFCErrorCode
  1.1564 +WFC_Context_GetElementAbove(WFC_CONTEXT* context,
  1.1565 +                            WFCElement element,
  1.1566 +                            WFCElement* result)
  1.1567 +{
  1.1568 +    WFC_ELEMENT*            object = NULL;
  1.1569 +    WFCErrorCode            error = WFC_ERROR_ILLEGAL_ARGUMENT;
  1.1570 +
  1.1571 +    OWF_ASSERT(context);
  1.1572 +    OWF_ASSERT(result);
  1.1573 +
  1.1574 +    object = WFC_Context_FindElement(context, element);
  1.1575 +
  1.1576 +    if (object)
  1.1577 +    {
  1.1578 +        WFCElement          temp;
  1.1579 +
  1.1580 +        temp = WFC_Scene_GetNeighbourElement(context->workScene, element, 1);
  1.1581 +        *result = temp;
  1.1582 +        error = WFC_ERROR_NONE;
  1.1583 +    }
  1.1584 +    return error;
  1.1585 +}
  1.1586 +
  1.1587 +/*---------------------------------------------------------------------------
  1.1588 + *
  1.1589 + *----------------------------------------------------------------------------*/
  1.1590 +OWF_API_CALL WFCErrorCode
  1.1591 +WFC_Context_GetElementBelow(WFC_CONTEXT* context,
  1.1592 +                            WFCElement element,
  1.1593 +                            WFCElement* result)
  1.1594 +{
  1.1595 +    WFC_ELEMENT*            object = NULL;
  1.1596 +    WFCErrorCode            error = WFC_ERROR_ILLEGAL_ARGUMENT;
  1.1597 +
  1.1598 +    OWF_ASSERT(context);
  1.1599 +    OWF_ASSERT(result);
  1.1600 +
  1.1601 +    object = WFC_Context_FindElement(context, element);
  1.1602 +    if (object)
  1.1603 +    {
  1.1604 +        WFCElement          temp;
  1.1605 +
  1.1606 +        temp = WFC_Scene_GetNeighbourElement(context->workScene, element, -1);
  1.1607 +        *result = temp;
  1.1608 +        error = WFC_ERROR_NONE;
  1.1609 +    }
  1.1610 +    return error;
  1.1611 +}
  1.1612 +
  1.1613 +/*---------------------------------------------------------------------------
  1.1614 + *
  1.1615 + *----------------------------------------------------------------------------*/
  1.1616 +OWF_API_CALL WFCErrorCode
  1.1617 +WFC_Context_GetAttribi(WFC_CONTEXT* context,
  1.1618 +                       WFCContextAttrib attrib,
  1.1619 +                       WFCint* value)
  1.1620 +{
  1.1621 +    WFCint                  temp = 0;
  1.1622 +    OWF_ATTRIBUTE_LIST_STATUS   err;
  1.1623 +    WFCErrorCode            result = WFC_ERROR_NONE;
  1.1624 +
  1.1625 +    OWF_ASSERT(context);
  1.1626 +    OWF_ASSERT(value);
  1.1627 +
  1.1628 +    temp    = OWF_Attribute_GetValuei(&context->attributes, attrib);
  1.1629 +    err     = OWF_AttributeList_GetError(&context->attributes);
  1.1630 +
  1.1631 +    if (err != ATTR_ERROR_NONE)
  1.1632 +    {
  1.1633 +        result = WFC_ERROR_BAD_ATTRIBUTE;
  1.1634 +    }
  1.1635 +    else
  1.1636 +    {
  1.1637 +        *value = temp;
  1.1638 +    }
  1.1639 +    return result;
  1.1640 +}
  1.1641 +
  1.1642 +/*---------------------------------------------------------------------------
  1.1643 + *
  1.1644 + *----------------------------------------------------------------------------*/
  1.1645 +OWF_API_CALL WFCErrorCode
  1.1646 +WFC_Context_SetAttribi(WFC_CONTEXT* context,
  1.1647 +                       WFCContextAttrib attrib,
  1.1648 +                       WFCint value)
  1.1649 +{
  1.1650 +    WFCErrorCode                result = WFC_ERROR_NONE;
  1.1651 +
  1.1652 +    OWF_ASSERT(context);
  1.1653 +
  1.1654 +    /* check value */
  1.1655 +    switch (attrib)
  1.1656 +    {
  1.1657 +        case WFC_CONTEXT_BG_COLOR:
  1.1658 +        {
  1.1659 +            OWFint              alpha;
  1.1660 +
  1.1661 +            /*
  1.1662 +             *  Color format is RGBA NOT RGBA.
  1.1663 +             */
  1.1664 +            alpha = value & 0xFF;
  1.1665 +
  1.1666 +            if (WFC_CONTEXT_TYPE_ON_SCREEN == context->type)
  1.1667 +            {
  1.1668 +                /* the only allowed value for on-screen context
  1.1669 +                 * background alpha is 255 */
  1.1670 +                if (alpha != 255)
  1.1671 +                {
  1.1672 +                    result = WFC_ERROR_ILLEGAL_ARGUMENT;
  1.1673 +                }
  1.1674 +            }
  1.1675 +            break;
  1.1676 +        }
  1.1677 +
  1.1678 +        case WFC_CONTEXT_ROTATION:
  1.1679 +        {
  1.1680 +            if (!(WFC_ROTATION_0 == value ||
  1.1681 +                  WFC_ROTATION_90 == value ||
  1.1682 +                  WFC_ROTATION_180 == value ||
  1.1683 +                  WFC_ROTATION_270 == value))
  1.1684 +            {
  1.1685 +               result = WFC_ERROR_ILLEGAL_ARGUMENT;
  1.1686 +            }
  1.1687 +            else
  1.1688 +            {
  1.1689 +                OWF_DisplayContext_ResetFastpathCheck(context->displayContext);
  1.1690 +            }
  1.1691 +            break;
  1.1692 +        }
  1.1693 +
  1.1694 +        case WFC_CONTEXT_TYPE:
  1.1695 +        case WFC_CONTEXT_TARGET_HEIGHT:
  1.1696 +        case WFC_CONTEXT_TARGET_WIDTH:
  1.1697 +        case WFC_CONTEXT_LOWEST_ELEMENT:
  1.1698 +        {
  1.1699 +            result = WFC_ERROR_BAD_ATTRIBUTE;
  1.1700 +            break;
  1.1701 +        }
  1.1702 +
  1.1703 +        case WFC_CONTEXT_FORCE_32BIT:
  1.1704 +        default:
  1.1705 +        {
  1.1706 +            result = WFC_ERROR_BAD_ATTRIBUTE;
  1.1707 +            break;
  1.1708 +        }
  1.1709 +    }
  1.1710 +
  1.1711 +    if (WFC_ERROR_NONE == result)
  1.1712 +    {
  1.1713 +        OWF_ATTRIBUTE_LIST_STATUS   error;
  1.1714 +
  1.1715 +        /* try changing the value */
  1.1716 +        OWF_Attribute_SetValuei(&context->attributes, attrib, value);
  1.1717 +        error = OWF_AttributeList_GetError(&context->attributes);
  1.1718 +
  1.1719 +        /* transform errors */
  1.1720 +        switch (error) {
  1.1721 +            case ATTR_ERROR_ACCESS_DENIED:
  1.1722 +            case ATTR_ERROR_INVALID_ATTRIBUTE:
  1.1723 +            {
  1.1724 +                result = WFC_ERROR_BAD_ATTRIBUTE;
  1.1725 +                break;
  1.1726 +            }
  1.1727 +            default:
  1.1728 +            {
  1.1729 +                break;
  1.1730 +            }
  1.1731 +        }
  1.1732 +    }
  1.1733 +
  1.1734 +    return result;
  1.1735 +}
  1.1736 +
  1.1737 +/*---------------------------------------------------------------------------
  1.1738 + *
  1.1739 + *----------------------------------------------------------------------------*/
  1.1740 +OWF_API_CALL WFCErrorCode
  1.1741 +WFC_Context_GetAttribfv(WFC_CONTEXT* context,
  1.1742 +                        WFCContextAttrib attrib,
  1.1743 +                        WFCint count,
  1.1744 +                        WFCfloat* values)
  1.1745 +{
  1.1746 +    WFCErrorCode            result = WFC_ERROR_NONE;
  1.1747 +
  1.1748 +    OWF_ASSERT(context);
  1.1749 +    OWF_ASSERT(values);
  1.1750 +
  1.1751 +    switch (attrib)
  1.1752 +    {
  1.1753 +        case WFC_CONTEXT_BG_COLOR:
  1.1754 +        {
  1.1755 +            if (4 != count)
  1.1756 +            {
  1.1757 +                result = WFC_ERROR_ILLEGAL_ARGUMENT;
  1.1758 +            }
  1.1759 +            else
  1.1760 +            {
  1.1761 +                OWFuint32      color;
  1.1762 +                OWF_ATTRIBUTE_LIST_STATUS   err;
  1.1763 +
  1.1764 +                color = OWF_Attribute_GetValuei(&context->attributes, attrib);
  1.1765 +                err = OWF_AttributeList_GetError(&context->attributes);
  1.1766 +
  1.1767 +                if (err != ATTR_ERROR_NONE)
  1.1768 +                {
  1.1769 +                    result = WFC_ERROR_BAD_ATTRIBUTE;
  1.1770 +                    break;
  1.1771 +                }
  1.1772 +
  1.1773 +                /* extract color channels and convert to float */
  1.1774 +                values[0] = (WFCfloat) (color >> 24) /
  1.1775 +                            (WFCfloat) OWF_BYTE_MAX_VALUE;
  1.1776 +                values[1] = (WFCfloat) ((color >> 16) & 0xFF) /
  1.1777 +                            (WFCfloat) OWF_BYTE_MAX_VALUE;
  1.1778 +                values[2] = (WFCfloat) ((color >> 8) & 0xFF) /
  1.1779 +                            (WFCfloat) OWF_BYTE_MAX_VALUE;
  1.1780 +                values[3] = (WFCfloat) (color & 0xFF) /
  1.1781 +                            (WFCfloat) OWF_BYTE_MAX_VALUE;
  1.1782 +            }
  1.1783 +            break;
  1.1784 +        }
  1.1785 +
  1.1786 +        default:
  1.1787 +        {
  1.1788 +            result = WFC_ERROR_BAD_ATTRIBUTE;
  1.1789 +            break;
  1.1790 +        }
  1.1791 +    }
  1.1792 +
  1.1793 +    return result;
  1.1794 +}
  1.1795 +
  1.1796 +/*---------------------------------------------------------------------------
  1.1797 + *
  1.1798 + *----------------------------------------------------------------------------*/
  1.1799 +OWF_API_CALL WFCErrorCode
  1.1800 +WFC_Context_SetAttribfv(WFC_CONTEXT* context,
  1.1801 +                        WFCContextAttrib attrib,
  1.1802 +                        WFCint count,
  1.1803 +                        const WFCfloat* values)
  1.1804 +{
  1.1805 +    WFCErrorCode            result = WFC_ERROR_NONE;
  1.1806 +
  1.1807 +    OWF_ASSERT(context);
  1.1808 +    OWF_ASSERT(values);
  1.1809 +
  1.1810 +    switch (attrib)
  1.1811 +    {
  1.1812 +        case WFC_CONTEXT_BG_COLOR:
  1.1813 +        {
  1.1814 +            if (4 != count)
  1.1815 +            {
  1.1816 +                result = WFC_ERROR_ILLEGAL_ARGUMENT;
  1.1817 +            }
  1.1818 +            else
  1.1819 +            {
  1.1820 +                OWFuint32    color;
  1.1821 +
  1.1822 +                /* Every color component value must fall within range [0, 1] */
  1.1823 +                if (INRANGE(values[0], 0.0f, 1.0f) &&
  1.1824 +                    INRANGE(values[1], 0.0f, 1.0f) &&
  1.1825 +                    INRANGE(values[2], 0.0f, 1.0f) &&
  1.1826 +                    INRANGE(values[3], 0.0f, 1.0f))
  1.1827 +                {
  1.1828 +                    color = (((OWFuint32) floor(values[0] * 255)) << 24) |
  1.1829 +                            (((OWFuint32) floor(values[1] * 255)) << 16) |
  1.1830 +                            (((OWFuint32) floor(values[2] * 255)) << 8) |
  1.1831 +                            ((OWFuint32) floor(values[3] * 255));
  1.1832 +
  1.1833 +                    /* delegate to integer accessor - it'll check the
  1.1834 +                     * the rest of the value and update it eventually  */
  1.1835 +                    result = WFC_Context_SetAttribi(context, attrib, color);
  1.1836 +                }
  1.1837 +                else
  1.1838 +                {
  1.1839 +                    result = WFC_ERROR_ILLEGAL_ARGUMENT;
  1.1840 +                }
  1.1841 +            }
  1.1842 +            break;
  1.1843 +        }
  1.1844 +
  1.1845 +        default:
  1.1846 +        {
  1.1847 +            result = WFC_ERROR_BAD_ATTRIBUTE;
  1.1848 +            break;
  1.1849 +        }
  1.1850 +    }
  1.1851 +
  1.1852 +    return result;
  1.1853 +}
  1.1854 +
  1.1855 +
  1.1856 +static void
  1.1857 +WFC_Context_AutoComposer(WFC_CONTEXT* context)
  1.1858 +{
  1.1859 +    OWF_Mutex_Lock(&context->updateFlagMutex);
  1.1860 +    if (context->sourceUpdateCount > 0)
  1.1861 +    {
  1.1862 +        DPRINT(("WFC_Context_ComposerThread: %d sources were updated, "
  1.1863 +                "invoking composition\n",
  1.1864 +                context->sourceUpdateCount));
  1.1865 +
  1.1866 +        /* reset update counter */
  1.1867 +
  1.1868 +        OWF_Mutex_Unlock(&context->updateFlagMutex);
  1.1869 +        WFC_Context_DoCompose(context);
  1.1870 +    }
  1.1871 +    else
  1.1872 +    {
  1.1873 +        OWF_Mutex_Unlock(&context->updateFlagMutex);
  1.1874 +    }
  1.1875 +}
  1.1876 +
  1.1877 +/*---------------------------------------------------------------------------
  1.1878 + *
  1.1879 + *----------------------------------------------------------------------------*/
  1.1880 +static void*
  1.1881 +WFC_Context_ComposerThread(void* data)
  1.1882 +{
  1.1883 +    WFC_CONTEXT*            context = (WFC_CONTEXT*) data;
  1.1884 +    OWFboolean screenCreated = OWF_TRUE;
  1.1885 +    OWF_MESSAGE             msg;
  1.1886 +
  1.1887 +
  1.1888 +    OWF_ASSERT(context);
  1.1889 +    DPRINT(("WFC_Context_ComposerThread starting"));
  1.1890 +
  1.1891 +    memset(&msg, 0, sizeof(OWF_MESSAGE));
  1.1892 +    
  1.1893 +    if (context->type == WFC_CONTEXT_TYPE_ON_SCREEN)
  1.1894 +        {
  1.1895 +        screenCreated = OWF_Screen_Create(context->screenNumber, context->displayContext);
  1.1896 +        }
  1.1897 +    
  1.1898 +    OWF_ComposerThread_Rendezvous(context->displayContext);
  1.1899 +
  1.1900 +    OWF_ComposerThread_RendezvousDestroy(context->displayContext);
  1.1901 +    
  1.1902 +    while (context->device && msg.id != WFC_MESSAGE_QUIT && screenCreated)
  1.1903 +    {
  1.1904 +        OWFint              err = -1;
  1.1905 +
  1.1906 +        if (WFC_CONTEXT_STATE_ACTIVE == context->activationState)
  1.1907 +        {
  1.1908 +            err = OWF_Message_Wait(&context->composerQueue,
  1.1909 +                                   &msg,
  1.1910 +                                   AUTO_COMPOSE_DELAY);
  1.1911 +
  1.1912 +            WFC_Context_AutoComposer(context);
  1.1913 +        }
  1.1914 +        else
  1.1915 +        {
  1.1916 +            DPRINT(("  ComposerThread waiting for message"));
  1.1917 +            err = OWF_Message_Wait(&context->composerQueue, &msg, WAIT_FOREVER);
  1.1918 +        }
  1.1919 +
  1.1920 +        if (0 == err)
  1.1921 +        {
  1.1922 +            switch (msg.id)
  1.1923 +            {
  1.1924 +                case WFC_MESSAGE_ACTIVATE:
  1.1925 +                {
  1.1926 +                    DPRINT(("****** ENABLING AUTO-COMPOSITION ******"));
  1.1927 +                    context->activationState = WFC_CONTEXT_STATE_ACTIVE;
  1.1928 +                    break;
  1.1929 +                }
  1.1930 +
  1.1931 +                case WFC_MESSAGE_DEACTIVATE:
  1.1932 +                {
  1.1933 +                    /* cancel possible countdown so that update won't occur
  1.1934 +                     * after deactivation */
  1.1935 +                    DPRINT(("****** DISABLING AUTO-COMPOSITION ******"));
  1.1936 +                    WFC_Device_EnableContentNotifications(context->device,
  1.1937 +                                                          context,
  1.1938 +                                                          WFC_FALSE);
  1.1939 +                    context->activationState = WFC_CONTEXT_STATE_PASSIVE;
  1.1940 +                    if (OWF_DisplayContext_FastpathEnabled(context->displayContext))
  1.1941 +                        {
  1.1942 +                        DPRINT(("COMMIT: Invoking fastpath recomposition after deactivate"));
  1.1943 +                        WFC_Context_DoCompose(context);
  1.1944 +                        }
  1.1945 +                    break;
  1.1946 +                }
  1.1947 +
  1.1948 +                case WFC_MESSAGE_COMMIT:
  1.1949 +                {
  1.1950 +                    DPRINT(("****** COMMITTING SCENE CHANGES ******"));
  1.1951 +
  1.1952 +                    DPRINT(("COMMIT: Invoking DoCommit"));
  1.1953 +                    WFC_Context_DoCommit(context);
  1.1954 +                    OWF_DisplayContext_ResetFastpathCheck(context->displayContext);
  1.1955 +                    if (!WFC_Context_Active(context))
  1.1956 +                    {
  1.1957 +                        DPRINT(("COMMIT: Context is inactive, composition "
  1.1958 +                                "not needed.", context->handle));
  1.1959 +                        break;
  1.1960 +                    }
  1.1961 +                    else
  1.1962 +                    {
  1.1963 +                        /* context is active; compose immediately after
  1.1964 +                         * commit has completed */
  1.1965 +
  1.1966 +                        DPRINT(("COMMIT: Invoking composition after commit"));
  1.1967 +                    }
  1.1968 +                    /* FLOW THROUGH */
  1.1969 +                }
  1.1970 +
  1.1971 +                case WFC_MESSAGE_COMPOSE:
  1.1972 +                {
  1.1973 +                    DPRINT(("****** COMPOSING SCENE ******"));
  1.1974 +
  1.1975 +                    WFC_Context_DoCompose(context);
  1.1976 +
  1.1977 +                    break;
  1.1978 +                }
  1.1979 +                
  1.1980 +                case WFC_MESSAGE_FENCE_1_DISPLAY:
  1.1981 +                {
  1.1982 +                    DPRINT(("****** STORING EGLDISPLAY (%p) ******", msg.data));
  1.1983 +
  1.1984 +                    context->nextSyncObjectDisplay = (WFCEGLDisplay)msg.data;
  1.1985 +                    break;
  1.1986 +                }
  1.1987 +
  1.1988 +                case WFC_MESSAGE_FENCE_2_SYNCOBJECT:
  1.1989 +                {
  1.1990 +                    DPRINT(("****** BREAKING FENCE (%p) ******", msg.data));
  1.1991 +                    OWF_ASSERT(context->eglPrivateSignalSync);
  1.1992 +
  1.1993 +                    context->eglPrivateSignalSync(context->nextSyncObjectDisplay,
  1.1994 +                                     (WFCEGLSync) msg.data,
  1.1995 +                                     EGL_SIGNALED_KHR);
  1.1996 +                    break;
  1.1997 +                }
  1.1998 +            }
  1.1999 +        }
  1.2000 +    }
  1.2001 +
  1.2002 +    if (context->type == WFC_CONTEXT_TYPE_ON_SCREEN)
  1.2003 +        {
  1.2004 +        OWF_Screen_Destroy(context->displayContext);
  1.2005 +        }
  1.2006 +    /* Release any use of EGL by this thread. */
  1.2007 +    eglReleaseThread();
  1.2008 +
  1.2009 +    DPRINT(("WFC_Context_ComposerThread terminating"));
  1.2010 +    OWF_Thread_Exit(NULL);
  1.2011 +    return NULL;
  1.2012 +}
  1.2013 +
  1.2014 +/*---------------------------------------------------------------------------
  1.2015 + *
  1.2016 + *----------------------------------------------------------------------------*/
  1.2017 +OWF_API_CALL WFCboolean
  1.2018 +WFC_Context_Active(WFC_CONTEXT* context)
  1.2019 +{
  1.2020 +    OWF_ASSERT(context);
  1.2021 +
  1.2022 +    return (WFC_CONTEXT_STATE_ACTIVE == context->activationState ||
  1.2023 +           WFC_CONTEXT_STATE_ACTIVATING == context->activationState) ? WFC_TRUE : WFC_FALSE;
  1.2024 +}
  1.2025 +
  1.2026 +#ifdef __cplusplus
  1.2027 +}
  1.2028 +#endif
  1.2029 +