mshtml: Create nsWineURI even if we don't have valid nsIURI.
[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 %ld\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 %ld\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     return IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, Format, Mode, (WINED3DDISPLAYMODE *) pMode);
107 }
108
109 static HRESULT WINAPI IDirect3D9Impl_GetAdapterDisplayMode(LPDIRECT3D9 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
110     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
111     return IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
112 }
113
114 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceType(LPDIRECT3D9 iface,
115                                               UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
116                                               D3DFORMAT BackBufferFormat, BOOL Windowed) {
117     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
118     return IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, DisplayFormat,
119                                     BackBufferFormat, Windowed);
120 }
121
122 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormat(LPDIRECT3D9 iface,
123                                                   UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
124                                                   DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
125     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
126     return IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, AdapterFormat,
127                                     Usage, RType, CheckFormat);
128 }
129
130 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceMultiSampleType(LPDIRECT3D9 iface,
131                                                            UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
132                                                            BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType, DWORD* pQualityLevels) {
133     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
134     return IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType, SurfaceFormat,
135                                                Windowed, MultiSampleType, pQualityLevels);
136 }
137
138 static HRESULT WINAPI IDirect3D9Impl_CheckDepthStencilMatch(LPDIRECT3D9 iface, 
139                                                        UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
140                                                        D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
141     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
142     return IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType, AdapterFormat,
143                                            RenderTargetFormat, DepthStencilFormat);
144 }
145
146 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormatConversion(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SourceFormat, D3DFORMAT TargetFormat) {
147     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
148     return IWineD3D_CheckDeviceFormatConversion(This->WineD3D, Adapter, DeviceType, SourceFormat,
149                                                 TargetFormat);
150 }
151
152 static HRESULT WINAPI IDirect3D9Impl_GetDeviceCaps(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS9* pCaps) {
153     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
154     HRESULT hrc = D3D_OK;
155     WINED3DCAPS *pWineCaps;
156
157     TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
158
159     if(NULL == pCaps){
160         return D3DERR_INVALIDCALL;
161     }
162     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
163     if(pWineCaps == NULL){
164         return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
165     }
166     D3D9CAPSTOWINECAPS(pCaps, pWineCaps)
167     hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
168     HeapFree(GetProcessHeap(), 0, pWineCaps);
169     TRACE("(%p) returning %p\n", This, pCaps);
170     return hrc;
171 }
172
173 static HMONITOR WINAPI IDirect3D9Impl_GetAdapterMonitor(LPDIRECT3D9 iface, UINT Adapter) {
174     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
175     return IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
176 }
177
178 /* Internal function called back during the CreateDevice to create a render target */
179 HRESULT WINAPI D3D9CB_CreateRenderTarget(IUnknown *device, UINT Width, UINT Height,
180                                          WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
181                                          DWORD MultisampleQuality, BOOL Lockable,
182                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
183     HRESULT res = D3D_OK;
184     IDirect3DSurface9Impl *d3dSurface = NULL;
185     TRACE("(%p) call back\n", device);
186     res = IDirect3DDevice9_CreateRenderTarget((IDirect3DDevice9 *)device, Width, Height, 
187                                          (D3DFORMAT)Format, MultiSample, MultisampleQuality, Lockable,
188                                          (IDirect3DSurface9 **)&d3dSurface, pSharedHandle);
189
190     if (SUCCEEDED(res)) {
191         *ppSurface = d3dSurface->wineD3DSurface;
192         IUnknown_Release(d3dSurface->parentDevice);
193         d3dSurface->parentDevice = NULL;
194     } else {
195         *ppSurface = NULL;
196     }
197     return res;
198 }
199
200 HRESULT WINAPI D3D9CB_CreateAdditionalSwapChain(IUnknown *device,
201                                                 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
202                                                 IWineD3DSwapChain ** ppSwapChain) {
203     HRESULT res = D3D_OK;
204     IDirect3DSwapChain9Impl *d3dSwapChain = NULL;
205     D3DPRESENT_PARAMETERS localParameters;
206     TRACE("(%p) call back\n", device);
207
208     localParameters.BackBufferWidth                = *(pPresentationParameters->BackBufferWidth);
209     localParameters.BackBufferHeight               = *(pPresentationParameters->BackBufferHeight);
210     localParameters.BackBufferFormat               = *(pPresentationParameters->BackBufferFormat);
211     localParameters.BackBufferCount                = *(pPresentationParameters->BackBufferCount);
212     localParameters.MultiSampleType                = *(pPresentationParameters->MultiSampleType);
213     localParameters.MultiSampleQuality             = *(pPresentationParameters->MultiSampleQuality);
214     localParameters.SwapEffect                     = *(pPresentationParameters->SwapEffect);
215     localParameters.hDeviceWindow                  = *(pPresentationParameters->hDeviceWindow);
216     localParameters.Windowed                       = *(pPresentationParameters->Windowed);
217     localParameters.EnableAutoDepthStencil         = *(pPresentationParameters->EnableAutoDepthStencil);
218     localParameters.AutoDepthStencilFormat         = *(pPresentationParameters->AutoDepthStencilFormat);
219     localParameters.Flags                          = *(pPresentationParameters->Flags);
220     localParameters.FullScreen_RefreshRateInHz     = *(pPresentationParameters->FullScreen_RefreshRateInHz);
221     localParameters.PresentationInterval           = *(pPresentationParameters->PresentationInterval);
222
223     /*copy the presentation parameters*/
224     res = IDirect3DDevice9_CreateAdditionalSwapChain((IDirect3DDevice9 *)device, &localParameters, (IDirect3DSwapChain9 **)&d3dSwapChain);
225
226     if (SUCCEEDED(res)) {
227         *ppSwapChain = d3dSwapChain->wineD3DSwapChain;
228         IUnknown_Release(d3dSwapChain->parentDevice);
229         d3dSwapChain->parentDevice = NULL;
230     } else {
231         *ppSwapChain = NULL;
232     }
233     /*Copy back the presentation parameters*/
234    *pPresentationParameters->BackBufferWidth               = localParameters.BackBufferWidth;
235    *pPresentationParameters->BackBufferHeight              = localParameters.BackBufferHeight;
236    *pPresentationParameters->BackBufferFormat              = localParameters.BackBufferFormat;
237    *pPresentationParameters->BackBufferCount               = localParameters.BackBufferCount;
238    *pPresentationParameters->MultiSampleType               = localParameters.MultiSampleType;
239    *pPresentationParameters->MultiSampleQuality            = localParameters.MultiSampleQuality;
240    *pPresentationParameters->SwapEffect                    = localParameters.SwapEffect;
241    *pPresentationParameters->hDeviceWindow                 = localParameters.hDeviceWindow;
242    *pPresentationParameters->Windowed                      = localParameters.Windowed;
243    *pPresentationParameters->EnableAutoDepthStencil        = localParameters.EnableAutoDepthStencil;
244    *pPresentationParameters->AutoDepthStencilFormat        = localParameters.AutoDepthStencilFormat;
245    *pPresentationParameters->Flags                         = localParameters.Flags;
246    *pPresentationParameters->FullScreen_RefreshRateInHz    = localParameters.FullScreen_RefreshRateInHz;
247    *pPresentationParameters->PresentationInterval          = localParameters.PresentationInterval;
248
249    return res;
250 }
251
252 /* Internal function called back during the CreateDevice to create a render target */
253 HRESULT WINAPI D3D9CB_CreateDepthStencilSurface(IUnknown *device, UINT Width, UINT Height, 
254                                          WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
255                                          DWORD MultisampleQuality, BOOL Discard,
256                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
257     HRESULT res = D3D_OK;
258     IDirect3DSurface9Impl *d3dSurface = NULL;
259     TRACE("(%p) call back\n", device);
260
261     res = IDirect3DDevice9_CreateDepthStencilSurface((IDirect3DDevice9 *)device, Width, Height, 
262                                          (D3DFORMAT)Format, MultiSample, MultisampleQuality, Discard, 
263                                          (IDirect3DSurface9 **)&d3dSurface, pSharedHandle);
264     if (SUCCEEDED(res)) {
265         *ppSurface = d3dSurface->wineD3DSurface;
266         IUnknown_Release(d3dSurface->parentDevice);
267         d3dSurface->parentDevice = NULL;
268     }
269     return res;
270 }
271
272
273 HRESULT  WINAPI  IDirect3D9Impl_CreateDevice(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
274                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
275                                             IDirect3DDevice9** ppReturnedDeviceInterface) {
276
277     IDirect3D9Impl       *This   = (IDirect3D9Impl *)iface;
278     IDirect3DDevice9Impl *object = NULL;
279     WINED3DPRESENT_PARAMETERS localParameters;
280     HRESULT hr;
281     TRACE("(%p) Relay\n", This);
282
283     /* Check the validity range of the adapter parameter */
284     if (Adapter >= IDirect3D9Impl_GetAdapterCount(iface)) {
285         *ppReturnedDeviceInterface = NULL;
286         return D3DERR_INVALIDCALL;
287     }
288
289     /* Allocate the storage for the device object */
290     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice9Impl));
291     if (NULL == object) {
292         FIXME("Allocation of memory failed\n");
293         *ppReturnedDeviceInterface = NULL;
294         return D3DERR_OUTOFVIDEOMEMORY;
295     }
296
297     object->lpVtbl = &Direct3DDevice9_Vtbl;
298     object->ref = 1;
299     *ppReturnedDeviceInterface = (IDirect3DDevice9 *)object;
300
301     /* Allocate an associated WineD3DDevice object */
302     localParameters.BackBufferWidth                = &pPresentationParameters->BackBufferWidth;
303     localParameters.BackBufferHeight               = &pPresentationParameters->BackBufferHeight;
304     localParameters.BackBufferFormat               = (WINED3DFORMAT *)&pPresentationParameters->BackBufferFormat;
305     localParameters.BackBufferCount                = &pPresentationParameters->BackBufferCount;
306     localParameters.MultiSampleType                = (WINED3DMULTISAMPLE_TYPE *) &pPresentationParameters->MultiSampleType;
307     localParameters.MultiSampleQuality             = &pPresentationParameters->MultiSampleQuality;
308     localParameters.SwapEffect                     = (WINED3DSWAPEFFECT *) &pPresentationParameters->SwapEffect;
309     localParameters.hDeviceWindow                  = &pPresentationParameters->hDeviceWindow;
310     localParameters.Windowed                       = &pPresentationParameters->Windowed;
311     localParameters.EnableAutoDepthStencil         = &pPresentationParameters->EnableAutoDepthStencil;
312     localParameters.AutoDepthStencilFormat         = (WINED3DFORMAT *)&pPresentationParameters->AutoDepthStencilFormat;
313     localParameters.Flags                          = &pPresentationParameters->Flags;
314     localParameters.FullScreen_RefreshRateInHz     = &pPresentationParameters->FullScreen_RefreshRateInHz; 
315     localParameters.PresentationInterval           = &pPresentationParameters->PresentationInterval;
316
317     hr =IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags, &object->WineD3DDevice, (IUnknown *)object);
318
319     if (hr != D3D_OK) {
320         HeapFree(GetProcessHeap(), 0, object);
321         *ppReturnedDeviceInterface = NULL;
322         return hr;
323     }
324
325     TRACE("(%p) : Created Device %p\n", This, object);
326
327     hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters, D3D9CB_CreateAdditionalSwapChain);
328     if (hr != D3D_OK) {
329         FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
330         HeapFree(GetProcessHeap(), 0, object);
331         *ppReturnedDeviceInterface = NULL;
332     }
333     return hr;
334 }
335
336
337
338 const IDirect3D9Vtbl Direct3D9_Vtbl =
339 {
340     /* IUnknown */
341     IDirect3D9Impl_QueryInterface,
342     IDirect3D9Impl_AddRef,
343     IDirect3D9Impl_Release,
344     /* IDirect3D9 */
345     IDirect3D9Impl_RegisterSoftwareDevice,
346     IDirect3D9Impl_GetAdapterCount,
347     IDirect3D9Impl_GetAdapterIdentifier,
348     IDirect3D9Impl_GetAdapterModeCount,
349     IDirect3D9Impl_EnumAdapterModes,
350     IDirect3D9Impl_GetAdapterDisplayMode,
351     IDirect3D9Impl_CheckDeviceType,
352     IDirect3D9Impl_CheckDeviceFormat,
353     IDirect3D9Impl_CheckDeviceMultiSampleType,
354     IDirect3D9Impl_CheckDepthStencilMatch,
355     IDirect3D9Impl_CheckDeviceFormatConversion,
356     IDirect3D9Impl_GetDeviceCaps,
357     IDirect3D9Impl_GetAdapterMonitor,
358     IDirect3D9Impl_CreateDevice
359 };