st/nine: Unset vtxbuf on reset
[mesa.git] / src / gallium / state_trackers / nine / iunknown.h
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_memory.h"
29
30 #include "guid.h"
31 #include "nine_debug.h"
32 #include "nine_quirk.h"
33
34 #include "d3d9.h"
35
36 struct Nine9;
37 struct NineDevice9;
38
39 struct NineUnknown
40 {
41 /* pointer to vtable */
42 void *vtable;
43
44 int32_t refs; /* external reference count */
45 int32_t bind; /* internal bind count */
46 boolean forward; /* whether to forward references to the container */
47
48 struct NineUnknown *container; /* referenced if (refs | bind) */
49 struct NineDevice9 *device; /* referenced if (refs) */
50
51 const GUID **guids; /* for QueryInterface */
52
53 void (*dtor)(void *data); /* top-level dtor */
54 };
55 static inline struct NineUnknown *
56 NineUnknown( void *data )
57 {
58 return (struct NineUnknown *)data;
59 }
60
61 /* Use this instead of a shitload of arguments: */
62 struct NineUnknownParams
63 {
64 void *vtable;
65 const GUID **guids;
66 void (*dtor)(void *data);
67 struct NineUnknown *container;
68 struct NineDevice9 *device;
69 };
70
71 HRESULT
72 NineUnknown_ctor( struct NineUnknown *This,
73 struct NineUnknownParams *pParams );
74
75 void
76 NineUnknown_dtor( struct NineUnknown *This );
77
78 /*** Direct3D public methods ***/
79
80 HRESULT WINAPI
81 NineUnknown_QueryInterface( struct NineUnknown *This,
82 REFIID riid,
83 void **ppvObject );
84
85 ULONG WINAPI
86 NineUnknown_AddRef( struct NineUnknown *This );
87
88 ULONG WINAPI
89 NineUnknown_Release( struct NineUnknown *This );
90
91 HRESULT WINAPI
92 NineUnknown_GetDevice( struct NineUnknown *This,
93 IDirect3DDevice9 **ppDevice );
94
95 /*** Nine private methods ***/
96
97 static inline void
98 NineUnknown_Destroy( struct NineUnknown *This )
99 {
100 assert(!(This->refs | This->bind));
101 This->dtor(This);
102 }
103
104 static inline UINT
105 NineUnknown_Bind( struct NineUnknown *This )
106 {
107 UINT b = ++This->bind;
108 assert(b);
109 if (b == 1 && This->container) {
110 if (This->container != NineUnknown(This->device))
111 NineUnknown_Bind(This->container);
112 }
113 return b;
114 }
115
116 static inline UINT
117 NineUnknown_Unbind( struct NineUnknown *This )
118 {
119 UINT b = --This->bind;
120 if (!b) {
121 if (This->container) {
122 if (This->container != NineUnknown(This->device))
123 NineUnknown_Unbind(This->container);
124 } else
125 if (This->refs == 0) {
126 This->dtor(This);
127 }
128 }
129 return b;
130 }
131
132 static inline void
133 NineUnknown_ConvertRefToBind( struct NineUnknown *This )
134 {
135 NineUnknown_Bind(This);
136 NineUnknown_Release(This);
137 }
138
139 /* Detach from container. */
140 static inline void
141 NineUnknown_Detach( struct NineUnknown *This )
142 {
143 assert(This->container && !This->forward);
144 if (This->refs)
145 NineUnknown_Unbind(This->container);
146 if (This->bind)
147 NineUnknown_Unbind(This->container);
148 This->container = NULL;
149 if (!(This->refs | This->bind))
150 This->dtor(This);
151 }
152
153 #endif /* _NINE_IUNKNOWN_H_ */