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