dsound: Always enumerate the default device first.
[wine] / dlls / ddraw / tests / refcount.c
1 /*
2  * Some unit tests for ddraw reference counting
3  *
4  * Copyright (C) 2006 Stefan Dösinger
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 #define COBJMACROS
21
22 #include "wine/test.h"
23 #include "ddraw.h"
24 #include "d3d.h"
25 #include "unknwn.h"
26
27 static HRESULT (WINAPI *pDirectDrawCreateEx)(LPGUID,LPVOID*,REFIID,LPUNKNOWN);
28
29 static void init_function_pointers(void)
30 {
31     HMODULE hmod = GetModuleHandleA("ddraw.dll");
32     pDirectDrawCreateEx = (void*)GetProcAddress(hmod, "DirectDrawCreateEx");
33 }
34
35 static ULONG getRefcount(IUnknown *iface)
36 {
37     IUnknown_AddRef(iface);
38     return IUnknown_Release(iface);
39 }
40
41 static void test_ddraw_objects(void)
42 {
43     HRESULT hr;
44     ULONG ref;
45     IDirectDraw7 *DDraw7;
46     IDirectDraw4 *DDraw4;
47     IDirectDraw2 *DDraw2;
48     IDirectDraw  *DDraw1;
49     IDirectDrawPalette *palette;
50     IDirectDrawSurface7 *surface = NULL, *stencil;
51     IDirectDrawSurface *surface1, *stencil1;
52     IDirectDrawSurface4 *surface4;
53     PALETTEENTRY Table[256];
54     DDSURFACEDESC2 ddsd, ddsd_stencil;
55
56     hr = pDirectDrawCreateEx(NULL, (void **) &DDraw7, &IID_IDirectDraw7, NULL);
57     ok(hr == DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", hr);
58     if(!DDraw7)
59     {
60         trace("Couldn't create DDraw interface, skipping tests\n");
61         return;
62     }
63
64     hr = IDirectDraw7_QueryInterface(DDraw7, &IID_IDirectDraw4, (void **) &DDraw4);
65     ok(hr == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", hr);
66     hr = IDirectDraw7_QueryInterface(DDraw7, &IID_IDirectDraw2, (void **) &DDraw2);
67     ok(hr == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", hr);
68     hr = IDirectDraw7_QueryInterface(DDraw7, &IID_IDirectDraw, (void **) &DDraw1);
69     ok(hr == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", hr);
70
71     ref = getRefcount( (IUnknown *) DDraw7);
72     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
73
74     /* Fails without a cooplevel */
75     hr = IDirectDraw7_CreatePalette(DDraw7, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
76     ok(hr == DDERR_NOCOOPERATIVELEVELSET, "CreatePalette returned %08x\n", hr);
77
78     /* This check is before the cooplevel check */
79     hr = IDirectDraw7_CreatePalette(DDraw7, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, (void *) 0xdeadbeef);
80     ok(hr == CLASS_E_NOAGGREGATION, "CreatePalette returned %08x\n", hr);
81
82     hr = IDirectDraw7_SetCooperativeLevel(DDraw7, 0, DDSCL_NORMAL);
83     ok(hr == DD_OK, "SetCooperativeLevel failed with %08x\n", hr);
84
85     memset(&ddsd, 0, sizeof(ddsd));
86     ddsd.dwSize = sizeof(ddsd);
87     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
88     ddsd.dwWidth = 64;
89     ddsd.dwHeight = 64;
90     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
91     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
92     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
93     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 8;
94
95     memset(&ddsd_stencil, 0, sizeof(ddsd_stencil));
96     ddsd_stencil.dwSize = sizeof(ddsd_stencil);
97     ddsd_stencil.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
98     ddsd_stencil.dwWidth = ddsd.dwWidth;
99     ddsd_stencil.dwHeight = ddsd.dwHeight;
100     ddsd_stencil.ddsCaps.dwCaps = DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER;
101     U4(ddsd_stencil).ddpfPixelFormat.dwSize = sizeof(U4(ddsd_stencil).ddpfPixelFormat);
102     U4(ddsd_stencil).ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
103     U1(U4(ddsd_stencil).ddpfPixelFormat).dwZBufferBitDepth = 16;
104     U3(U4(ddsd_stencil).ddpfPixelFormat).dwZBitMask = 0x0000FFFF;
105
106     hr = IDirectDraw7_CreateSurface(DDraw7, &ddsd, &surface, NULL);
107     if (!surface)
108     {
109         win_skip("Could not create surface : %08x\n", hr);
110         IDirectDraw7_Release(DDraw7);
111         return;
112     }
113     ok(hr == DD_OK, "CreateSurface failed with %08x\n", hr);
114
115     /* DDraw refcount increased by 1 */
116     ref = getRefcount( (IUnknown *) DDraw7);
117     ok(ref == 2, "Got refcount %d, expected 2\n", ref);
118
119     /* Surface refcount starts with 1 */
120     ref = getRefcount( (IUnknown *) surface);
121     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
122
123     hr = IDirectDraw7_CreatePalette(DDraw7, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
124     ok(hr == DD_OK, "CreatePalette returned %08x\n", hr);
125
126     /* DDraw refcount increased by 1 */
127     ref = getRefcount( (IUnknown *) DDraw7);
128     ok(ref == 3, "Got refcount %d, expected 3\n", ref);
129
130     /* Palette starts with 1 */
131     ref = getRefcount( (IUnknown *) palette);
132     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
133
134     /* Test attaching a palette to a surface */
135     hr = IDirectDrawSurface7_SetPalette(surface, palette);
136     ok(hr == DD_OK, "IDirectDrawSurface_SetPalette failed with %08x\n", hr);
137
138     /* Palette refcount increased, surface stays the same */
139     ref = getRefcount( (IUnknown *) palette);
140     ok(ref == 2, "Got refcount %d, expected 2\n", ref);
141     ref = getRefcount( (IUnknown *) surface);
142     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
143
144     IDirectDrawSurface7_Release(surface);
145     /* Increased before - decrease now */
146     ref = getRefcount( (IUnknown *) DDraw7);
147     ok(ref == 2, "Got refcount %d, expected 2\n", ref);
148
149     /* Releasing the surface detaches the palette */
150     ref = getRefcount( (IUnknown *) palette);
151     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
152
153     IDirectDrawPalette_Release(palette);
154
155     /* Increased before - decrease now */
156     ref = getRefcount( (IUnknown *) DDraw7);
157     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
158
159     /* Not all interfaces are AddRefed when a palette is created */
160     hr = IDirectDraw4_CreatePalette(DDraw4, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
161     ok(hr == DD_OK, "CreatePalette returned %08x\n", hr);
162     ref = getRefcount( (IUnknown *) DDraw4);
163     ok(ref == 2, "Got refcount %d, expected 2\n", ref);
164     IDirectDrawPalette_Release(palette);
165
166     /* No addref here */
167     hr = IDirectDraw2_CreatePalette(DDraw2, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
168     ok(hr == DD_OK, "CreatePalette returned %08x\n", hr);
169     ref = getRefcount( (IUnknown *) DDraw2);
170     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
171     IDirectDrawPalette_Release(palette);
172
173     /* No addref here */
174     hr = IDirectDraw_CreatePalette(DDraw1, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
175     ok(hr == DD_OK, "CreatePalette returned %08x\n", hr);
176     ref = getRefcount( (IUnknown *) DDraw1);
177     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
178     IDirectDrawPalette_Release(palette);
179
180     /* Similar for surfaces */
181     hr = IDirectDraw4_CreateSurface(DDraw4, &ddsd, &surface4, NULL);
182     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
183     ref = getRefcount( (IUnknown *) DDraw4);
184     ok(ref == 2, "Got refcount %d, expected 2\n", ref);
185     IDirectDrawSurface4_Release(surface4);
186
187     ddsd.dwSize = sizeof(DDSURFACEDESC);
188     hr = IDirectDraw2_CreateSurface(DDraw2, (DDSURFACEDESC *) &ddsd, &surface1, NULL);
189     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
190     ref = getRefcount( (IUnknown *) DDraw2);
191     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
192     IDirectDrawSurface_Release(surface1);
193
194     hr = IDirectDraw_CreateSurface(DDraw1, (DDSURFACEDESC *) &ddsd, &surface1, NULL);
195     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
196     ref = getRefcount( (IUnknown *) DDraw1);
197     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
198     IDirectDrawSurface_Release(surface1);
199
200     /* AddAttachedSurface with IDirectDrawSurface7 */
201     ddsd.dwSize = sizeof(DDSURFACEDESC2);
202     ddsd.ddsCaps.dwCaps |= DDSCAPS_3DDEVICE;
203     hr = IDirectDraw7_CreateSurface(DDraw7, &ddsd, &surface, NULL);
204     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
205     if (SUCCEEDED(hr))
206     {
207         ddsd_stencil.dwSize = sizeof(DDSURFACEDESC2);
208         hr = IDirectDraw7_CreateSurface(DDraw7, &ddsd_stencil, &stencil, NULL);
209         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
210         if (SUCCEEDED(hr))
211         {
212             /* AddAttachedSurface with DeleteAttachedSurface */
213             hr = IDirectDrawSurface7_AddAttachedSurface(surface, stencil);
214             ok(hr == DD_OK, "AddAttachedSurface returned %08x\n", hr);
215             if (SUCCEEDED(hr))
216             {
217                 ref = getRefcount( (IUnknown *) stencil);
218                 ok(ref == 2, "Got refcount %d, expected 2\n", ref);
219                 hr = IDirectDrawSurface7_QueryInterface(surface, &IID_IDirectDrawSurface, (void **) &surface1);
220                 ok(hr == DD_OK, "IDirectDrawSurface7_QueryInterface returned %08x\n", hr);
221                 hr = IDirectDrawSurface7_QueryInterface(stencil, &IID_IDirectDrawSurface, (void **) &stencil1);
222                 ok(hr == DD_OK, "IDirectDrawSurface7_QueryInterface returned %08x\n", hr);
223                 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, stencil1);
224                 ok(hr == DDERR_SURFACENOTATTACHED, "DeleteAttachedSurface returned %08x\n", hr);
225                 if (stencil1 != NULL) IDirectDrawSurface_Release(stencil1);
226                 if (surface1 != NULL) IDirectDrawSurface_Release(surface1);
227                 hr = IDirectDrawSurface7_DeleteAttachedSurface(surface, 0, stencil);
228                 ok(hr == DD_OK, "DeleteAttachedSurface returned %08x\n", hr);
229                 ref = getRefcount( (IUnknown *) stencil);
230                 ok(ref == 1, "Got refcount %d, expected 1\n", ref);
231             }
232
233             /* Releasing a surface should detach any attached surfaces */
234             hr = IDirectDrawSurface7_AddAttachedSurface(surface, stencil);
235             ok(hr == DD_OK, "AddAttachedSurface returned %08x\n", hr);
236             ref = getRefcount( (IUnknown *) stencil);
237             ok(ref == 2, "Got refcount %d, expected 2\n", ref);
238             ref = IDirectDrawSurface7_Release(surface);
239             ok(!ref, "Got refcount %d, expected 0\n", ref);
240             ref = getRefcount( (IUnknown *) stencil);
241             ok(ref == 1, "Got refcount %d, expected 1\n", ref);
242             ref = IDirectDrawSurface7_Release(stencil);
243             ok(!ref, "Got refcount %d, expected 0\n", ref);
244         }
245         else
246             IDirectDrawSurface7_Release(surface);
247     }
248
249     /* AddAttachedSurface with IDirectDrawSurface */
250     ddsd.dwSize = sizeof(DDSURFACEDESC);
251     hr = IDirectDraw_CreateSurface(DDraw1, (DDSURFACEDESC *) &ddsd, &surface1, NULL);
252     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
253     if (SUCCEEDED(hr))
254     {
255         ddsd_stencil.dwSize = sizeof(DDSURFACEDESC);
256         hr = IDirectDraw_CreateSurface(DDraw1, (DDSURFACEDESC *) &ddsd_stencil, &stencil1, NULL);
257         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
258         if (SUCCEEDED(hr))
259         {
260             /* AddAttachedSurface with DeleteAttachedSurface */
261             hr = IDirectDrawSurface_AddAttachedSurface(surface1, stencil1);
262             ok(hr == DD_OK, "AddAttachedSurface returned %08x\n", hr);
263             if (SUCCEEDED(hr))
264             {
265                 ref = getRefcount( (IUnknown *) stencil1);
266                 ok(ref == 2, "Got refcount %d, expected 2\n", ref);
267                 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, stencil1);
268                 ok(hr == DD_OK, "DeleteAttachedSurface returned %08x\n", hr);
269                 ref = getRefcount( (IUnknown *) stencil1);
270                 ok(ref == 1, "Got refcount %d, expected 1\n", ref);
271             }
272
273             /* Releasing a surface should detach any attached surfaces */
274             hr = IDirectDrawSurface_AddAttachedSurface(surface1, stencil1);
275             ok(hr == DD_OK, "AddAttachedSurface returned %08x\n", hr);
276             ref = getRefcount( (IUnknown *) stencil1);
277             ok(ref == 2, "Got refcount %d, expected 2\n", ref);
278             ref = IDirectDrawSurface_Release(surface1);
279             ok(!ref, "Got refcount %d, expected 0\n", ref);
280             ref = getRefcount( (IUnknown *) stencil1);
281             ok(ref == 1, "Got refcount %d, expected 1\n", ref);
282             ref = IDirectDrawSurface_Release(stencil1);
283             ok(!ref, "Got refcount %d, expected 0\n", ref);
284         }
285         else
286             IDirectDrawSurface_Release(surface1);
287     }
288
289     IDirectDraw7_Release(DDraw7);
290     IDirectDraw4_Release(DDraw4);
291     IDirectDraw2_Release(DDraw2);
292     IDirectDraw_Release(DDraw1);
293 }
294
295 static void test_iface_refcnt(void)
296 {
297     HRESULT hr;
298     IDirectDraw  *DDraw1;
299     IDirectDraw2 *DDraw2;
300     IDirectDraw4 *DDraw4;
301     IDirectDraw7 *DDraw7;
302     IDirect3D7   *D3D7;
303     IDirect3D3   *D3D3;
304     IDirect3D2   *D3D2;
305     IDirect3D    *D3D1;
306     long ref;
307
308     hr = pDirectDrawCreateEx(NULL, (void **) &DDraw7, &IID_IDirectDraw7, NULL);
309     ok(hr == DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", hr);
310     if(!DDraw7)
311     {
312         trace("Couldn't create DDraw interface, skipping tests\n");
313         return;
314     }
315
316     ref = getRefcount( (IUnknown *) DDraw7);
317     ok(ref == 1, "Initial IDirectDraw7 reference count is %ld\n", ref);
318
319     hr = IDirectDraw7_QueryInterface(DDraw7, &IID_IDirectDraw4, (void **) &DDraw4);
320     ok(hr == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", hr);
321     hr = IDirectDraw7_QueryInterface(DDraw7, &IID_IDirectDraw2, (void **) &DDraw2);
322     ok(hr == DD_OK, "IDirectDraw7_QueryInterf&ace returned %08x\n", hr);
323     hr = IDirectDraw7_QueryInterface(DDraw7, &IID_IDirectDraw, (void **) &DDraw1);
324     ok(hr == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", hr);
325
326     /* All interfaces now have refcount 1! */
327     ref = getRefcount( (IUnknown *) DDraw7);
328     ok(ref == 1, "IDirectDraw7 reference count is %ld\n", ref);
329     ref = getRefcount( (IUnknown *) DDraw7);
330     ok(ref == 1, "IDirectDraw7 reference count is %ld\n", ref);
331     ref = getRefcount( (IUnknown *) DDraw4);
332     ok(ref == 1, "IDirectDraw4 reference count is %ld\n", ref);
333     ref = getRefcount( (IUnknown *) DDraw2);
334     ok(ref == 1, "IDirectDraw2 reference count is %ld\n", ref);
335     ref = getRefcount( (IUnknown *) DDraw1);
336     ok(ref == 1, "IDirectDraw reference count is %ld\n", ref);
337
338     hr = IDirectDraw7_QueryInterface(DDraw7, &IID_IDirect3D7, (void **) &D3D7);
339     ok(hr == DD_OK || hr == E_NOINTERFACE, /* win64 */
340        "IDirectDraw7_QueryInterface returned %08x\n", hr);
341     if (FAILED(hr))
342     {
343         IDirectDraw7_Release(DDraw7);
344         IDirectDraw4_Release(DDraw4);
345         IDirectDraw2_Release(DDraw2);
346         IDirectDraw_Release(DDraw1);
347         skip( "no IDirect3D7 support\n" );
348         return;
349     }
350
351     /* Apparently IDirectDrawX and IDirect3DX are linked together */
352     ref = getRefcount( (IUnknown *) D3D7);
353     ok(ref == 2, "IDirect3D7 reference count is %ld\n", ref);
354     ref = getRefcount( (IUnknown *) DDraw7);
355     ok(ref == 2, "IDirectDraw7 reference count is %ld\n", ref);
356
357     IDirectDraw7_AddRef(DDraw7);
358     ref = getRefcount( (IUnknown *) D3D7);
359     ok(ref == 3, "IDirect3D7 reference count is %ld\n", ref);
360     ref = getRefcount( (IUnknown *) DDraw7);
361     ok(ref == 3, "IDirectDraw7 reference count is %ld\n", ref);
362
363     IDirect3D7_Release(D3D7);
364     ref = getRefcount( (IUnknown *) D3D7);
365     ok(ref == 2, "IDirect3D7 reference count is %ld\n", ref);
366     ref = getRefcount( (IUnknown *) DDraw7);
367     ok(ref == 2, "IDirectDraw7 reference count is %ld\n", ref);
368
369     /* Can't get older d3d interfaces. WHY????? */
370     hr = IDirectDraw7_QueryInterface(DDraw7, &IID_IDirect3D3, (void **) &D3D3);
371     todo_wine ok(hr == E_NOINTERFACE, "IDirectDraw7_QueryInterface returned %08x\n", hr);
372     if(hr == DD_OK && D3D3) IDirect3D3_Release(D3D3);
373
374     hr = IDirectDraw4_QueryInterface(DDraw4, &IID_IDirect3D3, (void **) &D3D3);
375     todo_wine ok(hr == E_NOINTERFACE, "IDirectDraw4_QueryInterface returned %08x\n", hr);
376     if(hr == DD_OK && D3D3) IDirect3D3_Release(D3D3);
377
378     hr = IDirectDraw7_QueryInterface(DDraw7, &IID_IDirect3D2, (void **) &D3D2);
379     todo_wine ok(hr == E_NOINTERFACE, "IDirectDraw7_QueryInterface returned %08x\n", hr);
380     if(hr == DD_OK && D3D2) IDirect3D2_Release(D3D2);
381
382     hr = IDirectDraw2_QueryInterface(DDraw2, &IID_IDirect3D2, (void **) &D3D2);
383     todo_wine ok(hr == E_NOINTERFACE, "IDirectDraw2_QueryInterface returned %08x\n", hr);
384     if(hr == DD_OK && D3D2) IDirect3D2_Release(D3D2);
385
386     hr = IDirectDraw7_QueryInterface(DDraw7, &IID_IDirect3D, (void **) &D3D1);
387     todo_wine ok(hr == E_NOINTERFACE, "IDirectDraw7_QueryInterface returned %08x\n", hr);
388     if(hr == DD_OK && D3D1) IDirect3D_Release(D3D1);
389
390     hr = IDirectDraw_QueryInterface(DDraw1, &IID_IDirect3D, (void **) &D3D1);
391     todo_wine ok(hr == E_NOINTERFACE, "IDirectDraw_QueryInterface returned %08x\n", hr);
392     if(hr == DD_OK && D3D1) IDirect3D_Release(D3D1);
393
394     hr = IDirect3D7_QueryInterface(D3D7, &IID_IDirect3D, (void **) &D3D1);
395     todo_wine ok(hr == E_NOINTERFACE, "IDirect3D7_QueryInterface returned %08x\n", hr);
396     if(hr == DD_OK && D3D1) IDirect3D_Release(D3D1);
397
398     /* Try an AddRef, it only affects the AddRefed interface */
399     IDirectDraw4_AddRef(DDraw4);
400     ref = getRefcount( (IUnknown *) DDraw7);
401     ok(ref == 2, "IDirectDraw7 reference count is %ld\n", ref); /* <-- From the d3d query */
402     ref = getRefcount( (IUnknown *) DDraw4);
403     ok(ref == 2, "IDirectDraw4 reference count is %ld\n", ref); /* <-- The AddRef call */
404     ref = getRefcount( (IUnknown *) DDraw2);
405     ok(ref == 1, "IDirectDraw2 reference count is %ld\n", ref);
406     ref = getRefcount( (IUnknown *) DDraw1);
407     ok(ref == 1, "IDirectDraw reference count is %ld\n", ref);
408     ref = getRefcount( (IUnknown *) D3D7);
409     ok(ref == 2, "IDirect3D7 reference count is %ld\n", ref); /* <-- From the d3d query */
410     IDirectDraw4_Release(DDraw4);
411
412     /* Make sure that they are one object, not different ones */
413     hr = IDirectDraw4_SetCooperativeLevel(DDraw4, GetDesktopWindow(), DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
414     ok(hr == DD_OK, "IDirectDraw4::SetCooperativeLevel returned %08x\n", hr);
415     /* After an window has been set, DDSCL_SETFOCUSWINDOW should return DDERR_HWNDALREADYSET, see the mode test */
416     hr = IDirectDraw7_SetCooperativeLevel(DDraw7, NULL, DDSCL_SETFOCUSWINDOW);
417     ok(hr == DDERR_HWNDALREADYSET, "IDirectDraw7::SetCooperativeLevel returned %08x\n", hr);
418
419     /* All done, release all interfaces */
420     IDirectDraw7_Release(DDraw7);
421     IDirectDraw4_Release(DDraw4);
422     IDirectDraw2_Release(DDraw2);
423     IDirectDraw_Release(DDraw1);
424     IDirect3D7_Release(D3D7);
425 }
426
427 static void test_d3d_ifaces(void)
428 {
429     IDirectDraw *DDraw1;
430     IDirectDraw2 *DDraw2;
431     IDirectDraw4 *DDraw4;
432     IDirect3D *D3D1;
433     IDirect3D2 *D3D2;
434     IDirect3D3 *D3D3;
435     IDirect3D7 *D3D7;
436     HRESULT hr;
437     long ref;
438
439     hr = DirectDrawCreate(NULL, &DDraw1, NULL);
440     ok(hr == DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", hr);
441     if(!DDraw1)
442     {
443         trace("DirectDrawCreate failed with %08x\n", hr);
444         return;
445     }
446
447     hr = IDirectDraw_QueryInterface(DDraw1, &IID_IDirectDraw2, (void **) &DDraw2);
448     ok(hr == DD_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
449     hr = IDirectDraw_QueryInterface(DDraw1, &IID_IDirectDraw4, (void **) &DDraw4);
450     ok(hr == DD_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
451
452     ref = getRefcount( (IUnknown *) DDraw4);
453     ok(ref == 1, "IDirectDraw4 reference count is %ld\n", ref);
454     ref = getRefcount( (IUnknown *) DDraw2);
455     ok(ref == 1, "IDirectDraw2 reference count is %ld\n", ref);
456     ref = getRefcount( (IUnknown *) DDraw1);
457     ok(ref == 1, "IDirectDraw reference count is %ld\n", ref);
458
459     hr = IDirectDraw_QueryInterface(DDraw1, &IID_IDirect3D, (void **) &D3D1);
460     if (hr == E_NOINTERFACE)  /* win64 */
461     {
462         IDirectDraw4_Release(DDraw4);
463         IDirectDraw2_Release(DDraw2);
464         IDirectDraw_Release(DDraw1);
465         skip( "no IDirect3D support\n" );
466         return;
467     }
468     ok(hr == DD_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
469     ref = getRefcount( (IUnknown *) DDraw4);
470     ok(ref == 1, "IDirectDraw4 reference count is %ld\n", ref);
471     ref = getRefcount( (IUnknown *) DDraw2);
472     ok(ref == 1, "IDirectDraw2 reference count is %ld\n", ref);
473     ref = getRefcount( (IUnknown *) DDraw1);
474     ok(ref == 2, "IDirectDraw reference count is %ld\n", ref);
475     IDirect3D_Release(D3D1);
476
477     hr = IDirectDraw2_QueryInterface(DDraw2, &IID_IDirect3D2, (void **) &D3D2);
478     ok(hr == DD_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
479     ref = getRefcount( (IUnknown *) DDraw4);
480     ok(ref == 1, "IDirectDraw4 reference count is %ld\n", ref);
481     ref = getRefcount( (IUnknown *) DDraw2);
482     ok(ref == 1, "IDirectDraw2 reference count is %ld\n", ref);
483     ref = getRefcount( (IUnknown *) DDraw1);
484     ok(ref == 2, "IDirectDraw reference count is %ld\n", ref);
485     IDirect3D2_Release(D3D2);
486
487     hr = IDirectDraw4_QueryInterface(DDraw4, &IID_IDirect3D3, (void **) &D3D3);
488     ok(hr == DD_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
489     ref = getRefcount( (IUnknown *) DDraw4);
490     ok(ref == 1, "IDirectDraw4 reference count is %ld\n", ref);
491     ref = getRefcount( (IUnknown *) DDraw2);
492     ok(ref == 1, "IDirectDraw2 reference count is %ld\n", ref);
493     ref = getRefcount( (IUnknown *) DDraw1);
494     ok(ref == 2, "IDirectDraw reference count is %ld\n", ref);
495     IDirect3D3_Release(D3D3);
496
497     /* Try to AddRef the D3D3 interface that has been released already */
498     IDirect3D3_AddRef(D3D3);
499     ref = getRefcount( (IUnknown *) DDraw1);
500     ok(ref == 2, "IDirectDraw reference count is %ld\n", ref);
501     ref = getRefcount( (IUnknown *) D3D3);
502     ok(ref == 2, "IDirect3D3 reference count is %ld\n", ref);
503     /* The newer interfaces remain untouched */
504     ref = getRefcount( (IUnknown *) DDraw4);
505     ok(ref == 1, "IDirectDraw4 reference count is %ld\n", ref);
506     ref = getRefcount( (IUnknown *) DDraw2);
507     ok(ref == 1, "IDirectDraw2 reference count is %ld\n", ref);
508     IDirect3D3_Release(D3D3);
509     ref = getRefcount( (IUnknown *) DDraw1);
510     ok(ref == 1, "IDirectDraw reference count is %ld\n", ref);
511     ref = getRefcount( (IUnknown *) DDraw1);
512     ok(ref == 1, "IDirectDraw reference count is %ld\n", ref);
513
514     /* It is possible to query any IDirect3D interfaces from any IDirectDraw interface,
515      * Except IDirect3D7, it can only be returned by IDirectDraw7(which can't return older ifaces)
516      */
517     hr = IDirectDraw_QueryInterface(DDraw2, &IID_IDirect3D, (void **) &D3D1);
518     ok(hr == DD_OK, "IDirectDraw2_QueryInterface returned %08x\n", hr);
519     IDirect3D_Release(D3D1);
520     hr = IDirectDraw4_QueryInterface(DDraw4, &IID_IDirect3D, (void **) &D3D1);
521     ok(hr == DD_OK, "IDirectDraw4_QueryInterface returned %08x\n", hr);
522     IDirect3D_Release(D3D1);
523
524     hr = IDirectDraw_QueryInterface(DDraw1, &IID_IDirect3D2, (void **) &D3D2);
525     ok(hr == DD_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
526     IDirect3D_Release(D3D2);
527     hr = IDirectDraw4_QueryInterface(DDraw4, &IID_IDirect3D2, (void **) &D3D2);
528     ok(hr == DD_OK, "IDirectDraw4_QueryInterface returned %08x\n", hr);
529     IDirect3D_Release(D3D2);
530
531     hr = IDirectDraw_QueryInterface(DDraw1, &IID_IDirect3D3, (void **) &D3D3);
532     ok(hr == DD_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
533     IDirect3D_Release(D3D3);
534     hr = IDirectDraw2_QueryInterface(DDraw2, &IID_IDirect3D3, (void **) &D3D3);
535     ok(hr == DD_OK, "IDirectDraw2_QueryInterface returned %08x\n", hr);
536     IDirect3D_Release(D3D3);
537
538     /* This does NOT work */
539     hr = IDirectDraw_QueryInterface(DDraw1, &IID_IDirect3D7, (void **) &D3D7);
540     todo_wine ok(hr == E_NOINTERFACE, "IDirectDraw_QueryInterface returned %08x\n", hr);
541     if(D3D7) IDirect3D_Release(D3D7);
542     hr = IDirectDraw2_QueryInterface(DDraw2, &IID_IDirect3D7, (void **) &D3D7);
543     todo_wine ok(hr == E_NOINTERFACE, "IDirectDraw2_QueryInterface returned %08x\n", hr);
544     if(D3D7) IDirect3D_Release(D3D7);
545     hr = IDirectDraw4_QueryInterface(DDraw4, &IID_IDirect3D7, (void **) &D3D7);
546     todo_wine ok(hr == E_NOINTERFACE, "IDirectDraw4_QueryInterface returned %08x\n", hr);
547     if(D3D7) IDirect3D_Release(D3D7);
548
549     /* Release the interfaces */
550     IDirectDraw4_Release(DDraw4);
551     IDirectDraw2_Release(DDraw2);
552     IDirectDraw_Release(DDraw1);
553 }
554
555 START_TEST(refcount)
556 {
557     init_function_pointers();
558     if(!pDirectDrawCreateEx)
559     {
560         win_skip("function DirectDrawCreateEx not available\n");
561         return;
562     }
563     test_ddraw_objects();
564     test_iface_refcnt();
565     test_d3d_ifaces();
566 }