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
38struct Nine9;
39struct NineDevice9;
40
41struct 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};
67static inline struct NineUnknown *
68NineUnknown( void *data )
69{
70    return (struct NineUnknown *)data;
71}
72
73/* Use this instead of a shitload of arguments: */
74struct 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
84HRESULT
85NineUnknown_ctor( struct NineUnknown *This,
86                  struct NineUnknownParams *pParams );
87
88void
89NineUnknown_dtor( struct NineUnknown *This );
90
91/*** Direct3D public methods ***/
92
93HRESULT NINE_WINAPI
94NineUnknown_QueryInterface( struct NineUnknown *This,
95                            REFIID riid,
96                            void **ppvObject );
97
98ULONG NINE_WINAPI
99NineUnknown_AddRef( struct NineUnknown *This );
100
101ULONG NINE_WINAPI
102NineUnknown_Release( struct NineUnknown *This );
103
104ULONG NINE_WINAPI
105NineUnknown_ReleaseWithDtorLock( struct NineUnknown *This );
106
107HRESULT NINE_WINAPI
108NineUnknown_GetDevice( struct NineUnknown *This,
109                       IDirect3DDevice9 **ppDevice );
110
111HRESULT NINE_WINAPI
112NineUnknown_SetPrivateData( struct NineUnknown *This,
113                            REFGUID refguid,
114                            const void *pData,
115                            DWORD SizeOfData,
116                            DWORD Flags );
117
118HRESULT NINE_WINAPI
119NineUnknown_GetPrivateData( struct NineUnknown *This,
120                            REFGUID refguid,
121                            void *pData,
122                            DWORD *pSizeOfData );
123
124HRESULT NINE_WINAPI
125NineUnknown_FreePrivateData( struct NineUnknown *This,
126                             REFGUID refguid );
127
128/*** Nine private methods ***/
129
130static inline void
131NineUnknown_Destroy( struct NineUnknown *This )
132{
133    assert(!(This->refs | This->bind));
134    This->dtor(This);
135}
136
137static inline UINT
138NineUnknown_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
149static inline UINT
150NineUnknown_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
162static inline void
163NineUnknown_ConvertRefToBind( struct NineUnknown *This )
164{
165    NineUnknown_Bind(This);
166    NineUnknown_Release(This);
167}
168
169/* Detach from container. */
170static inline void
171NineUnknown_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