msvfw32: Move MSVIDEO_SendMessage() up and make it and MSVIDEO_GetHicPtr() static.
[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., 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 /* IDirect3DSurface9 IUnknown parts follow: */
28 static 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         IDirect3DSurface9_AddRef(iface);
35         *ppobj = This;
36         return S_OK;
37     }
38
39     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
40     *ppobj = NULL;
41     return E_NOINTERFACE;
42 }
43
44 static ULONG WINAPI IDirect3DSurface9Impl_AddRef(LPDIRECT3DSURFACE9 iface) {
45     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
46
47     TRACE("(%p)\n", This);
48
49     if (This->forwardReference) {
50         /* Forward refcounting */
51         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
52         return IUnknown_AddRef(This->forwardReference);
53     } else {
54         /* No container, handle our own refcounting */
55         ULONG ref = InterlockedIncrement(&This->ref);
56         if(ref == 1 && This->parentDevice) IDirect3DDevice9Ex_AddRef(This->parentDevice);
57         TRACE("(%p) : AddRef from %d\n", This, ref - 1);
58
59         return ref;
60     }
61
62 }
63
64 static ULONG WINAPI IDirect3DSurface9Impl_Release(LPDIRECT3DSURFACE9 iface) {
65     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
66
67     TRACE("(%p)\n", This);
68
69     if (This->forwardReference) {
70         /* Forward to the containerParent */
71         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
72         return IUnknown_Release(This->forwardReference);
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             if (This->parentDevice) IDirect3DDevice9Ex_Release(This->parentDevice);
80             if (!This->isImplicit) {
81                 EnterCriticalSection(&d3d9_cs);
82                 IWineD3DSurface_Release(This->wineD3DSurface);
83                 LeaveCriticalSection(&d3d9_cs);
84                 HeapFree(GetProcessHeap(), 0, This);
85             }
86         }
87
88         return ref;
89     }
90 }
91
92 /* IDirect3DSurface9 IDirect3DResource9 Interface follow: */
93 static HRESULT WINAPI IDirect3DSurface9Impl_GetDevice(LPDIRECT3DSURFACE9 iface, IDirect3DDevice9** ppDevice) {
94     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
95     IWineD3DDevice *wined3d_device;
96     HRESULT hr;
97     TRACE("(%p)->(%p)\n", This, ppDevice);
98
99     EnterCriticalSection(&d3d9_cs);
100     hr = IWineD3DSurface_GetDevice(This->wineD3DSurface, &wined3d_device);
101     if (SUCCEEDED(hr))
102     {
103         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
104         IWineD3DDevice_Release(wined3d_device);
105     }
106     LeaveCriticalSection(&d3d9_cs);
107     return hr;
108 }
109
110 static HRESULT WINAPI IDirect3DSurface9Impl_SetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
111     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
112     HRESULT hr;
113     TRACE("(%p) Relay\n", This);
114
115     EnterCriticalSection(&d3d9_cs);
116     hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
117     LeaveCriticalSection(&d3d9_cs);
118     return hr;
119 }
120
121 static HRESULT WINAPI IDirect3DSurface9Impl_GetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
122     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
123     HRESULT hr;
124     TRACE("(%p) Relay\n", This);
125
126     EnterCriticalSection(&d3d9_cs);
127     hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
128     LeaveCriticalSection(&d3d9_cs);
129     return hr;
130 }
131
132 static HRESULT WINAPI IDirect3DSurface9Impl_FreePrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid) {
133     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
134     HRESULT hr;
135     TRACE("(%p) Relay\n", This);
136
137     EnterCriticalSection(&d3d9_cs);
138     hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
139     LeaveCriticalSection(&d3d9_cs);
140     return hr;
141 }
142
143 static DWORD WINAPI IDirect3DSurface9Impl_SetPriority(LPDIRECT3DSURFACE9 iface, DWORD PriorityNew) {
144     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
145     HRESULT hr;
146     TRACE("(%p) Relay\n", This);
147
148     EnterCriticalSection(&d3d9_cs);
149     hr = IWineD3DSurface_SetPriority(This->wineD3DSurface, PriorityNew);
150     LeaveCriticalSection(&d3d9_cs);
151     return hr;
152 }
153
154 static DWORD WINAPI IDirect3DSurface9Impl_GetPriority(LPDIRECT3DSURFACE9 iface) {
155     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
156     HRESULT hr;
157     TRACE("(%p) Relay\n", This);
158
159     EnterCriticalSection(&d3d9_cs);
160     hr = IWineD3DSurface_GetPriority(This->wineD3DSurface);
161     LeaveCriticalSection(&d3d9_cs);
162     return hr;
163 }
164
165 static void WINAPI IDirect3DSurface9Impl_PreLoad(LPDIRECT3DSURFACE9 iface) {
166     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
167     TRACE("(%p) Relay\n", This);
168
169     EnterCriticalSection(&d3d9_cs);
170     IWineD3DSurface_PreLoad(This->wineD3DSurface);
171     LeaveCriticalSection(&d3d9_cs);
172     return ;
173 }
174
175 static D3DRESOURCETYPE WINAPI IDirect3DSurface9Impl_GetType(LPDIRECT3DSURFACE9 iface) {
176     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
177     D3DRESOURCETYPE ret;
178     TRACE("(%p) Relay\n", This);
179
180     EnterCriticalSection(&d3d9_cs);
181     ret = IWineD3DSurface_GetType(This->wineD3DSurface);
182     LeaveCriticalSection(&d3d9_cs);
183     return ret;
184 }
185
186 /* IDirect3DSurface9 Interface follow: */
187 static HRESULT WINAPI IDirect3DSurface9Impl_GetContainer(LPDIRECT3DSURFACE9 iface, REFIID riid, void** ppContainer) {
188     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
189     HRESULT res;
190
191     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
192
193     if (!This->container) return E_NOINTERFACE;
194
195     if (!ppContainer) {
196         ERR("Called without a valid ppContainer\n");
197     }
198
199     res = IUnknown_QueryInterface(This->container, riid, ppContainer);
200
201     TRACE("Returning ppContainer %p, *ppContainer %p\n", ppContainer, *ppContainer);
202
203     return res;
204 }
205
206 static HRESULT WINAPI IDirect3DSurface9Impl_GetDesc(LPDIRECT3DSURFACE9 iface, D3DSURFACE_DESC* pDesc) {
207     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
208     WINED3DSURFACE_DESC wined3ddesc;
209     HRESULT hr;
210     TRACE("(%p) Relay\n", This);
211
212     EnterCriticalSection(&d3d9_cs);
213     hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
214     LeaveCriticalSection(&d3d9_cs);
215
216     if (SUCCEEDED(hr))
217     {
218         pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
219         pDesc->Type = wined3ddesc.resource_type;
220         pDesc->Usage = wined3ddesc.usage;
221         pDesc->Pool = wined3ddesc.pool;
222         pDesc->MultiSampleType = wined3ddesc.multisample_type;
223         pDesc->MultiSampleQuality = wined3ddesc.multisample_quality;
224         pDesc->Width = wined3ddesc.width;
225         pDesc->Height = wined3ddesc.height;
226     }
227
228     return hr;
229 }
230
231 static HRESULT WINAPI IDirect3DSurface9Impl_LockRect(LPDIRECT3DSURFACE9 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
232     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
233     HRESULT hr;
234     TRACE("(%p) Relay\n", This);
235
236     EnterCriticalSection(&d3d9_cs);
237     TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %d\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
238     hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
239     LeaveCriticalSection(&d3d9_cs);
240     return hr;
241 }
242
243 static HRESULT WINAPI IDirect3DSurface9Impl_UnlockRect(LPDIRECT3DSURFACE9 iface) {
244     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
245     HRESULT hr;
246     TRACE("(%p) Relay\n", This);
247
248     EnterCriticalSection(&d3d9_cs);
249     hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
250     LeaveCriticalSection(&d3d9_cs);
251     switch(hr)
252     {
253         case WINEDDERR_NOTLOCKED:       return D3DERR_INVALIDCALL;
254         default:                        return hr;
255     }
256 }
257
258 static HRESULT WINAPI IDirect3DSurface9Impl_GetDC(LPDIRECT3DSURFACE9 iface, HDC* phdc) {
259     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
260     HRESULT hr;
261     TRACE("(%p) Relay\n", This);
262
263     EnterCriticalSection(&d3d9_cs);
264     hr = IWineD3DSurface_GetDC(This->wineD3DSurface, phdc);
265     LeaveCriticalSection(&d3d9_cs);
266     return hr;
267 }
268
269 static HRESULT WINAPI IDirect3DSurface9Impl_ReleaseDC(LPDIRECT3DSURFACE9 iface, HDC hdc) {
270     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
271     HRESULT hr;
272     TRACE("(%p) Relay\n", This);
273
274     EnterCriticalSection(&d3d9_cs);
275     hr = IWineD3DSurface_ReleaseDC(This->wineD3DSurface, hdc);
276     LeaveCriticalSection(&d3d9_cs);
277     switch(hr) {
278         case WINEDDERR_NODC:    return WINED3DERR_INVALIDCALL;
279         default:                return hr;
280     }
281 }
282
283
284 const IDirect3DSurface9Vtbl Direct3DSurface9_Vtbl =
285 {
286     /* IUnknown */
287     IDirect3DSurface9Impl_QueryInterface,
288     IDirect3DSurface9Impl_AddRef,
289     IDirect3DSurface9Impl_Release,
290     /* IDirect3DResource9 */
291     IDirect3DSurface9Impl_GetDevice,
292     IDirect3DSurface9Impl_SetPrivateData,
293     IDirect3DSurface9Impl_GetPrivateData,
294     IDirect3DSurface9Impl_FreePrivateData,
295     IDirect3DSurface9Impl_SetPriority,
296     IDirect3DSurface9Impl_GetPriority,
297     IDirect3DSurface9Impl_PreLoad,
298     IDirect3DSurface9Impl_GetType,
299     /* IDirect3DSurface9 */
300     IDirect3DSurface9Impl_GetContainer,
301     IDirect3DSurface9Impl_GetDesc,
302     IDirect3DSurface9Impl_LockRect,
303     IDirect3DSurface9Impl_UnlockRect,
304     IDirect3DSurface9Impl_GetDC,
305     IDirect3DSurface9Impl_ReleaseDC
306 };