wined3d: Fix the relation between surfaces and their container.
[wine] / dlls / d3d9 / surface.c
1 /*
2  * IDirect3DSurface9 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 /* IDirect3DSurface9 IUnknown parts follow: */
28 HRESULT WINAPI IDirect3DSurface9Impl_QueryInterface(LPDIRECT3DSURFACE9 iface, REFIID riid, LPVOID* ppobj) {
29     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
30
31     if (IsEqualGUID(riid, &IID_IUnknown)
32         || IsEqualGUID(riid, &IID_IDirect3DResource9)
33         || IsEqualGUID(riid, &IID_IDirect3DSurface9)) {
34         IUnknown_AddRef(iface);
35         *ppobj = This;
36         return D3D_OK;
37     }
38
39     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
40     return E_NOINTERFACE;
41 }
42
43 ULONG WINAPI IDirect3DSurface9Impl_AddRef(LPDIRECT3DSURFACE9 iface) {
44     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)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 %ld\n", This, ref - 1);
58
59         return ref;
60     }
61
62 }
63
64 ULONG WINAPI IDirect3DSurface9Impl_Release(LPDIRECT3DSURFACE9 iface) {
65     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
66     IUnknown *containerParent = NULL;
67
68     TRACE("(%p)\n", This);
69
70     IWineD3DSurface_GetContainerParent(This->wineD3DSurface, &containerParent);
71     if (containerParent) {
72         /* Forward to the containerParent */
73         TRACE("(%p) : Forwarding to %p\n", This, containerParent);
74         return IUnknown_Release(containerParent);
75     } else {
76         /* No container, handle our own refcounting */
77         ULONG ref = InterlockedDecrement(&This->ref);
78         TRACE("(%p) : ReleaseRef to %ld\n", This, ref);
79
80         if (ref == 0) {
81             IWineD3DSurface_Release(This->wineD3DSurface);
82             HeapFree(GetProcessHeap(), 0, This);
83         }
84
85         return ref;
86     }
87 }
88
89 /* IDirect3DSurface9 IDirect3DResource9 Interface follow: */
90 HRESULT WINAPI IDirect3DSurface9Impl_GetDevice(LPDIRECT3DSURFACE9 iface, IDirect3DDevice9** ppDevice) {
91     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
92     return IDirect3DResource9Impl_GetDevice((LPDIRECT3DRESOURCE9) This, ppDevice);
93 }
94
95 HRESULT WINAPI IDirect3DSurface9Impl_SetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
96     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
97     TRACE("(%p) Relay\n", This);
98     return IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
99 }
100
101 HRESULT WINAPI IDirect3DSurface9Impl_GetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
102     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
103     TRACE("(%p) Relay\n", This);
104     return IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
105 }
106
107 HRESULT WINAPI IDirect3DSurface9Impl_FreePrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid) {
108     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
109     TRACE("(%p) Relay\n", This);
110     return IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
111 }
112
113 DWORD WINAPI IDirect3DSurface9Impl_SetPriority(LPDIRECT3DSURFACE9 iface, DWORD PriorityNew) {
114     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
115     TRACE("(%p) Relay\n", This);
116     return IWineD3DSurface_SetPriority(This->wineD3DSurface, PriorityNew);
117 }
118
119 DWORD WINAPI IDirect3DSurface9Impl_GetPriority(LPDIRECT3DSURFACE9 iface) {
120     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
121     TRACE("(%p) Relay\n", This);
122     return IWineD3DSurface_GetPriority(This->wineD3DSurface);
123 }
124
125 void WINAPI IDirect3DSurface9Impl_PreLoad(LPDIRECT3DSURFACE9 iface) {
126     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
127     TRACE("(%p) Relay\n", This);
128     IWineD3DSurface_PreLoad(This->wineD3DSurface);
129     return ;
130 }
131
132 D3DRESOURCETYPE WINAPI IDirect3DSurface9Impl_GetType(LPDIRECT3DSURFACE9 iface) {
133     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
134     TRACE("(%p) Relay\n", This);
135     return IWineD3DSurface_GetType(This->wineD3DSurface);
136 }
137
138 /* IDirect3DSurface9 Interface follow: */
139 HRESULT WINAPI IDirect3DSurface9Impl_GetContainer(LPDIRECT3DSURFACE9 iface, REFIID riid, void** ppContainer) {
140     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
141     IWineD3DBase *wineD3DContainer = NULL;
142     IUnknown *wineD3DContainerParent = NULL;
143     HRESULT res;
144
145     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
146
147     if (!ppContainer) {
148         ERR("Called without a valid ppContainer\n");
149     }
150
151     /* Get the WineD3D container. */
152     res = IWineD3DSurface_GetContainer(This->wineD3DSurface, &IID_IWineD3DBase, (void **)&wineD3DContainer);
153     if (res != D3D_OK) return res;
154
155     if (!wineD3DContainer) {
156         ERR("IWineD3DSurface_GetContainer should never return NULL\n");
157     }
158
159     /* Get the parent */
160     IWineD3DBase_GetParent(wineD3DContainer, &wineD3DContainerParent);
161     IUnknown_Release(wineD3DContainer);
162
163     if (!wineD3DContainerParent) {
164         ERR("IWineD3DBase_GetParent should never return NULL\n");
165     }
166
167     /* Now, query the interface of the parent for the riid */
168     res = IUnknown_QueryInterface(wineD3DContainerParent, riid, ppContainer);
169     IUnknown_Release(wineD3DContainerParent);
170
171     TRACE("Returning ppContainer %p, *ppContainer %p\n", ppContainer, *ppContainer);
172
173     return res;
174 }
175
176 HRESULT WINAPI IDirect3DSurface9Impl_GetDesc(LPDIRECT3DSURFACE9 iface, D3DSURFACE_DESC* pDesc) {
177     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
178     WINED3DSURFACE_DESC    wined3ddesc;
179     UINT                   tmpInt = -1;
180     TRACE("(%p) Relay\n", This);
181
182     /* As d3d8 and d3d9 structures differ, pass in ptrs to where data needs to go */
183     wined3ddesc.Format              = (WINED3DFORMAT *)&pDesc->Format;
184     wined3ddesc.Type                = &pDesc->Type;
185     wined3ddesc.Usage               = &pDesc->Usage;
186     wined3ddesc.Pool                = &pDesc->Pool;
187     wined3ddesc.Size                = &tmpInt;
188     wined3ddesc.MultiSampleType     = &pDesc->MultiSampleType;
189     wined3ddesc.MultiSampleQuality  = &pDesc->MultiSampleQuality;
190     wined3ddesc.Width               = &pDesc->Width;
191     wined3ddesc.Height              = &pDesc->Height;
192
193     return IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
194 }
195
196 HRESULT WINAPI IDirect3DSurface9Impl_LockRect(LPDIRECT3DSURFACE9 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
197     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
198     TRACE("(%p) Relay\n", This);
199     TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %ld\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
200     return IWineD3DSurface_LockRect(This->wineD3DSurface, pLockedRect, pRect, Flags);
201 }
202
203 HRESULT WINAPI IDirect3DSurface9Impl_UnlockRect(LPDIRECT3DSURFACE9 iface) {
204     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
205     TRACE("(%p) Relay\n", This);
206     return IWineD3DSurface_UnlockRect(This->wineD3DSurface);
207 }
208
209 HRESULT WINAPI IDirect3DSurface9Impl_GetDC(LPDIRECT3DSURFACE9 iface, HDC* phdc) {
210     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
211     TRACE("(%p) Relay\n", This);
212     return IWineD3DSurface_GetDC(This->wineD3DSurface, phdc);
213 }
214
215 HRESULT WINAPI IDirect3DSurface9Impl_ReleaseDC(LPDIRECT3DSURFACE9 iface, HDC hdc) {
216     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
217     TRACE("(%p) Relay\n", This);
218     return IWineD3DSurface_ReleaseDC(This->wineD3DSurface, hdc);
219 }
220
221
222 const IDirect3DSurface9Vtbl Direct3DSurface9_Vtbl =
223 {
224     /* IUnknown */
225     IDirect3DSurface9Impl_QueryInterface,
226     IDirect3DSurface9Impl_AddRef,
227     IDirect3DSurface9Impl_Release,
228     /* IDirect3DResource9 */
229     IDirect3DSurface9Impl_GetDevice,
230     IDirect3DSurface9Impl_SetPrivateData,
231     IDirect3DSurface9Impl_GetPrivateData,
232     IDirect3DSurface9Impl_FreePrivateData,
233     IDirect3DSurface9Impl_SetPriority,
234     IDirect3DSurface9Impl_GetPriority,
235     IDirect3DSurface9Impl_PreLoad,
236     IDirect3DSurface9Impl_GetType,
237     /* IDirect3DSurface9 */
238     IDirect3DSurface9Impl_GetContainer,
239     IDirect3DSurface9Impl_GetDesc,
240     IDirect3DSurface9Impl_LockRect,
241     IDirect3DSurface9Impl_UnlockRect,
242     IDirect3DSurface9Impl_GetDC,
243     IDirect3DSurface9Impl_ReleaseDC
244 };