mshtml: Fixed tests.
[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                 wined3d_mutex_lock();
82                 IWineD3DSurface_Release(This->wineD3DSurface);
83                 wined3d_mutex_unlock();
84
85                 HeapFree(GetProcessHeap(), 0, This);
86             }
87         }
88
89         return ref;
90     }
91 }
92
93 /* IDirect3DSurface9 IDirect3DResource9 Interface follow: */
94 static HRESULT WINAPI IDirect3DSurface9Impl_GetDevice(LPDIRECT3DSURFACE9 iface, IDirect3DDevice9** ppDevice) {
95     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
96     IWineD3DDevice *wined3d_device;
97     HRESULT hr;
98     TRACE("(%p)->(%p)\n", This, ppDevice);
99
100     wined3d_mutex_lock();
101     hr = IWineD3DSurface_GetDevice(This->wineD3DSurface, &wined3d_device);
102     if (SUCCEEDED(hr))
103     {
104         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
105         IWineD3DDevice_Release(wined3d_device);
106     }
107     wined3d_mutex_unlock();
108
109     return hr;
110 }
111
112 static HRESULT WINAPI IDirect3DSurface9Impl_SetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
113     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
114     HRESULT hr;
115     TRACE("(%p) Relay\n", This);
116
117     wined3d_mutex_lock();
118     hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
119     wined3d_mutex_unlock();
120
121     return hr;
122 }
123
124 static HRESULT WINAPI IDirect3DSurface9Impl_GetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
125     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
126     HRESULT hr;
127     TRACE("(%p) Relay\n", This);
128
129     wined3d_mutex_lock();
130     hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
131     wined3d_mutex_unlock();
132
133     return hr;
134 }
135
136 static HRESULT WINAPI IDirect3DSurface9Impl_FreePrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid) {
137     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
138     HRESULT hr;
139     TRACE("(%p) Relay\n", This);
140
141     wined3d_mutex_lock();
142     hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
143     wined3d_mutex_unlock();
144
145     return hr;
146 }
147
148 static DWORD WINAPI IDirect3DSurface9Impl_SetPriority(LPDIRECT3DSURFACE9 iface, DWORD PriorityNew) {
149     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
150     HRESULT hr;
151     TRACE("(%p) Relay\n", This);
152
153     wined3d_mutex_lock();
154     hr = IWineD3DSurface_SetPriority(This->wineD3DSurface, PriorityNew);
155     wined3d_mutex_unlock();
156
157     return hr;
158 }
159
160 static DWORD WINAPI IDirect3DSurface9Impl_GetPriority(LPDIRECT3DSURFACE9 iface) {
161     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
162     HRESULT hr;
163     TRACE("(%p) Relay\n", This);
164
165     wined3d_mutex_lock();
166     hr = IWineD3DSurface_GetPriority(This->wineD3DSurface);
167     wined3d_mutex_unlock();
168
169     return hr;
170 }
171
172 static void WINAPI IDirect3DSurface9Impl_PreLoad(LPDIRECT3DSURFACE9 iface) {
173     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
174     TRACE("(%p) Relay\n", This);
175
176     wined3d_mutex_lock();
177     IWineD3DSurface_PreLoad(This->wineD3DSurface);
178     wined3d_mutex_unlock();
179 }
180
181 static D3DRESOURCETYPE WINAPI IDirect3DSurface9Impl_GetType(LPDIRECT3DSURFACE9 iface) {
182     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
183     D3DRESOURCETYPE ret;
184     TRACE("(%p) Relay\n", This);
185
186     wined3d_mutex_lock();
187     ret = IWineD3DSurface_GetType(This->wineD3DSurface);
188     wined3d_mutex_unlock();
189
190     return ret;
191 }
192
193 /* IDirect3DSurface9 Interface follow: */
194 static HRESULT WINAPI IDirect3DSurface9Impl_GetContainer(LPDIRECT3DSURFACE9 iface, REFIID riid, void** ppContainer) {
195     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
196     HRESULT res;
197
198     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
199
200     if (!This->container) return E_NOINTERFACE;
201
202     if (!ppContainer) {
203         ERR("Called without a valid ppContainer\n");
204     }
205
206     res = IUnknown_QueryInterface(This->container, riid, ppContainer);
207
208     TRACE("Returning ppContainer %p, *ppContainer %p\n", ppContainer, *ppContainer);
209
210     return res;
211 }
212
213 static HRESULT WINAPI IDirect3DSurface9Impl_GetDesc(LPDIRECT3DSURFACE9 iface, D3DSURFACE_DESC* pDesc) {
214     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
215     WINED3DSURFACE_DESC wined3ddesc;
216     HRESULT hr;
217     TRACE("(%p) Relay\n", This);
218
219     wined3d_mutex_lock();
220     hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
221     wined3d_mutex_unlock();
222
223     if (SUCCEEDED(hr))
224     {
225         pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
226         pDesc->Type = wined3ddesc.resource_type;
227         pDesc->Usage = wined3ddesc.usage;
228         pDesc->Pool = wined3ddesc.pool;
229         pDesc->MultiSampleType = wined3ddesc.multisample_type;
230         pDesc->MultiSampleQuality = wined3ddesc.multisample_quality;
231         pDesc->Width = wined3ddesc.width;
232         pDesc->Height = wined3ddesc.height;
233     }
234
235     return hr;
236 }
237
238 static HRESULT WINAPI IDirect3DSurface9Impl_LockRect(LPDIRECT3DSURFACE9 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
239     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
240     HRESULT hr;
241     TRACE("(%p) Relay\n", This);
242
243     wined3d_mutex_lock();
244     TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %d\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
245     hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
246     wined3d_mutex_unlock();
247
248     return hr;
249 }
250
251 static HRESULT WINAPI IDirect3DSurface9Impl_UnlockRect(LPDIRECT3DSURFACE9 iface) {
252     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
253     HRESULT hr;
254     TRACE("(%p) Relay\n", This);
255
256     wined3d_mutex_lock();
257     hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
258     wined3d_mutex_unlock();
259
260     switch(hr)
261     {
262         case WINEDDERR_NOTLOCKED:       return D3DERR_INVALIDCALL;
263         default:                        return hr;
264     }
265 }
266
267 static HRESULT WINAPI IDirect3DSurface9Impl_GetDC(LPDIRECT3DSURFACE9 iface, HDC* phdc) {
268     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
269     HRESULT hr;
270     TRACE("(%p) Relay\n", This);
271
272     if(!This->getdc_supported)
273     {
274         WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
275         /* Don't touch the DC */
276         return D3DERR_INVALIDCALL;
277     }
278
279     wined3d_mutex_lock();
280     hr = IWineD3DSurface_GetDC(This->wineD3DSurface, phdc);
281     wined3d_mutex_unlock();
282
283     return hr;
284 }
285
286 static HRESULT WINAPI IDirect3DSurface9Impl_ReleaseDC(LPDIRECT3DSURFACE9 iface, HDC hdc) {
287     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
288     HRESULT hr;
289     TRACE("(%p) Relay\n", This);
290
291     wined3d_mutex_lock();
292     hr = IWineD3DSurface_ReleaseDC(This->wineD3DSurface, hdc);
293     wined3d_mutex_unlock();
294
295     switch(hr) {
296         case WINEDDERR_NODC:    return WINED3DERR_INVALIDCALL;
297         default:                return hr;
298     }
299 }
300
301
302 const IDirect3DSurface9Vtbl Direct3DSurface9_Vtbl =
303 {
304     /* IUnknown */
305     IDirect3DSurface9Impl_QueryInterface,
306     IDirect3DSurface9Impl_AddRef,
307     IDirect3DSurface9Impl_Release,
308     /* IDirect3DResource9 */
309     IDirect3DSurface9Impl_GetDevice,
310     IDirect3DSurface9Impl_SetPrivateData,
311     IDirect3DSurface9Impl_GetPrivateData,
312     IDirect3DSurface9Impl_FreePrivateData,
313     IDirect3DSurface9Impl_SetPriority,
314     IDirect3DSurface9Impl_GetPriority,
315     IDirect3DSurface9Impl_PreLoad,
316     IDirect3DSurface9Impl_GetType,
317     /* IDirect3DSurface9 */
318     IDirect3DSurface9Impl_GetContainer,
319     IDirect3DSurface9Impl_GetDesc,
320     IDirect3DSurface9Impl_LockRect,
321     IDirect3DSurface9Impl_UnlockRect,
322     IDirect3DSurface9Impl_GetDC,
323     IDirect3DSurface9Impl_ReleaseDC
324 };