os/graphics/graphicscomposition/openwfcompositionengine/composition/src/wfcimageprovider.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* Copyright (c) 2009 The Khronos Group Inc.
     2  * Portions copyright (c) 2009-2010  Nokia Corporation and/or its subsidiary(-ies)
     3  *
     4  * Permission is hereby granted, free of charge, to any person obtaining a
     5  * copy of this software and/or associated documentation files (the
     6  * "Materials"), to deal in the Materials without restriction, including
     7  * without limitation the rights to use, copy, modify, merge, publish,
     8  * distribute, sublicense, and/or sell copies of the Materials, and to
     9  * permit persons to whom the Materials are furnished to do so, subject to
    10  * the following conditions:
    11  *
    12  * The above copyright notice and this permission notice shall be included
    13  * in all copies or substantial portions of the Materials.
    14  *
    15  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    21  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
    22  */
    23 /*! \ingroup wfc
    24  *  \file wfcimageprovider.c
    25  *
    26  *  \brief SI image providers
    27  */
    28 
    29 #include <stdio.h>
    30 #include <stdlib.h>
    31 
    32 #include "wfcimageprovider.h"
    33 #include "wfcdevice.h"
    34 #include "wfccontext.h"
    35 #include "wfcstructs.h"
    36 
    37 #include "owfimage.h"
    38 #include "owfarray.h"
    39 #include "owfmemory.h"
    40 #include "owfobject.h"
    41 #include "owfnativestream.h"
    42 #include "owfdebug.h"
    43 
    44 #ifdef __cplusplus
    45 extern "C"
    46 {
    47 #endif
    48 
    49 #define FIRST_IMAGEPROVIDER_HANDLE   4000
    50 
    51 static WFCint               nextImageProviderHandle =
    52                             FIRST_IMAGEPROVIDER_HANDLE;
    53 
    54 OWF_API_CALL void
    55 WFC_IMAGE_PROVIDER_Ctor(void* self)
    56 {
    57     WFC_IMAGE_PROVIDER*     ip;
    58 
    59     ENTER(WFC_IMAGE_PROVIDER_Ctor);
    60 
    61     ip = IMAGE_PROVIDER(self);
    62     
    63     ip->lockedStream.image=NULL;
    64     ip->lockedStream.lockCount=0;
    65     
    66     LEAVE(WFC_IMAGE_PROVIDER_Dtor);
    67 }
    68 
    69 OWF_API_CALL void
    70 WFC_IMAGE_PROVIDER_Dtor(void* self)
    71 {
    72 
    73     WFC_IMAGE_PROVIDER*     ip;
    74 
    75     ENTER(WFC_IMAGE_PROVIDER_Dtor);
    76 
    77     ip = IMAGE_PROVIDER(self);
    78 
    79     DPRINT(("ptr=%p, handle=%d", ip, ip->handle));
    80     owfNativeStreamDestroy(ip->streamHandle);
    81     DESTROY(ip->owner);
    82     if (ip->lockedStream.image)
    83         {
    84         if (ip->lockedStream.lockCount)
    85             {   /* belts and braces: unlock the read buffer when image provider is destroyed */
    86             DPRINT(("Native stream buffer still locked when Image Provider destroyed ptr=%p, handle=%d", ip, ip->handle));
    87             owfNativeStreamReleaseReadBuffer(ip->streamHandle, ip->lockedStream.buffer);
    88             }
    89         OWF_Image_Destroy(ip->lockedStream.image);
    90         }
    91     LEAVE(WFC_IMAGE_PROVIDER_Dtor);
    92 }
    93 
    94 static WFC_IMAGE_PROVIDER*
    95 WFC_ImageProvider_DoCreate(void* owner, /*WFC_CONTEXT* context,*/
    96                            OWFNativeStreamType stream,
    97                            WFC_IMAGE_PROVIDER_TYPE type)
    98 {
    99     WFC_IMAGE_PROVIDER*         object;
   100 
   101     ENTER(WFC_ImageProvider_DoCreate);
   102 
   103     if (!stream)
   104     {
   105         return NULL;
   106     }
   107     object = CREATE(WFC_IMAGE_PROVIDER);
   108 
   109     if (!object)
   110     {
   111         return NULL;
   112     }
   113 
   114     owfNativeStreamAddReference(stream);
   115     object->streamHandle = stream;
   116     object->type            = type;
   117     object->contentUpdated  = WFC_FALSE;
   118 
   119     WFC_ImageProvider_LockForReading(object);
   120     if (object->lockedStream.image==NULL || object->lockedStream.image->data==NULL)
   121         {
   122         owfNativeStreamDestroy(stream);
   123         DESTROY(object);
   124         return NULL;
   125         }
   126     WFC_ImageProvider_Unlock(object);
   127     ADDREF(object->owner, owner);
   128 
   129     LEAVE(WFC_ImageProvider_DoCreate);
   130     return object;
   131 }
   132 
   133 OWF_API_CALL WFC_IMAGE_PROVIDER*
   134 WFC_ImageProvider_Create(void* owner, /*WFC_CONTEXT* context,*/
   135                          OWFNativeStreamType stream,
   136                          WFC_IMAGE_PROVIDER_TYPE type)
   137 {
   138     WFC_IMAGE_PROVIDER*               object;
   139 
   140     ENTER(WFC_ImageProvider_Create);
   141 
   142     object = WFC_ImageProvider_DoCreate(owner/*context*/, stream, type);
   143 
   144     if (object)
   145     {
   146         object->handle = nextImageProviderHandle++;
   147         DPRINT(("WFC_ImageProvider_Create: attaching image provider %d to "
   148                 "stream %p",
   149                object->handle, object->streamHandle));
   150     }
   151 
   152     LEAVE(WFC_ImageProvider_Create);
   153     return object;
   154 }
   155 
   156 OWF_API_CALL void
   157 WFC_ImageProvider_LockForReading(WFC_IMAGE_PROVIDER* provider)
   158 {
   159     void*                   pixels;
   160     OWFint                  width, height, pixelSize = 0;
   161     OWF_IMAGE_FORMAT        imgf;
   162     OWFint                  stride;
   163 
   164     if (!provider) {
   165         DPRINT(("WFC_ImageProvider_LockForReading: provider = NULL"));
   166         return;
   167     }
   168     OWF_ASSERT(provider->streamHandle);
   169 
   170      DPRINT(("stream = %p", provider->streamHandle));
   171 
   172      if (!provider->lockedStream.lockCount)
   173      {
   174          DPRINT(("About to acquire & lock a read buffer"));
   175          /* acquire buffer */
   176          provider->lockedStream.buffer = owfNativeStreamAcquireReadBuffer(provider->streamHandle);
   177          DPRINT(("  Acquired read buffer stream=%p, buffer=%d",
   178                  provider->streamHandle, provider->lockedStream.buffer));
   179 
   180          /* Bind source image to pixel buffer */
   181          pixels = owfNativeStreamGetBufferPtr(provider->streamHandle, provider->lockedStream.buffer);
   182          if (provider->lockedStream.image)
   183              {
   184              OWF_Image_SetPixelBuffer(provider->lockedStream.image,pixels);
   185              }
   186          else
   187              {
   188              owfNativeStreamGetHeader(provider->streamHandle,
   189                                       &width, &height,
   190                                       &stride, &imgf, &pixelSize);
   191              provider->lockedStream.image = OWF_Image_Create(width, height, &imgf, pixels, stride);
   192              }
   193      }
   194 
   195      ++provider->lockedStream.lockCount;
   196      DPRINT(("lock count = %d", provider->lockedStream.lockCount));
   197 
   198 }
   199 
   200 OWF_API_CALL void
   201 WFC_ImageProvider_Unlock(WFC_IMAGE_PROVIDER* provider)
   202 {
   203     if (!provider) {
   204         DPRINT(("WFC_ImageProvider_Unlock: provider = NULL"));
   205         return;
   206     }
   207 
   208     if (provider->lockedStream.lockCount > 0)
   209     {
   210         --provider->lockedStream.lockCount;
   211         DPRINT(("lock count = %d", provider->lockedStream.lockCount));
   212 
   213         if (!provider->lockedStream.lockCount)
   214         {
   215             DPRINT(("  Releasing read buffer provider=%p, buffer=%d",
   216                     provider->handle, provider->lockedStream.buffer));
   217             owfNativeStreamReleaseReadBuffer(provider->streamHandle, provider->lockedStream.buffer);
   218         }
   219     }
   220 }
   221 
   222 
   223 
   224 #ifdef __cplusplus
   225 }
   226 #endif
   227