fltlib: Add a stub dll.
[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)
57         {
58             if (This->parentDevice) IDirect3DDevice9Ex_AddRef(This->parentDevice);
59             wined3d_mutex_lock();
60             IWineD3DSurface_AddRef(This->wineD3DSurface);
61             wined3d_mutex_unlock();
62         }
63         TRACE("(%p) : AddRef from %d\n", This, ref - 1);
64
65         return ref;
66     }
67
68 }
69
70 static ULONG WINAPI IDirect3DSurface9Impl_Release(LPDIRECT3DSURFACE9 iface) {
71     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
72
73     TRACE("(%p)\n", This);
74
75     if (This->forwardReference) {
76         /* Forward to the containerParent */
77         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
78         return IUnknown_Release(This->forwardReference);
79     } else {
80         /* No container, handle our own refcounting */
81         ULONG ref = InterlockedDecrement(&This->ref);
82         TRACE("(%p) : ReleaseRef to %d\n", This, ref);
83
84         if (ref == 0) {
85             IDirect3DDevice9Ex *parentDevice = This->parentDevice;
86
87             wined3d_mutex_lock();
88             IWineD3DSurface_Release(This->wineD3DSurface);
89             wined3d_mutex_unlock();
90
91             /* Release the device last, as it may cause the device to be destroyed. */
92             if (parentDevice) IDirect3DDevice9Ex_Release(parentDevice);
93         }
94
95         return ref;
96     }
97 }
98
99 /* IDirect3DSurface9 IDirect3DResource9 Interface follow: */
100 static HRESULT WINAPI IDirect3DSurface9Impl_GetDevice(LPDIRECT3DSURFACE9 iface, IDirect3DDevice9** ppDevice) {
101     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
102     IWineD3DDevice *wined3d_device;
103     HRESULT hr;
104     TRACE("(%p)->(%p)\n", This, ppDevice);
105
106     wined3d_mutex_lock();
107     hr = IWineD3DSurface_GetDevice(This->wineD3DSurface, &wined3d_device);
108     if (SUCCEEDED(hr))
109     {
110         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
111         IWineD3DDevice_Release(wined3d_device);
112     }
113     wined3d_mutex_unlock();
114
115     return hr;
116 }
117
118 static HRESULT WINAPI IDirect3DSurface9Impl_SetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
119     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
120     HRESULT hr;
121     TRACE("(%p) Relay\n", This);
122
123     wined3d_mutex_lock();
124     hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
125     wined3d_mutex_unlock();
126
127     return hr;
128 }
129
130 static HRESULT WINAPI IDirect3DSurface9Impl_GetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
131     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
132     HRESULT hr;
133     TRACE("(%p) Relay\n", This);
134
135     wined3d_mutex_lock();
136     hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
137     wined3d_mutex_unlock();
138
139     return hr;
140 }
141
142 static HRESULT WINAPI IDirect3DSurface9Impl_FreePrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid) {
143     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
144     HRESULT hr;
145     TRACE("(%p) Relay\n", This);
146
147     wined3d_mutex_lock();
148     hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
149     wined3d_mutex_unlock();
150
151     return hr;
152 }
153
154 static DWORD WINAPI IDirect3DSurface9Impl_SetPriority(LPDIRECT3DSURFACE9 iface, DWORD PriorityNew) {
155     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
156     HRESULT hr;
157     TRACE("(%p) Relay\n", This);
158
159     wined3d_mutex_lock();
160     hr = IWineD3DSurface_SetPriority(This->wineD3DSurface, PriorityNew);
161     wined3d_mutex_unlock();
162
163     return hr;
164 }
165
166 static DWORD WINAPI IDirect3DSurface9Impl_GetPriority(LPDIRECT3DSURFACE9 iface) {
167     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
168     HRESULT hr;
169     TRACE("(%p) Relay\n", This);
170
171     wined3d_mutex_lock();
172     hr = IWineD3DSurface_GetPriority(This->wineD3DSurface);
173     wined3d_mutex_unlock();
174
175     return hr;
176 }
177
178 static void WINAPI IDirect3DSurface9Impl_PreLoad(LPDIRECT3DSURFACE9 iface) {
179     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
180     TRACE("(%p) Relay\n", This);
181
182     wined3d_mutex_lock();
183     IWineD3DSurface_PreLoad(This->wineD3DSurface);
184     wined3d_mutex_unlock();
185 }
186
187 static D3DRESOURCETYPE WINAPI IDirect3DSurface9Impl_GetType(LPDIRECT3DSURFACE9 iface) {
188     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
189     D3DRESOURCETYPE ret;
190     TRACE("(%p) Relay\n", This);
191
192     wined3d_mutex_lock();
193     ret = IWineD3DSurface_GetType(This->wineD3DSurface);
194     wined3d_mutex_unlock();
195
196     return ret;
197 }
198
199 /* IDirect3DSurface9 Interface follow: */
200 static HRESULT WINAPI IDirect3DSurface9Impl_GetContainer(LPDIRECT3DSURFACE9 iface, REFIID riid, void** ppContainer) {
201     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
202     HRESULT res;
203
204     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
205
206     if (!This->container) return E_NOINTERFACE;
207
208     if (!ppContainer) {
209         ERR("Called without a valid ppContainer\n");
210     }
211
212     res = IUnknown_QueryInterface(This->container, riid, ppContainer);
213
214     TRACE("Returning ppContainer %p, *ppContainer %p\n", ppContainer, *ppContainer);
215
216     return res;
217 }
218
219 static HRESULT WINAPI IDirect3DSurface9Impl_GetDesc(LPDIRECT3DSURFACE9 iface, D3DSURFACE_DESC* pDesc) {
220     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
221     WINED3DSURFACE_DESC wined3ddesc;
222     HRESULT hr;
223     TRACE("(%p) Relay\n", This);
224
225     wined3d_mutex_lock();
226     hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
227     wined3d_mutex_unlock();
228
229     if (SUCCEEDED(hr))
230     {
231         pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
232         pDesc->Type = wined3ddesc.resource_type;
233         pDesc->Usage = wined3ddesc.usage;
234         pDesc->Pool = wined3ddesc.pool;
235         pDesc->MultiSampleType = wined3ddesc.multisample_type;
236         pDesc->MultiSampleQuality = wined3ddesc.multisample_quality;
237         pDesc->Width = wined3ddesc.width;
238         pDesc->Height = wined3ddesc.height;
239     }
240
241     return hr;
242 }
243
244 static HRESULT WINAPI IDirect3DSurface9Impl_LockRect(LPDIRECT3DSURFACE9 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
245     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
246     HRESULT hr;
247     TRACE("(%p) Relay\n", This);
248
249     wined3d_mutex_lock();
250     TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %d\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
251     hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
252     wined3d_mutex_unlock();
253
254     return hr;
255 }
256
257 static HRESULT WINAPI IDirect3DSurface9Impl_UnlockRect(LPDIRECT3DSURFACE9 iface) {
258     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
259     HRESULT hr;
260     TRACE("(%p) Relay\n", This);
261
262     wined3d_mutex_lock();
263     hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
264     wined3d_mutex_unlock();
265
266     switch(hr)
267     {
268         case WINEDDERR_NOTLOCKED:       return D3DERR_INVALIDCALL;
269         default:                        return hr;
270     }
271 }
272
273 static HRESULT WINAPI IDirect3DSurface9Impl_GetDC(LPDIRECT3DSURFACE9 iface, HDC* phdc) {
274     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
275     HRESULT hr;
276     TRACE("(%p) Relay\n", This);
277
278     if(!This->getdc_supported)
279     {
280         WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
281         /* Don't touch the DC */
282         return D3DERR_INVALIDCALL;
283     }
284
285     wined3d_mutex_lock();
286     hr = IWineD3DSurface_GetDC(This->wineD3DSurface, phdc);
287     wined3d_mutex_unlock();
288
289     return hr;
290 }
291
292 static HRESULT WINAPI IDirect3DSurface9Impl_ReleaseDC(LPDIRECT3DSURFACE9 iface, HDC hdc) {
293     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
294     HRESULT hr;
295     TRACE("(%p) Relay\n", This);
296
297     wined3d_mutex_lock();
298     hr = IWineD3DSurface_ReleaseDC(This->wineD3DSurface, hdc);
299     wined3d_mutex_unlock();
300
301     switch(hr) {
302         case WINEDDERR_NODC:    return WINED3DERR_INVALIDCALL;
303         default:                return hr;
304     }
305 }
306
307 static const IDirect3DSurface9Vtbl Direct3DSurface9_Vtbl =
308 {
309     /* IUnknown */
310     IDirect3DSurface9Impl_QueryInterface,
311     IDirect3DSurface9Impl_AddRef,
312     IDirect3DSurface9Impl_Release,
313     /* IDirect3DResource9 */
314     IDirect3DSurface9Impl_GetDevice,
315     IDirect3DSurface9Impl_SetPrivateData,
316     IDirect3DSurface9Impl_GetPrivateData,
317     IDirect3DSurface9Impl_FreePrivateData,
318     IDirect3DSurface9Impl_SetPriority,
319     IDirect3DSurface9Impl_GetPriority,
320     IDirect3DSurface9Impl_PreLoad,
321     IDirect3DSurface9Impl_GetType,
322     /* IDirect3DSurface9 */
323     IDirect3DSurface9Impl_GetContainer,
324     IDirect3DSurface9Impl_GetDesc,
325     IDirect3DSurface9Impl_LockRect,
326     IDirect3DSurface9Impl_UnlockRect,
327     IDirect3DSurface9Impl_GetDC,
328     IDirect3DSurface9Impl_ReleaseDC
329 };
330
331 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
332 {
333     HeapFree(GetProcessHeap(), 0, parent);
334 }
335
336 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
337 {
338     surface_wined3d_object_destroyed,
339 };
340
341 HRESULT surface_init(IDirect3DSurface9Impl *surface, IDirect3DDevice9Impl *device,
342         UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
343         DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
344 {
345     HRESULT hr;
346
347     surface->lpVtbl = &Direct3DSurface9_Vtbl;
348     surface->ref = 1;
349
350     switch (format)
351     {
352         case D3DFMT_A8R8G8B8:
353         case D3DFMT_X8R8G8B8:
354         case D3DFMT_R5G6B5:
355         case D3DFMT_X1R5G5B5:
356         case D3DFMT_A1R5G5B5:
357         case D3DFMT_R8G8B8:
358             surface->getdc_supported = TRUE;
359             break;
360
361         default:
362             surface->getdc_supported = FALSE;
363             break;
364     }
365
366     /* FIXME: Check MAX bounds of MultisampleQuality. */
367     if (multisample_quality > 0)
368     {
369         FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
370         multisample_quality = 0;
371     }
372
373     wined3d_mutex_lock();
374     hr = IWineD3DDevice_CreateSurface(device->WineD3DDevice, width, height, wined3dformat_from_d3dformat(format),
375             lockable, discard, level, &surface->wineD3DSurface, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool,
376             multisample_type, multisample_quality, SURFACE_OPENGL, (IUnknown *)surface,
377             &d3d9_surface_wined3d_parent_ops);
378     wined3d_mutex_unlock();
379     if (FAILED(hr))
380     {
381         WARN("Failed to create wined3d surface, hr %#x.\n", hr);
382         return hr;
383     }
384
385     surface->parentDevice = (IDirect3DDevice9Ex *)device;
386     IDirect3DDevice9Ex_AddRef(surface->parentDevice);
387
388     return D3D_OK;
389 }