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 #ifndef _NINE_IUNKNOWN_H_
24 #define _NINE_IUNKNOWN_H_
25 
26 #include "pipe/p_compiler.h"
27 
28 #include "util/u_atomic.h"
29 #include "util/u_memory.h"
30 
31 #include "guid.h"
32 #include "nine_flags.h"
33 #include "nine_debug.h"
34 #include "nine_quirk.h"
35 
36 #include "d3d9.h"
37 
38 struct Nine9;
39 struct NineDevice9;
40 
41 struct NineUnknown
42 {
43     /* pointer to vtable (can be overriden outside gallium nine) */
44     void *vtable;
45     /* pointer to internal vtable  */
46     void *vtable_internal;
47 
48     int32_t refs; /* external reference count */
49     int32_t bind; /* internal bind count */
50     boolean forward; /* whether to forward references to the container */
51 
52     /* container: for surfaces and volumes only.
53      * Can be a texture, a volume texture or a swapchain.
54      * forward is set to false for the swapchain case.
55      * If forward is set, refs are passed to the container if forward is set
56      * and the container has bind increased if the object has non null bind. */
57     struct NineUnknown *container;
58     struct NineDevice9 *device;    /* referenced if (refs) */
59 
60     const GUID **guids; /* for QueryInterface */
61 
62     /* for [GS]etPrivateData/FreePrivateData */
63     struct hash_table *pdata;
64 
65     void (*dtor)(void *data); /* top-level dtor */
66 };
67 static inline struct NineUnknown *
NineUnknown(void * data)68 NineUnknown( void *data )
69 {
70     return (struct NineUnknown *)data;
71 }
72 
73 /* Use this instead of a shitload of arguments: */
74 struct NineUnknownParams
75 {
76     void *vtable;
77     const GUID **guids;
78     void (*dtor)(void *data);
79     struct NineUnknown *container;
80     struct NineDevice9 *device;
81     bool start_with_bind_not_ref;
82 };
83 
84 HRESULT
85 NineUnknown_ctor( struct NineUnknown *This,
86                   struct NineUnknownParams *pParams );
87 
88 void
89 NineUnknown_dtor( struct NineUnknown *This );
90 
91 /*** Direct3D public methods ***/
92 
93 HRESULT NINE_WINAPI
94 NineUnknown_QueryInterface( struct NineUnknown *This,
95                             REFIID riid,
96                             void **ppvObject );
97 
98 ULONG NINE_WINAPI
99 NineUnknown_AddRef( struct NineUnknown *This );
100 
101 ULONG NINE_WINAPI
102 NineUnknown_Release( struct NineUnknown *This );
103 
104 ULONG NINE_WINAPI
105 NineUnknown_ReleaseWithDtorLock( struct NineUnknown *This );
106 
107 HRESULT NINE_WINAPI
108 NineUnknown_GetDevice( struct NineUnknown *This,
109                        IDirect3DDevice9 **ppDevice );
110 
111 HRESULT NINE_WINAPI
112 NineUnknown_SetPrivateData( struct NineUnknown *This,
113                             REFGUID refguid,
114                             const void *pData,
115                             DWORD SizeOfData,
116                             DWORD Flags );
117 
118 HRESULT NINE_WINAPI
119 NineUnknown_GetPrivateData( struct NineUnknown *This,
120                             REFGUID refguid,
121                             void *pData,
122                             DWORD *pSizeOfData );
123 
124 HRESULT NINE_WINAPI
125 NineUnknown_FreePrivateData( struct NineUnknown *This,
126                              REFGUID refguid );
127 
128 /*** Nine private methods ***/
129 
130 static inline void
NineUnknown_Destroy(struct NineUnknown * This)131 NineUnknown_Destroy( struct NineUnknown *This )
132 {
133     assert(!(This->refs | This->bind));
134     This->dtor(This);
135 }
136 
137 static inline UINT
NineUnknown_Bind(struct NineUnknown * This)138 NineUnknown_Bind( struct NineUnknown *This )
139 {
140     UINT b = p_atomic_inc_return(&This->bind);
141     assert(b);
142 
143     if (b == 1 && This->forward)
144         NineUnknown_Bind(This->container);
145 
146     return b;
147 }
148 
149 static inline UINT
NineUnknown_Unbind(struct NineUnknown * This)150 NineUnknown_Unbind( struct NineUnknown *This )
151 {
152     UINT b = p_atomic_dec_return(&This->bind);
153 
154     if (b == 0 && This->forward)
155         NineUnknown_Unbind(This->container);
156     else if (b == 0 && This->refs == 0 && !This->container)
157         This->dtor(This);
158 
159     return b;
160 }
161 
162 static inline void
NineUnknown_ConvertRefToBind(struct NineUnknown * This)163 NineUnknown_ConvertRefToBind( struct NineUnknown *This )
164 {
165     NineUnknown_Bind(This);
166     NineUnknown_Release(This);
167 }
168 
169 /* Detach from container. */
170 static inline void
NineUnknown_Detach(struct NineUnknown * This)171 NineUnknown_Detach( struct NineUnknown *This )
172 {
173     assert(This->container && !This->forward);
174 
175     This->container = NULL;
176     if (!(This->refs | This->bind))
177         This->dtor(This);
178 }
179 
180 #endif /* _NINE_IUNKNOWN_H_ */
181