ddraw/tests: Use ULONG instead of unsigned long.
[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  * Copyright (C) 2008 Alexander Dorofeyev
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 #define COBJMACROS
25
26 #include <assert.h>
27 #include "wine/test.h"
28 #include "ddraw.h"
29 #include "unknwn.h"
30
31 static LPDIRECTDRAW lpDD = NULL;
32
33 static BOOL CreateDirectDraw(void)
34 {
35     HRESULT rc;
36
37     rc = DirectDrawCreate(NULL, &lpDD, NULL);
38     ok(rc==DD_OK || rc==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", rc);
39     if (!lpDD) {
40         trace("DirectDrawCreateEx() failed with an error %x\n", rc);
41         return FALSE;
42     }
43
44     rc = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
45     ok(rc==DD_OK,"SetCooperativeLevel returned: %x\n",rc);
46
47     return TRUE;
48 }
49
50
51 static void ReleaseDirectDraw(void)
52 {
53     if( lpDD != NULL )
54     {
55         IDirectDraw_Release(lpDD);
56         lpDD = NULL;
57     }
58 }
59
60 static void MipMapCreationTest(void)
61 {
62     LPDIRECTDRAWSURFACE lpDDSMipMapTest;
63     DDSURFACEDESC ddsd;
64     HRESULT rc;
65
66     /* First mipmap creation test: create a surface with DDSCAPS_COMPLEX,
67        DDSCAPS_MIPMAP, and DDSD_MIPMAPCOUNT. This create the number of
68         requested mipmap levels. */
69     ddsd.dwSize = sizeof(ddsd);
70     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
71     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
72     U2(ddsd).dwMipMapCount = 3;
73     ddsd.dwWidth = 128;
74     ddsd.dwHeight = 32;
75     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
76     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
77     if (FAILED(rc)) {
78         skip("failed to create surface\n");
79         return;
80     }
81
82     /* Check the number of created mipmaps */
83     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
84     ddsd.dwSize = sizeof(ddsd);
85     rc = IDirectDrawSurface_GetSurfaceDesc(lpDDSMipMapTest, &ddsd);
86     ok(rc==DD_OK,"GetSurfaceDesc returned: %x\n",rc);
87     ok(ddsd.dwFlags & DDSD_MIPMAPCOUNT,
88         "GetSurfaceDesc returned no mipmapcount.\n");
89     ok(U2(ddsd).dwMipMapCount == 3, "Incorrect mipmap count: %d.\n",
90         U2(ddsd).dwMipMapCount);
91
92     /* Destroy the surface. */
93     IDirectDrawSurface_Release(lpDDSMipMapTest);
94
95
96     /* Second mipmap creation test: create a surface without a mipmap
97        count, with DDSCAPS_MIPMAP and without DDSCAPS_COMPLEX.
98        This creates a single mipmap level. */
99     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
100     ddsd.dwSize = sizeof(ddsd);
101     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
102     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_MIPMAP;
103     ddsd.dwWidth = 128;
104     ddsd.dwHeight = 32;
105     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
106     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
107     if (FAILED(rc)) {
108         skip("failed to create surface\n");
109         return;
110     }
111     /* Check the number of created mipmaps */
112     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
113     ddsd.dwSize = sizeof(ddsd);
114     rc = IDirectDrawSurface_GetSurfaceDesc(lpDDSMipMapTest, &ddsd);
115     ok(rc==DD_OK,"GetSurfaceDesc returned: %x\n",rc);
116     ok(ddsd.dwFlags & DDSD_MIPMAPCOUNT,
117         "GetSurfaceDesc returned no mipmapcount.\n");
118     ok(U2(ddsd).dwMipMapCount == 1, "Incorrect mipmap count: %d.\n",
119         U2(ddsd).dwMipMapCount);
120
121     /* Destroy the surface. */
122     IDirectDrawSurface_Release(lpDDSMipMapTest);
123
124
125     /* Third mipmap creation test: create a surface with DDSCAPS_MIPMAP,
126         DDSCAPS_COMPLEX and without DDSD_MIPMAPCOUNT.
127        It's an undocumented features where a chain of mipmaps, starting from
128        he specified size and down to the smallest size, is automatically
129        created.
130        Anarchy Online needs this feature to work. */
131     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
132     ddsd.dwSize = sizeof(ddsd);
133     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
134     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
135     ddsd.dwWidth = 128;
136     ddsd.dwHeight = 32;
137     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
138     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
139     if (FAILED(rc)) {
140         skip("failed to create surface\n");
141         return;
142     }
143
144     /* Check the number of created mipmaps */
145     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
146     ddsd.dwSize = sizeof(ddsd);
147     rc = IDirectDrawSurface_GetSurfaceDesc(lpDDSMipMapTest, &ddsd);
148     ok(rc==DD_OK,"GetSurfaceDesc returned: %x\n",rc);
149     ok(ddsd.dwFlags & DDSD_MIPMAPCOUNT,
150         "GetSurfaceDesc returned no mipmapcount.\n");
151     ok(U2(ddsd).dwMipMapCount == 6, "Incorrect mipmap count: %d.\n",
152         U2(ddsd).dwMipMapCount);
153
154     /* Destroy the surface. */
155     IDirectDrawSurface_Release(lpDDSMipMapTest);
156
157
158     /* Fourth mipmap creation test: same as above with a different texture
159        size.
160        The purpose is to verify that the number of generated mipmaps is
161        dependent on the smallest dimension. */
162     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
163     ddsd.dwSize = sizeof(ddsd);
164     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
165     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
166     ddsd.dwWidth = 32;
167     ddsd.dwHeight = 64;
168     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
169     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
170     if (FAILED(rc)) {
171         skip("failed to create surface\n");
172         return;
173     }
174
175     /* Check the number of created mipmaps */
176     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
177     ddsd.dwSize = sizeof(ddsd);
178     rc = IDirectDrawSurface_GetSurfaceDesc(lpDDSMipMapTest, &ddsd);
179     ok(rc==DD_OK,"GetSurfaceDesc returned: %x\n",rc);
180     ok(ddsd.dwFlags & DDSD_MIPMAPCOUNT,
181         "GetSurfaceDesc returned no mipmapcount.\n");
182     ok(U2(ddsd).dwMipMapCount == 6, "Incorrect mipmap count: %d.\n",
183         U2(ddsd).dwMipMapCount);
184
185     /* Destroy the surface. */
186     IDirectDrawSurface_Release(lpDDSMipMapTest);
187
188
189     /* Fifth mipmap creation test: try to create a surface with
190        DDSCAPS_COMPLEX, DDSCAPS_MIPMAP, DDSD_MIPMAPCOUNT,
191        where dwMipMapCount = 0. This should fail. */
192
193     ddsd.dwSize = sizeof(ddsd);
194     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
195     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
196     U2(ddsd).dwMipMapCount = 0;
197     ddsd.dwWidth = 128;
198     ddsd.dwHeight = 32;
199     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
200     ok(rc==DDERR_INVALIDPARAMS,"CreateSurface returned: %x\n",rc);
201
202     /* Destroy the surface. */
203     if( rc == DD_OK )
204         IDirectDrawSurface_Release(lpDDSMipMapTest);
205
206 }
207
208 static void SrcColorKey32BlitTest(void)
209 {
210     LPDIRECTDRAWSURFACE lpSrc;
211     LPDIRECTDRAWSURFACE lpDst;
212     DDSURFACEDESC ddsd, ddsd2, ddsd3;
213     DDCOLORKEY DDColorKey;
214     LPDWORD lpData;
215     HRESULT rc;
216     DDBLTFX fx;
217
218     ddsd2.dwSize = sizeof(ddsd2);
219     ddsd2.ddpfPixelFormat.dwSize = sizeof(ddsd2.ddpfPixelFormat);
220
221     ddsd.dwSize = sizeof(ddsd);
222     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
223     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
224     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
225     ddsd.dwWidth = 800;
226     ddsd.dwHeight = 600;
227     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
228     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
229     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xFF0000;
230     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x00FF00;
231     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x0000FF;
232     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDst, NULL);
233     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
234     if (FAILED(rc)) {
235         skip("failed to create surface\n");
236         return;
237     }
238
239     ddsd.dwFlags |= DDSD_CKSRCBLT;
240     ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0xFF00FF;
241     ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0xFF00FF;
242     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpSrc, NULL);
243     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
244     if (FAILED(rc)) {
245         skip("failed to create surface\n");
246         return;
247     }
248
249     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
250     ok(rc==DD_OK,"Lock returned: %x\n",rc);
251     lpData = ddsd2.lpSurface;
252     lpData[0] = 0xCCCCCCCC;
253     lpData[1] = 0xCCCCCCCC;
254     lpData[2] = 0xCCCCCCCC;
255     lpData[3] = 0xCCCCCCCC;
256
257     memset(&ddsd3, 0, sizeof(ddsd3));
258     ddsd3.dwSize = sizeof(ddsd3);
259     ddsd3.ddpfPixelFormat.dwSize = sizeof(ddsd3.ddpfPixelFormat);
260     rc = IDirectDrawSurface_GetSurfaceDesc(lpDst, &ddsd3);
261     ok(rc == DD_OK, "IDirectDrawSurface_GetSurfaceDesc between a lock/unlock pair returned %08x\n", rc);
262     ok(ddsd3.lpSurface == ddsd3.lpSurface, "lpSurface from GetSurfaceDesc(%p) differs from the one returned by Lock(%p)\n", ddsd3.lpSurface, ddsd2.lpSurface);
263
264     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
265     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
266
267     memset(&ddsd3, 0, sizeof(ddsd3));
268     ddsd3.dwSize = sizeof(ddsd3);
269     ddsd3.ddpfPixelFormat.dwSize = sizeof(ddsd3.ddpfPixelFormat);
270     rc = IDirectDrawSurface_GetSurfaceDesc(lpDst, &ddsd3);
271     ok(rc == DD_OK, "IDirectDrawSurface_GetSurfaceDesc between a lock/unlock pair returned %08x\n", rc);
272     ok(ddsd3.lpSurface == NULL, "lpSurface from GetSurfaceDesc(%p) is not NULL after unlock\n", ddsd3.lpSurface);
273
274     rc = IDirectDrawSurface_Lock(lpSrc, NULL, &ddsd2, DDLOCK_WAIT, NULL);
275     ok(rc==DD_OK,"Lock returned: %x\n",rc);
276     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
277     lpData = ddsd2.lpSurface;
278     lpData[0] = 0x77010203;
279     lpData[1] = 0x00010203;
280     lpData[2] = 0x77FF00FF;
281     lpData[3] = 0x00FF00FF;
282     rc = IDirectDrawSurface_Unlock(lpSrc, NULL);
283     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
284
285     IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRC, NULL);
286
287     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
288     ok(rc==DD_OK,"Lock returned: %x\n",rc);
289     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
290     lpData = ddsd2.lpSurface;
291     /* Different behavior on some drivers / windows versions. Some versions ignore the X channel when
292      * color keying, but copy it to the destination surface. Others apply it for color keying, but
293      * do not copy it into the destination surface.
294      */
295     if(lpData[0]==0x00010203) {
296         trace("X channel was not copied into the destination surface\n");
297         ok((lpData[0]==0x00010203)&&(lpData[1]==0x00010203)&&(lpData[2]==0x00FF00FF)&&(lpData[3]==0xCCCCCCCC),
298            "Destination data after blitting is not correct\n");
299     } else {
300         ok((lpData[0]==0x77010203)&&(lpData[1]==0x00010203)&&(lpData[2]==0xCCCCCCCC)&&(lpData[3]==0xCCCCCCCC),
301            "Destination data after blitting is not correct\n");
302     }
303     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
304     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
305
306     /* Also test SetColorKey */
307     IDirectDrawSurface_GetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
308     ok(DDColorKey.dwColorSpaceLowValue == 0xFF00FF && DDColorKey.dwColorSpaceHighValue == 0xFF00FF,
309        "GetColorKey does not return the colorkey used at surface creation\n");
310
311     DDColorKey.dwColorSpaceLowValue = 0x00FF00;
312     DDColorKey.dwColorSpaceHighValue = 0x00FF00;
313     IDirectDrawSurface_SetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
314
315     DDColorKey.dwColorSpaceLowValue = 0;
316     DDColorKey.dwColorSpaceHighValue = 0;
317     IDirectDrawSurface_GetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
318     ok(DDColorKey.dwColorSpaceLowValue == 0x00FF00 && DDColorKey.dwColorSpaceHighValue == 0x00FF00,
319        "GetColorKey does not return the colorkey set with SetColorKey\n");
320
321     ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0;
322     ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0;
323     IDirectDrawSurface_GetSurfaceDesc(lpSrc, &ddsd);
324     ok(ddsd.ddckCKSrcBlt.dwColorSpaceLowValue == 0x00FF00 && ddsd.ddckCKSrcBlt.dwColorSpaceHighValue == 0x00FF00,
325        "GetSurfaceDesc does not return the colorkey set with SetColorKey\n");
326
327     IDirectDrawSurface_Release(lpSrc);
328     IDirectDrawSurface_Release(lpDst);
329
330     /* start with a new set of surfaces to test the color keying parameters to blit */
331     memset(&ddsd, 0, sizeof(ddsd));
332     ddsd.dwSize = sizeof(ddsd);
333     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
334     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT | DDSD_CKDESTBLT;
335     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
336     ddsd.dwWidth = 800;
337     ddsd.dwHeight = 600;
338     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
339     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
340     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xFF0000;
341     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x00FF00;
342     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x0000FF;
343     ddsd.ddckCKDestBlt.dwColorSpaceLowValue = 0xFF0000;
344     ddsd.ddckCKDestBlt.dwColorSpaceHighValue = 0xFF0000;
345     ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00FF00;
346     ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00FF00;
347     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDst, NULL);
348     ok(rc==DD_OK || rc == DDERR_NOCOLORKEYHW,"CreateSurface returned: %x\n",rc);
349     if(FAILED(rc))
350     {
351         skip("Failed to create surface\n");
352         return;
353     }
354
355     /* start with a new set of surfaces to test the color keying parameters to blit */
356     memset(&ddsd, 0, sizeof(ddsd));
357     ddsd.dwSize = sizeof(ddsd);
358     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
359     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT | DDSD_CKDESTBLT;
360     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
361     ddsd.dwWidth = 800;
362     ddsd.dwHeight = 600;
363     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
364     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
365     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xFF0000;
366     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x00FF00;
367     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x0000FF;
368     ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0x0000FF;
369     ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0x0000FF;
370     ddsd.ddckCKDestBlt.dwColorSpaceLowValue = 0x000000;
371     ddsd.ddckCKDestBlt.dwColorSpaceHighValue = 0x000000;
372     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpSrc, NULL);
373     ok(rc==DD_OK || rc == DDERR_NOCOLORKEYHW,"CreateSurface returned: %x\n",rc);
374     if(FAILED(rc))
375     {
376         skip("Failed to create surface\n");
377         IDirectDrawSurface_Release(lpDst);
378         return;
379     }
380
381     memset(&fx, 0, sizeof(fx));
382     fx.dwSize = sizeof(fx);
383     fx.ddckSrcColorkey.dwColorSpaceHighValue = 0x110000;
384     fx.ddckSrcColorkey.dwColorSpaceLowValue = 0x110000;
385     fx.ddckDestColorkey.dwColorSpaceHighValue = 0x001100;
386     fx.ddckDestColorkey.dwColorSpaceLowValue = 0x001100;
387
388     rc = IDirectDrawSurface_Lock(lpSrc, 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 = ddsd2.lpSurface;
392     lpData[0] = 0x000000FF; /* Applies to src blt key in src surface */
393     lpData[1] = 0x00000000; /* Applies to dst blt key in src surface */
394     lpData[2] = 0x00FF0000; /* Dst color key in dst surface */
395     lpData[3] = 0x0000FF00; /* Src color key in dst surface */
396     lpData[4] = 0x00001100; /* Src color key in ddbltfx */
397     lpData[5] = 0x00110000; /* Dst color key in ddbltfx */
398     rc = IDirectDrawSurface_Unlock(lpSrc, NULL);
399     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
400
401     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
402     ok(rc==DD_OK,"Lock returned: %x\n",rc);
403     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
404     lpData = ddsd2.lpSurface;
405     lpData[0] = 0x55555555;
406     lpData[1] = 0x55555555;
407     lpData[2] = 0x55555555;
408     lpData[3] = 0x55555555;
409     lpData[4] = 0x55555555;
410     lpData[5] = 0x55555555;
411     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
412     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
413
414     /* Test a blit without keying */
415     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, 0, &fx);
416     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
417
418     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
419     ok(rc==DD_OK,"Lock returned: %x\n",rc);
420     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
421     lpData = ddsd2.lpSurface;
422     /* Should have copied src data unmodified to dst */
423     ok(lpData[0] == 0x000000FF &&
424        lpData[1] == 0x00000000 &&
425        lpData[2] == 0x00FF0000 &&
426        lpData[3] == 0x0000FF00 &&
427        lpData[4] == 0x00001100 &&
428        lpData[5] == 0x00110000, "Surface data after unkeyed blit does not match\n");
429
430     lpData[0] = 0x55555555;
431     lpData[1] = 0x55555555;
432     lpData[2] = 0x55555555;
433     lpData[3] = 0x55555555;
434     lpData[4] = 0x55555555;
435     lpData[5] = 0x55555555;
436     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
437     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
438
439     /* Src key */
440     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRC, &fx);
441     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
442
443     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
444     ok(rc==DD_OK,"Lock returned: %x\n",rc);
445     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
446     lpData = ddsd2.lpSurface;
447
448     ok(lpData[0] == 0x55555555 && /* Here the src key applied */
449        lpData[1] == 0x00000000 &&
450        lpData[2] == 0x00FF0000 &&
451        lpData[3] == 0x0000FF00 &&
452        lpData[4] == 0x00001100 &&
453        lpData[5] == 0x00110000, "Surface data after srckey blit does not match\n");
454
455     lpData[0] = 0x55555555;
456     lpData[1] = 0x55555555;
457     lpData[2] = 0x55555555;
458     lpData[3] = 0x55555555;
459     lpData[4] = 0x55555555;
460     lpData[5] = 0x55555555;
461     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
462     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
463
464     /* Src override */
465     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRCOVERRIDE, &fx);
466     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
467
468     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
469     ok(rc==DD_OK,"Lock returned: %x\n",rc);
470     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
471     lpData = ddsd2.lpSurface;
472
473     ok(lpData[0] == 0x000000FF &&
474        lpData[1] == 0x00000000 &&
475        lpData[2] == 0x00FF0000 &&
476        lpData[3] == 0x0000FF00 &&
477        lpData[4] == 0x00001100 &&
478        lpData[5] == 0x55555555, /* Override key applies here */
479        "Surface data after src override key blit does not match\n");
480
481     lpData[0] = 0x55555555;
482     lpData[1] = 0x55555555;
483     lpData[2] = 0x55555555;
484     lpData[3] = 0x55555555;
485     lpData[4] = 0x55555555;
486     lpData[5] = 0x55555555;
487     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
488     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
489
490     /* Src override AND src key. That is not supposed to work */
491     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRC | DDBLT_KEYSRCOVERRIDE, &fx);
492     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
493
494     /* Verify that the destination is unchanged */
495     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
496     ok(rc==DD_OK,"Lock returned: %x\n",rc);
497     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
498     lpData = ddsd2.lpSurface;
499
500     ok(lpData[0] == 0x55555555 &&
501        lpData[1] == 0x55555555 &&
502        lpData[2] == 0x55555555 &&
503        lpData[3] == 0x55555555 &&
504        lpData[4] == 0x55555555 &&
505        lpData[5] == 0x55555555, /* Override key applies here */
506        "Surface data after src key blit with override does not match\n");
507
508     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
509     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
510     lpData[2] = 0x00001100; /* Dest key in override */
511     lpData[3] = 0x00001100; /* Dest key in override */
512     lpData[4] = 0x00000000; /* Dest key in src surface */
513     lpData[5] = 0x00000000; /* Dest key in src surface */
514     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
515     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
516
517     /* Dest key blit */
518     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDEST, &fx);
519     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
520
521     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
522     ok(rc==DD_OK,"Lock returned: %x\n",rc);
523     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
524     lpData = ddsd2.lpSurface;
525
526     /* DirectDraw uses the dest blit key from the SOURCE surface ! */
527     ok(lpData[0] == 0x00ff0000 &&
528        lpData[1] == 0x00ff0000 &&
529        lpData[2] == 0x00001100 &&
530        lpData[3] == 0x00001100 &&
531        lpData[4] == 0x00001100 && /* Key applies here */
532        lpData[5] == 0x00110000,   /* Key applies here */
533        "Surface data after dest key blit does not match\n");
534
535     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
536     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
537     lpData[2] = 0x00001100; /* Dest key in override */
538     lpData[3] = 0x00001100; /* Dest key in override */
539     lpData[4] = 0x00000000; /* Dest key in src surface */
540     lpData[5] = 0x00000000; /* Dest key in src surface */
541     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
542     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
543
544     /* Dest override key blit */
545     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDESTOVERRIDE, &fx);
546     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
547
548     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
549     ok(rc==DD_OK,"Lock returned: %x\n",rc);
550     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
551     lpData = ddsd2.lpSurface;
552
553     ok(lpData[0] == 0x00FF0000 &&
554        lpData[1] == 0x00FF0000 &&
555        lpData[2] == 0x00FF0000 && /* Key applies here */
556        lpData[3] == 0x0000FF00 && /* Key applies here */
557        lpData[4] == 0x00000000 &&
558        lpData[5] == 0x00000000,
559        "Surface data after dest key override blit does not match\n");
560
561     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
562     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
563     lpData[2] = 0x00001100; /* Dest key in override */
564     lpData[3] = 0x00001100; /* Dest key in override */
565     lpData[4] = 0x00000000; /* Dest key in src surface */
566     lpData[5] = 0x00000000; /* Dest key in src surface */
567     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
568     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
569
570     /* Dest override key blit. Supposed to fail too */
571     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDEST | DDBLT_KEYDESTOVERRIDE, &fx);
572     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
573
574     /* Check for unchanged data */
575     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
576     ok(rc==DD_OK,"Lock returned: %x\n",rc);
577     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
578     lpData = ddsd2.lpSurface;
579
580     ok(lpData[0] == 0x00FF0000 &&
581        lpData[1] == 0x00FF0000 &&
582        lpData[2] == 0x00001100 && /* Key applies here */
583        lpData[3] == 0x00001100 && /* Key applies here */
584        lpData[4] == 0x00000000 &&
585        lpData[5] == 0x00000000,
586        "Surface data with dest key and dest override does not match\n");
587
588     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
589     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
590     lpData[2] = 0x00001100; /* Dest key in override */
591     lpData[3] = 0x00001100; /* Dest key in override */
592     lpData[4] = 0x00000000; /* Dest key in src surface */
593     lpData[5] = 0x00000000; /* Dest key in src surface */
594     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
595     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
596
597     /* Modify the source data a bit to give some more conclusive results */
598     rc = IDirectDrawSurface_Lock(lpSrc, NULL, &ddsd2, DDLOCK_WAIT, NULL);
599     ok(rc==DD_OK,"Lock returned: %x\n",rc);
600     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
601     lpData = ddsd2.lpSurface;
602     lpData[5] = 0x000000FF; /* Applies to src blt key in src surface */
603     rc = IDirectDrawSurface_Unlock(lpSrc, NULL);
604     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
605
606     /* Source and destination key */
607     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDEST | DDBLT_KEYSRC, &fx);
608     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
609
610     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
611     ok(rc==DD_OK,"Lock returned: %x\n",rc);
612     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
613     lpData = ddsd2.lpSurface;
614
615     ok(lpData[0] == 0x00FF0000 && /* Masked by Destination key */
616        lpData[1] == 0x00FF0000 && /* Masked by Destination key */
617        lpData[2] == 0x00001100 && /* Masked by Destination key */
618        lpData[3] == 0x00001100 && /* Masked by Destination key */
619        lpData[4] == 0x00001100 && /* Allowed by destination key, not masked by source key */
620        lpData[5] == 0x00000000,   /* Allowed by dst key, but masked by source key */
621        "Surface data with src key and dest key blit does not match\n");
622
623     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
624     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
625     lpData[2] = 0x00001100; /* Dest key in override */
626     lpData[3] = 0x00001100; /* Dest key in override */
627     lpData[4] = 0x00000000; /* Dest key in src surface */
628     lpData[5] = 0x00000000; /* Dest key in src surface */
629     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
630     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
631
632     /* Override keys without ddbltfx parameter fail */
633     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDESTOVERRIDE, NULL);
634     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
635     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRCOVERRIDE, NULL);
636     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
637
638     /* Try blitting without keys in the source surface*/
639     rc = IDirectDrawSurface_SetColorKey(lpSrc, DDCKEY_SRCBLT, NULL);
640     ok(rc == DD_OK, "SetColorKey returned %x\n", rc);
641     rc = IDirectDrawSurface_SetColorKey(lpSrc, DDCKEY_DESTBLT, NULL);
642     ok(rc == DD_OK, "SetColorKey returned %x\n", rc);
643
644     /* That fails now. Do not bother to check that the data is unmodified */
645     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRC, &fx);
646     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
647
648     /* Dest key blit still works. Which key is used this time??? */
649     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDEST, &fx);
650     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
651
652     /* With correctly passed override keys no key in the surface is needed.
653      * Again, the result was checked before, no need to do that again
654      */
655     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDESTOVERRIDE, &fx);
656     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
657     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRCOVERRIDE, &fx);
658     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
659
660     IDirectDrawSurface_Release(lpSrc);
661     IDirectDrawSurface_Release(lpDst);
662 }
663
664 static void QueryInterface(void)
665 {
666     LPDIRECTDRAWSURFACE dsurface;
667     DDSURFACEDESC surface;
668     LPVOID object;
669     HRESULT ret;
670
671     /* Create a surface */
672     ZeroMemory(&surface, sizeof(surface));
673     surface.dwSize = sizeof(surface);
674     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
675     surface.dwHeight = 10;
676     surface.dwWidth = 10;
677     ret = IDirectDraw_CreateSurface(lpDD, &surface, &dsurface, NULL);
678     if(ret != DD_OK)
679     {
680         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
681         return;
682     }
683
684     /* Call IUnknown::QueryInterface */
685     ret = IDirectDrawSurface_QueryInterface(dsurface, 0, &object);
686     ok(ret == DDERR_INVALIDPARAMS, "IDirectDrawSurface::QueryInterface returned %x\n", ret);
687
688     IDirectDrawSurface_Release(dsurface);
689 }
690
691 /* The following tests test which interface is returned by IDirectDrawSurfaceX::GetDDInterface.
692  * It uses refcounts to test that and compares the interface addresses. Partially fits here, and
693  * partially in the refcount test
694  */
695
696 static ULONG getref(IUnknown *iface)
697 {
698     IUnknown_AddRef(iface);
699     return IUnknown_Release(iface);
700 }
701
702 static void GetDDInterface_1(void)
703 {
704     LPDIRECTDRAWSURFACE dsurface;
705     LPDIRECTDRAWSURFACE2 dsurface2;
706     DDSURFACEDESC surface;
707     HRESULT ret;
708     IDirectDraw2 *dd2;
709     IDirectDraw4 *dd4;
710     IDirectDraw7 *dd7;
711     ULONG ref1, ref2, ref4, ref7;
712     void *dd;
713
714     /* Create a surface */
715     ZeroMemory(&surface, sizeof(surface));
716     surface.dwSize = sizeof(surface);
717     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
718     surface.dwHeight = 10;
719     surface.dwWidth = 10;
720     ret = IDirectDraw_CreateSurface(lpDD, &surface, &dsurface, NULL);
721     if(ret != DD_OK)
722     {
723         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
724         return;
725     }
726     ret = IDirectDrawSurface_QueryInterface(dsurface, &IID_IDirectDrawSurface2, (void **) &dsurface2);
727     ok(ret == DD_OK, "IDirectDrawSurface_QueryInterface returned %08x\n", ret);
728     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
729     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
730     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
731     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
732     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
733     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
734
735     ref1 = getref((IUnknown *) lpDD);
736     ok(ref1 == 1, "IDirectDraw refcount is %d\n", ref1);
737     ref2 = getref((IUnknown *) dd2);
738     ok(ref2 == 1, "IDirectDraw2 refcount is %d\n", ref2);
739     ref4 = getref((IUnknown *) dd4);
740     ok(ref4 == 1, "IDirectDraw4 refcount is %d\n", ref4);
741     ref7 = getref((IUnknown *) dd7);
742     ok(ref7 == 1, "IDirectDraw7 refcount is %d\n", ref7);
743
744
745     ret = IDirectDrawSurface2_GetDDInterface(dsurface2, &dd);
746     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
747     ok(getref((IUnknown *) lpDD) == ref1 + 1, "IDirectDraw refcount was increased by %d\n", getref((IUnknown *) lpDD) - ref1);
748     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %d\n", getref((IUnknown *) dd2) - ref2);
749     ok(getref((IUnknown *) dd4) == ref4 + 0, "IDirectDraw4 refcount was increased by %d\n", getref((IUnknown *) dd4) - ref4);
750     ok(getref((IUnknown *) dd7) == ref7 + 0, "IDirectDraw7 refcount was increased by %d\n", getref((IUnknown *) dd7) - ref7);
751
752     ok(dd == lpDD, "Returned interface pointer is not equal to the creation interface\n");
753     IUnknown_Release((IUnknown *) dd);
754
755     /* try a NULL pointer */
756     ret = IDirectDrawSurface2_GetDDInterface(dsurface2, NULL);
757     ok(ret == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
758
759     IDirectDraw_Release(dd2);
760     IDirectDraw_Release(dd4);
761     IDirectDraw_Release(dd7);
762     IDirectDrawSurface2_Release(dsurface2);
763     IDirectDrawSurface_Release(dsurface);
764 }
765
766 static void GetDDInterface_2(void)
767 {
768     LPDIRECTDRAWSURFACE dsurface;
769     LPDIRECTDRAWSURFACE2 dsurface2;
770     DDSURFACEDESC surface;
771     HRESULT ret;
772     IDirectDraw2 *dd2;
773     IDirectDraw4 *dd4;
774     IDirectDraw7 *dd7;
775     ULONG ref1, ref2, ref4, ref7;
776     void *dd;
777
778     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
779     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
780     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
781     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
782     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
783     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
784
785     /* Create a surface */
786     ZeroMemory(&surface, sizeof(surface));
787     surface.dwSize = sizeof(surface);
788     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
789     surface.dwHeight = 10;
790     surface.dwWidth = 10;
791     ret = IDirectDraw2_CreateSurface(dd2, &surface, &dsurface, NULL);
792     if(ret != DD_OK)
793     {
794         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
795         return;
796     }
797     ret = IDirectDrawSurface_QueryInterface(dsurface, &IID_IDirectDrawSurface2, (void **) &dsurface2);
798     ok(ret == DD_OK, "IDirectDrawSurface_QueryInterface returned %08x\n", ret);
799
800     ref1 = getref((IUnknown *) lpDD);
801     ok(ref1 == 1, "IDirectDraw refcount is %d\n", ref1);
802     ref2 = getref((IUnknown *) dd2);
803     ok(ref2 == 1, "IDirectDraw2 refcount is %d\n", ref2);
804     ref4 = getref((IUnknown *) dd4);
805     ok(ref4 == 1, "IDirectDraw4 refcount is %d\n", ref4);
806     ref7 = getref((IUnknown *) dd7);
807     ok(ref7 == 1, "IDirectDraw7 refcount is %d\n", ref7);
808
809
810     ret = IDirectDrawSurface2_GetDDInterface(dsurface2, &dd);
811     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
812     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %d\n", getref((IUnknown *) lpDD) - ref1);
813     ok(getref((IUnknown *) dd2) == ref2 + 1, "IDirectDraw2 refcount was increased by %d\n", getref((IUnknown *) dd2) - ref2);
814     ok(getref((IUnknown *) dd4) == ref4 + 0, "IDirectDraw4 refcount was increased by %d\n", getref((IUnknown *) dd4) - ref4);
815     ok(getref((IUnknown *) dd7) == ref7 + 0, "IDirectDraw7 refcount was increased by %d\n", getref((IUnknown *) dd7) - ref7);
816
817     ok(dd == dd2, "Returned interface pointer is not equal to the creation interface\n");
818     IUnknown_Release((IUnknown *) dd);
819
820     IDirectDraw_Release(dd2);
821     IDirectDraw_Release(dd4);
822     IDirectDraw_Release(dd7);
823     IDirectDrawSurface2_Release(dsurface2);
824     IDirectDrawSurface_Release(dsurface);
825 }
826
827 static void GetDDInterface_4(void)
828 {
829     LPDIRECTDRAWSURFACE2 dsurface2;
830     LPDIRECTDRAWSURFACE4 dsurface4;
831     DDSURFACEDESC2 surface;
832     HRESULT ret;
833     IDirectDraw2 *dd2;
834     IDirectDraw4 *dd4;
835     IDirectDraw7 *dd7;
836     ULONG ref1, ref2, ref4, ref7;
837     void *dd;
838
839     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
840     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
841     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
842     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
843     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
844     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
845
846     /* Create a surface */
847     ZeroMemory(&surface, sizeof(surface));
848     surface.dwSize = sizeof(surface);
849     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
850     surface.dwHeight = 10;
851     surface.dwWidth = 10;
852     ret = IDirectDraw4_CreateSurface(dd4, &surface, &dsurface4, NULL);
853     if(ret != DD_OK)
854     {
855         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
856         return;
857     }
858     ret = IDirectDrawSurface4_QueryInterface(dsurface4, &IID_IDirectDrawSurface2, (void **) &dsurface2);
859     ok(ret == DD_OK, "IDirectDrawSurface_QueryInterface returned %08x\n", ret);
860
861     ref1 = getref((IUnknown *) lpDD);
862     ok(ref1 == 1, "IDirectDraw refcount is %d\n", ref1);
863     ref2 = getref((IUnknown *) dd2);
864     ok(ref2 == 1, "IDirectDraw2 refcount is %d\n", ref2);
865     ref4 = getref((IUnknown *) dd4);
866     ok(ref4 == 2, "IDirectDraw4 refcount is %d\n", ref4);
867     ref7 = getref((IUnknown *) dd7);
868     ok(ref7 == 1, "IDirectDraw7 refcount is %d\n", ref7);
869
870     ret = IDirectDrawSurface4_GetDDInterface(dsurface4, &dd);
871     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
872     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %d\n", getref((IUnknown *) lpDD) - ref1);
873     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %d\n", getref((IUnknown *) dd2) - ref2);
874     ok(getref((IUnknown *) dd4) == ref4 + 1, "IDirectDraw4 refcount was increased by %d\n", getref((IUnknown *) dd4) - ref4);
875     ok(getref((IUnknown *) dd7) == ref7 + 0, "IDirectDraw7 refcount was increased by %d\n", getref((IUnknown *) dd7) - ref7);
876
877     ok(dd == dd4, "Returned interface pointer is not equal to the creation interface\n");
878     IUnknown_Release((IUnknown *) dd);
879
880     /* Now test what happens if we QI the surface for some other version - It should still return the creation interface */
881     ret = IDirectDrawSurface2_GetDDInterface(dsurface2, &dd);
882     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
883     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %d\n", getref((IUnknown *) lpDD) - ref1);
884     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %d\n", getref((IUnknown *) dd2) - ref2);
885     ok(getref((IUnknown *) dd4) == ref4 + 1, "IDirectDraw4 refcount was increased by %d\n", getref((IUnknown *) dd4) - ref4);
886     ok(getref((IUnknown *) dd7) == ref7 + 0, "IDirectDraw7 refcount was increased by %d\n", getref((IUnknown *) dd7) - ref7);
887
888     ok(dd == dd4, "Returned interface pointer is not equal to the creation interface\n");
889     IUnknown_Release((IUnknown *) dd);
890
891     IDirectDraw_Release(dd2);
892     IDirectDraw_Release(dd4);
893     IDirectDraw_Release(dd7);
894     IDirectDrawSurface4_Release(dsurface4);
895     IDirectDrawSurface2_Release(dsurface2);
896 }
897
898 static void GetDDInterface_7(void)
899 {
900     LPDIRECTDRAWSURFACE4 dsurface4;
901     LPDIRECTDRAWSURFACE7 dsurface7;
902     DDSURFACEDESC2 surface;
903     HRESULT ret;
904     IDirectDraw2 *dd2;
905     IDirectDraw4 *dd4;
906     IDirectDraw7 *dd7;
907     ULONG ref1, ref2, ref4, ref7;
908     void *dd;
909
910     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
911     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
912     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
913     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
914     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
915     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
916
917     /* Create a surface */
918     ZeroMemory(&surface, sizeof(surface));
919     surface.dwSize = sizeof(surface);
920     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
921     surface.dwHeight = 10;
922     surface.dwWidth = 10;
923     ret = IDirectDraw7_CreateSurface(dd7, &surface, &dsurface7, NULL);
924     if(ret != DD_OK)
925     {
926         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
927         return;
928     }
929     ret = IDirectDrawSurface7_QueryInterface(dsurface7, &IID_IDirectDrawSurface4, (void **) &dsurface4);
930     ok(ret == DD_OK, "IDirectDrawSurface_QueryInterface returned %08x\n", ret);
931
932     ref1 = getref((IUnknown *) lpDD);
933     ok(ref1 == 1, "IDirectDraw refcount is %d\n", ref1);
934     ref2 = getref((IUnknown *) dd2);
935     ok(ref2 == 1, "IDirectDraw2 refcount is %d\n", ref2);
936     ref4 = getref((IUnknown *) dd4);
937     ok(ref4 == 1, "IDirectDraw4 refcount is %d\n", ref4);
938     ref7 = getref((IUnknown *) dd7);
939     ok(ref7 == 2, "IDirectDraw7 refcount is %d\n", ref7);
940
941     ret = IDirectDrawSurface7_GetDDInterface(dsurface7, &dd);
942     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
943     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %d\n", getref((IUnknown *) lpDD) - ref1);
944     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %d\n", getref((IUnknown *) dd2) - ref2);
945     ok(getref((IUnknown *) dd4) == ref4 + 0, "IDirectDraw4 refcount was increased by %d\n", getref((IUnknown *) dd4) - ref4);
946     ok(getref((IUnknown *) dd7) == ref7 + 1, "IDirectDraw7 refcount was increased by %d\n", getref((IUnknown *) dd7) - ref7);
947
948     ok(dd == dd7, "Returned interface pointer is not equal to the creation interface\n");
949     IUnknown_Release((IUnknown *) dd);
950
951     /* Now test what happens if we QI the surface for some other version - It should still return the creation interface */
952     ret = IDirectDrawSurface4_GetDDInterface(dsurface4, &dd);
953     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
954     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %d\n", getref((IUnknown *) lpDD) - ref1);
955     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %d\n", getref((IUnknown *) dd2) - ref2);
956     ok(getref((IUnknown *) dd4) == ref4 + 0, "IDirectDraw4 refcount was increased by %d\n", getref((IUnknown *) dd4) - ref4);
957     ok(getref((IUnknown *) dd7) == ref7 + 1, "IDirectDraw7 refcount was increased by %d\n", getref((IUnknown *) dd7) - ref7);
958
959     ok(dd == dd7, "Returned interface pointer is not equal to the creation interface\n");
960     IUnknown_Release((IUnknown *) dd);
961
962     IDirectDraw_Release(dd2);
963     IDirectDraw_Release(dd4);
964     IDirectDraw_Release(dd7);
965     IDirectDrawSurface4_Release(dsurface4);
966     IDirectDrawSurface7_Release(dsurface7);
967 }
968
969 #define MAXEXPECTED 8  /* Can match up to 8 expected surfaces */
970 struct enumstruct
971 {
972     IDirectDrawSurface *expected[MAXEXPECTED];
973     UINT count;
974 };
975
976 static HRESULT WINAPI enumCB(IDirectDrawSurface *surf, DDSURFACEDESC *desc, void *ctx)
977 {
978     int i;
979     BOOL found = FALSE;
980
981     for(i = 0; i < MAXEXPECTED; i++)
982     {
983         if(((struct enumstruct *)ctx)->expected[i] == surf) found = TRUE;
984     }
985
986     ok(found, "Unexpected surface %p enumerated\n", surf);
987     ((struct enumstruct *)ctx)->count++;
988     IDirectDrawSurface_Release(surf);
989     return DDENUMRET_OK;
990 }
991
992 static void EnumTest(void)
993 {
994     HRESULT rc;
995     DDSURFACEDESC ddsd;
996     IDirectDrawSurface *surface;
997     struct enumstruct ctx;
998
999     ddsd.dwSize = sizeof(ddsd);
1000     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
1001     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1002     U2(ddsd).dwMipMapCount = 3;
1003     ddsd.dwWidth = 32;
1004     ddsd.dwHeight = 32;
1005     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface, NULL);
1006     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
1007
1008     memset(&ctx, 0, sizeof(ctx));
1009     ctx.expected[0] = surface;
1010     rc = IDirectDrawSurface_GetAttachedSurface(ctx.expected[0], &ddsd.ddsCaps, &ctx.expected[1]);
1011     ok(rc == DD_OK, "GetAttachedSurface returned %08x\n", rc);
1012     rc = IDirectDrawSurface_GetAttachedSurface(ctx.expected[1], &ddsd.ddsCaps, &ctx.expected[2]);
1013     ok(rc == DD_OK, "GetAttachedSurface returned %08x\n", rc);
1014     rc = IDirectDrawSurface_GetAttachedSurface(ctx.expected[2], &ddsd.ddsCaps, &ctx.expected[3]);
1015     ok(rc == DDERR_NOTFOUND, "GetAttachedSurface returned %08x\n", rc);
1016     ok(!ctx.expected[3], "expected NULL pointer\n");
1017     ctx.count = 0;
1018
1019     rc = IDirectDraw_EnumSurfaces(lpDD, DDENUMSURFACES_DOESEXIST | DDENUMSURFACES_ALL, &ddsd, &ctx, enumCB);
1020     ok(rc == DD_OK, "IDirectDraw_EnumSurfaces returned %08x\n", rc);
1021     ok(ctx.count == 3, "%d surfaces enumerated, expected 3\n", ctx.count);
1022
1023     IDirectDrawSurface_Release(ctx.expected[2]);
1024     IDirectDrawSurface_Release(ctx.expected[1]);
1025     IDirectDrawSurface_Release(surface);
1026 }
1027
1028 static HRESULT WINAPI SurfaceCounter(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
1029 {
1030     UINT *num = context;
1031     (*num)++;
1032     IDirectDrawSurface_Release(surface);
1033     return DDENUMRET_OK;
1034 }
1035
1036 static void AttachmentTest7(void)
1037 {
1038     HRESULT hr;
1039     IDirectDraw7 *dd7;
1040     IDirectDrawSurface7 *surface1, *surface2, *surface3, *surface4;
1041     DDSURFACEDESC2 ddsd;
1042     UINT num;
1043     DDSCAPS2 caps = {DDSCAPS_TEXTURE, 0, 0, 0};
1044     HWND window = CreateWindow( "static", "ddraw_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1045
1046     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
1047     ok(hr == DD_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
1048
1049     memset(&ddsd, 0, sizeof(ddsd));
1050     ddsd.dwSize = sizeof(ddsd);
1051     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
1052     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1053     U2(ddsd).dwMipMapCount = 3; /* Will create 128x128, 64x64, 32x32 */
1054     ddsd.dwWidth = 128;
1055     ddsd.dwHeight = 128;
1056     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface1, NULL);
1057     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1058
1059     /* ROOT */
1060     num = 0;
1061     IDirectDrawSurface7_EnumAttachedSurfaces(surface1, &num, SurfaceCounter);
1062     ok(num == 1, "Mipmap root has %d surfaces attached, expected 1\n", num);
1063     /* DONE ROOT */
1064
1065     /* LEVEL 1 */
1066     hr = IDirectDrawSurface7_GetAttachedSurface(surface1, &caps, &surface2);
1067     ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
1068     num = 0;
1069     IDirectDrawSurface7_EnumAttachedSurfaces(surface2, &num, SurfaceCounter);
1070     ok(num == 1, "First mip level has %d surfaces attached, expected 1\n", num);
1071     /* DONE LEVEL 1 */
1072
1073     /* LEVEL 2 */
1074     hr = IDirectDrawSurface7_GetAttachedSurface(surface2, &caps, &surface3);
1075     ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
1076     IDirectDrawSurface7_Release(surface2);
1077     num = 0;
1078     IDirectDrawSurface7_EnumAttachedSurfaces(surface3, &num, SurfaceCounter);
1079     ok(num == 0, "Second mip level has %d surfaces attached, expected 1\n", num);
1080     /* Done level 2 */
1081     /* Mip level 3 is still needed */
1082     hr = IDirectDrawSurface7_GetAttachedSurface(surface3, &caps, &surface4);
1083     ok(hr == DDERR_NOTFOUND, "GetAttachedSurface returned %08x\n", hr);
1084     ok(!surface4, "expected NULL pointer\n");
1085
1086     /* Try to attach a 16x16 miplevel - Should not work as far I can see */
1087     memset(&ddsd, 0, sizeof(ddsd));
1088     ddsd.dwSize = sizeof(ddsd);
1089     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1090     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1091     ddsd.dwWidth = 16;
1092     ddsd.dwHeight = 16;
1093     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface2, NULL);
1094     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1095
1096     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface2);
1097     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 128x128 texture root returned %08x\n", hr);
1098     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface1);
1099     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 128x128 texture root to a 16x16 texture returned %08x\n", hr);
1100     hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface2);
1101     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 32x32 texture mip level returned %08x\n", hr);
1102     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface3);
1103     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 32x32 texture mip level to a 16x16 surface returned %08x\n", hr);
1104
1105     IDirectDrawSurface7_Release(surface2);
1106
1107     memset(&ddsd, 0, sizeof(ddsd));
1108     ddsd.dwSize = sizeof(ddsd);
1109     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1110     ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
1111     ddsd.dwWidth = 16;
1112     ddsd.dwHeight = 16;
1113     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface2, NULL);
1114     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1115
1116     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface2);
1117     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 128x128 texture root returned %08x\n", hr);
1118     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface1);
1119     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 128x128 texture root to a 16x16 offscreen plain surface returned %08x\n", hr);
1120     hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface2);
1121     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 32x32 texture mip level returned %08x\n", hr);
1122     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface3);
1123     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 32x32 texture mip level to a 16x16 offscreen plain surface returned %08x\n", hr);
1124
1125     IDirectDrawSurface7_Release(surface3);
1126     IDirectDrawSurface7_Release(surface2);
1127     IDirectDrawSurface7_Release(surface1);
1128
1129     hr = IDirectDraw7_SetCooperativeLevel(dd7, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1130     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
1131
1132     memset(&ddsd, 0, sizeof(ddsd));
1133     ddsd.dwSize = sizeof(ddsd);
1134     ddsd.dwFlags = DDSD_BACKBUFFERCOUNT | DDSD_CAPS;
1135     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
1136     ddsd.dwBackBufferCount = 2;
1137     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface1, NULL);
1138     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1139
1140     num = 0;
1141     IDirectDrawSurface7_EnumAttachedSurfaces(surface1, &num, SurfaceCounter);
1142     ok(num == 1, "Primary surface has %d surfaces attached, expected 1\n", num);
1143     IDirectDrawSurface7_Release(surface1);
1144
1145     /* Those are some invalid descriptions, no need to test attachments with them */
1146     memset(&ddsd, 0, sizeof(ddsd));
1147     ddsd.dwSize = sizeof(ddsd);
1148     ddsd.dwFlags = DDSD_CAPS;
1149     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER;
1150     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface1, NULL);
1151     ok(hr==DDERR_INVALIDCAPS,"CreateSurface returned: %x\n",hr);
1152     memset(&ddsd, 0, sizeof(ddsd));
1153     ddsd.dwSize = sizeof(ddsd);
1154     ddsd.dwFlags = DDSD_CAPS;
1155     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER;
1156     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface2, NULL);
1157     ok(hr==DDERR_INVALIDCAPS,"CreateSurface returned: %x\n",hr);
1158
1159     /* Try a single primary and two offscreen plain surfaces */
1160     memset(&ddsd, 0, sizeof(ddsd));
1161     ddsd.dwSize = sizeof(ddsd);
1162     ddsd.dwFlags = DDSD_CAPS;
1163     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
1164     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface1, NULL);
1165     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1166
1167     memset(&ddsd, 0, sizeof(ddsd));
1168     ddsd.dwSize = sizeof(ddsd);
1169     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1170     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1171     ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
1172     ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
1173     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface2, NULL);
1174     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1175
1176     memset(&ddsd, 0, sizeof(ddsd));
1177     ddsd.dwSize = sizeof(ddsd);
1178     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1179     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1180     ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
1181     ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
1182     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface3, NULL);
1183     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1184
1185     /* This one has a different size */
1186     memset(&ddsd, 0, sizeof(ddsd));
1187     ddsd.dwSize = sizeof(ddsd);
1188     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1189     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1190     ddsd.dwWidth = 128;
1191     ddsd.dwHeight = 128;
1192     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface4, NULL);
1193     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1194
1195     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface2);
1196     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching an offscreen plain surface to a front buffer returned %08x\n", hr);
1197     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface1);
1198     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a front buffer to an offscreen plain surface returned %08x\n", hr);
1199     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface3);
1200     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching an offscreen plain surface to another offscreen plain surface returned %08x\n", hr);
1201     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface4);
1202     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching an offscreen plain surface to a front buffer of different size returned %08x\n", hr);
1203     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface1);
1204     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a front buffer to an offscreen plain surface of different size returned %08x\n", hr);
1205
1206     IDirectDrawSurface7_Release(surface4);
1207     IDirectDrawSurface7_Release(surface3);
1208     IDirectDrawSurface7_Release(surface2);
1209     IDirectDrawSurface7_Release(surface1);
1210
1211     hr =IDirectDraw7_SetCooperativeLevel(dd7, NULL, DDSCL_NORMAL);
1212     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
1213     IDirectDraw7_Release(dd7);
1214 }
1215
1216 static void AttachmentTest(void)
1217 {
1218     HRESULT hr;
1219     IDirectDrawSurface *surface1, *surface2, *surface3, *surface4;
1220     DDSURFACEDESC ddsd;
1221     DDSCAPS caps = {DDSCAPS_TEXTURE};
1222     BOOL refrast = FALSE;
1223     HWND window = CreateWindow( "static", "ddraw_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1224
1225     memset(&ddsd, 0, sizeof(ddsd));
1226     ddsd.dwSize = sizeof(ddsd);
1227     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
1228     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1229     U2(ddsd).dwMipMapCount = 3; /* Will create 128x128, 64x64, 32x32 */
1230     ddsd.dwWidth = 128;
1231     ddsd.dwHeight = 128;
1232     hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &surface1, NULL);
1233     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1234
1235     hr = IDirectDrawSurface7_GetAttachedSurface(surface1, &caps, &surface2);
1236     ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
1237     hr = IDirectDrawSurface7_GetAttachedSurface(surface2, &caps, &surface3);
1238     ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
1239
1240     /* Try to attach a 16x16 miplevel - Should not work as far I can see */
1241     memset(&ddsd, 0, sizeof(ddsd));
1242     ddsd.dwSize = sizeof(ddsd);
1243     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1244     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1245     ddsd.dwWidth = 16;
1246     ddsd.dwHeight = 16;
1247     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface4, NULL);
1248     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1249
1250     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface4);
1251     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 128x128 texture root returned %08x\n", hr);
1252     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface1);
1253     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 128x128 texture root to a 16x16 texture returned %08x\n", hr);
1254     hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface4);
1255     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 32x32 texture mip level returned %08x\n", hr);
1256     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface3);
1257     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 32x32 texture mip level to a 16x16 surface returned %08x\n", hr);
1258     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface4);
1259     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 64x64 texture sublevel returned %08x\n", hr);
1260     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface2);
1261     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 64x64 texture sublevel to a 16x16 texture returned %08x\n", hr);
1262
1263     IDirectDrawSurface7_Release(surface4);
1264
1265     memset(&ddsd, 0, sizeof(ddsd));
1266     ddsd.dwSize = sizeof(ddsd);
1267     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1268     ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
1269     ddsd.dwWidth = 16;
1270     ddsd.dwHeight = 16;
1271     hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &surface4, NULL);
1272     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1273
1274     if (SUCCEEDED(IDirectDrawSurface7_AddAttachedSurface(surface1, surface4)))
1275     {
1276         IDirectDrawSurface7_DeleteAttachedSurface(surface1, 0, surface4);
1277         refrast = TRUE;
1278     }
1279
1280     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface4); /* Succeeds on refrast */
1281     if (refrast)
1282         ok(hr == S_OK, "Attaching a 16x16 offscreen plain surface to a 128x128 texture root returned %08x\n", hr);
1283     else
1284         ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 128x128 texture root returned %08x\n", hr);
1285     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface1, 0, surface4);
1286
1287     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface1);  /* Succeeds on refrast */
1288     if (refrast)
1289         ok(hr == S_OK, "Attaching a 128x128 texture root to a 16x16 offscreen plain surface returned %08x\n", hr);
1290     else
1291         ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 128x128 texture root to a 16x16 offscreen plain surface returned %08x\n", hr);
1292     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface1, 0, surface1);
1293
1294     hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface4);  /* Succeeds on refrast */
1295     if (refrast)
1296         ok(hr == S_OK, "Attaching a 16x16 offscreen plain surface to a 32x32 texture mip level returned %08x\n", hr);
1297     else
1298         ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 32x32 texture mip level returned %08x\n", hr);
1299     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface3, 0, surface4);
1300
1301     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface3);
1302     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 32x32 texture mip level to a 16x16 offscreen plain surface returned %08x\n", hr);
1303     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface4, 0, surface3);
1304
1305     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface4);  /* Succeeds on refrast */
1306     if (refrast)
1307         ok(hr == S_OK, "Attaching a 16x16 offscreen plain surface to a 64x64 texture sublevel returned %08x\n", hr);
1308     else
1309         ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 64x64 texture sublevel returned %08x\n", hr);
1310     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface2, 0, surface4);
1311
1312     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface2);
1313     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 64x64 texture sublevel to a 16x16 offscreen plain surface returned %08x\n", hr);
1314     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface4, 0, surface2);
1315
1316     IDirectDrawSurface7_Release(surface4);
1317     IDirectDrawSurface7_Release(surface3);
1318     IDirectDrawSurface7_Release(surface2);
1319     IDirectDrawSurface7_Release(surface1);
1320
1321     hr = IDirectDraw_SetCooperativeLevel(lpDD, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1322     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
1323
1324     /* Creating a back buffer as-is, is not allowed. No need to perform attachment tests */
1325     memset(&ddsd, 0, sizeof(ddsd));
1326     ddsd.dwSize = sizeof(ddsd);
1327     ddsd.dwFlags = DDSD_CAPS;
1328     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER;
1329     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface2, NULL);
1330     ok(hr==DDERR_INVALIDCAPS,"CreateSurface returned: %x\n",hr);
1331     /* This old ddraw version happily creates explicit front buffers */
1332     memset(&ddsd, 0, sizeof(ddsd));
1333     ddsd.dwSize = sizeof(ddsd);
1334     ddsd.dwFlags = DDSD_CAPS;
1335     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER;
1336     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface1, NULL);
1337     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1338     IDirectDrawSurface_Release(surface1);
1339
1340     /* Try a single primary and two offscreen plain surfaces */
1341     memset(&ddsd, 0, sizeof(ddsd));
1342     ddsd.dwSize = sizeof(ddsd);
1343     ddsd.dwFlags = DDSD_CAPS;
1344     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
1345     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface1, NULL);
1346     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1347
1348     memset(&ddsd, 0, sizeof(ddsd));
1349     ddsd.dwSize = sizeof(ddsd);
1350     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1351     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1352     ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
1353     ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
1354     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface2, NULL);
1355     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1356
1357     memset(&ddsd, 0, sizeof(ddsd));
1358     ddsd.dwSize = sizeof(ddsd);
1359     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1360     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1361     ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
1362     ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
1363     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface3, NULL);
1364     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1365
1366     /* This one has a different size */
1367     memset(&ddsd, 0, sizeof(ddsd));
1368     ddsd.dwSize = sizeof(ddsd);
1369     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1370     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1371     ddsd.dwWidth = 128;
1372     ddsd.dwHeight = 128;
1373     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface4, NULL);
1374     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1375
1376     hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
1377     ok(hr == DD_OK, "Attaching an offscreen plain surface to a front buffer returned %08x\n", hr);
1378     /* Try the reverse without detaching first */
1379     hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
1380     ok(hr == DDERR_SURFACEALREADYATTACHED, "Attaching an attached surface to its attachee returned %08x\n", hr);
1381     hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
1382     ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
1383     hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
1384     ok(hr == DD_OK, "Attaching a front buffer to an offscreen plain surface returned %08x\n", hr);
1385     /* Try to detach reversed */
1386     hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
1387     ok(hr == DDERR_CANNOTDETACHSURFACE, "DeleteAttachedSurface returned %08x\n", hr);
1388     /* Now the proper detach */
1389     hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface1);
1390     ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
1391     hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface3); /* Fails on refrast */
1392     ok(hr == DD_OK || broken(hr == DDERR_CANNOTATTACHSURFACE),
1393        "Attaching an offscreen plain surface to another offscreen plain surface returned %08x\n", hr);
1394     if(SUCCEEDED(hr))
1395     {
1396         hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface3);
1397         ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
1398     }
1399     hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4);
1400     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching an offscreen plain surface to a front buffer of different size returned %08x\n", hr);
1401     hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
1402     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a front buffer to an offscreen plain surface of different size returned %08x\n", hr);
1403
1404     IDirectDrawSurface_Release(surface4);
1405     IDirectDrawSurface_Release(surface3);
1406     IDirectDrawSurface_Release(surface2);
1407     IDirectDrawSurface_Release(surface1);
1408
1409     hr =IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
1410     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
1411
1412     DestroyWindow(window);
1413 }
1414
1415 struct compare
1416 {
1417     DWORD width, height;
1418     DWORD caps, caps2;
1419     UINT mips;
1420 };
1421
1422 static HRESULT WINAPI CubeTestLvl2Enum(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
1423 {
1424     UINT *mips = context;
1425
1426     (*mips)++;
1427     IDirectDrawSurface7_EnumAttachedSurfaces(surface,
1428                                              context,
1429                                              CubeTestLvl2Enum);
1430
1431     return DDENUMRET_OK;
1432 }
1433
1434 static HRESULT WINAPI CubeTestLvl1Enum(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
1435 {
1436     UINT mips = 0;
1437     UINT *num = context;
1438     static const struct compare expected[] =
1439     {
1440         {
1441             128, 128,
1442             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1443             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ,
1444             7
1445         },
1446         {
1447             128, 128,
1448             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1449             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ,
1450             7
1451         },
1452         {
1453             128, 128,
1454             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1455             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY,
1456             7
1457         },
1458         {
1459             128, 128,
1460             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1461             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY,
1462             7
1463         },
1464         {
1465             128, 128,
1466             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1467             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX,
1468             7
1469         },
1470         {
1471             64, 64, /* This is the first mipmap */
1472             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1473             DDSCAPS2_MIPMAPSUBLEVEL | DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX,
1474             6
1475         },
1476     };
1477
1478     mips = 0;
1479     IDirectDrawSurface7_EnumAttachedSurfaces(surface,
1480                                              &mips,
1481                                              CubeTestLvl2Enum);
1482
1483     ok(desc->dwWidth == expected[*num].width, "Surface width is %d expected %d\n", desc->dwWidth, expected[*num].width);
1484     ok(desc->dwHeight == expected[*num].height, "Surface height is %d expected %d\n", desc->dwHeight, expected[*num].height);
1485     ok(desc->ddsCaps.dwCaps == expected[*num].caps, "Surface caps are %08x expected %08x\n", desc->ddsCaps.dwCaps, expected[*num].caps);
1486     ok(desc->ddsCaps.dwCaps2 == expected[*num].caps2, "Surface caps2 are %08x expected %08x\n", desc->ddsCaps.dwCaps2, expected[*num].caps2);
1487     ok(mips == expected[*num].mips, "Surface has %d mipmaps, expected %d\n", mips, expected[*num].mips);
1488
1489     (*num)++;
1490
1491     IDirectDrawSurface7_Release(surface);
1492
1493     return DDENUMRET_OK;
1494 }
1495
1496 static void CubeMapTest(void)
1497 {
1498     IDirectDraw7 *dd7 = NULL;
1499     IDirectDrawSurface7 *cubemap = NULL;
1500     DDSURFACEDESC2 ddsd;
1501     HRESULT hr;
1502     UINT num = 0;
1503     struct enumstruct ctx;
1504
1505     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
1506     ok(hr == DD_OK, "IDirectDraw::QueryInterface returned %08x\n", hr);
1507     if (FAILED(hr)) goto err;
1508
1509     memset(&ddsd, 0, sizeof(ddsd));
1510     ddsd.dwSize = sizeof(ddsd);
1511     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1512     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1513     ddsd.dwWidth = 128;
1514     ddsd.dwHeight = 128;
1515     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_SYSTEMMEMORY;
1516     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES;
1517
1518     /* D3DFMT_R5G6B5 */
1519     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
1520     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 16;
1521     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0xF800;
1522     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x07E0;
1523     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x001F;
1524
1525     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &cubemap, NULL);
1526     if (FAILED(hr))
1527     {
1528         skip("Can't create cubemap surface\n");
1529         goto err;
1530     }
1531
1532     hr = IDirectDrawSurface7_GetSurfaceDesc(cubemap, &ddsd);
1533     ok(hr == DD_OK, "IDirectDrawSurface7_GetSurfaceDesc returned %08x\n", hr);
1534     ok(ddsd.ddsCaps.dwCaps == (DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX),
1535        "Root Caps are %08x\n", ddsd.ddsCaps.dwCaps);
1536     ok(ddsd.ddsCaps.dwCaps2 == (DDSCAPS2_CUBEMAP_POSITIVEX | DDSCAPS2_CUBEMAP),
1537        "Root Caps2 are %08x\n", ddsd.ddsCaps.dwCaps2);
1538
1539     IDirectDrawSurface7_EnumAttachedSurfaces(cubemap,
1540                                              &num,
1541                                              CubeTestLvl1Enum);
1542     ok(num == 6, "Surface has %d attachments\n", num);
1543     IDirectDrawSurface7_Release(cubemap);
1544
1545     /* What happens if I do not specify any faces? */
1546     memset(&ddsd, 0, sizeof(ddsd));
1547     ddsd.dwSize = sizeof(ddsd);
1548     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1549     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1550     ddsd.dwWidth = 128;
1551     ddsd.dwHeight = 128;
1552     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_SYSTEMMEMORY;
1553     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP;
1554
1555     /* D3DFMT_R5G6B5 */
1556     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
1557     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 16;
1558     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0xF800;
1559     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x07E0;
1560     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x001F;
1561
1562     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &cubemap, NULL);
1563     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw7::CreateSurface asking for a cube map without faces returned %08x\n", hr);
1564
1565     /* Cube map faces without a cube map? */
1566     memset(&ddsd, 0, sizeof(ddsd));
1567     ddsd.dwSize = sizeof(ddsd);
1568     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1569     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1570     ddsd.dwWidth = 128;
1571     ddsd.dwHeight = 128;
1572     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_SYSTEMMEMORY;
1573     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP_ALLFACES;
1574
1575     /* D3DFMT_R5G6B5 */
1576     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
1577     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 16;
1578     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0xF800;
1579     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x07E0;
1580     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x001F;
1581
1582     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &cubemap, NULL);
1583     ok(hr == DDERR_INVALIDCAPS, "IDirectDraw7::CreateSurface returned %08x\n", hr);
1584
1585     memset(&ddsd, 0, sizeof(ddsd));
1586     ddsd.dwSize = sizeof(ddsd);
1587     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1588     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1589     ddsd.dwWidth = 128;
1590     ddsd.dwHeight = 128;
1591     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_SYSTEMMEMORY;
1592     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP_POSITIVEX;
1593
1594     /* D3DFMT_R5G6B5 */
1595     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
1596     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 16;
1597     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0xF800;
1598     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x07E0;
1599     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x001F;
1600
1601     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &cubemap, NULL);
1602     ok(hr == DDERR_INVALIDCAPS, "IDirectDraw7::CreateSurface returned %08x\n", hr);
1603
1604     /* Make sure everything is cleaned up properly. Use the enumSurfaces test infrastructure */
1605     memset(&ctx, 0, sizeof(ctx));
1606     memset(&ddsd, 0, sizeof(ddsd));
1607     ddsd.dwSize = sizeof(DDSURFACEDESC);
1608     hr = IDirectDraw_EnumSurfaces(lpDD, DDENUMSURFACES_DOESEXIST | DDENUMSURFACES_ALL, (DDSURFACEDESC *) &ddsd, (void *) &ctx, enumCB);
1609     ok(hr == DD_OK, "IDirectDraw_EnumSurfaces returned %08x\n", hr);
1610     ok(ctx.count == 0, "%d surfaces enumerated, expected 0\n", ctx.count);
1611
1612     err:
1613     if (dd7) IDirectDraw7_Release(dd7);
1614 }
1615
1616 static void test_lockrect_invalid(void)
1617 {
1618     unsigned int i, j;
1619
1620     RECT valid[] = {
1621         {60, 60, 68, 68},
1622         {60, 60, 60, 68},
1623         {60, 60, 68, 60},
1624         {120, 60, 128, 68},
1625         {60, 120, 68, 128},
1626     };
1627
1628     RECT invalid[] = {
1629         {68, 60, 60, 68},       /* left > right */
1630         {60, 68, 68, 60},       /* top > bottom */
1631         {-8, 60,  0, 68},       /* left < surface */
1632         {60, -8, 68,  0},       /* top < surface */
1633         {-16, 60, -8, 68},      /* right < surface */
1634         {60, -16, 68, -8},      /* bottom < surface */
1635         {60, 60, 136, 68},      /* right > surface */
1636         {60, 60, 68, 136},      /* bottom > surface */
1637         {136, 60, 144, 68},     /* left > surface */
1638         {60, 136, 68, 144},     /* top > surface */
1639     };
1640
1641     const DWORD dds_caps[] = {
1642         DDSCAPS_OFFSCREENPLAIN
1643     };
1644
1645     for (j = 0; j < (sizeof(dds_caps) / sizeof(*dds_caps)); ++j)
1646     {
1647         IDirectDrawSurface *surface = 0;
1648         DDSURFACEDESC surface_desc = {0};
1649         DDSURFACEDESC locked_desc = {0};
1650         HRESULT hr;
1651
1652         surface_desc.dwSize = sizeof(surface_desc);
1653         surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
1654         surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
1655         surface_desc.ddsCaps.dwCaps = dds_caps[j];
1656         surface_desc.dwWidth = 128;
1657         surface_desc.dwHeight = 128;
1658         surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
1659         U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
1660         U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0xFF0000;
1661         U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x00FF00;
1662         U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x0000FF;
1663
1664         hr = IDirectDraw_CreateSurface(lpDD, &surface_desc, &surface, NULL);
1665         ok(SUCCEEDED(hr), "CreateSurface failed (0x%08x)\n", hr);
1666         if (FAILED(hr)) {
1667             skip("failed to create surface\n");
1668             continue;
1669         }
1670
1671         for (i = 0; i < (sizeof(valid) / sizeof(*valid)); ++i)
1672         {
1673             RECT *rect = &valid[i];
1674
1675             memset(&locked_desc, 0, sizeof(locked_desc));
1676             locked_desc.dwSize = sizeof(locked_desc);
1677
1678             hr = IDirectDrawSurface_Lock(surface, rect, &locked_desc, DDLOCK_WAIT, NULL);
1679             ok(SUCCEEDED(hr), "Lock failed (0x%08x) for rect [%d, %d]->[%d, %d]\n",
1680                     hr, rect->left, rect->top, rect->right, rect->bottom);
1681
1682             hr = IDirectDrawSurface_Unlock(surface, NULL);
1683             ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1684         }
1685
1686         for (i = 0; i < (sizeof(invalid) / sizeof(*invalid)); ++i)
1687         {
1688             RECT *rect = &invalid[i];
1689
1690             memset(&locked_desc, 1, sizeof(locked_desc));
1691             locked_desc.dwSize = sizeof(locked_desc);
1692
1693             hr = IDirectDrawSurface_Lock(surface, rect, &locked_desc, DDLOCK_WAIT, NULL);
1694             ok(hr == DDERR_INVALIDPARAMS, "Lock returned 0x%08x for rect [%d, %d]->[%d, %d]"
1695                     ", expected DDERR_INVALIDPARAMS (0x%08x)\n", hr, rect->left, rect->top,
1696                     rect->right, rect->bottom, DDERR_INVALIDPARAMS);
1697             ok(!locked_desc.lpSurface, "IDirectDrawSurface_Lock did not set lpSurface in the surface desc to zero.\n");
1698         }
1699
1700         hr = IDirectDrawSurface_Lock(surface, NULL, &locked_desc, DDLOCK_WAIT, NULL);
1701         ok(hr == DD_OK, "IDirectDrawSurface_Lock(rect = NULL) failed (0x%08x)\n", hr);
1702         hr = IDirectDrawSurface_Lock(surface, NULL, &locked_desc, DDLOCK_WAIT, NULL);
1703         ok(hr == DDERR_SURFACEBUSY, "Double lock(rect = NULL) returned 0x%08x\n", hr);
1704         if(SUCCEEDED(hr)) {
1705             hr = IDirectDrawSurface_Unlock(surface, NULL);
1706             ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1707         }
1708         hr = IDirectDrawSurface_Unlock(surface, NULL);
1709         ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1710
1711         memset(&locked_desc, 0, sizeof(locked_desc));
1712         locked_desc.dwSize = sizeof(locked_desc);
1713         hr = IDirectDrawSurface_Lock(surface, &valid[0], &locked_desc, DDLOCK_WAIT, NULL);
1714         ok(hr == DD_OK, "IDirectDrawSurface_Lock(rect = [%d, %d]->[%d, %d]) failed (0x%08x)\n",
1715            valid[0].left, valid[0].top, valid[0].right, valid[0].bottom, hr);
1716         hr = IDirectDrawSurface_Lock(surface, &valid[0], &locked_desc, DDLOCK_WAIT, NULL);
1717         ok(hr == DDERR_SURFACEBUSY, "Double lock(rect = [%d, %d]->[%d, %d]) failed (0x%08x)\n",
1718            valid[0].left, valid[0].top, valid[0].right, valid[0].bottom, hr);
1719
1720         /* Locking a different rectangle returns DD_OK, but it seems to break the surface.
1721          * Afterwards unlocking the surface fails(NULL rectangle, and both locked rectangles
1722          */
1723
1724         hr = IDirectDrawSurface_Unlock(surface, NULL);
1725         ok(hr == DD_OK, "Unlock returned (0x%08x)\n", hr);
1726
1727         IDirectDrawSurface_Release(surface);
1728     }
1729 }
1730
1731 static void CompressedTest(void)
1732 {
1733     HRESULT hr;
1734     IDirectDrawSurface7 *surface;
1735     DDSURFACEDESC2 ddsd, ddsd2;
1736     IDirectDraw7 *dd7 = NULL;
1737     RECT r = { 0, 0, 128, 128 };
1738     RECT r2 = { 32, 32, 64, 64 };
1739
1740     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
1741     ok(hr == DD_OK, "IDirectDraw::QueryInterface returned %08x\n", hr);
1742
1743     memset(&ddsd, 0, sizeof(ddsd));
1744     ddsd.dwSize = sizeof(ddsd);
1745     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1746     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1747     ddsd.dwWidth = 128;
1748     ddsd.dwHeight = 128;
1749     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY;
1750     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_FOURCC;
1751     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','1');
1752
1753     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1754     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
1755     if (FAILED(hr)) {
1756         skip("failed to create surface\n");
1757         return;
1758     }
1759
1760     memset(&ddsd2, 0, sizeof(ddsd2));
1761     ddsd2.dwSize = sizeof(ddsd2);
1762     U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1763     hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1764     ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1765
1766     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1767        "Surface desc flags: %08x\n", ddsd2.dwFlags);
1768     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1769     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1770     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1771        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1772     ok(U1(ddsd2).dwLinearSize == 8192, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1773     ok(ddsd2.ddsCaps.dwCaps2 == 0, "Caps2: %08x\n", ddsd2.ddsCaps.dwCaps2);
1774     IDirectDrawSurface7_Release(surface);
1775
1776     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','3');
1777     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1778     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
1779     if (FAILED(hr)) {
1780         skip("failed to create surface\n");
1781         return;
1782     }
1783
1784     memset(&ddsd2, 0, sizeof(ddsd2));
1785     ddsd2.dwSize = sizeof(ddsd2);
1786     U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1787     hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1788     ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1789
1790     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1791        "Surface desc flags: %08x\n", ddsd2.dwFlags);
1792     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1793     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1794     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1795        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1796     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1797     IDirectDrawSurface7_Release(surface);
1798
1799     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','5');
1800     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1801     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
1802     if (FAILED(hr)) {
1803         skip("failed to create surface\n");
1804         return;
1805     }
1806
1807     memset(&ddsd2, 0, sizeof(ddsd2));
1808     ddsd2.dwSize = sizeof(ddsd2);
1809     U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1810     hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1811     ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1812
1813     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1814        "Surface desc flags: %08x\n", ddsd2.dwFlags);
1815     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1816     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1817     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1818        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1819     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1820     ok(ddsd2.lpSurface == 0, "Surface memory is at %p, expected NULL\n", ddsd2.lpSurface);
1821
1822     memset(&ddsd2, 0, sizeof(ddsd2));
1823     ddsd2.dwSize = sizeof(ddsd2);
1824     U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1825
1826     /* Show that the description is not changed when locking the surface. What is really interesting
1827      * about this is that DDSD_LPSURFACE isn't set.
1828      */
1829     hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd2, DDLOCK_READONLY, 0);
1830     ok(hr == DD_OK, "Lock returned %08x\n", hr);
1831
1832     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1833        "Surface desc flags: %08x\n", ddsd2.dwFlags);
1834     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1835     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1836     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1837        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1838     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1839     ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
1840
1841     hr = IDirectDrawSurface7_Unlock(surface, NULL);
1842     ok(hr == DD_OK, "Unlock returned %08x\n", hr);
1843
1844     /* Now what about a locking rect?  */
1845     hr = IDirectDrawSurface7_Lock(surface, &r, &ddsd2, DDLOCK_READONLY, 0);
1846     ok(hr == DD_OK, "Lock returned %08x\n", hr);
1847
1848     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1849        "Surface desc flags: %08x\n", ddsd2.dwFlags);
1850     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1851     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1852     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1853        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1854     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1855     ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
1856
1857     hr = IDirectDrawSurface7_Unlock(surface, &r);
1858     ok(hr == DD_OK, "Unlock returned %08x\n", hr);
1859
1860     /* Now what about a different locking offset? */
1861     hr = IDirectDrawSurface7_Lock(surface, &r2, &ddsd2, DDLOCK_READONLY, 0);
1862     ok(hr == DD_OK, "Lock returned %08x\n", hr);
1863
1864     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1865        "Surface desc flags: %08x\n", ddsd2.dwFlags);
1866     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1867     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1868     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
1869        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1870     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
1871     ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
1872
1873     hr = IDirectDrawSurface7_Unlock(surface, &r2);
1874     ok(hr == DD_OK, "Unlock returned %08x\n", hr);
1875     IDirectDrawSurface7_Release(surface);
1876
1877     /* Try this with video memory. A kind of surprise. It still has the LINEARSIZE flag set,
1878      * but seems to have a pitch instead.
1879      */
1880     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
1881     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','1');
1882
1883     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1884     ok(hr == DD_OK || hr == DDERR_NOTEXTUREHW || hr == DDERR_INVALIDPARAMS ||
1885        broken(hr == DDERR_NODIRECTDRAWHW), "CreateSurface returned %08x\n", hr);
1886
1887     /* Not supported everywhere */
1888     if(SUCCEEDED(hr))
1889     {
1890         memset(&ddsd2, 0, sizeof(ddsd2));
1891         ddsd2.dwSize = sizeof(ddsd2);
1892         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1893         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1894         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1895
1896         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1897         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1898         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1899         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1900         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
1901         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1902         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
1903         ok(ddsd2.ddsCaps.dwCaps2 == 0, "Caps2: %08x\n", ddsd2.ddsCaps.dwCaps2);
1904         IDirectDrawSurface7_Release(surface);
1905
1906         U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','3');
1907         hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1908         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
1909
1910         memset(&ddsd2, 0, sizeof(ddsd2));
1911         ddsd2.dwSize = sizeof(ddsd2);
1912         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1913         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1914         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1915
1916         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1917         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1918         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1919         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1920         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
1921         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1922         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
1923         IDirectDrawSurface7_Release(surface);
1924
1925         U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','5');
1926         hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
1927         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
1928
1929         memset(&ddsd2, 0, sizeof(ddsd2));
1930         ddsd2.dwSize = sizeof(ddsd2);
1931         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1932         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
1933         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
1934
1935         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1936         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1937         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1938         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1939         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
1940         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1941         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
1942         ok(ddsd2.lpSurface == 0, "Surface memory is at %p, expected NULL\n", ddsd2.lpSurface);
1943
1944         memset(&ddsd2, 0, sizeof(ddsd2));
1945         ddsd2.dwSize = sizeof(ddsd2);
1946         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
1947
1948         /* Show that the description is not changed when locking the surface. What is really interesting
1949         * about this is that DDSD_LPSURFACE isn't set.
1950         */
1951         hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd2, DDLOCK_READONLY, 0);
1952         ok(hr == DD_OK, "Lock returned %08x\n", hr);
1953
1954         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1955         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1956         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1957         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1958         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
1959         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1960         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
1961         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
1962
1963         hr = IDirectDrawSurface7_Unlock(surface, NULL);
1964         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
1965
1966         /* Now what about a locking rect?  */
1967         hr = IDirectDrawSurface7_Lock(surface, &r, &ddsd2, DDLOCK_READONLY, 0);
1968         ok(hr == DD_OK, "Lock returned %08x\n", hr);
1969
1970         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1971         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1972         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1973         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1974         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
1975         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1976         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
1977         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
1978
1979         hr = IDirectDrawSurface7_Unlock(surface, &r);
1980         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
1981
1982         /* Now what about a different locking offset? */
1983         hr = IDirectDrawSurface7_Lock(surface, &r2, &ddsd2, DDLOCK_READONLY, 0);
1984         ok(hr == DD_OK, "Lock returned %08x\n", hr);
1985
1986         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
1987         "Surface desc flags: %08x\n", ddsd2.dwFlags);
1988         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
1989         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
1990         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
1991         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
1992         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
1993         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
1994
1995         hr = IDirectDrawSurface7_Unlock(surface, &r2);
1996         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
1997
1998         IDirectDrawSurface7_Release(surface);
1999     }
2000     else
2001     {
2002         skip("Hardware DXTN textures not supported\n");
2003     }
2004
2005     /* What happens to managed textures? Interestingly, Windows reports them as being in system
2006      * memory. The linear size fits again.
2007      */
2008     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
2009     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE;
2010     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','1');
2011
2012     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
2013     ok(hr == DD_OK || hr == DDERR_NOTEXTUREHW, "CreateSurface returned %08x\n", hr);
2014
2015     /* Not supported everywhere */
2016     if(SUCCEEDED(hr))
2017     {
2018         memset(&ddsd2, 0, sizeof(ddsd2));
2019         ddsd2.dwSize = sizeof(ddsd2);
2020         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2021         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
2022         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
2023
2024         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2025         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2026         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2027         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2028         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2029         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2030         ok(U1(ddsd2).dwLinearSize == 8192, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2031         ok(ddsd2.ddsCaps.dwCaps2 == DDSCAPS2_TEXTUREMANAGE, "Caps2: %08x\n", ddsd2.ddsCaps.dwCaps2);
2032         IDirectDrawSurface7_Release(surface);
2033
2034         U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','3');
2035         hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
2036         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
2037
2038         memset(&ddsd2, 0, sizeof(ddsd2));
2039         ddsd2.dwSize = sizeof(ddsd2);
2040         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2041         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
2042         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
2043
2044         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2045         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2046         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2047         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2048         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2049         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2050         ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2051         IDirectDrawSurface7_Release(surface);
2052
2053         U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','5');
2054         hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
2055         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
2056
2057         memset(&ddsd2, 0, sizeof(ddsd2));
2058         ddsd2.dwSize = sizeof(ddsd2);
2059         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2060         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
2061         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
2062
2063         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2064         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2065         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2066         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2067         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2068         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2069         ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2070         ok(ddsd2.lpSurface == 0, "Surface memory is at %p, expected NULL\n", ddsd2.lpSurface);
2071
2072         memset(&ddsd2, 0, sizeof(ddsd2));
2073         ddsd2.dwSize = sizeof(ddsd2);
2074         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2075
2076         /* Show that the description is not changed when locking the surface. What is really interesting
2077         * about this is that DDSD_LPSURFACE isn't set.
2078         */
2079         hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd2, DDLOCK_READONLY, 0);
2080         ok(hr == DD_OK, "Lock returned %08x\n", hr);
2081
2082         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2083         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2084         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2085         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2086         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2087         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2088         ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2089         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2090
2091         hr = IDirectDrawSurface7_Unlock(surface, NULL);
2092         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2093
2094         /* Now what about a locking rect?  */
2095         hr = IDirectDrawSurface7_Lock(surface, &r, &ddsd2, DDLOCK_READONLY, 0);
2096         ok(hr == DD_OK, "Lock returned %08x\n", hr);
2097
2098         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2099         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2100         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2101         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2102         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2103         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2104         ok(U1(ddsd2).dwLinearSize == 16384, "\"Linear\" size is %d\n", U1(ddsd2).dwLinearSize);
2105         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2106
2107         hr = IDirectDrawSurface7_Unlock(surface, &r);
2108         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2109
2110         /* Now what about a different locking offset? */
2111         hr = IDirectDrawSurface7_Lock(surface, &r2, &ddsd2, DDLOCK_READONLY, 0);
2112         ok(hr == DD_OK, "Lock returned %08x\n", hr);
2113
2114         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2115         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2116         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2117         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2118         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2119         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2120         ok(U1(ddsd2).dwLinearSize == 16384, "\"Linear\" size is %d\n", U1(ddsd2).dwLinearSize);
2121         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2122
2123         hr = IDirectDrawSurface7_Unlock(surface, &r2);
2124         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2125
2126         IDirectDrawSurface7_Release(surface);
2127     }
2128     else
2129     {
2130         skip("Hardware DXTN textures not supported\n");
2131     }
2132
2133     IDirectDraw7_Release(dd7);
2134 }
2135
2136 static void SizeTest(void)
2137 {
2138     LPDIRECTDRAWSURFACE dsurface = NULL;
2139     DDSURFACEDESC desc;
2140     HRESULT ret;
2141     HWND window = CreateWindow( "static", "ddraw_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
2142
2143     /* Create an offscreen surface surface without a size */
2144     ZeroMemory(&desc, sizeof(desc));
2145     desc.dwSize = sizeof(desc);
2146     desc.dwFlags = DDSD_CAPS;
2147     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2148     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2149     ok(ret == DDERR_INVALIDPARAMS, "Creating an offscreen plain surface without a size info returned %08x (dsurface=%p)\n", ret, dsurface);
2150     if(dsurface)
2151     {
2152         trace("Surface at %p\n", dsurface);
2153         IDirectDrawSurface_Release(dsurface);
2154         dsurface = NULL;
2155     }
2156
2157     /* Create an offscreen surface surface with only a width parameter */
2158     ZeroMemory(&desc, sizeof(desc));
2159     desc.dwSize = sizeof(desc);
2160     desc.dwFlags = DDSD_CAPS | DDSD_WIDTH;
2161     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2162     desc.dwWidth = 128;
2163     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2164     ok(ret == DDERR_INVALIDPARAMS, "Creating an offscreen plain surface without hight info returned %08x\n", ret);
2165     if(dsurface)
2166     {
2167         IDirectDrawSurface_Release(dsurface);
2168         dsurface = NULL;
2169     }
2170
2171     /* Create an offscreen surface surface with only a height parameter */
2172     ZeroMemory(&desc, sizeof(desc));
2173     desc.dwSize = sizeof(desc);
2174     desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT;
2175     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2176     desc.dwHeight = 128;
2177     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2178     ok(ret == DDERR_INVALIDPARAMS, "Creating an offscreen plain surface without width info returned %08x\n", ret);
2179     if(dsurface)
2180     {
2181         IDirectDrawSurface_Release(dsurface);
2182         dsurface = NULL;
2183     }
2184
2185     /* Sanity check */
2186     ZeroMemory(&desc, sizeof(desc));
2187     desc.dwSize = sizeof(desc);
2188     desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
2189     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2190     desc.dwHeight = 128;
2191     desc.dwWidth = 128;
2192     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2193     ok(ret == DD_OK, "Creating an offscreen plain surface with width and height info returned %08x\n", ret);
2194     if(dsurface)
2195     {
2196         IDirectDrawSurface_Release(dsurface);
2197         dsurface = NULL;
2198     }
2199
2200     /* Test a primary surface size */
2201     ret = IDirectDraw_SetCooperativeLevel(lpDD, window, DDSCL_NORMAL);
2202     ok(ret == DD_OK, "SetCooperativeLevel failed with %08x\n", ret);
2203
2204     ZeroMemory(&desc, sizeof(desc));
2205     desc.dwSize = sizeof(desc);
2206     desc.dwFlags = DDSD_CAPS;
2207     desc.ddsCaps.dwCaps |= DDSCAPS_PRIMARYSURFACE;
2208     desc.dwHeight = 128; /* Keep them set to  check what happens */
2209     desc.dwWidth = 128; /* Keep them set to  check what happens */
2210     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2211     ok(ret == DD_OK, "Creating a primary surface without width and height info returned %08x\n", ret);
2212     if(dsurface)
2213     {
2214         ret = IDirectDrawSurface_GetSurfaceDesc(dsurface, &desc);
2215         ok(ret == DD_OK, "GetSurfaceDesc returned %x\n", ret);
2216
2217         IDirectDrawSurface_Release(dsurface);
2218         dsurface = NULL;
2219
2220         ok(desc.dwFlags & DDSD_WIDTH, "Primary surface doesn't have width set\n");
2221         ok(desc.dwFlags & DDSD_HEIGHT, "Primary surface doesn't have height set\n");
2222         ok(desc.dwWidth == GetSystemMetrics(SM_CXSCREEN), "Surface width differs from screen width\n");
2223         ok(desc.dwHeight == GetSystemMetrics(SM_CYSCREEN), "Surface height differs from screen height\n");
2224     }
2225     ret = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
2226     ok(ret == DD_OK, "SetCooperativeLevel failed with %08x\n", ret);
2227 }
2228
2229 static void PrivateDataTest(void)
2230 {
2231     HRESULT hr;
2232     IDirectDrawSurface7 *surface7 = NULL;
2233     IDirectDrawSurface *surface = NULL;
2234     DDSURFACEDESC desc;
2235     ULONG ref, ref2;
2236     IUnknown *ptr;
2237     DWORD size = sizeof(IUnknown *);
2238
2239     ZeroMemory(&desc, sizeof(desc));
2240     desc.dwSize = sizeof(desc);
2241     desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
2242     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2243     desc.dwHeight = 128;
2244     desc.dwWidth = 128;
2245     hr = IDirectDraw_CreateSurface(lpDD, &desc, &surface, NULL);
2246     ok(hr == DD_OK, "Creating an offscreen plain surface failed with %08x\n", hr);
2247     if(!surface)
2248     {
2249         return;
2250     }
2251     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface7, (void **) &surface7);
2252     ok(hr == DD_OK, "IDirectDrawSurface_QueryInterface failed with %08x\n", hr);
2253     if(!surface7)
2254     {
2255         IDirectDrawSurface_Release(surface);
2256         return;
2257     }
2258
2259     /* This fails */
2260     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7 /* Abuse this tag */, lpDD, 0, DDSPD_IUNKNOWNPOINTER);
2261     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2262     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7 /* Abuse this tag */, lpDD, 5, DDSPD_IUNKNOWNPOINTER);
2263     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2264     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7 /* Abuse this tag */, lpDD, sizeof(IUnknown *) * 2, DDSPD_IUNKNOWNPOINTER);
2265     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2266
2267     ref = getref((IUnknown *) lpDD);
2268     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7 /* Abuse this tag */, lpDD, sizeof(IUnknown *), DDSPD_IUNKNOWNPOINTER);
2269     ok(hr == DD_OK, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2270     ref2 = getref((IUnknown *) lpDD);
2271     ok(ref2 == ref + 1, "Object reference is %d, expected %d\n", ref2, ref + 1);
2272     hr = IDirectDrawSurface7_FreePrivateData(surface7, &IID_IDirectDrawSurface7);
2273     ref2 = getref((IUnknown *) lpDD);
2274     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
2275
2276     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7, lpDD, sizeof(IUnknown *), DDSPD_IUNKNOWNPOINTER);
2277     ok(hr == DD_OK, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2278     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7, surface7, sizeof(IUnknown *), DDSPD_IUNKNOWNPOINTER);
2279     ok(hr == DD_OK, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2280     ref2 = getref((IUnknown *) lpDD);
2281     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
2282
2283     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7, lpDD, sizeof(IUnknown *), DDSPD_IUNKNOWNPOINTER);
2284     ok(hr == DD_OK, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2285     hr = IDirectDrawSurface7_GetPrivateData(surface7, &IID_IDirectDrawSurface7, &ptr, &size);
2286     ok(hr == DD_OK, "IDirectDrawSurface7_GetPrivateData failed with %08x\n", hr);
2287     ref2 = getref((IUnknown *) lpDD);
2288     /* Object is NOT being addrefed */
2289     ok(ptr == (IUnknown *) lpDD, "Returned interface pointer is %p, expected %p\n", ptr, lpDD);
2290     ok(ref2 == ref + 1, "Object reference is %d, expected %d. ptr at %p, orig at %p\n", ref2, ref + 1, ptr, lpDD);
2291
2292     IDirectDrawSurface_Release(surface);
2293     IDirectDrawSurface7_Release(surface7);
2294
2295     /* Destroying the surface frees the held reference */
2296     ref2 = getref((IUnknown *) lpDD);
2297     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
2298 }
2299
2300 static void BltParamTest(void)
2301 {
2302     IDirectDrawSurface *surface1 = NULL, *surface2 = NULL;
2303     DDSURFACEDESC desc;
2304     HRESULT hr;
2305     DDBLTFX BltFx;
2306     RECT valid = {10, 10, 20, 20};
2307     RECT invalid1 = {20, 10, 10, 20};
2308     RECT invalid2 = {20, 20, 20, 20};
2309     RECT invalid3 = {-1, -1, 20, 20};
2310     RECT invalid4 = {60, 60, 70, 70};
2311
2312     memset(&desc, 0, sizeof(desc));
2313     desc.dwSize = sizeof(desc);
2314     desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
2315     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2316     desc.dwHeight = 128;
2317     desc.dwWidth = 128;
2318     hr = IDirectDraw_CreateSurface(lpDD, &desc, &surface1, NULL);
2319     ok(hr == DD_OK, "Creating an offscreen plain surface failed with %08x\n", hr);
2320
2321     desc.dwHeight = 64;
2322     desc.dwWidth = 64;
2323     hr = IDirectDraw_CreateSurface(lpDD, &desc, &surface2, NULL);
2324     ok(hr == DD_OK, "Creating an offscreen plain surface failed with %08x\n", hr);
2325
2326     if(0)
2327     {
2328         /* This crashes */
2329         hr = IDirectDrawSurface_BltFast(surface1, 0, 0, NULL, NULL, 0);
2330         ok(hr == DD_OK, "BltFast from NULL surface returned %08x\n", hr);
2331     }
2332     hr = IDirectDrawSurface_BltFast(surface1, 0, 0, surface2, NULL, 0);
2333     ok(hr == DD_OK, "BltFast from smaller to bigger surface returned %08x\n", hr);
2334     hr = IDirectDrawSurface_BltFast(surface2, 0, 0, surface1, NULL, 0);
2335     ok(hr == DDERR_INVALIDRECT, "BltFast from bigger to smaller surface returned %08x\n", hr);
2336     hr = IDirectDrawSurface_BltFast(surface2, 0, 0, surface1, &valid, 0);
2337     ok(hr == DD_OK, "BltFast from bigger to smaller surface using a valid rectangle returned %08x\n", hr);
2338     hr = IDirectDrawSurface_BltFast(surface2, 60, 60, surface1, &valid, 0);
2339     ok(hr == DDERR_INVALIDRECT, "BltFast with a rectangle resulting in an off-surface write returned %08x\n", hr);
2340     hr = IDirectDrawSurface_BltFast(surface1, 90, 90, surface2, NULL, 0);
2341     ok(hr == DDERR_INVALIDRECT, "BltFast with a rectangle resulting in an off-surface write returned %08x\n", hr);
2342     hr = IDirectDrawSurface_BltFast(surface2, 0, 0, surface1, &invalid1, 0);
2343     ok(hr == DDERR_INVALIDRECT, "BltFast with invalid rectangle 1 returned %08x\n", hr);
2344     hr = IDirectDrawSurface_BltFast(surface2, 0, 0, surface1, &invalid2, 0);
2345     ok(hr == DDERR_INVALIDRECT, "BltFast with invalid rectangle 2 returned %08x\n", hr);
2346     hr = IDirectDrawSurface_BltFast(surface2, 0, 0, surface1, &invalid3, 0);
2347     ok(hr == DDERR_INVALIDRECT, "BltFast with invalid rectangle 3 returned %08x\n", hr);
2348     hr = IDirectDrawSurface_BltFast(surface1, 0, 0, surface2, &invalid4, 0);
2349     ok(hr == DDERR_INVALIDRECT, "BltFast with invalid rectangle 3 returned %08x\n", hr);
2350     hr = IDirectDrawSurface_BltFast(surface1, 0, 0, surface1, NULL, 0);
2351     ok(hr == DD_OK, "BltFast blitting a surface onto itself returned %08x\n", hr);
2352
2353     /* Blt(non-fast) tests */
2354     memset(&BltFx, 0, sizeof(BltFx));
2355     BltFx.dwSize = sizeof(BltFx);
2356     U5(BltFx).dwFillColor = 0xaabbccdd;
2357
2358     hr = IDirectDrawSurface_Blt(surface1, &valid, NULL, NULL, DDBLT_COLORFILL, &BltFx);
2359     ok(hr == DD_OK, "IDirectDrawSurface_Blt with a valid rectangle for color fill returned %08x\n", hr);
2360     hr = IDirectDrawSurface_Blt(surface1, &valid, NULL, &invalid3, DDBLT_COLORFILL, &BltFx);
2361     ok(hr == DD_OK, "IDirectDrawSurface_Blt with a invalid, unused rectangle returned %08x\n", hr);
2362     hr = IDirectDrawSurface_Blt(surface2, &invalid1, NULL, NULL, DDBLT_COLORFILL, &BltFx);
2363     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 1 returned %08x\n", hr);
2364     hr = IDirectDrawSurface_Blt(surface2, &invalid2, NULL, NULL, DDBLT_COLORFILL, &BltFx);
2365     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 2 returned %08x\n", hr);
2366     hr = IDirectDrawSurface_Blt(surface2, &invalid3, NULL, NULL, DDBLT_COLORFILL, &BltFx);
2367     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 3 returned %08x\n", hr);
2368     hr = IDirectDrawSurface_Blt(surface2, &invalid4, NULL, NULL, DDBLT_COLORFILL, &BltFx);
2369     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 4 returned %08x\n", hr);
2370
2371     /* Valid on surface 1 */
2372     hr = IDirectDrawSurface_Blt(surface1, &invalid4, NULL, NULL, DDBLT_COLORFILL, &BltFx);
2373     ok(hr == DD_OK, "IDirectDrawSurface_Blt with a subrectangle fill returned %08x\n", hr);
2374
2375     /* Works - stretched blit */
2376     hr = IDirectDrawSurface_Blt(surface1, NULL, surface2, NULL, 0, NULL);
2377     ok(hr == DD_OK, "IDirectDrawSurface_Blt from a smaller to a bigger surface returned %08x\n", hr);
2378     hr = IDirectDrawSurface_Blt(surface2, NULL, surface1, NULL, 0, NULL);
2379     ok(hr == DD_OK, "IDirectDrawSurface_Blt from a bigger to a smaller surface %08x\n", hr);
2380
2381     /* Invalid dest rects in sourced blits */
2382     hr = IDirectDrawSurface_Blt(surface2, &invalid1, surface1, NULL, 0, NULL);
2383     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 1 returned %08x\n", hr);
2384     hr = IDirectDrawSurface_Blt(surface2, &invalid2, surface1, NULL, 0, NULL);
2385     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 2 returned %08x\n", hr);
2386     hr = IDirectDrawSurface_Blt(surface2, &invalid3, surface1, NULL, 0, NULL);
2387     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 3 returned %08x\n", hr);
2388     hr = IDirectDrawSurface_Blt(surface2, &invalid4, surface1, NULL, 0, NULL);
2389     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 4 returned %08x\n", hr);
2390
2391     /* Invalid src rects */
2392     hr = IDirectDrawSurface_Blt(surface2, NULL, surface1, &invalid1, 0, NULL);
2393     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 1 returned %08x\n", hr);
2394     hr = IDirectDrawSurface_Blt(surface2, NULL, surface1, &invalid2, 0, NULL);
2395     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 2 returned %08x\n", hr);
2396     hr = IDirectDrawSurface_Blt(surface2, NULL, surface1, &invalid3, 0, NULL);
2397     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 3 returned %08x\n", hr);
2398     hr = IDirectDrawSurface_Blt(surface1, NULL, surface2, &invalid4, 0, NULL);
2399     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 4 returned %08x\n", hr);
2400
2401     IDirectDrawSurface_Release(surface1);
2402     IDirectDrawSurface_Release(surface2);
2403 }
2404
2405 static void PaletteTest(void)
2406 {
2407     HRESULT hr;
2408     LPDIRECTDRAWSURFACE lpSurf = NULL;
2409     DDSURFACEDESC ddsd;
2410     IDirectDrawPalette *palette = NULL;
2411     PALETTEENTRY Table[256];
2412     PALETTEENTRY palEntries[256];
2413     int i;
2414
2415     for(i=0; i<256; i++)
2416     {
2417         Table[i].peRed   = 0xff;
2418         Table[i].peGreen = 0;
2419         Table[i].peBlue  = 0;
2420         Table[i].peFlags = 0;
2421     }
2422
2423     /* Create a 8bit palette without DDPCAPS_ALLOW256 set */
2424     hr = IDirectDraw_CreatePalette(lpDD, DDPCAPS_8BIT, Table, &palette, NULL);
2425     ok(hr == DD_OK, "CreatePalette failed with %08x\n", hr);
2426     if (FAILED(hr)) goto err;
2427     /* Read back the palette and verify the entries. Without DDPCAPS_ALLOW256 set
2428     /  entry 0 and 255 should have been overwritten with black and white */
2429     IDirectDrawPalette_GetEntries(palette , 0, 0, 256, &palEntries[0]);
2430     ok(hr == DD_OK, "GetEntries failed with %08x\n", hr);
2431     if(hr == DD_OK)
2432     {
2433         ok((palEntries[0].peRed == 0) && (palEntries[0].peGreen == 0) && (palEntries[0].peBlue == 0),
2434            "Palette entry 0 of a palette without DDPCAPS_ALLOW256 set should be (0,0,0) but it is (%d,%d,%d)\n",
2435            palEntries[0].peRed, palEntries[0].peGreen, palEntries[0].peBlue);
2436         ok((palEntries[255].peRed == 255) && (palEntries[255].peGreen == 255) && (palEntries[255].peBlue == 255),
2437            "Palette entry 255 of a palette without DDPCAPS_ALLOW256 set should be (255,255,255) but it is (%d,%d,%d)\n",
2438            palEntries[255].peRed, palEntries[255].peGreen, palEntries[255].peBlue);
2439
2440         /* Entry 1-254 should contain red */
2441         for(i=1; i<255; i++)
2442             ok((palEntries[i].peRed == 255) && (palEntries[i].peGreen == 0) && (palEntries[i].peBlue == 0),
2443                "Palette entry %d should have contained (255,0,0) but was set to %d,%d,%d)\n",
2444                i, palEntries[i].peRed, palEntries[i].peGreen, palEntries[i].peBlue);
2445     }
2446
2447     /* CreatePalette without DDPCAPS_ALLOW256 ignores entry 0 and 255,
2448     /  now check we are able to update the entries afterwards. */
2449     IDirectDrawPalette_SetEntries(palette , 0, 0, 256, &Table[0]);
2450     ok(hr == DD_OK, "SetEntries failed with %08x\n", hr);
2451     IDirectDrawPalette_GetEntries(palette , 0, 0, 256, &palEntries[0]);
2452     ok(hr == DD_OK, "GetEntries failed with %08x\n", hr);
2453     if(hr == DD_OK)
2454     {
2455         ok((palEntries[0].peRed == 0) && (palEntries[0].peGreen == 0) && (palEntries[0].peBlue == 0),
2456            "Palette entry 0 should have been set to (0,0,0) but it contains (%d,%d,%d)\n",
2457            palEntries[0].peRed, palEntries[0].peGreen, palEntries[0].peBlue);
2458         ok((palEntries[255].peRed == 255) && (palEntries[255].peGreen == 255) && (palEntries[255].peBlue == 255),
2459            "Palette entry 255 should have been set to (255,255,255) but it contains (%d,%d,%d)\n",
2460            palEntries[255].peRed, palEntries[255].peGreen, palEntries[255].peBlue);
2461     }
2462     IDirectDrawPalette_Release(palette);
2463
2464     /* Create a 8bit palette with DDPCAPS_ALLOW256 set */
2465     hr = IDirectDraw_CreatePalette(lpDD, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
2466     ok(hr == DD_OK, "CreatePalette failed with %08x\n", hr);
2467     if (FAILED(hr)) goto err;
2468
2469     IDirectDrawPalette_GetEntries(palette , 0, 0, 256, &palEntries[0]);
2470     ok(hr == DD_OK, "GetEntries failed with %08x\n", hr);
2471     if(hr == DD_OK)
2472     {
2473         /* All entries should contain red */
2474         for(i=0; i<256; i++)
2475             ok((palEntries[i].peRed == 255) && (palEntries[i].peGreen == 0) && (palEntries[i].peBlue == 0),
2476                "Palette entry %d should have contained (255,0,0) but was set to %d,%d,%d)\n",
2477                i, palEntries[i].peRed, palEntries[i].peGreen, palEntries[i].peBlue);
2478     }
2479
2480     /* Try to set palette to a non-palettized surface */
2481     ddsd.dwSize = sizeof(ddsd);
2482     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
2483     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
2484     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
2485     ddsd.dwWidth = 800;
2486     ddsd.dwHeight = 600;
2487     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
2488     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
2489     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xFF0000;
2490     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x00FF00;
2491     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x0000FF;
2492     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpSurf, NULL);
2493     ok(hr==DD_OK, "CreateSurface returned: %x\n",hr);
2494     if (FAILED(hr)) {
2495         skip("failed to create surface\n");
2496         goto err;
2497     }
2498
2499     hr = IDirectDrawSurface_SetPalette(lpSurf, palette);
2500     ok(hr == DDERR_INVALIDPIXELFORMAT, "CreateSurface returned: %x\n",hr);
2501
2502     IDirectDrawPalette_Release(palette);
2503     palette = NULL;
2504
2505     hr = IDirectDrawSurface_GetPalette(lpSurf, &palette);
2506     ok(hr == DDERR_NOPALETTEATTACHED, "CreateSurface returned: %x\n",hr);
2507
2508     err:
2509
2510     if (lpSurf) IDirectDrawSurface_Release(lpSurf);
2511     if (palette) IDirectDrawPalette_Release(palette);
2512 }
2513
2514 static void StructSizeTest(void)
2515 {
2516     IDirectDrawSurface *surface1;
2517     IDirectDrawSurface7 *surface7;
2518     union {
2519         DDSURFACEDESC desc1;
2520         DDSURFACEDESC2 desc2;
2521         char blob[1024]; /* To get a bunch of writable memory */
2522     } desc;
2523     DDSURFACEDESC create;
2524     HRESULT hr;
2525
2526     memset(&desc, 0, sizeof(desc));
2527     memset(&create, 0, sizeof(create));
2528
2529     memset(&create, 0, sizeof(create));
2530     create.dwSize = sizeof(create);
2531     create.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
2532     create.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2533     create.dwHeight = 128;
2534     create.dwWidth = 128;
2535     hr = IDirectDraw_CreateSurface(lpDD, &create, &surface1, NULL);
2536     ok(hr == DD_OK, "Creating an offscreen plain surface failed with %08x\n", hr);
2537     hr = IDirectDrawSurface_QueryInterface(surface1, &IID_IDirectDrawSurface7, (void **) &surface7);
2538     ok(hr == DD_OK, "IDirectDrawSurface_QueryInterface failed with %08x\n", hr);
2539
2540     desc.desc1.dwSize = sizeof(DDSURFACEDESC);
2541     hr = IDirectDrawSurface_GetSurfaceDesc(surface1, &desc.desc1);
2542     ok(hr == DD_OK, "IDirectDrawSurface_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC) returned %08x\n", hr);
2543     hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &desc.desc2);
2544     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC) returned %08x\n", hr);
2545
2546     desc.desc2.dwSize = sizeof(DDSURFACEDESC2);
2547     hr = IDirectDrawSurface_GetSurfaceDesc(surface1, &desc.desc1);
2548     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC2) returned %08x\n", hr);
2549     hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &desc.desc2);
2550     ok(hr == DD_OK, "IDirectDrawSurface7_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC2) returned %08x\n", hr);
2551
2552     desc.desc2.dwSize = 0;
2553     hr = IDirectDrawSurface_GetSurfaceDesc(surface1, &desc.desc1);
2554     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_GetSurfaceDesc with desc size 0 returned %08x\n", hr);
2555     hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &desc.desc2);
2556     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_GetSurfaceDesc with desc size 0 returned %08x\n", hr);
2557
2558     desc.desc1.dwSize = sizeof(DDSURFACEDESC) + 1;
2559     hr = IDirectDrawSurface_GetSurfaceDesc(surface1, &desc.desc1);
2560     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC) + 1 returned %08x\n", hr);
2561     hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &desc.desc2);
2562     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC) + 1 returned %08x\n", hr);
2563
2564     desc.desc2.dwSize = sizeof(DDSURFACEDESC2) + 1;
2565     hr = IDirectDrawSurface_GetSurfaceDesc(surface1, &desc.desc1);
2566     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC2) + 1returned %08x\n", hr);
2567     hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &desc.desc2);
2568     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC2) + 1returned %08x\n", hr);
2569
2570     /* Tests for Lock() */
2571
2572     desc.desc1.dwSize = sizeof(DDSURFACEDESC);
2573     hr = IDirectDrawSurface_Lock(surface1, NULL, &desc.desc1, 0, 0);
2574     ok(hr == DD_OK, "IDirectDrawSurface_Lock with desc size sizeof(DDSURFACEDESC) returned %08x\n", hr);
2575     if(SUCCEEDED(hr)) IDirectDrawSurface_Unlock(surface1, NULL);
2576     ok(desc.desc1.dwSize == sizeof(DDSURFACEDESC), "Destination size was changed to %d\n", desc.desc1.dwSize);
2577     hr = IDirectDrawSurface7_Lock(surface7, NULL, &desc.desc2, 0, 0);
2578     ok(hr == DD_OK, "IDirectDrawSurface7_Lock with desc size sizeof(DDSURFACEDESC) returned %08x\n", hr);
2579     if(SUCCEEDED(hr)) IDirectDrawSurface7_Unlock(surface7, NULL);
2580     ok(desc.desc2.dwSize == sizeof(DDSURFACEDESC), "Destination size was changed to %d\n", desc.desc1.dwSize);
2581
2582     desc.desc2.dwSize = sizeof(DDSURFACEDESC2);
2583     hr = IDirectDrawSurface_Lock(surface1, NULL, &desc.desc1, 0, 0);
2584     ok(hr == DD_OK, "IDirectDrawSurface_Lock with desc size sizeof(DDSURFACEDESC2) returned %08x\n", hr);
2585     ok(desc.desc1.dwSize == sizeof(DDSURFACEDESC2), "Destination size was changed to %d\n", desc.desc1.dwSize);
2586     if(SUCCEEDED(hr)) IDirectDrawSurface_Unlock(surface1, NULL);
2587     hr = IDirectDrawSurface7_Lock(surface7, NULL, &desc.desc2, 0, 0);
2588     ok(hr == DD_OK, "IDirectDrawSurface7_Lock with desc size sizeof(DDSURFACEDESC2) returned %08x\n", hr);
2589     if(SUCCEEDED(hr)) IDirectDrawSurface7_Unlock(surface7, NULL);
2590     ok(desc.desc2.dwSize == sizeof(DDSURFACEDESC2), "Destination size was changed to %d\n", desc.desc1.dwSize);
2591
2592     desc.desc2.dwSize = 0;
2593     hr = IDirectDrawSurface_Lock(surface1, NULL, &desc.desc1, 0, 0);
2594     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Lock with desc size 0 returned %08x\n", hr);
2595     if(SUCCEEDED(hr)) IDirectDrawSurface_Unlock(surface1, NULL);
2596     hr = IDirectDrawSurface7_Lock(surface7, NULL, &desc.desc2, 0, 0);
2597     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_Lock with desc size 0 returned %08x\n", hr);
2598     if(SUCCEEDED(hr)) IDirectDrawSurface7_Unlock(surface7, NULL);
2599
2600     desc.desc1.dwSize = sizeof(DDSURFACEDESC) + 1;
2601     hr = IDirectDrawSurface_Lock(surface1, NULL, &desc.desc1, 0, 0);
2602     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Lock with desc size sizeof(DDSURFACEDESC) + 1 returned %08x\n", hr);
2603     if(SUCCEEDED(hr)) IDirectDrawSurface_Unlock(surface1, NULL);
2604     hr = IDirectDrawSurface7_Lock(surface7, NULL, &desc.desc2, 0, 0);
2605     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_Lock with desc size sizeof(DDSURFACEDESC) + 1 returned %08x\n", hr);
2606     if(SUCCEEDED(hr)) IDirectDrawSurface7_Unlock(surface7, NULL);
2607
2608     desc.desc2.dwSize = sizeof(DDSURFACEDESC2) + 1;
2609     hr = IDirectDrawSurface_Lock(surface1, NULL, &desc.desc1, 0, 0);
2610     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Lock with desc size sizeof(DDSURFACEDESC2) + 1returned %08x\n", hr);
2611     if(SUCCEEDED(hr)) IDirectDrawSurface_Unlock(surface1, NULL);
2612     hr = IDirectDrawSurface7_Lock(surface7, NULL, &desc.desc2, 0, 0);
2613     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_Lock with desc size sizeof(DDSURFACEDESC2) + 1returned %08x\n", hr);
2614     if(SUCCEEDED(hr)) IDirectDrawSurface7_Unlock(surface7, NULL);
2615
2616     IDirectDrawSurface7_Release(surface7);
2617     IDirectDrawSurface_Release(surface1);
2618 }
2619
2620 static void SurfaceCapsTest(void)
2621 {
2622     DDSURFACEDESC create;
2623     DDSURFACEDESC desc;
2624     HRESULT hr;
2625     IDirectDrawSurface *surface1 = NULL;
2626     DDSURFACEDESC2 create2, desc2;
2627     IDirectDrawSurface7 *surface7 = NULL;
2628     IDirectDraw7 *dd7 = NULL;
2629     DWORD create_caps[] = {
2630         DDSCAPS_OFFSCREENPLAIN,
2631         DDSCAPS_TEXTURE,
2632         DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
2633         0,
2634         DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD | DDSCAPS_SYSTEMMEMORY,
2635         DDSCAPS_PRIMARYSURFACE,
2636         DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY
2637     };
2638     DWORD expected_caps[] = {
2639         DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
2640         DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
2641         DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM | DDSCAPS_ALLOCONLOAD,
2642         DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
2643         DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD | DDSCAPS_SYSTEMMEMORY,
2644         DDSCAPS_PRIMARYSURFACE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM | DDSCAPS_VISIBLE,
2645         DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_VISIBLE
2646     };
2647     UINT i;
2648     DDCAPS ddcaps;
2649
2650     /* Tests various surface flags, what changes do they undergo during surface creation. Forsaken
2651      * engine expects texture surfaces without memory flag to get a video memory flag right after
2652      * creation. Currently, Wine adds DDSCAPS_FRONTBUFFER to primary surface, but native doesn't do this
2653      * for single buffered primaries. Because of this primary surface creation tests are todo_wine. No real
2654      * app is known so far to care about this. */
2655     ddcaps.dwSize = sizeof(DDCAPS);
2656     hr = IDirectDraw_GetCaps(lpDD, &ddcaps, NULL);
2657     ok(hr == DD_OK, "IDirectDraw_GetCaps failed with %08x\n", hr);
2658
2659     if (!(ddcaps.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
2660     {
2661         skip("DDraw reported no VIDEOMEMORY cap. Broken video driver? Skipping surface caps tests.\n");
2662         return ;
2663     }
2664
2665     for (i = 0; i < sizeof(create_caps) / sizeof(DWORD); i++)
2666     {
2667         memset(&create, 0, sizeof(create));
2668         create.dwSize = sizeof(create);
2669         create.ddsCaps.dwCaps = create_caps[i];
2670         create.dwFlags = DDSD_CAPS;
2671
2672         if (!(create.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2673         {
2674             create.dwFlags |= DDSD_HEIGHT | DDSD_WIDTH;
2675             create.dwHeight = 128;
2676             create.dwWidth = 128;
2677         }
2678
2679         hr = IDirectDraw_CreateSurface(lpDD, &create, &surface1, NULL);
2680         ok(hr == DD_OK, "IDirectDraw_CreateSurface failed with %08x\n", hr);
2681
2682         if (SUCCEEDED(hr))
2683         {
2684             memset(&desc, 0, sizeof(desc));
2685             desc.dwSize = sizeof(DDSURFACEDESC);
2686             hr = IDirectDrawSurface_GetSurfaceDesc(surface1, &desc);
2687             ok(hr == DD_OK, "IDirectDrawSurface_GetSurfaceDesc failed with %08x\n", hr);
2688
2689             if (!(create_caps[i] & DDSCAPS_PRIMARYSURFACE))
2690                 ok(desc.ddsCaps.dwCaps == expected_caps[i],
2691                     "GetSurfaceDesc returned caps %x, expected %x\n", desc.ddsCaps.dwCaps,
2692                     expected_caps[i]);
2693             else
2694                 todo_wine ok(desc.ddsCaps.dwCaps == expected_caps[i],
2695                                 "GetSurfaceDesc returned caps %x, expected %x\n", desc.ddsCaps.dwCaps,
2696                                 expected_caps[i]);
2697
2698             IDirectDrawSurface_Release(surface1);
2699         }
2700     }
2701
2702     /* Test for differences in ddraw 7 */
2703     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
2704     ok(hr == DD_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
2705     if (FAILED(hr))
2706     {
2707         skip("Failed to get IDirectDraw7 interface, skipping tests\n");
2708     }
2709     else
2710     {
2711         for (i = 0; i < sizeof(create_caps) / sizeof(DWORD); i++)
2712         {
2713             memset(&create2, 0, sizeof(create2));
2714             create2.dwSize = sizeof(create2);
2715             create2.ddsCaps.dwCaps = create_caps[i];
2716             create2.dwFlags = DDSD_CAPS;
2717
2718             if (!(create2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2719             {
2720                 create2.dwFlags |= DDSD_HEIGHT | DDSD_WIDTH;
2721                 create2.dwHeight = 128;
2722                 create2.dwWidth = 128;
2723             }
2724
2725             hr = IDirectDraw7_CreateSurface(dd7, &create2, &surface7, NULL);
2726             ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
2727
2728             if (SUCCEEDED(hr))
2729             {
2730                 memset(&desc2, 0, sizeof(desc2));
2731                 desc2.dwSize = sizeof(DDSURFACEDESC2);
2732                 hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &desc2);
2733                 ok(hr == DD_OK, "IDirectDrawSurface_GetSurfaceDesc failed with %08x\n", hr);
2734
2735                 if (!(create_caps[i] & DDSCAPS_PRIMARYSURFACE))
2736                     ok(desc2.ddsCaps.dwCaps == expected_caps[i],
2737                         "GetSurfaceDesc returned caps %x, expected %x\n", desc2.ddsCaps.dwCaps,
2738                         expected_caps[i]);
2739                 else
2740                     todo_wine ok(desc2.ddsCaps.dwCaps == expected_caps[i],
2741                                     "GetSurfaceDesc returned caps %x, expected %x\n", desc2.ddsCaps.dwCaps,
2742                                     expected_caps[i]);
2743
2744                 IDirectDrawSurface7_Release(surface7);
2745             }
2746         }
2747
2748         IDirectDraw7_Release(dd7);
2749     }
2750
2751     memset(&create, 0, sizeof(create));
2752     create.dwSize = sizeof(create);
2753     create.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
2754     create.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_VIDEOMEMORY;
2755     create.dwWidth = 64;
2756     create.dwHeight = 64;
2757     hr = IDirectDraw_CreateSurface(lpDD, &create, &surface1, NULL);
2758     ok(hr == DDERR_INVALIDCAPS, "Creating a SYSMEM | VIDMEM surface returned 0x%08x, expected DDERR_INVALIDCAPS\n", hr);
2759     if(surface1) IDirectDrawSurface_Release(surface1);
2760 }
2761
2762 static BOOL can_create_primary_surface(void)
2763 {
2764     DDSURFACEDESC ddsd;
2765     IDirectDrawSurface *surface;
2766     HRESULT hr;
2767
2768     memset(&ddsd, 0, sizeof(ddsd));
2769     ddsd.dwSize = sizeof(ddsd);
2770     ddsd.dwFlags = DDSD_CAPS;
2771     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2772     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface, NULL);
2773     if(FAILED(hr)) return FALSE;
2774     IDirectDrawSurface_Release(surface);
2775     return TRUE;
2776 }
2777
2778 static void dctest_surf(IDirectDrawSurface *surf, int ddsdver) {
2779     HRESULT hr;
2780     HDC dc, dc2 = (HDC) 0x1234;
2781     DDSURFACEDESC ddsd;
2782     DDSURFACEDESC2 ddsd2;
2783
2784     memset(&ddsd, 0, sizeof(ddsd));
2785     ddsd.dwSize = sizeof(ddsd);
2786     memset(&ddsd2, 0, sizeof(ddsd2));
2787     ddsd2.dwSize = sizeof(ddsd2);
2788
2789     hr = IDirectDrawSurface_GetDC(surf, &dc);
2790     ok(hr == DD_OK, "IDirectDrawSurface_GetDC failed: 0x%08x\n", hr);
2791
2792     hr = IDirectDrawSurface_GetDC(surf, &dc2);
2793     ok(hr == DDERR_DCALREADYCREATED, "IDirectDrawSurface_GetDC failed: 0x%08x\n", hr);
2794     ok(dc2 == (HDC) 0x1234, "The failed GetDC call changed the dc: %p\n", dc2);
2795
2796     hr = IDirectDrawSurface_Lock(surf, NULL, ddsdver == 1 ? &ddsd : ((DDSURFACEDESC *) &ddsd2), 0, NULL);
2797     ok(hr == DDERR_SURFACEBUSY, "IDirectDrawSurface_Lock returned 0x%08x, expected DDERR_ALREADYLOCKED\n", hr);
2798
2799     hr = IDirectDrawSurface_ReleaseDC(surf, dc);
2800     ok(hr == DD_OK, "IDirectDrawSurface_ReleaseDC failed: 0x%08x\n", hr);
2801     hr = IDirectDrawSurface_ReleaseDC(surf, dc);
2802     ok(hr == DDERR_NODC, "IDirectDrawSurface_ReleaseDC returned 0x%08x, expected DDERR_NODC\n", hr);
2803 }
2804
2805 static void GetDCTest(void)
2806 {
2807     DDSURFACEDESC ddsd;
2808     DDSURFACEDESC2 ddsd2;
2809     IDirectDrawSurface *surf;
2810     IDirectDrawSurface2 *surf2;
2811     IDirectDrawSurface4 *surf4;
2812     IDirectDrawSurface7 *surf7;
2813     HRESULT hr;
2814     IDirectDraw2 *dd2;
2815     IDirectDraw4 *dd4;
2816     IDirectDraw7 *dd7;
2817
2818     memset(&ddsd, 0, sizeof(ddsd));
2819     ddsd.dwSize = sizeof(ddsd);
2820     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
2821     ddsd.dwWidth = 64;
2822     ddsd.dwHeight = 64;
2823     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
2824     memset(&ddsd2, 0, sizeof(ddsd2));
2825     ddsd2.dwSize = sizeof(ddsd2);
2826     ddsd2.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
2827     ddsd2.dwWidth = 64;
2828     ddsd2.dwHeight = 64;
2829     ddsd2.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
2830
2831     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surf, NULL);
2832     ok(hr == DD_OK, "IDirectDraw_CreateSurface failed: 0x%08x\n", hr);
2833     dctest_surf(surf, 1);
2834     IDirectDrawSurface_Release(surf);
2835
2836     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
2837     ok(hr == DD_OK, "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
2838
2839     hr = IDirectDraw2_CreateSurface(dd2, &ddsd, &surf, NULL);
2840     ok(hr == DD_OK, "IDirectDraw2_CreateSurface failed: 0x%08x\n", hr);
2841     dctest_surf(surf, 1);
2842
2843     hr = IDirectDrawSurface_QueryInterface(surf, &IID_IDirectDrawSurface2, (void **) &surf2);
2844     ok(hr == DD_OK, "IDirectDrawSurface_QueryInterface failed: 0x%08x\n", hr);
2845     dctest_surf((IDirectDrawSurface *) surf2, 1);
2846
2847     IDirectDrawSurface2_Release(surf2);
2848     IDirectDrawSurface_Release(surf);
2849     IDirectDraw2_Release(dd2);
2850
2851     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
2852     ok(hr == DD_OK, "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
2853
2854     surf = NULL;
2855     hr = IDirectDraw4_CreateSurface(dd4, &ddsd2, &surf4, NULL);
2856     ok(hr == DD_OK, "IDirectDraw4_CreateSurface failed: 0x%08x\n", hr);
2857     dctest_surf((IDirectDrawSurface *) surf4, 2);
2858
2859     IDirectDrawSurface4_Release(surf4);
2860     IDirectDraw4_Release(dd4);
2861
2862     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
2863     ok(hr == DD_OK, "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
2864     surf = NULL;
2865     hr = IDirectDraw7_CreateSurface(dd7, &ddsd2, &surf7, NULL);
2866     ok(hr == DD_OK, "IDirectDraw7_CreateSurface failed: 0x%08x\n", hr);
2867     dctest_surf((IDirectDrawSurface *) surf7, 2);
2868
2869     IDirectDrawSurface7_Release(surf7);
2870     IDirectDraw7_Release(dd7);
2871 }
2872
2873 START_TEST(dsurface)
2874 {
2875     HRESULT ret;
2876     IDirectDraw4 *dd4;
2877
2878     if (!CreateDirectDraw())
2879         return;
2880
2881     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
2882     if (ret == E_NOINTERFACE)
2883     {
2884         win_skip("DirectDraw4 and higher are not supported\n");
2885         ReleaseDirectDraw();
2886         return;
2887     }
2888     IDirectDraw_Release(dd4);
2889
2890     if(!can_create_primary_surface())
2891     {
2892         skip("Unable to create primary surface\n");
2893         return;
2894     }
2895
2896     MipMapCreationTest();
2897     SrcColorKey32BlitTest();
2898     QueryInterface();
2899     GetDDInterface_1();
2900     GetDDInterface_2();
2901     GetDDInterface_4();
2902     GetDDInterface_7();
2903     EnumTest();
2904     AttachmentTest();
2905     AttachmentTest7();
2906     CubeMapTest();
2907     test_lockrect_invalid();
2908     CompressedTest();
2909     SizeTest();
2910     PrivateDataTest();
2911     BltParamTest();
2912     StructSizeTest();
2913     PaletteTest();
2914     SurfaceCapsTest();
2915     GetDCTest();
2916     ReleaseDirectDraw();
2917 }