Revert "d3d: Remove AddRef from IWineD3DDevice_GetBackBuffer.".
[wine] / dlls / d3d8 / surface.c
1 /*
2  * IDirect3DSurface8 implementation
3  *
4  * Copyright 2005 Oliver Stieber
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "d3d8_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
25
26 /* IDirect3DSurface8 IUnknown parts follow: */
27 static HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(LPDIRECT3DSURFACE8 iface, REFIID riid, LPVOID *ppobj) {
28     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
29
30     if (IsEqualGUID(riid, &IID_IUnknown)
31         || IsEqualGUID(riid, &IID_IDirect3DResource8)
32         || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
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 IDirect3DSurface8Impl_AddRef(LPDIRECT3DSURFACE8 iface) {
44     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
45     IUnknown *containerParent = NULL;
46
47     TRACE("(%p)\n", This);
48
49     IWineD3DSurface_GetContainerParent(This->wineD3DSurface, &containerParent);
50     if (containerParent) {
51         /* Forward to the containerParent */
52         TRACE("(%p) : Forwarding to %p\n", This, containerParent);
53         return IUnknown_AddRef(containerParent);
54     } else {
55         /* No container, handle our own refcounting */
56         ULONG ref = InterlockedIncrement(&This->ref);
57         TRACE("(%p) : AddRef from %d\n", This, ref - 1);
58         return ref;
59     }
60 }
61
62 static ULONG WINAPI IDirect3DSurface8Impl_Release(LPDIRECT3DSURFACE8 iface) {
63     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
64     IUnknown *containerParent = NULL;
65
66     TRACE("(%p)\n", This);
67
68     IWineD3DSurface_GetContainerParent(This->wineD3DSurface, &containerParent);
69     if (containerParent) {
70         /* Forward to the containerParent */
71         TRACE("(%p) : Forwarding to %p\n", This, containerParent);
72         return IUnknown_Release(containerParent);
73     } else {
74         /* No container, handle our own refcounting */
75         ULONG ref = InterlockedDecrement(&This->ref);
76         TRACE("(%p) : ReleaseRef to %d\n", This, ref);
77
78         if (ref == 0) {
79             IWineD3DSurface_Release(This->wineD3DSurface);
80             if (This->parentDevice) IUnknown_Release(This->parentDevice);
81             HeapFree(GetProcessHeap(), 0, This);
82         }
83
84         return ref;
85     }
86 }
87
88 /* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
89 static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(LPDIRECT3DSURFACE8 iface, IDirect3DDevice8 **ppDevice) {
90     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
91     return IDirect3DResource8Impl_GetDevice((LPDIRECT3DRESOURCE8) This, ppDevice);
92 }
93
94 static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void *pData, DWORD SizeOfData, DWORD Flags) {
95     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
96     TRACE("(%p) Relay\n", This);
97     return IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
98 }
99
100 static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
101     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
102     TRACE("(%p) Relay\n", This);
103     return IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
104 }
105
106 static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
107     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
108     TRACE("(%p) Relay\n", This);
109     return IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
110 }
111
112 /* IDirect3DSurface8 Interface follow: */
113 static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void **ppContainer) {
114     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
115     HRESULT res;
116     IUnknown *IWineContainer = NULL;
117
118     TRACE("(%p) Relay\n", This);
119
120     /* The container returned from IWineD3DSurface_GetContainer is either an IWineD3DDevice,
121        one of the subclasses of IWineD3DBaseTexture or an IWineD3DSwapChain  */
122     /* Get the IUnknown container. */
123     res = IWineD3DSurface_GetContainer(This->wineD3DSurface, &IID_IUnknown, (void **)&IWineContainer);
124     if (res == D3D_OK && IWineContainer != NULL) {
125
126         /* Now find out what kind of container it is (so that we can get its parent)*/
127         IUnknown  *IUnknownParent = NULL;
128         IUnknown  *myContainer    = NULL;
129         if (D3D_OK == IUnknown_QueryInterface(IWineContainer, &IID_IWineD3DDevice, (void **)&myContainer)) {
130             IWineD3DDevice_GetParent((IWineD3DDevice *)IWineContainer, &IUnknownParent);
131             IUnknown_Release(myContainer);
132         } else 
133         if (D3D_OK == IUnknown_QueryInterface(IWineContainer, &IID_IWineD3DBaseTexture, (void **)&myContainer)) {
134             IWineD3DBaseTexture_GetParent((IWineD3DBaseTexture *)IWineContainer, &IUnknownParent);
135             IUnknown_Release(myContainer);
136         } else
137         if (D3D_OK == IUnknown_QueryInterface(IWineContainer, &IID_IWineD3DSwapChain, (void **)&myContainer)) {
138             IWineD3DSwapChain_GetParent((IWineD3DSwapChain *)IWineContainer, &IUnknownParent);
139             IUnknown_Release(myContainer);
140         } else {
141             FIXME("Container is of unknown interface\n");
142         }
143         /* Tidy up.. */
144         IUnknown_Release((IWineD3DDevice *)IWineContainer);
145
146         /* Now, query the interface of the parent for the riid */
147         if (IUnknownParent != NULL) {
148             res = IUnknown_QueryInterface(IUnknownParent, riid, ppContainer);
149             /* Tidy up.. */
150             IUnknown_Release(IUnknownParent);
151         }
152     }
153
154     TRACE("(%p) : returning %p\n", This, *ppContainer);    
155     return res;
156 }
157
158 static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
159     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
160     WINED3DSURFACE_DESC    wined3ddesc;
161     TRACE("(%p) Relay\n", This);
162
163     /* As d3d8 and d3d9 structures differ, pass in ptrs to where data needs to go */
164     memset(&wined3ddesc, 0, sizeof(wined3ddesc));
165     wined3ddesc.Format              = (WINED3DFORMAT *)&pDesc->Format;
166     wined3ddesc.Type                = (WINED3DRESOURCETYPE *)&pDesc->Type;
167     wined3ddesc.Usage               = &pDesc->Usage;
168     wined3ddesc.Pool                = (WINED3DPOOL *) &pDesc->Pool;
169     wined3ddesc.Size                = &pDesc->Size;
170     wined3ddesc.MultiSampleType     = (WINED3DMULTISAMPLE_TYPE *) &pDesc->MultiSampleType;
171     wined3ddesc.Width               = &pDesc->Width;
172     wined3ddesc.Height              = &pDesc->Height;
173
174     return IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
175 }
176
177 static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT *pLockedRect, CONST RECT *pRect, DWORD Flags) {
178     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
179     TRACE("(%p) Relay\n", This);
180     TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %d\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
181     return IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
182 }
183
184 static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
185     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
186     TRACE("(%p) Relay\n", This);
187     return IWineD3DSurface_UnlockRect(This->wineD3DSurface);
188 }
189
190 const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
191 {
192     /* IUnknown */
193     IDirect3DSurface8Impl_QueryInterface,
194     IDirect3DSurface8Impl_AddRef,
195     IDirect3DSurface8Impl_Release,
196     /* IDirect3DResource8 */
197     IDirect3DSurface8Impl_GetDevice,
198     IDirect3DSurface8Impl_SetPrivateData,
199     IDirect3DSurface8Impl_GetPrivateData,
200     IDirect3DSurface8Impl_FreePrivateData,
201     /* IDirect3DSurface8 */
202     IDirect3DSurface8Impl_GetContainer,
203     IDirect3DSurface8Impl_GetDesc,
204     IDirect3DSurface8Impl_LockRect,
205     IDirect3DSurface8Impl_UnlockRect
206 };