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 #include "nine_lock.h"
30
31 #define DBG_CHANNEL DBG_UNKNOWN
32
33 HRESULT
NineUnknown_ctor(struct NineUnknown * This,struct NineUnknownParams * pParams)34 NineUnknown_ctor( struct NineUnknown *This,
35 struct NineUnknownParams *pParams )
36 {
37 if (pParams->container) {
38 This->refs = 0;
39 This->forward = true;
40 This->bind = 0;
41 assert(!pParams->start_with_bind_not_ref);
42 } else if (pParams->start_with_bind_not_ref) {
43 This->refs = 0;
44 This->forward = false;
45 This->bind = 1;
46 } else {
47 This->refs = 1;
48 This->forward = false;
49 This->bind = 0;
50 }
51
52 This->container = pParams->container;
53 This->device = pParams->device;
54 if (This->refs && This->device)
55 NineUnknown_AddRef(NineUnknown(This->device));
56
57 This->vtable = pParams->vtable;
58 This->vtable_internal = pParams->vtable;
59 This->guids = pParams->guids;
60 This->dtor = pParams->dtor;
61
62 This->pdata = _mesa_hash_table_create(NULL, ht_guid_hash, ht_guid_compare);
63 if (!This->pdata)
64 return E_OUTOFMEMORY;
65
66 return D3D_OK;
67 }
68
69 void
NineUnknown_dtor(struct NineUnknown * This)70 NineUnknown_dtor( struct NineUnknown *This )
71 {
72 if (This->refs && This->device) /* Possible only if early exit after a ctor failed */
73 (void) NineUnknown_Release(NineUnknown(This->device));
74
75 if (This->pdata) {
76 util_hash_table_foreach(This->pdata, ht_guid_delete, NULL);
77 _mesa_hash_table_destroy(This->pdata, NULL);
78 }
79
80 FREE(This);
81 }
82
83 HRESULT NINE_WINAPI
NineUnknown_QueryInterface(struct NineUnknown * This,REFIID riid,void ** ppvObject)84 NineUnknown_QueryInterface( struct NineUnknown *This,
85 REFIID riid,
86 void **ppvObject )
87 {
88 unsigned i = 0;
89 char guid_str[64];
90
91 DBG("This=%p riid=%p id=%s ppvObject=%p\n",
92 This, riid, riid ? GUID_sprintf(guid_str, riid) : "", ppvObject);
93
94 (void)guid_str;
95
96 if (!ppvObject) return E_POINTER;
97
98 do {
99 if (GUID_equal(This->guids[i], riid)) {
100 *ppvObject = This;
101 /* Tests showed that this call succeeds even on objects with
102 * zero refcount. This can happen if the app released all references
103 * but the resource is still bound.
104 */
105 NineUnknown_AddRef(This);
106 return S_OK;
107 }
108 } while (This->guids[++i]);
109
110 *ppvObject = NULL;
111 return E_NOINTERFACE;
112 }
113
114 ULONG NINE_WINAPI
NineUnknown_AddRef(struct NineUnknown * This)115 NineUnknown_AddRef( struct NineUnknown *This )
116 {
117 ULONG r;
118 if (This->forward)
119 return NineUnknown_AddRef(This->container);
120 else
121 r = p_atomic_inc_return(&This->refs);
122
123 if (r == 1) {
124 if (This->device)
125 NineUnknown_AddRef(NineUnknown(This->device));
126 }
127 return r;
128 }
129
130 ULONG NINE_WINAPI
NineUnknown_Release(struct NineUnknown * This)131 NineUnknown_Release( struct NineUnknown *This )
132 {
133 if (This->forward)
134 return NineUnknown_Release(This->container);
135
136 ULONG r = p_atomic_dec_return(&This->refs);
137
138 if (r == 0) {
139 if (This->device) {
140 if (NineUnknown_Release(NineUnknown(This->device)) == 0)
141 return r; /* everything's gone */
142 }
143 /* Containers (here with !forward) take care of item destruction */
144 if (!This->container && This->bind == 0) {
145 This->dtor(This);
146 }
147 }
148 return r;
149 }
150
151 /* No need to lock the mutex protecting nine (when D3DCREATE_MULTITHREADED)
152 * for AddRef and Release, except for dtor as some of the dtors require it. */
153 ULONG NINE_WINAPI
NineUnknown_ReleaseWithDtorLock(struct NineUnknown * This)154 NineUnknown_ReleaseWithDtorLock( struct NineUnknown *This )
155 {
156 if (This->forward)
157 return NineUnknown_ReleaseWithDtorLock(This->container);
158
159 ULONG r = p_atomic_dec_return(&This->refs);
160
161 if (r == 0) {
162 if (This->device) {
163 if (NineUnknown_ReleaseWithDtorLock(NineUnknown(This->device)) == 0)
164 return r; /* everything's gone */
165 }
166 /* Containers (here with !forward) take care of item destruction */
167 if (!This->container && This->bind == 0) {
168 NineLockGlobalMutex();
169 This->dtor(This);
170 NineUnlockGlobalMutex();
171 }
172 }
173 return r;
174 }
175
176 HRESULT NINE_WINAPI
NineUnknown_GetDevice(struct NineUnknown * This,IDirect3DDevice9 ** ppDevice)177 NineUnknown_GetDevice( struct NineUnknown *This,
178 IDirect3DDevice9 **ppDevice )
179 {
180 user_assert(ppDevice, E_POINTER);
181 NineUnknown_AddRef(NineUnknown(This->device));
182 *ppDevice = (IDirect3DDevice9 *)This->device;
183 return D3D_OK;
184 }
185
186 HRESULT NINE_WINAPI
NineUnknown_SetPrivateData(struct NineUnknown * This,REFGUID refguid,const void * pData,DWORD SizeOfData,DWORD Flags)187 NineUnknown_SetPrivateData( struct NineUnknown *This,
188 REFGUID refguid,
189 const void *pData,
190 DWORD SizeOfData,
191 DWORD Flags )
192 {
193 struct pheader *header;
194 const void *user_data = pData;
195 char guid_str[64];
196 void *header_data;
197
198 DBG("This=%p GUID=%s pData=%p SizeOfData=%u Flags=%x\n",
199 This, GUID_sprintf(guid_str, refguid), pData, SizeOfData, Flags);
200
201 (void)guid_str;
202
203 if (Flags & D3DSPD_IUNKNOWN)
204 user_assert(SizeOfData == sizeof(IUnknown *), D3DERR_INVALIDCALL);
205
206 /* data consists of a header and the actual data. avoiding 2 mallocs */
207 header = CALLOC_VARIANT_LENGTH_STRUCT(pheader, SizeOfData);
208 if (!header) { return E_OUTOFMEMORY; }
209 header->unknown = (Flags & D3DSPD_IUNKNOWN) ? TRUE : FALSE;
210
211 /* if the refguid already exists, delete it */
212 NineUnknown_FreePrivateData(This, refguid);
213
214 /* IUnknown special case */
215 if (header->unknown) {
216 /* here the pointer doesn't point to the data we want, so point at the
217 * pointer making what we eventually copy is the pointer itself */
218 user_data = &pData;
219 }
220
221 header->size = SizeOfData;
222 header_data = (void *)header + sizeof(*header);
223 memcpy(header_data, user_data, header->size);
224 memcpy(&header->guid, refguid, sizeof(header->guid));
225
226 _mesa_hash_table_insert(This->pdata, &header->guid, header);
227 if (header->unknown) { IUnknown_AddRef(*(IUnknown **)header_data); }
228 return D3D_OK;
229 }
230
231 HRESULT NINE_WINAPI
NineUnknown_GetPrivateData(struct NineUnknown * This,REFGUID refguid,void * pData,DWORD * pSizeOfData)232 NineUnknown_GetPrivateData( struct NineUnknown *This,
233 REFGUID refguid,
234 void *pData,
235 DWORD *pSizeOfData )
236 {
237 struct pheader *header;
238 DWORD sizeofdata;
239 char guid_str[64];
240 void *header_data;
241
242 DBG("This=%p GUID=%s pData=%p pSizeOfData=%p\n",
243 This, GUID_sprintf(guid_str, refguid), pData, pSizeOfData);
244
245 (void)guid_str;
246
247 header = util_hash_table_get(This->pdata, refguid);
248 if (!header) { return D3DERR_NOTFOUND; }
249
250 user_assert(pSizeOfData, E_POINTER);
251 sizeofdata = *pSizeOfData;
252 *pSizeOfData = header->size;
253
254 if (!pData) {
255 return D3D_OK;
256 }
257 if (sizeofdata < header->size) {
258 return D3DERR_MOREDATA;
259 }
260
261 header_data = (void *)header + sizeof(*header);
262 if (header->unknown) { IUnknown_AddRef(*(IUnknown **)header_data); }
263 memcpy(pData, header_data, header->size);
264
265 return D3D_OK;
266 }
267
268 HRESULT NINE_WINAPI
NineUnknown_FreePrivateData(struct NineUnknown * This,REFGUID refguid)269 NineUnknown_FreePrivateData( struct NineUnknown *This,
270 REFGUID refguid )
271 {
272 struct pheader *header;
273 char guid_str[64];
274
275 DBG("This=%p GUID=%s\n", This, GUID_sprintf(guid_str, refguid));
276
277 (void)guid_str;
278
279 header = util_hash_table_get(This->pdata, refguid);
280 if (!header)
281 return D3DERR_NOTFOUND;
282
283 ht_guid_delete(NULL, header, NULL);
284 _mesa_hash_table_remove_key(This->pdata, refguid);
285
286 return D3D_OK;
287 }
288