ddraw/tests: IDirect3DViewport3, IDirect3DViewport2 and IDirect3DViewport use same...
[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 "wine/test.h"
27 #include "wine/exception.h"
28 #include "ddraw.h"
29 #include "d3d.h"
30 #include "unknwn.h"
31
32 static LPDIRECTDRAW lpDD = NULL;
33 static DDCAPS ddcaps;
34
35 static BOOL CreateDirectDraw(void)
36 {
37     HRESULT rc;
38
39     rc = DirectDrawCreate(NULL, &lpDD, NULL);
40     ok(rc==DD_OK || rc==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", rc);
41     if (!lpDD) {
42         trace("DirectDrawCreateEx() failed with an error %x\n", rc);
43         return FALSE;
44     }
45
46     rc = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
47     ok(rc==DD_OK,"SetCooperativeLevel returned: %x\n",rc);
48
49     return TRUE;
50 }
51
52
53 static void ReleaseDirectDraw(void)
54 {
55     if( lpDD != NULL )
56     {
57         IDirectDraw_Release(lpDD);
58         lpDD = NULL;
59     }
60 }
61
62 static void MipMapCreationTest(void)
63 {
64     LPDIRECTDRAWSURFACE lpDDSMipMapTest;
65     DDSURFACEDESC ddsd;
66     HRESULT rc;
67
68     /* First mipmap creation test: create a surface with DDSCAPS_COMPLEX,
69        DDSCAPS_MIPMAP, and DDSD_MIPMAPCOUNT. This create the number of
70         requested mipmap levels. */
71     ddsd.dwSize = sizeof(ddsd);
72     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
73     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
74     U2(ddsd).dwMipMapCount = 3;
75     ddsd.dwWidth = 128;
76     ddsd.dwHeight = 32;
77     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
78     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
79     if (FAILED(rc))
80     {
81         skip("failed to create surface\n");
82         return;
83     }
84
85     /* Check the number of created mipmaps */
86     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
87     ddsd.dwSize = sizeof(ddsd);
88     rc = IDirectDrawSurface_GetSurfaceDesc(lpDDSMipMapTest, &ddsd);
89     ok(rc==DD_OK,"GetSurfaceDesc returned: %x\n",rc);
90     ok(ddsd.dwFlags & DDSD_MIPMAPCOUNT,
91         "GetSurfaceDesc returned no mipmapcount.\n");
92     ok(U2(ddsd).dwMipMapCount == 3, "Incorrect mipmap count: %d.\n",
93         U2(ddsd).dwMipMapCount);
94
95     /* Destroy the surface. */
96     IDirectDrawSurface_Release(lpDDSMipMapTest);
97
98
99     /* Second mipmap creation test: create a surface without a mipmap
100        count, with DDSCAPS_MIPMAP and without DDSCAPS_COMPLEX.
101        This creates a single mipmap level. */
102     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
103     ddsd.dwSize = sizeof(ddsd);
104     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
105     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_MIPMAP;
106     ddsd.dwWidth = 128;
107     ddsd.dwHeight = 32;
108     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
109     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
110     if (FAILED(rc))
111     {
112         skip("failed to create surface\n");
113         return;
114     }
115     /* Check the number of created mipmaps */
116     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
117     ddsd.dwSize = sizeof(ddsd);
118     rc = IDirectDrawSurface_GetSurfaceDesc(lpDDSMipMapTest, &ddsd);
119     ok(rc==DD_OK,"GetSurfaceDesc returned: %x\n",rc);
120     ok(ddsd.dwFlags & DDSD_MIPMAPCOUNT,
121         "GetSurfaceDesc returned no mipmapcount.\n");
122     ok(U2(ddsd).dwMipMapCount == 1, "Incorrect mipmap count: %d.\n",
123         U2(ddsd).dwMipMapCount);
124
125     /* Destroy the surface. */
126     IDirectDrawSurface_Release(lpDDSMipMapTest);
127
128
129     /* Third mipmap creation test: create a surface with DDSCAPS_MIPMAP,
130         DDSCAPS_COMPLEX and without DDSD_MIPMAPCOUNT.
131        It's an undocumented features where a chain of mipmaps, starting from
132        he specified size and down to the smallest size, is automatically
133        created.
134        Anarchy Online needs this feature to work. */
135     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
136     ddsd.dwSize = sizeof(ddsd);
137     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
138     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
139     ddsd.dwWidth = 128;
140     ddsd.dwHeight = 32;
141     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
142     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
143     if (FAILED(rc))
144     {
145         skip("failed to create surface\n");
146         return;
147     }
148
149     /* Check the number of created mipmaps */
150     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
151     ddsd.dwSize = sizeof(ddsd);
152     rc = IDirectDrawSurface_GetSurfaceDesc(lpDDSMipMapTest, &ddsd);
153     ok(rc==DD_OK,"GetSurfaceDesc returned: %x\n",rc);
154     ok(ddsd.dwFlags & DDSD_MIPMAPCOUNT,
155         "GetSurfaceDesc returned no mipmapcount.\n");
156     ok(U2(ddsd).dwMipMapCount == 6, "Incorrect mipmap count: %d.\n",
157         U2(ddsd).dwMipMapCount);
158
159     /* Destroy the surface. */
160     IDirectDrawSurface_Release(lpDDSMipMapTest);
161
162
163     /* Fourth mipmap creation test: same as above with a different texture
164        size.
165        The purpose is to verify that the number of generated mipmaps is
166        dependent on the smallest dimension. */
167     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
168     ddsd.dwSize = sizeof(ddsd);
169     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
170     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
171     ddsd.dwWidth = 32;
172     ddsd.dwHeight = 64;
173     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
174     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
175     if (FAILED(rc))
176     {
177         skip("failed to create surface\n");
178         return;
179     }
180
181     /* Check the number of created mipmaps */
182     memset(&ddsd, 0, sizeof(DDSURFACEDESC));
183     ddsd.dwSize = sizeof(ddsd);
184     rc = IDirectDrawSurface_GetSurfaceDesc(lpDDSMipMapTest, &ddsd);
185     ok(rc==DD_OK,"GetSurfaceDesc returned: %x\n",rc);
186     ok(ddsd.dwFlags & DDSD_MIPMAPCOUNT,
187         "GetSurfaceDesc returned no mipmapcount.\n");
188     ok(U2(ddsd).dwMipMapCount == 6, "Incorrect mipmap count: %d.\n",
189         U2(ddsd).dwMipMapCount);
190
191     /* Destroy the surface. */
192     IDirectDrawSurface_Release(lpDDSMipMapTest);
193
194
195     /* Fifth mipmap creation test: try to create a surface with
196        DDSCAPS_COMPLEX, DDSCAPS_MIPMAP, DDSD_MIPMAPCOUNT,
197        where dwMipMapCount = 0. This should fail. */
198
199     ddsd.dwSize = sizeof(ddsd);
200     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
201     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
202     U2(ddsd).dwMipMapCount = 0;
203     ddsd.dwWidth = 128;
204     ddsd.dwHeight = 32;
205     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSMipMapTest, NULL);
206     ok(rc==DDERR_INVALIDPARAMS,"CreateSurface returned: %x\n",rc);
207
208     /* Destroy the surface. */
209     if( rc == DD_OK )
210         IDirectDrawSurface_Release(lpDDSMipMapTest);
211
212 }
213
214 static void SrcColorKey32BlitTest(void)
215 {
216     LPDIRECTDRAWSURFACE lpSrc;
217     LPDIRECTDRAWSURFACE lpDst;
218     DDSURFACEDESC ddsd, ddsd2, ddsd3;
219     DDCOLORKEY DDColorKey;
220     LPDWORD lpData;
221     HRESULT rc;
222     DDBLTFX fx;
223
224     ddsd2.dwSize = sizeof(ddsd2);
225     ddsd2.ddpfPixelFormat.dwSize = sizeof(ddsd2.ddpfPixelFormat);
226
227     ddsd.dwSize = sizeof(ddsd);
228     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
229     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
230     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
231     ddsd.dwWidth = 800;
232     ddsd.dwHeight = 600;
233     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
234     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
235     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xFF0000;
236     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x00FF00;
237     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x0000FF;
238     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDst, NULL);
239     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
240     if (FAILED(rc))
241     {
242         skip("failed to create surface\n");
243         return;
244     }
245
246     ddsd.dwFlags |= DDSD_CKSRCBLT;
247     ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0xFF00FF;
248     ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0xFF00FF;
249     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpSrc, NULL);
250     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
251     if (FAILED(rc))
252     {
253         skip("failed to create surface\n");
254         return;
255     }
256
257     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
258     ok(rc==DD_OK,"Lock returned: %x\n",rc);
259     lpData = ddsd2.lpSurface;
260     lpData[0] = 0xCCCCCCCC;
261     lpData[1] = 0xCCCCCCCC;
262     lpData[2] = 0xCCCCCCCC;
263     lpData[3] = 0xCCCCCCCC;
264
265     memset(&ddsd3, 0, sizeof(ddsd3));
266     ddsd3.dwSize = sizeof(ddsd3);
267     ddsd3.ddpfPixelFormat.dwSize = sizeof(ddsd3.ddpfPixelFormat);
268     rc = IDirectDrawSurface_GetSurfaceDesc(lpDst, &ddsd3);
269     ok(rc == DD_OK, "IDirectDrawSurface_GetSurfaceDesc between a lock/unlock pair returned %08x\n", rc);
270     ok(ddsd3.lpSurface == ddsd3.lpSurface, "lpSurface from GetSurfaceDesc(%p) differs from the one returned by Lock(%p)\n", ddsd3.lpSurface, ddsd2.lpSurface);
271
272     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
273     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
274
275     memset(&ddsd3, 0, sizeof(ddsd3));
276     ddsd3.dwSize = sizeof(ddsd3);
277     ddsd3.ddpfPixelFormat.dwSize = sizeof(ddsd3.ddpfPixelFormat);
278     rc = IDirectDrawSurface_GetSurfaceDesc(lpDst, &ddsd3);
279     ok(rc == DD_OK, "IDirectDrawSurface_GetSurfaceDesc between a lock/unlock pair returned %08x\n", rc);
280     ok(ddsd3.lpSurface == NULL, "lpSurface from GetSurfaceDesc(%p) is not NULL after unlock\n", ddsd3.lpSurface);
281
282     rc = IDirectDrawSurface_Lock(lpSrc, NULL, &ddsd2, DDLOCK_WAIT, NULL);
283     ok(rc==DD_OK,"Lock returned: %x\n",rc);
284     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
285     lpData = ddsd2.lpSurface;
286     lpData[0] = 0x77010203;
287     lpData[1] = 0x00010203;
288     lpData[2] = 0x77FF00FF;
289     lpData[3] = 0x00FF00FF;
290     rc = IDirectDrawSurface_Unlock(lpSrc, NULL);
291     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
292
293     IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRC, NULL);
294
295     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
296     ok(rc==DD_OK,"Lock returned: %x\n",rc);
297     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
298     lpData = ddsd2.lpSurface;
299     /* Different behavior on some drivers / windows versions. Some versions ignore the X channel when
300      * color keying, but copy it to the destination surface. Others apply it for color keying, but
301      * do not copy it into the destination surface.
302      */
303     if(lpData[0]==0x00010203) {
304         trace("X channel was not copied into the destination surface\n");
305         ok((lpData[0]==0x00010203)&&(lpData[1]==0x00010203)&&(lpData[2]==0x00FF00FF)&&(lpData[3]==0xCCCCCCCC),
306            "Destination data after blitting is not correct\n");
307     } else {
308         ok((lpData[0]==0x77010203)&&(lpData[1]==0x00010203)&&(lpData[2]==0xCCCCCCCC)&&(lpData[3]==0xCCCCCCCC),
309            "Destination data after blitting is not correct\n");
310     }
311     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
312     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
313
314     /* Below we repeat the same test as above but now using BltFast instead of Blt. Before
315      * we can carry out the test we need to restore the color of the destination surface.
316      */
317     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
318     ok(rc==DD_OK,"Lock returned: %x\n",rc);
319     lpData = ddsd2.lpSurface;
320     lpData[0] = 0xCCCCCCCC;
321     lpData[1] = 0xCCCCCCCC;
322     lpData[2] = 0xCCCCCCCC;
323     lpData[3] = 0xCCCCCCCC;
324     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
325     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
326
327     IDirectDrawSurface_BltFast(lpDst, 0, 0, lpSrc, NULL, DDBLTFAST_SRCCOLORKEY);
328
329     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
330     ok(rc==DD_OK,"Lock returned: %x\n",rc);
331     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
332     lpData = ddsd2.lpSurface;
333     /* Different behavior on some drivers / windows versions. Some versions ignore the X channel when
334      * color keying, but copy it to the destination surface. Others apply it for color keying, but
335      * do not copy it into the destination surface.
336      */
337     if(lpData[0]==0x00010203) {
338         trace("X channel was not copied into the destination surface\n");
339         ok((lpData[0]==0x00010203)&&(lpData[1]==0x00010203)&&(lpData[2]==0x00FF00FF)&&(lpData[3]==0xCCCCCCCC),
340            "Destination data after blitting is not correct\n");
341     } else {
342         ok((lpData[0]==0x77010203)&&(lpData[1]==0x00010203)&&(lpData[2]==0xCCCCCCCC)&&(lpData[3]==0xCCCCCCCC),
343            "Destination data after blitting is not correct\n");
344     }
345     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
346     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
347
348     /* Also test SetColorKey */
349     IDirectDrawSurface_GetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
350     ok(DDColorKey.dwColorSpaceLowValue == 0xFF00FF && DDColorKey.dwColorSpaceHighValue == 0xFF00FF,
351        "GetColorKey does not return the colorkey used at surface creation\n");
352
353     DDColorKey.dwColorSpaceLowValue = 0x00FF00;
354     DDColorKey.dwColorSpaceHighValue = 0x00FF00;
355     IDirectDrawSurface_SetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
356
357     DDColorKey.dwColorSpaceLowValue = 0;
358     DDColorKey.dwColorSpaceHighValue = 0;
359     IDirectDrawSurface_GetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
360     ok(DDColorKey.dwColorSpaceLowValue == 0x00FF00 && DDColorKey.dwColorSpaceHighValue == 0x00FF00,
361        "GetColorKey does not return the colorkey set with SetColorKey\n");
362
363     ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0;
364     ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0;
365     IDirectDrawSurface_GetSurfaceDesc(lpSrc, &ddsd);
366     ok(ddsd.ddckCKSrcBlt.dwColorSpaceLowValue == 0x00FF00 && ddsd.ddckCKSrcBlt.dwColorSpaceHighValue == 0x00FF00,
367        "GetSurfaceDesc does not return the colorkey set with SetColorKey\n");
368
369     /* Test SetColorKey with dwColorSpaceHighValue < dwColorSpaceLowValue */
370     DDColorKey.dwColorSpaceLowValue = 0x0000FF;
371     DDColorKey.dwColorSpaceHighValue = 0x000000;
372     IDirectDrawSurface_SetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
373
374     DDColorKey.dwColorSpaceLowValue = 0;
375     DDColorKey.dwColorSpaceHighValue = 0;
376     IDirectDrawSurface_GetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
377     ok(DDColorKey.dwColorSpaceLowValue == 0x0000FF && DDColorKey.dwColorSpaceHighValue == 0x0000FF,
378        "GetColorKey does not return the colorkey set with SetColorKey (%x %x)\n", DDColorKey.dwColorSpaceLowValue, DDColorKey.dwColorSpaceHighValue);
379
380     DDColorKey.dwColorSpaceLowValue = 0x0000FF;
381     DDColorKey.dwColorSpaceHighValue = 0x000001;
382     IDirectDrawSurface_SetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
383
384     DDColorKey.dwColorSpaceLowValue = 0;
385     DDColorKey.dwColorSpaceHighValue = 0;
386     IDirectDrawSurface_GetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
387     ok(DDColorKey.dwColorSpaceLowValue == 0x0000FF && DDColorKey.dwColorSpaceHighValue == 0x0000FF,
388        "GetColorKey does not return the colorkey set with SetColorKey (%x %x)\n", DDColorKey.dwColorSpaceLowValue, DDColorKey.dwColorSpaceHighValue);
389
390     DDColorKey.dwColorSpaceLowValue = 0x0000FF;
391     DDColorKey.dwColorSpaceHighValue = 0x0000FE;
392     IDirectDrawSurface_SetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
393
394     DDColorKey.dwColorSpaceLowValue = 0;
395     DDColorKey.dwColorSpaceHighValue = 0;
396     IDirectDrawSurface_GetColorKey(lpSrc, DDCKEY_SRCBLT, &DDColorKey);
397     ok(DDColorKey.dwColorSpaceLowValue == 0x0000FF && DDColorKey.dwColorSpaceHighValue == 0x0000FF,
398        "GetColorKey does not return the colorkey set with SetColorKey (%x %x)\n", DDColorKey.dwColorSpaceLowValue, DDColorKey.dwColorSpaceHighValue);
399
400     IDirectDrawSurface_Release(lpSrc);
401     IDirectDrawSurface_Release(lpDst);
402
403     /* start with a new set of surfaces to test the color keying parameters to blit */
404     memset(&ddsd, 0, sizeof(ddsd));
405     ddsd.dwSize = sizeof(ddsd);
406     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
407     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT | DDSD_CKDESTBLT;
408     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
409     ddsd.dwWidth = 800;
410     ddsd.dwHeight = 600;
411     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
412     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
413     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xFF0000;
414     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x00FF00;
415     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x0000FF;
416     ddsd.ddckCKDestBlt.dwColorSpaceLowValue = 0xFF0000;
417     ddsd.ddckCKDestBlt.dwColorSpaceHighValue = 0xFF0000;
418     ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00FF00;
419     ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00FF00;
420     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDst, NULL);
421     ok(rc==DD_OK || rc == DDERR_NOCOLORKEYHW,"CreateSurface returned: %x\n",rc);
422     if(FAILED(rc))
423     {
424         skip("Failed to create surface\n");
425         return;
426     }
427
428     /* start with a new set of surfaces to test the color keying parameters to blit */
429     memset(&ddsd, 0, sizeof(ddsd));
430     ddsd.dwSize = sizeof(ddsd);
431     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
432     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT | DDSD_CKDESTBLT;
433     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
434     ddsd.dwWidth = 800;
435     ddsd.dwHeight = 600;
436     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
437     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
438     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xFF0000;
439     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x00FF00;
440     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x0000FF;
441     ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0x0000FF;
442     ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0x0000FF;
443     ddsd.ddckCKDestBlt.dwColorSpaceLowValue = 0x000000;
444     ddsd.ddckCKDestBlt.dwColorSpaceHighValue = 0x000000;
445     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpSrc, NULL);
446     ok(rc==DD_OK || rc == DDERR_NOCOLORKEYHW,"CreateSurface returned: %x\n",rc);
447     if(FAILED(rc))
448     {
449         skip("Failed to create surface\n");
450         IDirectDrawSurface_Release(lpDst);
451         return;
452     }
453
454     memset(&fx, 0, sizeof(fx));
455     fx.dwSize = sizeof(fx);
456     fx.ddckSrcColorkey.dwColorSpaceHighValue = 0x110000;
457     fx.ddckSrcColorkey.dwColorSpaceLowValue = 0x110000;
458     fx.ddckDestColorkey.dwColorSpaceHighValue = 0x001100;
459     fx.ddckDestColorkey.dwColorSpaceLowValue = 0x001100;
460
461     rc = IDirectDrawSurface_Lock(lpSrc, NULL, &ddsd2, DDLOCK_WAIT, NULL);
462     ok(rc==DD_OK,"Lock returned: %x\n",rc);
463     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
464     lpData = ddsd2.lpSurface;
465     lpData[0] = 0x000000FF; /* Applies to src blt key in src surface */
466     lpData[1] = 0x00000000; /* Applies to dst blt key in src surface */
467     lpData[2] = 0x00FF0000; /* Dst color key in dst surface */
468     lpData[3] = 0x0000FF00; /* Src color key in dst surface */
469     lpData[4] = 0x00001100; /* Src color key in ddbltfx */
470     lpData[5] = 0x00110000; /* Dst color key in ddbltfx */
471     rc = IDirectDrawSurface_Unlock(lpSrc, NULL);
472     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
473
474     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
475     ok(rc==DD_OK,"Lock returned: %x\n",rc);
476     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
477     lpData = ddsd2.lpSurface;
478     lpData[0] = 0x55555555;
479     lpData[1] = 0x55555555;
480     lpData[2] = 0x55555555;
481     lpData[3] = 0x55555555;
482     lpData[4] = 0x55555555;
483     lpData[5] = 0x55555555;
484     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
485     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
486
487     /* Test a blit without keying */
488     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, 0, &fx);
489     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
490
491     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
492     ok(rc==DD_OK,"Lock returned: %x\n",rc);
493     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
494     lpData = ddsd2.lpSurface;
495     /* Should have copied src data unmodified to dst */
496     ok(lpData[0] == 0x000000FF &&
497        lpData[1] == 0x00000000 &&
498        lpData[2] == 0x00FF0000 &&
499        lpData[3] == 0x0000FF00 &&
500        lpData[4] == 0x00001100 &&
501        lpData[5] == 0x00110000, "Surface data after unkeyed blit does not match\n");
502
503     lpData[0] = 0x55555555;
504     lpData[1] = 0x55555555;
505     lpData[2] = 0x55555555;
506     lpData[3] = 0x55555555;
507     lpData[4] = 0x55555555;
508     lpData[5] = 0x55555555;
509     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
510     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
511
512     /* Src key */
513     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRC, &fx);
514     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
515
516     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
517     ok(rc==DD_OK,"Lock returned: %x\n",rc);
518     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
519     lpData = ddsd2.lpSurface;
520
521     ok(lpData[0] == 0x55555555 && /* Here the src key applied */
522        lpData[1] == 0x00000000 &&
523        lpData[2] == 0x00FF0000 &&
524        lpData[3] == 0x0000FF00 &&
525        lpData[4] == 0x00001100 &&
526        lpData[5] == 0x00110000, "Surface data after srckey blit does not match\n");
527
528     lpData[0] = 0x55555555;
529     lpData[1] = 0x55555555;
530     lpData[2] = 0x55555555;
531     lpData[3] = 0x55555555;
532     lpData[4] = 0x55555555;
533     lpData[5] = 0x55555555;
534     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
535     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
536
537     /* Src override */
538     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRCOVERRIDE, &fx);
539     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
540
541     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
542     ok(rc==DD_OK,"Lock returned: %x\n",rc);
543     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
544     lpData = ddsd2.lpSurface;
545
546     ok(lpData[0] == 0x000000FF &&
547        lpData[1] == 0x00000000 &&
548        lpData[2] == 0x00FF0000 &&
549        lpData[3] == 0x0000FF00 &&
550        lpData[4] == 0x00001100 &&
551        lpData[5] == 0x55555555, /* Override key applies here */
552        "Surface data after src override key blit does not match\n");
553
554     lpData[0] = 0x55555555;
555     lpData[1] = 0x55555555;
556     lpData[2] = 0x55555555;
557     lpData[3] = 0x55555555;
558     lpData[4] = 0x55555555;
559     lpData[5] = 0x55555555;
560     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
561     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
562
563     /* Src override AND src key. That is not supposed to work */
564     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRC | DDBLT_KEYSRCOVERRIDE, &fx);
565     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
566
567     /* Verify that the destination is unchanged */
568     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
569     ok(rc==DD_OK,"Lock returned: %x\n",rc);
570     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
571     lpData = ddsd2.lpSurface;
572
573     ok(lpData[0] == 0x55555555 &&
574        lpData[1] == 0x55555555 &&
575        lpData[2] == 0x55555555 &&
576        lpData[3] == 0x55555555 &&
577        lpData[4] == 0x55555555 &&
578        lpData[5] == 0x55555555, /* Override key applies here */
579        "Surface data after src key blit with override does not match\n");
580
581     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
582     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
583     lpData[2] = 0x00001100; /* Dest key in override */
584     lpData[3] = 0x00001100; /* Dest key in override */
585     lpData[4] = 0x00000000; /* Dest key in src surface */
586     lpData[5] = 0x00000000; /* Dest key in src surface */
587     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
588     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
589
590     /* Dest key blit */
591     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDEST, &fx);
592     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
593
594     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
595     ok(rc==DD_OK,"Lock returned: %x\n",rc);
596     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
597     lpData = ddsd2.lpSurface;
598
599     /* DirectDraw uses the dest blit key from the SOURCE surface ! */
600     ok(lpData[0] == 0x00ff0000 &&
601        lpData[1] == 0x00ff0000 &&
602        lpData[2] == 0x00001100 &&
603        lpData[3] == 0x00001100 &&
604        lpData[4] == 0x00001100 && /* Key applies here */
605        lpData[5] == 0x00110000,   /* Key applies here */
606        "Surface data after dest key blit does not match\n");
607
608     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
609     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
610     lpData[2] = 0x00001100; /* Dest key in override */
611     lpData[3] = 0x00001100; /* Dest key in override */
612     lpData[4] = 0x00000000; /* Dest key in src surface */
613     lpData[5] = 0x00000000; /* Dest key in src surface */
614     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
615     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
616
617     /* Dest override key blit */
618     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDESTOVERRIDE, &fx);
619     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
620
621     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
622     ok(rc==DD_OK,"Lock returned: %x\n",rc);
623     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
624     lpData = ddsd2.lpSurface;
625
626     ok(lpData[0] == 0x00FF0000 &&
627        lpData[1] == 0x00FF0000 &&
628        lpData[2] == 0x00FF0000 && /* Key applies here */
629        lpData[3] == 0x0000FF00 && /* Key applies here */
630        lpData[4] == 0x00000000 &&
631        lpData[5] == 0x00000000,
632        "Surface data after dest key override blit does not match\n");
633
634     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
635     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
636     lpData[2] = 0x00001100; /* Dest key in override */
637     lpData[3] = 0x00001100; /* Dest key in override */
638     lpData[4] = 0x00000000; /* Dest key in src surface */
639     lpData[5] = 0x00000000; /* Dest key in src surface */
640     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
641     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
642
643     /* Dest override key blit. Supposed to fail too */
644     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDEST | DDBLT_KEYDESTOVERRIDE, &fx);
645     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
646
647     /* Check for unchanged data */
648     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
649     ok(rc==DD_OK,"Lock returned: %x\n",rc);
650     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
651     lpData = ddsd2.lpSurface;
652
653     ok(lpData[0] == 0x00FF0000 &&
654        lpData[1] == 0x00FF0000 &&
655        lpData[2] == 0x00001100 && /* Key applies here */
656        lpData[3] == 0x00001100 && /* Key applies here */
657        lpData[4] == 0x00000000 &&
658        lpData[5] == 0x00000000,
659        "Surface data with dest key and dest override does not match\n");
660
661     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
662     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
663     lpData[2] = 0x00001100; /* Dest key in override */
664     lpData[3] = 0x00001100; /* Dest key in override */
665     lpData[4] = 0x00000000; /* Dest key in src surface */
666     lpData[5] = 0x00000000; /* Dest key in src surface */
667     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
668     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
669
670     /* Modify the source data a bit to give some more conclusive results */
671     rc = IDirectDrawSurface_Lock(lpSrc, NULL, &ddsd2, DDLOCK_WAIT, NULL);
672     ok(rc==DD_OK,"Lock returned: %x\n",rc);
673     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
674     lpData = ddsd2.lpSurface;
675     lpData[5] = 0x000000FF; /* Applies to src blt key in src surface */
676     rc = IDirectDrawSurface_Unlock(lpSrc, NULL);
677     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
678
679     /* Source and destination key */
680     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDEST | DDBLT_KEYSRC, &fx);
681     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
682
683     rc = IDirectDrawSurface_Lock(lpDst, NULL, &ddsd2, DDLOCK_WAIT, NULL);
684     ok(rc==DD_OK,"Lock returned: %x\n",rc);
685     ok((ddsd2.dwFlags & DDSD_LPSURFACE) == 0, "Surface desc has LPSURFACE Flags set\n");
686     lpData = ddsd2.lpSurface;
687
688     ok(lpData[0] == 0x00FF0000 && /* Masked by Destination key */
689        lpData[1] == 0x00FF0000 && /* Masked by Destination key */
690        lpData[2] == 0x00001100 && /* Masked by Destination key */
691        lpData[3] == 0x00001100 && /* Masked by Destination key */
692        lpData[4] == 0x00001100 && /* Allowed by destination key, not masked by source key */
693        lpData[5] == 0x00000000,   /* Allowed by dst key, but masked by source key */
694        "Surface data with src key and dest key blit does not match\n");
695
696     lpData[0] = 0x00FF0000; /* Dest key in dst surface */
697     lpData[1] = 0x00FF0000; /* Dest key in dst surface */
698     lpData[2] = 0x00001100; /* Dest key in override */
699     lpData[3] = 0x00001100; /* Dest key in override */
700     lpData[4] = 0x00000000; /* Dest key in src surface */
701     lpData[5] = 0x00000000; /* Dest key in src surface */
702     rc = IDirectDrawSurface_Unlock(lpDst, NULL);
703     ok(rc==DD_OK,"Unlock returned: %x\n",rc);
704
705     /* Override keys without ddbltfx parameter fail */
706     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDESTOVERRIDE, NULL);
707     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
708     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRCOVERRIDE, NULL);
709     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
710
711     /* Try blitting without keys in the source surface*/
712     rc = IDirectDrawSurface_SetColorKey(lpSrc, DDCKEY_SRCBLT, NULL);
713     ok(rc == DD_OK, "SetColorKey returned %x\n", rc);
714     rc = IDirectDrawSurface_SetColorKey(lpSrc, DDCKEY_DESTBLT, NULL);
715     ok(rc == DD_OK, "SetColorKey returned %x\n", rc);
716
717     /* That fails now. Do not bother to check that the data is unmodified */
718     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRC, &fx);
719     ok(rc == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Blt returned %08x\n", rc);
720
721     /* Dest key blit still works. Which key is used this time??? */
722     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDEST, &fx);
723     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
724
725     /* With correctly passed override keys no key in the surface is needed.
726      * Again, the result was checked before, no need to do that again
727      */
728     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYDESTOVERRIDE, &fx);
729     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
730     rc = IDirectDrawSurface_Blt(lpDst, NULL, lpSrc, NULL, DDBLT_KEYSRCOVERRIDE, &fx);
731     ok(rc == DD_OK, "IDirectDrawSurface_Blt returned %08x\n", rc);
732
733     IDirectDrawSurface_Release(lpSrc);
734     IDirectDrawSurface_Release(lpDst);
735 }
736
737 static void QueryInterface(void)
738 {
739     LPDIRECTDRAWSURFACE dsurface;
740     DDSURFACEDESC surface;
741     LPVOID object;
742     HRESULT ret;
743
744     /* Create a surface */
745     ZeroMemory(&surface, sizeof(surface));
746     surface.dwSize = sizeof(surface);
747     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
748     surface.dwHeight = 10;
749     surface.dwWidth = 10;
750     ret = IDirectDraw_CreateSurface(lpDD, &surface, &dsurface, NULL);
751     if(ret != DD_OK)
752     {
753         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
754         return;
755     }
756
757     /* Call IUnknown::QueryInterface */
758     ret = IDirectDrawSurface_QueryInterface(dsurface, 0, &object);
759     ok(ret == DDERR_INVALIDPARAMS, "IDirectDrawSurface::QueryInterface returned %x\n", ret);
760
761     IDirectDrawSurface_Release(dsurface);
762 }
763
764 /* The following tests test which interface is returned by IDirectDrawSurfaceX::GetDDInterface.
765  * It uses refcounts to test that and compares the interface addresses. Partially fits here, and
766  * partially in the refcount test
767  */
768
769 static ULONG getref(IUnknown *iface)
770 {
771     IUnknown_AddRef(iface);
772     return IUnknown_Release(iface);
773 }
774
775 static void GetDDInterface_1(void)
776 {
777     LPDIRECTDRAWSURFACE dsurface;
778     LPDIRECTDRAWSURFACE2 dsurface2;
779     DDSURFACEDESC surface;
780     HRESULT ret;
781     IDirectDraw2 *dd2;
782     IDirectDraw4 *dd4;
783     IDirectDraw7 *dd7;
784     ULONG ref1, ref2, ref4, ref7;
785     void *dd;
786
787     /* Create a surface */
788     ZeroMemory(&surface, sizeof(surface));
789     surface.dwSize = sizeof(surface);
790     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
791     surface.dwHeight = 10;
792     surface.dwWidth = 10;
793     ret = IDirectDraw_CreateSurface(lpDD, &surface, &dsurface, NULL);
794     if(ret != DD_OK)
795     {
796         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
797         return;
798     }
799     ret = IDirectDrawSurface_QueryInterface(dsurface, &IID_IDirectDrawSurface2, (void **) &dsurface2);
800     ok(ret == DD_OK, "IDirectDrawSurface_QueryInterface returned %08x\n", ret);
801     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
802     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
803     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
804     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
805     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
806     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
807
808     ref1 = getref((IUnknown *) lpDD);
809     ok(ref1 == 1, "IDirectDraw refcount is %d\n", ref1);
810     ref2 = getref((IUnknown *) dd2);
811     ok(ref2 == 1, "IDirectDraw2 refcount is %d\n", ref2);
812     ref4 = getref((IUnknown *) dd4);
813     ok(ref4 == 1, "IDirectDraw4 refcount is %d\n", ref4);
814     ref7 = getref((IUnknown *) dd7);
815     ok(ref7 == 1, "IDirectDraw7 refcount is %d\n", ref7);
816
817
818     ret = IDirectDrawSurface2_GetDDInterface(dsurface2, &dd);
819     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
820     ok(getref((IUnknown *) lpDD) == ref1 + 1, "IDirectDraw refcount was increased by %d\n", getref((IUnknown *) lpDD) - ref1);
821     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %d\n", getref((IUnknown *) dd2) - ref2);
822     ok(getref((IUnknown *) dd4) == ref4 + 0, "IDirectDraw4 refcount was increased by %d\n", getref((IUnknown *) dd4) - ref4);
823     ok(getref((IUnknown *) dd7) == ref7 + 0, "IDirectDraw7 refcount was increased by %d\n", getref((IUnknown *) dd7) - ref7);
824
825     ok(dd == lpDD, "Returned interface pointer is not equal to the creation interface\n");
826     IUnknown_Release((IUnknown *) dd);
827
828     /* try a NULL pointer */
829     ret = IDirectDrawSurface2_GetDDInterface(dsurface2, NULL);
830     ok(ret == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
831
832     IDirectDraw_Release(dd2);
833     IDirectDraw_Release(dd4);
834     IDirectDraw_Release(dd7);
835     IDirectDrawSurface2_Release(dsurface2);
836     IDirectDrawSurface_Release(dsurface);
837 }
838
839 static void GetDDInterface_2(void)
840 {
841     LPDIRECTDRAWSURFACE dsurface;
842     LPDIRECTDRAWSURFACE2 dsurface2;
843     DDSURFACEDESC surface;
844     HRESULT ret;
845     IDirectDraw2 *dd2;
846     IDirectDraw4 *dd4;
847     IDirectDraw7 *dd7;
848     ULONG ref1, ref2, ref4, ref7;
849     void *dd;
850
851     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
852     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
853     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
854     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
855     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
856     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
857
858     /* Create a surface */
859     ZeroMemory(&surface, sizeof(surface));
860     surface.dwSize = sizeof(surface);
861     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
862     surface.dwHeight = 10;
863     surface.dwWidth = 10;
864     ret = IDirectDraw2_CreateSurface(dd2, &surface, &dsurface, NULL);
865     if(ret != DD_OK)
866     {
867         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
868         return;
869     }
870     ret = IDirectDrawSurface_QueryInterface(dsurface, &IID_IDirectDrawSurface2, (void **) &dsurface2);
871     ok(ret == DD_OK, "IDirectDrawSurface_QueryInterface returned %08x\n", ret);
872
873     ref1 = getref((IUnknown *) lpDD);
874     ok(ref1 == 1, "IDirectDraw refcount is %d\n", ref1);
875     ref2 = getref((IUnknown *) dd2);
876     ok(ref2 == 1, "IDirectDraw2 refcount is %d\n", ref2);
877     ref4 = getref((IUnknown *) dd4);
878     ok(ref4 == 1, "IDirectDraw4 refcount is %d\n", ref4);
879     ref7 = getref((IUnknown *) dd7);
880     ok(ref7 == 1, "IDirectDraw7 refcount is %d\n", ref7);
881
882
883     ret = IDirectDrawSurface2_GetDDInterface(dsurface2, &dd);
884     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
885     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %d\n", getref((IUnknown *) lpDD) - ref1);
886     ok(getref((IUnknown *) dd2) == ref2 + 1, "IDirectDraw2 refcount was increased by %d\n", getref((IUnknown *) dd2) - ref2);
887     ok(getref((IUnknown *) dd4) == ref4 + 0, "IDirectDraw4 refcount was increased by %d\n", getref((IUnknown *) dd4) - ref4);
888     ok(getref((IUnknown *) dd7) == ref7 + 0, "IDirectDraw7 refcount was increased by %d\n", getref((IUnknown *) dd7) - ref7);
889
890     ok(dd == dd2, "Returned interface pointer is not equal to the creation interface\n");
891     IUnknown_Release((IUnknown *) dd);
892
893     IDirectDraw_Release(dd2);
894     IDirectDraw_Release(dd4);
895     IDirectDraw_Release(dd7);
896     IDirectDrawSurface2_Release(dsurface2);
897     IDirectDrawSurface_Release(dsurface);
898 }
899
900 static void GetDDInterface_4(void)
901 {
902     LPDIRECTDRAWSURFACE2 dsurface2;
903     LPDIRECTDRAWSURFACE4 dsurface4;
904     DDSURFACEDESC2 surface;
905     HRESULT ret;
906     IDirectDraw2 *dd2;
907     IDirectDraw4 *dd4;
908     IDirectDraw7 *dd7;
909     ULONG ref1, ref2, ref4, ref7;
910     void *dd;
911
912     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
913     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
914     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
915     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
916     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
917     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
918
919     /* Create a surface */
920     ZeroMemory(&surface, sizeof(surface));
921     surface.dwSize = sizeof(surface);
922     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
923     surface.dwHeight = 10;
924     surface.dwWidth = 10;
925     ret = IDirectDraw4_CreateSurface(dd4, &surface, &dsurface4, NULL);
926     if(ret != DD_OK)
927     {
928         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
929         return;
930     }
931     ret = IDirectDrawSurface4_QueryInterface(dsurface4, &IID_IDirectDrawSurface2, (void **) &dsurface2);
932     ok(ret == DD_OK, "IDirectDrawSurface_QueryInterface returned %08x\n", ret);
933
934     ref1 = getref((IUnknown *) lpDD);
935     ok(ref1 == 1, "IDirectDraw refcount is %d\n", ref1);
936     ref2 = getref((IUnknown *) dd2);
937     ok(ref2 == 1, "IDirectDraw2 refcount is %d\n", ref2);
938     ref4 = getref((IUnknown *) dd4);
939     ok(ref4 == 2, "IDirectDraw4 refcount is %d\n", ref4);
940     ref7 = getref((IUnknown *) dd7);
941     ok(ref7 == 1, "IDirectDraw7 refcount is %d\n", ref7);
942
943     ret = IDirectDrawSurface4_GetDDInterface(dsurface4, &dd);
944     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
945     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %d\n", getref((IUnknown *) lpDD) - ref1);
946     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %d\n", getref((IUnknown *) dd2) - ref2);
947     ok(getref((IUnknown *) dd4) == ref4 + 1, "IDirectDraw4 refcount was increased by %d\n", getref((IUnknown *) dd4) - ref4);
948     ok(getref((IUnknown *) dd7) == ref7 + 0, "IDirectDraw7 refcount was increased by %d\n", getref((IUnknown *) dd7) - ref7);
949
950     ok(dd == dd4, "Returned interface pointer is not equal to the creation interface\n");
951     IUnknown_Release((IUnknown *) dd);
952
953     /* Now test what happens if we QI the surface for some other version - It should still return the creation interface */
954     ret = IDirectDrawSurface2_GetDDInterface(dsurface2, &dd);
955     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
956     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %d\n", getref((IUnknown *) lpDD) - ref1);
957     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %d\n", getref((IUnknown *) dd2) - ref2);
958     ok(getref((IUnknown *) dd4) == ref4 + 1, "IDirectDraw4 refcount was increased by %d\n", getref((IUnknown *) dd4) - ref4);
959     ok(getref((IUnknown *) dd7) == ref7 + 0, "IDirectDraw7 refcount was increased by %d\n", getref((IUnknown *) dd7) - ref7);
960
961     ok(dd == dd4, "Returned interface pointer is not equal to the creation interface\n");
962     IUnknown_Release((IUnknown *) dd);
963
964     IDirectDraw_Release(dd2);
965     IDirectDraw_Release(dd4);
966     IDirectDraw_Release(dd7);
967     IDirectDrawSurface4_Release(dsurface4);
968     IDirectDrawSurface2_Release(dsurface2);
969 }
970
971 static void GetDDInterface_7(void)
972 {
973     LPDIRECTDRAWSURFACE4 dsurface4;
974     LPDIRECTDRAWSURFACE7 dsurface7;
975     DDSURFACEDESC2 surface;
976     HRESULT ret;
977     IDirectDraw2 *dd2;
978     IDirectDraw4 *dd4;
979     IDirectDraw7 *dd7;
980     ULONG ref1, ref2, ref4, ref7;
981     void *dd;
982
983     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
984     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
985     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
986     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
987     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
988     ok(ret == DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", ret);
989
990     /* Create a surface */
991     ZeroMemory(&surface, sizeof(surface));
992     surface.dwSize = sizeof(surface);
993     surface.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
994     surface.dwHeight = 10;
995     surface.dwWidth = 10;
996     ret = IDirectDraw7_CreateSurface(dd7, &surface, &dsurface7, NULL);
997     if(ret != DD_OK)
998     {
999         ok(FALSE, "IDirectDraw::CreateSurface failed with error %x\n", ret);
1000         return;
1001     }
1002     ret = IDirectDrawSurface7_QueryInterface(dsurface7, &IID_IDirectDrawSurface4, (void **) &dsurface4);
1003     ok(ret == DD_OK, "IDirectDrawSurface_QueryInterface returned %08x\n", ret);
1004
1005     ref1 = getref((IUnknown *) lpDD);
1006     ok(ref1 == 1, "IDirectDraw refcount is %d\n", ref1);
1007     ref2 = getref((IUnknown *) dd2);
1008     ok(ref2 == 1, "IDirectDraw2 refcount is %d\n", ref2);
1009     ref4 = getref((IUnknown *) dd4);
1010     ok(ref4 == 1, "IDirectDraw4 refcount is %d\n", ref4);
1011     ref7 = getref((IUnknown *) dd7);
1012     ok(ref7 == 2, "IDirectDraw7 refcount is %d\n", ref7);
1013
1014     ret = IDirectDrawSurface7_GetDDInterface(dsurface7, &dd);
1015     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
1016     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %d\n", getref((IUnknown *) lpDD) - ref1);
1017     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %d\n", getref((IUnknown *) dd2) - ref2);
1018     ok(getref((IUnknown *) dd4) == ref4 + 0, "IDirectDraw4 refcount was increased by %d\n", getref((IUnknown *) dd4) - ref4);
1019     ok(getref((IUnknown *) dd7) == ref7 + 1, "IDirectDraw7 refcount was increased by %d\n", getref((IUnknown *) dd7) - ref7);
1020
1021     ok(dd == dd7, "Returned interface pointer is not equal to the creation interface\n");
1022     IUnknown_Release((IUnknown *) dd);
1023
1024     /* Now test what happens if we QI the surface for some other version - It should still return the creation interface */
1025     ret = IDirectDrawSurface4_GetDDInterface(dsurface4, &dd);
1026     ok(ret == DD_OK, "IDirectDrawSurface7_GetDDInterface returned %08x\n", ret);
1027     ok(getref((IUnknown *) lpDD) == ref1 + 0, "IDirectDraw refcount was increased by %d\n", getref((IUnknown *) lpDD) - ref1);
1028     ok(getref((IUnknown *) dd2) == ref2 + 0, "IDirectDraw2 refcount was increased by %d\n", getref((IUnknown *) dd2) - ref2);
1029     ok(getref((IUnknown *) dd4) == ref4 + 0, "IDirectDraw4 refcount was increased by %d\n", getref((IUnknown *) dd4) - ref4);
1030     ok(getref((IUnknown *) dd7) == ref7 + 1, "IDirectDraw7 refcount was increased by %d\n", getref((IUnknown *) dd7) - ref7);
1031
1032     ok(dd == dd7, "Returned interface pointer is not equal to the creation interface\n");
1033     IUnknown_Release((IUnknown *) dd);
1034
1035     IDirectDraw_Release(dd2);
1036     IDirectDraw_Release(dd4);
1037     IDirectDraw_Release(dd7);
1038     IDirectDrawSurface4_Release(dsurface4);
1039     IDirectDrawSurface7_Release(dsurface7);
1040 }
1041
1042 static ULONG getRefcount(IUnknown *iface)
1043 {
1044     IUnknown_AddRef(iface);
1045     return IUnknown_Release(iface);
1046 }
1047
1048 static void IFaceRefCount(void)
1049 {
1050     LPDIRECTDRAWSURFACE surf;
1051     DDSURFACEDESC surface;
1052     HRESULT ret;
1053     IDirectDrawSurface2 *surf2;
1054     IDirectDrawSurface2 *surf2a;
1055     IDirectDrawSurface3 *surf3;
1056     IDirectDrawSurface4 *surf4;
1057     IDirectDrawSurface7 *surf7a;
1058     IDirectDrawSurface7 *surf7b;
1059     IDirect3DTexture* tex;
1060     IDirect3DTexture2* tex2;
1061     IDirectDrawGammaControl* gamma;
1062     ULONG ref;
1063
1064     /* Create a surface */
1065     ZeroMemory(&surface, sizeof(surface));
1066     surface.dwSize = sizeof(surface);
1067     surface.dwFlags = DDSD_CAPS;
1068     surface.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
1069     ret = IDirectDraw_CreateSurface(lpDD, &surface, &surf, NULL);
1070
1071     if (ret != DD_OK)
1072     {
1073         ok(FALSE, "Could not create surface, skipping test\n");
1074         return;
1075     }
1076
1077     ref = getRefcount((IUnknown *) surf);
1078     ok(ref == 1, "Refcount is %u, expected 1\n", ref); /* Check the ref count is one */
1079
1080     IDirectDrawSurface_QueryInterface(surf, &IID_IDirectDrawSurface2, (void **) &surf2);
1081     ref = getRefcount((IUnknown *) surf);
1082     ok(ref == 1, "Refcount is %u, expected 1\n", ref); /* Check the ref count is one */
1083     ref = getRefcount((IUnknown *) surf2);
1084     ok(ref == 1, "Refcount is %u, expected 1\n", ref); /* This should also be one */
1085
1086     IDirectDrawSurface_QueryInterface(surf, &IID_IDirectDrawSurface2, (void **) &surf2a);
1087     ref = getRefcount((IUnknown *) surf2);
1088     ok(ref == 2, "Refcount is %u, expected 2\n", ref);   /* Surf2's refcount should be 2 now, but surf should be 1 */
1089     ref = getRefcount((IUnknown *) surf);
1090     ok(ref == 1, "Refcount is %u, expected 1\n", ref);
1091
1092     IDirectDrawSurface_QueryInterface(surf, &IID_IDirectDrawSurface3, (void **) &surf3);
1093     ref = getRefcount((IUnknown *) surf3);
1094     ok(ref == 1, "Refcount is %u, expected 1\n", ref);
1095
1096     IDirectDrawSurface_QueryInterface(surf, &IID_IDirectDrawSurface4, (void **) &surf4);
1097     ref = getRefcount((IUnknown *) surf4);
1098     ok(ref == 1, "Refcount is %u, expected 1\n", ref);
1099
1100     IDirectDrawSurface_QueryInterface(surf, &IID_IDirectDrawSurface7, (void **) &surf7a);
1101     ref = getRefcount((IUnknown *) surf7a);
1102     ok(ref == 1, "Refcount is %u, expected 1\n", ref);
1103
1104     IDirectDrawSurface_QueryInterface(surf, &IID_IDirectDrawSurface7, (void **) &surf7b);
1105     ref = getRefcount((IUnknown *) surf7b);
1106     ok(ref == 2, "Refcount is %u, expected 2\n", ref);
1107
1108     /* IDirect3DTexture interface (unlike the others) alters the original IDirectDrawSurface ref count */
1109     ret = IDirectDrawSurface_QueryInterface(surf, &IID_IDirect3DTexture, (void **) &tex);
1110     if (SUCCEEDED(ret))
1111     {
1112         ref = getRefcount((IUnknown *) tex);
1113         ok(ref == 2, "Refcount is %u, expected 2\n", ref);
1114         ref = getRefcount((IUnknown *) surf);
1115         ok(ref == 2, "Refcount is %u, expected 2\n", ref);
1116
1117         IDirectDrawSurface_QueryInterface(surf, &IID_IDirect3DTexture2, (void **) &tex2);
1118         ref = getRefcount((IUnknown *) tex);
1119         ok(ref == 3, "Refcount is %u, expected 3\n", ref);
1120         ref = getRefcount((IUnknown *) tex2);
1121         ok(ref == 3, "Refcount is %u, expected 3\n", ref);
1122         ref = getRefcount((IUnknown *) surf);
1123         ok(ref == 3, "Refcount is %u, expected 3\n", ref);
1124
1125         IDirectDrawSurface_QueryInterface(surf, &IID_IDirectDrawGammaControl, (void **) &gamma);
1126         ref = getRefcount((IUnknown *) gamma);
1127         ok(ref == 1, "Refcount is %u, expected 1\n", ref);
1128
1129         ref = IDirect3DTexture2_Release(tex2); /* Release the texture */
1130         ok(ref == 2, "Refcount is %u, expected 2\n", ref);
1131         ref = getRefcount((IUnknown *) surf);
1132         ok(ref == 2, "Refcount is %u, expected 2\n", ref);
1133
1134         ref = IDirect3DTexture_Release(tex); /* Release the texture */
1135         ok(ref == 1, "Refcount is %u, expected 1\n", ref);
1136         ref = getRefcount((IUnknown *) surf);
1137         ok(ref == 1, "Refcount is %u, expected 1\n", ref);
1138
1139         ref = IDirectDrawGammaControl_Release(gamma); /* Release the gamma control */
1140         ok(ref == 0, "Refcount is %u, expected 0\n", ref);
1141     }
1142
1143     ref = IDirectDrawSurface2_Release(surf2); /* Release one of the 2 surf2 interfaces */
1144     ok(ref == 1, "Refcount is %u, expected 1\n", ref);
1145
1146     ref = IDirectDrawSurface2_Release(surf2a); /* Release the other */
1147     ok(ref == 0, "Refcount is %u, expected 0\n", ref);
1148
1149     ref = IDirectDrawSurface3_Release(surf3);
1150     ok(ref == 0, "Refcount is %u, expected 0\n", ref);
1151
1152     ref = IDirectDrawSurface4_Release(surf4);
1153     ok(ref == 0, "Refcount is %u, expected 0\n", ref);
1154
1155     ref = IDirectDrawSurface7_Release(surf7a);
1156     ok(ref == 1, "Refcount is %u, expected 1\n", ref);
1157
1158     ref = IDirectDrawSurface7_Release(surf7b);
1159     ok(ref == 0, "Refcount is %u, expected 0\n", ref);
1160
1161     ref = IDirectDrawSurface_Release(surf);
1162     ok(ref == 0, "Refcount is %u, expected 0\n", ref);
1163 }
1164
1165 #define MAXEXPECTED 8  /* Can match up to 8 expected surfaces */
1166 struct enumstruct
1167 {
1168     IDirectDrawSurface *expected[MAXEXPECTED];
1169     UINT count;
1170 };
1171
1172 static HRESULT WINAPI enumCB(IDirectDrawSurface *surf, DDSURFACEDESC *desc, void *ctx)
1173 {
1174     int i;
1175     BOOL found = FALSE;
1176
1177     for(i = 0; i < MAXEXPECTED; i++)
1178     {
1179         if(((struct enumstruct *)ctx)->expected[i] == surf) found = TRUE;
1180     }
1181
1182     ok(found, "Unexpected surface %p enumerated\n", surf);
1183     ((struct enumstruct *)ctx)->count++;
1184     IDirectDrawSurface_Release(surf);
1185     return DDENUMRET_OK;
1186 }
1187
1188 static void EnumTest(void)
1189 {
1190     HRESULT rc;
1191     DDSURFACEDESC ddsd;
1192     IDirectDrawSurface *surface;
1193     struct enumstruct ctx;
1194
1195     ddsd.dwSize = sizeof(ddsd);
1196     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
1197     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1198     U2(ddsd).dwMipMapCount = 3;
1199     ddsd.dwWidth = 32;
1200     ddsd.dwHeight = 32;
1201     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface, NULL);
1202     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
1203
1204     memset(&ctx, 0, sizeof(ctx));
1205     ctx.expected[0] = surface;
1206     rc = IDirectDrawSurface_GetAttachedSurface(ctx.expected[0], &ddsd.ddsCaps, &ctx.expected[1]);
1207     ok(rc == DD_OK, "GetAttachedSurface returned %08x\n", rc);
1208     rc = IDirectDrawSurface_GetAttachedSurface(ctx.expected[1], &ddsd.ddsCaps, &ctx.expected[2]);
1209     ok(rc == DD_OK, "GetAttachedSurface returned %08x\n", rc);
1210     rc = IDirectDrawSurface_GetAttachedSurface(ctx.expected[2], &ddsd.ddsCaps, &ctx.expected[3]);
1211     ok(rc == DDERR_NOTFOUND, "GetAttachedSurface returned %08x\n", rc);
1212     ok(!ctx.expected[3], "expected NULL pointer\n");
1213     ctx.count = 0;
1214
1215     rc = IDirectDraw_EnumSurfaces(lpDD, DDENUMSURFACES_DOESEXIST | DDENUMSURFACES_ALL, &ddsd, &ctx, enumCB);
1216     ok(rc == DD_OK, "IDirectDraw_EnumSurfaces returned %08x\n", rc);
1217     ok(ctx.count == 3, "%d surfaces enumerated, expected 3\n", ctx.count);
1218
1219     ctx.count = 0;
1220     rc = IDirectDraw_EnumSurfaces(lpDD, DDENUMSURFACES_DOESEXIST | DDENUMSURFACES_ALL, NULL, &ctx, enumCB);
1221     ok(rc == DD_OK, "IDirectDraw_EnumSurfaces returned %08x\n", rc);
1222     ok(ctx.count == 3, "%d surfaces enumerated, expected 3\n", ctx.count);
1223
1224     IDirectDrawSurface_Release(ctx.expected[2]);
1225     IDirectDrawSurface_Release(ctx.expected[1]);
1226     IDirectDrawSurface_Release(surface);
1227 }
1228
1229 static HRESULT WINAPI SurfaceCounter(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
1230 {
1231     UINT *num = context;
1232     (*num)++;
1233     IDirectDrawSurface_Release(surface);
1234     return DDENUMRET_OK;
1235 }
1236
1237 static void AttachmentTest7(void)
1238 {
1239     HRESULT hr;
1240     IDirectDraw7 *dd7;
1241     IDirectDrawSurface7 *surface1, *surface2, *surface3, *surface4;
1242     DDSURFACEDESC2 ddsd, ddsd2;
1243     UINT num;
1244     DDSCAPS2 caps = {DDSCAPS_TEXTURE, 0, 0, 0}, caps2 = {DDSCAPS_BACKBUFFER,0,0,0};
1245     HWND window = CreateWindow( "static", "ddraw_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1246
1247     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
1248     ok(hr == DD_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
1249
1250     memset(&ddsd, 0, sizeof(ddsd));
1251     ddsd.dwSize = sizeof(ddsd);
1252     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
1253     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1254     U2(ddsd).dwMipMapCount = 3; /* Will create 128x128, 64x64, 32x32 */
1255     ddsd.dwWidth = 128;
1256     ddsd.dwHeight = 128;
1257     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface1, NULL);
1258     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1259
1260     /* ROOT */
1261     num = 0;
1262     IDirectDrawSurface7_EnumAttachedSurfaces(surface1, &num, SurfaceCounter);
1263     ok(num == 1, "Mipmap root has %d surfaces attached, expected 1\n", num);
1264     /* DONE ROOT */
1265
1266     /* LEVEL 1 */
1267     hr = IDirectDrawSurface7_GetAttachedSurface(surface1, &caps, &surface2);
1268     ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
1269     num = 0;
1270     IDirectDrawSurface7_EnumAttachedSurfaces(surface2, &num, SurfaceCounter);
1271     ok(num == 1, "First mip level has %d surfaces attached, expected 1\n", num);
1272     /* DONE LEVEL 1 */
1273
1274     /* LEVEL 2 */
1275     hr = IDirectDrawSurface7_GetAttachedSurface(surface2, &caps, &surface3);
1276     ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
1277     IDirectDrawSurface7_Release(surface2);
1278     num = 0;
1279     IDirectDrawSurface7_EnumAttachedSurfaces(surface3, &num, SurfaceCounter);
1280     ok(num == 0, "Second mip level has %d surfaces attached, expected 1\n", num);
1281     /* Done level 2 */
1282     /* Mip level 3 is still needed */
1283     hr = IDirectDrawSurface7_GetAttachedSurface(surface3, &caps, &surface4);
1284     ok(hr == DDERR_NOTFOUND, "GetAttachedSurface returned %08x\n", hr);
1285     ok(!surface4, "expected NULL pointer\n");
1286
1287     /* Try to attach a 16x16 miplevel - Should not work as far I can see */
1288     memset(&ddsd, 0, sizeof(ddsd));
1289     ddsd.dwSize = sizeof(ddsd);
1290     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1291     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1292     ddsd.dwWidth = 16;
1293     ddsd.dwHeight = 16;
1294     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface2, NULL);
1295     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1296
1297     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface2);
1298     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 128x128 texture root returned %08x\n", hr);
1299     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface1);
1300     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 128x128 texture root to a 16x16 texture returned %08x\n", hr);
1301     hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface2);
1302     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 32x32 texture mip level returned %08x\n", hr);
1303     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface3);
1304     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 32x32 texture mip level to a 16x16 surface returned %08x\n", hr);
1305
1306     IDirectDrawSurface7_Release(surface2);
1307
1308     memset(&ddsd, 0, sizeof(ddsd));
1309     ddsd.dwSize = sizeof(ddsd);
1310     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1311     ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
1312     ddsd.dwWidth = 16;
1313     ddsd.dwHeight = 16;
1314     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface2, NULL);
1315     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1316
1317     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface2);
1318     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 128x128 texture root returned %08x\n", hr);
1319     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface1);
1320     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 128x128 texture root to a 16x16 offscreen plain surface returned %08x\n", hr);
1321     hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface2);
1322     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 32x32 texture mip level returned %08x\n", hr);
1323     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface3);
1324     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 32x32 texture mip level to a 16x16 offscreen plain surface returned %08x\n", hr);
1325
1326     IDirectDrawSurface7_Release(surface3);
1327     IDirectDrawSurface7_Release(surface2);
1328     IDirectDrawSurface7_Release(surface1);
1329
1330     hr = IDirectDraw7_SetCooperativeLevel(dd7, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1331     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
1332
1333     memset(&ddsd, 0, sizeof(ddsd));
1334     ddsd.dwSize = sizeof(ddsd);
1335     ddsd.dwFlags = DDSD_BACKBUFFERCOUNT | DDSD_CAPS;
1336     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
1337     ddsd.dwBackBufferCount = 2;
1338     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface1, NULL);
1339     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1340
1341     /* backbuffer surfaces must not have dwBackBufferCount set */
1342     ddsd2.dwSize = sizeof(ddsd2);
1343     hr = IDirectDrawSurface7_GetAttachedSurface(surface1, &caps2, &surface2);
1344     ok(hr==DD_OK,"GetAttachedSurface returned: %x\n", hr);
1345     hr = IDirectDrawSurface7_GetSurfaceDesc(surface2, &ddsd2);
1346     ok(hr==DD_OK,"GetSurfaceDesc returned: %x\n", hr);
1347     ok(ddsd2.dwBackBufferCount==0,"backbuffer surface has dwBackBufferCount==%u\n", ddsd2.dwBackBufferCount);
1348
1349     num = 0;
1350     IDirectDrawSurface7_EnumAttachedSurfaces(surface1, &num, SurfaceCounter);
1351     ok(num == 1, "Primary surface has %d surfaces attached, expected 1\n", num);
1352     IDirectDrawSurface7_Release(surface1);
1353
1354     /* Those are some invalid descriptions, no need to test attachments with them */
1355     memset(&ddsd, 0, sizeof(ddsd));
1356     ddsd.dwSize = sizeof(ddsd);
1357     ddsd.dwFlags = DDSD_CAPS;
1358     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER;
1359     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface1, NULL);
1360     ok(hr==DDERR_INVALIDCAPS,"CreateSurface returned: %x\n",hr);
1361     memset(&ddsd, 0, sizeof(ddsd));
1362     ddsd.dwSize = sizeof(ddsd);
1363     ddsd.dwFlags = DDSD_CAPS;
1364     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER;
1365     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface2, NULL);
1366     ok(hr==DDERR_INVALIDCAPS,"CreateSurface returned: %x\n",hr);
1367
1368     /* Try a single primary and two offscreen plain surfaces */
1369     memset(&ddsd, 0, sizeof(ddsd));
1370     ddsd.dwSize = sizeof(ddsd);
1371     ddsd.dwFlags = DDSD_CAPS;
1372     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
1373     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface1, NULL);
1374     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1375
1376     memset(&ddsd, 0, sizeof(ddsd));
1377     ddsd.dwSize = sizeof(ddsd);
1378     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1379     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1380     ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
1381     ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
1382     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface2, NULL);
1383     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1384
1385     memset(&ddsd, 0, sizeof(ddsd));
1386     ddsd.dwSize = sizeof(ddsd);
1387     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1388     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1389     ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
1390     ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
1391     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface3, NULL);
1392     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1393
1394     /* This one has a different size */
1395     memset(&ddsd, 0, sizeof(ddsd));
1396     ddsd.dwSize = sizeof(ddsd);
1397     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1398     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1399     ddsd.dwWidth = 128;
1400     ddsd.dwHeight = 128;
1401     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface4, NULL);
1402     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1403
1404     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface2);
1405     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching an offscreen plain surface to a front buffer returned %08x\n", hr);
1406     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface1);
1407     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a front buffer to an offscreen plain surface returned %08x\n", hr);
1408     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface3);
1409     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching an offscreen plain surface to another offscreen plain surface returned %08x\n", hr);
1410     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface4);
1411     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching an offscreen plain surface to a front buffer of different size returned %08x\n", hr);
1412     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface1);
1413     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a front buffer to an offscreen plain surface of different size returned %08x\n", hr);
1414
1415     IDirectDrawSurface7_Release(surface4);
1416     IDirectDrawSurface7_Release(surface3);
1417     IDirectDrawSurface7_Release(surface2);
1418     IDirectDrawSurface7_Release(surface1);
1419
1420     hr =IDirectDraw7_SetCooperativeLevel(dd7, NULL, DDSCL_NORMAL);
1421     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
1422     IDirectDraw7_Release(dd7);
1423 }
1424
1425 static void AttachmentTest(void)
1426 {
1427     HRESULT hr;
1428     IDirectDrawSurface *surface1, *surface2, *surface3, *surface4;
1429     DDSURFACEDESC ddsd;
1430     DDSCAPS caps = {DDSCAPS_TEXTURE};
1431     BOOL refrast = FALSE;
1432     HWND window = CreateWindow( "static", "ddraw_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
1433
1434     memset(&ddsd, 0, sizeof(ddsd));
1435     ddsd.dwSize = sizeof(ddsd);
1436     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
1437     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1438     U2(ddsd).dwMipMapCount = 3; /* Will create 128x128, 64x64, 32x32 */
1439     ddsd.dwWidth = 128;
1440     ddsd.dwHeight = 128;
1441     hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &surface1, NULL);
1442     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1443
1444     hr = IDirectDrawSurface7_GetAttachedSurface(surface1, &caps, &surface2);
1445     ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
1446     hr = IDirectDrawSurface7_GetAttachedSurface(surface2, &caps, &surface3);
1447     ok(hr == DD_OK, "GetAttachedSurface returned %08x\n", hr);
1448
1449     /* Try to attach a 16x16 miplevel - Should not work as far I can see */
1450     memset(&ddsd, 0, sizeof(ddsd));
1451     ddsd.dwSize = sizeof(ddsd);
1452     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1453     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1454     ddsd.dwWidth = 16;
1455     ddsd.dwHeight = 16;
1456     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface4, NULL);
1457     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1458
1459     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface4);
1460     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 128x128 texture root returned %08x\n", hr);
1461     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface1);
1462     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 128x128 texture root to a 16x16 texture returned %08x\n", hr);
1463     hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface4);
1464     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 32x32 texture mip level returned %08x\n", hr);
1465     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface3);
1466     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 32x32 texture mip level to a 16x16 surface returned %08x\n", hr);
1467     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface4);
1468     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 surface to a 64x64 texture sublevel returned %08x\n", hr);
1469     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface2);
1470     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 64x64 texture sublevel to a 16x16 texture returned %08x\n", hr);
1471
1472     IDirectDrawSurface7_Release(surface4);
1473
1474     memset(&ddsd, 0, sizeof(ddsd));
1475     ddsd.dwSize = sizeof(ddsd);
1476     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1477     ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
1478     ddsd.dwWidth = 16;
1479     ddsd.dwHeight = 16;
1480     hr = IDirectDraw7_CreateSurface(lpDD, &ddsd, &surface4, NULL);
1481     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1482
1483     if (SUCCEEDED(IDirectDrawSurface7_AddAttachedSurface(surface1, surface4)))
1484     {
1485         IDirectDrawSurface7_DeleteAttachedSurface(surface1, 0, surface4);
1486         refrast = TRUE;
1487     }
1488
1489     hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface4); /* Succeeds on refrast */
1490     if (refrast)
1491         ok(hr == S_OK, "Attaching a 16x16 offscreen plain surface to a 128x128 texture root returned %08x\n", hr);
1492     else
1493         ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 128x128 texture root returned %08x\n", hr);
1494     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface1, 0, surface4);
1495
1496     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface1);  /* Succeeds on refrast */
1497     if (refrast)
1498         ok(hr == S_OK, "Attaching a 128x128 texture root to a 16x16 offscreen plain surface returned %08x\n", hr);
1499     else
1500         ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 128x128 texture root to a 16x16 offscreen plain surface returned %08x\n", hr);
1501     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface1, 0, surface1);
1502
1503     hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface4);  /* Succeeds on refrast */
1504     if (refrast)
1505         ok(hr == S_OK, "Attaching a 16x16 offscreen plain surface to a 32x32 texture mip level returned %08x\n", hr);
1506     else
1507         ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 32x32 texture mip level returned %08x\n", hr);
1508     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface3, 0, surface4);
1509
1510     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface3);
1511     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 32x32 texture mip level to a 16x16 offscreen plain surface returned %08x\n", hr);
1512     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface4, 0, surface3);
1513
1514     hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface4);  /* Succeeds on refrast */
1515     if (refrast)
1516         ok(hr == S_OK, "Attaching a 16x16 offscreen plain surface to a 64x64 texture sublevel returned %08x\n", hr);
1517     else
1518         ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 16x16 offscreen plain surface to a 64x64 texture sublevel returned %08x\n", hr);
1519     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface2, 0, surface4);
1520
1521     hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface2);
1522     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a 64x64 texture sublevel to a 16x16 offscreen plain surface returned %08x\n", hr);
1523     if(SUCCEEDED(hr)) IDirectDrawSurface7_DeleteAttachedSurface(surface4, 0, surface2);
1524
1525     IDirectDrawSurface7_Release(surface4);
1526     IDirectDrawSurface7_Release(surface3);
1527     IDirectDrawSurface7_Release(surface2);
1528     IDirectDrawSurface7_Release(surface1);
1529
1530     hr = IDirectDraw_SetCooperativeLevel(lpDD, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1531     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
1532
1533     /* Creating a back buffer as-is, is not allowed. No need to perform attachment tests */
1534     memset(&ddsd, 0, sizeof(ddsd));
1535     ddsd.dwSize = sizeof(ddsd);
1536     ddsd.dwFlags = DDSD_CAPS;
1537     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER;
1538     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface2, NULL);
1539     ok(hr==DDERR_INVALIDCAPS,"CreateSurface returned: %x\n",hr);
1540     /* This old ddraw version happily creates explicit front buffers */
1541     memset(&ddsd, 0, sizeof(ddsd));
1542     ddsd.dwSize = sizeof(ddsd);
1543     ddsd.dwFlags = DDSD_CAPS;
1544     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER;
1545     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface1, NULL);
1546     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1547     IDirectDrawSurface_Release(surface1);
1548
1549     /* Try a single primary and two offscreen plain surfaces */
1550     memset(&ddsd, 0, sizeof(ddsd));
1551     ddsd.dwSize = sizeof(ddsd);
1552     ddsd.dwFlags = DDSD_CAPS;
1553     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
1554     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface1, NULL);
1555     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1556
1557     memset(&ddsd, 0, sizeof(ddsd));
1558     ddsd.dwSize = sizeof(ddsd);
1559     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1560     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1561     ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
1562     ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
1563     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface2, NULL);
1564     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1565
1566     memset(&ddsd, 0, sizeof(ddsd));
1567     ddsd.dwSize = sizeof(ddsd);
1568     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1569     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1570     ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
1571     ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
1572     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface3, NULL);
1573     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1574
1575     /* This one has a different size */
1576     memset(&ddsd, 0, sizeof(ddsd));
1577     ddsd.dwSize = sizeof(ddsd);
1578     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1579     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
1580     ddsd.dwWidth = 128;
1581     ddsd.dwHeight = 128;
1582     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface4, NULL);
1583     ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
1584
1585     hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
1586     ok(hr == DD_OK || broken(hr == DDERR_CANNOTATTACHSURFACE),
1587        "Attaching an offscreen plain surface to a front buffer returned %08x\n", hr);
1588     if(SUCCEEDED(hr))
1589     {
1590         /* Try the reverse without detaching first */
1591         hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
1592         ok(hr == DDERR_SURFACEALREADYATTACHED, "Attaching an attached surface to its attachee returned %08x\n", hr);
1593         hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
1594         ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
1595     }
1596     hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
1597     ok(hr == DD_OK || broken(hr == DDERR_CANNOTATTACHSURFACE),
1598        "Attaching a front buffer to an offscreen plain surface returned %08x\n", hr);
1599     if(SUCCEEDED(hr))
1600     {
1601         /* Try to detach reversed */
1602         hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
1603         ok(hr == DDERR_CANNOTDETACHSURFACE, "DeleteAttachedSurface returned %08x\n", hr);
1604         /* Now the proper detach */
1605         hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface1);
1606         ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
1607     }
1608     hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface3); /* Fails on refrast */
1609     ok(hr == DD_OK || broken(hr == DDERR_CANNOTATTACHSURFACE),
1610        "Attaching an offscreen plain surface to another offscreen plain surface returned %08x\n", hr);
1611     if(SUCCEEDED(hr))
1612     {
1613         hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface3);
1614         ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
1615     }
1616     hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4);
1617     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching an offscreen plain surface to a front buffer of different size returned %08x\n", hr);
1618     hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
1619     ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a front buffer to an offscreen plain surface of different size returned %08x\n", hr);
1620
1621     IDirectDrawSurface_Release(surface4);
1622     IDirectDrawSurface_Release(surface3);
1623     IDirectDrawSurface_Release(surface2);
1624     IDirectDrawSurface_Release(surface1);
1625
1626     hr =IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
1627     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
1628
1629     DestroyWindow(window);
1630 }
1631
1632 struct compare
1633 {
1634     DWORD width, height;
1635     DWORD caps, caps2;
1636     UINT mips;
1637 };
1638
1639 static HRESULT WINAPI CubeTestPaletteEnum(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
1640 {
1641     HRESULT hr;
1642
1643     hr = IDirectDrawSurface7_SetPalette(surface, context);
1644     if (desc->dwWidth == 64) /* This is for first mimpmap */
1645         ok(hr == DDERR_NOTONMIPMAPSUBLEVEL, "SetPalette returned: %x\n",hr);
1646     else
1647         ok(hr == DD_OK, "SetPalette returned: %x\n",hr);
1648
1649     IDirectDrawSurface7_Release(surface);
1650
1651     return DDENUMRET_OK;
1652 }
1653
1654 static HRESULT WINAPI CubeTestLvl2Enum(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
1655 {
1656     UINT *mips = context;
1657
1658     (*mips)++;
1659     IDirectDrawSurface7_EnumAttachedSurfaces(surface,
1660                                              context,
1661                                              CubeTestLvl2Enum);
1662
1663     return DDENUMRET_OK;
1664 }
1665
1666 static HRESULT WINAPI CubeTestLvl1Enum(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
1667 {
1668     UINT mips = 0;
1669     UINT *num = context;
1670     static const struct compare expected[] =
1671     {
1672         {
1673             128, 128,
1674             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1675             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ,
1676             7
1677         },
1678         {
1679             128, 128,
1680             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1681             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ,
1682             7
1683         },
1684         {
1685             128, 128,
1686             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1687             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY,
1688             7
1689         },
1690         {
1691             128, 128,
1692             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1693             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY,
1694             7
1695         },
1696         {
1697             128, 128,
1698             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1699             DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX,
1700             7
1701         },
1702         {
1703             64, 64, /* This is the first mipmap */
1704             DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX,
1705             DDSCAPS2_MIPMAPSUBLEVEL | DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX,
1706             6
1707         },
1708     };
1709
1710     mips = 0;
1711     IDirectDrawSurface7_EnumAttachedSurfaces(surface,
1712                                              &mips,
1713                                              CubeTestLvl2Enum);
1714
1715     ok(desc->dwWidth == expected[*num].width, "Surface width is %d expected %d\n", desc->dwWidth, expected[*num].width);
1716     ok(desc->dwHeight == expected[*num].height, "Surface height is %d expected %d\n", desc->dwHeight, expected[*num].height);
1717     ok(desc->ddsCaps.dwCaps == expected[*num].caps, "Surface caps are %08x expected %08x\n", desc->ddsCaps.dwCaps, expected[*num].caps);
1718     ok(desc->ddsCaps.dwCaps2 == expected[*num].caps2, "Surface caps2 are %08x expected %08x\n", desc->ddsCaps.dwCaps2, expected[*num].caps2);
1719     ok(mips == expected[*num].mips, "Surface has %d mipmaps, expected %d\n", mips, expected[*num].mips);
1720
1721     (*num)++;
1722
1723     IDirectDrawSurface7_Release(surface);
1724
1725     return DDENUMRET_OK;
1726 }
1727
1728 static void CubeMapTest(void)
1729 {
1730     IDirectDraw7 *dd7 = NULL;
1731     IDirectDrawSurface7 *cubemap = NULL;
1732     IDirectDrawPalette *palette = NULL;
1733     DDSURFACEDESC2 ddsd;
1734     HRESULT hr;
1735     PALETTEENTRY Table[256];
1736     int i;
1737     UINT num = 0;
1738     UINT ref;
1739     struct enumstruct ctx;
1740
1741     for(i=0; i<256; i++)
1742     {
1743         Table[i].peRed   = 0xff;
1744         Table[i].peGreen = 0;
1745         Table[i].peBlue  = 0;
1746         Table[i].peFlags = 0;
1747     }
1748
1749     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
1750     ok(hr == DD_OK, "IDirectDraw::QueryInterface returned %08x\n", hr);
1751     if (FAILED(hr)) goto err;
1752
1753     memset(&ddsd, 0, sizeof(ddsd));
1754     ddsd.dwSize = sizeof(ddsd);
1755     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1756     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1757     ddsd.dwWidth = 128;
1758     ddsd.dwHeight = 128;
1759     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_SYSTEMMEMORY;
1760     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES;
1761
1762     /* D3DFMT_R5G6B5 */
1763     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
1764     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 16;
1765     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0xF800;
1766     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x07E0;
1767     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x001F;
1768
1769     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &cubemap, NULL);
1770     if (FAILED(hr))
1771     {
1772         skip("Can't create cubemap surface\n");
1773         goto err;
1774     }
1775
1776     hr = IDirectDrawSurface7_GetSurfaceDesc(cubemap, &ddsd);
1777     ok(hr == DD_OK, "IDirectDrawSurface7_GetSurfaceDesc returned %08x\n", hr);
1778     ok(ddsd.ddsCaps.dwCaps == (DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX),
1779        "Root Caps are %08x\n", ddsd.ddsCaps.dwCaps);
1780     ok(ddsd.ddsCaps.dwCaps2 == (DDSCAPS2_CUBEMAP_POSITIVEX | DDSCAPS2_CUBEMAP),
1781        "Root Caps2 are %08x\n", ddsd.ddsCaps.dwCaps2);
1782
1783     IDirectDrawSurface7_EnumAttachedSurfaces(cubemap,
1784                                              &num,
1785                                              CubeTestLvl1Enum);
1786     ok(num == 6, "Surface has %d attachments\n", num);
1787     IDirectDrawSurface7_Release(cubemap);
1788
1789     /* What happens if I do not specify any faces? */
1790     memset(&ddsd, 0, sizeof(ddsd));
1791     ddsd.dwSize = sizeof(ddsd);
1792     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1793     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1794     ddsd.dwWidth = 128;
1795     ddsd.dwHeight = 128;
1796     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_SYSTEMMEMORY;
1797     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP;
1798
1799     /* D3DFMT_R5G6B5 */
1800     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
1801     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 16;
1802     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0xF800;
1803     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x07E0;
1804     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x001F;
1805
1806     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &cubemap, NULL);
1807     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw7::CreateSurface asking for a cube map without faces returned %08x\n", hr);
1808
1809     /* Cube map faces without a cube map? */
1810     memset(&ddsd, 0, sizeof(ddsd));
1811     ddsd.dwSize = sizeof(ddsd);
1812     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1813     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1814     ddsd.dwWidth = 128;
1815     ddsd.dwHeight = 128;
1816     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_SYSTEMMEMORY;
1817     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP_ALLFACES;
1818
1819     /* D3DFMT_R5G6B5 */
1820     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
1821     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 16;
1822     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0xF800;
1823     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x07E0;
1824     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x001F;
1825
1826     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &cubemap, NULL);
1827     ok(hr == DDERR_INVALIDCAPS, "IDirectDraw7::CreateSurface returned %08x\n", hr);
1828
1829     memset(&ddsd, 0, sizeof(ddsd));
1830     ddsd.dwSize = sizeof(ddsd);
1831     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1832     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1833     ddsd.dwWidth = 128;
1834     ddsd.dwHeight = 128;
1835     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_SYSTEMMEMORY;
1836     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP_POSITIVEX;
1837
1838     /* D3DFMT_R5G6B5 */
1839     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
1840     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 16;
1841     U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0xF800;
1842     U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x07E0;
1843     U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x001F;
1844
1845     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &cubemap, NULL);
1846     ok(hr == DDERR_INVALIDCAPS, "IDirectDraw7::CreateSurface returned %08x\n", hr);
1847
1848     memset(&ddsd, 0, sizeof(ddsd));
1849     ddsd.dwSize = sizeof(ddsd);
1850     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
1851     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
1852     ddsd.dwWidth = 128;
1853     ddsd.dwHeight = 128;
1854     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_SYSTEMMEMORY;
1855     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES;
1856
1857     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_PALETTEINDEXED8;
1858     U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 8;
1859
1860     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &cubemap, NULL);
1861     if (FAILED(hr))
1862     {
1863         skip("Can't create palletized cubemap surface\n");
1864         goto err;
1865     }
1866
1867     hr = IDirectDraw7_CreatePalette(dd7, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
1868     ok(hr == DD_OK, "CreatePalette failed with %08x\n", hr);
1869
1870     hr = IDirectDrawSurface7_EnumAttachedSurfaces(cubemap, palette, CubeTestPaletteEnum);
1871     ok(hr == DD_OK, "EnumAttachedSurfaces failed\n");
1872
1873     ref = getRefcount((IUnknown *) palette);
1874     ok(ref == 6, "Refcount is %u, expected 1\n", ref);
1875
1876     IDirectDrawSurface7_Release(cubemap);
1877
1878     ref = getRefcount((IUnknown *) palette);
1879     todo_wine ok(ref == 1, "Refcount is %u, expected 1\n", ref);
1880
1881     IDirectDrawPalette_Release(palette);
1882
1883     /* Make sure everything is cleaned up properly. Use the enumSurfaces test infrastructure */
1884     memset(&ctx, 0, sizeof(ctx));
1885     memset(&ddsd, 0, sizeof(ddsd));
1886     ddsd.dwSize = sizeof(DDSURFACEDESC);
1887     hr = IDirectDraw_EnumSurfaces(lpDD, DDENUMSURFACES_DOESEXIST | DDENUMSURFACES_ALL, (DDSURFACEDESC *) &ddsd, (void *) &ctx, enumCB);
1888     ok(hr == DD_OK, "IDirectDraw_EnumSurfaces returned %08x\n", hr);
1889     ok(ctx.count == 0, "%d surfaces enumerated, expected 0\n", ctx.count);
1890
1891     err:
1892     if (dd7) IDirectDraw7_Release(dd7);
1893 }
1894
1895 static void test_lockrect_invalid(void)
1896 {
1897     unsigned int i, j;
1898
1899     RECT valid[] = {
1900         {60, 60, 68, 68},
1901         {60, 60, 60, 68},
1902         {60, 60, 68, 60},
1903         {120, 60, 128, 68},
1904         {60, 120, 68, 128},
1905     };
1906
1907     RECT invalid[] = {
1908         {68, 60, 60, 68},       /* left > right */
1909         {60, 68, 68, 60},       /* top > bottom */
1910         {-8, 60,  0, 68},       /* left < surface */
1911         {60, -8, 68,  0},       /* top < surface */
1912         {-16, 60, -8, 68},      /* right < surface */
1913         {60, -16, 68, -8},      /* bottom < surface */
1914         {60, 60, 136, 68},      /* right > surface */
1915         {60, 60, 68, 136},      /* bottom > surface */
1916         {136, 60, 144, 68},     /* left > surface */
1917         {60, 136, 68, 144},     /* top > surface */
1918     };
1919
1920     const DWORD dds_caps[] = {
1921         DDSCAPS_OFFSCREENPLAIN
1922     };
1923
1924     for (j = 0; j < (sizeof(dds_caps) / sizeof(*dds_caps)); ++j)
1925     {
1926         IDirectDrawSurface *surface = 0;
1927         DDSURFACEDESC surface_desc = {0};
1928         DDSURFACEDESC locked_desc = {0};
1929         HRESULT hr;
1930
1931         surface_desc.dwSize = sizeof(surface_desc);
1932         surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
1933         surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
1934         surface_desc.ddsCaps.dwCaps = dds_caps[j];
1935         surface_desc.dwWidth = 128;
1936         surface_desc.dwHeight = 128;
1937         surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
1938         U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
1939         U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0xFF0000;
1940         U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x00FF00;
1941         U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x0000FF;
1942
1943         hr = IDirectDraw_CreateSurface(lpDD, &surface_desc, &surface, NULL);
1944         ok(SUCCEEDED(hr), "CreateSurface failed (0x%08x)\n", hr);
1945         if (FAILED(hr))
1946         {
1947             skip("failed to create surface\n");
1948             continue;
1949         }
1950
1951         hr = IDirectDrawSurface_Lock(surface, NULL, NULL, DDLOCK_WAIT, NULL);
1952         ok(hr == DDERR_INVALIDPARAMS, "Lock returned 0x%08x for NULL DDSURFACEDESC,"
1953                 " expected DDERR_INVALIDPARAMS (0x%08x)\n", hr, DDERR_INVALIDPARAMS);
1954
1955         for (i = 0; i < (sizeof(valid) / sizeof(*valid)); ++i)
1956         {
1957             RECT *rect = &valid[i];
1958
1959             memset(&locked_desc, 0, sizeof(locked_desc));
1960             locked_desc.dwSize = sizeof(locked_desc);
1961
1962             hr = IDirectDrawSurface_Lock(surface, rect, &locked_desc, DDLOCK_WAIT, NULL);
1963             ok(SUCCEEDED(hr), "Lock failed (0x%08x) for rect [%d, %d]->[%d, %d]\n",
1964                     hr, rect->left, rect->top, rect->right, rect->bottom);
1965
1966             hr = IDirectDrawSurface_Unlock(surface, NULL);
1967             ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1968         }
1969
1970         for (i = 0; i < (sizeof(invalid) / sizeof(*invalid)); ++i)
1971         {
1972             RECT *rect = &invalid[i];
1973
1974             memset(&locked_desc, 1, sizeof(locked_desc));
1975             locked_desc.dwSize = sizeof(locked_desc);
1976
1977             hr = IDirectDrawSurface_Lock(surface, rect, &locked_desc, DDLOCK_WAIT, NULL);
1978             ok(hr == DDERR_INVALIDPARAMS, "Lock returned 0x%08x for rect [%d, %d]->[%d, %d]"
1979                     ", expected DDERR_INVALIDPARAMS (0x%08x)\n", hr, rect->left, rect->top,
1980                     rect->right, rect->bottom, DDERR_INVALIDPARAMS);
1981             ok(!locked_desc.lpSurface, "IDirectDrawSurface_Lock did not set lpSurface in the surface desc to zero.\n");
1982         }
1983
1984         hr = IDirectDrawSurface_Lock(surface, NULL, &locked_desc, DDLOCK_WAIT, NULL);
1985         ok(hr == DD_OK, "IDirectDrawSurface_Lock(rect = NULL) failed (0x%08x)\n", hr);
1986         hr = IDirectDrawSurface_Lock(surface, NULL, &locked_desc, DDLOCK_WAIT, NULL);
1987         ok(hr == DDERR_SURFACEBUSY, "Double lock(rect = NULL) returned 0x%08x\n", hr);
1988         if(SUCCEEDED(hr)) {
1989             hr = IDirectDrawSurface_Unlock(surface, NULL);
1990             ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1991         }
1992         hr = IDirectDrawSurface_Unlock(surface, NULL);
1993         ok(SUCCEEDED(hr), "Unlock failed (0x%08x)\n", hr);
1994
1995         memset(&locked_desc, 0, sizeof(locked_desc));
1996         locked_desc.dwSize = sizeof(locked_desc);
1997         hr = IDirectDrawSurface_Lock(surface, &valid[0], &locked_desc, DDLOCK_WAIT, NULL);
1998         ok(hr == DD_OK, "IDirectDrawSurface_Lock(rect = [%d, %d]->[%d, %d]) failed (0x%08x)\n",
1999            valid[0].left, valid[0].top, valid[0].right, valid[0].bottom, hr);
2000         hr = IDirectDrawSurface_Lock(surface, &valid[0], &locked_desc, DDLOCK_WAIT, NULL);
2001         ok(hr == DDERR_SURFACEBUSY, "Double lock(rect = [%d, %d]->[%d, %d]) failed (0x%08x)\n",
2002            valid[0].left, valid[0].top, valid[0].right, valid[0].bottom, hr);
2003
2004         /* Locking a different rectangle returns DD_OK, but it seems to break the surface.
2005          * Afterwards unlocking the surface fails(NULL rectangle, and both locked rectangles
2006          */
2007
2008         hr = IDirectDrawSurface_Unlock(surface, NULL);
2009         ok(hr == DD_OK, "Unlock returned (0x%08x)\n", hr);
2010
2011         IDirectDrawSurface_Release(surface);
2012     }
2013 }
2014
2015 static void CompressedTest(void)
2016 {
2017     HRESULT hr;
2018     IDirectDrawSurface7 *surface;
2019     DDSURFACEDESC2 ddsd, ddsd2;
2020     IDirectDraw7 *dd7 = NULL;
2021     RECT r = { 0, 0, 128, 128 };
2022     RECT r2 = { 32, 32, 64, 64 };
2023
2024     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
2025     ok(hr == DD_OK, "IDirectDraw::QueryInterface returned %08x\n", hr);
2026
2027     memset(&ddsd, 0, sizeof(ddsd));
2028     ddsd.dwSize = sizeof(ddsd);
2029     U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
2030     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
2031     ddsd.dwWidth = 128;
2032     ddsd.dwHeight = 128;
2033     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY;
2034     U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_FOURCC;
2035     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','1');
2036
2037     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
2038     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
2039     if (FAILED(hr))
2040     {
2041         skip("failed to create surface\n");
2042         return;
2043     }
2044
2045     memset(&ddsd2, 0, sizeof(ddsd2));
2046     ddsd2.dwSize = sizeof(ddsd2);
2047     U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2048     hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
2049     ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
2050
2051     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2052        "Surface desc flags: %08x\n", ddsd2.dwFlags);
2053     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2054     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2055     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2056        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2057     ok(U1(ddsd2).dwLinearSize == 8192, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2058     ok(ddsd2.ddsCaps.dwCaps2 == 0, "Caps2: %08x\n", ddsd2.ddsCaps.dwCaps2);
2059     IDirectDrawSurface7_Release(surface);
2060
2061     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','3');
2062     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
2063     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
2064     if (FAILED(hr))
2065     {
2066         skip("failed to create surface\n");
2067         return;
2068     }
2069
2070     memset(&ddsd2, 0, sizeof(ddsd2));
2071     ddsd2.dwSize = sizeof(ddsd2);
2072     U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2073     hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
2074     ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
2075
2076     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2077        "Surface desc flags: %08x\n", ddsd2.dwFlags);
2078     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2079     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2080     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2081        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2082     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2083     IDirectDrawSurface7_Release(surface);
2084
2085     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','5');
2086     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
2087     ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
2088     if (FAILED(hr))
2089     {
2090         skip("failed to create surface\n");
2091         return;
2092     }
2093
2094     memset(&ddsd2, 0, sizeof(ddsd2));
2095     ddsd2.dwSize = sizeof(ddsd2);
2096     U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2097     hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
2098     ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
2099
2100     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2101        "Surface desc flags: %08x\n", ddsd2.dwFlags);
2102     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2103     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2104     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2105        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2106     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2107     ok(ddsd2.lpSurface == 0, "Surface memory is at %p, expected NULL\n", ddsd2.lpSurface);
2108
2109     memset(&ddsd2, 0, sizeof(ddsd2));
2110     ddsd2.dwSize = sizeof(ddsd2);
2111     U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2112
2113     /* Show that the description is not changed when locking the surface. What is really interesting
2114      * about this is that DDSD_LPSURFACE isn't set.
2115      */
2116     hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd2, DDLOCK_READONLY, 0);
2117     ok(hr == DD_OK, "Lock returned %08x\n", hr);
2118
2119     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2120        "Surface desc flags: %08x\n", ddsd2.dwFlags);
2121     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2122     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2123     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2124        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2125     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2126     ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2127
2128     hr = IDirectDrawSurface7_Unlock(surface, NULL);
2129     ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2130
2131     /* Now what about a locking rect?  */
2132     hr = IDirectDrawSurface7_Lock(surface, &r, &ddsd2, DDLOCK_READONLY, 0);
2133     ok(hr == DD_OK, "Lock returned %08x\n", hr);
2134
2135     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2136        "Surface desc flags: %08x\n", ddsd2.dwFlags);
2137     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2138     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2139     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2140        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2141     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2142     ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2143
2144     hr = IDirectDrawSurface7_Unlock(surface, &r);
2145     ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2146
2147     /* Now what about a different locking offset? */
2148     hr = IDirectDrawSurface7_Lock(surface, &r2, &ddsd2, DDLOCK_READONLY, 0);
2149     ok(hr == DD_OK, "Lock returned %08x\n", hr);
2150
2151     ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2152        "Surface desc flags: %08x\n", ddsd2.dwFlags);
2153     ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2154     ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2155     ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2156        "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2157     ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2158     ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2159
2160     hr = IDirectDrawSurface7_Unlock(surface, &r2);
2161     ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2162     IDirectDrawSurface7_Release(surface);
2163
2164     /* Try this with video memory. A kind of surprise. It still has the LINEARSIZE flag set,
2165      * but seems to have a pitch instead.
2166      */
2167     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
2168     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','1');
2169
2170     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
2171     ok(hr == DD_OK || hr == DDERR_NOTEXTUREHW || hr == DDERR_INVALIDPARAMS ||
2172        broken(hr == DDERR_NODIRECTDRAWHW), "CreateSurface returned %08x\n", hr);
2173
2174     /* Not supported everywhere */
2175     if(SUCCEEDED(hr))
2176     {
2177         memset(&ddsd2, 0, sizeof(ddsd2));
2178         ddsd2.dwSize = sizeof(ddsd2);
2179         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2180         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
2181         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
2182
2183         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2184         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2185         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2186         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2187         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
2188         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2189         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
2190         ok(ddsd2.ddsCaps.dwCaps2 == 0, "Caps2: %08x\n", ddsd2.ddsCaps.dwCaps2);
2191         IDirectDrawSurface7_Release(surface);
2192
2193         U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','3');
2194         hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
2195         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
2196
2197         memset(&ddsd2, 0, sizeof(ddsd2));
2198         ddsd2.dwSize = sizeof(ddsd2);
2199         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2200         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
2201         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
2202
2203         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2204         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2205         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2206         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2207         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
2208         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2209         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
2210         IDirectDrawSurface7_Release(surface);
2211
2212         U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','5');
2213         hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
2214         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
2215
2216         memset(&ddsd2, 0, sizeof(ddsd2));
2217         ddsd2.dwSize = sizeof(ddsd2);
2218         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2219         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
2220         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
2221
2222         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2223         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2224         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2225         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2226         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
2227         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2228         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
2229         ok(ddsd2.lpSurface == 0, "Surface memory is at %p, expected NULL\n", ddsd2.lpSurface);
2230
2231         memset(&ddsd2, 0, sizeof(ddsd2));
2232         ddsd2.dwSize = sizeof(ddsd2);
2233         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2234
2235         /* Show that the description is not changed when locking the surface. What is really interesting
2236         * about this is that DDSD_LPSURFACE isn't set.
2237         */
2238         hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd2, DDLOCK_READONLY, 0);
2239         ok(hr == DD_OK, "Lock returned %08x\n", hr);
2240
2241         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2242         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2243         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2244         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2245         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
2246         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2247         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
2248         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2249
2250         hr = IDirectDrawSurface7_Unlock(surface, NULL);
2251         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2252
2253         /* Now what about a locking rect?  */
2254         hr = IDirectDrawSurface7_Lock(surface, &r, &ddsd2, DDLOCK_READONLY, 0);
2255         ok(hr == DD_OK, "Lock returned %08x\n", hr);
2256
2257         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2258         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2259         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2260         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2261         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
2262         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2263         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
2264         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2265
2266         hr = IDirectDrawSurface7_Unlock(surface, &r);
2267         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2268
2269         /* Now what about a different locking offset? */
2270         hr = IDirectDrawSurface7_Lock(surface, &r2, &ddsd2, DDLOCK_READONLY, 0);
2271         ok(hr == DD_OK, "Lock returned %08x\n", hr);
2272
2273         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2274         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2275         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2276         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2277         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
2278         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2279         /* ATI drivers report a broken linear size, thus no need to clone the exact behaviour. nvidia reports the correct size */
2280         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2281
2282         hr = IDirectDrawSurface7_Unlock(surface, &r2);
2283         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2284
2285         IDirectDrawSurface7_Release(surface);
2286     }
2287     else
2288     {
2289         skip("Hardware DXTN textures not supported\n");
2290     }
2291
2292     /* What happens to managed textures? Interestingly, Windows reports them as being in system
2293      * memory. The linear size fits again.
2294      */
2295     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
2296     ddsd.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE;
2297     U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','1');
2298
2299     hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
2300     ok(hr == DD_OK || hr == DDERR_NOTEXTUREHW, "CreateSurface returned %08x\n", hr);
2301
2302     /* Not supported everywhere */
2303     if(SUCCEEDED(hr))
2304     {
2305         memset(&ddsd2, 0, sizeof(ddsd2));
2306         ddsd2.dwSize = sizeof(ddsd2);
2307         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2308         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
2309         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
2310
2311         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2312         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2313         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2314         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2315         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2316         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2317         ok(U1(ddsd2).dwLinearSize == 8192, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2318         ok(ddsd2.ddsCaps.dwCaps2 == DDSCAPS2_TEXTUREMANAGE, "Caps2: %08x\n", ddsd2.ddsCaps.dwCaps2);
2319         IDirectDrawSurface7_Release(surface);
2320
2321         U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','3');
2322         hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
2323         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
2324
2325         memset(&ddsd2, 0, sizeof(ddsd2));
2326         ddsd2.dwSize = sizeof(ddsd2);
2327         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2328         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
2329         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
2330
2331         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2332         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2333         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2334         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2335         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2336         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2337         ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2338         IDirectDrawSurface7_Release(surface);
2339
2340         U4(ddsd).ddpfPixelFormat.dwFourCC = MAKEFOURCC('D','X','T','5');
2341         hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
2342         ok(hr == DD_OK, "CreateSurface returned %08x\n", hr);
2343
2344         memset(&ddsd2, 0, sizeof(ddsd2));
2345         ddsd2.dwSize = sizeof(ddsd2);
2346         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2347         hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd2);
2348         ok(hr == DD_OK, "GetSurfaceDesc returned %08x\n", hr);
2349
2350         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2351         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2352         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2353         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2354         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2355         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2356         ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2357         ok(ddsd2.lpSurface == 0, "Surface memory is at %p, expected NULL\n", ddsd2.lpSurface);
2358
2359         memset(&ddsd2, 0, sizeof(ddsd2));
2360         ddsd2.dwSize = sizeof(ddsd2);
2361         U4(ddsd2).ddpfPixelFormat.dwSize = sizeof(U4(ddsd2).ddpfPixelFormat);
2362
2363         /* Show that the description is not changed when locking the surface. What is really interesting
2364         * about this is that DDSD_LPSURFACE isn't set.
2365         */
2366         hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd2, DDLOCK_READONLY, 0);
2367         ok(hr == DD_OK, "Lock returned %08x\n", hr);
2368
2369         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2370         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2371         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2372         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2373         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2374         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2375         ok(U1(ddsd2).dwLinearSize == 16384, "Linear size is %d\n", U1(ddsd2).dwLinearSize);
2376         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2377
2378         hr = IDirectDrawSurface7_Unlock(surface, NULL);
2379         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2380
2381         /* Now what about a locking rect?  */
2382         hr = IDirectDrawSurface7_Lock(surface, &r, &ddsd2, DDLOCK_READONLY, 0);
2383         ok(hr == DD_OK, "Lock returned %08x\n", hr);
2384
2385         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2386         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2387         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2388         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2389         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2390         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2391         ok(U1(ddsd2).dwLinearSize == 16384, "\"Linear\" size is %d\n", U1(ddsd2).dwLinearSize);
2392         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2393
2394         hr = IDirectDrawSurface7_Unlock(surface, &r);
2395         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2396
2397         /* Now what about a different locking offset? */
2398         hr = IDirectDrawSurface7_Lock(surface, &r2, &ddsd2, DDLOCK_READONLY, 0);
2399         ok(hr == DD_OK, "Lock returned %08x\n", hr);
2400
2401         ok(ddsd2.dwFlags == (DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_LINEARSIZE),
2402         "Surface desc flags: %08x\n", ddsd2.dwFlags);
2403         ok(U4(ddsd2).ddpfPixelFormat.dwFlags == DDPF_FOURCC, "Pixel format flags: %08x\n", U4(ddsd2).ddpfPixelFormat.dwFlags);
2404         ok(U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount == 0, "RGB bitcount: %08x\n", U1(U4(ddsd2).ddpfPixelFormat).dwRGBBitCount);
2405         ok(ddsd2.ddsCaps.dwCaps == (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY),
2406         "Surface caps flags: %08x\n", ddsd2.ddsCaps.dwCaps);
2407         ok(U1(ddsd2).dwLinearSize == 16384, "\"Linear\" size is %d\n", U1(ddsd2).dwLinearSize);
2408         ok(ddsd2.lpSurface != 0, "Surface memory is at NULL\n");
2409
2410         hr = IDirectDrawSurface7_Unlock(surface, &r2);
2411         ok(hr == DD_OK, "Unlock returned %08x\n", hr);
2412
2413         IDirectDrawSurface7_Release(surface);
2414     }
2415     else
2416     {
2417         skip("Hardware DXTN textures not supported\n");
2418     }
2419
2420     IDirectDraw7_Release(dd7);
2421 }
2422
2423 static void SizeTest(void)
2424 {
2425     LPDIRECTDRAWSURFACE dsurface = NULL;
2426     DDSURFACEDESC desc;
2427     HRESULT ret;
2428     HWND window = CreateWindow( "static", "ddraw_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
2429
2430     /* Create an offscreen surface surface without a size */
2431     ZeroMemory(&desc, sizeof(desc));
2432     desc.dwSize = sizeof(desc);
2433     desc.dwFlags = DDSD_CAPS;
2434     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2435     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2436     ok(ret == DDERR_INVALIDPARAMS, "Creating an offscreen plain surface without a size info returned %08x (dsurface=%p)\n", ret, dsurface);
2437     if(dsurface)
2438     {
2439         trace("Surface at %p\n", dsurface);
2440         IDirectDrawSurface_Release(dsurface);
2441         dsurface = NULL;
2442     }
2443
2444     /* Create an offscreen surface surface with only a width parameter */
2445     ZeroMemory(&desc, sizeof(desc));
2446     desc.dwSize = sizeof(desc);
2447     desc.dwFlags = DDSD_CAPS | DDSD_WIDTH;
2448     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2449     desc.dwWidth = 128;
2450     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2451     ok(ret == DDERR_INVALIDPARAMS, "Creating an offscreen plain surface without height info returned %08x\n", ret);
2452     if(dsurface)
2453     {
2454         IDirectDrawSurface_Release(dsurface);
2455         dsurface = NULL;
2456     }
2457
2458     /* Create an offscreen surface surface with only a height parameter */
2459     ZeroMemory(&desc, sizeof(desc));
2460     desc.dwSize = sizeof(desc);
2461     desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT;
2462     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2463     desc.dwHeight = 128;
2464     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2465     ok(ret == DDERR_INVALIDPARAMS, "Creating an offscreen plain surface without width info returned %08x\n", ret);
2466     if(dsurface)
2467     {
2468         IDirectDrawSurface_Release(dsurface);
2469         dsurface = NULL;
2470     }
2471
2472     /* Test 0 height. */
2473     memset(&desc, 0, sizeof(desc));
2474     desc.dwSize = sizeof(desc);
2475     desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
2476     desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
2477     desc.dwWidth = 1;
2478     desc.dwHeight = 0;
2479     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2480     ok(ret == DDERR_INVALIDPARAMS, "Creating a 0 height surface returned %#x, expected DDERR_INVALIDPARAMS.\n", ret);
2481     if (SUCCEEDED(ret)) IDirectDrawSurface_Release(dsurface);
2482     dsurface = NULL;
2483
2484     /* Test 0 width. */
2485     memset(&desc, 0, sizeof(desc));
2486     desc.dwSize = sizeof(desc);
2487     desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
2488     desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
2489     desc.dwWidth = 0;
2490     desc.dwHeight = 1;
2491     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2492     ok(ret == DDERR_INVALIDPARAMS, "Creating a 0 width surface returned %#x, expected DDERR_INVALIDPARAMS.\n", ret);
2493     if (SUCCEEDED(ret)) IDirectDrawSurface_Release(dsurface);
2494     dsurface = NULL;
2495
2496     /* Sanity check */
2497     ZeroMemory(&desc, sizeof(desc));
2498     desc.dwSize = sizeof(desc);
2499     desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
2500     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2501     desc.dwHeight = 128;
2502     desc.dwWidth = 128;
2503     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2504     ok(ret == DD_OK, "Creating an offscreen plain surface with width and height info returned %08x\n", ret);
2505     if(dsurface)
2506     {
2507         IDirectDrawSurface_Release(dsurface);
2508         dsurface = NULL;
2509     }
2510
2511     /* Test a primary surface size */
2512     ret = IDirectDraw_SetCooperativeLevel(lpDD, window, DDSCL_NORMAL);
2513     ok(ret == DD_OK, "SetCooperativeLevel failed with %08x\n", ret);
2514
2515     ZeroMemory(&desc, sizeof(desc));
2516     desc.dwSize = sizeof(desc);
2517     desc.dwFlags = DDSD_CAPS;
2518     desc.ddsCaps.dwCaps |= DDSCAPS_PRIMARYSURFACE;
2519     desc.dwHeight = 128; /* Keep them set to  check what happens */
2520     desc.dwWidth = 128; /* Keep them set to  check what happens */
2521     ret = IDirectDraw_CreateSurface(lpDD, &desc, &dsurface, NULL);
2522     ok(ret == DD_OK, "Creating a primary surface without width and height info returned %08x\n", ret);
2523     if(dsurface)
2524     {
2525         ret = IDirectDrawSurface_GetSurfaceDesc(dsurface, &desc);
2526         ok(ret == DD_OK, "GetSurfaceDesc returned %x\n", ret);
2527
2528         IDirectDrawSurface_Release(dsurface);
2529         dsurface = NULL;
2530
2531         ok(desc.dwFlags & DDSD_WIDTH, "Primary surface doesn't have width set\n");
2532         ok(desc.dwFlags & DDSD_HEIGHT, "Primary surface doesn't have height set\n");
2533         ok(desc.dwWidth == GetSystemMetrics(SM_CXSCREEN), "Surface width differs from screen width\n");
2534         ok(desc.dwHeight == GetSystemMetrics(SM_CYSCREEN), "Surface height differs from screen height\n");
2535     }
2536     ret = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
2537     ok(ret == DD_OK, "SetCooperativeLevel failed with %08x\n", ret);
2538 }
2539
2540 static void PrivateDataTest(void)
2541 {
2542     HRESULT hr;
2543     IDirectDrawSurface7 *surface7 = NULL;
2544     IDirectDrawSurface *surface = NULL;
2545     DDSURFACEDESC desc;
2546     ULONG ref, ref2;
2547     IUnknown *ptr;
2548     DWORD size = sizeof(IUnknown *);
2549
2550     ZeroMemory(&desc, sizeof(desc));
2551     desc.dwSize = sizeof(desc);
2552     desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
2553     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2554     desc.dwHeight = 128;
2555     desc.dwWidth = 128;
2556     hr = IDirectDraw_CreateSurface(lpDD, &desc, &surface, NULL);
2557     ok(hr == DD_OK, "Creating an offscreen plain surface failed with %08x\n", hr);
2558     if(!surface)
2559     {
2560         return;
2561     }
2562     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface7, (void **) &surface7);
2563     ok(hr == DD_OK, "IDirectDrawSurface_QueryInterface failed with %08x\n", hr);
2564     if(!surface7)
2565     {
2566         IDirectDrawSurface_Release(surface);
2567         return;
2568     }
2569
2570     /* This fails */
2571     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7 /* Abuse this tag */, lpDD, 0, DDSPD_IUNKNOWNPOINTER);
2572     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2573     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7 /* Abuse this tag */, lpDD, 5, DDSPD_IUNKNOWNPOINTER);
2574     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2575     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7 /* Abuse this tag */, lpDD, sizeof(IUnknown *) * 2, DDSPD_IUNKNOWNPOINTER);
2576     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2577
2578     ref = getref((IUnknown *) lpDD);
2579     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7 /* Abuse this tag */, lpDD, sizeof(IUnknown *), DDSPD_IUNKNOWNPOINTER);
2580     ok(hr == DD_OK, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2581     ref2 = getref((IUnknown *) lpDD);
2582     ok(ref2 == ref + 1, "Object reference is %d, expected %d\n", ref2, ref + 1);
2583     hr = IDirectDrawSurface7_FreePrivateData(surface7, &IID_IDirectDrawSurface7);
2584     ok(SUCCEEDED(hr), "IDirectDrawSurface7_FreePrivateData returned %#x.\n", hr);
2585     ref2 = getref((IUnknown *) lpDD);
2586     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
2587
2588     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7, lpDD, sizeof(IUnknown *), DDSPD_IUNKNOWNPOINTER);
2589     ok(hr == DD_OK, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2590     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7, surface7, sizeof(IUnknown *), DDSPD_IUNKNOWNPOINTER);
2591     ok(hr == DD_OK, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2592     ref2 = getref((IUnknown *) lpDD);
2593     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
2594
2595     hr = IDirectDrawSurface7_SetPrivateData(surface7, &IID_IDirectDrawSurface7, lpDD, sizeof(IUnknown *), DDSPD_IUNKNOWNPOINTER);
2596     ok(hr == DD_OK, "IDirectDrawSurface7_SetPrivateData failed with %08x\n", hr);
2597     hr = IDirectDrawSurface7_GetPrivateData(surface7, &IID_IDirectDrawSurface7, &ptr, &size);
2598     ok(hr == DD_OK, "IDirectDrawSurface7_GetPrivateData failed with %08x\n", hr);
2599     ref2 = getref((IUnknown *) lpDD);
2600     /* Object is NOT being addrefed */
2601     ok(ptr == (IUnknown *) lpDD, "Returned interface pointer is %p, expected %p\n", ptr, lpDD);
2602     ok(ref2 == ref + 1, "Object reference is %d, expected %d. ptr at %p, orig at %p\n", ref2, ref + 1, ptr, lpDD);
2603
2604     IDirectDrawSurface_Release(surface);
2605     IDirectDrawSurface7_Release(surface7);
2606
2607     /* Destroying the surface frees the held reference */
2608     ref2 = getref((IUnknown *) lpDD);
2609     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
2610 }
2611
2612 static void BltParamTest(void)
2613 {
2614     IDirectDrawSurface *surface1 = NULL, *surface2 = NULL;
2615     DDSURFACEDESC desc;
2616     HRESULT hr;
2617     DDBLTFX BltFx;
2618     RECT valid = {10, 10, 20, 20};
2619     RECT invalid1 = {20, 10, 10, 20};
2620     RECT invalid2 = {20, 20, 20, 20};
2621     RECT invalid3 = {-1, -1, 20, 20};
2622     RECT invalid4 = {60, 60, 70, 70};
2623
2624     memset(&desc, 0, sizeof(desc));
2625     desc.dwSize = sizeof(desc);
2626     desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
2627     desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2628     desc.dwHeight = 128;
2629     desc.dwWidth = 128;
2630     hr = IDirectDraw_CreateSurface(lpDD, &desc, &surface1, NULL);
2631     ok(hr == DD_OK, "Creating an offscreen plain surface failed with %08x\n", hr);
2632
2633     desc.dwHeight = 64;
2634     desc.dwWidth = 64;
2635     hr = IDirectDraw_CreateSurface(lpDD, &desc, &surface2, NULL);
2636     ok(hr == DD_OK, "Creating an offscreen plain surface failed with %08x\n", hr);
2637
2638     if(0)
2639     {
2640         /* This crashes */
2641         hr = IDirectDrawSurface_BltFast(surface1, 0, 0, NULL, NULL, 0);
2642         ok(hr == DD_OK, "BltFast from NULL surface returned %08x\n", hr);
2643     }
2644     hr = IDirectDrawSurface_BltFast(surface1, 0, 0, surface2, NULL, 0);
2645     ok(hr == DD_OK, "BltFast from smaller to bigger surface returned %08x\n", hr);
2646     hr = IDirectDrawSurface_BltFast(surface2, 0, 0, surface1, NULL, 0);
2647     ok(hr == DDERR_INVALIDRECT, "BltFast from bigger to smaller surface returned %08x\n", hr);
2648     hr = IDirectDrawSurface_BltFast(surface2, 0, 0, surface1, &valid, 0);
2649     ok(hr == DD_OK, "BltFast from bigger to smaller surface using a valid rectangle returned %08x\n", hr);
2650     hr = IDirectDrawSurface_BltFast(surface2, 60, 60, surface1, &valid, 0);
2651     ok(hr == DDERR_INVALIDRECT, "BltFast with a rectangle resulting in an off-surface write returned %08x\n", hr);
2652     hr = IDirectDrawSurface_BltFast(surface1, 90, 90, surface2, NULL, 0);
2653     ok(hr == DDERR_INVALIDRECT, "BltFast with a rectangle resulting in an off-surface write returned %08x\n", hr);
2654
2655     hr = IDirectDrawSurface_BltFast(surface1, -10, 0, surface2, NULL, 0);
2656     ok(hr == DDERR_INVALIDRECT, "BltFast with an offset resulting in an off-surface write returned %08x\n", hr);
2657     hr = IDirectDrawSurface_BltFast(surface1, 0, -10, surface2, NULL, 0);
2658     ok(hr == DDERR_INVALIDRECT, "BltFast with an offset resulting in an off-surface write returned %08x\n", hr);
2659     hr = IDirectDrawSurface_BltFast(surface2, 20, 20, surface1, &valid, 0);
2660     ok(hr == DD_OK, "BltFast from bigger to smaller surface using a valid rectangle and offset returned %08x\n", hr);
2661
2662     hr = IDirectDrawSurface_BltFast(surface2, 0, 0, surface1, &invalid1, 0);
2663     ok(hr == DDERR_INVALIDRECT, "BltFast with invalid rectangle 1 returned %08x\n", hr);
2664     hr = IDirectDrawSurface_BltFast(surface2, 0, 0, surface1, &invalid2, 0);
2665     ok(hr == DDERR_INVALIDRECT, "BltFast with invalid rectangle 2 returned %08x\n", hr);
2666     hr = IDirectDrawSurface_BltFast(surface2, 0, 0, surface1, &invalid3, 0);
2667     ok(hr == DDERR_INVALIDRECT, "BltFast with invalid rectangle 3 returned %08x\n", hr);
2668     hr = IDirectDrawSurface_BltFast(surface1, 0, 0, surface2, &invalid4, 0);
2669     ok(hr == DDERR_INVALIDRECT, "BltFast with invalid rectangle 3 returned %08x\n", hr);
2670     hr = IDirectDrawSurface_BltFast(surface1, 0, 0, surface1, NULL, 0);
2671     ok(hr == DD_OK, "BltFast blitting a surface onto itself returned %08x\n", hr);
2672
2673     /* Blt(non-fast) tests */
2674     memset(&BltFx, 0, sizeof(BltFx));
2675     BltFx.dwSize = sizeof(BltFx);
2676     U5(BltFx).dwFillColor = 0xaabbccdd;
2677
2678     hr = IDirectDrawSurface_Blt(surface1, &valid, NULL, NULL, DDBLT_COLORFILL, &BltFx);
2679     ok(hr == DD_OK, "IDirectDrawSurface_Blt with a valid rectangle for color fill returned %08x\n", hr);
2680     hr = IDirectDrawSurface_Blt(surface1, &valid, NULL, &invalid3, DDBLT_COLORFILL, &BltFx);
2681     ok(hr == DD_OK, "IDirectDrawSurface_Blt with a invalid, unused rectangle returned %08x\n", hr);
2682     hr = IDirectDrawSurface_Blt(surface2, &invalid1, NULL, NULL, DDBLT_COLORFILL, &BltFx);
2683     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 1 returned %08x\n", hr);
2684     hr = IDirectDrawSurface_Blt(surface2, &invalid2, NULL, NULL, DDBLT_COLORFILL, &BltFx);
2685     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 2 returned %08x\n", hr);
2686     hr = IDirectDrawSurface_Blt(surface2, &invalid3, NULL, NULL, DDBLT_COLORFILL, &BltFx);
2687     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 3 returned %08x\n", hr);
2688     hr = IDirectDrawSurface_Blt(surface2, &invalid4, NULL, NULL, DDBLT_COLORFILL, &BltFx);
2689     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 4 returned %08x\n", hr);
2690
2691     /* Valid on surface 1 */
2692     hr = IDirectDrawSurface_Blt(surface1, &invalid4, NULL, NULL, DDBLT_COLORFILL, &BltFx);
2693     ok(hr == DD_OK, "IDirectDrawSurface_Blt with a subrectangle fill returned %08x\n", hr);
2694
2695     /* Works - stretched blit */
2696     hr = IDirectDrawSurface_Blt(surface1, NULL, surface2, NULL, 0, NULL);
2697     ok(hr == DD_OK, "IDirectDrawSurface_Blt from a smaller to a bigger surface returned %08x\n", hr);
2698     hr = IDirectDrawSurface_Blt(surface2, NULL, surface1, NULL, 0, NULL);
2699     ok(hr == DD_OK, "IDirectDrawSurface_Blt from a bigger to a smaller surface %08x\n", hr);
2700
2701     /* Invalid dest rects in sourced blits */
2702     hr = IDirectDrawSurface_Blt(surface2, &invalid1, surface1, NULL, 0, NULL);
2703     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 1 returned %08x\n", hr);
2704     hr = IDirectDrawSurface_Blt(surface2, &invalid2, surface1, NULL, 0, NULL);
2705     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 2 returned %08x\n", hr);
2706     hr = IDirectDrawSurface_Blt(surface2, &invalid3, surface1, NULL, 0, NULL);
2707     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 3 returned %08x\n", hr);
2708     hr = IDirectDrawSurface_Blt(surface2, &invalid4, surface1, NULL, 0, NULL);
2709     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 4 returned %08x\n", hr);
2710
2711     /* Invalid src rects */
2712     hr = IDirectDrawSurface_Blt(surface2, NULL, surface1, &invalid1, 0, NULL);
2713     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 1 returned %08x\n", hr);
2714     hr = IDirectDrawSurface_Blt(surface2, NULL, surface1, &invalid2, 0, NULL);
2715     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 2 returned %08x\n", hr);
2716     hr = IDirectDrawSurface_Blt(surface2, NULL, surface1, &invalid3, 0, NULL);
2717     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 3 returned %08x\n", hr);
2718     hr = IDirectDrawSurface_Blt(surface1, NULL, surface2, &invalid4, 0, NULL);
2719     ok(hr == DDERR_INVALIDRECT, "IDirectDrawSurface_Blt with a with invalid rectangle 4 returned %08x\n", hr);
2720
2721     IDirectDrawSurface_Release(surface1);
2722     IDirectDrawSurface_Release(surface2);
2723 }
2724
2725 static void PaletteTest(void)
2726 {
2727     HRESULT hr;
2728     LPDIRECTDRAWSURFACE lpSurf = NULL;
2729     IDirectDrawSurface *backbuffer = NULL;
2730     DDSCAPS ddscaps;
2731     DDSURFACEDESC ddsd;
2732     IDirectDrawPalette *palette = NULL;
2733     PALETTEENTRY Table[256];
2734     PALETTEENTRY palEntries[256];
2735     int i;
2736
2737     for(i=0; i<256; i++)
2738     {
2739         Table[i].peRed   = 0xff;
2740         Table[i].peGreen = 0;
2741         Table[i].peBlue  = 0;
2742         Table[i].peFlags = 0;
2743     }
2744
2745     /* Create a 8bit palette without DDPCAPS_ALLOW256 set */
2746     hr = IDirectDraw_CreatePalette(lpDD, DDPCAPS_8BIT, Table, &palette, NULL);
2747     ok(hr == DD_OK, "CreatePalette failed with %08x\n", hr);
2748     if (FAILED(hr)) goto err;
2749     /* Read back the palette and verify the entries. Without DDPCAPS_ALLOW256 set
2750     /  entry 0 and 255 should have been overwritten with black and white */
2751     IDirectDrawPalette_GetEntries(palette , 0, 0, 256, &palEntries[0]);
2752     ok(hr == DD_OK, "GetEntries failed with %08x\n", hr);
2753     if(hr == DD_OK)
2754     {
2755         ok((palEntries[0].peRed == 0) && (palEntries[0].peGreen == 0) && (palEntries[0].peBlue == 0),
2756            "Palette entry 0 of a palette without DDPCAPS_ALLOW256 set should be (0,0,0) but it is (%d,%d,%d)\n",
2757            palEntries[0].peRed, palEntries[0].peGreen, palEntries[0].peBlue);
2758         ok((palEntries[255].peRed == 255) && (palEntries[255].peGreen == 255) && (palEntries[255].peBlue == 255),
2759            "Palette entry 255 of a palette without DDPCAPS_ALLOW256 set should be (255,255,255) but it is (%d,%d,%d)\n",
2760            palEntries[255].peRed, palEntries[255].peGreen, palEntries[255].peBlue);
2761
2762         /* Entry 1-254 should contain red */
2763         for(i=1; i<255; i++)
2764             ok((palEntries[i].peRed == 255) && (palEntries[i].peGreen == 0) && (palEntries[i].peBlue == 0),
2765                "Palette entry %d should have contained (255,0,0) but was set to %d,%d,%d)\n",
2766                i, palEntries[i].peRed, palEntries[i].peGreen, palEntries[i].peBlue);
2767     }
2768
2769     /* CreatePalette without DDPCAPS_ALLOW256 ignores entry 0 and 255,
2770     /  now check we are able to update the entries afterwards. */
2771     IDirectDrawPalette_SetEntries(palette , 0, 0, 256, &Table[0]);
2772     ok(hr == DD_OK, "SetEntries failed with %08x\n", hr);
2773     IDirectDrawPalette_GetEntries(palette , 0, 0, 256, &palEntries[0]);
2774     ok(hr == DD_OK, "GetEntries failed with %08x\n", hr);
2775     if(hr == DD_OK)
2776     {
2777         ok((palEntries[0].peRed == 0) && (palEntries[0].peGreen == 0) && (palEntries[0].peBlue == 0),
2778            "Palette entry 0 should have been set to (0,0,0) but it contains (%d,%d,%d)\n",
2779            palEntries[0].peRed, palEntries[0].peGreen, palEntries[0].peBlue);
2780         ok((palEntries[255].peRed == 255) && (palEntries[255].peGreen == 255) && (palEntries[255].peBlue == 255),
2781            "Palette entry 255 should have been set to (255,255,255) but it contains (%d,%d,%d)\n",
2782            palEntries[255].peRed, palEntries[255].peGreen, palEntries[255].peBlue);
2783     }
2784     IDirectDrawPalette_Release(palette);
2785
2786     /* Create a 8bit palette with DDPCAPS_ALLOW256 set */
2787     hr = IDirectDraw_CreatePalette(lpDD, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
2788     ok(hr == DD_OK, "CreatePalette failed with %08x\n", hr);
2789     if (FAILED(hr)) goto err;
2790
2791     IDirectDrawPalette_GetEntries(palette , 0, 0, 256, &palEntries[0]);
2792     ok(hr == DD_OK, "GetEntries failed with %08x\n", hr);
2793     if(hr == DD_OK)
2794     {
2795         /* All entries should contain red */
2796         for(i=0; i<256; i++)
2797             ok((palEntries[i].peRed == 255) && (palEntries[i].peGreen == 0) && (palEntries[i].peBlue == 0),
2798                "Palette entry %d should have contained (255,0,0) but was set to %d,%d,%d)\n",
2799                i, palEntries[i].peRed, palEntries[i].peGreen, palEntries[i].peBlue);
2800     }
2801
2802     /* Try to set palette to a non-palettized surface */
2803     ddsd.dwSize = sizeof(ddsd);
2804     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
2805     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
2806     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
2807     ddsd.dwWidth = 800;
2808     ddsd.dwHeight = 600;
2809     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
2810     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
2811     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xFF0000;
2812     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x00FF00;
2813     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x0000FF;
2814     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpSurf, NULL);
2815     ok(hr==DD_OK, "CreateSurface returned: %x\n",hr);
2816     if (FAILED(hr))
2817     {
2818         skip("failed to create surface\n");
2819         goto err;
2820     }
2821
2822     hr = IDirectDrawSurface_SetPalette(lpSurf, palette);
2823     ok(hr == DDERR_INVALIDPIXELFORMAT, "CreateSurface returned: %x\n",hr);
2824
2825     IDirectDrawPalette_Release(palette);
2826     palette = NULL;
2827
2828     hr = IDirectDrawSurface_GetPalette(lpSurf, &palette);
2829     ok(hr == DDERR_NOPALETTEATTACHED, "CreateSurface returned: %x\n",hr);
2830
2831     err:
2832
2833     if (lpSurf) IDirectDrawSurface_Release(lpSurf);
2834     if (palette) IDirectDrawPalette_Release(palette);
2835
2836     hr = IDirectDraw_CreatePalette(lpDD, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
2837     ok(hr == DD_OK, "CreatePalette failed with %08x\n", hr);
2838
2839     ddsd.dwSize = sizeof(ddsd);
2840     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
2841     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_BACKBUFFERCOUNT;
2842     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
2843     ddsd.dwWidth = 64;
2844     ddsd.dwHeight = 64;
2845     ddsd.dwBackBufferCount = 1;
2846     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_PALETTEINDEXED8;
2847     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 8;
2848
2849     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpSurf, NULL);
2850     ok(hr==DD_OK, "CreateSurface returned: %x\n",hr);
2851     if (FAILED(hr))
2852     {
2853         skip("failed to create surface\n");
2854         return;
2855     }
2856
2857     ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
2858     hr = IDirectDrawSurface_GetAttachedSurface(lpSurf, &ddscaps, &backbuffer);
2859     ok(hr == DD_OK, "GetAttachedSurface returned: %x\n",hr);
2860
2861     hr = IDirectDrawSurface_SetPalette(backbuffer, palette);
2862     ok(hr == DD_OK, "SetPalette returned: %x\n",hr);
2863
2864     IDirectDrawPalette_Release(palette);
2865     palette = NULL;
2866
2867     hr = IDirectDrawSurface_GetPalette(backbuffer, &palette);
2868     ok(hr == DD_OK, "CreateSurface returned: %x\n",hr);
2869
2870     IDirectDrawSurface_Release(backbuffer);
2871     IDirectDrawSurface_Release(lpSurf);
2872 }
2873
2874 static void StructSizeTest(void)
2875 {
2876     IDirectDrawSurface *surface1;
2877     IDirectDrawSurface7 *surface7;
2878     union {
2879         DDSURFACEDESC desc1;
2880         DDSURFACEDESC2 desc2;
2881         char blob[1024]; /* To get a bunch of writable memory */
2882     } desc;
2883     DDSURFACEDESC create;
2884     HRESULT hr;
2885
2886     memset(&desc, 0, sizeof(desc));
2887     memset(&create, 0, sizeof(create));
2888
2889     memset(&create, 0, sizeof(create));
2890     create.dwSize = sizeof(create);
2891     create.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
2892     create.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
2893     create.dwHeight = 128;
2894     create.dwWidth = 128;
2895     hr = IDirectDraw_CreateSurface(lpDD, &create, &surface1, NULL);
2896     ok(hr == DD_OK, "Creating an offscreen plain surface failed with %08x\n", hr);
2897     hr = IDirectDrawSurface_QueryInterface(surface1, &IID_IDirectDrawSurface7, (void **) &surface7);
2898     ok(hr == DD_OK, "IDirectDrawSurface_QueryInterface failed with %08x\n", hr);
2899
2900     desc.desc1.dwSize = sizeof(DDSURFACEDESC);
2901     hr = IDirectDrawSurface_GetSurfaceDesc(surface1, &desc.desc1);
2902     ok(hr == DD_OK, "IDirectDrawSurface_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC) returned %08x\n", hr);
2903     hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &desc.desc2);
2904     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC) returned %08x\n", hr);
2905
2906     desc.desc2.dwSize = sizeof(DDSURFACEDESC2);
2907     hr = IDirectDrawSurface_GetSurfaceDesc(surface1, &desc.desc1);
2908     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC2) returned %08x\n", hr);
2909     hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &desc.desc2);
2910     ok(hr == DD_OK, "IDirectDrawSurface7_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC2) returned %08x\n", hr);
2911
2912     desc.desc2.dwSize = 0;
2913     hr = IDirectDrawSurface_GetSurfaceDesc(surface1, &desc.desc1);
2914     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_GetSurfaceDesc with desc size 0 returned %08x\n", hr);
2915     hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &desc.desc2);
2916     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_GetSurfaceDesc with desc size 0 returned %08x\n", hr);
2917
2918     desc.desc1.dwSize = sizeof(DDSURFACEDESC) + 1;
2919     hr = IDirectDrawSurface_GetSurfaceDesc(surface1, &desc.desc1);
2920     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC) + 1 returned %08x\n", hr);
2921     hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &desc.desc2);
2922     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC) + 1 returned %08x\n", hr);
2923
2924     desc.desc2.dwSize = sizeof(DDSURFACEDESC2) + 1;
2925     hr = IDirectDrawSurface_GetSurfaceDesc(surface1, &desc.desc1);
2926     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC2) + 1returned %08x\n", hr);
2927     hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &desc.desc2);
2928     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_GetSurfaceDesc with desc size sizeof(DDSURFACEDESC2) + 1returned %08x\n", hr);
2929
2930     /* Tests for Lock() */
2931
2932     desc.desc1.dwSize = sizeof(DDSURFACEDESC);
2933     hr = IDirectDrawSurface_Lock(surface1, NULL, &desc.desc1, 0, 0);
2934     ok(hr == DD_OK, "IDirectDrawSurface_Lock with desc size sizeof(DDSURFACEDESC) returned %08x\n", hr);
2935     if(SUCCEEDED(hr)) IDirectDrawSurface_Unlock(surface1, NULL);
2936     ok(desc.desc1.dwSize == sizeof(DDSURFACEDESC), "Destination size was changed to %d\n", desc.desc1.dwSize);
2937     hr = IDirectDrawSurface7_Lock(surface7, NULL, &desc.desc2, 0, 0);
2938     ok(hr == DD_OK, "IDirectDrawSurface7_Lock with desc size sizeof(DDSURFACEDESC) returned %08x\n", hr);
2939     if(SUCCEEDED(hr)) IDirectDrawSurface7_Unlock(surface7, NULL);
2940     ok(desc.desc2.dwSize == sizeof(DDSURFACEDESC), "Destination size was changed to %d\n", desc.desc1.dwSize);
2941
2942     desc.desc2.dwSize = sizeof(DDSURFACEDESC2);
2943     hr = IDirectDrawSurface_Lock(surface1, NULL, &desc.desc1, 0, 0);
2944     ok(hr == DD_OK, "IDirectDrawSurface_Lock with desc size sizeof(DDSURFACEDESC2) returned %08x\n", hr);
2945     ok(desc.desc1.dwSize == sizeof(DDSURFACEDESC2), "Destination size was changed to %d\n", desc.desc1.dwSize);
2946     if(SUCCEEDED(hr)) IDirectDrawSurface_Unlock(surface1, NULL);
2947     hr = IDirectDrawSurface7_Lock(surface7, NULL, &desc.desc2, 0, 0);
2948     ok(hr == DD_OK, "IDirectDrawSurface7_Lock with desc size sizeof(DDSURFACEDESC2) returned %08x\n", hr);
2949     if(SUCCEEDED(hr)) IDirectDrawSurface7_Unlock(surface7, NULL);
2950     ok(desc.desc2.dwSize == sizeof(DDSURFACEDESC2), "Destination size was changed to %d\n", desc.desc1.dwSize);
2951
2952     desc.desc2.dwSize = 0;
2953     hr = IDirectDrawSurface_Lock(surface1, NULL, &desc.desc1, 0, 0);
2954     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Lock with desc size 0 returned %08x\n", hr);
2955     if(SUCCEEDED(hr)) IDirectDrawSurface_Unlock(surface1, NULL);
2956     hr = IDirectDrawSurface7_Lock(surface7, NULL, &desc.desc2, 0, 0);
2957     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_Lock with desc size 0 returned %08x\n", hr);
2958     if(SUCCEEDED(hr)) IDirectDrawSurface7_Unlock(surface7, NULL);
2959
2960     desc.desc1.dwSize = sizeof(DDSURFACEDESC) + 1;
2961     hr = IDirectDrawSurface_Lock(surface1, NULL, &desc.desc1, 0, 0);
2962     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Lock with desc size sizeof(DDSURFACEDESC) + 1 returned %08x\n", hr);
2963     if(SUCCEEDED(hr)) IDirectDrawSurface_Unlock(surface1, NULL);
2964     hr = IDirectDrawSurface7_Lock(surface7, NULL, &desc.desc2, 0, 0);
2965     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_Lock with desc size sizeof(DDSURFACEDESC) + 1 returned %08x\n", hr);
2966     if(SUCCEEDED(hr)) IDirectDrawSurface7_Unlock(surface7, NULL);
2967
2968     desc.desc2.dwSize = sizeof(DDSURFACEDESC2) + 1;
2969     hr = IDirectDrawSurface_Lock(surface1, NULL, &desc.desc1, 0, 0);
2970     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface_Lock with desc size sizeof(DDSURFACEDESC2) + 1returned %08x\n", hr);
2971     if(SUCCEEDED(hr)) IDirectDrawSurface_Unlock(surface1, NULL);
2972     hr = IDirectDrawSurface7_Lock(surface7, NULL, &desc.desc2, 0, 0);
2973     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface7_Lock with desc size sizeof(DDSURFACEDESC2) + 1returned %08x\n", hr);
2974     if(SUCCEEDED(hr)) IDirectDrawSurface7_Unlock(surface7, NULL);
2975
2976     IDirectDrawSurface7_Release(surface7);
2977     IDirectDrawSurface_Release(surface1);
2978 }
2979
2980 static void SurfaceCapsTest(void)
2981 {
2982     DDSURFACEDESC create;
2983     DDSURFACEDESC desc;
2984     HRESULT hr;
2985     IDirectDrawSurface *surface1 = NULL;
2986     DDSURFACEDESC2 create2, desc2;
2987     IDirectDrawSurface7 *surface7 = NULL;
2988     IDirectDraw7 *dd7 = NULL;
2989     DWORD create_caps[] = {
2990         DDSCAPS_OFFSCREENPLAIN,
2991         DDSCAPS_TEXTURE,
2992         DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
2993         0,
2994         DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD | DDSCAPS_SYSTEMMEMORY,
2995         DDSCAPS_PRIMARYSURFACE,
2996         DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY,
2997         DDSCAPS_3DDEVICE,
2998         DDSCAPS_ZBUFFER,
2999         DDSCAPS_3DDEVICE | DDSCAPS_OFFSCREENPLAIN
3000     };
3001     DWORD expected_caps[] = {
3002         DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3003         DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3004         DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM | DDSCAPS_ALLOCONLOAD,
3005         DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3006         DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD | DDSCAPS_SYSTEMMEMORY,
3007         DDSCAPS_PRIMARYSURFACE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM | DDSCAPS_VISIBLE,
3008         DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_VISIBLE,
3009         DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3010         DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY,
3011         DDSCAPS_3DDEVICE | DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM
3012     };
3013     UINT i;
3014
3015     /* Tests various surface flags, what changes do they undergo during surface creation. Forsaken
3016      * engine expects texture surfaces without memory flag to get a video memory flag right after
3017      * creation. Currently, Wine adds DDSCAPS_FRONTBUFFER to primary surface, but native doesn't do this
3018      * for single buffered primaries. Because of this primary surface creation tests are todo_wine. No real
3019      * app is known so far to care about this. */
3020
3021     if (!(ddcaps.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
3022     {
3023         skip("DDraw reported no VIDEOMEMORY cap. Broken video driver? Skipping surface caps tests.\n");
3024         return ;
3025     }
3026
3027     for (i = 0; i < sizeof(create_caps) / sizeof(DWORD); i++)
3028     {
3029         memset(&create, 0, sizeof(create));
3030         create.dwSize = sizeof(create);
3031         create.ddsCaps.dwCaps = create_caps[i];
3032         create.dwFlags = DDSD_CAPS;
3033
3034         if (!(create.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
3035         {
3036             create.dwFlags |= DDSD_HEIGHT | DDSD_WIDTH;
3037             create.dwHeight = 128;
3038             create.dwWidth = 128;
3039         }
3040
3041         if (create.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
3042         {
3043             create.dwFlags |= DDSD_PIXELFORMAT;
3044             create.ddpfPixelFormat.dwSize = sizeof(create.ddpfPixelFormat);
3045             create.ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
3046             U1(create.ddpfPixelFormat).dwZBufferBitDepth = 16;
3047             U3(create.ddpfPixelFormat).dwZBitMask = 0x0000FFFF;
3048         }
3049
3050         hr = IDirectDraw_CreateSurface(lpDD, &create, &surface1, NULL);
3051         ok(hr == DD_OK, "IDirectDraw_CreateSurface failed with %08x\n", hr);
3052
3053         if (SUCCEEDED(hr))
3054         {
3055             memset(&desc, 0, sizeof(desc));
3056             desc.dwSize = sizeof(DDSURFACEDESC);
3057             hr = IDirectDrawSurface_GetSurfaceDesc(surface1, &desc);
3058             ok(hr == DD_OK, "IDirectDrawSurface_GetSurfaceDesc failed with %08x\n", hr);
3059
3060             if (!(create_caps[i] & DDSCAPS_PRIMARYSURFACE))
3061                 ok(desc.ddsCaps.dwCaps == expected_caps[i],
3062                     "GetSurfaceDesc test %d returned caps %x, expected %x\n", i,
3063                     desc.ddsCaps.dwCaps, expected_caps[i]);
3064             else
3065                 todo_wine ok(desc.ddsCaps.dwCaps == expected_caps[i],
3066                                 "GetSurfaceDesc test %d returned caps %x, expected %x\n", i,
3067                                 desc.ddsCaps.dwCaps, expected_caps[i]);
3068
3069             IDirectDrawSurface_Release(surface1);
3070         }
3071     }
3072
3073     /* Test for differences in ddraw 7 */
3074     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
3075     ok(hr == DD_OK, "IDirectDraw_QueryInterface returned %08x\n", hr);
3076     if (FAILED(hr))
3077     {
3078         skip("Failed to get IDirectDraw7 interface, skipping tests\n");
3079     }
3080     else
3081     {
3082         for (i = 0; i < sizeof(create_caps) / sizeof(DWORD); i++)
3083         {
3084             memset(&create2, 0, sizeof(create2));
3085             create2.dwSize = sizeof(create2);
3086             create2.ddsCaps.dwCaps = create_caps[i];
3087             create2.dwFlags = DDSD_CAPS;
3088
3089             if (!(create2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
3090             {
3091                 create2.dwFlags |= DDSD_HEIGHT | DDSD_WIDTH;
3092                 create2.dwHeight = 128;
3093                 create2.dwWidth = 128;
3094             }
3095
3096             if (create2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
3097             {
3098                 create2.dwFlags |= DDSD_PIXELFORMAT;
3099                 U4(create2).ddpfPixelFormat.dwSize = sizeof(U4(create2).ddpfPixelFormat);
3100                 U4(create2).ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
3101                 U1(U4(create2).ddpfPixelFormat).dwZBufferBitDepth = 16;
3102                 U3(U4(create2).ddpfPixelFormat).dwZBitMask = 0x0000FFFF;
3103             }
3104
3105             hr = IDirectDraw7_CreateSurface(dd7, &create2, &surface7, NULL);
3106             ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
3107
3108             if (SUCCEEDED(hr))
3109             {
3110                 memset(&desc2, 0, sizeof(desc2));
3111                 desc2.dwSize = sizeof(DDSURFACEDESC2);
3112                 hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &desc2);
3113                 ok(hr == DD_OK, "IDirectDrawSurface_GetSurfaceDesc failed with %08x\n", hr);
3114
3115                 if (!(create_caps[i] & DDSCAPS_PRIMARYSURFACE))
3116                     ok(desc2.ddsCaps.dwCaps == expected_caps[i],
3117                         "GetSurfaceDesc test %d returned caps %x, expected %x\n", i,
3118                         desc2.ddsCaps.dwCaps, expected_caps[i]);
3119                 else
3120                     todo_wine ok(desc2.ddsCaps.dwCaps == expected_caps[i],
3121                                     "GetSurfaceDesc test %d returned caps %x, expected %x\n", i,
3122                                     desc2.ddsCaps.dwCaps, expected_caps[i]);
3123
3124                 IDirectDrawSurface7_Release(surface7);
3125             }
3126         }
3127
3128         IDirectDraw7_Release(dd7);
3129     }
3130
3131     memset(&create, 0, sizeof(create));
3132     create.dwSize = sizeof(create);
3133     create.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
3134     create.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_VIDEOMEMORY;
3135     create.dwWidth = 64;
3136     create.dwHeight = 64;
3137     hr = IDirectDraw_CreateSurface(lpDD, &create, &surface1, NULL);
3138     ok(hr == DDERR_INVALIDCAPS, "Creating a SYSMEM | VIDMEM surface returned 0x%08x, expected DDERR_INVALIDCAPS\n", hr);
3139     if(surface1) IDirectDrawSurface_Release(surface1);
3140 }
3141
3142 static BOOL can_create_primary_surface(void)
3143 {
3144     DDSURFACEDESC ddsd;
3145     IDirectDrawSurface *surface;
3146     HRESULT hr;
3147
3148     memset(&ddsd, 0, sizeof(ddsd));
3149     ddsd.dwSize = sizeof(ddsd);
3150     ddsd.dwFlags = DDSD_CAPS;
3151     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3152     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface, NULL);
3153     if(FAILED(hr)) return FALSE;
3154     IDirectDrawSurface_Release(surface);
3155     return TRUE;
3156 }
3157
3158 static void dctest_surf(IDirectDrawSurface *surf, int ddsdver) {
3159     HRESULT hr;
3160     HDC dc, dc2 = (HDC) 0x1234;
3161     DDSURFACEDESC ddsd;
3162     DDSURFACEDESC2 ddsd2;
3163
3164     memset(&ddsd, 0, sizeof(ddsd));
3165     ddsd.dwSize = sizeof(ddsd);
3166     memset(&ddsd2, 0, sizeof(ddsd2));
3167     ddsd2.dwSize = sizeof(ddsd2);
3168
3169     hr = IDirectDrawSurface_GetDC(surf, &dc);
3170     ok(hr == DD_OK, "IDirectDrawSurface_GetDC failed: 0x%08x\n", hr);
3171
3172     hr = IDirectDrawSurface_GetDC(surf, &dc2);
3173     ok(hr == DDERR_DCALREADYCREATED, "IDirectDrawSurface_GetDC failed: 0x%08x\n", hr);
3174     ok(dc2 == (HDC) 0x1234, "The failed GetDC call changed the dc: %p\n", dc2);
3175
3176     hr = IDirectDrawSurface_Lock(surf, NULL, ddsdver == 1 ? &ddsd : ((DDSURFACEDESC *) &ddsd2), 0, NULL);
3177     ok(hr == DDERR_SURFACEBUSY, "IDirectDrawSurface_Lock returned 0x%08x, expected DDERR_ALREADYLOCKED\n", hr);
3178
3179     hr = IDirectDrawSurface_ReleaseDC(surf, dc);
3180     ok(hr == DD_OK, "IDirectDrawSurface_ReleaseDC failed: 0x%08x\n", hr);
3181     hr = IDirectDrawSurface_ReleaseDC(surf, dc);
3182     ok(hr == DDERR_NODC, "IDirectDrawSurface_ReleaseDC returned 0x%08x, expected DDERR_NODC\n", hr);
3183 }
3184
3185 static void GetDCTest(void)
3186 {
3187     DDSURFACEDESC ddsd;
3188     DDSURFACEDESC2 ddsd2;
3189     IDirectDrawSurface *surf;
3190     IDirectDrawSurface2 *surf2;
3191     IDirectDrawSurface4 *surf4;
3192     IDirectDrawSurface7 *surf7;
3193     IDirectDrawSurface *tmp;
3194     IDirectDrawSurface7 *tmp7;
3195     HRESULT hr;
3196     IDirectDraw2 *dd2;
3197     IDirectDraw4 *dd4;
3198     IDirectDraw7 *dd7;
3199     HDC dc;
3200     ULONG ref;
3201
3202     memset(&ddsd, 0, sizeof(ddsd));
3203     ddsd.dwSize = sizeof(ddsd);
3204     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
3205     ddsd.dwWidth = 64;
3206     ddsd.dwHeight = 64;
3207     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3208     memset(&ddsd2, 0, sizeof(ddsd2));
3209     ddsd2.dwSize = sizeof(ddsd2);
3210     ddsd2.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
3211     ddsd2.dwWidth = 64;
3212     ddsd2.dwHeight = 64;
3213     ddsd2.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3214
3215     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surf, NULL);
3216     ok(hr == DD_OK, "IDirectDraw_CreateSurface failed: 0x%08x\n", hr);
3217     dctest_surf(surf, 1);
3218     IDirectDrawSurface_Release(surf);
3219
3220     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
3221     ok(hr == DD_OK, "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
3222
3223     hr = IDirectDraw2_CreateSurface(dd2, &ddsd, &surf, NULL);
3224     ok(hr == DD_OK, "IDirectDraw2_CreateSurface failed: 0x%08x\n", hr);
3225     dctest_surf(surf, 1);
3226
3227     hr = IDirectDrawSurface_QueryInterface(surf, &IID_IDirectDrawSurface2, (void **) &surf2);
3228     ok(hr == DD_OK, "IDirectDrawSurface_QueryInterface failed: 0x%08x\n", hr);
3229     dctest_surf((IDirectDrawSurface *) surf2, 1);
3230
3231     IDirectDrawSurface2_Release(surf2);
3232     IDirectDrawSurface_Release(surf);
3233     IDirectDraw2_Release(dd2);
3234
3235     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
3236     ok(hr == DD_OK, "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
3237
3238     surf = NULL;
3239     hr = IDirectDraw4_CreateSurface(dd4, &ddsd2, &surf4, NULL);
3240     ok(hr == DD_OK, "IDirectDraw4_CreateSurface failed: 0x%08x\n", hr);
3241     dctest_surf((IDirectDrawSurface *) surf4, 2);
3242
3243     hr = IDirectDrawSurface4_QueryInterface(surf4, &IID_IDirectDrawSurface, (void **)&surf);
3244     ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3245
3246     ref = getRefcount((IUnknown *) surf);
3247     ok(ref == 1, "Refcount is %u, expected 1\n", ref);
3248     ref = getRefcount((IUnknown *) surf4);
3249     ok(ref == 1, "Refcount is %u, expected 1\n", ref);
3250
3251     hr = IDirectDrawSurface4_GetDC(surf4, &dc);
3252     ok(SUCCEEDED(hr), "GetDC failed, hr %#x.\n", hr);
3253
3254     hr = IDirectDraw4_GetSurfaceFromDC(dd4, dc, NULL);
3255     ok(hr == E_INVALIDARG, "Expected hr E_INVALIDARG, got %#x.\n", hr);
3256
3257     hr = IDirectDraw4_GetSurfaceFromDC(dd4, dc, (IDirectDrawSurface4 **)&tmp);
3258     ok(SUCCEEDED(hr), "GetSurfaceFromDC failed, hr %#x.\n", hr);
3259     ok(tmp == surf, "Expected surface %p, got %p.\n\n", surf, tmp);
3260
3261     ref = getRefcount((IUnknown *) surf);
3262     ok(ref == 2, "Refcount is %u, expected 2\n", ref);
3263     ref = getRefcount((IUnknown *) tmp);
3264     ok(ref == 2, "Refcount is %u, expected 2\n", ref);
3265     ref = getRefcount((IUnknown *) surf4);
3266     ok(ref == 1, "Refcount is %u, expected 1\n", ref);
3267
3268     hr = IDirectDrawSurface4_ReleaseDC(surf4, dc);
3269     ok(SUCCEEDED(hr), "ReleaseDC failed, hr %#x.\n", hr);
3270
3271     IDirectDrawSurface_Release(tmp);
3272
3273     dc = CreateCompatibleDC(NULL);
3274     ok(!!dc, "CreateCompatibleDC failed.\n");
3275
3276     tmp = (IDirectDrawSurface *)0xdeadbeef;
3277     hr = IDirectDraw4_GetSurfaceFromDC(dd4, dc, (IDirectDrawSurface4 **)&tmp);
3278     ok(hr == DDERR_NOTFOUND, "GetSurfaceFromDC failed, hr %#x.\n", hr);
3279     ok(!tmp, "Expected surface NULL, got %p.\n", tmp);
3280
3281     ok(DeleteDC(dc), "DeleteDC failed.\n");
3282
3283     tmp = (IDirectDrawSurface *)0xdeadbeef;
3284     hr = IDirectDraw4_GetSurfaceFromDC(dd4, NULL, (IDirectDrawSurface4 **)&tmp);
3285     ok(hr == DDERR_NOTFOUND, "GetSurfaceFromDC failed, hr %#x.\n", hr);
3286     ok(!tmp, "Expected surface NULL, got %p.\n", tmp);
3287
3288     IDirectDrawSurface4_Release(surf4);
3289     IDirectDrawSurface_Release(surf);
3290     IDirectDraw4_Release(dd4);
3291
3292     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
3293     ok(hr == DD_OK, "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
3294
3295     hr = IDirectDraw7_CreateSurface(dd7, &ddsd2, &surf7, NULL);
3296     ok(hr == DD_OK, "IDirectDraw7_CreateSurface failed: 0x%08x\n", hr);
3297     dctest_surf((IDirectDrawSurface *) surf7, 2);
3298
3299     hr = IDirectDrawSurface7_GetDC(surf7, &dc);
3300     ok(SUCCEEDED(hr), "GetDC failed, hr %#x.\n", hr);
3301
3302     hr = IDirectDraw7_GetSurfaceFromDC(dd7, dc, NULL);
3303     ok(hr == E_INVALIDARG, "Expected hr E_INVALIDARG, got %#x.\n", hr);
3304
3305     hr = IDirectDraw7_GetSurfaceFromDC(dd7, dc, &tmp7);
3306     ok(SUCCEEDED(hr), "GetSurfaceFromDC failed, hr %#x.\n", hr);
3307     ok(tmp7 == surf7, "Expected surface %p, got %p.\n\n", surf7, tmp7);
3308     IDirectDrawSurface7_Release(tmp7);
3309
3310     hr = IDirectDrawSurface7_ReleaseDC(surf7, dc);
3311     ok(SUCCEEDED(hr), "ReleaseDC failed, hr %#x.\n", hr);
3312
3313     dc = CreateCompatibleDC(NULL);
3314     ok(!!dc, "CreateCompatibleDC failed.\n");
3315
3316     tmp7 = (IDirectDrawSurface7 *)0xdeadbeef;
3317     hr = IDirectDraw7_GetSurfaceFromDC(dd7, dc, &tmp7);
3318     ok(hr == DDERR_NOTFOUND, "GetSurfaceFromDC failed, hr %#x.\n", hr);
3319     ok(!tmp7, "Expected surface NULL, got %p.\n", tmp7);
3320
3321     ok(DeleteDC(dc), "DeleteDC failed.\n");
3322
3323     tmp7 = (IDirectDrawSurface7 *)0xdeadbeef;
3324     hr = IDirectDraw7_GetSurfaceFromDC(dd7, NULL, (IDirectDrawSurface7 **)&tmp7);
3325     ok(hr == DDERR_NOTFOUND, "GetSurfaceFromDC failed, hr %#x.\n", hr);
3326     ok(!tmp7, "Expected surface NULL, got %p.\n", tmp7);
3327
3328     IDirectDrawSurface7_Release(surf7);
3329     IDirectDraw7_Release(dd7);
3330 }
3331
3332 static void GetDCFormatTest(void)
3333 {
3334     DDSURFACEDESC2 ddsd;
3335     unsigned int i;
3336     IDirectDrawSurface7 *surface;
3337     IDirectDraw7 *dd7;
3338     HRESULT hr;
3339     HDC dc;
3340
3341     struct
3342     {
3343         const char *name;
3344         DDPIXELFORMAT fmt;
3345         BOOL getdc_capable;
3346         HRESULT alt_result;
3347     } testdata[] = {
3348         {
3349             "D3DFMT_A8R8G8B8",
3350             {
3351                 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
3352                 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
3353             },
3354             TRUE
3355         },
3356         {
3357             "D3DFMT_X8R8G8B8",
3358             {
3359                 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
3360                 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0x00000000}
3361             },
3362             TRUE
3363         },
3364         {
3365             "D3DFMT_X8B8G8R8",
3366             {
3367                 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
3368                 {32}, {0x000000ff}, {0x0000ff00}, {0x00ff0000}, {0x00000000}
3369             },
3370             TRUE,
3371             DDERR_CANTCREATEDC /* Vista+ */
3372         },
3373         {
3374             "D3DFMT_X8B8G8R8",
3375             {
3376                 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
3377                        {32}, {0x000000ff}, {0x0000ff00}, {0x00ff0000}, {0xff000000}
3378             },
3379             TRUE,
3380             DDERR_CANTCREATEDC /* Vista+ */
3381         },
3382         {
3383             "D3DFMT_A4R4G4B4",
3384             {
3385                 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
3386                        {16}, {0x00000f00}, {0x000000f0}, {0x0000000f}, {0x0000f000}
3387             },
3388             TRUE,
3389             DDERR_CANTCREATEDC /* Vista+ */
3390         },
3391         {
3392             "D3DFMT_X4R4G4B4",
3393             {
3394                 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
3395                        {16}, {0x00000f00}, {0x000000f0}, {0x0000000f}, {0x00000000}
3396             },
3397             TRUE,
3398             DDERR_CANTCREATEDC /* Vista+ */
3399         },
3400         {
3401             "D3DFMT_R5G6B5",
3402             {
3403                 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
3404                        {16}, {0x0000F800}, {0x000007E0}, {0x0000001F}, {0x00000000}
3405             },
3406             TRUE
3407         },
3408         {
3409             "D3DFMT_A1R5G5B5",
3410             {
3411                 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
3412                        {16}, {0x00007C00}, {0x000003E0}, {0x0000001F}, {0x00008000}
3413             },
3414             TRUE
3415         },
3416         {
3417             "D3DFMT_X1R5G5B5",
3418             {
3419                 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
3420                        {16}, {0x00007C00}, {0x000003E0}, {0x0000001F}, {0x00000000}
3421             },
3422             TRUE
3423         },
3424         {
3425             "D3DFMT_R3G3B2",
3426             {
3427                 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
3428                        { 8}, {0x000000E0}, {0x0000001C}, {0x00000003}, {0x00000000}
3429             },
3430             FALSE
3431         },
3432         {
3433             /* Untested, windows test machine didn't support this format */
3434             "D3DFMT_A2R10G10B10",
3435             {
3436                 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
3437                        {32}, {0xC0000000}, {0x3FF00000}, {0x000FFC00}, {0x000003FF}
3438             },
3439             TRUE
3440         },
3441         /*
3442          * GetDC on a P8 surface fails unless the display mode is 8 bpp. This is not
3443          * implemented in wine yet, so disable the test for now. Succeeding P8 getDC
3444          * calls are tested in the ddraw.visual test.
3445          *
3446         {
3447             "D3DFMT_P8",
3448             {
3449                 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
3450                 {8 }, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}
3451             },
3452             FALSE
3453         },
3454          */
3455         {
3456             "D3DFMT_L8",
3457             {
3458                 sizeof(DDPIXELFORMAT), DDPF_LUMINANCE, 0,
3459                 {8 }, {0x000000ff}, {0x00000000}, {0x00000000}, {0x00000000}
3460             },
3461             FALSE
3462         },
3463         {
3464             "D3DFMT_A8L8",
3465             {
3466                 sizeof(DDPIXELFORMAT), DDPF_ALPHAPIXELS | DDPF_LUMINANCE, 0,
3467                 {16}, {0x000000ff}, {0x00000000}, {0x00000000}, {0x0000ff00}
3468             },
3469             FALSE
3470         },
3471         {
3472             "D3DFMT_DXT1",
3473             {
3474                 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D','X','T','1'),
3475                 {0 }, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}
3476             },
3477             FALSE
3478         },
3479         {
3480             "D3DFMT_DXT2",
3481             {
3482                 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D','X','T','2'),
3483                 {0 }, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}
3484             },
3485             FALSE
3486         },
3487         {
3488             "D3DFMT_DXT3",
3489             {
3490                 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D','X','T','3'),
3491                 {0 }, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}
3492             },
3493             FALSE
3494         },
3495         {
3496             "D3DFMT_DXT4",
3497             {
3498                 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D','X','T','4'),
3499                 {0 }, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}
3500             },
3501             FALSE
3502         },
3503         {
3504             "D3DFMT_DXT5",
3505             {
3506                 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D','X','T','5'),
3507                 {0 }, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}
3508             },
3509             FALSE
3510         },
3511     };
3512
3513     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
3514     ok(hr == DD_OK, "IDirectDraw_QueryInterface failed, hr = 0x%08x\n", hr);
3515
3516     for(i = 0; i < (sizeof(testdata) / sizeof(testdata[0])); i++)
3517     {
3518         memset(&ddsd, 0, sizeof(ddsd));
3519         ddsd.dwSize = sizeof(ddsd);
3520         ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
3521         ddsd.dwWidth = 64;
3522         ddsd.dwHeight = 64;
3523         U4(ddsd).ddpfPixelFormat = testdata[i].fmt;
3524         ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3525
3526         hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
3527         if(FAILED(hr))
3528         {
3529             ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
3530             ddsd.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE;
3531             hr = IDirectDraw7_CreateSurface(dd7, &ddsd, &surface, NULL);
3532             if(FAILED(hr))
3533             {
3534                 skip("IDirectDraw7_CreateSurface failed, hr = 0x%08x, format %s\n", hr, testdata[i].name);
3535                 continue;
3536             }
3537         }
3538
3539         dc = (void *) 0x1234;
3540         hr = IDirectDrawSurface7_GetDC(surface, &dc);
3541         if(testdata[i].getdc_capable)
3542         {
3543             ok(SUCCEEDED(hr) ||
3544                (testdata[i].alt_result && hr == testdata[i].alt_result),
3545                "GetDC on a %s surface failed(0x%08x), expected it to work\n",
3546                testdata[i].name, hr);
3547         }
3548         else
3549         {
3550             ok(FAILED(hr), "GetDC on a %s surface succeeded(0x%08x), expected it to fail\n",
3551                testdata[i].name, hr);
3552         }
3553
3554         if(SUCCEEDED(hr))
3555         {
3556             IDirectDrawSurface7_ReleaseDC(surface, dc);
3557             ok(hr == DD_OK, "IDirectDrawSurface7_ReleaseDC failed, hr = 0x%08x\n", hr);
3558             dc = 0;
3559         }
3560         else
3561         {
3562             ok(dc == NULL, "After failed GetDC dc is %p\n", dc);
3563         }
3564
3565         IDirectDrawSurface7_Release(surface);
3566     }
3567
3568     IDirectDraw7_Release(dd7);
3569 }
3570
3571 static void BackBufferCreateSurfaceTest(void)
3572 {
3573     DDSURFACEDESC ddsd;
3574     DDSURFACEDESC created_ddsd;
3575     DDSURFACEDESC2 ddsd2;
3576     IDirectDrawSurface *surf;
3577     IDirectDrawSurface4 *surf4;
3578     IDirectDrawSurface7 *surf7;
3579     HRESULT hr;
3580     IDirectDraw2 *dd2;
3581     IDirectDraw4 *dd4;
3582     IDirectDraw7 *dd7;
3583
3584     const DWORD caps = DDSCAPS_BACKBUFFER;
3585     const DWORD expected_caps = DDSCAPS_BACKBUFFER | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM;
3586
3587     if (!(ddcaps.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY))
3588     {
3589         skip("DDraw reported no VIDEOMEMORY cap. Broken video driver? Skipping surface caps tests.\n");
3590         return ;
3591     }
3592
3593     memset(&ddsd, 0, sizeof(ddsd));
3594     ddsd.dwSize = sizeof(ddsd);
3595     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
3596     ddsd.dwWidth = 64;
3597     ddsd.dwHeight = 64;
3598     ddsd.ddsCaps.dwCaps = caps;
3599     memset(&ddsd2, 0, sizeof(ddsd2));
3600     ddsd2.dwSize = sizeof(ddsd2);
3601     ddsd2.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
3602     ddsd2.dwWidth = 64;
3603     ddsd2.dwHeight = 64;
3604     ddsd2.ddsCaps.dwCaps = caps;
3605     memset(&created_ddsd, 0, sizeof(created_ddsd));
3606     created_ddsd.dwSize = sizeof(DDSURFACEDESC);
3607
3608     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surf, NULL);
3609     ok(SUCCEEDED(hr), "IDirectDraw_CreateSurface failed: 0x%08x\n", hr);
3610     if (surf != NULL)
3611     {
3612         hr = IDirectDrawSurface_GetSurfaceDesc(surf, &created_ddsd);
3613         ok(SUCCEEDED(hr), "IDirectDraw_GetSurfaceDesc failed: 0x%08x\n", hr);
3614         ok(created_ddsd.ddsCaps.dwCaps == expected_caps,
3615            "GetSurfaceDesc returned caps %x, expected %x\n", created_ddsd.ddsCaps.dwCaps,
3616            expected_caps);
3617         IDirectDrawSurface_Release(surf);
3618     }
3619
3620     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
3621     ok(SUCCEEDED(hr), "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
3622
3623     hr = IDirectDraw2_CreateSurface(dd2, &ddsd, &surf, NULL);
3624     ok(hr == DDERR_INVALIDCAPS, "IDirectDraw2_CreateSurface didn't return %x08x, but %x08x\n",
3625        DDERR_INVALIDCAPS, hr);
3626
3627     IDirectDraw2_Release(dd2);
3628
3629     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
3630     ok(SUCCEEDED(hr), "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
3631
3632     hr = IDirectDraw4_CreateSurface(dd4, &ddsd2, &surf4, NULL);
3633     ok(hr == DDERR_INVALIDCAPS, "IDirectDraw4_CreateSurface didn't return %x08x, but %x08x\n",
3634        DDERR_INVALIDCAPS, hr);
3635
3636     IDirectDraw4_Release(dd4);
3637
3638     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
3639     ok(SUCCEEDED(hr), "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
3640
3641     hr = IDirectDraw7_CreateSurface(dd7, &ddsd2, &surf7, NULL);
3642     ok(hr == DDERR_INVALIDCAPS, "IDirectDraw7_CreateSurface didn't return %x08x, but %x08x\n",
3643        DDERR_INVALIDCAPS, hr);
3644
3645     IDirectDraw7_Release(dd7);
3646 }
3647
3648 static void BackBufferAttachmentFlipTest(void)
3649 {
3650     HRESULT hr;
3651     IDirectDrawSurface *surface1, *surface2, *surface3, *surface4;
3652     DDSURFACEDESC ddsd;
3653     HWND window = CreateWindow( "static", "ddraw_test", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
3654
3655     hr = IDirectDraw_SetCooperativeLevel(lpDD, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3656     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
3657
3658     /* Perform attachment tests on a back-buffer */
3659     memset(&ddsd, 0, sizeof(ddsd));
3660     ddsd.dwSize = sizeof(ddsd);
3661     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
3662     ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
3663     ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
3664     ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
3665     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface2, NULL);
3666     ok(SUCCEEDED(hr), "CreateSurface returned: %x\n",hr);
3667
3668     if (surface2 != NULL)
3669     {
3670         /* Try a single primary and a two back buffers */
3671         memset(&ddsd, 0, sizeof(ddsd));
3672         ddsd.dwSize = sizeof(ddsd);
3673         ddsd.dwFlags = DDSD_CAPS;
3674         ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3675         hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface1, NULL);
3676         ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
3677
3678         memset(&ddsd, 0, sizeof(ddsd));
3679         ddsd.dwSize = sizeof(ddsd);
3680         ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
3681         ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
3682         ddsd.dwWidth = GetSystemMetrics(SM_CXSCREEN);
3683         ddsd.dwHeight = GetSystemMetrics(SM_CYSCREEN);
3684         hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface3, NULL);
3685         ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
3686
3687         /* This one has a different size */
3688         memset(&ddsd, 0, sizeof(ddsd));
3689         ddsd.dwSize = sizeof(ddsd);
3690         ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
3691         ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
3692         ddsd.dwWidth = 128;
3693         ddsd.dwHeight = 128;
3694         hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface4, NULL);
3695         ok(hr==DD_OK,"CreateSurface returned: %x\n",hr);
3696
3697         hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
3698         todo_wine ok(hr == DD_OK || broken(hr == DDERR_CANNOTATTACHSURFACE),
3699            "Attaching a back buffer to a front buffer returned %08x\n", hr);
3700         if(SUCCEEDED(hr))
3701         {
3702             /* Try flipping the surfaces */
3703             hr = IDirectDrawSurface_Flip(surface1, NULL, DDFLIP_WAIT);
3704             todo_wine ok(hr == DD_OK, "IDirectDrawSurface_Flip returned 0x%08x\n", hr);
3705             hr = IDirectDrawSurface_Flip(surface2, NULL, DDFLIP_WAIT);
3706             todo_wine ok(hr == DDERR_NOTFLIPPABLE, "IDirectDrawSurface_Flip returned 0x%08x\n", hr);
3707
3708             /* Try the reverse without detaching first */
3709             hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
3710             ok(hr == DDERR_SURFACEALREADYATTACHED, "Attaching an attached surface to its attachee returned %08x\n", hr);
3711             hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
3712             ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
3713         }
3714         hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
3715         todo_wine ok(hr == DD_OK || broken(hr == DDERR_CANNOTATTACHSURFACE),
3716            "Attaching a front buffer to a back buffer returned %08x\n", hr);
3717         if(SUCCEEDED(hr))
3718         {
3719             /* Try flipping the surfaces */
3720             hr = IDirectDrawSurface_Flip(surface1, NULL, DDFLIP_WAIT);
3721             todo_wine ok(hr == DD_OK, "IDirectDrawSurface_Flip returned 0x%08x\n", hr);
3722             hr = IDirectDrawSurface_Flip(surface2, NULL, DDFLIP_WAIT);
3723             todo_wine ok(hr == DDERR_NOTFLIPPABLE, "IDirectDrawSurface_Flip returned 0x%08x\n", hr);
3724
3725             /* Try to detach reversed */
3726             hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
3727             ok(hr == DDERR_CANNOTDETACHSURFACE, "DeleteAttachedSurface returned %08x\n", hr);
3728             /* Now the proper detach */
3729             hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface1);
3730             ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
3731         }
3732         hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface3);
3733         todo_wine ok(hr == DD_OK || broken(hr == DDERR_CANNOTATTACHSURFACE),
3734            "Attaching a back buffer to another back buffer returned %08x\n", hr);
3735         if(SUCCEEDED(hr))
3736         {
3737             /* Try flipping the surfaces */
3738             hr = IDirectDrawSurface_Flip(surface3, NULL, DDFLIP_WAIT);
3739             todo_wine ok(hr == DD_OK, "IDirectDrawSurface_Flip returned 0x%08x\n", hr);
3740             hr = IDirectDrawSurface_Flip(surface2, NULL, DDFLIP_WAIT);
3741             todo_wine ok(hr == DDERR_NOTFLIPPABLE, "IDirectDrawSurface_Flip returned 0x%08x\n", hr);
3742             hr = IDirectDrawSurface_Flip(surface1, NULL, DDFLIP_WAIT);
3743             ok(hr == DDERR_NOTFLIPPABLE, "IDirectDrawSurface_Flip returned 0x%08x\n", hr);
3744
3745             hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface3);
3746             ok(hr == DD_OK, "DeleteAttachedSurface failed with %08x\n", hr);
3747         }
3748         hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4);
3749         ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a back buffer to a front buffer of different size returned %08x\n", hr);
3750         hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
3751         ok(hr == DDERR_CANNOTATTACHSURFACE, "Attaching a front buffer to a back buffer of different size returned %08x\n", hr);
3752
3753         IDirectDrawSurface_Release(surface4);
3754         IDirectDrawSurface_Release(surface3);
3755         IDirectDrawSurface_Release(surface2);
3756         IDirectDrawSurface_Release(surface1);
3757     }
3758
3759     hr =IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
3760     ok(hr == DD_OK, "SetCooperativeLevel returned %08x\n", hr);
3761
3762     DestroyWindow(window);
3763 }
3764
3765 static void CreateSurfaceBadCapsSizeTest(void)
3766 {
3767     DDSURFACEDESC ddsd_ok;
3768     DDSURFACEDESC ddsd_bad1;
3769     DDSURFACEDESC ddsd_bad2;
3770     DDSURFACEDESC ddsd_bad3;
3771     DDSURFACEDESC ddsd_bad4;
3772     DDSURFACEDESC2 ddsd2_ok;
3773     DDSURFACEDESC2 ddsd2_bad1;
3774     DDSURFACEDESC2 ddsd2_bad2;
3775     DDSURFACEDESC2 ddsd2_bad3;
3776     DDSURFACEDESC2 ddsd2_bad4;
3777     IDirectDrawSurface *surf;
3778     IDirectDrawSurface4 *surf4;
3779     IDirectDrawSurface7 *surf7;
3780     HRESULT hr;
3781     IDirectDraw2 *dd2;
3782     IDirectDraw4 *dd4;
3783     IDirectDraw7 *dd7;
3784
3785     const DWORD caps = DDSCAPS_OFFSCREENPLAIN;
3786
3787     memset(&ddsd_ok, 0, sizeof(ddsd_ok));
3788     ddsd_ok.dwSize = sizeof(ddsd_ok);
3789     ddsd_ok.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
3790     ddsd_ok.dwWidth = 64;
3791     ddsd_ok.dwHeight = 64;
3792     ddsd_ok.ddsCaps.dwCaps = caps;
3793     memcpy(&ddsd_bad1, &ddsd_ok, sizeof(ddsd_bad1));
3794     ddsd_bad1.dwSize--;
3795     memcpy(&ddsd_bad2, &ddsd_ok, sizeof(ddsd_bad2));
3796     ddsd_bad2.dwSize++;
3797     memcpy(&ddsd_bad3, &ddsd_ok, sizeof(ddsd_bad3));
3798     ddsd_bad3.dwSize = 0;
3799     memcpy(&ddsd_bad4, &ddsd_ok, sizeof(ddsd_bad4));
3800     ddsd_bad4.dwSize = sizeof(DDSURFACEDESC2);
3801
3802     memset(&ddsd2_ok, 0, sizeof(ddsd2_ok));
3803     ddsd2_ok.dwSize = sizeof(ddsd2_ok);
3804     ddsd2_ok.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
3805     ddsd2_ok.dwWidth = 64;
3806     ddsd2_ok.dwHeight = 64;
3807     ddsd2_ok.ddsCaps.dwCaps = caps;
3808     memcpy(&ddsd2_bad1, &ddsd2_ok, sizeof(ddsd2_bad1));
3809     ddsd2_bad1.dwSize--;
3810     memcpy(&ddsd2_bad2, &ddsd2_ok, sizeof(ddsd2_bad2));
3811     ddsd2_bad2.dwSize++;
3812     memcpy(&ddsd2_bad3, &ddsd2_ok, sizeof(ddsd2_bad3));
3813     ddsd2_bad3.dwSize = 0;
3814     memcpy(&ddsd2_bad4, &ddsd2_ok, sizeof(ddsd2_bad4));
3815     ddsd2_bad4.dwSize = sizeof(DDSURFACEDESC);
3816
3817     hr = IDirectDraw_CreateSurface(lpDD, &ddsd_ok, &surf, NULL);
3818     ok(SUCCEEDED(hr), "IDirectDraw_CreateSurface failed: 0x%08x\n", hr);
3819     IDirectDrawSurface_Release(surf);
3820
3821     hr = IDirectDraw_CreateSurface(lpDD, &ddsd_bad1, &surf, NULL);
3822     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3823        DDERR_INVALIDPARAMS, hr);
3824     hr = IDirectDraw_CreateSurface(lpDD, &ddsd_bad2, &surf, NULL);
3825     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3826        DDERR_INVALIDPARAMS, hr);
3827     hr = IDirectDraw_CreateSurface(lpDD, &ddsd_bad3, &surf, NULL);
3828     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3829        DDERR_INVALIDPARAMS, hr);
3830     hr = IDirectDraw_CreateSurface(lpDD, &ddsd_bad4, &surf, NULL);
3831     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3832        DDERR_INVALIDPARAMS, hr);
3833     hr = IDirectDraw_CreateSurface(lpDD, NULL, &surf, NULL);
3834     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3835        DDERR_INVALIDPARAMS, hr);
3836
3837     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw2, (void **) &dd2);
3838     ok(SUCCEEDED(hr), "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
3839
3840     hr = IDirectDraw2_CreateSurface(dd2, &ddsd_ok, &surf, NULL);
3841     ok(SUCCEEDED(hr), "IDirectDraw2_CreateSurface failed: 0x%08x\n", hr);
3842     IDirectDrawSurface_Release(surf);
3843
3844     hr = IDirectDraw2_CreateSurface(dd2, &ddsd_bad1, &surf, NULL);
3845     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw2_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3846        DDERR_INVALIDPARAMS, hr);
3847     hr = IDirectDraw2_CreateSurface(dd2, &ddsd_bad2, &surf, NULL);
3848     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw2_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3849        DDERR_INVALIDPARAMS, hr);
3850     hr = IDirectDraw2_CreateSurface(dd2, &ddsd_bad3, &surf, NULL);
3851     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw2_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3852        DDERR_INVALIDPARAMS, hr);
3853     hr = IDirectDraw2_CreateSurface(dd2, &ddsd_bad4, &surf, NULL);
3854     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw2_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3855        DDERR_INVALIDPARAMS, hr);
3856     hr = IDirectDraw2_CreateSurface(dd2, NULL, &surf, NULL);
3857     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw2_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3858        DDERR_INVALIDPARAMS, hr);
3859
3860     IDirectDraw2_Release(dd2);
3861
3862     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
3863     ok(SUCCEEDED(hr), "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
3864
3865     hr = IDirectDraw4_CreateSurface(dd4, &ddsd2_ok, &surf4, NULL);
3866     ok(SUCCEEDED(hr), "IDirectDraw4_CreateSurface failed: 0x%08x\n", hr);
3867     IDirectDrawSurface4_Release(surf4);
3868
3869     hr = IDirectDraw4_CreateSurface(dd4, &ddsd2_bad1, &surf4, NULL);
3870     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw4_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3871        DDERR_INVALIDPARAMS, hr);
3872     hr = IDirectDraw4_CreateSurface(dd4, &ddsd2_bad2, &surf4, NULL);
3873     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw4_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3874        DDERR_INVALIDPARAMS, hr);
3875     hr = IDirectDraw4_CreateSurface(dd4, &ddsd2_bad3, &surf4, NULL);
3876     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw4_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3877        DDERR_INVALIDPARAMS, hr);
3878     hr = IDirectDraw4_CreateSurface(dd4, &ddsd2_bad4, &surf4, NULL);
3879     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw4_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3880        DDERR_INVALIDPARAMS, hr);
3881     hr = IDirectDraw4_CreateSurface(dd4, NULL, &surf4, NULL);
3882     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw4_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3883        DDERR_INVALIDPARAMS, hr);
3884
3885     IDirectDraw4_Release(dd4);
3886
3887     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
3888     ok(SUCCEEDED(hr), "IDirectDraw_QueryInterface failed: 0x%08x\n", hr);
3889
3890     hr = IDirectDraw7_CreateSurface(dd7, &ddsd2_ok, &surf7, NULL);
3891     ok(SUCCEEDED(hr), "IDirectDraw7_CreateSurface failed: 0x%08x\n", hr);
3892     IDirectDrawSurface7_Release(surf7);
3893
3894     hr = IDirectDraw7_CreateSurface(dd7, &ddsd2_bad1, &surf7, NULL);
3895     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw7_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3896        DDERR_INVALIDPARAMS, hr);
3897     hr = IDirectDraw7_CreateSurface(dd7, &ddsd2_bad2, &surf7, NULL);
3898     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw7_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3899        DDERR_INVALIDPARAMS, hr);
3900     hr = IDirectDraw7_CreateSurface(dd7, &ddsd2_bad3, &surf7, NULL);
3901     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw7_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3902        DDERR_INVALIDPARAMS, hr);
3903     hr = IDirectDraw7_CreateSurface(dd7, &ddsd2_bad4, &surf7, NULL);
3904     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw7_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3905        DDERR_INVALIDPARAMS, hr);
3906     hr = IDirectDraw7_CreateSurface(dd7, NULL, &surf7, NULL);
3907     ok(hr == DDERR_INVALIDPARAMS, "IDirectDraw7_CreateSurface didn't return 0x%08x, but 0x%08x\n",
3908        DDERR_INVALIDPARAMS, hr);
3909
3910     IDirectDraw7_Release(dd7);
3911 }
3912
3913 static void reset_ddsd(DDSURFACEDESC *ddsd)
3914 {
3915     memset(ddsd, 0, sizeof(*ddsd));
3916     ddsd->dwSize = sizeof(*ddsd);
3917 }
3918
3919 static void no_ddsd_caps_test(void)
3920 {
3921     DDSURFACEDESC ddsd;
3922     HRESULT hr;
3923     IDirectDrawSurface *surface;
3924
3925     reset_ddsd(&ddsd);
3926     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
3927     ddsd.dwWidth = 128;
3928     ddsd.dwHeight = 128;
3929     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3930     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface, NULL);
3931     ok(SUCCEEDED(hr), "IDirectDraw_CreateSurface failed, hr %#x.\n", hr);
3932     reset_ddsd(&ddsd);
3933     hr = IDirectDrawSurface_GetSurfaceDesc(surface, &ddsd);
3934     IDirectDrawSurface_Release(surface);
3935     ok(ddsd.dwFlags & DDSD_CAPS, "DDSD_CAPS is not set\n");
3936     ok(ddsd.ddsCaps.dwCaps & DDSCAPS_OFFSCREENPLAIN, "DDSCAPS_OFFSCREENPLAIN is not set\n");
3937
3938     reset_ddsd(&ddsd);
3939     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3940     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface, NULL);
3941     ok(SUCCEEDED(hr), "IDirectDraw_CreateSurface failed, hr %#x.\n", hr);
3942     reset_ddsd(&ddsd);
3943     hr = IDirectDrawSurface_GetSurfaceDesc(surface, &ddsd);
3944     IDirectDrawSurface_Release(surface);
3945     ok(ddsd.dwFlags & DDSD_CAPS, "DDSD_CAPS is not set\n");
3946     ok(ddsd.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE, "DDSCAPS_OFFSCREENPLAIN is not set\n");
3947
3948     reset_ddsd(&ddsd);
3949     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
3950     ddsd.dwWidth = 128;
3951     ddsd.dwHeight = 128;
3952     ddsd.ddsCaps.dwCaps = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
3953     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface, NULL);
3954     ok(hr == DDERR_INVALIDCAPS, "IDirectDraw_CreateSurface returned %#x, expected DDERR_INVALIDCAPS.\n", hr);
3955 }
3956
3957 static void dump_format(const DDPIXELFORMAT *fmt)
3958 {
3959     trace("dwFlags %08x, FourCC %08x, dwZBufferBitDepth %u, stencil %u\n", fmt->dwFlags, fmt->dwFourCC,
3960           U1(*fmt).dwZBufferBitDepth, U2(*fmt).dwStencilBitDepth);
3961     trace("dwZBitMask %08x, dwStencilBitMask %08x, dwRGBZBitMask %08x\n", U3(*fmt).dwZBitMask,
3962           U4(*fmt).dwStencilBitMask, U5(*fmt).dwRGBZBitMask);
3963 }
3964
3965 static void zbufferbitdepth_test(void)
3966 {
3967     enum zfmt_succeed
3968     {
3969         ZFMT_SUPPORTED_ALWAYS,
3970         ZFMT_SUPPORTED_NEVER,
3971         ZFMT_SUPPORTED_HWDEPENDENT
3972     };
3973     struct
3974     {
3975         DWORD depth;
3976         enum zfmt_succeed supported;
3977         DDPIXELFORMAT pf;
3978     }
3979     test_data[] =
3980     {
3981         {
3982             16, ZFMT_SUPPORTED_ALWAYS,
3983             {
3984                 sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0,
3985                 {16}, {0}, {0x0000ffff}, {0x00000000}, {0x00000000}
3986             }
3987         },
3988         {
3989             24, ZFMT_SUPPORTED_HWDEPENDENT,
3990             {
3991                 sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0,
3992                 {24}, {0}, {0x00ffffff}, {0x00000000}, {0x00000000}
3993             }
3994         },
3995         {
3996             32, ZFMT_SUPPORTED_HWDEPENDENT,
3997             {
3998                 sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0,
3999                 {32}, {0}, {0xffffffff}, {0x00000000}, {0x00000000}
4000             }
4001         },
4002         /* Returns DDERR_INVALIDPARAMS instead of DDERR_INVALIDPIXELFORMAT.
4003          * Disabled for now
4004         {
4005             0, ZFMT_SUPPORTED_NEVER
4006         },
4007         */
4008         {
4009             15, ZFMT_SUPPORTED_NEVER
4010         },
4011         {
4012             28, ZFMT_SUPPORTED_NEVER
4013         },
4014         {
4015             40, ZFMT_SUPPORTED_NEVER
4016         },
4017     };
4018
4019     DDSURFACEDESC ddsd;
4020     IDirectDrawSurface *surface;
4021     HRESULT hr;
4022     unsigned int i;
4023     DDCAPS caps;
4024
4025     memset(&caps, 0, sizeof(caps));
4026     caps.dwSize = sizeof(caps);
4027     hr = IDirectDraw_GetCaps(lpDD, &caps, NULL);
4028     ok(SUCCEEDED(hr), "IDirectDraw_GetCaps failed, hr %#x.\n", hr);
4029     if (!(caps.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
4030     {
4031         skip("Z buffers not supported, skipping DDSD_ZBUFFERBITDEPTH test\n");
4032         return;
4033     }
4034
4035     for (i = 0; i < (sizeof(test_data) / sizeof(*test_data)); i++)
4036     {
4037         reset_ddsd(&ddsd);
4038         ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_ZBUFFERBITDEPTH;
4039         ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
4040         ddsd.dwWidth = 256;
4041         ddsd.dwHeight = 256;
4042         U2(ddsd).dwZBufferBitDepth = test_data[i].depth;
4043
4044         hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface, NULL);
4045         if (test_data[i].supported == ZFMT_SUPPORTED_ALWAYS)
4046         {
4047             ok(SUCCEEDED(hr), "IDirectDraw_CreateSurface failed, hr %#x.\n", hr);
4048         }
4049         else if (test_data[i].supported == ZFMT_SUPPORTED_NEVER)
4050         {
4051             ok(hr == DDERR_INVALIDPIXELFORMAT, "IDirectDraw_CreateSurface returned %#x, expected %x.\n",
4052                hr, DDERR_INVALIDPIXELFORMAT);
4053         }
4054         if (!surface) continue;
4055
4056         reset_ddsd(&ddsd);
4057         hr = IDirectDrawSurface_GetSurfaceDesc(surface, &ddsd);
4058         ok(SUCCEEDED(hr), "IDirectDrawSurface_GetSurfaceDesc failed, hr %#x.\n", hr);
4059         IDirectDrawSurface_Release(surface);
4060
4061         ok(ddsd.dwFlags & DDSD_ZBUFFERBITDEPTH, "DDSD_ZBUFFERBITDEPTH is not set\n");
4062         ok(!(ddsd.dwFlags & DDSD_PIXELFORMAT), "DDSD_PIXELFORMAT is set\n");
4063         /* Yet the ddpfPixelFormat member contains valid data */
4064         if (memcmp(&ddsd.ddpfPixelFormat, &test_data[i].pf, ddsd.ddpfPixelFormat.dwSize))
4065         {
4066             ok(0, "Unexpected format for depth %u\n", test_data[i].depth);
4067             dump_format(&ddsd.ddpfPixelFormat);
4068         }
4069     }
4070
4071     /* DDSD_ZBUFFERBITDEPTH vs DDSD_PIXELFORMAT? */
4072     reset_ddsd(&ddsd);
4073     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_ZBUFFERBITDEPTH;
4074     ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
4075     ddsd.dwWidth = 256;
4076     ddsd.dwHeight = 256;
4077     U2(ddsd).dwZBufferBitDepth = 24;
4078     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
4079     ddsd.ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
4080     U1(ddsd.ddpfPixelFormat).dwZBufferBitDepth = 16;
4081     U3(ddsd.ddpfPixelFormat).dwZBitMask = 0x0000ffff;
4082
4083     surface = NULL;
4084     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface, NULL);
4085     ok(SUCCEEDED(hr), "IDirectDraw_CreateSurface failed, hr %#x.\n", hr);
4086     if (!surface) return;
4087     reset_ddsd(&ddsd);
4088     hr = IDirectDrawSurface_GetSurfaceDesc(surface, &ddsd);
4089     ok(SUCCEEDED(hr), "IDirectDrawSurface_GetSurfaceDesc failed, hr %#x.\n", hr);
4090     IDirectDrawSurface_Release(surface);
4091     ok(U1(ddsd.ddpfPixelFormat).dwZBufferBitDepth == 16, "Expected a 16bpp depth buffer, got %ubpp\n",
4092        U1(ddsd.ddpfPixelFormat).dwZBufferBitDepth);
4093     ok(ddsd.dwFlags & DDSD_ZBUFFERBITDEPTH, "DDSD_ZBUFFERBITDEPTH is not set\n");
4094     ok(!(ddsd.dwFlags & DDSD_PIXELFORMAT), "DDSD_PIXELFORMAT is set\n");
4095     ok(U2(ddsd).dwZBufferBitDepth == 16, "Expected dwZBufferBitDepth=16, got %u\n",
4096        U2(ddsd).dwZBufferBitDepth);
4097
4098     /* DDSD_PIXELFORMAT vs invalid ZBUFFERBITDEPTH */
4099     reset_ddsd(&ddsd);
4100     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_PIXELFORMAT;
4101     ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
4102     ddsd.dwWidth = 256;
4103     ddsd.dwHeight = 256;
4104     U2(ddsd).dwZBufferBitDepth = 40;
4105     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
4106     ddsd.ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
4107     U1(ddsd.ddpfPixelFormat).dwZBufferBitDepth = 16;
4108     U3(ddsd.ddpfPixelFormat).dwZBitMask = 0x0000ffff;
4109     surface = NULL;
4110     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface, NULL);
4111     ok(SUCCEEDED(hr), "IDirectDraw_CreateSurface failed, hr %#x.\n", hr);
4112     if (surface) IDirectDrawSurface_Release(surface);
4113
4114     /* Create a PIXELFORMAT-only surface, see if ZBUFFERBITDEPTH is set */
4115     reset_ddsd(&ddsd);
4116     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
4117     ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
4118     ddsd.dwWidth = 256;
4119     ddsd.dwHeight = 256;
4120     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
4121     ddsd.ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
4122     U1(ddsd.ddpfPixelFormat).dwZBufferBitDepth = 16;
4123     U3(ddsd.ddpfPixelFormat).dwZBitMask = 0x0000ffff;
4124     surface = NULL;
4125     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface, NULL);
4126     ok(SUCCEEDED(hr), "IDirectDrawSurface_GetSurfaceDesc failed, hr %#x.\n", hr);
4127     reset_ddsd(&ddsd);
4128     hr = IDirectDrawSurface_GetSurfaceDesc(surface, &ddsd);
4129     ok(SUCCEEDED(hr), "IDirectDrawSurface_GetSurfaceDesc failed, hr %#x.\n", hr);
4130     IDirectDrawSurface_Release(surface);
4131     ok(U1(ddsd.ddpfPixelFormat).dwZBufferBitDepth == 16, "Expected a 16bpp depth buffer, got %ubpp\n",
4132        U1(ddsd.ddpfPixelFormat).dwZBufferBitDepth);
4133     ok(ddsd.dwFlags & DDSD_ZBUFFERBITDEPTH, "DDSD_ZBUFFERBITDEPTH is not set\n");
4134     ok(!(ddsd.dwFlags & DDSD_PIXELFORMAT), "DDSD_PIXELFORMAT is set\n");
4135     ok(U2(ddsd).dwZBufferBitDepth == 16, "Expected dwZBufferBitDepth=16, got %u\n",
4136        U2(ddsd).dwZBufferBitDepth);
4137 }
4138
4139 static void test_ddsd(DDSURFACEDESC *ddsd, BOOL expect_pf, BOOL expect_zd, const char *name, DWORD z_bit_depth)
4140 {
4141     IDirectDrawSurface *surface;
4142     IDirectDrawSurface7 *surface7;
4143     HRESULT hr;
4144     DDSURFACEDESC out;
4145     DDSURFACEDESC2 out2;
4146
4147     hr = IDirectDraw_CreateSurface(lpDD, ddsd, &surface, NULL);
4148     if (hr == DDERR_NOZBUFFERHW)
4149     {
4150         skip("Z buffers not supported, skipping Z flag test\n");
4151         return;
4152     }
4153     ok(SUCCEEDED(hr), "IDirectDraw_CreateSurface failed, hr %#x.\n", hr);
4154     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface7, (void **) &surface7);
4155     ok(SUCCEEDED(hr), "IDirectDrawSurface_QueryInterface failed, hr %#x.\n", hr);
4156
4157     reset_ddsd(&out);
4158     hr = IDirectDrawSurface_GetSurfaceDesc(surface, &out);
4159     ok(SUCCEEDED(hr), "IDirectDrawSurface_GetSurfaceDesc failed, hr %#x.\n", hr);
4160     memset(&out2, 0, sizeof(out2));
4161     out2.dwSize = sizeof(out2);
4162     hr = IDirectDrawSurface7_GetSurfaceDesc(surface7, &out2);
4163     ok(SUCCEEDED(hr), "IDirectDrawSurface_GetSurfaceDesc failed, hr %#x.\n", hr);
4164
4165     if (expect_pf)
4166     {
4167         ok(out.dwFlags & DDSD_PIXELFORMAT, "%s surface: Expected DDSD_PIXELFORMAT to be set\n", name);
4168         ok(out2.dwFlags & DDSD_PIXELFORMAT,
4169                 "%s surface: Expected DDSD_PIXELFORMAT to be set in DDSURFACEDESC2\n", name);
4170     }
4171     else
4172     {
4173         ok(!(out.dwFlags & DDSD_PIXELFORMAT), "%s surface: Expected DDSD_PIXELFORMAT not to be set\n", name);
4174         ok(out2.dwFlags & DDSD_PIXELFORMAT,
4175                 "%s surface: Expected DDSD_PIXELFORMAT to be set in DDSURFACEDESC2\n", name);
4176     }
4177     if (expect_zd)
4178     {
4179         ok(out.dwFlags & DDSD_ZBUFFERBITDEPTH, "%s surface: Expected DDSD_ZBUFFERBITDEPTH to be set\n", name);
4180         ok(U2(out).dwZBufferBitDepth == z_bit_depth, "ZBufferBitDepth is %u, expected %u\n",
4181                 U2(out).dwZBufferBitDepth, z_bit_depth);
4182         ok(!(out2.dwFlags & DDSD_ZBUFFERBITDEPTH),
4183                 "%s surface: Did not expect DDSD_ZBUFFERBITDEPTH to be set in DDSURFACEDESC2\n", name);
4184         /* dwMipMapCount and dwZBufferBitDepth share the same union */
4185         ok(U2(out2).dwMipMapCount == 0, "dwMipMapCount is %u, expected 0\n", U2(out2).dwMipMapCount);
4186     }
4187     else
4188     {
4189         ok(!(out.dwFlags & DDSD_ZBUFFERBITDEPTH), "%s surface: Expected DDSD_ZBUFFERBITDEPTH not to be set\n", name);
4190         ok(U2(out).dwZBufferBitDepth == 0, "ZBufferBitDepth is %u, expected 0\n", U2(out).dwZBufferBitDepth);
4191         ok(!(out2.dwFlags & DDSD_ZBUFFERBITDEPTH),
4192                 "%s surface: Did not expect DDSD_ZBUFFERBITDEPTH to be set in DDSURFACEDESC2\n", name);
4193         ok(U2(out2).dwMipMapCount == 0, "dwMipMapCount is %u, expected 0\n", U2(out2).dwMipMapCount);
4194     }
4195
4196     reset_ddsd(&out);
4197     hr = IDirectDrawSurface_Lock(surface, NULL, &out, 0, NULL);
4198     if (SUCCEEDED(hr))
4199     {
4200         hr = IDirectDrawSurface_Unlock(surface, NULL);
4201         ok(SUCCEEDED(hr), "IDirectDrawSurface_GetSurfaceDesc failed, hr %#x.\n", hr);
4202
4203         /* DDSD_ZBUFFERBITDEPTH is never set on Nvidia, but follows GetSurfaceDesc rules on AMD */
4204         if (!expect_zd)
4205         {
4206             ok(!(out.dwFlags & DDSD_ZBUFFERBITDEPTH),
4207                 "Lock %s surface: Expected DDSD_ZBUFFERBITDEPTH not to be set\n", name);
4208         }
4209
4210         /* DDSD_PIXELFORMAT follows GetSurfaceDesc rules */
4211         if (expect_pf)
4212         {
4213             ok(out.dwFlags & DDSD_PIXELFORMAT, "%s surface: Expected DDSD_PIXELFORMAT to be set\n", name);
4214         }
4215         else
4216         {
4217             ok(!(out.dwFlags & DDSD_PIXELFORMAT),
4218                 "Lock %s surface: Expected DDSD_PIXELFORMAT not to be set\n", name);
4219         }
4220         if (out.dwFlags & DDSD_ZBUFFERBITDEPTH)
4221             ok(U2(out).dwZBufferBitDepth == z_bit_depth, "ZBufferBitDepth is %u, expected %u\n",
4222                     U2(out).dwZBufferBitDepth, z_bit_depth);
4223         else
4224             ok(U2(out).dwZBufferBitDepth == 0, "ZBufferBitDepth is %u, expected 0\n", U2(out).dwZBufferBitDepth);
4225     }
4226
4227     hr = IDirectDrawSurface7_Lock(surface7, NULL, &out2, 0, NULL);
4228     ok(SUCCEEDED(hr), "IDirectDrawSurface7_Lock failed, hr %#x.\n", hr);
4229     if (SUCCEEDED(hr))
4230     {
4231         hr = IDirectDrawSurface7_Unlock(surface7, NULL);
4232         ok(SUCCEEDED(hr), "IDirectDrawSurface7_Unlock failed, hr %#x.\n", hr);
4233         /* DDSD_PIXELFORMAT is always set, DDSD_ZBUFFERBITDEPTH never */
4234         ok(out2.dwFlags & DDSD_PIXELFORMAT,
4235                 "Lock %s surface: Expected DDSD_PIXELFORMAT to be set in DDSURFACEDESC2\n", name);
4236         ok(!(out2.dwFlags & DDSD_ZBUFFERBITDEPTH),
4237                 "Lock %s surface: Did not expect DDSD_ZBUFFERBITDEPTH to be set in DDSURFACEDESC2\n", name);
4238         ok(U2(out2).dwMipMapCount == 0, "dwMipMapCount is %u, expected 0\n", U2(out2).dwMipMapCount);
4239     }
4240
4241     IDirectDrawSurface7_Release(surface7);
4242     IDirectDrawSurface_Release(surface);
4243 }
4244
4245 static void pixelformat_flag_test(void)
4246 {
4247     DDSURFACEDESC ddsd;
4248     DDCAPS caps;
4249     HRESULT hr;
4250
4251     memset(&caps, 0, sizeof(caps));
4252     caps.dwSize = sizeof(caps);
4253     hr = IDirectDraw_GetCaps(lpDD, &caps, NULL);
4254     ok(SUCCEEDED(hr), "IDirectDraw_GetCaps failed, hr %#x.\n", hr);
4255     if (!(caps.ddsCaps.dwCaps & DDSCAPS_ZBUFFER))
4256     {
4257         skip("Z buffers not supported, skipping DDSD_PIXELFORMAT test\n");
4258         return;
4259     }
4260
4261     reset_ddsd(&ddsd);
4262     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4263     ddsd.dwWidth = 64;
4264     ddsd.dwHeight = 64;
4265     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
4266     test_ddsd(&ddsd, TRUE, FALSE, "offscreen plain", ~0U);
4267
4268     reset_ddsd(&ddsd);
4269     ddsd.dwFlags = DDSD_CAPS;
4270     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
4271     test_ddsd(&ddsd, TRUE, FALSE, "primary", ~0U);
4272
4273     reset_ddsd(&ddsd);
4274     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_ZBUFFERBITDEPTH;
4275     ddsd.dwWidth = 64;
4276     ddsd.dwHeight = 64;
4277     U2(ddsd).dwZBufferBitDepth = 16;
4278     ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
4279     test_ddsd(&ddsd, FALSE, TRUE, "Z buffer", 16);
4280 }
4281
4282 static void set_surface_desc_test(void)
4283 {
4284     HRESULT hr;
4285     DDSURFACEDESC ddsd;
4286     IDirectDrawSurface *surface;
4287     IDirectDrawSurface3 *surface3;
4288     BYTE data[8*8*4];
4289
4290     hr = IDirectDraw_CreateSurface(lpDD, NULL, &surface, NULL);
4291     ok(hr == DDERR_INVALIDPARAMS, "CreateSurface with a NULL DDSD returned %#x,"
4292             " expected DDERR_INVALIDPARAMS(%#x)\n", hr, DDERR_INVALIDPARAMS);
4293
4294     reset_ddsd(&ddsd);
4295     ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
4296     ddsd.dwWidth = 8;
4297     ddsd.dwHeight = 8;
4298     ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
4299     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
4300     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
4301     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
4302     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
4303     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
4304     ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY;
4305
4306     hr = IDirectDraw_CreateSurface(lpDD, &ddsd, &surface, NULL);
4307     ok(SUCCEEDED(hr), "IDirectDraw_CreateSurface failed, hr %#x.\n", hr);
4308
4309     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **) &surface3);
4310     ok(SUCCEEDED(hr), "IDirectDrawSurface_QueryInterface failed, hr %#x.\n", hr);
4311     IDirectDrawSurface_Release(surface);
4312
4313     reset_ddsd(&ddsd);
4314     ddsd.dwFlags = DDSD_LPSURFACE;
4315     ddsd.lpSurface = data;
4316     hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
4317     ok(SUCCEEDED(hr), "IDirectDrawSurface3_SetSurfaceDesc failed, hr %#x.\n", hr);
4318
4319     /* Redundantly setting the same lpSurface is not an error */
4320     hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 0);
4321     ok(SUCCEEDED(hr), "IDirectDrawSurface3_SetSurfaceDesc failed, hr %#x.\n", hr);
4322
4323     hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, &ddsd, 1);
4324     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface3_SetSurfaceDesc returned %#x, expected"
4325             " DDERR_INVALIDPARAMS(%#x)\n", hr, DDERR_INVALIDPARAMS);
4326
4327     hr = IDirectDrawSurface3_SetSurfaceDesc(surface3, NULL, 0);
4328     ok(hr == DDERR_INVALIDPARAMS, "IDirectDrawSurface3_SetSurfaceDesc returned %#x, expected"
4329             " DDERR_INVALIDPARAMS(%#x)\n", hr, DDERR_INVALIDPARAMS);
4330
4331     IDirectDrawSurface_Release(surface3);
4332 }
4333
4334 START_TEST(dsurface)
4335 {
4336     HRESULT ret;
4337     IDirectDraw4 *dd4;
4338
4339     if (!CreateDirectDraw())
4340         return;
4341
4342     ret = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw4, (void **) &dd4);
4343     if (ret == E_NOINTERFACE)
4344     {
4345         win_skip("DirectDraw4 and higher are not supported\n");
4346         ReleaseDirectDraw();
4347         return;
4348     }
4349     IDirectDraw_Release(dd4);
4350
4351     if(!can_create_primary_surface())
4352     {
4353         skip("Unable to create primary surface\n");
4354         return;
4355     }
4356
4357     ddcaps.dwSize = sizeof(DDCAPS);
4358     ret = IDirectDraw_GetCaps(lpDD, &ddcaps, NULL);
4359     if (ret != DD_OK)
4360     {
4361         skip("IDirectDraw_GetCaps failed with %08x\n", ret);
4362         return;
4363     }
4364
4365     MipMapCreationTest();
4366     SrcColorKey32BlitTest();
4367     QueryInterface();
4368     GetDDInterface_1();
4369     GetDDInterface_2();
4370     GetDDInterface_4();
4371     GetDDInterface_7();
4372     IFaceRefCount();
4373     EnumTest();
4374     AttachmentTest();
4375     AttachmentTest7();
4376     CubeMapTest();
4377     test_lockrect_invalid();
4378     CompressedTest();
4379     SizeTest();
4380     PrivateDataTest();
4381     BltParamTest();
4382     StructSizeTest();
4383     PaletteTest();
4384     SurfaceCapsTest();
4385     GetDCTest();
4386     GetDCFormatTest();
4387     BackBufferCreateSurfaceTest();
4388     BackBufferAttachmentFlipTest();
4389     CreateSurfaceBadCapsSizeTest();
4390     no_ddsd_caps_test();
4391     zbufferbitdepth_test();
4392     pixelformat_flag_test();
4393     set_surface_desc_test();
4394     ReleaseDirectDraw();
4395 }