comctl32: A couple fixes for tab icon offsets.
[wine] / dlls / d3d8 / directx.c
1 /*
2  * IDirect3D8 implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2003-2004 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24
25 #include <stdarg.h>
26
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35
36 #include "d3d8_private.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
39
40 /* IDirect3D IUnknown parts follow: */
41 HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface, REFIID riid,LPVOID *ppobj)
42 {
43     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
44
45     if (IsEqualGUID(riid, &IID_IUnknown)
46         || IsEqualGUID(riid, &IID_IDirect3D8)) {
47         IUnknown_AddRef(iface);
48         *ppobj = This;
49         return D3D_OK;
50     }
51
52     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid),ppobj);
53     return E_NOINTERFACE;
54 }
55
56 ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
57     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
58     ULONG ref = InterlockedIncrement(&This->ref);
59
60     TRACE("(%p) : AddRef from %ld\n", This, ref - 1);
61
62     return ref;
63 }
64
65 ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
66     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
67     ULONG ref = InterlockedDecrement(&This->ref);
68
69     TRACE("(%p) : ReleaseRef to %ld\n", This, ref);
70
71     if (ref == 0) {
72         TRACE("Releasing wined3d %p\n", This->WineD3D);
73         IWineD3D_Release(This->WineD3D);
74         HeapFree(GetProcessHeap(), 0, This);
75     }
76
77     return ref;
78 }
79
80 /* IDirect3D8 Interface follow: */
81 HRESULT WINAPI IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface, void* pInitializeFunction) {
82     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
83     return IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
84 }
85
86 UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
87     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
88     return IWineD3D_GetAdapterCount(This->WineD3D);
89 }
90
91 HRESULT  WINAPI  IDirect3D8Impl_GetAdapterIdentifier       (LPDIRECT3D8 iface,
92                                                             UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8* pIdentifier) {
93     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
94     WINED3DADAPTER_IDENTIFIER adapter_id;
95
96     /* dx8 and dx9 have different structures to be filled in, with incompatible 
97        layouts so pass in pointers to the places to be filled via an internal 
98        structure                                                                */
99     adapter_id.Driver           = pIdentifier->Driver;
100     adapter_id.Description      = pIdentifier->Description;
101     adapter_id.DeviceName       = NULL; /* d3d9 only */
102     adapter_id.DriverVersion    = &pIdentifier->DriverVersion;
103     adapter_id.VendorId         = &pIdentifier->VendorId;
104     adapter_id.DeviceId         = &pIdentifier->DeviceId;
105     adapter_id.SubSysId         = &pIdentifier->SubSysId;
106     adapter_id.Revision         = &pIdentifier->Revision;
107     adapter_id.DeviceIdentifier = &pIdentifier->DeviceIdentifier;
108     adapter_id.WHQLLevel        = &pIdentifier->WHQLLevel;
109
110     return IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
111 }
112
113 UINT WINAPI IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface,UINT Adapter) {
114     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
115     return IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, 0 /* format */);
116 }
117
118 HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface, UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
119     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
120     /* FIXME: USe a constant WINED3DFOTMAT_ANY, or something similar */
121     return IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, 0 /* format */, Mode, pMode);
122 }
123
124 HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
125     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
126     return IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, pMode);
127 }
128
129 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceType            (LPDIRECT3D8 iface,
130                                                             UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
131                                                             D3DFORMAT BackBufferFormat, BOOL Windowed) {
132     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
133     return IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, DisplayFormat,
134                                     BackBufferFormat, Windowed);
135 }
136
137 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceFormat          (LPDIRECT3D8 iface,
138                                                             UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
139                                                             DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
140     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
141     return IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, AdapterFormat,
142                                     Usage, RType, CheckFormat);
143 }
144
145 HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceMultiSampleType(LPDIRECT3D8 iface,
146                                                            UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
147                                                            BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType) {
148     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
149     return IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType, SurfaceFormat,
150                                                Windowed, MultiSampleType, NULL);
151 }
152
153 HRESULT  WINAPI  IDirect3D8Impl_CheckDepthStencilMatch(LPDIRECT3D8 iface, 
154                                                        UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
155                                                        D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
156     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
157     return IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType, AdapterFormat,
158                                            RenderTargetFormat, DepthStencilFormat);
159 }
160
161 HRESULT  WINAPI  IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
162     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
163     HRESULT hrc = D3D_OK;
164     WINED3DCAPS *pWineCaps;
165
166     TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
167
168     if(NULL == pCaps){
169         return D3DERR_INVALIDCALL;
170     }
171     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
172     if(pWineCaps == NULL){
173         return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
174     }
175     D3D8CAPSTOWINECAPS(pCaps, pWineCaps)
176     hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
177     HeapFree(GetProcessHeap(), 0, pWineCaps);
178     TRACE("(%p) returning %p\n", This, pCaps);
179     return hrc;
180 }
181
182 HMONITOR WINAPI  IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
183     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
184     return IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
185 }
186
187 /* Internal function called back during the CreateDevice to create a render target */
188 HRESULT WINAPI D3D8CB_CreateRenderTarget(IUnknown *device, UINT Width, UINT Height, 
189                                          WINED3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, 
190                                          DWORD MultisampleQuality, BOOL Lockable, 
191                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
192     HRESULT res = D3D_OK;
193     IDirect3DSurface8Impl *d3dSurface = NULL;
194
195     TRACE("(%p) call back\n", device);
196     res = IDirect3DDevice8_CreateRenderTarget((IDirect3DDevice8 *)device, Width, Height, 
197                                          (D3DFORMAT)Format, MultiSample, Lockable, 
198                                          (IDirect3DSurface8 **)&d3dSurface);
199
200     if (SUCCEEDED(res)) {
201          *ppSurface = d3dSurface->wineD3DSurface;
202     } else {
203         *ppSurface = NULL;
204     }
205     return res;
206 }
207
208 /* Callback for creating the inplicite swapchain when the device is created */
209 HRESULT WINAPI D3D8CB_CreateAdditionalSwapChain(IUnknown *device,
210                                                 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
211                                                 IWineD3DSwapChain ** ppSwapChain){
212     HRESULT res = D3D_OK;
213     IDirect3DSwapChain8Impl *d3dSwapChain = NULL;
214     D3DPRESENT_PARAMETERS localParameters;
215     TRACE("(%p) call back\n", device);
216
217     localParameters.BackBufferWidth                = *(pPresentationParameters->BackBufferWidth);
218     localParameters.BackBufferHeight               = *(pPresentationParameters->BackBufferHeight);
219     localParameters.BackBufferFormat               = *(pPresentationParameters->BackBufferFormat);
220     localParameters.BackBufferCount                = *(pPresentationParameters->BackBufferCount);
221     localParameters.MultiSampleType                = *(pPresentationParameters->MultiSampleType);
222     /* d3d9 only */
223     /* localParameters.MultiSampleQuality             = *(pPresentationParameters->MultiSampleQuality); */
224     localParameters.SwapEffect                     = *(pPresentationParameters->SwapEffect);
225     localParameters.hDeviceWindow                  = *(pPresentationParameters->hDeviceWindow);
226     localParameters.Windowed                       = *(pPresentationParameters->Windowed);
227     localParameters.EnableAutoDepthStencil         = *(pPresentationParameters->EnableAutoDepthStencil);
228     localParameters.AutoDepthStencilFormat         = *(pPresentationParameters->AutoDepthStencilFormat);
229     localParameters.Flags                          = *(pPresentationParameters->Flags);
230     localParameters.FullScreen_RefreshRateInHz     = *(pPresentationParameters->FullScreen_RefreshRateInHz);
231     /* d3d9 only */
232     /*    localParameters.PresentationInterval           = *(pPresentationParameters->PresentationInterval); */
233
234     /*copy the presentation parameters*/
235     res = IDirect3DDevice8_CreateAdditionalSwapChain((IDirect3DDevice8 *)device, &localParameters, (IDirect3DSwapChain8 **)&d3dSwapChain);
236
237     if (res == D3D_OK && d3dSwapChain != NULL){
238         *ppSwapChain = d3dSwapChain->wineD3DSwapChain;
239     } else {
240         *ppSwapChain = NULL;
241     }
242
243     /*Copy back the presentation parameters*/
244     *pPresentationParameters->BackBufferWidth               = localParameters.BackBufferWidth;
245     *pPresentationParameters->BackBufferHeight              = localParameters.BackBufferHeight;
246     *pPresentationParameters->BackBufferFormat              = localParameters.BackBufferFormat;
247     *pPresentationParameters->BackBufferCount               = localParameters.BackBufferCount;
248     *pPresentationParameters->MultiSampleType               = localParameters.MultiSampleType;
249     /* d3d9 only */
250     /* *pPresentationParameters->MultiSampleQuality            = localParameters.MultiSampleQuality; */
251     *pPresentationParameters->SwapEffect                    = localParameters.SwapEffect;
252     *pPresentationParameters->hDeviceWindow                 = localParameters.hDeviceWindow;
253     *pPresentationParameters->Windowed                      = localParameters.Windowed;
254     *pPresentationParameters->EnableAutoDepthStencil        = localParameters.EnableAutoDepthStencil;
255     *pPresentationParameters->AutoDepthStencilFormat        = localParameters.AutoDepthStencilFormat;
256     *pPresentationParameters->Flags                         = localParameters.Flags;
257     *pPresentationParameters->FullScreen_RefreshRateInHz    = localParameters.FullScreen_RefreshRateInHz;
258     /* d3d9 only */
259     /* *pPresentationParameters->PresentationInterval          = localParameters.PresentationInterval; */
260
261    return res;
262 }
263
264 /* Internal function called back during the CreateDevice to create a render target */
265 HRESULT WINAPI D3D8CB_CreateDepthStencilSurface(IUnknown *device, UINT Width, UINT Height,
266                                          WINED3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample,
267                                          DWORD MultisampleQuality, BOOL Discard,
268                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
269     HRESULT res = D3D_OK;
270     IDirect3DSurface8Impl *d3dSurface = NULL;
271     TRACE("(%p) call back\n", device);
272
273     res = IDirect3DDevice8_CreateDepthStencilSurface((IDirect3DDevice8 *)device, Width, Height, 
274                                          (D3DFORMAT)Format, MultiSample, (IDirect3DSurface8 **)&d3dSurface);
275     if (res == D3D_OK) {
276         *ppSurface = d3dSurface->wineD3DSurface;
277     }
278     return res;
279 }
280
281 HRESULT  WINAPI  IDirect3D8Impl_CreateDevice(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
282                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
283                                             IDirect3DDevice8** ppReturnedDeviceInterface) {
284
285     IDirect3D8Impl       *This   = (IDirect3D8Impl *)iface;
286     IDirect3DDevice8Impl *object = NULL;
287     WINED3DPRESENT_PARAMETERS localParameters;
288     HRESULT hr;
289     TRACE("(%p) Relay\n", This);
290
291     /* Check the validity range of the adapter parameter */
292     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
293         *ppReturnedDeviceInterface = NULL;
294         return D3DERR_INVALIDCALL;
295     }
296
297     /* Allocate the storage for the device object */
298     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
299     if (NULL == object) {
300         FIXME("Allocation of memory failed\n");
301         *ppReturnedDeviceInterface = NULL;
302         return D3DERR_OUTOFVIDEOMEMORY;
303     }
304
305     object->lpVtbl = &Direct3DDevice8_Vtbl;
306     object->ref = 1;
307     *ppReturnedDeviceInterface = (IDirect3DDevice8 *)object;
308
309     /* Allocate an associated WineD3DDevice object */
310     localParameters.BackBufferWidth                = &pPresentationParameters->BackBufferWidth;             
311     localParameters.BackBufferHeight               = &pPresentationParameters->BackBufferHeight;
312     localParameters.BackBufferFormat               = (WINED3DFORMAT *)&pPresentationParameters->BackBufferFormat;
313     localParameters.BackBufferCount                = &pPresentationParameters->BackBufferCount;
314     localParameters.MultiSampleType                = &pPresentationParameters->MultiSampleType;
315     /* d3d9 only */
316     localParameters.MultiSampleQuality             = NULL;
317     localParameters.SwapEffect                     = &pPresentationParameters->SwapEffect;
318     localParameters.hDeviceWindow                  = &pPresentationParameters->hDeviceWindow;
319     localParameters.Windowed                       = &pPresentationParameters->Windowed;
320     localParameters.EnableAutoDepthStencil         = &pPresentationParameters->EnableAutoDepthStencil;
321     localParameters.AutoDepthStencilFormat         = (WINED3DFORMAT *)&pPresentationParameters->AutoDepthStencilFormat;
322     localParameters.Flags                          = &pPresentationParameters->Flags;
323     localParameters.FullScreen_RefreshRateInHz     = &pPresentationParameters->FullScreen_RefreshRateInHz;
324     localParameters.PresentationInterval           = &pPresentationParameters->FullScreen_PresentationInterval;/* Renamed in dx9 */
325
326     hr =IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags, &localParameters, &object->WineD3DDevice, (IUnknown *)object, D3D8CB_CreateAdditionalSwapChain);
327  
328     if (hr == D3D_OK) {
329         TRACE("(%p) : Created Device %p\n", This, object);
330     } else {
331         HeapFree(GetProcessHeap(), 0, object);
332         *ppReturnedDeviceInterface = NULL;
333     }
334
335     return hr;
336 }
337
338 const IDirect3D8Vtbl Direct3D8_Vtbl =
339 {
340     /* IUnknown */
341     IDirect3D8Impl_QueryInterface,
342     IDirect3D8Impl_AddRef,
343     IDirect3D8Impl_Release,
344     /* IDirect3D8 */
345     IDirect3D8Impl_RegisterSoftwareDevice,
346     IDirect3D8Impl_GetAdapterCount,
347     IDirect3D8Impl_GetAdapterIdentifier,
348     IDirect3D8Impl_GetAdapterModeCount,
349     IDirect3D8Impl_EnumAdapterModes,
350     IDirect3D8Impl_GetAdapterDisplayMode,
351     IDirect3D8Impl_CheckDeviceType,
352     IDirect3D8Impl_CheckDeviceFormat,
353     IDirect3D8Impl_CheckDeviceMultiSampleType,
354     IDirect3D8Impl_CheckDepthStencilMatch,
355     IDirect3D8Impl_GetDeviceCaps,
356     IDirect3D8Impl_GetAdapterMonitor,
357     IDirect3D8Impl_CreateDevice
358 };