ole32: Always rewind the stream in StdGlobalInterfaceTable_GetInterfaceFromGlobal
[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 %ld\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 %ld\n", This, ref);
77
78         if (ref == 0) {
79             IWineD3DSurface_Release(This->wineD3DSurface);
80             HeapFree(GetProcessHeap(), 0, This);
81         }
82
83         return ref;
84     }
85 }
86
87 /* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
88 static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(LPDIRECT3DSURFACE8 iface, IDirect3DDevice8 **ppDevice) {
89     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
90     return IDirect3DResource8Impl_GetDevice((LPDIRECT3DRESOURCE8) This, ppDevice);
91 }
92
93 static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void *pData, DWORD SizeOfData, DWORD Flags) {
94     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
95     TRACE("(%p) Relay\n", This);
96     return IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
97 }
98
99 static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
100     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
101     TRACE("(%p) Relay\n", This);
102     return IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
103 }
104
105 static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
106     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
107     TRACE("(%p) Relay\n", This);
108     return IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
109 }
110
111 /* IDirect3DSurface8 Interface follow: */
112 static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void **ppContainer) {
113     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
114     HRESULT res;
115     IUnknown *IWineContainer = NULL;
116
117     TRACE("(%p) Relay\n", This);
118
119     /* The container returned from IWineD3DSurface_GetContainer is either an IWineD3DDevice,
120        one of the subclasses of IWineD3DBaseTexture or an IWineD3DSwapChain  */
121     /* Get the IUnknown container. */
122     res = IWineD3DSurface_GetContainer(This->wineD3DSurface, &IID_IUnknown, (void **)&IWineContainer);
123     if (res == D3D_OK && IWineContainer != NULL) {
124
125         /* Now find out what kind of container it is (so that we can get its parent)*/
126         IUnknown  *IUnknownParent = NULL;
127         IUnknown  *myContainer    = NULL;
128         if (D3D_OK == IUnknown_QueryInterface(IWineContainer, &IID_IWineD3DDevice, (void **)&myContainer)) {
129             IWineD3DDevice_GetParent((IWineD3DDevice *)IWineContainer, &IUnknownParent);
130             IUnknown_Release(myContainer);
131         } else 
132         if (D3D_OK == IUnknown_QueryInterface(IWineContainer, &IID_IWineD3DBaseTexture, (void **)&myContainer)) {
133             IWineD3DBaseTexture_GetParent((IWineD3DBaseTexture *)IWineContainer, &IUnknownParent);
134             IUnknown_Release(myContainer);
135         } else
136         if (D3D_OK == IUnknown_QueryInterface(IWineContainer, &IID_IWineD3DSwapChain, (void **)&myContainer)) {
137             IWineD3DSwapChain_GetParent((IWineD3DSwapChain *)IWineContainer, &IUnknownParent);
138             IUnknown_Release(myContainer);
139         } else {
140             FIXME("Container is of unknown interface\n");
141         }
142         /* Tidy up.. */
143         IUnknown_Release((IWineD3DDevice *)IWineContainer);
144
145         /* Now, query the interface of the parent for the riid */
146         if (IUnknownParent != NULL) {
147             res = IUnknown_QueryInterface(IUnknownParent, riid, ppContainer);
148             /* Tidy up.. */
149             IUnknown_Release(IUnknownParent);
150         }
151     }
152
153     TRACE("(%p) : returning %p\n", This, *ppContainer);    
154     return res;
155 }
156
157 static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
158     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
159     WINED3DSURFACE_DESC    wined3ddesc;
160     TRACE("(%p) Relay\n", This);
161
162     /* As d3d8 and d3d9 structures differ, pass in ptrs to where data needs to go */
163     memset(&wined3ddesc, 0, sizeof(wined3ddesc));
164     wined3ddesc.Format              = (WINED3DFORMAT *)&pDesc->Format;
165     wined3ddesc.Type                = (WINED3DRESOURCETYPE *)&pDesc->Type;
166     wined3ddesc.Usage               = &pDesc->Usage;
167     wined3ddesc.Pool                = (WINED3DPOOL *) &pDesc->Pool;
168     wined3ddesc.Size                = &pDesc->Size;
169     wined3ddesc.MultiSampleType     = (WINED3DMULTISAMPLE_TYPE *) &pDesc->MultiSampleType;
170     wined3ddesc.Width               = &pDesc->Width;
171     wined3ddesc.Height              = &pDesc->Height;
172
173     return IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
174 }
175
176 static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT *pLockedRect, CONST RECT *pRect, DWORD Flags) {
177     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
178     TRACE("(%p) Relay\n", This);
179     TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %ld\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
180     return IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
181 }
182
183 static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
184     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
185     TRACE("(%p) Relay\n", This);
186     return IWineD3DSurface_UnlockRect(This->wineD3DSurface);
187 }
188
189 const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
190 {
191     /* IUnknown */
192     IDirect3DSurface8Impl_QueryInterface,
193     IDirect3DSurface8Impl_AddRef,
194     IDirect3DSurface8Impl_Release,
195     /* IDirect3DResource8 */
196     IDirect3DSurface8Impl_GetDevice,
197     IDirect3DSurface8Impl_SetPrivateData,
198     IDirect3DSurface8Impl_GetPrivateData,
199     IDirect3DSurface8Impl_FreePrivateData,
200     /* IDirect3DSurface8 */
201     IDirect3DSurface8Impl_GetContainer,
202     IDirect3DSurface8Impl_GetDesc,
203     IDirect3DSurface8Impl_LockRect,
204     IDirect3DSurface8Impl_UnlockRect
205 };