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