os/graphics/graphicscomposition/openwfcompositionengine/composition/src/wfcimageprovider.c
First public contribution.
1 /* Copyright (c) 2009 The Khronos Group Inc.
2 * Portions copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies)
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:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Materials.
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.
24 * \file wfcimageprovider.c
26 * \brief SI image providers
32 #include "wfcimageprovider.h"
33 #include "wfcdevice.h"
34 #include "wfccontext.h"
35 #include "wfcstructs.h"
39 #include "owfmemory.h"
40 #include "owfobject.h"
41 #include "owfnativestream.h"
49 #define FIRST_IMAGEPROVIDER_HANDLE 4000
51 static WFCint nextImageProviderHandle =
52 FIRST_IMAGEPROVIDER_HANDLE;
55 WFC_IMAGE_PROVIDER_Ctor(void* self)
57 WFC_IMAGE_PROVIDER* ip;
59 ENTER(WFC_IMAGE_PROVIDER_Ctor);
61 ip = IMAGE_PROVIDER(self);
63 ip->lockedStream.image=NULL;
64 ip->lockedStream.lockCount=0;
66 LEAVE(WFC_IMAGE_PROVIDER_Dtor);
70 WFC_IMAGE_PROVIDER_Dtor(void* self)
73 WFC_IMAGE_PROVIDER* ip;
75 ENTER(WFC_IMAGE_PROVIDER_Dtor);
77 ip = IMAGE_PROVIDER(self);
79 DPRINT(("ptr=%p, handle=%d", ip, ip->handle));
80 owfNativeStreamDestroy(ip->streamHandle);
82 if (ip->lockedStream.image)
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);
89 OWF_Image_Destroy(ip->lockedStream.image);
91 LEAVE(WFC_IMAGE_PROVIDER_Dtor);
94 static WFC_IMAGE_PROVIDER*
95 WFC_ImageProvider_DoCreate(void* owner, /*WFC_CONTEXT* context,*/
96 OWFNativeStreamType stream,
97 WFC_IMAGE_PROVIDER_TYPE type)
99 WFC_IMAGE_PROVIDER* object;
101 ENTER(WFC_ImageProvider_DoCreate);
107 object = CREATE(WFC_IMAGE_PROVIDER);
114 owfNativeStreamAddReference(stream);
115 object->streamHandle = stream;
117 object->contentUpdated = WFC_FALSE;
119 WFC_ImageProvider_LockForReading(object);
120 if (object->lockedStream.image==NULL || object->lockedStream.image->data==NULL)
122 owfNativeStreamDestroy(stream);
126 WFC_ImageProvider_Unlock(object);
127 ADDREF(object->owner, owner);
129 LEAVE(WFC_ImageProvider_DoCreate);
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)
138 WFC_IMAGE_PROVIDER* object;
140 ENTER(WFC_ImageProvider_Create);
142 object = WFC_ImageProvider_DoCreate(owner/*context*/, stream, type);
146 object->handle = nextImageProviderHandle++;
147 DPRINT(("WFC_ImageProvider_Create: attaching image provider %d to "
149 object->handle, object->streamHandle));
152 LEAVE(WFC_ImageProvider_Create);
157 WFC_ImageProvider_LockForReading(WFC_IMAGE_PROVIDER* provider)
160 OWFint width, height, pixelSize = 0;
161 OWF_IMAGE_FORMAT imgf;
165 DPRINT(("WFC_ImageProvider_LockForReading: provider = NULL"));
168 OWF_ASSERT(provider->streamHandle);
170 DPRINT(("stream = %p", provider->streamHandle));
172 if (!provider->lockedStream.lockCount)
174 DPRINT(("About to acquire & lock a read buffer"));
176 provider->lockedStream.buffer = owfNativeStreamAcquireReadBuffer(provider->streamHandle);
177 DPRINT((" Acquired read buffer stream=%p, buffer=%d",
178 provider->streamHandle, provider->lockedStream.buffer));
180 /* Bind source image to pixel buffer */
181 pixels = owfNativeStreamGetBufferPtr(provider->streamHandle, provider->lockedStream.buffer);
182 if (provider->lockedStream.image)
184 OWF_Image_SetPixelBuffer(provider->lockedStream.image,pixels);
188 owfNativeStreamGetHeader(provider->streamHandle,
190 &stride, &imgf, &pixelSize);
191 provider->lockedStream.image = OWF_Image_Create(width, height, &imgf, pixels, stride);
195 ++provider->lockedStream.lockCount;
196 DPRINT(("lock count = %d", provider->lockedStream.lockCount));
201 WFC_ImageProvider_Unlock(WFC_IMAGE_PROVIDER* provider)
204 DPRINT(("WFC_ImageProvider_Unlock: provider = NULL"));
208 if (provider->lockedStream.lockCount > 0)
210 --provider->lockedStream.lockCount;
211 DPRINT(("lock count = %d", provider->lockedStream.lockCount));
213 if (!provider->lockedStream.lockCount)
215 DPRINT((" Releasing read buffer provider=%p, buffer=%d",
216 provider->handle, provider->lockedStream.buffer));
217 owfNativeStreamReleaseReadBuffer(provider->streamHandle, provider->lockedStream.buffer);