st/nine: Fix compiler warning
[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 if (!ppvObject) return E_POINTER;
82
83 do {
84 if (GUID_equal(This->guids[i], riid)) {
85 *ppvObject = This;
86 /* Tests showed that this call succeeds even on objects with
87 * zero refcount. This can happen if the app released all references
88 * but the resource is still bound.
89 */
90 NineUnknown_AddRef(This);
91 return S_OK;
92 }
93 } while (This->guids[++i]);
94
95 *ppvObject = NULL;
96 return E_NOINTERFACE;
97 }
98
99 ULONG NINE_WINAPI
100 NineUnknown_AddRef( struct NineUnknown *This )
101 {
102 ULONG r;
103 if (This->forward)
104 return NineUnknown_AddRef(This->container);
105 else
106 r = p_atomic_inc_return(&This->refs);
107
108 if (r == 1) {
109 if (This->device)
110 NineUnknown_AddRef(NineUnknown(This->device));
111 /* This shouldn't be necessary:
112 if (This->container)
113 NineUnknown_Bind(NineUnknown(This->container)); */
114 }
115 return r;
116 }
117
118 ULONG NINE_WINAPI
119 NineUnknown_Release( struct NineUnknown *This )
120 {
121 if (This->forward)
122 return NineUnknown_Release(This->container);
123
124 ULONG r = p_atomic_dec_return(&This->refs);
125
126 if (r == 0) {
127 if (This->device) {
128 if (NineUnknown_Release(NineUnknown(This->device)) == 0)
129 return r; /* everything's gone */
130 }
131 if (This->container) {
132 /* NineUnknown_Unbind(NineUnknown(This->container)); */
133 } else
134 if (This->bind == 0) {
135 This->dtor(This);
136 }
137 }
138 return r;
139 }
140
141 HRESULT NINE_WINAPI
142 NineUnknown_GetDevice( struct NineUnknown *This,
143 IDirect3DDevice9 **ppDevice )
144 {
145 user_assert(ppDevice, E_POINTER);
146 NineUnknown_AddRef(NineUnknown(This->device));
147 *ppDevice = (IDirect3DDevice9 *)This->device;
148 return D3D_OK;
149 }
150
151 HRESULT NINE_WINAPI
152 NineUnknown_SetPrivateData( struct NineUnknown *This,
153 REFGUID refguid,
154 const void *pData,
155 DWORD SizeOfData,
156 DWORD Flags )
157 {
158 enum pipe_error err;
159 struct pheader *header;
160 const void *user_data = pData;
161 char guid_str[64];
162 void *header_data;
163
164 DBG("This=%p GUID=%s pData=%p SizeOfData=%u Flags=%x\n",
165 This, GUID_sprintf(guid_str, refguid), pData, SizeOfData, Flags);
166
167 if (Flags & D3DSPD_IUNKNOWN)
168 user_assert(SizeOfData == sizeof(IUnknown *), D3DERR_INVALIDCALL);
169
170 /* data consists of a header and the actual data. avoiding 2 mallocs */
171 header = CALLOC_VARIANT_LENGTH_STRUCT(pheader, SizeOfData);
172 if (!header) { return E_OUTOFMEMORY; }
173 header->unknown = (Flags & D3DSPD_IUNKNOWN) ? TRUE : FALSE;
174
175 /* if the refguid already exists, delete it */
176 NineUnknown_FreePrivateData(This, refguid);
177
178 /* IUnknown special case */
179 if (header->unknown) {
180 /* here the pointer doesn't point to the data we want, so point at the
181 * pointer making what we eventually copy is the pointer itself */
182 user_data = &pData;
183 }
184
185 header->size = SizeOfData;
186 header_data = (void *)header + sizeof(*header);
187 memcpy(header_data, user_data, header->size);
188 memcpy(&header->guid, refguid, sizeof(header->guid));
189
190 err = util_hash_table_set(This->pdata, &header->guid, header);
191 if (err == PIPE_OK) {
192 if (header->unknown) { IUnknown_AddRef(*(IUnknown **)header_data); }
193 return D3D_OK;
194 }
195
196 FREE(header);
197 if (err == PIPE_ERROR_OUT_OF_MEMORY) { return E_OUTOFMEMORY; }
198
199 return D3DERR_DRIVERINTERNALERROR;
200 }
201
202 HRESULT NINE_WINAPI
203 NineUnknown_GetPrivateData( struct NineUnknown *This,
204 REFGUID refguid,
205 void *pData,
206 DWORD *pSizeOfData )
207 {
208 struct pheader *header;
209 DWORD sizeofdata;
210 char guid_str[64];
211 void *header_data;
212
213 DBG("This=%p GUID=%s pData=%p pSizeOfData=%p\n",
214 This, GUID_sprintf(guid_str, refguid), pData, pSizeOfData);
215
216 header = util_hash_table_get(This->pdata, refguid);
217 if (!header) { return D3DERR_NOTFOUND; }
218
219 user_assert(pSizeOfData, E_POINTER);
220 sizeofdata = *pSizeOfData;
221 *pSizeOfData = header->size;
222
223 if (!pData) {
224 return D3D_OK;
225 }
226 if (sizeofdata < header->size) {
227 return D3DERR_MOREDATA;
228 }
229
230 header_data = (void *)header + sizeof(*header);
231 if (header->unknown) { IUnknown_AddRef(*(IUnknown **)header_data); }
232 memcpy(pData, header_data, header->size);
233
234 return D3D_OK;
235 }
236
237 HRESULT NINE_WINAPI
238 NineUnknown_FreePrivateData( struct NineUnknown *This,
239 REFGUID refguid )
240 {
241 struct pheader *header;
242 char guid_str[64];
243
244 DBG("This=%p GUID=%s\n", This, GUID_sprintf(guid_str, refguid));
245
246 header = util_hash_table_get(This->pdata, refguid);
247 if (!header)
248 return D3DERR_NOTFOUND;
249
250 ht_guid_delete(NULL, header, NULL);
251 util_hash_table_remove(This->pdata, refguid);
252
253 return D3D_OK;
254 }