st/nine: Add asserts for output/input packing
[mesa.git] / src / gallium / state_trackers / nine / iunknown.c
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 #include "iunknown.h"
24 #include "util/u_atomic.h"
25 #include "nine_helpers.h"
26
27 #define DBG_CHANNEL DBG_UNKNOWN
28
29 HRESULT
30 NineUnknown_ctor( struct NineUnknown *This,
31 struct NineUnknownParams *pParams )
32 {
33 This->refs = pParams->container ? 0 : 1;
34 This->bind = 0;
35 This->forward = !This->refs;
36 This->container = pParams->container;
37 This->device = pParams->device;
38 if (This->refs && This->device)
39 NineUnknown_AddRef(NineUnknown(This->device));
40
41 This->vtable = pParams->vtable;
42 This->vtable_internal = pParams->vtable;
43 This->guids = pParams->guids;
44 This->dtor = pParams->dtor;
45
46 return D3D_OK;
47 }
48
49 void
50 NineUnknown_dtor( struct NineUnknown *This )
51 {
52 if (This->refs && This->device) /* Possible only if early exit after a ctor failed */
53 (void) NineUnknown_Release(NineUnknown(This->device));
54 FREE(This);
55 }
56
57 HRESULT NINE_WINAPI
58 NineUnknown_QueryInterface( struct NineUnknown *This,
59 REFIID riid,
60 void **ppvObject )
61 {
62 unsigned i = 0;
63
64 DBG("This=%p riid=%p ppvObject=%p\n", This, riid, ppvObject);
65
66 if (!ppvObject) return E_POINTER;
67
68 do {
69 if (GUID_equal(This->guids[i], riid)) {
70 *ppvObject = This;
71 assert(This->refs);
72 NineUnknown_AddRef(This);
73 return S_OK;
74 }
75 } while (This->guids[++i]);
76
77 *ppvObject = NULL;
78 return E_NOINTERFACE;
79 }
80
81 ULONG NINE_WINAPI
82 NineUnknown_AddRef( struct NineUnknown *This )
83 {
84 ULONG r;
85 if (This->forward)
86 return NineUnknown_AddRef(This->container);
87 else
88 r = p_atomic_inc_return(&This->refs);
89
90 if (r == 1) {
91 if (This->device)
92 NineUnknown_AddRef(NineUnknown(This->device));
93 /* This shouldn't be necessary:
94 if (This->container)
95 NineUnknown_Bind(NineUnknown(This->container)); */
96 }
97 return r;
98 }
99
100 ULONG NINE_WINAPI
101 NineUnknown_Release( struct NineUnknown *This )
102 {
103 if (This->forward)
104 return NineUnknown_Release(This->container);
105
106 ULONG r = p_atomic_dec_return(&This->refs);
107
108 if (r == 0) {
109 if (This->device) {
110 if (NineUnknown_Release(NineUnknown(This->device)) == 0)
111 return r; /* everything's gone */
112 }
113 if (This->container) {
114 /* NineUnknown_Unbind(NineUnknown(This->container)); */
115 } else
116 if (This->bind == 0) {
117 This->dtor(This);
118 }
119 }
120 return r;
121 }
122
123 HRESULT NINE_WINAPI
124 NineUnknown_GetDevice( struct NineUnknown *This,
125 IDirect3DDevice9 **ppDevice )
126 {
127 user_assert(ppDevice, E_POINTER);
128 NineUnknown_AddRef(NineUnknown(This->device));
129 *ppDevice = (IDirect3DDevice9 *)This->device;
130 return D3D_OK;
131 }