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
);
83 if (!ppvObject
) return E_POINTER
;
86 if (GUID_equal(This
->guids
[i
], riid
)) {
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.
92 NineUnknown_AddRef(This
);
95 } while (This
->guids
[++i
]);
102 NineUnknown_AddRef( struct NineUnknown
*This
)
106 return NineUnknown_AddRef(This
->container
);
108 r
= p_atomic_inc_return(&This
->refs
);
112 NineUnknown_AddRef(NineUnknown(This
->device
));
113 /* This shouldn't be necessary:
115 NineUnknown_Bind(NineUnknown(This->container)); */
121 NineUnknown_Release( struct NineUnknown
*This
)
124 return NineUnknown_Release(This
->container
);
126 ULONG r
= p_atomic_dec_return(&This
->refs
);
130 if (NineUnknown_Release(NineUnknown(This
->device
)) == 0)
131 return r
; /* everything's gone */
133 if (This
->container
) {
134 /* NineUnknown_Unbind(NineUnknown(This->container)); */
136 if (This
->bind
== 0) {
144 NineUnknown_GetDevice( struct NineUnknown
*This
,
145 IDirect3DDevice9
**ppDevice
)
147 user_assert(ppDevice
, E_POINTER
);
148 NineUnknown_AddRef(NineUnknown(This
->device
));
149 *ppDevice
= (IDirect3DDevice9
*)This
->device
;
154 NineUnknown_SetPrivateData( struct NineUnknown
*This
,
161 struct pheader
*header
;
162 const void *user_data
= pData
;
166 DBG("This=%p GUID=%s pData=%p SizeOfData=%u Flags=%x\n",
167 This
, GUID_sprintf(guid_str
, refguid
), pData
, SizeOfData
, Flags
);
171 if (Flags
& D3DSPD_IUNKNOWN
)
172 user_assert(SizeOfData
== sizeof(IUnknown
*), D3DERR_INVALIDCALL
);
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
;
179 /* if the refguid already exists, delete it */
180 NineUnknown_FreePrivateData(This
, refguid
);
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 */
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
));
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
); }
201 if (err
== PIPE_ERROR_OUT_OF_MEMORY
) { return E_OUTOFMEMORY
; }
203 return D3DERR_DRIVERINTERNALERROR
;
207 NineUnknown_GetPrivateData( struct NineUnknown
*This
,
212 struct pheader
*header
;
217 DBG("This=%p GUID=%s pData=%p pSizeOfData=%p\n",
218 This
, GUID_sprintf(guid_str
, refguid
), pData
, pSizeOfData
);
222 header
= util_hash_table_get(This
->pdata
, refguid
);
223 if (!header
) { return D3DERR_NOTFOUND
; }
225 user_assert(pSizeOfData
, E_POINTER
);
226 sizeofdata
= *pSizeOfData
;
227 *pSizeOfData
= header
->size
;
232 if (sizeofdata
< header
->size
) {
233 return D3DERR_MOREDATA
;
236 header_data
= (void *)header
+ sizeof(*header
);
237 if (header
->unknown
) { IUnknown_AddRef(*(IUnknown
**)header_data
); }
238 memcpy(pData
, header_data
, header
->size
);
244 NineUnknown_FreePrivateData( struct NineUnknown
*This
,
247 struct pheader
*header
;
250 DBG("This=%p GUID=%s\n", This
, GUID_sprintf(guid_str
, refguid
));
254 header
= util_hash_table_get(This
->pdata
, refguid
);
256 return D3DERR_NOTFOUND
;
258 ht_guid_delete(NULL
, header
, NULL
);
259 util_hash_table_remove(This
->pdata
, refguid
);