d3drm: Implement D3DRMCreateColorRGB.
[wine] / dlls / ddraw / tests / dsurface.c
1 /*
2  * Unit tests for (a few) ddraw surface functions
3  *
4  * Copyright (C) 2005 Antoine Chavasse (a.chavasse@gmail.com)
5  * Copyright (C) 2005 Christian Costa
6  * Copyright 2005 Ivan Leo Puoti
7  * Copyright (C) 2007 Stefan Dösinger
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 #define COBJMACROS
24
25 #include <assert.h>
26 #include "wine/test.h"
27 #include "ddraw.h"
28 #include "unknwn.h"
29
30 static LPDIRECTDRAW lpDD = NULL;
31
32 static BOOL CreateDirectDraw(void)
33 {
34     HRESULT rc;
35
36     rc = DirectDrawCreate(NULL, &lpDD, NULL);
37     ok(rc==DD_OK || rc==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", rc);
38     if (!lpDD) {
39         trace("DirectDrawCreateEx() failed with an error %x\n", rc);
40         return FALSE;
41     }
42
43     rc = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
44     ok(rc==DD_OK,"SetCooperativeLevel returned: %x\n",rc);
45
46     return TRUE;
47 }
48
49
50 static void ReleaseDirectDraw(void)
51 {
52     if( lpDD != NULL )
53     {
54         IDirectDraw_Release(lpDD);
55         lpDD = NULL;
56     }
57 }
58
59 static void MipMapCreationTest(void)
60 {
61     LPDIRECTDRAWSURFACE lpDDSMipMapTest;
62     DDSURFACEDESC ddsd;
63     HRESULT rc;
64
65     /* First mipmap creation test: create a surface with DDSCAPS_COMPLEX,
66        DDSCAPS_MIPMAP, and DDSD_MIPMAPCOUNT. This create the number of
67         requested mipmap levels. */
68     ddsd.dwSize = sizeof(ddsd);
69     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
70     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
71     U2(ddsd).dwMipMapCount = 3;
72     ddsd.dwWidth = 128;
73     ddsd.dwHeight = 32;
74     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
75     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
76     if (FAILED(rc)) {
77         skip("failed to create surface\n");
78         return;
79     }
80
81     /* Check the number of created mipmaps */
82     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
83     ddsd.dwSize = sizeof(ddsd);
84     rc = IDirectDrawSurface_GetSurfaceDesc(lpDDSMipMapTest, &ddsd);
85     ok(rc==DD_OK,"GetSurfaceDesc returned: %x\n",rc);
86     ok(ddsd.dwFlags & DDSD_MIPMAPCOUNT,
87         "GetSurfaceDesc returned no mipmapcount.\n");
88     ok(U2(ddsd).dwMipMapCount == 3, "Incorrect mipmap count: %d.\n",
89         U2(ddsd).dwMipMapCount);
90
91     /* Destroy the surface. */
92     IDirectDrawSurface_Release(lpDDSMipMapTest);
93
94
95     /* Second mipmap creation test: create a surface without a mipmap
96        count, with DDSCAPS_MIPMAP and without DDSCAPS_COMPLEX.
97        This creates a single mipmap level. */
98     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
99     ddsd.dwSize = sizeof(ddsd);
100     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
101     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_MIPMAP;
102     ddsd.dwWidth = 128;
103     ddsd.dwHeight = 32;
104     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
105     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
106     if (FAILED(rc)) {
107         skip("failed to create surface\n");
108         return;
109     }
110     /* Check the number of created mipmaps */
111     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
112     ddsd.dwSize = sizeof(ddsd);
113     rc = IDirectDrawSurface_GetSurfaceDesc(lpDDSMipMapTest, &ddsd);
114     ok(rc==DD_OK,"GetSurfaceDesc returned: %x\n",rc);
115     ok(ddsd.dwFlags & DDSD_MIPMAPCOUNT,
116         "GetSurfaceDesc returned no mipmapcount.\n");
117     ok(U2(ddsd).dwMipMapCount == 1, "Incorrect mipmap count: %d.\n",
118         U2(ddsd).dwMipMapCount);
119
120     /* Destroy the surface. */
121     IDirectDrawSurface_Release(lpDDSMipMapTest);
122
123
124     /* Third mipmap creation test: create a surface with DDSCAPS_MIPMAP,
125         DDSCAPS_COMPLEX and without DDSD_MIPMAPCOUNT.
126        It's an undocumented features where a chain of mipmaps, starting from
127        he specified size and down to the smallest size, is automatically
128        created.
129        Anarchy Online needs this feature to work. */
130     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
131     ddsd.dwSize = sizeof(ddsd);
132     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
133     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
134     ddsd.dwWidth = 128;
135     ddsd.dwHeight = 32;
136     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
137     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
138     if (FAILED(rc)) {
139         skip("failed to create surface\n");
140         return;
141     }
142
143     /* Check the number of created mipmaps */
144     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
145     ddsd.dwSize = sizeof(ddsd);
146     rc = IDirectDrawSurface_GetSurfaceDesc(lpDDSMipMapTest, &ddsd);
147     ok(rc==DD_OK,"GetSurfaceDesc returned: %x\n",rc);
148     ok(ddsd.dwFlags & DDSD_MIPMAPCOUNT,
149         "GetSurfaceDesc returned no mipmapcount.\n");
150     ok(U2(ddsd).dwMipMapCount == 6, "Incorrect mipmap count: %d.\n",
151         U2(ddsd).dwMipMapCount);
152
153     /* Destroy the surface. */
154     IDirectDrawSurface_Release(lpDDSMipMapTest);
155
156
157     /* Fourth mipmap creation test: same as above with a different texture
158        size.
159        The purpose is to verify that the number of generated mipmaps is
160        dependent on the smallest dimension. */
161     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
162     ddsd.dwSize = sizeof(ddsd);
163     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
164     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
165     ddsd.dwWidth = 32;
166     ddsd.dwHeight = 64;
167     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
168     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
169     if (FAILED(rc)) {
170         skip("failed to create surface\n");
171         return;
172     }
173
174     /* Check the number of created mipmaps */
175     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
176     ddsd.dwSize = sizeof(ddsd);
177     rc = IDirectDrawSurface_GetSurfaceDesc(lpDDSMipMapTest, &ddsd);
178     ok(rc==DD_OK,"GetSurfaceDesc returned: %x\n",rc);
179     ok(ddsd.dwFlags & DDSD_MIPMAPCOUNT,
180         "GetSurfaceDesc returned no mipmapcount.\n");
181     ok(U2(ddsd).dwMipMapCount == 6, "Incorrect mipmap count: %d.\n",
182         U2(ddsd).dwMipMapCount);
183
184     /* Destroy the surface. */
185     IDirectDrawSurface_Release(lpDDSMipMapTest);
186 }
187
188 static void SrcColorKey32BlitTest(void)
189 {
190     LPDIRECTDRAWSURFACE lpSrc;
191     LPDIRECTDRAWSURFACE lpDst;
192     DDSURFACEDESC ddsd, ddsd2, ddsd3;
193     DDCOLORKEY DDColorKey;
194     LPDWORD lpData;
195     HRESULT rc;
196     DDBLTFX fx;
197
198     ddsd2.dwSize = sizeof(ddsd2);
199     ddsd2.ddpfPixelFormat.dwSize = sizeof(ddsd2.ddpfPixelFormat);
200
201     ddsd.dwSize = sizeof(ddsd);
202     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
203     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
204     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
205     ddsd.dwWidth = 800;
206     ddsd.dwHeight = 600;
207     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
208     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
209     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xFF0000;
210     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x00FF00;
211     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x0000FF;
212     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDst, NULL);
213     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
214     if (FAILED(rc)) {
215         skip("failed to create surface\n");
216         return;
217     }
218
219     ddsd.dwFlags |= DDSD_CKSRCBLT;
220     ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0xFF00FF;
221     ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0xFF00FF;
222     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpSrc, NULL);
223     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
224     if (FAILED(rc)) {
225         skip("failed to create surface\n");
226         return;
227     }
228
229     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
230     ok(rc==DD_OK,"Lock returned: %x\n",rc);
231     lpData = (LPDWORD)ddsd2.lpSurface;
232     lpData[0] = 0xCCCCCCCC;
233     lpData[1] = 0xCCCCCCCC;
234     lpData[2] = 0xCCCCCCCC;
235     lpData[3] = 0xCCCCCCCC;
236
237     memset(&ddsd3, 0, sizeof(ddsd3));
238     ddsd3.dwSize = sizeof(ddsd3);
239     ddsd3.ddpfPixelFormat.dwSize = sizeof(ddsd3.ddpfPixelFormat);
240     rc = IDirectDrawSurface_GetSurfaceDesc(lpDst, &ddsd3);
241     ok(rc == DD_OK, "IDirectDrawSurface_GetSurfaceDesc between a lock/unlock pair returned %08x\n", rc);
242     ok(ddsd3.lpSurface == ddsd3.lpSurface, "lpSurface from GetSurfaceDesc(%p) differs from the one returned by Lock(%p)\n", ddsd3.lpSurface, ddsd2.lpSurface);
243
244     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
245     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
246
247     memset(&ddsd3, 0, sizeof(ddsd3));
248     ddsd3.dwSize = sizeof(ddsd3);
249     ddsd3.ddpfPixelFormat.dwSize = sizeof(ddsd3.ddpfPixelFormat);
250     rc = IDirectDrawSurface_GetSurfaceDesc(lpDst, &ddsd3);
251     ok(rc == DD_OK, "IDirectDrawSurface_GetSurfaceDesc between a lock/unlock pair returned %08x\n", rc);
252     ok(ddsd3.lpSurface == NULL, "lpSurface from GetSurfaceDesc(%p) is not NULL after unlock\n", ddsd3.lpSurface);
253
254     rc = IDirectDrawSurface_Lock(lpSrc, NULL, &ddsd2, DDLOCK_WAIT, NULL);
255     ok(rc==DD_OK,"Lock returned: %x\n",rc);
256     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
257     lpData = (LPDWORD)ddsd2.lpSurface;
258     lpData[0] = 0x77010203;
259     lpData[1] = 0x00010203;
260     lpData[2] = 0x77FF00FF;
261     lpData[3] = 0x00FF00FF;
262     rc = IDirectDrawSurface_Unlock(lpSrc, NULL);
263     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
264
265     IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRC, NULL);
266
267     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
268     ok(rc==DD_OK,"Lock returned: %x\n",rc);
269     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
270     lpData = (LPDWORD)ddsd2.lpSurface;
271     ok((lpData[0]==0x77010203)&&(lpData[1]==0x00010203)&&(lpData[2]==0xCCCCCCCC)&&(lpData[3]==0xCCCCCCCC),
272        "Destination data after blitting is not correct\n");
273     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
274     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
275
276     /* Also test SetColorKey */
277     IDirectDrawSurface_GetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
278     ok(DDColorKey.dwColorSpaceLowValue == 0xFF00FF && DDColorKey.dwColorSpaceHighValue == 0xFF00FF,
279        "GetColorKey does not return the colorkey used at surface creation\n");
280
281     DDColorKey.dwColorSpaceLowValue = 0x00FF00;
282     DDColorKey.dwColorSpaceHighValue = 0x00FF00;
283     IDirectDrawSurface_SetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
284
285     DDColorKey.dwColorSpaceLowValue = 0;
286     DDColorKey.dwColorSpaceHighValue = 0;
287     IDirectDrawSurface_GetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
288     ok(DDColorKey.dwColorSpaceLowValue == 0x00FF00 && DDColorKey.dwColorSpaceHighValue == 0x00FF00,
289        "GetColorKey does not return the colorkey set with SetColorKey\n");
290
291     ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0;
292     ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0;
293     IDirectDrawSurface_GetSurfaceDesc(lpSrc, &ddsd);
294     ok(ddsd.ddckCKSrcBlt.dwColorSpaceLowValue == 0x00FF00 && ddsd.ddckCKSrcBlt.dwColorSpaceHighValue == 0x00FF00,
295        "GetSurfaceDesc does not return the colorkey set with SetColorKey\n");
296
297     IDirectDrawSurface_Release(lpSrc);
298     IDirectDrawSurface_Release(lpDst);
299
300     /* start with a new set of surfaces to test the color keying parameters to blit */
301     memset(&ddsd, 0, sizeof(ddsd));
302     ddsd.dwSize = sizeof(ddsd);
303     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
304     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT | DDSD_CKDESTBLT;
305     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
306     ddsd.dwWidth = 800;
307     ddsd.dwHeight = 600;
308     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
309     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
310     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xFF0000;
311     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x00FF00;
312     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x0000FF;
313     ddsd.ddckCKDestBlt.dwColorSpaceLowValue = 0xFF0000;
314     ddsd.ddckCKDestBlt.dwColorSpaceHighValue = 0xFF0000;
315     ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00FF00;
316     ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00FF00;
317     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDst, NULL);
318     ok(rc==DD_OK || rc == DDERR_NOCOLORKEYHW,"CreateSurface returned: %x\n",rc);
319     if(FAILED(rc))
320     {
321         skip("Failed to create surface\n");
322         return;
323     }
324
325     /* start with a new set of surfaces to test the color keying parameters to blit */
326     memset(&ddsd, 0, sizeof(ddsd));
327     ddsd.dwSize = sizeof(ddsd);
328     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
329     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT | DDSD_CKDESTBLT;
330     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
331     ddsd.dwWidth = 800;
332     ddsd.dwHeight = 600;
333     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
334     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
335     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xFF0000;
336     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x00FF00;
337     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x0000FF;
338     ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0x0000FF;
339     ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0x0000FF;
340     ddsd.ddckCKDestBlt.dwColorSpaceLowValue = 0x000000;
341     ddsd.ddckCKDestBlt.dwColorSpaceHighValue = 0x000000;
342     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpSrc, NULL);
343     ok(rc==DD_OK || rc == DDERR_NOCOLORKEYHW,"CreateSurface returned: %x\n",rc);
344     if(FAILED(rc))
345     {
346         skip("Failed to create surface\n");
347         IDirectDrawSurface_Release(lpDst);
348         return;
349     }
350
351     memset(&fx, 0, sizeof(fx));
352     fx.dwSize = sizeof(fx);
353     fx.ddckSrcColorkey.dwColorSpaceHighValue = 0x110000;
354     fx.ddckSrcColorkey.dwColorSpaceLowValue = 0x110000;
355     fx.ddckDestColorkey.dwColorSpaceHighValue = 0x001100;
356     fx.ddckDestColorkey.dwColorSpaceLowValue = 0x001100;
357
358     rc = IDirectDrawSurface_Lock(lpSrc, NULL, &ddsd2, DDLOCK_WAIT, NULL);
359     ok(rc==DD_OK,"Lock returned: %x\n",rc);
360     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
361     lpData = (LPDWORD)ddsd2.lpSurface;
362     lpData[0] = 0x000000FF; /* Applies to src blt key in src surface */
363     lpData[1] = 0x00000000; /* Applies to dst blt key in src surface */
364     lpData[2] = 0x00FF0000; /* Dst color key in dst surface */
365     lpData[3] = 0x0000FF00; /* Src color key in dst surface */
366     lpData[4] = 0x00001100; /* Src color key in ddbltfx */
367     lpData[5] = 0x00110000; /* Dst color key in ddbltfx */
368     rc = IDirectDrawSurface_Unlock(lpSrc, NULL);
369     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
370
371     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
372     ok(rc==DD_OK,"Lock returned: %x\n",rc);
373     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
374     lpData = (LPDWORD)ddsd2.lpSurface;
375     lpData[0] = 0x55555555;
376     lpData[1] = 0x55555555;
377     lpData[2] = 0x55555555;
378     lpData[3] = 0x55555555;
379     lpData[4] = 0x55555555;
380     lpData[5] = 0x55555555;
381     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
382     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
383
384     /* Test a blit without keying */
385     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, 0, &fx);
386     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
387
388     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
389     ok(rc==DD_OK,"Lock returned: %x\n",rc);
390     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
391     lpData = (LPDWORD)ddsd2.lpSurface;
392     /* Should have copied src data unmodified to dst */
393     ok(lpData[0] == 0x000000FF &&
394        lpData[1] == 0x00000000 &&
395        lpData[2] == 0x00FF0000 &&
396        lpData[3] == 0x0000FF00 &&
397        lpData[4] == 0x00001100 &&
398        lpData[5] == 0x00110000, "Surface data after unkeyed blit does not match\n");
399
400     lpData[0] = 0x55555555;
401     lpData[1] = 0x55555555;
402     lpData[2] = 0x55555555;
403     lpData[3] = 0x55555555;
404     lpData[4] = 0x55555555;
405     lpData[5] = 0x55555555;
406     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
407     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
408
409     /* Src key */
410     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRC, &fx);
411     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
412
413     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
414     ok(rc==DD_OK,"Lock returned: %x\n",rc);
415     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
416     lpData = (LPDWORD)ddsd2.lpSurface;
417
418     ok(lpData[0] == 0x55555555 && /* Here the src key applied */
419        lpData[1] == 0x00000000 &&
420        lpData[2] == 0x00FF0000 &&
421        lpData[3] == 0x0000FF00 &&
422        lpData[4] == 0x00001100 &&
423        lpData[5] == 0x00110000, "Surface data after srckey blit does not match\n");
424
425     lpData[0] = 0x55555555;
426     lpData[1] = 0x55555555;
427     lpData[2] = 0x55555555;
428     lpData[3] = 0x55555555;
429     lpData[4] = 0x55555555;
430     lpData[5] = 0x55555555;
431     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
432     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
433
434     /* Src override */
435     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRCOVERRIDE, &fx);
436     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
437
438     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
439     ok(rc==DD_OK,"Lock returned: %x\n",rc);
440     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
441     lpData = (LPDWORD)ddsd2.lpSurface;
442
443     ok(lpData[0] == 0x000000FF &&
444        lpData[1] == 0x00000000 &&
445        lpData[2] == 0x00FF0000 &&
446        lpData[3] == 0x0000FF00 &&
447        lpData[4] == 0x00001100 &&
448        lpData[5] == 0x55555555, /* Override key applies here */
449        "Surface data after src override key blit does not match\n");
450
451     lpData[0] = 0x55555555;
452     lpData[1] = 0x55555555;
453     lpData[2] = 0x55555555;
454     lpData[3] = 0x55555555;
455     lpData[4] = 0x55555555;
456     lpData[5] = 0x55555555;
457     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
458     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
459
460     /* Src override AND src key. That is not supposed to work */
461     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRC | DDBLT_KEYSRCOVERRIDE, &fx);
462     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
463
464     /* Verify that the destination is unchanged */
465     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
466     ok(rc==DD_OK,"Lock returned: %x\n",rc);
467     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
468     lpData = (LPDWORD)ddsd2.lpSurface;
469
470     ok(lpData[0] == 0x55555555 &&
471        lpData[1] == 0x55555555 &&
472        lpData[2] == 0x55555555 &&
473        lpData[3] == 0x55555555 &&
474        lpData[4] == 0x55555555 &&
475        lpData[5] == 0x55555555, /* Override key applies here */
476        "Surface data after src key blit with override does not match\n");
477
478     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
479     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
480     lpData[2] = 0x00001100; /* Dest key in override */
481     lpData[3] = 0x00001100; /* Dest key in override */
482     lpData[4] = 0x00000000; /* Dest key in src surface */
483     lpData[5] = 0x00000000; /* Dest key in src surface */
484     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
485     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
486
487     /* Dest key blit */
488     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDEST, &fx);
489     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
490
491     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
492     ok(rc==DD_OK,"Lock returned: %x\n",rc);
493     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
494     lpData = (LPDWORD)ddsd2.lpSurface;
495
496     /* DirectDraw uses the dest blit key from the SOURCE surface ! */
497     ok(lpData[0] == 0x00ff0000 &&
498        lpData[1] == 0x00ff0000 &&
499        lpData[2] == 0x00001100 &&
500        lpData[3] == 0x00001100 &&
501        lpData[4] == 0x00001100 && /* Key applies here */
502        lpData[5] == 0x00110000,   /* Key applies here */
503        "Surface data after dest key blit does not match\n");
504
505     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
506     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
507     lpData[2] = 0x00001100; /* Dest key in override */
508     lpData[3] = 0x00001100; /* Dest key in override */
509     lpData[4] = 0x00000000; /* Dest key in src surface */
510     lpData[5] = 0x00000000; /* Dest key in src surface */
511     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
512     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
513
514     /* Dest override key blit */
515     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDESTOVERRIDE, &fx);
516     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
517
518     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
519     ok(rc==DD_OK,"Lock returned: %x\n",rc);
520     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
521     lpData = (LPDWORD)ddsd2.lpSurface;
522
523     ok(lpData[0] == 0x00FF0000 &&
524        lpData[1] == 0x00FF0000 &&
525        lpData[2] == 0x00FF0000 && /* Key applies here */
526        lpData[3] == 0x0000FF00 && /* Key applies here */
527        lpData[4] == 0x00000000 &&
528        lpData[5] == 0x00000000,
529        "Surface data after dest key override blit does not match\n");
530
531     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
532     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
533     lpData[2] = 0x00001100; /* Dest key in override */
534     lpData[3] = 0x00001100; /* Dest key in override */
535     lpData[4] = 0x00000000; /* Dest key in src surface */
536     lpData[5] = 0x00000000; /* Dest key in src surface */
537     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
538     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
539
540     /* Dest override key blit. Supposed to fail too */
541     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDEST | DDBLT_KEYDESTOVERRIDE, &fx);
542     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
543
544     /* Check for unchanged data */
545     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
546     ok(rc==DD_OK,"Lock returned: %x\n",rc);
547     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
548     lpData = (LPDWORD)ddsd2.lpSurface;
549
550     ok(lpData[0] == 0x00FF0000 &&
551        lpData[1] == 0x00FF0000 &&
552        lpData[2] == 0x00001100 && /* Key applies here */
553        lpData[3] == 0x00001100 && /* Key applies here */
554        lpData[4] == 0x00000000 &&
555        lpData[5] == 0x00000000,
556        "Surface data with dest key and dest override does not match\n");
557
558     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
559     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
560     lpData[2] = 0x00001100; /* Dest key in override */
561     lpData[3] = 0x00001100; /* Dest key in override */
562     lpData[4] = 0x00000000; /* Dest key in src surface */
563     lpData[5] = 0x00000000; /* Dest key in src surface */
564     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
565     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
566
567     /* Modify the source data a bit to give some more conclusive results */
568     rc = IDirectDrawSurface_Lock(lpSrc, NULL, &ddsd2, DDLOCK_WAIT, NULL);
569     ok(rc==DD_OK,"Lock returned: %x\n",rc);
570     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
571     lpData = (LPDWORD)ddsd2.lpSurface;
572     lpData[5] = 0x000000FF; /* Applies to src blt key in src surface */
573     rc = IDirectDrawSurface_Unlock(lpSrc, NULL);
574     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
575
576     /* Source and destination key */
577     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDEST | DDBLT_KEYSRC, &fx);
578     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
579
580     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
581     ok(rc==DD_OK,"Lock returned: %x\n",rc);
582     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
583     lpData = (LPDWORD)ddsd2.lpSurface;
584
585     ok(lpData[0] == 0x00FF0000 && /* Masked by Destination key */
586        lpData[1] == 0x00FF0000 && /* Masked by Destination key */
587        lpData[2] == 0x00001100 && /* Masked by Destination key */
588        lpData[3] == 0x00001100 && /* Masked by Destination key */
589        lpData[4] == 0x00001100 && /* Allowed by destination key, not masked by source key */
590        lpData[5] == 0x00000000,   /* Allowed by dst key, but masked by source key */
591        "Surface data with src key and dest key blit does not match\n");
592
593     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
594     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
595     lpData[2] = 0x00001100; /* Dest key in override */
596     lpData[3] = 0x00001100; /* Dest key in override */
597     lpData[4] = 0x00000000; /* Dest key in src surface */
598     lpData[5] = 0x00000000; /* Dest key in src surface */
599     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
600     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
601
602     /* Override keys without ddbltfx parameter fail */
603     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDESTOVERRIDE, NULL);
604     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
605     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRCOVERRIDE, NULL);
606     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
607
608     /* Try blitting without keys in the source surface*/
609     rc = IDirectDrawSurface_SetColorKey(lpSrc, DDCKEY_SRCBLT, NULL);
610     ok(rc == DD_OK, "SetColorKey returned %x\n", rc);
611     rc = IDirectDrawSurface_SetColorKey(lpSrc, DDCKEY_DESTBLT, NULL);
612     ok(rc == DD_OK, "SetColorKey returned %x\n", rc);
613
614     /* That fails now. Do not bother to check that the data is unmodified */
615     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRC, &fx);
616     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
617
618     /* Dest key blit still works. Which key is used this time??? */
619     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDEST, &fx);
620     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
621
622     /* With korrectly passed override keys no key in the surface is needed.
623      * Again, the result was checked before, no need to do that again
624      */
625     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDESTOVERRIDE, &fx);
626     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
627     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRCOVERRIDE, &fx);
628     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
629
630     IDirectDrawSurface_Release(lpSrc);
631     IDirectDrawSurface_Release(lpDst);
632 }
633
634 static void QueryInterface(void)
635 {
636     LPDIRECTDRAWSURFACE dsurface;
637     DDSURFACEDESC surface;
638     LPVOID object;
639     HRESULT ret;
640
641     /* Create a surface */
642     ZeroMemory(&surface, sizeof(surface));
643     surface.dwSize = sizeof(surface);
644     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
645     surface.dwHeight = 10;
646     surface.dwWidth = 10;
647     ret = IDirectDraw_CreateSurface(lpDD, &surface, &dsurface, NULL);
648     if(ret != DD_OK)
649     {
650         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
651         return;
652     }
653
654     /* Call IUnknown::QueryInterface */
655     ret = IDirectDrawSurface_QueryInterface(dsurface, 0, &object);
656     ok(ret == DDERR_INVALIDPARAMS, "IDirectDrawSurface::QueryInterface returned %x\n", ret);
657
658     IDirectDrawSurface_Release(dsurface);
659 }
660
661 /* The following tests test which interface is returned by IDirectDrawSurfaceX::GetDDInterface.
662  * It uses refcounts to test that and compares the interface addresses. Partially fits here, and
663  * partially in the refcount test
664  */
665
666 static unsigned long getref(IUnknown *iface)
667 {
668     IUnknown_AddRef(iface);
669     return IUnknown_Release(iface);
670 }
671
672 static void GetDDInterface_1(void)
673 {
674     LPDIRECTDRAWSURFACE dsurface;
675     LPDIRECTDRAWSURFACE2 dsurface2;
676     DDSURFACEDESC surface;
677     HRESULT ret;
678     IDirectDraw2 *dd2;
679     IDirectDraw4 *dd4;
680     IDirectDraw7 *dd7;
681     unsigned long ref1, ref2, ref4, ref7;
682     void *dd;
683
684     /* Create a surface */
685     ZeroMemory(&surface, sizeof(surface));
686     surface.dwSize = sizeof(surface);
687     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
688     surface.dwHeight = 10;
689     surface.dwWidth = 10;
690     ret = IDirectDraw_CreateSurface(lpDD, &surface, &dsurface, NULL);
691     if(ret != DD_OK)
692     {
693         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
694         return;
695     }
696     ret = IDirectDrawSurface_QueryInterface(dsurface, &IID_IDirectDrawSurface2, (void **) &dsurface2);
697     ok(ret == DD_OK, "IDirectDrawSurface_QueryInterface returned %08x\n", ret);
698     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
699     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
700     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
701     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
702     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
703     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
704
705     ref1 = getref((IUnknown *) lpDD);
706     ok(ref1 == 1, "IDirectDraw refcount is %ld\n", ref1);
707     ref2 = getref((IUnknown *) dd2);
708     ok(ref2 == 1, "IDirectDraw2 refcount is %ld\n", ref2);
709     ref4 = getref((IUnknown *) dd4);
710     ok(ref4 == 1, "IDirectDraw4 refcount is %ld\n", ref4);
711     ref7 = getref((IUnknown *) dd7);
712     ok(ref7 == 1, "IDirectDraw7 refcount is %ld\n", ref7);
713
714
715     ret = IDirectDrawSurface2_GetDDInterface(dsurface2, &dd);
716     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
717     ok(getref((IUnknown *) lpDD) == ref1 + 1, "IDirectDraw refcount was increased by %ld\n", getref((IUnknown *) lpDD) - ref1);
718     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %ld\n", getref((IUnknown *) dd2) - ref2);
719     ok(getref((IUnknown *) dd4) == ref4 + 0, "IDirectDraw4 refcount was increased by %ld\n", getref((IUnknown *) dd4) - ref4);
720     ok(getref((IUnknown *) dd7) == ref7 + 0, "IDirectDraw7 refcount was increased by %ld\n", getref((IUnknown *) dd7) - ref7);
721
722     ok(dd == lpDD, "Returned interface pointer is not equal to the creation interface\n");
723     IUnknown_Release((IUnknown *) dd);
724
725     /* try a NULL pointer */
726     ret = IDirectDrawSurface2_GetDDInterface(dsurface2, NULL);
727     ok(ret == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
728
729     IDirectDraw_Release(dd2);
730     IDirectDraw_Release(dd4);
731     IDirectDraw_Release(dd7);
732     IDirectDrawSurface2_Release(dsurface2);
733     IDirectDrawSurface_Release(dsurface);
734 }
735
736 static void GetDDInterface_2(void)
737 {
738     LPDIRECTDRAWSURFACE dsurface;
739     LPDIRECTDRAWSURFACE2 dsurface2;
740     DDSURFACEDESC surface;
741     HRESULT ret;
742     IDirectDraw2 *dd2;
743     IDirectDraw4 *dd4;
744     IDirectDraw7 *dd7;
745     unsigned long ref1, ref2, ref4, ref7;
746     void *dd;
747
748     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
749     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
750     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
751     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
752     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
753     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
754
755     /* Create a surface */
756     ZeroMemory(&surface, sizeof(surface));
757     surface.dwSize = sizeof(surface);
758     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
759     surface.dwHeight = 10;
760     surface.dwWidth = 10;
761     ret = IDirectDraw2_CreateSurface(dd2, &surface, &dsurface, NULL);
762     if(ret != DD_OK)
763     {
764         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
765         return;
766     }
767     ret = IDirectDrawSurface_QueryInterface(dsurface, &IID_IDirectDrawSurface2, (void **) &dsurface2);
768     ok(ret == DD_OK, "IDirectDrawSurface_QueryInterface returned %08x\n", ret);
769
770     ref1 = getref((IUnknown *) lpDD);
771     ok(ref1 == 1, "IDirectDraw refcount is %ld\n", ref1);
772     ref2 = getref((IUnknown *) dd2);
773     ok(ref2 == 1, "IDirectDraw2 refcount is %ld\n", ref2);
774     ref4 = getref((IUnknown *) dd4);
775     ok(ref4 == 1, "IDirectDraw4 refcount is %ld\n", ref4);
776     ref7 = getref((IUnknown *) dd7);
777     ok(ref7 == 1, "IDirectDraw7 refcount is %ld\n", ref7);
778
779
780     ret = IDirectDrawSurface2_GetDDInterface(dsurface2, &dd);
781     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
782     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %ld\n", getref((IUnknown *) lpDD) - ref1);
783     ok(getref((IUnknown *) dd2) == ref2 + 1, "IDirectDraw2 refcount was increased by %ld\n", getref((IUnknown *) dd2) - ref2);
784     ok(getref((IUnknown *) dd4) == ref4 + 0, "IDirectDraw4 refcount was increased by %ld\n", getref((IUnknown *) dd4) - ref4);
785     ok(getref((IUnknown *) dd7) == ref7 + 0, "IDirectDraw7 refcount was increased by %ld\n", getref((IUnknown *) dd7) - ref7);
786
787     ok(dd == dd2, "Returned interface pointer is not equal to the creation interface\n");
788     IUnknown_Release((IUnknown *) dd);
789
790     IDirectDraw_Release(dd2);
791     IDirectDraw_Release(dd4);
792     IDirectDraw_Release(dd7);
793     IDirectDrawSurface2_Release(dsurface2);
794     IDirectDrawSurface_Release(dsurface);
795 }
796
797 static void GetDDInterface_4(void)
798 {
799     LPDIRECTDRAWSURFACE2 dsurface2;
800     LPDIRECTDRAWSURFACE4 dsurface4;
801     DDSURFACEDESC2 surface;
802     HRESULT ret;
803     IDirectDraw2 *dd2;
804     IDirectDraw4 *dd4;
805     IDirectDraw7 *dd7;
806     unsigned long ref1, ref2, ref4, ref7;
807     void *dd;
808
809     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
810     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
811     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
812     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
813     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
814     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
815
816     /* Create a surface */
817     ZeroMemory(&surface, sizeof(surface));
818     surface.dwSize = sizeof(surface);
819     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
820     surface.dwHeight = 10;
821     surface.dwWidth = 10;
822     ret = IDirectDraw4_CreateSurface(dd4, &surface, &dsurface4, NULL);
823     if(ret != DD_OK)
824     {
825         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
826         return;
827     }
828     ret = IDirectDrawSurface4_QueryInterface(dsurface4, &IID_IDirectDrawSurface2, (void **) &dsurface2);
829     ok(ret == DD_OK, "IDirectDrawSurface_QueryInterface returned %08x\n", ret);
830
831     ref1 = getref((IUnknown *) lpDD);
832     ok(ref1 == 1, "IDirectDraw refcount is %ld\n", ref1);
833     ref2 = getref((IUnknown *) dd2);
834     ok(ref2 == 1, "IDirectDraw2 refcount is %ld\n", ref2);
835     ref4 = getref((IUnknown *) dd4);
836     ok(ref4 == 2, "IDirectDraw4 refcount is %ld\n", ref4);
837     ref7 = getref((IUnknown *) dd7);
838     ok(ref7 == 1, "IDirectDraw7 refcount is %ld\n", ref7);
839
840     ret = IDirectDrawSurface4_GetDDInterface(dsurface4, &dd);
841     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
842     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %ld\n", getref((IUnknown *) lpDD) - ref1);
843     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %ld\n", getref((IUnknown *) dd2) - ref2);
844     ok(getref((IUnknown *) dd4) == ref4 + 1, "IDirectDraw4 refcount was increased by %ld\n", getref((IUnknown *) dd4) - ref4);
845     ok(getref((IUnknown *) dd7) == ref7 + 0, "IDirectDraw7 refcount was increased by %ld\n", getref((IUnknown *) dd7) - ref7);
846
847     ok(dd == dd4, "Returned interface pointer is not equal to the creation interface\n");
848     IUnknown_Release((IUnknown *) dd);
849
850     /* Now test what happens if we QI the surface for some other version - It should still return the creation interface */
851     ret = IDirectDrawSurface2_GetDDInterface(dsurface2, &dd);
852     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
853     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %ld\n", getref((IUnknown *) lpDD) - ref1);
854     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %ld\n", getref((IUnknown *) dd2) - ref2);
855     ok(getref((IUnknown *) dd4) == ref4 + 1, "IDirectDraw4 refcount was increased by %ld\n", getref((IUnknown *) dd4) - ref4);
856     ok(getref((IUnknown *) dd7) == ref7 + 0, "IDirectDraw7 refcount was increased by %ld\n", getref((IUnknown *) dd7) - ref7);
857
858     ok(dd == dd4, "Returned interface pointer is not equal to the creation interface\n");
859     IUnknown_Release((IUnknown *) dd);
860
861     IDirectDraw_Release(dd2);
862     IDirectDraw_Release(dd4);
863     IDirectDraw_Release(dd7);
864     IDirectDrawSurface4_Release(dsurface4);
865     IDirectDrawSurface2_Release(dsurface2);
866 }
867
868 static void GetDDInterface_7(void)
869 {
870     LPDIRECTDRAWSURFACE4 dsurface4;
871     LPDIRECTDRAWSURFACE7 dsurface7;
872     DDSURFACEDESC2 surface;
873     HRESULT ret;
874     IDirectDraw2 *dd2;
875     IDirectDraw4 *dd4;
876     IDirectDraw7 *dd7;
877     unsigned long ref1, ref2, ref4, ref7;
878     void *dd;
879
880     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
881     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
882     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
883     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
884     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
885     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
886
887     /* Create a surface */
888     ZeroMemory(&surface, sizeof(surface));
889     surface.dwSize = sizeof(surface);
890     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
891     surface.dwHeight = 10;
892     surface.dwWidth = 10;
893     ret = IDirectDraw7_CreateSurface(dd7, &surface, &dsurface7, NULL);
894     if(ret != DD_OK)
895     {
896         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
897         return;
898     }
899     ret = IDirectDrawSurface7_QueryInterface(dsurface7, &IID_IDirectDrawSurface4, (void **) &dsurface4);
900     ok(ret == DD_OK, "IDirectDrawSurface_QueryInterface returned %08x\n", ret);
901
902     ref1 = getref((IUnknown *) lpDD);
903     ok(ref1 == 1, "IDirectDraw refcount is %ld\n", ref1);
904     ref2 = getref((IUnknown *) dd2);
905     ok(ref2 == 1, "IDirectDraw2 refcount is %ld\n", ref2);
906     ref4 = getref((IUnknown *) dd4);
907     ok(ref4 == 1, "IDirectDraw4 refcount is %ld\n", ref4);
908     ref7 = getref((IUnknown *) dd7);
909     ok(ref7 == 2, "IDirectDraw7 refcount is %ld\n", ref7);
910
911     ret = IDirectDrawSurface7_GetDDInterface(dsurface7, &dd);
912     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
913     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %ld\n", getref((IUnknown *) lpDD) - ref1);
914     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %ld\n", getref((IUnknown *) dd2) - ref2);
915     ok(getref((IUnknown *) dd4) == ref4 + 0, "IDirectDraw4 refcount was increased by %ld\n", getref((IUnknown *) dd4) - ref4);
916     ok(getref((IUnknown *) dd7) == ref7 + 1, "IDirectDraw7 refcount was increased by %ld\n", getref((IUnknown *) dd7) - ref7);
917
918     ok(dd == dd7, "Returned interface pointer is not equal to the creation interface\n");
919     IUnknown_Release((IUnknown *) dd);
920
921     /* Now test what happens if we QI the surface for some other version - It should still return the creation interface */
922     ret = IDirectDrawSurface4_GetDDInterface(dsurface4, &dd);
923     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
924     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %ld\n", getref((IUnknown *) lpDD) - ref1);
925     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %ld\n", getref((IUnknown *) dd2) - ref2);
926     ok(getref((IUnknown *) dd4) == ref4 + 0, "IDirectDraw4 refcount was increased by %ld\n", getref((IUnknown *) dd4) - ref4);
927     ok(getref((IUnknown *) dd7) == ref7 + 1, "IDirectDraw7 refcount was increased by %ld\n", getref((IUnknown *) dd7) - ref7);
928
929     ok(dd == dd7, "Returned interface pointer is not equal to the creation interface\n");
930     IUnknown_Release((IUnknown *) dd);
931
932     IDirectDraw_Release(dd2);
933     IDirectDraw_Release(dd4);
934     IDirectDraw_Release(dd7);
935     IDirectDrawSurface4_Release(dsurface4);
936     IDirectDrawSurface7_Release(dsurface7);
937 }
938
939 #define MAXEXPECTED 8  /* Can match up to 8 expected surfaces */
940 struct enumstruct
941 {
942     IDirectDrawSurface *expected[MAXEXPECTED];
943     UINT count;
944 };
945
946 static HRESULT WINAPI enumCB(IDirectDrawSurface *surf, DDSURFACEDESC *desc, void *ctx)
947 {
948     int i;
949     BOOL found = FALSE;
950
951     for(i = 0; i < MAXEXPECTED; i++)
952     {
953         if(((struct enumstruct *)ctx)->expected[i] == surf) found = TRUE;
954     }
955
956     ok(found, "Unexpected surface %p enumerated\n", surf);
957     ((struct enumstruct *)ctx)->count++;
958     IDirectDrawSurface_Release(surf);
959     return DDENUMRET_OK;
960 }
961
962 static void EnumTest(void)
963 {
964     HRESULT rc;
965     DDSURFACEDESC ddsd;
966     IDirectDrawSurface *surface;
967     struct enumstruct ctx;
968
969     ddsd.dwSize = sizeof(ddsd);
970     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
971     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
972     U2(ddsd).dwMipMapCount = 3;
973     ddsd.dwWidth = 32;
974     ddsd.dwHeight = 32;
975     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface, NULL);
976     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
977
978     memset(&ctx, 0, sizeof(ctx));
979     ctx.expected[0] = surface;
980     rc = IDirectDrawSurface_GetAttachedSurface(ctx.expected[0], &ddsd.ddsCaps, &ctx.expected[1]);
981     ok(rc == DD_OK, "GetAttachedSurface returned %08x\n", rc);
982     rc = IDirectDrawSurface_GetAttachedSurface(ctx.expected[1], &ddsd.ddsCaps, &ctx.expected[2]);
983     ok(rc == DD_OK, "GetAttachedSurface returned %08x\n", rc);
984     rc = IDirectDrawSurface_GetAttachedSurface(ctx.expected[2], &ddsd.ddsCaps, &ctx.expected[3]);
985     ok(rc == DDERR_NOTFOUND, "GetAttachedSurface returned %08x\n", rc);
986     ctx.count = 0;
987
988     rc = IDirectDraw_EnumSurfaces(lpDD, DDENUMSURFACES_DOESEXIST | DDENUMSURFACES_ALL, &ddsd, (void *) &ctx, enumCB);
989     ok(rc == DD_OK, "IDirectDraw_EnumSurfaces returned %08x\n", rc);
990     ok(ctx.count == 3, "%d surfaces enumerated, expected 3\n", ctx.count);
991
992     IDirectDrawSurface_Release(ctx.expected[2]);
993     IDirectDrawSurface_Release(ctx.expected[1]);
994     IDirectDrawSurface_Release(surface);
995 }
996
997 HRESULT WINAPI SurfaceCounter(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
998 {
999     UINT *num = context;
1000     (*num)++;
1001     IDirectDrawSurface_Release(surface);
1002     return DDENUMRET_OK;
1003 }
1004
1005 static void AttachmentTest7(void)
1006 {
1007     HRESULT hr;
1008     IDirectDraw7 *dd7;
1009     IDirectDrawSurface7 *surface1, *surface2, *surface3, *surface4;
1010     DDSURFACEDESC2 ddsd;
1011     UINT num;
1012     DDSCAPS2 caps = {DDSCAPS_TEXTURE, 0, 0, 0};
1013     HWND window = CreateWindow( "static", "ddraw_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1014
1015     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
1016     ok(hr == DD_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
1017
1018     memset(&ddsd, 0, sizeof(ddsd));
1019     ddsd.dwSize = sizeof(ddsd);
1020     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
1021     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1022     U2(ddsd).dwMipMapCount = 3; /* Will create 128x128, 64x64, 32x32 */
1023     ddsd.dwWidth = 128;
1024     ddsd.dwHeight = 128;
1025     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface1, NULL);
1026     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1027
1028     /* ROOT */
1029     num = 0;
1030     IDirectDrawSurface7_EnumAttachedSurfaces(surface1, &num, SurfaceCounter);
1031     ok(num == 1, "Mipmap root has %d surfaces attached, expected 1\n", num);
1032     /* DONE ROOT */
1033
1034     /* LEVEL 1 */
1035     hr = IDirectDrawSurface7_GetAttachedSurface(surface1, &caps, &surface2);
1036     ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
1037     num = 0;
1038     IDirectDrawSurface7_EnumAttachedSurfaces(surface2, &num, SurfaceCounter);
1039     ok(num == 1, "First mip level has %d surfaces attached, expected 1\n", num);
1040     /* DONE LEVEL 1 */
1041
1042     /* LEVEL 2 */
1043     hr = IDirectDrawSurface7_GetAttachedSurface(surface2, &caps, &surface3);
1044     ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
1045     IDirectDrawSurface7_Release(surface2);
1046     num = 0;
1047     IDirectDrawSurface7_EnumAttachedSurfaces(surface3, &num, SurfaceCounter);
1048     ok(num == 0, "Second mip level has %d surfaces attached, expected 1\n", num);
1049     /* Done level 2 */
1050     /* Mip level 3 is still needed */
1051
1052     /* Try to attach a 16x16 miplevel - Should not work as far I can see */
1053     memset(&ddsd, 0, sizeof(ddsd));
1054     ddsd.dwSize = sizeof(ddsd);
1055     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1056     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1057     ddsd.dwWidth = 16;
1058     ddsd.dwHeight = 16;
1059     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface2, NULL);
1060     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1061
1062     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface2);
1063     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 128x128 texture root returned %08x\n", hr);
1064     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface1);
1065     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 128x128 texture root to a 16x16 texture returned %08x\n", hr);
1066     hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface2);
1067     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 32x32 texture mip level returned %08x\n", hr);
1068     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface3);
1069     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 32x32 texture mip level to a 16x16 surface returned %08x\n", hr);
1070
1071     IDirectDrawSurface7_Release(surface2);
1072
1073     memset(&ddsd, 0, sizeof(ddsd));
1074     ddsd.dwSize = sizeof(ddsd);
1075     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1076     ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
1077     ddsd.dwWidth = 16;
1078     ddsd.dwHeight = 16;
1079     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface2, NULL);
1080     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1081
1082     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface2);
1083     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 128x128 texture root returned %08x\n", hr);
1084     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface1);
1085     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 128x128 texture root to a 16x16 offscreen plain surface returned %08x\n", hr);
1086     hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface2);
1087     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 32x32 texture mip level returned %08x\n", hr);
1088     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface3);
1089     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 32x32 texture mip level to a 16x16 offscreen plain surface returned %08x\n", hr);
1090
1091     IDirectDrawSurface7_Release(surface3);
1092     IDirectDrawSurface7_Release(surface2);
1093     IDirectDrawSurface7_Release(surface1);
1094
1095     hr = IDirectDraw7_SetCooperativeLevel(dd7, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1096     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
1097
1098     memset(&ddsd, 0, sizeof(ddsd));
1099     ddsd.dwSize = sizeof(ddsd);
1100     ddsd.dwFlags = DDSD_BACKBUFFERCOUNT | DDSD_CAPS;
1101     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
1102     ddsd.dwBackBufferCount = 2;
1103     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface1, NULL);
1104     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1105
1106     num = 0;
1107     IDirectDrawSurface7_EnumAttachedSurfaces(surface1, &num, SurfaceCounter);
1108     ok(num == 1, "Primary surface has %d surfaces attached, expected 1\n", num);
1109     IDirectDrawSurface7_Release(surface1);
1110
1111     /* Those are some invalid descriptions, no need to test attachments with them */
1112     memset(&ddsd, 0, sizeof(ddsd));
1113     ddsd.dwSize = sizeof(ddsd);
1114     ddsd.dwFlags = DDSD_CAPS;
1115     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER;
1116     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface1, NULL);
1117     ok(hr==DDERR_INVALIDCAPS,"CreateSurface returned: %x\n",hr);
1118     memset(&ddsd, 0, sizeof(ddsd));
1119     ddsd.dwSize = sizeof(ddsd);
1120     ddsd.dwFlags = DDSD_CAPS;
1121     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER;
1122     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface2, NULL);
1123     ok(hr==DDERR_INVALIDCAPS,"CreateSurface returned: %x\n",hr);
1124
1125     /* Try a single primary and two offscreen plain surfaces */
1126     memset(&ddsd, 0, sizeof(ddsd));
1127     ddsd.dwSize = sizeof(ddsd);
1128     ddsd.dwFlags = DDSD_CAPS;
1129     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
1130     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface1, NULL);
1131     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1132
1133     memset(&ddsd, 0, sizeof(ddsd));
1134     ddsd.dwSize = sizeof(ddsd);
1135     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1136     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1137     ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
1138     ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
1139     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface2, NULL);
1140     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1141
1142     memset(&ddsd, 0, sizeof(ddsd));
1143     ddsd.dwSize = sizeof(ddsd);
1144     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1145     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1146     ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
1147     ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
1148     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface3, NULL);
1149     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1150
1151     /* This one has a different size */
1152     memset(&ddsd, 0, sizeof(ddsd));
1153     ddsd.dwSize = sizeof(ddsd);
1154     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1155     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1156     ddsd.dwWidth = 128;
1157     ddsd.dwHeight = 128;
1158     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface4, NULL);
1159     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1160
1161     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface2);
1162     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching an offscreen plain surface to a front buffer returned %08x\n", hr);
1163     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface1);
1164     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a front buffer to an offscreen plain surface returned %08x\n", hr);
1165     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface3);
1166     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching an offscreen plain surface to another offscreen plain surface returned %08x\n", hr);
1167     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface4);
1168     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching an offscreen plain surface to a front buffer of different size returned %08x\n", hr);
1169     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface1);
1170     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a front buffer to an offscreen plain surface of different size returned %08x\n", hr);
1171
1172     IDirectDrawSurface7_Release(surface4);
1173     IDirectDrawSurface7_Release(surface3);
1174     IDirectDrawSurface7_Release(surface2);
1175     IDirectDrawSurface7_Release(surface1);
1176
1177     hr =IDirectDraw7_SetCooperativeLevel(dd7, NULL, DDSCL_NORMAL);
1178     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
1179     IDirectDraw7_Release(dd7);
1180 }
1181
1182 static void AttachmentTest(void)
1183 {
1184     HRESULT hr;
1185     IDirectDrawSurface *surface1, *surface2, *surface3, *surface4;
1186     DDSURFACEDESC ddsd;
1187     DDSCAPS caps = {DDSCAPS_TEXTURE};
1188     HWND window = CreateWindow( "static", "ddraw_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1189
1190     memset(&ddsd, 0, sizeof(ddsd));
1191     ddsd.dwSize = sizeof(ddsd);
1192     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
1193     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1194     U2(ddsd).dwMipMapCount = 3; /* Will create 128x128, 64x64, 32x32 */
1195     ddsd.dwWidth = 128;
1196     ddsd.dwHeight = 128;
1197     hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &surface1, NULL);
1198     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1199
1200     hr = IDirectDrawSurface7_GetAttachedSurface(surface1, &caps, &surface2);
1201     ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
1202     hr = IDirectDrawSurface7_GetAttachedSurface(surface2, &caps, &surface3);
1203     ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
1204
1205     /* Try to attach a 16x16 miplevel - Should not work as far I can see */
1206     memset(&ddsd, 0, sizeof(ddsd));
1207     ddsd.dwSize = sizeof(ddsd);
1208     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1209     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1210     ddsd.dwWidth = 16;
1211     ddsd.dwHeight = 16;
1212     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface4, NULL);
1213     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1214
1215     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface4);
1216     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 128x128 texture root returned %08x\n", hr);
1217     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface1);
1218     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 128x128 texture root to a 16x16 texture returned %08x\n", hr);
1219     hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface4);
1220     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 32x32 texture mip level returned %08x\n", hr);
1221     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface3);
1222     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 32x32 texture mip level to a 16x16 surface returned %08x\n", hr);
1223     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface4);
1224     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 64x64 texture sublevel returned %08x\n", hr);
1225     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface2);
1226     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 64x64 texture sublevel to a 16x16 texture returned %08x\n", hr);
1227
1228     IDirectDrawSurface7_Release(surface4);
1229
1230     memset(&ddsd, 0, sizeof(ddsd));
1231     ddsd.dwSize = sizeof(ddsd);
1232     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1233     ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
1234     ddsd.dwWidth = 16;
1235     ddsd.dwHeight = 16;
1236     hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &surface4, NULL);
1237     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1238
1239     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface4); /* Succeeds on refrast */
1240     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 128x128 texture root returned %08x\n", hr);
1241     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface1, 0, surface4);
1242     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface1);  /* Succeeds on refrast */
1243     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 128x128 texture root to a 16x16 offscreen plain surface returned %08x\n", hr);
1244     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface1, 0, surface1);
1245     hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface4);  /* Succeeds on refrast */
1246     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 32x32 texture mip level returned %08x\n", hr);
1247     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface3, 0, surface4);
1248     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface3);
1249     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 32x32 texture mip level to a 16x16 offscreen plain surface returned %08x\n", hr);
1250     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface4, 0, surface3);
1251     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface4);  /* Succeeds on refrast */
1252     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 64x64 texture sublevel returned %08x\n", hr);
1253     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface2, 0, surface4);
1254     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface2);
1255     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 64x64 texture sublevel to a 16x16 offscreen plain surface returned %08x\n", hr);
1256     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface4, 0, surface2);
1257
1258     IDirectDrawSurface7_Release(surface4);
1259     IDirectDrawSurface7_Release(surface3);
1260     IDirectDrawSurface7_Release(surface2);
1261     IDirectDrawSurface7_Release(surface1);
1262
1263     hr = IDirectDraw_SetCooperativeLevel(lpDD, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1264     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
1265
1266     /* Creating a back buffer as-is, is not allowed. No need to perform attachment tests */
1267     memset(&ddsd, 0, sizeof(ddsd));
1268     ddsd.dwSize = sizeof(ddsd);
1269     ddsd.dwFlags = DDSD_CAPS;
1270     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER;
1271     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface2, NULL);
1272     ok(hr==DDERR_INVALIDCAPS,"CreateSurface returned: %x\n",hr);
1273     /* This old ddraw version happily creates explicit front buffers */
1274     memset(&ddsd, 0, sizeof(ddsd));
1275     ddsd.dwSize = sizeof(ddsd);
1276     ddsd.dwFlags = DDSD_CAPS;
1277     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER;
1278     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface1, NULL);
1279     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1280     IDirectDrawSurface_Release(surface1);
1281
1282     /* Try a single primary and two offscreen plain surfaces */
1283     memset(&ddsd, 0, sizeof(ddsd));
1284     ddsd.dwSize = sizeof(ddsd);
1285     ddsd.dwFlags = DDSD_CAPS;
1286     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
1287     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface1, NULL);
1288     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1289
1290     memset(&ddsd, 0, sizeof(ddsd));
1291     ddsd.dwSize = sizeof(ddsd);
1292     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1293     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1294     ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
1295     ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
1296     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface2, NULL);
1297     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1298
1299     memset(&ddsd, 0, sizeof(ddsd));
1300     ddsd.dwSize = sizeof(ddsd);
1301     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1302     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1303     ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
1304     ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
1305     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface3, NULL);
1306     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1307
1308     /* This one has a different size */
1309     memset(&ddsd, 0, sizeof(ddsd));
1310     ddsd.dwSize = sizeof(ddsd);
1311     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1312     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1313     ddsd.dwWidth = 128;
1314     ddsd.dwHeight = 128;
1315     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface4, NULL);
1316     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1317
1318     hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
1319     ok(hr == DD_OK, "Attaching an offscreen plain surface to a front buffer returned %08x\n", hr);
1320     /* Try the reverse without detaching first */
1321     hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
1322     ok(hr == DDERR_SURFACEALREADYATTACHED, "Attaching an attached surface to its attachee returned %08x\n", hr);
1323     hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
1324     ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
1325     hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
1326     ok(hr == DD_OK, "Attaching a front buffer to an offscreen plain surface returned %08x\n", hr);
1327     /* Try to detach reversed */
1328     hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
1329     ok(hr == DDERR_CANNOTDETACHSURFACE, "DeleteAttachedSurface returned %08x\n", hr);
1330     /* Now the proper detach */
1331     hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface1);
1332     ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
1333     hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface3); /* Fails on refrast */
1334     ok(hr == DD_OK, "Attaching an offscreen plain surface to another offscreen plain surface returned %08x\n", hr);
1335     if(SUCCEEDED(hr))
1336     {
1337         hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface3);
1338         ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
1339     }
1340     hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4);
1341     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching an offscreen plain surface to a front buffer of different size returned %08x\n", hr);
1342     hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
1343     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a front buffer to an offscreen plain surface of different size returned %08x\n", hr);
1344
1345     IDirectDrawSurface_Release(surface4);
1346     IDirectDrawSurface_Release(surface3);
1347     IDirectDrawSurface_Release(surface2);
1348     IDirectDrawSurface_Release(surface1);
1349
1350     hr =IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
1351     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
1352
1353     DestroyWindow(window);
1354 }
1355
1356 struct compare
1357 {
1358     DWORD width, height;
1359     DWORD caps, caps2;
1360     UINT mips;
1361 };
1362
1363 HRESULT WINAPI CubeTestLvl2Enum(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
1364 {
1365     UINT *mips = context;
1366
1367     (*mips)++;
1368     IDirectDrawSurface7_EnumAttachedSurfaces(surface,
1369                                              context,
1370                                              CubeTestLvl2Enum);
1371
1372     return DDENUMRET_OK;
1373 }
1374
1375 HRESULT WINAPI CubeTestLvl1Enum(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
1376 {
1377     UINT mips = 0;
1378     UINT *num = (UINT *) context;
1379     static const struct compare expected[] =
1380     {
1381         {
1382             128, 128,
1383             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1384             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ,
1385             7
1386         },
1387         {
1388             128, 128,
1389             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1390             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ,
1391             7
1392         },
1393         {
1394             128, 128,
1395             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1396             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY,
1397             7
1398         },
1399         {
1400             128, 128,
1401             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1402             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY,
1403             7
1404         },
1405         {
1406             128, 128,
1407             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1408             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX,
1409             7
1410         },
1411         {
1412             64, 64, /* This is the first mipmap */
1413             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1414             DDSCAPS2_MIPMAPSUBLEVEL | DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX,
1415             6
1416         },
1417     };
1418
1419     mips = 0;
1420     IDirectDrawSurface7_EnumAttachedSurfaces(surface,
1421                                              &mips,
1422                                              CubeTestLvl2Enum);
1423
1424     ok(desc->dwWidth == expected[*num].width, "Surface width is %d expected %d\n", desc->dwWidth, expected[*num].width);
1425     ok(desc->dwHeight == expected[*num].height, "Surface height is %d expected %d\n", desc->dwHeight, expected[*num].height);
1426     ok(desc->ddsCaps.dwCaps == expected[*num].caps, "Surface caps are %08x expected %08x\n", desc->ddsCaps.dwCaps, expected[*num].caps);
1427     ok(desc->ddsCaps.dwCaps2 == expected[*num].caps2, "Surface caps2 are %08x expected %08x\n", desc->ddsCaps.dwCaps2, expected[*num].caps2);
1428     ok(mips == expected[*num].mips, "Surface has %d mipmaps, expected %d\n", mips, expected[*num].mips);
1429
1430     (*num)++;
1431
1432     IDirectDrawSurface7_Release(surface);
1433
1434     return DDENUMRET_OK;
1435 }
1436
1437 static void CubeMapTest(void)
1438 {
1439     IDirectDraw7 *dd7 = NULL;
1440     IDirectDrawSurface7 *cubemap;
1441     DDSURFACEDESC2 ddsd;
1442     HRESULT hr;
1443     UINT num = 0;
1444     struct enumstruct ctx;
1445
1446     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
1447     ok(hr == DD_OK, "IDirectDraw::QueryInterface returned %08x\n", hr);
1448
1449     memset(&ddsd, 0, sizeof(ddsd));
1450     ddsd.dwSize = sizeof(ddsd);
1451     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1452     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1453     ddsd.dwWidth = 128;
1454     ddsd.dwHeight = 128;
1455     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_SYSTEMMEMORY;
1456     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES;
1457
1458     /* D3DFMT_R5G6B5 */
1459     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
1460     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 16;
1461     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0xF800;
1462     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x07E0;
1463     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x001F;
1464
1465     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &cubemap, NULL);
1466     ok(hr == DD_OK, "IDirectDraw7::CreateSurface returned %08x\n", hr);
1467
1468     hr = IDirectDrawSurface7_GetSurfaceDesc(cubemap, &ddsd);
1469     ok(hr == DD_OK, "IDirectDrawSurface7_GetSurfaceDesc returned %08x\n", hr);
1470     ok(ddsd.ddsCaps.dwCaps == (DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX),
1471        "Root Caps are %08x\n", ddsd.ddsCaps.dwCaps);
1472     ok(ddsd.ddsCaps.dwCaps2 == (DDSCAPS2_CUBEMAP_POSITIVEX | DDSCAPS2_CUBEMAP),
1473        "Root Caps2 are %08x\n", ddsd.ddsCaps.dwCaps2);
1474
1475     IDirectDrawSurface7_EnumAttachedSurfaces(cubemap,
1476                                              &num,
1477                                              CubeTestLvl1Enum);
1478     trace("Enumerated %d surfaces in total\n", num);
1479     ok(num == 6, "Surface has %d attachments\n", num);
1480     IDirectDrawSurface7_Release(cubemap);
1481
1482     /* What happens if I do not specify any faces? */
1483     memset(&ddsd, 0, sizeof(ddsd));
1484     ddsd.dwSize = sizeof(ddsd);
1485     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1486     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1487     ddsd.dwWidth = 128;
1488     ddsd.dwHeight = 128;
1489     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_SYSTEMMEMORY;
1490     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP;
1491
1492     /* D3DFMT_R5G6B5 */
1493     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
1494     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 16;
1495     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0xF800;
1496     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x07E0;
1497     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x001F;
1498
1499     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &cubemap, NULL);
1500     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw7::CreateSurface asking for a cube map without faces returned %08x\n", hr);
1501
1502     /* Cube map faces without a cube map? */
1503     memset(&ddsd, 0, sizeof(ddsd));
1504     ddsd.dwSize = sizeof(ddsd);
1505     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1506     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1507     ddsd.dwWidth = 128;
1508     ddsd.dwHeight = 128;
1509     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_SYSTEMMEMORY;
1510     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP_ALLFACES;
1511
1512     /* D3DFMT_R5G6B5 */
1513     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
1514     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 16;
1515     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0xF800;
1516     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x07E0;
1517     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x001F;
1518
1519     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &cubemap, NULL);
1520     ok(hr == DDERR_INVALIDCAPS, "IDirectDraw7::CreateSurface returned %08x\n", hr);
1521
1522     memset(&ddsd, 0, sizeof(ddsd));
1523     ddsd.dwSize = sizeof(ddsd);
1524     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1525     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1526     ddsd.dwWidth = 128;
1527     ddsd.dwHeight = 128;
1528     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_SYSTEMMEMORY;
1529     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP_POSITIVEX;
1530
1531     /* D3DFMT_R5G6B5 */
1532     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
1533     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 16;
1534     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0xF800;
1535     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x07E0;
1536     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x001F;
1537
1538     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &cubemap, NULL);
1539     ok(hr == DDERR_INVALIDCAPS, "IDirectDraw7::CreateSurface returned %08x\n", hr);
1540
1541     /* Make sure everything is cleaned up properly. Use the enumSurfaces test infrastructure */
1542     memset(&ctx, 0, sizeof(ctx));
1543     memset(&ddsd, 0, sizeof(ddsd));
1544     ddsd.dwSize = sizeof(DDSURFACEDESC);
1545     hr = IDirectDraw_EnumSurfaces(lpDD, DDENUMSURFACES_DOESEXIST | DDENUMSURFACES_ALL, (DDSURFACEDESC *) &ddsd, (void *) &ctx, enumCB);
1546     ok(hr == DD_OK, "IDirectDraw_EnumSurfaces returned %08x\n", hr);
1547     ok(ctx.count == 0, "%d surfaces enumerated, expected 0\n", ctx.count);
1548
1549     IDirectDraw7_Release(dd7);
1550 }
1551
1552 static void test_lockrect_invalid(void)
1553 {
1554     unsigned int i, j;
1555
1556     RECT valid[] = {
1557         {60, 60, 68, 68},
1558         {60, 60, 60, 68},
1559         {60, 60, 68, 60},
1560         {120, 60, 128, 68},
1561         {60, 120, 68, 128},
1562     };
1563
1564     RECT invalid[] = {
1565         {68, 60, 60, 68},       /* left > right */
1566         {60, 68, 68, 60},       /* top > bottom */
1567         {-8, 60,  0, 68},       /* left < surface */
1568         {60, -8, 68,  0},       /* top < surface */
1569         {-16, 60, -8, 68},      /* right < surface */
1570         {60, -16, 68, -8},      /* bottom < surface */
1571         {60, 60, 136, 68},      /* right > surface */
1572         {60, 60, 68, 136},      /* bottom > surface */
1573         {136, 60, 144, 68},     /* left > surface */
1574         {60, 136, 68, 144},     /* top > surface */
1575     };
1576
1577     const DWORD dds_caps[] = {
1578         DDSCAPS_OFFSCREENPLAIN,
1579         DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
1580     };
1581
1582     for (j = 0; j < (sizeof(dds_caps) / sizeof(*dds_caps)); ++j)
1583     {
1584         IDirectDrawSurface *surface = 0;
1585         DDSURFACEDESC surface_desc = {0};
1586         DDSURFACEDESC locked_desc = {0};
1587         HRESULT hr;
1588
1589         surface_desc.dwSize = sizeof(surface_desc);
1590         surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
1591         surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
1592         surface_desc.ddsCaps.dwCaps = dds_caps[j];
1593         surface_desc.dwWidth = 128;
1594         surface_desc.dwHeight = 128;
1595         surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
1596         U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
1597         U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0xFF0000;
1598         U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x00FF00;
1599         U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x0000FF;
1600
1601         hr = IDirectDraw_CreateSurface(lpDD, &surface_desc, &surface, NULL);
1602         ok(SUCCEEDED(hr), "CreateSurface failed (0x%08x)\n", hr);
1603         if (FAILED(hr)) {
1604             skip("failed to create surface\n");
1605             continue;
1606         }
1607
1608         for (i = 0; i < (sizeof(valid) / sizeof(*valid)); ++i)
1609         {
1610             RECT *rect = &valid[i];
1611
1612             memset(&locked_desc, 0, sizeof(locked_desc));
1613             locked_desc.dwSize = sizeof(locked_desc);
1614
1615             hr = IDirectDrawSurface_Lock(surface, rect, &locked_desc, DDLOCK_WAIT, NULL);
1616             ok(SUCCEEDED(hr), "Lock failed (0x%08x) for rect [%d, %d]->[%d, %d]\n",
1617                     hr, rect->left, rect->top, rect->right, rect->bottom);
1618
1619             hr = IDirectDrawSurface_Unlock(surface, NULL);
1620             ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1621         }
1622
1623         for (i = 0; i < (sizeof(invalid) / sizeof(*invalid)); ++i)
1624         {
1625             RECT *rect = &invalid[i];
1626
1627             hr = IDirectDrawSurface_Lock(surface, rect, &locked_desc, DDLOCK_WAIT, NULL);
1628             ok(hr == DDERR_INVALIDPARAMS, "Lock returned 0x%08x for rect [%d, %d]->[%d, %d]"
1629                     ", expected DDERR_INVALIDPARAMS (0x%08x)\n", hr, rect->left, rect->top,
1630                     rect->right, rect->bottom, DDERR_INVALIDPARAMS);
1631         }
1632
1633         IDirectDrawSurface_Release(surface);
1634     }
1635 }
1636
1637 static void CompressedTest(void)
1638 {
1639     HRESULT hr;
1640     IDirectDrawSurface7 *surface;
1641     DDSURFACEDESC2 ddsd, ddsd2;
1642     IDirectDraw7 *dd7 = NULL;
1643     RECT r = { 0, 0, 128, 128 };
1644     RECT r2 = { 32, 32, 64, 64 };
1645
1646     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
1647     ok(hr == DD_OK, "IDirectDraw::QueryInterface returned %08x\n", hr);
1648
1649     memset(&ddsd, 0, sizeof(ddsd));
1650     ddsd.dwSize = sizeof(ddsd);
1651     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1652     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1653     ddsd.dwWidth = 128;
1654     ddsd.dwHeight = 128;
1655     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY;
1656     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_FOURCC;
1657     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','1');
1658
1659     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1660     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
1661     if (FAILED(hr)) {
1662         skip("failed to create surface\n");
1663         return;
1664     }
1665
1666     memset(&ddsd2, 0, sizeof(ddsd2));
1667     ddsd2.dwSize = sizeof(ddsd2);
1668     U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1669     hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1670     ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1671
1672     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1673        "Surface desc flags: %08x\n", ddsd2.dwFlags);
1674     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1675     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1676     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1677        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1678     ok(U1(ddsd2).dwLinearSize == 8192, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1679     ok(ddsd2.ddsCaps.dwCaps2 == 0, "Caps2: %08x\n", ddsd2.ddsCaps.dwCaps2);
1680     IDirectDrawSurface7_Release(surface);
1681
1682     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','3');
1683     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1684     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
1685     if (FAILED(hr)) {
1686         skip("failed to create surface\n");
1687         return;
1688     }
1689
1690     memset(&ddsd2, 0, sizeof(ddsd2));
1691     ddsd2.dwSize = sizeof(ddsd2);
1692     U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1693     hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1694     ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1695
1696     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1697        "Surface desc flags: %08x\n", ddsd2.dwFlags);
1698     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1699     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1700     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1701        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1702     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1703     IDirectDrawSurface7_Release(surface);
1704
1705     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','5');
1706     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1707     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
1708     if (FAILED(hr)) {
1709         skip("failed to create surface\n");
1710         return;
1711     }
1712
1713     memset(&ddsd2, 0, sizeof(ddsd2));
1714     ddsd2.dwSize = sizeof(ddsd2);
1715     U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1716     hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1717     ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1718
1719     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1720        "Surface desc flags: %08x\n", ddsd2.dwFlags);
1721     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1722     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1723     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1724        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1725     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1726     ok(ddsd2.lpSurface == 0, "Surface memory is at %p, expected NULL\n", ddsd2.lpSurface);
1727
1728     memset(&ddsd2, 0, sizeof(ddsd2));
1729     ddsd2.dwSize = sizeof(ddsd2);
1730     U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1731
1732     /* Show that the description is not changed when locking the surface. What is really interesting
1733      * about this is that DDSD_LPSURFACE isn't set.
1734      */
1735     hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd2, DDLOCK_READONLY, 0);
1736     ok(hr == DD_OK, "Lock returned %08x\n", hr);
1737
1738     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1739        "Surface desc flags: %08x\n", ddsd2.dwFlags);
1740     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1741     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1742     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1743        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1744     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1745     ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
1746
1747     hr = IDirectDrawSurface7_Unlock(surface, NULL);
1748     ok(hr == DD_OK, "Unlock returned %08x\n", hr);
1749
1750     /* Now what about a locking rect?  */
1751     hr = IDirectDrawSurface7_Lock(surface, &r, &ddsd2, DDLOCK_READONLY, 0);
1752     ok(hr == DD_OK, "Lock returned %08x\n", hr);
1753
1754     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1755        "Surface desc flags: %08x\n", ddsd2.dwFlags);
1756     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1757     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1758     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1759        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1760     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1761     ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
1762
1763     hr = IDirectDrawSurface7_Unlock(surface, &r);
1764     ok(hr == DD_OK, "Unlock returned %08x\n", hr);
1765
1766     /* Now what about a different locking offset? */
1767     hr = IDirectDrawSurface7_Lock(surface, &r2, &ddsd2, DDLOCK_READONLY, 0);
1768     ok(hr == DD_OK, "Lock returned %08x\n", hr);
1769
1770     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1771        "Surface desc flags: %08x\n", ddsd2.dwFlags);
1772     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1773     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1774     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1775        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1776     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1777     ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
1778
1779     hr = IDirectDrawSurface7_Unlock(surface, &r2);
1780     ok(hr == DD_OK, "Unlock returned %08x\n", hr);
1781     IDirectDrawSurface7_Release(surface);
1782
1783     /* Try this with video memory. A kind of surprise. It still has the LINEARSIZE flag set,
1784      * but seems to have a pitch instead.
1785      */
1786     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
1787     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','1');
1788
1789     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1790     ok(hr == DD_OK || hr == DDERR_NOTEXTUREHW, "CreateSurface returned %08x\n", hr);
1791
1792     /* Not supported everywhere */
1793     if(SUCCEEDED(hr))
1794     {
1795         memset(&ddsd2, 0, sizeof(ddsd2));
1796         ddsd2.dwSize = sizeof(ddsd2);
1797         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1798         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1799         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1800
1801         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1802         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1803         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1804         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1805         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
1806         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1807         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
1808         ok(ddsd2.ddsCaps.dwCaps2 == 0, "Caps2: %08x\n", ddsd2.ddsCaps.dwCaps2);
1809         IDirectDrawSurface7_Release(surface);
1810
1811         U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','3');
1812         hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1813         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
1814
1815         memset(&ddsd2, 0, sizeof(ddsd2));
1816         ddsd2.dwSize = sizeof(ddsd2);
1817         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1818         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1819         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1820
1821         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1822         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1823         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1824         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1825         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
1826         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1827         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
1828         IDirectDrawSurface7_Release(surface);
1829
1830         U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','5');
1831         hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1832         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
1833
1834         memset(&ddsd2, 0, sizeof(ddsd2));
1835         ddsd2.dwSize = sizeof(ddsd2);
1836         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1837         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1838         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1839
1840         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1841         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1842         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1843         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1844         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
1845         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1846         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
1847         ok(ddsd2.lpSurface == 0, "Surface memory is at %p, expected NULL\n", ddsd2.lpSurface);
1848
1849         memset(&ddsd2, 0, sizeof(ddsd2));
1850         ddsd2.dwSize = sizeof(ddsd2);
1851         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1852
1853         /* Show that the description is not changed when locking the surface. What is really interesting
1854         * about this is that DDSD_LPSURFACE isn't set.
1855         */
1856         hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd2, DDLOCK_READONLY, 0);
1857         ok(hr == DD_OK, "Lock returned %08x\n", hr);
1858
1859         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1860         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1861         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1862         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1863         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
1864         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1865         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
1866         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
1867
1868         hr = IDirectDrawSurface7_Unlock(surface, NULL);
1869         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
1870
1871         /* Now what about a locking rect?  */
1872         hr = IDirectDrawSurface7_Lock(surface, &r, &ddsd2, DDLOCK_READONLY, 0);
1873         ok(hr == DD_OK, "Lock returned %08x\n", hr);
1874
1875         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1876         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1877         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1878         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1879         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
1880         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1881         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
1882         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
1883
1884         hr = IDirectDrawSurface7_Unlock(surface, &r);
1885         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
1886
1887         /* Now what about a different locking offset? */
1888         hr = IDirectDrawSurface7_Lock(surface, &r2, &ddsd2, DDLOCK_READONLY, 0);
1889         ok(hr == DD_OK, "Lock returned %08x\n", hr);
1890
1891         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1892         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1893         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1894         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1895         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
1896         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1897         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
1898         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
1899
1900         hr = IDirectDrawSurface7_Unlock(surface, &r2);
1901         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
1902
1903         IDirectDrawSurface7_Release(surface);
1904     }
1905     else
1906     {
1907         skip("Hardware DXTN textures not supported\n");
1908     }
1909
1910     /* What happens to managed textures? Interestingly, Windows reports them as being in system
1911      * memory. The linear size fits again.
1912      */
1913     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1914     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE;
1915     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','1');
1916
1917     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1918     ok(hr == DD_OK || hr == DDERR_NOTEXTUREHW, "CreateSurface returned %08x\n", hr);
1919
1920     /* Not supported everywhere */
1921     if(SUCCEEDED(hr))
1922     {
1923         memset(&ddsd2, 0, sizeof(ddsd2));
1924         ddsd2.dwSize = sizeof(ddsd2);
1925         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1926         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1927         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1928
1929         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1930         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1931         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1932         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1933         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1934         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1935         ok(U1(ddsd2).dwLinearSize == 8192, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1936         ok(ddsd2.ddsCaps.dwCaps2 == DDSCAPS2_TEXTUREMANAGE, "Caps2: %08x\n", ddsd2.ddsCaps.dwCaps2);
1937         IDirectDrawSurface7_Release(surface);
1938
1939         U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','3');
1940         hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1941         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
1942
1943         memset(&ddsd2, 0, sizeof(ddsd2));
1944         ddsd2.dwSize = sizeof(ddsd2);
1945         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1946         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1947         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1948
1949         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1950         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1951         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1952         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1953         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1954         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1955         ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1956         IDirectDrawSurface7_Release(surface);
1957
1958         U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','5');
1959         hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1960         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
1961
1962         memset(&ddsd2, 0, sizeof(ddsd2));
1963         ddsd2.dwSize = sizeof(ddsd2);
1964         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1965         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1966         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1967
1968         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1969         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1970         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1971         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1972         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1973         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1974         ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1975         ok(ddsd2.lpSurface == 0, "Surface memory is at %p, expected NULL\n", ddsd2.lpSurface);
1976
1977         memset(&ddsd2, 0, sizeof(ddsd2));
1978         ddsd2.dwSize = sizeof(ddsd2);
1979         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1980
1981         /* Show that the description is not changed when locking the surface. What is really interesting
1982         * about this is that DDSD_LPSURFACE isn't set.
1983         */
1984         hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd2, DDLOCK_READONLY, 0);
1985         ok(hr == DD_OK, "Lock returned %08x\n", hr);
1986
1987         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1988         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1989         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1990         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1991         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1992         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1993         ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1994         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
1995
1996         hr = IDirectDrawSurface7_Unlock(surface, NULL);
1997         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
1998
1999         /* Now what about a locking rect?  */
2000         hr = IDirectDrawSurface7_Lock(surface, &r, &ddsd2, DDLOCK_READONLY, 0);
2001         ok(hr == DD_OK, "Lock returned %08x\n", hr);
2002
2003         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2004         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2005         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2006         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2007         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2008         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2009         ok(U1(ddsd2).dwLinearSize == 16384, "\"Linear\" size is %d\n", U1(ddsd2).dwLinearSize);
2010         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2011
2012         hr = IDirectDrawSurface7_Unlock(surface, &r);
2013         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2014
2015         /* Now what about a different locking offset? */
2016         hr = IDirectDrawSurface7_Lock(surface, &r2, &ddsd2, DDLOCK_READONLY, 0);
2017         ok(hr == DD_OK, "Lock returned %08x\n", hr);
2018
2019         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2020         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2021         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2022         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2023         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2024         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2025         ok(U1(ddsd2).dwLinearSize == 16384, "\"Linear\" size is %d\n", U1(ddsd2).dwLinearSize);
2026         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2027
2028         hr = IDirectDrawSurface7_Unlock(surface, &r2);
2029         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2030
2031         IDirectDrawSurface7_Release(surface);
2032     }
2033     else
2034     {
2035         skip("Hardware DXTN textures not supported\n");
2036     }
2037
2038     IDirectDraw7_Release(dd7);
2039 }
2040
2041 static void SizeTest(void)
2042 {
2043     LPDIRECTDRAWSURFACE dsurface = NULL;
2044     DDSURFACEDESC desc;
2045     HRESULT ret;
2046     HWND window = CreateWindow( "static", "ddraw_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
2047
2048     /* Create an offscreen surface surface without a size */
2049     ZeroMemory(&desc, sizeof(desc));
2050     desc.dwSize = sizeof(desc);
2051     desc.dwFlags = DDSD_CAPS;
2052     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2053     trace("before offscreenplain create dsurface = %p\n", dsurface);
2054     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2055     ok(ret == DDERR_INVALIDPARAMS, "Creating an offscreen plain surface without a size info returned %08x (dsurface=%p)\n", ret, dsurface);
2056     if(dsurface)
2057     {
2058         trace("Surface at %p\n", dsurface);
2059         IDirectDrawSurface_Release(dsurface);
2060         dsurface = NULL;
2061     }
2062
2063     /* Create an offscreen surface surface with only a width parameter */
2064     ZeroMemory(&desc, sizeof(desc));
2065     desc.dwSize = sizeof(desc);
2066     desc.dwFlags = DDSD_CAPS | DDSD_WIDTH;
2067     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2068     desc.dwWidth = 128;
2069     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2070     ok(ret == DDERR_INVALIDPARAMS, "Creating an offscreen plain surface without hight info returned %08x\n", ret);
2071     if(dsurface)
2072     {
2073         IDirectDrawSurface_Release(dsurface);
2074         dsurface = NULL;
2075     }
2076
2077     /* Create an offscreen surface surface with only a height parameter */
2078     ZeroMemory(&desc, sizeof(desc));
2079     desc.dwSize = sizeof(desc);
2080     desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT;
2081     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2082     desc.dwHeight = 128;
2083     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2084     ok(ret == DDERR_INVALIDPARAMS, "Creating an offscreen plain surface without width info returned %08x\n", ret);
2085     if(dsurface)
2086     {
2087         IDirectDrawSurface_Release(dsurface);
2088         dsurface = NULL;
2089     }
2090
2091     /* Sanity check */
2092     ZeroMemory(&desc, sizeof(desc));
2093     desc.dwSize = sizeof(desc);
2094     desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
2095     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2096     desc.dwHeight = 128;
2097     desc.dwWidth = 128;
2098     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2099     ok(ret == DD_OK, "Creating an offscreen plain surface with width and height info returned %08x\n", ret);
2100     if(dsurface)
2101     {
2102         IDirectDrawSurface_Release(dsurface);
2103         dsurface = NULL;
2104     }
2105
2106     /* Test a primary surface size */
2107     ret = IDirectDraw_SetCooperativeLevel(lpDD, window, DDSCL_NORMAL);
2108     ok(ret == DD_OK, "SetCooperativeLevel failed with %08x\n", ret);
2109
2110     ZeroMemory(&desc, sizeof(desc));
2111     desc.dwSize = sizeof(desc);
2112     desc.dwFlags = DDSD_CAPS;
2113     desc.ddsCaps.dwCaps |= DDSCAPS_PRIMARYSURFACE;
2114     desc.dwHeight = 128; /* Keep them set to  check what happens */
2115     desc.dwWidth = 128; /* Keep them set to  check what happens */
2116     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2117     ok(ret == DD_OK, "Creating a primary surface without width and height info returned %08x\n", ret);
2118     if(dsurface)
2119     {
2120         ret = IDirectDrawSurface_GetSurfaceDesc(dsurface, &desc);
2121         ok(ret == DD_OK, "GetSurfaceDesc returned %x\n", ret);
2122
2123         IDirectDrawSurface_Release(dsurface);
2124         dsurface = NULL;
2125
2126         ok(desc.dwFlags & DDSD_WIDTH, "Primary surface doesn't have width set\n");
2127         ok(desc.dwFlags & DDSD_HEIGHT, "Primary surface doesn't have height set\n");
2128         ok(desc.dwWidth == GetSystemMetrics(SM_CXSCREEN), "Surface width differs from screen width\n");
2129         ok(desc.dwHeight == GetSystemMetrics(SM_CYSCREEN), "Surface height differs from screen height\n");
2130     }
2131     ret = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
2132     ok(ret == DD_OK, "SetCooperativeLevel failed with %08x\n", ret);
2133 }
2134
2135 static void PrivateDataTest(void)
2136 {
2137     HRESULT hr;
2138     IDirectDrawSurface7 *surface7 = NULL;
2139     IDirectDrawSurface *surface = NULL;
2140     DDSURFACEDESC desc;
2141     ULONG ref, ref2;
2142     IUnknown *ptr;
2143     DWORD size = sizeof(IUnknown *);
2144
2145     ZeroMemory(&desc, sizeof(desc));
2146     desc.dwSize = sizeof(desc);
2147     desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
2148     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2149     desc.dwHeight = 128;
2150     desc.dwWidth = 128;
2151     hr = IDirectDraw_CreateSurface(lpDD, &desc, &surface, NULL);
2152     ok(hr == DD_OK, "Creating an offscreen plain surface failed with %08x\n", hr);
2153     if(!surface)
2154     {
2155         return;
2156     }
2157     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface7, (void **) &surface7);
2158     ok(hr == DD_OK, "IDirectDrawSurface_QueryInterface failed with %08x\n", hr);
2159     if(!surface)
2160     {
2161         IDirectDrawSurface_Release(surface);
2162         return;
2163     }
2164
2165     /* This fails */
2166     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7 /* Abuse this tag */, lpDD, 0, DDSPD_IUNKNOWNPTR);
2167     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2168     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7 /* Abuse this tag */, lpDD, 5, DDSPD_IUNKNOWNPTR);
2169     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2170     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7 /* Abuse this tag */, lpDD, sizeof(IUnknown *) * 2, DDSPD_IUNKNOWNPTR);
2171     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2172
2173     ref = getref((IUnknown *) lpDD);
2174     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7 /* Abuse this tag */, lpDD, sizeof(IUnknown *), DDSPD_IUNKNOWNPTR);
2175     ok(hr == DD_OK, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2176     ref2 = getref((IUnknown *) lpDD);
2177     ok(ref2 == ref + 1, "Object reference is %d, expected %d\n", ref2, ref + 1);
2178     hr = IDirectDrawSurface7_FreePrivateData(surface7, &IID_IDirectDrawSurface7);
2179     ref2 = getref((IUnknown *) lpDD);
2180     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
2181
2182     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7, lpDD, sizeof(IUnknown *), DDSPD_IUNKNOWNPTR);
2183     ok(hr == DD_OK, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2184     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7, surface7, sizeof(IUnknown *), DDSPD_IUNKNOWNPTR);
2185     ok(hr == DD_OK, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2186     ref2 = getref((IUnknown *) lpDD);
2187     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
2188
2189     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7, lpDD, sizeof(IUnknown *), DDSPD_IUNKNOWNPTR);
2190     ok(hr == DD_OK, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2191     hr = IDirectDrawSurface7_GetPrivateData(surface7, &IID_IDirectDrawSurface7, &ptr, &size);
2192     ok(hr == DD_OK, "IDirectDrawSurface7_GetPrivateData failed with %08x\n", hr);
2193     ref2 = getref((IUnknown *) lpDD);
2194     /* Object is NOT beein addrefed */
2195     ok(ptr == (IUnknown *) lpDD, "Returned interface pointer is %p, expected %p\n", ptr, lpDD);
2196     ok(ref2 == ref + 1, "Object reference is %d, expected %d. ptr at %p, orig at %p\n", ref2, ref + 1, ptr, lpDD);
2197
2198     IDirectDrawSurface_Release(surface);
2199     IDirectDrawSurface7_Release(surface7);
2200
2201     /* Destroying the surface frees the held reference */
2202     ref2 = getref((IUnknown *) lpDD);
2203     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
2204 }
2205
2206 START_TEST(dsurface)
2207 {
2208     if (!CreateDirectDraw())
2209         return;
2210     MipMapCreationTest();
2211     SrcColorKey32BlitTest();
2212     QueryInterface();
2213     GetDDInterface_1();
2214     GetDDInterface_2();
2215     GetDDInterface_4();
2216     GetDDInterface_7();
2217     EnumTest();
2218     AttachmentTest();
2219     AttachmentTest7();
2220     CubeMapTest();
2221     test_lockrect_invalid();
2222     CompressedTest();
2223     SizeTest();
2224     PrivateDataTest();
2225     ReleaseDirectDraw();
2226 }