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