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