d3d9: Fix implicit swap chain refcounting.
[wine] / dlls / d3d9 / volume.c
1 /*
2  * IDirect3DVolume9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  *                     Raphael Junqueira
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 /* IDirect3DVolume9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3DVolume9Impl_QueryInterface(LPDIRECT3DVOLUME9 iface, REFIID riid, LPVOID* ppobj) {
29     IDirect3DVolume9Impl *This = (IDirect3DVolume9Impl *)iface;
30
31     if (IsEqualGUID(riid, &IID_IUnknown)
32         || IsEqualGUID(riid, &IID_IDirect3DVolume9)) {
33         IUnknown_AddRef(iface);
34         *ppobj = This;
35         return S_OK;
36     }
37
38     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
39     *ppobj = NULL;
40     return E_NOINTERFACE;
41 }
42
43 static ULONG WINAPI IDirect3DVolume9Impl_AddRef(LPDIRECT3DVOLUME9 iface) {
44     IDirect3DVolume9Impl *This = (IDirect3DVolume9Impl *)iface;
45
46     TRACE("(%p)\n", This);
47
48     if (This->forwardReference) {
49         /* Forward refcounting */
50         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
51         return IUnknown_AddRef(This->forwardReference);
52     } else {
53         /* No container, handle our own refcounting */
54         ULONG ref = InterlockedIncrement(&This->ref);
55         TRACE("(%p) : AddRef from %d\n", This, ref - 1);
56         return ref;
57     }
58 }
59
60 static ULONG WINAPI IDirect3DVolume9Impl_Release(LPDIRECT3DVOLUME9 iface) {
61     IDirect3DVolume9Impl *This = (IDirect3DVolume9Impl *)iface;
62
63     TRACE("(%p)\n", This);
64
65     if (This->forwardReference) {
66         /* Forward refcounting */
67         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
68         return IUnknown_Release(This->forwardReference);
69     } else {
70         /* No container, handle our own refcounting */
71         ULONG ref = InterlockedDecrement(&This->ref);
72         TRACE("(%p) : ReleaseRef to %d\n", This, ref);
73
74         if (ref == 0) {
75             IWineD3DVolume_Release(This->wineD3DVolume);
76             HeapFree(GetProcessHeap(), 0, This);
77         }
78
79         return ref;
80     }
81 }
82
83 /* IDirect3DVolume9 Interface follow: */
84 static HRESULT WINAPI IDirect3DVolume9Impl_GetDevice(LPDIRECT3DVOLUME9 iface, IDirect3DDevice9** ppDevice) {
85     IDirect3DVolume9Impl *This = (IDirect3DVolume9Impl *)iface;
86     IWineD3DDevice       *myDevice = NULL;
87
88     IWineD3DVolume_GetDevice(This->wineD3DVolume, &myDevice);
89     IWineD3DDevice_GetParent(myDevice, (IUnknown **)ppDevice);
90     IWineD3DDevice_Release(myDevice);
91     return D3D_OK;
92 }
93
94 static HRESULT WINAPI IDirect3DVolume9Impl_SetPrivateData(LPDIRECT3DVOLUME9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
95     IDirect3DVolume9Impl *This = (IDirect3DVolume9Impl *)iface;
96     TRACE("(%p) Relay\n", This);
97     return IWineD3DVolume_SetPrivateData(This->wineD3DVolume, refguid, pData, SizeOfData, Flags);
98 }
99
100 static HRESULT WINAPI IDirect3DVolume9Impl_GetPrivateData(LPDIRECT3DVOLUME9 iface, REFGUID  refguid, void* pData, DWORD* pSizeOfData) {
101     IDirect3DVolume9Impl *This = (IDirect3DVolume9Impl *)iface;
102     TRACE("(%p) Relay\n", This);
103     return IWineD3DVolume_GetPrivateData(This->wineD3DVolume, refguid, pData, pSizeOfData);
104 }
105
106 static HRESULT WINAPI IDirect3DVolume9Impl_FreePrivateData(LPDIRECT3DVOLUME9 iface, REFGUID refguid) {
107     IDirect3DVolume9Impl *This = (IDirect3DVolume9Impl *)iface;
108     TRACE("(%p) Relay\n", This);
109     return IWineD3DVolume_FreePrivateData(This->wineD3DVolume, refguid);
110 }
111
112 static HRESULT WINAPI IDirect3DVolume9Impl_GetContainer(LPDIRECT3DVOLUME9 iface, REFIID riid, void** ppContainer) {
113     IDirect3DVolume9Impl *This = (IDirect3DVolume9Impl *)iface;
114     IWineD3DBase *wineD3DContainer = NULL;
115     IUnknown *wineD3DContainerParent = NULL;
116     HRESULT res;
117
118     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
119
120     if (!ppContainer) {
121         ERR("Called without a valid ppContainer.\n");
122     }
123
124     /* Get the WineD3D container. */
125     res = IWineD3DVolume_GetContainer(This->wineD3DVolume, &IID_IWineD3DBase, (void **)&wineD3DContainer);
126     if (res != D3D_OK) return res;
127
128     if (!wineD3DContainer) {
129         ERR("IWineD3DSurface_GetContainer should never return NULL\n");
130     }
131
132     /* Get the parent */
133     IWineD3DBase_GetParent(wineD3DContainer, &wineD3DContainerParent);
134     IUnknown_Release(wineD3DContainer);
135
136     if (!wineD3DContainerParent) {
137         ERR("IWineD3DBase_GetParent should never return NULL\n");
138     }
139
140     /* Now, query the interface of the parent for the riid */
141     res = IUnknown_QueryInterface(wineD3DContainerParent, riid, ppContainer);
142     IUnknown_Release(wineD3DContainerParent);
143
144     TRACE("Returning ppContainer %p, *ppContainer %p\n", ppContainer, *ppContainer);
145
146     return res;
147 }
148
149 static HRESULT WINAPI IDirect3DVolume9Impl_GetDesc(LPDIRECT3DVOLUME9 iface, D3DVOLUME_DESC* pDesc) {
150     IDirect3DVolume9Impl *This = (IDirect3DVolume9Impl *)iface;
151     WINED3DVOLUME_DESC     wined3ddesc;
152     UINT                   tmpInt = -1;
153
154     TRACE("(%p) Relay\n", This);
155
156     /* As d3d8 and d3d9 structures differ, pass in ptrs to where data needs to go */
157     wined3ddesc.Format              = (WINED3DFORMAT *)&pDesc->Format;
158     wined3ddesc.Type                = (WINED3DRESOURCETYPE *)&pDesc->Type;
159     wined3ddesc.Usage               = &pDesc->Usage;
160     wined3ddesc.Pool                = (WINED3DPOOL *) &pDesc->Pool;
161     wined3ddesc.Size                = &tmpInt;
162     wined3ddesc.Width               = &pDesc->Width;
163     wined3ddesc.Height              = &pDesc->Height;
164     wined3ddesc.Depth               = &pDesc->Depth;
165
166     return IWineD3DVolume_GetDesc(This->wineD3DVolume, &wined3ddesc);
167 }
168
169 static HRESULT WINAPI IDirect3DVolume9Impl_LockBox(LPDIRECT3DVOLUME9 iface, D3DLOCKED_BOX* pLockedVolume, CONST D3DBOX* pBox, DWORD Flags) {
170     IDirect3DVolume9Impl *This = (IDirect3DVolume9Impl *)iface;
171     TRACE("(%p) relay %p %p %p %d\n", This, This->wineD3DVolume, pLockedVolume, pBox, Flags);
172     return IWineD3DVolume_LockBox(This->wineD3DVolume, (WINED3DLOCKED_BOX *)pLockedVolume, (CONST WINED3DBOX *)pBox, Flags);
173 }
174
175 static HRESULT WINAPI IDirect3DVolume9Impl_UnlockBox(LPDIRECT3DVOLUME9 iface) {
176     IDirect3DVolume9Impl *This = (IDirect3DVolume9Impl *)iface;
177     TRACE("(%p) relay %p\n", This, This->wineD3DVolume);
178     return IWineD3DVolume_UnlockBox(This->wineD3DVolume);
179 }
180
181 static const IDirect3DVolume9Vtbl Direct3DVolume9_Vtbl =
182 {
183     /* IUnknown */
184     IDirect3DVolume9Impl_QueryInterface,
185     IDirect3DVolume9Impl_AddRef,
186     IDirect3DVolume9Impl_Release,
187     /* IDirect3DVolume9 */
188     IDirect3DVolume9Impl_GetDevice,
189     IDirect3DVolume9Impl_SetPrivateData,
190     IDirect3DVolume9Impl_GetPrivateData,
191     IDirect3DVolume9Impl_FreePrivateData,
192     IDirect3DVolume9Impl_GetContainer,
193     IDirect3DVolume9Impl_GetDesc,
194     IDirect3DVolume9Impl_LockBox,
195     IDirect3DVolume9Impl_UnlockBox
196 };
197
198
199 /* Internal function called back during the CreateVolumeTexture */
200 HRESULT WINAPI D3D9CB_CreateVolume(IUnknown  *pDevice, IUnknown *pSuperior, UINT Width, UINT Height, UINT Depth,
201                                    WINED3DFORMAT  Format, WINED3DPOOL Pool, DWORD Usage,
202                                    IWineD3DVolume **ppVolume,
203                                    HANDLE   * pSharedHandle) {
204     IDirect3DVolume9Impl *object;
205     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)pDevice;
206     HRESULT hrc = D3D_OK;
207     
208     /* Allocate the storage for the device */
209     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVolume9Impl));
210     if (NULL == object) {
211         FIXME("Allocation of memory failed\n");
212         *ppVolume = NULL;
213         return D3DERR_OUTOFVIDEOMEMORY;
214     }
215
216     object->lpVtbl = &Direct3DVolume9_Vtbl;
217     object->ref = 1;
218     hrc = IWineD3DDevice_CreateVolume(This->WineD3DDevice, Width, Height, Depth, Usage & WINED3DUSAGE_MASK, Format, 
219                                        Pool, &object->wineD3DVolume, pSharedHandle, (IUnknown *)object);
220     if (hrc != D3D_OK) {
221         /* free up object */ 
222         FIXME("(%p) call to IWineD3DDevice_CreateVolume failed\n", This);
223         HeapFree(GetProcessHeap(), 0, object);
224         *ppVolume = NULL;
225     } else {
226         *ppVolume = (IWineD3DVolume *)object->wineD3DVolume;
227         object->forwardReference = pSuperior;
228     }
229     TRACE("(%p) Created volume %p\n", This, *ppVolume);
230     return hrc;
231 }
232
233 ULONG WINAPI D3D9CB_DestroyVolume(IWineD3DVolume *pVolume) {
234     IDirect3DVolume9Impl* volumeParent;
235
236     IWineD3DVolume_GetParent(pVolume, (IUnknown **) &volumeParent);
237     /* GetParent's AddRef was forwarded to an object in destruction.
238      * Releasing it here again would cause an endless recursion. */
239     volumeParent->forwardReference = NULL;
240     return IDirect3DVolume9_Release((IDirect3DVolume9*) volumeParent);
241 }