d3drm: Avoid LPDDPIXELFORMAT.
[wine] / dlls / ddrawex / tests / surface.c
1 /*
2  * Unit tests for ddrawex surfaces
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 #define COBJMACROS
19 /* For IID_IDirectDraw3 - it is not in dxguid.dll */
20 #define INITGUID
21
22 #include "wine/test.h"
23 #include "windef.h"
24 #include "winbase.h"
25 #include "ddraw.h"
26 #include "ddrawex.h"
27 #include "unknwn.h"
28
29 static IDirectDrawFactory *factory;
30 static HRESULT (WINAPI *pDllGetClassObject)(REFCLSID rclsid, REFIID riid, LPVOID *ppv);
31
32 static IDirectDraw *createDD(void)
33 {
34     HRESULT hr;
35     IDirectDraw *dd;
36
37     hr = IDirectDrawFactory_CreateDirectDraw(factory, NULL, NULL, DDSCL_NORMAL, 0,
38                                              0, &dd);
39     ok(hr == DD_OK, "Failed to create an IDirectDraw interface, hr = 0x%08x\n", hr);
40     return SUCCEEDED(hr) ? dd : NULL;
41 }
42
43 static void dctest_surf(IDirectDrawSurface *surf, int ddsdver) {
44     HRESULT hr;
45     HDC dc, dc2 = (HDC) 0x1234;
46     DDSURFACEDESC ddsd;
47     DDSURFACEDESC2 ddsd2;
48
49     memset(&ddsd, 0, sizeof(ddsd));
50     ddsd.dwSize = sizeof(ddsd);
51     memset(&ddsd2, 0, sizeof(ddsd2));
52     ddsd2.dwSize = sizeof(ddsd2);
53
54     hr = IDirectDrawSurface_GetDC(surf, &dc);
55     ok(hr == DD_OK, "IDirectDrawSurface_GetDC failed: 0x%08x\n", hr);
56
57     hr = IDirectDrawSurface_GetDC(surf, &dc);
58     ok(hr == DDERR_DCALREADYCREATED, "IDirectDrawSurface_GetDC failed: 0x%08x\n", hr);
59     ok(dc2 == (HDC) 0x1234, "The failed GetDC call changed the dc: %p\n", dc2);
60
61     hr = IDirectDrawSurface_Lock(surf, NULL, ddsdver == 1 ? &ddsd : ((DDSURFACEDESC *) &ddsd2), 0, NULL);
62     ok(hr == DDERR_SURFACEBUSY, "IDirectDrawSurface_Lock returned 0x%08x, expected DDERR_SURFACEBUSY\n", hr);
63
64     hr = IDirectDrawSurface_ReleaseDC(surf, dc);
65     ok(hr == DD_OK, "IDirectDrawSurface_ReleaseDC failed: 0x%08x\n", hr);
66     hr = IDirectDrawSurface_ReleaseDC(surf, dc);
67     ok(hr == DDERR_NODC, "IDirectDrawSurface_ReleaseDC returned 0x%08x, expected DDERR_NODC\n", hr);
68 }
69
70 static void GetDCTest_main(DDSURFACEDESC *ddsd, DDSURFACEDESC2 *ddsd2, void (*testfunc)(IDirectDrawSurface *surf, int ddsdver))
71 {
72     IDirectDrawSurface *surf;
73     IDirectDrawSurface2 *surf2;
74     IDirectDrawSurface2 *surf3;
75     IDirectDrawSurface4 *surf4;
76     HRESULT hr;
77     IDirectDraw  *dd1 = createDD();
78     IDirectDraw2 *dd2;
79     IDirectDraw3 *dd3;
80     IDirectDraw4 *dd4;
81
82     hr = IDirectDraw_CreateSurface(dd1, ddsd, &surf, NULL);
83     if (hr == DDERR_UNSUPPORTEDMODE) {
84         win_skip("Unsupported mode\n");
85         return;
86     }
87     ok(hr == DD_OK, "IDirectDraw_CreateSurface failed: 0x%08x\n", hr);
88     testfunc(surf, 1);
89     IDirectDrawSurface_Release(surf);
90
91     hr = IDirectDraw_QueryInterface(dd1, &IID_IDirectDraw2, (void **) &dd2);
92     ok(hr == DD_OK, "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
93
94     hr = IDirectDraw2_CreateSurface(dd2, ddsd, &surf, NULL);
95     ok(hr == DD_OK, "IDirectDraw2_CreateSurface failed: 0x%08x\n", hr);
96     testfunc(surf, 1);
97
98     hr = IDirectDrawSurface_QueryInterface(surf, &IID_IDirectDrawSurface2, (void **) &surf2);
99     ok(hr == DD_OK, "IDirectDrawSurface_QueryInterface failed: 0x%08x\n", hr);
100     testfunc((IDirectDrawSurface *) surf2, 1);
101
102     IDirectDrawSurface2_Release(surf2);
103     IDirectDrawSurface_Release(surf);
104     IDirectDraw2_Release(dd2);
105
106     hr = IDirectDraw_QueryInterface(dd1, &IID_IDirectDraw3, (void **) &dd3);
107     ok(hr == DD_OK, "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
108
109     hr = IDirectDraw3_CreateSurface(dd3, ddsd, &surf, NULL);
110     ok(hr == DD_OK, "IDirectDraw3_CreateSurface failed: 0x%08x\n", hr);
111     testfunc(surf, 1);
112
113     hr = IDirectDrawSurface_QueryInterface(surf, &IID_IDirectDrawSurface3, (void **) &surf3);
114     ok(hr == DD_OK, "IDirectDrawSurface_QueryInterface failed: 0x%08x\n", hr);
115     testfunc((IDirectDrawSurface *) surf3, 1);
116
117     IDirectDrawSurface3_Release(surf3);
118     IDirectDrawSurface_Release(surf);
119     IDirectDraw3_Release(dd3);
120
121     hr = IDirectDraw_QueryInterface(dd1, &IID_IDirectDraw4, (void **) &dd4);
122     ok(hr == DD_OK, "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
123
124     surf = NULL;
125     hr = IDirectDraw4_CreateSurface(dd4, ddsd2, &surf4, NULL);
126     ok(hr == DD_OK, "IDirectDraw4_CreateSurface failed: 0x%08x\n", hr);
127     testfunc((IDirectDrawSurface *) surf4, 2);
128
129     IDirectDrawSurface4_Release(surf4);
130     IDirectDraw4_Release(dd4);
131
132     IDirectDraw_Release(dd1);
133 }
134
135 static void GetDCTest(void)
136 {
137     DDSURFACEDESC ddsd;
138     DDSURFACEDESC2 ddsd2;
139
140     memset(&ddsd, 0, sizeof(ddsd));
141     ddsd.dwSize = sizeof(ddsd);
142     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
143     ddsd.dwWidth = 64;
144     ddsd.dwHeight = 64;
145     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
146     memset(&ddsd2, 0, sizeof(ddsd2));
147     ddsd2.dwSize = sizeof(ddsd2);
148     ddsd2.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
149     ddsd2.dwWidth = 64;
150     ddsd2.dwHeight = 64;
151     ddsd2.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
152
153     GetDCTest_main(&ddsd, &ddsd2, dctest_surf);
154 }
155
156 static void CapsTest(void)
157 {
158     DDSURFACEDESC ddsd;
159     IDirectDraw  *dd1 = createDD();
160     IDirectDrawSurface *surf;
161     HRESULT hr;
162
163     memset(&ddsd, 0, sizeof(ddsd));
164     ddsd.dwSize = sizeof(ddsd);
165     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
166     ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_VIDEOMEMORY;
167     ddsd.dwWidth = 64;
168     ddsd.dwHeight = 64;
169     hr = IDirectDraw_CreateSurface(dd1, &ddsd, &surf, NULL);
170     if (hr == DDERR_UNSUPPORTEDMODE) {
171         win_skip("Unsupported mode\n");
172         return;
173     }
174     ok(hr == DD_OK, "Creating a SYSMEM | VIDMEM surface returned 0x%08x, expected DD_OK\n", hr);
175     if(surf) IDirectDrawSurface_Release(surf);
176
177     IDirectDraw_Release(dd1);
178 }
179
180 static void dctest_sysvidmem(IDirectDrawSurface *surf, int ddsdver) {
181     HRESULT hr;
182     HDC dc, dc2 = (HDC) 0x1234;
183     DDSURFACEDESC ddsd;
184     DDSURFACEDESC2 ddsd2;
185
186     memset(&ddsd, 0, sizeof(ddsd));
187     ddsd.dwSize = sizeof(ddsd);
188     memset(&ddsd2, 0, sizeof(ddsd2));
189     ddsd2.dwSize = sizeof(ddsd2);
190
191     hr = IDirectDrawSurface_GetDC(surf, &dc);
192     ok(hr == DD_OK, "IDirectDrawSurface_GetDC failed: 0x%08x\n", hr);
193
194     hr = IDirectDrawSurface_GetDC(surf, &dc2);
195     ok(hr == DD_OK, "IDirectDrawSurface_GetDC failed: 0x%08x\n", hr);
196     ok(dc == dc2, "Got two different DCs\n");
197
198     hr = IDirectDrawSurface_Lock(surf, NULL, ddsdver == 1 ? &ddsd : ((DDSURFACEDESC *) &ddsd2), 0, NULL);
199     ok(hr == DD_OK, "IDirectDrawSurface_Lock returned 0x%08x, expected DD_OK\n", hr);
200
201     hr = IDirectDrawSurface_Lock(surf, NULL, ddsdver == 1 ? &ddsd : ((DDSURFACEDESC *) &ddsd2), 0, NULL);
202     ok(hr == DDERR_SURFACEBUSY, "IDirectDrawSurface_Lock returned 0x%08x, expected DDERR_SURFACEBUSY\n", hr);
203
204     hr = IDirectDrawSurface_Unlock(surf, NULL);
205     ok(hr == DD_OK, "IDirectDrawSurface_Unlock returned 0x%08x, expected DD_OK\n", hr);
206     hr = IDirectDrawSurface_Unlock(surf, NULL);
207     ok(hr == DDERR_NOTLOCKED, "IDirectDrawSurface_Unlock returned 0x%08x, expected DDERR_NOTLOCKED\n", hr);
208
209     hr = IDirectDrawSurface_ReleaseDC(surf, dc);
210     ok(hr == DD_OK, "IDirectDrawSurface_ReleaseDC failed: 0x%08x\n", hr);
211     hr = IDirectDrawSurface_ReleaseDC(surf, dc);
212     ok(hr == DD_OK, "IDirectDrawSurface_ReleaseDC failed: 0x%08x\n", hr);
213     /* That works any number of times... */
214     hr = IDirectDrawSurface_ReleaseDC(surf, dc);
215     ok(hr == DD_OK, "IDirectDrawSurface_ReleaseDC failed: 0x%08x\n", hr);
216 }
217
218 static void SysVidMemTest(void)
219 {
220     DDSURFACEDESC ddsd;
221     DDSURFACEDESC2 ddsd2;
222
223     memset(&ddsd, 0, sizeof(ddsd));
224     ddsd.dwSize = sizeof(ddsd);
225     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
226     ddsd.dwWidth = 64;
227     ddsd.dwHeight = 64;
228     ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_VIDEOMEMORY;
229     memset(&ddsd2, 0, sizeof(ddsd2));
230     ddsd2.dwSize = sizeof(ddsd2);
231     ddsd2.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
232     ddsd2.dwWidth = 64;
233     ddsd2.dwHeight = 64;
234     ddsd2.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_VIDEOMEMORY;
235
236     GetDCTest_main(&ddsd, &ddsd2, dctest_sysvidmem);
237 }
238
239 static void test_surface_from_dc3(void)
240 {
241     IDirectDrawSurface3 *surf3;
242     IDirectDrawSurface *surf1;
243     IDirectDrawSurface *tmp;
244     DDSURFACEDESC ddsd;
245     IDirectDraw3 *dd3;
246     IDirectDraw *dd1;
247     HRESULT hr;
248     HDC dc;
249
250     dd1 = createDD();
251     hr = IDirectDraw_QueryInterface(dd1, &IID_IDirectDraw3, (void **)&dd3);
252     ok(SUCCEEDED(hr), "IDirectDraw_QueryInterface failed, hr %#x.\n", hr);
253     IDirectDraw_Release(dd1);
254
255     memset(&ddsd, 0, sizeof(ddsd));
256     ddsd.dwSize = sizeof(ddsd);
257     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
258     ddsd.dwWidth = 64;
259     ddsd.dwHeight = 64;
260     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
261
262     hr = IDirectDraw3_CreateSurface(dd3, &ddsd, &surf1, NULL);
263     if (hr == DDERR_UNSUPPORTEDMODE) {
264         win_skip("Unsupported mode\n");
265         IDirectDraw3_Release(dd3);
266         return;
267     }
268     ok(SUCCEEDED(hr), "CreateSurface failed, hr %#x.\n", hr);
269
270     hr = IDirectDrawSurface3_QueryInterface(surf1, &IID_IDirectDrawSurface, (void **)&surf3);
271     ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
272     IDirectDrawSurface_Release(surf1);
273
274     hr = IDirectDrawSurface3_GetDC(surf3, &dc);
275     ok(SUCCEEDED(hr), "GetDC failed, hr %#x.\n", hr);
276
277     hr = IDirectDraw3_GetSurfaceFromDC(dd3, dc, NULL);
278     ok(hr == E_POINTER, "Expected E_POINTER, got %#x.\n", hr);
279
280     hr = IDirectDraw3_GetSurfaceFromDC(dd3, dc, &tmp);
281     ok(SUCCEEDED(hr), "GetSurfaceFromDC failed, hr %#x.\n", hr);
282     ok((IDirectDrawSurface3 *)tmp == surf3, "Expected surface != %p.\n", surf3);
283
284     IUnknown_Release(tmp);
285
286     hr = IDirectDrawSurface3_ReleaseDC(surf3, dc);
287     ok(SUCCEEDED(hr), "ReleaseDC failed, hr %#x.\n", hr);
288
289     dc = CreateCompatibleDC(NULL);
290     ok(!!dc, "CreateCompatibleDC failed.\n");
291
292     tmp = (IDirectDrawSurface *)0xdeadbeef;
293     hr = IDirectDraw3_GetSurfaceFromDC(dd3, dc, &tmp);
294     ok(hr == DDERR_NOTFOUND, "Expected DDERR_NOTFOUND, got %#x.\n", hr);
295     ok(!tmp, "Expected surface NULL, got %p.\n", tmp);
296
297     ok(DeleteDC(dc), "DeleteDC failed.\n");
298
299     IDirectDrawSurface3_Release(surf3);
300     IDirectDraw3_Release(dd3);
301 }
302
303 DEFINE_GUID(guid, 0x38594b23, 0x2311, 0x4332, 0x95, 0xde, 0x2b, 0x0c, 0x61, 0xbf, 0x7b, 0x84);
304
305 static void test_surface_from_dc4(void)
306 {
307     IDirectDrawSurface4 *surf4;
308     IDirectDrawSurface *surf1;
309     DDSURFACEDESC2 ddsd2;
310     IUnknown *tmp, *tmp2;
311     IDirectDraw4 *dd4;
312     IDirectDraw *dd1;
313     DWORD priv, size;
314     HRESULT hr;
315     HDC dc;
316
317     dd1 = createDD();
318     hr = IDirectDraw_QueryInterface(dd1, &IID_IDirectDraw4, (void **)&dd4);
319     if (hr == E_NOINTERFACE) {
320         win_skip("DirectDraw4 is not supported\n");
321         IDirectDraw_Release(dd1);
322         return;
323     }
324     ok(SUCCEEDED(hr), "IDirectDraw_QueryInterface failed, hr %#x.\n", hr);
325     IDirectDraw_Release(dd1);
326
327     memset(&ddsd2, 0, sizeof(ddsd2));
328     ddsd2.dwSize = sizeof(ddsd2);
329     ddsd2.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
330     ddsd2.dwWidth = 64;
331     ddsd2.dwHeight = 64;
332     ddsd2.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
333
334     hr = IDirectDraw4_CreateSurface(dd4, &ddsd2, &surf4, NULL);
335     if (hr == DDERR_UNSUPPORTEDMODE) {
336         win_skip("Unsupported mode\n");
337         IDirectDraw3_Release(dd4);
338         return;
339     }
340     ok(SUCCEEDED(hr), "CreateSurface failed, hr %#x.\n", hr);
341
342     hr = IDirectDrawSurface4_QueryInterface(surf4, &IID_IDirectDrawSurface, (void **)&surf1);
343     ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
344
345     priv = 0xdeadbeef;
346     size = sizeof(priv);
347     hr = IDirectDrawSurface4_SetPrivateData(surf4, &guid, &priv, size, 0);
348     ok(SUCCEEDED(hr), "SetPrivateData failed, hr %#x.\n", hr);
349
350     priv = 0;
351     hr = IDirectDrawSurface4_GetPrivateData(surf4, &guid, &priv, &size);
352     ok(SUCCEEDED(hr), "GetPrivateData failed, hr %#x.\n", hr);
353     ok(priv == 0xdeadbeef, "Expected private data 0xdeadbeef, got %#x.\n", priv);
354
355     hr = IDirectDrawSurface4_GetDC(surf4, &dc);
356     ok(SUCCEEDED(hr), "GetDC failed, hr %#x.\n", hr);
357
358     hr = IDirectDraw4_GetSurfaceFromDC(dd4, dc, NULL);
359     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %#x.\n", hr);
360
361     hr = IDirectDraw4_GetSurfaceFromDC(dd4, dc, (IDirectDrawSurface4 **)&tmp);
362     ok(SUCCEEDED(hr), "GetSurfaceFromDC failed, hr %#x.\n", hr);
363     ok((IDirectDrawSurface4 *)tmp != surf4, "Expected surface != %p.\n", surf4);
364
365     hr = IUnknown_QueryInterface(tmp, &IID_IDirectDrawSurface, (void **)&tmp2);
366     ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
367     ok(tmp2 == tmp, "Expected %p, got %p.\n", tmp, tmp2);
368     ok((IDirectDrawSurface *)tmp2 != surf1, "Expected surface != %p.\n", surf1);
369     IUnknown_Release(tmp2);
370
371     hr = IUnknown_QueryInterface(tmp, &IID_IDirectDrawSurface4, (void **)&tmp2);
372     ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
373     ok((IDirectDrawSurface4 *)tmp2 != surf4, "Expected surface != %p.\n", surf4);
374
375     priv = 0;
376     hr = IDirectDrawSurface4_GetPrivateData((IDirectDrawSurface4 *)tmp2, &guid, &priv, &size);
377     ok(SUCCEEDED(hr), "GetPrivateData failed, hr %#x.\n", hr);
378     ok(priv == 0xdeadbeef, "Expected private data 0xdeadbeef, got %#x.\n", priv);
379     IUnknown_Release(tmp2);
380
381     IUnknown_Release(tmp);
382
383     hr = IDirectDrawSurface4_ReleaseDC(surf4, dc);
384     ok(SUCCEEDED(hr), "ReleaseDC failed, hr %#x.\n", hr);
385
386     dc = CreateCompatibleDC(NULL);
387     ok(!!dc, "CreateCompatibleDC failed.\n");
388
389     tmp = (IUnknown *)0xdeadbeef;
390     hr = IDirectDraw4_GetSurfaceFromDC(dd4, dc, (IDirectDrawSurface4 **)&tmp);
391     ok(hr == DDERR_NOTFOUND, "Expected DDERR_NOTFOUND, got %#x.\n", hr);
392     ok(!tmp, "Expected surface NULL, got %p.\n", tmp);
393
394     ok(DeleteDC(dc), "DeleteDC failed.\n");
395
396     tmp = (IUnknown *)0xdeadbeef;
397     hr = IDirectDraw4_GetSurfaceFromDC(dd4, NULL, (IDirectDrawSurface4 **)&tmp);
398     ok(hr == DDERR_NOTFOUND, "Expected DDERR_NOTFOUND, got %#x.\n", hr);
399     ok(!tmp, "Expected surface NULL, got %p.\n", tmp);
400
401     IDirectDrawSurface_Release(surf1);
402     IDirectDrawSurface4_Release(surf4);
403     IDirectDraw4_Release(dd4);
404 }
405
406 START_TEST(surface)
407 {
408     IClassFactory *classfactory = NULL;
409     ULONG ref;
410     HRESULT hr;
411     HMODULE hmod = LoadLibrary("ddrawex.dll");
412     if(hmod == NULL) {
413         skip("Failed to load ddrawex.dll\n");
414         return;
415     }
416     pDllGetClassObject = (void*)GetProcAddress(hmod, "DllGetClassObject");
417     if(pDllGetClassObject == NULL) {
418         skip("Failed to get DllGetClassObject\n");
419         return;
420     }
421
422     hr = pDllGetClassObject(&CLSID_DirectDrawFactory, &IID_IClassFactory, (void **) &classfactory);
423     ok(hr == S_OK, "Failed to create a IClassFactory\n");
424     if (FAILED(hr)) {
425         skip("Failed to get DirectDrawFactory\n");
426         return;
427     }
428     hr = IClassFactory_CreateInstance(classfactory, NULL, &IID_IDirectDrawFactory, (void **) &factory);
429     ok(hr == S_OK, "Failed to create a IDirectDrawFactory\n");
430     if (FAILED(hr)) {
431         IClassFactory_Release(classfactory);
432         skip("Failed to get a DirectDrawFactory\n");
433         return;
434     }
435
436     GetDCTest();
437     CapsTest();
438     SysVidMemTest();
439     test_surface_from_dc3();
440     test_surface_from_dc4();
441
442     ref = IDirectDrawFactory_Release(factory);
443     ok(ref == 0, "IDirectDrawFactory not cleanly released\n");
444     ref = IClassFactory_Release(classfactory);
445     todo_wine ok(ref == 1, "IClassFactory refcount wrong, ref = %u\n", ref);
446 }