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