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