2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
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:
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
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. */
24 #include "util/u_atomic.h"
25 #include "util/u_hash_table.h"
27 #include "nine_helpers.h"
28 #include "nine_pdata.h"
30 #define DBG_CHANNEL DBG_UNKNOWN
33 NineUnknown_ctor( struct NineUnknown
*This
,
34 struct NineUnknownParams
*pParams
)
36 This
->refs
= pParams
->container
? 0 : 1;
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
));
44 This
->vtable
= pParams
->vtable
;
45 This
->vtable_internal
= pParams
->vtable
;
46 This
->guids
= pParams
->guids
;
47 This
->dtor
= pParams
->dtor
;
49 This
->pdata
= util_hash_table_create(ht_guid_hash
, ht_guid_compare
);
57 NineUnknown_dtor( struct NineUnknown
*This
)
59 if (This
->refs
&& This
->device
) /* Possible only if early exit after a ctor failed */
60 (void) NineUnknown_Release(NineUnknown(This
->device
));
63 util_hash_table_foreach(This
->pdata
, ht_guid_delete
, NULL
);
64 util_hash_table_destroy(This
->pdata
);
71 NineUnknown_QueryInterface( struct NineUnknown
*This
,
78 DBG("This=%p riid=%p id=%s ppvObject=%p\n",
79 This
, riid
, riid
? GUID_sprintf(guid_str
, riid
) : "", ppvObject
);
81 if (!ppvObject
) return E_POINTER
;
84 if (GUID_equal(This
->guids
[i
], riid
)) {
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.
90 NineUnknown_AddRef(This
);
93 } while (This
->guids
[++i
]);
100 NineUnknown_AddRef( struct NineUnknown
*This
)
104 return NineUnknown_AddRef(This
->container
);
106 r
= p_atomic_inc_return(&This
->refs
);
110 NineUnknown_AddRef(NineUnknown(This
->device
));
111 /* This shouldn't be necessary:
113 NineUnknown_Bind(NineUnknown(This->container)); */
119 NineUnknown_Release( struct NineUnknown
*This
)
122 return NineUnknown_Release(This
->container
);
124 ULONG r
= p_atomic_dec_return(&This
->refs
);
128 if (NineUnknown_Release(NineUnknown(This
->device
)) == 0)
129 return r
; /* everything's gone */
131 if (This
->container
) {
132 /* NineUnknown_Unbind(NineUnknown(This->container)); */
134 if (This
->bind
== 0) {
142 NineUnknown_GetDevice( struct NineUnknown
*This
,
143 IDirect3DDevice9
**ppDevice
)
145 user_assert(ppDevice
, E_POINTER
);
146 NineUnknown_AddRef(NineUnknown(This
->device
));
147 *ppDevice
= (IDirect3DDevice9
*)This
->device
;
152 NineUnknown_SetPrivateData( struct NineUnknown
*This
,
159 struct pheader
*header
;
160 const void *user_data
= pData
;
164 DBG("This=%p GUID=%s pData=%p SizeOfData=%u Flags=%x\n",
165 This
, GUID_sprintf(guid_str
, refguid
), pData
, SizeOfData
, Flags
);
167 if (Flags
& D3DSPD_IUNKNOWN
)
168 user_assert(SizeOfData
== sizeof(IUnknown
*), D3DERR_INVALIDCALL
);
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
;
175 /* if the refguid already exists, delete it */
176 NineUnknown_FreePrivateData(This
, refguid
);
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 */
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
));
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
); }
197 if (err
== PIPE_ERROR_OUT_OF_MEMORY
) { return E_OUTOFMEMORY
; }
199 return D3DERR_DRIVERINTERNALERROR
;
203 NineUnknown_GetPrivateData( struct NineUnknown
*This
,
208 struct pheader
*header
;
213 DBG("This=%p GUID=%s pData=%p pSizeOfData=%p\n",
214 This
, GUID_sprintf(guid_str
, refguid
), pData
, pSizeOfData
);
216 header
= util_hash_table_get(This
->pdata
, refguid
);
217 if (!header
) { return D3DERR_NOTFOUND
; }
219 user_assert(pSizeOfData
, E_POINTER
);
220 sizeofdata
= *pSizeOfData
;
221 *pSizeOfData
= header
->size
;
226 if (sizeofdata
< header
->size
) {
227 return D3DERR_MOREDATA
;
230 header_data
= (void *)header
+ sizeof(*header
);
231 if (header
->unknown
) { IUnknown_AddRef(*(IUnknown
**)header_data
); }
232 memcpy(pData
, header_data
, header
->size
);
238 NineUnknown_FreePrivateData( struct NineUnknown
*This
,
241 struct pheader
*header
;
244 DBG("This=%p GUID=%s\n", This
, GUID_sprintf(guid_str
, refguid
));
246 header
= util_hash_table_get(This
->pdata
, refguid
);
248 return D3DERR_NOTFOUND
;
250 ht_guid_delete(NULL
, header
, NULL
);
251 util_hash_table_remove(This
->pdata
, refguid
);