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