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 util_hash_table *pdata;
64 
65     void (*dtor)(void *data); /* top-level dtor */
66 };
67 static inline struct NineUnknown *
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 };
82 
83 HRESULT
84 NineUnknown_ctor( struct NineUnknown *This,
85                   struct NineUnknownParams *pParams );
86 
87 void
88 NineUnknown_dtor( struct NineUnknown *This );
89 
90 /*** Direct3D public methods ***/
91 
92 HRESULT NINE_WINAPI
93 NineUnknown_QueryInterface( struct NineUnknown *This,
94                             REFIID riid,
95                             void **ppvObject );
96 
97 ULONG NINE_WINAPI
98 NineUnknown_AddRef( struct NineUnknown *This );
99 
100 ULONG NINE_WINAPI
101 NineUnknown_Release( struct NineUnknown *This );
102 
103 ULONG NINE_WINAPI
104 NineUnknown_ReleaseWithDtorLock( struct NineUnknown *This );
105 
106 HRESULT NINE_WINAPI
107 NineUnknown_GetDevice( struct NineUnknown *This,
108                        IDirect3DDevice9 **ppDevice );
109 
110 HRESULT NINE_WINAPI
111 NineUnknown_SetPrivateData( struct NineUnknown *This,
112                             REFGUID refguid,
113                             const void *pData,
114                             DWORD SizeOfData,
115                             DWORD Flags );
116 
117 HRESULT NINE_WINAPI
118 NineUnknown_GetPrivateData( struct NineUnknown *This,
119                             REFGUID refguid,
120                             void *pData,
121                             DWORD *pSizeOfData );
122 
123 HRESULT NINE_WINAPI
124 NineUnknown_FreePrivateData( struct NineUnknown *This,
125                              REFGUID refguid );
126 
127 /*** Nine private methods ***/
128 
129 static inline void
130 NineUnknown_Destroy( struct NineUnknown *This )
131 {
132     assert(!(This->refs | This->bind));
133     This->dtor(This);
134 }
135 
136 static inline UINT
137 NineUnknown_Bind( struct NineUnknown *This )
138 {
139     UINT b = p_atomic_inc_return(&This->bind);
140     assert(b);
141 
142     if (b == 1 && This->forward)
143         NineUnknown_Bind(This->container);
144 
145     return b;
146 }
147 
148 static inline UINT
149 NineUnknown_Unbind( struct NineUnknown *This )
150 {
151     UINT b = p_atomic_dec_return(&This->bind);
152 
153     if (b == 0 && This->forward)
154         NineUnknown_Unbind(This->container);
155     else if (b == 0 && This->refs == 0 && !This->container)
156         This->dtor(This);
157 
158     return b;
159 }
160 
161 static inline void
162 NineUnknown_ConvertRefToBind( struct NineUnknown *This )
163 {
164     NineUnknown_Bind(This);
165     NineUnknown_Release(This);
166 }
167 
168 /* Detach from container. */
169 static inline void
170 NineUnknown_Detach( struct NineUnknown *This )
171 {
172     assert(This->container && !This->forward);
173 
174     This->container = NULL;
175     if (!(This->refs | This->bind))
176         This->dtor(This);
177 }
178 
179 #endif /* _NINE_IUNKNOWN_H_ */
180