ole32: Track the pending call count so that the correct call type can be passed to...
[wine] / dlls / d3d9 / directx.c
1 /*
2  * IDirect3D9 implementation
3  *
4  * Copyright 2002 Jason Edmeades
5  * Copyright 2005 Oliver Stieber
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 /* IDirect3D9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3D9Impl_QueryInterface(LPDIRECT3D9 iface, REFIID riid, LPVOID* ppobj)
29 {
30     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
31
32     if (IsEqualGUID(riid, &IID_IUnknown)
33         || IsEqualGUID(riid, &IID_IDirect3D9)) {
34         IUnknown_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 IDirect3D9Impl_AddRef(LPDIRECT3D9 iface) {
45     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
46     ULONG ref = InterlockedIncrement(&This->ref);
47
48     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
49
50     return ref;
51 }
52
53 static ULONG WINAPI IDirect3D9Impl_Release(LPDIRECT3D9 iface) {
54     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
55     ULONG ref = InterlockedDecrement(&This->ref);
56
57     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
58
59     if (ref == 0) {
60         IWineD3D_Release(This->WineD3D);
61         HeapFree(GetProcessHeap(), 0, This);
62     }
63
64     return ref;
65 }
66
67 /* IDirect3D9 Interface follow: */
68 static HRESULT  WINAPI  IDirect3D9Impl_RegisterSoftwareDevice(LPDIRECT3D9 iface, void* pInitializeFunction) {
69     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
70     return IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
71 }
72
73 static UINT     WINAPI  IDirect3D9Impl_GetAdapterCount(LPDIRECT3D9 iface) {
74     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
75     return IWineD3D_GetAdapterCount(This->WineD3D);
76 }
77
78 static HRESULT WINAPI IDirect3D9Impl_GetAdapterIdentifier(LPDIRECT3D9 iface, UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER9* pIdentifier) {
79     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
80     WINED3DADAPTER_IDENTIFIER adapter_id;
81
82     /* dx8 and dx9 have different structures to be filled in, with incompatible 
83        layouts so pass in pointers to the places to be filled via an internal 
84        structure                                                                */
85     adapter_id.Driver           = pIdentifier->Driver;          
86     adapter_id.Description      = pIdentifier->Description;     
87     adapter_id.DeviceName       = pIdentifier->DeviceName;      
88     adapter_id.DriverVersion    = &pIdentifier->DriverVersion;   
89     adapter_id.VendorId         = &pIdentifier->VendorId;        
90     adapter_id.DeviceId         = &pIdentifier->DeviceId;        
91     adapter_id.SubSysId         = &pIdentifier->SubSysId;        
92     adapter_id.Revision         = &pIdentifier->Revision;        
93     adapter_id.DeviceIdentifier = &pIdentifier->DeviceIdentifier;
94     adapter_id.WHQLLevel        = &pIdentifier->WHQLLevel;       
95
96     return IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
97 }
98
99 static UINT WINAPI IDirect3D9Impl_GetAdapterModeCount(LPDIRECT3D9 iface, UINT Adapter, D3DFORMAT Format) {
100     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
101     return IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, Format);
102 }
103
104 static HRESULT WINAPI IDirect3D9Impl_EnumAdapterModes(LPDIRECT3D9 iface, UINT Adapter, D3DFORMAT Format, UINT Mode, D3DDISPLAYMODE* pMode) {
105     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
106     /* We can't pass this to WineD3D, otherwise it'll think it came from D3D8.
107        It's supposed to fail anyway, so no harm returning failure. */
108     if(Format == D3DFMT_UNKNOWN)
109         return D3DERR_INVALIDCALL;
110     return IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, Format, Mode, (WINED3DDISPLAYMODE *) pMode);
111 }
112
113 static HRESULT WINAPI IDirect3D9Impl_GetAdapterDisplayMode(LPDIRECT3D9 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
114     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
115     return IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
116 }
117
118 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceType(LPDIRECT3D9 iface,
119                                               UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
120                                               D3DFORMAT BackBufferFormat, BOOL Windowed) {
121     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
122     return IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, DisplayFormat,
123                                     BackBufferFormat, Windowed);
124 }
125
126 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormat(LPDIRECT3D9 iface,
127                                                   UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
128                                                   DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
129     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
130     return IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, AdapterFormat,
131                                     Usage, RType, CheckFormat);
132 }
133
134 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceMultiSampleType(LPDIRECT3D9 iface,
135                                                            UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
136                                                            BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType, DWORD* pQualityLevels) {
137     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
138     return IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType, SurfaceFormat,
139                                                Windowed, MultiSampleType, pQualityLevels);
140 }
141
142 static HRESULT WINAPI IDirect3D9Impl_CheckDepthStencilMatch(LPDIRECT3D9 iface, 
143                                                        UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
144                                                        D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
145     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
146     return IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType, AdapterFormat,
147                                            RenderTargetFormat, DepthStencilFormat);
148 }
149
150 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormatConversion(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SourceFormat, D3DFORMAT TargetFormat) {
151     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
152     return IWineD3D_CheckDeviceFormatConversion(This->WineD3D, Adapter, DeviceType, SourceFormat,
153                                                 TargetFormat);
154 }
155
156 static HRESULT WINAPI IDirect3D9Impl_GetDeviceCaps(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS9* pCaps) {
157     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
158     HRESULT hrc = D3D_OK;
159     WINED3DCAPS *pWineCaps;
160
161     TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
162
163     if(NULL == pCaps){
164         return D3DERR_INVALIDCALL;
165     }
166     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
167     if(pWineCaps == NULL){
168         return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
169     }
170     D3D9CAPSTOWINECAPS(pCaps, pWineCaps)
171     hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
172     HeapFree(GetProcessHeap(), 0, pWineCaps);
173     TRACE("(%p) returning %p\n", This, pCaps);
174     return hrc;
175 }
176
177 static HMONITOR WINAPI IDirect3D9Impl_GetAdapterMonitor(LPDIRECT3D9 iface, UINT Adapter) {
178     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
179     return IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
180 }
181
182 /* Internal function called back during the CreateDevice to create a render target */
183 HRESULT WINAPI D3D9CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
184                                          WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
185                                          DWORD MultisampleQuality, BOOL Lockable,
186                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
187     HRESULT res = D3D_OK;
188     IDirect3DSurface9Impl *d3dSurface = NULL;
189     TRACE("(%p) call back\n", device);
190     res = IDirect3DDevice9_CreateRenderTarget((IDirect3DDevice9 *)device, Width, Height, 
191                                          (D3DFORMAT)Format, MultiSample, MultisampleQuality, Lockable,
192                                          (IDirect3DSurface9 **)&d3dSurface, pSharedHandle);
193
194     if (SUCCEEDED(res)) {
195         *ppSurface = d3dSurface->wineD3DSurface;
196         d3dSurface->container = pSuperior;
197         d3dSurface->isImplicit = TRUE;
198         /* Implicit surfaces are created with an refcount of 0 */
199         IUnknown_Release((IUnknown *)d3dSurface);
200     } else {
201         *ppSurface = NULL;
202     }
203     return res;
204 }
205
206 ULONG WINAPI D3D9CB_DestroyRenderTarget(IWineD3DSurface *pSurface) {
207     IDirect3DSurface9Impl* surfaceParent;
208     TRACE("(%p) call back\n", pSurface);
209
210     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
211     surfaceParent->isImplicit = FALSE;
212     /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
213     return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
214 }
215
216 HRESULT WINAPI D3D9CB_CreateAdditionalSwapChain(IUnknown *device,
217                                                 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
218                                                 IWineD3DSwapChain ** ppSwapChain) {
219     HRESULT res = D3D_OK;
220     IDirect3DSwapChain9Impl *d3dSwapChain = NULL;
221     D3DPRESENT_PARAMETERS localParameters;
222     TRACE("(%p) call back\n", device);
223
224     localParameters.BackBufferWidth                = *(pPresentationParameters->BackBufferWidth);
225     localParameters.BackBufferHeight               = *(pPresentationParameters->BackBufferHeight);
226     localParameters.BackBufferFormat               = *(pPresentationParameters->BackBufferFormat);
227     localParameters.BackBufferCount                = *(pPresentationParameters->BackBufferCount);
228     localParameters.MultiSampleType                = *(pPresentationParameters->MultiSampleType);
229     localParameters.MultiSampleQuality             = *(pPresentationParameters->MultiSampleQuality);
230     localParameters.SwapEffect                     = *(pPresentationParameters->SwapEffect);
231     localParameters.hDeviceWindow                  = *(pPresentationParameters->hDeviceWindow);
232     localParameters.Windowed                       = *(pPresentationParameters->Windowed);
233     localParameters.EnableAutoDepthStencil         = *(pPresentationParameters->EnableAutoDepthStencil);
234     localParameters.AutoDepthStencilFormat         = *(pPresentationParameters->AutoDepthStencilFormat);
235     localParameters.Flags                          = *(pPresentationParameters->Flags);
236     localParameters.FullScreen_RefreshRateInHz     = *(pPresentationParameters->FullScreen_RefreshRateInHz);
237     localParameters.PresentationInterval           = *(pPresentationParameters->PresentationInterval);
238
239     /*copy the presentation parameters*/
240     res = IDirect3DDevice9_CreateAdditionalSwapChain((IDirect3DDevice9 *)device, &localParameters, (IDirect3DSwapChain9 **)&d3dSwapChain);
241
242     if (SUCCEEDED(res)) {
243         *ppSwapChain = d3dSwapChain->wineD3DSwapChain;
244         d3dSwapChain->isImplicit = TRUE;
245         /* Implicit swap chains are created with an refcount of 0 */
246         IUnknown_Release((IUnknown *)d3dSwapChain);
247     } else {
248         *ppSwapChain = NULL;
249     }
250     /*Copy back the presentation parameters*/
251    *pPresentationParameters->BackBufferWidth               = localParameters.BackBufferWidth;
252    *pPresentationParameters->BackBufferHeight              = localParameters.BackBufferHeight;
253    *pPresentationParameters->BackBufferFormat              = localParameters.BackBufferFormat;
254    *pPresentationParameters->BackBufferCount               = localParameters.BackBufferCount;
255    *pPresentationParameters->MultiSampleType               = localParameters.MultiSampleType;
256    *pPresentationParameters->MultiSampleQuality            = localParameters.MultiSampleQuality;
257    *pPresentationParameters->SwapEffect                    = localParameters.SwapEffect;
258    *pPresentationParameters->hDeviceWindow                 = localParameters.hDeviceWindow;
259    *pPresentationParameters->Windowed                      = localParameters.Windowed;
260    *pPresentationParameters->EnableAutoDepthStencil        = localParameters.EnableAutoDepthStencil;
261    *pPresentationParameters->AutoDepthStencilFormat        = localParameters.AutoDepthStencilFormat;
262    *pPresentationParameters->Flags                         = localParameters.Flags;
263    *pPresentationParameters->FullScreen_RefreshRateInHz    = localParameters.FullScreen_RefreshRateInHz;
264    *pPresentationParameters->PresentationInterval          = localParameters.PresentationInterval;
265
266    return res;
267 }
268
269 ULONG WINAPI D3D9CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
270     IDirect3DSwapChain9Impl* swapChainParent;
271     TRACE("(%p) call back\n", pSwapChain);
272
273     IWineD3DSwapChain_GetParent(pSwapChain,(IUnknown **) &swapChainParent);
274     swapChainParent->isImplicit = FALSE;
275     /* Swap chain had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
276     return IDirect3DSwapChain9_Release((IDirect3DSwapChain9*) swapChainParent);
277 }
278
279 /* Internal function called back during the CreateDevice to create a render target */
280 HRESULT WINAPI D3D9CB_CreateDepthStencilSurface(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
281                                          WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
282                                          DWORD MultisampleQuality, BOOL Discard,
283                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
284     HRESULT res = D3D_OK;
285     IDirect3DSurface9Impl *d3dSurface = NULL;
286     TRACE("(%p) call back\n", device);
287
288     res = IDirect3DDevice9_CreateDepthStencilSurface((IDirect3DDevice9 *)device, Width, Height, 
289                                          (D3DFORMAT)Format, MultiSample, MultisampleQuality, Discard, 
290                                          (IDirect3DSurface9 **)&d3dSurface, pSharedHandle);
291     if (SUCCEEDED(res)) {
292         *ppSurface = d3dSurface->wineD3DSurface;
293         d3dSurface->container = device;
294         d3dSurface->isImplicit = TRUE;
295         /* Implicit surfaces are created with an refcount of 0 */
296         IUnknown_Release((IUnknown *)d3dSurface);
297     }
298     return res;
299 }
300
301 ULONG WINAPI D3D9CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
302     IDirect3DSurface9Impl* surfaceParent;
303     TRACE("(%p) call back\n", pSurface);
304
305     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
306     surfaceParent->isImplicit = FALSE;
307     /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
308     return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
309 }
310
311 HRESULT  WINAPI  IDirect3D9Impl_CreateDevice(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
312                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
313                                             IDirect3DDevice9** ppReturnedDeviceInterface) {
314
315     IDirect3D9Impl       *This   = (IDirect3D9Impl *)iface;
316     IDirect3DDevice9Impl *object = NULL;
317     WINED3DPRESENT_PARAMETERS localParameters;
318     HRESULT hr;
319     TRACE("(%p) Relay\n", This);
320
321     /* Check the validity range of the adapter parameter */
322     if (Adapter >= IDirect3D9Impl_GetAdapterCount(iface)) {
323         *ppReturnedDeviceInterface = NULL;
324         return D3DERR_INVALIDCALL;
325     }
326
327     /* Allocate the storage for the device object */
328     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice9Impl));
329     if (NULL == object) {
330         FIXME("Allocation of memory failed\n");
331         *ppReturnedDeviceInterface = NULL;
332         return D3DERR_OUTOFVIDEOMEMORY;
333     }
334
335     object->lpVtbl = &Direct3DDevice9_Vtbl;
336     object->ref = 1;
337     *ppReturnedDeviceInterface = (IDirect3DDevice9 *)object;
338
339     /* Allocate an associated WineD3DDevice object */
340     localParameters.BackBufferWidth                = &pPresentationParameters->BackBufferWidth;
341     localParameters.BackBufferHeight               = &pPresentationParameters->BackBufferHeight;
342     localParameters.BackBufferFormat               = (WINED3DFORMAT *)&pPresentationParameters->BackBufferFormat;
343     localParameters.BackBufferCount                = &pPresentationParameters->BackBufferCount;
344     localParameters.MultiSampleType                = (WINED3DMULTISAMPLE_TYPE *) &pPresentationParameters->MultiSampleType;
345     localParameters.MultiSampleQuality             = &pPresentationParameters->MultiSampleQuality;
346     localParameters.SwapEffect                     = (WINED3DSWAPEFFECT *) &pPresentationParameters->SwapEffect;
347     localParameters.hDeviceWindow                  = &pPresentationParameters->hDeviceWindow;
348     localParameters.Windowed                       = &pPresentationParameters->Windowed;
349     localParameters.EnableAutoDepthStencil         = &pPresentationParameters->EnableAutoDepthStencil;
350     localParameters.AutoDepthStencilFormat         = (WINED3DFORMAT *)&pPresentationParameters->AutoDepthStencilFormat;
351     localParameters.Flags                          = &pPresentationParameters->Flags;
352     localParameters.FullScreen_RefreshRateInHz     = &pPresentationParameters->FullScreen_RefreshRateInHz; 
353     localParameters.PresentationInterval           = &pPresentationParameters->PresentationInterval;
354
355     hr =IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags, &object->WineD3DDevice, (IUnknown *)object);
356
357     if (hr != D3D_OK) {
358         HeapFree(GetProcessHeap(), 0, object);
359         *ppReturnedDeviceInterface = NULL;
360         return hr;
361     }
362
363     TRACE("(%p) : Created Device %p\n", This, object);
364
365     hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters, D3D9CB_CreateAdditionalSwapChain);
366     if (hr != D3D_OK) {
367         FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
368         HeapFree(GetProcessHeap(), 0, object);
369         *ppReturnedDeviceInterface = NULL;
370     }
371     return hr;
372 }
373
374
375
376 const IDirect3D9Vtbl Direct3D9_Vtbl =
377 {
378     /* IUnknown */
379     IDirect3D9Impl_QueryInterface,
380     IDirect3D9Impl_AddRef,
381     IDirect3D9Impl_Release,
382     /* IDirect3D9 */
383     IDirect3D9Impl_RegisterSoftwareDevice,
384     IDirect3D9Impl_GetAdapterCount,
385     IDirect3D9Impl_GetAdapterIdentifier,
386     IDirect3D9Impl_GetAdapterModeCount,
387     IDirect3D9Impl_EnumAdapterModes,
388     IDirect3D9Impl_GetAdapterDisplayMode,
389     IDirect3D9Impl_CheckDeviceType,
390     IDirect3D9Impl_CheckDeviceFormat,
391     IDirect3D9Impl_CheckDeviceMultiSampleType,
392     IDirect3D9Impl_CheckDepthStencilMatch,
393     IDirect3D9Impl_CheckDeviceFormatConversion,
394     IDirect3D9Impl_GetDeviceCaps,
395     IDirect3D9Impl_GetAdapterMonitor,
396     IDirect3D9Impl_CreateDevice
397 };