541c528591aad4f0a812c12400ae7890f65ad050
[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
163 DBG("This=%p GUID=%s pData=%p SizeOfData=%u Flags=%x\n",
164 This, GUID_sprintf(guid_str, refguid), pData, SizeOfData, Flags);
165
166 if (Flags & D3DSPD_IUNKNOWN)
167 user_assert(SizeOfData == sizeof(IUnknown *), D3DERR_INVALIDCALL);
168
169 /* data consists of a header and the actual data. avoiding 2 mallocs */
170 header = CALLOC_VARIANT_LENGTH_STRUCT(pheader, SizeOfData-1);
171 if (!header) { return E_OUTOFMEMORY; }
172 header->unknown = (Flags & D3DSPD_IUNKNOWN) ? TRUE : FALSE;
173
174 /* if the refguid already exists, delete it */
175 NineUnknown_FreePrivateData(This, refguid);
176
177 /* IUnknown special case */
178 if (header->unknown) {
179 /* here the pointer doesn't point to the data we want, so point at the
180 * pointer making what we eventually copy is the pointer itself */
181 user_data = &pData;
182 }
183
184 header->size = SizeOfData;
185 memcpy(header->data, user_data, header->size);
186 memcpy(&header->guid, refguid, sizeof(header->guid));
187
188 err = util_hash_table_set(This->pdata, &header->guid, header);
189 if (err == PIPE_OK) {
190 if (header->unknown) { IUnknown_AddRef(*(IUnknown **)header->data); }
191 return D3D_OK;
192 }
193
194 FREE(header);
195 if (err == PIPE_ERROR_OUT_OF_MEMORY) { return E_OUTOFMEMORY; }
196
197 return D3DERR_DRIVERINTERNALERROR;
198 }
199
200 HRESULT NINE_WINAPI
201 NineUnknown_GetPrivateData( struct NineUnknown *This,
202 REFGUID refguid,
203 void *pData,
204 DWORD *pSizeOfData )
205 {
206 struct pheader *header;
207 DWORD sizeofdata;
208 char guid_str[64];
209
210 DBG("This=%p GUID=%s pData=%p pSizeOfData=%p\n",
211 This, GUID_sprintf(guid_str, refguid), pData, pSizeOfData);
212
213 header = util_hash_table_get(This->pdata, refguid);
214 if (!header) { return D3DERR_NOTFOUND; }
215
216 user_assert(pSizeOfData, E_POINTER);
217 sizeofdata = *pSizeOfData;
218 *pSizeOfData = header->size;
219
220 if (!pData) {
221 return D3D_OK;
222 }
223 if (sizeofdata < header->size) {
224 return D3DERR_MOREDATA;
225 }
226
227 if (header->unknown) { IUnknown_AddRef(*(IUnknown **)header->data); }
228 memcpy(pData, header->data, header->size);
229
230 return D3D_OK;
231 }
232
233 HRESULT NINE_WINAPI
234 NineUnknown_FreePrivateData( struct NineUnknown *This,
235 REFGUID refguid )
236 {
237 struct pheader *header;
238 char guid_str[64];
239
240 DBG("This=%p GUID=%s\n", This, GUID_sprintf(guid_str, refguid));
241
242 header = util_hash_table_get(This->pdata, refguid);
243 if (!header)
244 return D3DERR_NOTFOUND;
245
246 ht_guid_delete(NULL, header, NULL);
247 util_hash_table_remove(This->pdata, refguid);
248
249 return D3D_OK;
250 }