st/nine: Silent warnings with guid_str
[mesa.git] / src / gallium / state_trackers / nine / iunknown.c
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "iunknown.h"
24 #include "util/u_atomic.h"
25 #include "util/u_hash_table.h"
26
27 #include "nine_helpers.h"
28 #include "nine_pdata.h"
29
30 #define DBG_CHANNEL DBG_UNKNOWN
31
32 HRESULT
33 NineUnknown_ctor( struct NineUnknown *This,
34 struct NineUnknownParams *pParams )
35 {
36 This->refs = pParams->container ? 0 : 1;
37 This->bind = 0;
38 This->forward = !This->refs;
39 This->container = pParams->container;
40 This->device = pParams->device;
41 if (This->refs && This->device)
42 NineUnknown_AddRef(NineUnknown(This->device));
43
44 This->vtable = pParams->vtable;
45 This->vtable_internal = pParams->vtable;
46 This->guids = pParams->guids;
47 This->dtor = pParams->dtor;
48
49 This->pdata = util_hash_table_create(ht_guid_hash, ht_guid_compare);
50 if (!This->pdata)
51 return E_OUTOFMEMORY;
52
53 return D3D_OK;
54 }
55
56 void
57 NineUnknown_dtor( struct NineUnknown *This )
58 {
59 if (This->refs && This->device) /* Possible only if early exit after a ctor failed */
60 (void) NineUnknown_Release(NineUnknown(This->device));
61
62 if (This->pdata) {
63 util_hash_table_foreach(This->pdata, ht_guid_delete, NULL);
64 util_hash_table_destroy(This->pdata);
65 }
66
67 FREE(This);
68 }
69
70 HRESULT NINE_WINAPI
71 NineUnknown_QueryInterface( struct NineUnknown *This,
72 REFIID riid,
73 void **ppvObject )
74 {
75 unsigned i = 0;
76 char guid_str[64];
77
78 DBG("This=%p riid=%p id=%s ppvObject=%p\n",
79 This, riid, riid ? GUID_sprintf(guid_str, riid) : "", ppvObject);
80
81 (void)guid_str;
82
83 if (!ppvObject) return E_POINTER;
84
85 do {
86 if (GUID_equal(This->guids[i], riid)) {
87 *ppvObject = This;
88 /* Tests showed that this call succeeds even on objects with
89 * zero refcount. This can happen if the app released all references
90 * but the resource is still bound.
91 */
92 NineUnknown_AddRef(This);
93 return S_OK;
94 }
95 } while (This->guids[++i]);
96
97 *ppvObject = NULL;
98 return E_NOINTERFACE;
99 }
100
101 ULONG NINE_WINAPI
102 NineUnknown_AddRef( struct NineUnknown *This )
103 {
104 ULONG r;
105 if (This->forward)
106 return NineUnknown_AddRef(This->container);
107 else
108 r = p_atomic_inc_return(&This->refs);
109
110 if (r == 1) {
111 if (This->device)
112 NineUnknown_AddRef(NineUnknown(This->device));
113 /* This shouldn't be necessary:
114 if (This->container)
115 NineUnknown_Bind(NineUnknown(This->container)); */
116 }
117 return r;
118 }
119
120 ULONG NINE_WINAPI
121 NineUnknown_Release( struct NineUnknown *This )
122 {
123 if (This->forward)
124 return NineUnknown_Release(This->container);
125
126 ULONG r = p_atomic_dec_return(&This->refs);
127
128 if (r == 0) {
129 if (This->device) {
130 if (NineUnknown_Release(NineUnknown(This->device)) == 0)
131 return r; /* everything's gone */
132 }
133 if (This->container) {
134 /* NineUnknown_Unbind(NineUnknown(This->container)); */
135 } else
136 if (This->bind == 0) {
137 This->dtor(This);
138 }
139 }
140 return r;
141 }
142
143 HRESULT NINE_WINAPI
144 NineUnknown_GetDevice( struct NineUnknown *This,
145 IDirect3DDevice9 **ppDevice )
146 {
147 user_assert(ppDevice, E_POINTER);
148 NineUnknown_AddRef(NineUnknown(This->device));
149 *ppDevice = (IDirect3DDevice9 *)This->device;
150 return D3D_OK;
151 }
152
153 HRESULT NINE_WINAPI
154 NineUnknown_SetPrivateData( struct NineUnknown *This,
155 REFGUID refguid,
156 const void *pData,
157 DWORD SizeOfData,
158 DWORD Flags )
159 {
160 enum pipe_error err;
161 struct pheader *header;
162 const void *user_data = pData;
163 char guid_str[64];
164 void *header_data;
165
166 DBG("This=%p GUID=%s pData=%p SizeOfData=%u Flags=%x\n",
167 This, GUID_sprintf(guid_str, refguid), pData, SizeOfData, Flags);
168
169 (void)guid_str;
170
171 if (Flags & D3DSPD_IUNKNOWN)
172 user_assert(SizeOfData == sizeof(IUnknown *), D3DERR_INVALIDCALL);
173
174 /* data consists of a header and the actual data. avoiding 2 mallocs */
175 header = CALLOC_VARIANT_LENGTH_STRUCT(pheader, SizeOfData);
176 if (!header) { return E_OUTOFMEMORY; }
177 header->unknown = (Flags & D3DSPD_IUNKNOWN) ? TRUE : FALSE;
178
179 /* if the refguid already exists, delete it */
180 NineUnknown_FreePrivateData(This, refguid);
181
182 /* IUnknown special case */
183 if (header->unknown) {
184 /* here the pointer doesn't point to the data we want, so point at the
185 * pointer making what we eventually copy is the pointer itself */
186 user_data = &pData;
187 }
188
189 header->size = SizeOfData;
190 header_data = (void *)header + sizeof(*header);
191 memcpy(header_data, user_data, header->size);
192 memcpy(&header->guid, refguid, sizeof(header->guid));
193
194 err = util_hash_table_set(This->pdata, &header->guid, header);
195 if (err == PIPE_OK) {
196 if (header->unknown) { IUnknown_AddRef(*(IUnknown **)header_data); }
197 return D3D_OK;
198 }
199
200 FREE(header);
201 if (err == PIPE_ERROR_OUT_OF_MEMORY) { return E_OUTOFMEMORY; }
202
203 return D3DERR_DRIVERINTERNALERROR;
204 }
205
206 HRESULT NINE_WINAPI
207 NineUnknown_GetPrivateData( struct NineUnknown *This,
208 REFGUID refguid,
209 void *pData,
210 DWORD *pSizeOfData )
211 {
212 struct pheader *header;
213 DWORD sizeofdata;
214 char guid_str[64];
215 void *header_data;
216
217 DBG("This=%p GUID=%s pData=%p pSizeOfData=%p\n",
218 This, GUID_sprintf(guid_str, refguid), pData, pSizeOfData);
219
220 (void)guid_str;
221
222 header = util_hash_table_get(This->pdata, refguid);
223 if (!header) { return D3DERR_NOTFOUND; }
224
225 user_assert(pSizeOfData, E_POINTER);
226 sizeofdata = *pSizeOfData;
227 *pSizeOfData = header->size;
228
229 if (!pData) {
230 return D3D_OK;
231 }
232 if (sizeofdata < header->size) {
233 return D3DERR_MOREDATA;
234 }
235
236 header_data = (void *)header + sizeof(*header);
237 if (header->unknown) { IUnknown_AddRef(*(IUnknown **)header_data); }
238 memcpy(pData, header_data, header->size);
239
240 return D3D_OK;
241 }
242
243 HRESULT NINE_WINAPI
244 NineUnknown_FreePrivateData( struct NineUnknown *This,
245 REFGUID refguid )
246 {
247 struct pheader *header;
248 char guid_str[64];
249
250 DBG("This=%p GUID=%s\n", This, GUID_sprintf(guid_str, refguid));
251
252 (void)guid_str;
253
254 header = util_hash_table_get(This->pdata, refguid);
255 if (!header)
256 return D3DERR_NOTFOUND;
257
258 ht_guid_delete(NULL, header, NULL);
259 util_hash_table_remove(This->pdata, refguid);
260
261 return D3D_OK;
262 }