winealsa: Map ALSA errors to AUDCLNT_E_*.
[wine] / dlls / ddraw / tests / ddraw1.c
1 /*
2  * Copyright 2011 Henri Verbeet for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "wine/test.h"
20 #include "d3d.h"
21
22 static BOOL compare_color(D3DCOLOR c1, D3DCOLOR c2, BYTE max_diff)
23 {
24     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
25     c1 >>= 8; c2 >>= 8;
26     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
27     c1 >>= 8; c2 >>= 8;
28     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
29     c1 >>= 8; c2 >>= 8;
30     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
31     return TRUE;
32 }
33
34 static D3DCOLOR get_surface_color(IDirectDrawSurface *surface, UINT x, UINT y)
35 {
36     RECT rect = {x, y, x + 1, y + 1};
37     DDSURFACEDESC surface_desc;
38     D3DCOLOR color;
39     HRESULT hr;
40
41     memset(&surface_desc, 0, sizeof(surface_desc));
42     surface_desc.dwSize = sizeof(surface_desc);
43
44     hr = IDirectDrawSurface_Lock(surface, &rect, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
45     ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
46     if (FAILED(hr))
47         return 0xdeadbeef;
48
49     color = *((DWORD *)surface_desc.lpSurface) & 0x00ffffff;
50
51     hr = IDirectDrawSurface_Unlock(surface, NULL);
52     ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
53
54     return color;
55 }
56
57 static HRESULT CALLBACK enum_z_fmt(GUID *guid, char *description, char *name,
58         D3DDEVICEDESC *hal_desc, D3DDEVICEDESC *hel_desc, void *ctx)
59 {
60     DWORD *z_depth = ctx;
61
62     if (!IsEqualGUID(&IID_IDirect3DHALDevice, guid))
63         return D3DENUMRET_OK;
64
65     if (hal_desc->dwDeviceZBufferBitDepth & DDBD_32)
66         *z_depth = 32;
67     else if (hal_desc->dwDeviceZBufferBitDepth & DDBD_24)
68         *z_depth = 24;
69     else if (hal_desc->dwDeviceZBufferBitDepth & DDBD_16)
70         *z_depth = 16;
71
72     return DDENUMRET_OK;
73 }
74
75 static IDirectDraw *create_ddraw(void)
76 {
77     IDirectDraw *ddraw;
78
79     if (FAILED(DirectDrawCreate(NULL, &ddraw, NULL)))
80         return NULL;
81
82     return ddraw;
83 }
84
85 static IDirect3DDevice *create_device(IDirectDraw *ddraw, HWND window, DWORD coop_level)
86 {
87     IDirectDrawSurface *surface, *ds;
88     IDirect3DDevice *device = NULL;
89     DDSURFACEDESC surface_desc;
90     DWORD z_depth = 0;
91     IDirect3D *d3d;
92     HRESULT hr;
93
94     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, coop_level);
95     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
96
97     memset(&surface_desc, 0, sizeof(surface_desc));
98     surface_desc.dwSize = sizeof(surface_desc);
99     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
100     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
101     surface_desc.dwWidth = 640;
102     surface_desc.dwHeight = 480;
103
104     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
105     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
106
107     if (coop_level & DDSCL_NORMAL)
108     {
109         IDirectDrawClipper *clipper;
110
111         hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
112         ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
113         hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
114         ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
115         hr = IDirectDrawSurface_SetClipper(surface, clipper);
116         ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
117         IDirectDrawClipper_Release(clipper);
118     }
119
120     hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
121     if (FAILED(hr))
122     {
123         IDirectDrawSurface_Release(surface);
124         return NULL;
125     }
126
127     hr = IDirect3D_EnumDevices(d3d, enum_z_fmt, &z_depth);
128     ok(SUCCEEDED(hr), "Failed to enumerate z-formats, hr %#x.\n", hr);
129     IDirect3D_Release(d3d);
130     if (FAILED(hr) || !z_depth)
131     {
132         IDirectDrawSurface_Release(surface);
133         return NULL;
134     }
135
136     memset(&surface_desc, 0, sizeof(surface_desc));
137     surface_desc.dwSize = sizeof(surface_desc);
138     surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
139     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
140     U2(surface_desc).dwZBufferBitDepth = z_depth;
141     surface_desc.dwWidth = 640;
142     surface_desc.dwHeight = 480;
143     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
144     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
145     if (FAILED(hr))
146     {
147         IDirectDrawSurface_Release(surface);
148         return NULL;
149     }
150
151     hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
152     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
153     IDirectDrawSurface_Release(ds);
154     if (FAILED(hr))
155     {
156         IDirectDrawSurface_Release(surface);
157         return NULL;
158     }
159
160     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DHALDevice, (void **)&device);
161     IDirectDrawSurface_Release(surface);
162     if (FAILED(hr))
163         return NULL;
164
165     return device;
166 }
167
168 static HRESULT CALLBACK restore_callback(IDirectDrawSurface *surface, DDSURFACEDESC *desc, void *context)
169 {
170     HRESULT hr = IDirectDrawSurface_Restore(surface);
171     ok(SUCCEEDED(hr), "Failed to restore surface, hr %#x.\n", hr);
172     IDirectDrawSurface_Release(surface);
173
174     return DDENUMRET_OK;
175 }
176
177 static HRESULT restore_surfaces(IDirectDraw *ddraw)
178 {
179     return IDirectDraw_EnumSurfaces(ddraw, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
180             NULL, NULL, restore_callback);
181 }
182
183 static void test_coop_level_create_device_window(void)
184 {
185     HWND focus_window, device_window;
186     IDirectDraw *ddraw;
187     HRESULT hr;
188
189     focus_window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
190             0, 0, 640, 480, 0, 0, 0, 0);
191     if (!(ddraw = create_ddraw()))
192     {
193         skip("Failed to create a ddraw object, skipping test.\n");
194         DestroyWindow(focus_window);
195         return;
196     }
197
198     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
199     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
200     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
201     ok(!device_window, "Unexpected device window found.\n");
202     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
203     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
204     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
205     ok(!device_window, "Unexpected device window found.\n");
206     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
207     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
208     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
209     ok(!device_window, "Unexpected device window found.\n");
210     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
211     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
212     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
213     ok(!device_window, "Unexpected device window found.\n");
214     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
215     ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
216     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
217     ok(!device_window, "Unexpected device window found.\n");
218
219     /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
220     if (broken(hr == DDERR_INVALIDPARAMS))
221     {
222         win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
223         IDirectDraw_Release(ddraw);
224         DestroyWindow(focus_window);
225         return;
226     }
227
228     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
229     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
230     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
231     ok(!device_window, "Unexpected device window found.\n");
232     hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
233     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
234     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
235     ok(!device_window, "Unexpected device window found.\n");
236
237     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
238     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
239     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
240     ok(!device_window, "Unexpected device window found.\n");
241     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
242             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
243     ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
244     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
245     ok(!!device_window, "Device window not found.\n");
246
247     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
248     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
249     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
250     ok(!device_window, "Unexpected device window found.\n");
251     hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
252             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
253     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
254     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
255     ok(!!device_window, "Device window not found.\n");
256
257     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
258     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
259     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
260     ok(!device_window, "Unexpected device window found.\n");
261     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
262     ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
263     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
264     ok(!device_window, "Unexpected device window found.\n");
265     hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
266     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
267     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
268     ok(!device_window, "Unexpected device window found.\n");
269     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
270     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
271     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
272     ok(!!device_window, "Device window not found.\n");
273
274     IDirectDraw_Release(ddraw);
275     DestroyWindow(focus_window);
276 }
277
278 static void test_clipper_blt(void)
279 {
280     IDirectDrawSurface *src_surface, *dst_surface;
281     RECT client_rect, src_rect, *rect;
282     IDirectDrawClipper *clipper;
283     DDSURFACEDESC surface_desc;
284     unsigned int i, j, x, y;
285     IDirectDraw *ddraw;
286     RGNDATA *rgn_data;
287     D3DCOLOR color;
288     HRGN r1, r2;
289     HWND window;
290     DDBLTFX fx;
291     HRESULT hr;
292     DWORD *ptr;
293     DWORD ret;
294
295     static const DWORD src_data[] =
296     {
297         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
298         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
299         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
300     };
301     static const D3DCOLOR expected1[] =
302     {
303         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
304         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
305         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
306         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
307     };
308     static const D3DCOLOR expected2[] =
309     {
310         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
311         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
312         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
313         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
314     };
315
316     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
317             10, 10, 640, 480, 0, 0, 0, 0);
318     ShowWindow(window, SW_SHOW);
319     if (!(ddraw = create_ddraw()))
320     {
321         skip("Failed to create a ddraw object, skipping test.\n");
322         DestroyWindow(window);
323         return;
324     }
325
326     ret = GetClientRect(window, &client_rect);
327     ok(ret, "Failed to get client rect.\n");
328     ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
329     ok(ret, "Failed to map client rect.\n");
330
331     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
332     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
333
334     hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
335     ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
336     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
337     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
338     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
339     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
340     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
341     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
342     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
343     hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
344     ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
345     ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
346     ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
347     ok(rgn_data->rdh.nCount == 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
348     ok(rgn_data->rdh.nRgnSize == 16 || broken(rgn_data->rdh.nRgnSize == 168 /* NT4 */),
349             "Got unexpected region size %u.\n", rgn_data->rdh.nRgnSize);
350     ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
351             "Got unexpected bounding rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
352             rgn_data->rdh.rcBound.left, rgn_data->rdh.rcBound.top,
353             rgn_data->rdh.rcBound.right, rgn_data->rdh.rcBound.bottom,
354             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
355     rect = (RECT *)&rgn_data->Buffer[0];
356     ok(EqualRect(rect, &client_rect),
357             "Got unexpected clip rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
358             rect->left, rect->top, rect->right, rect->bottom,
359             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
360     HeapFree(GetProcessHeap(), 0, rgn_data);
361
362     r1 = CreateRectRgn(0, 0, 320, 240);
363     ok(!!r1, "Failed to create region.\n");
364     r2 = CreateRectRgn(320, 240, 640, 480);
365     ok(!!r2, "Failed to create region.\n");
366     CombineRgn(r1, r1, r2, RGN_OR);
367     ret = GetRegionData(r1, 0, NULL);
368     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
369     ret = GetRegionData(r1, ret, rgn_data);
370     ok(!!ret, "Failed to get region data.\n");
371
372     DeleteObject(r2);
373     DeleteObject(r1);
374
375     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
376     ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
377     hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
378     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
379     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
380     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
381
382     HeapFree(GetProcessHeap(), 0, rgn_data);
383
384     memset(&surface_desc, 0, sizeof(surface_desc));
385     surface_desc.dwSize = sizeof(surface_desc);
386     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
387     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
388     surface_desc.dwWidth = 640;
389     surface_desc.dwHeight = 480;
390     surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
391     surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
392     U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
393     U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
394     U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
395     U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
396
397     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
398     ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
399     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
400     ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
401
402     memset(&fx, 0, sizeof(fx));
403     fx.dwSize = sizeof(fx);
404     hr = IDirectDrawSurface_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
405     ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
406     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
407     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
408
409     hr = IDirectDrawSurface_Lock(src_surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
410     ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
411     ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
412     ptr = surface_desc.lpSurface;
413     memcpy(&ptr[   0], &src_data[ 0], 6 * sizeof(DWORD));
414     memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
415     memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
416     hr = IDirectDrawSurface_Unlock(src_surface, NULL);
417     ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
418
419     hr = IDirectDrawSurface_SetClipper(dst_surface, clipper);
420     ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
421
422     SetRect(&src_rect, 1, 1, 5, 2);
423     hr = IDirectDrawSurface_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
424     ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
425     for (i = 0; i < 4; ++i)
426     {
427         for (j = 0; j < 4; ++j)
428         {
429             x = 80 * ((2 * j) + 1);
430             y = 60 * ((2 * i) + 1);
431             color = get_surface_color(dst_surface, x, y);
432             ok(compare_color(color, expected1[i * 4 + j], 1),
433                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
434         }
435     }
436
437     U5(fx).dwFillColor = 0xff0000ff;
438     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
439     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
440     for (i = 0; i < 4; ++i)
441     {
442         for (j = 0; j < 4; ++j)
443         {
444             x = 80 * ((2 * j) + 1);
445             y = 60 * ((2 * i) + 1);
446             color = get_surface_color(dst_surface, x, y);
447             ok(compare_color(color, expected2[i * 4 + j], 1),
448                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
449         }
450     }
451
452     hr = IDirectDrawSurface_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
453     ok(hr == DDERR_BLTFASTCANTCLIP || broken(hr == E_NOTIMPL /* NT4 */), "Got unexpected hr %#x.\n", hr);
454
455     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
456     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
457     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
458     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
459     DestroyWindow(window);
460     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
461     ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
462     hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
463     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
464     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
465     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
466     hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
467     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
468     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
469     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
470     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
471     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
472
473     IDirectDrawSurface_Release(dst_surface);
474     IDirectDrawSurface_Release(src_surface);
475     IDirectDrawClipper_Release(clipper);
476     IDirectDraw_Release(ddraw);
477 }
478
479 static void test_coop_level_d3d_state(void)
480 {
481     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
482     D3DMATERIALHANDLE background_handle;
483     IDirectDrawSurface *rt, *surface;
484     IDirect3DMaterial *background;
485     IDirect3DViewport *viewport;
486     IDirect3DDevice *device;
487     D3DMATERIAL material;
488     IDirectDraw *ddraw;
489     D3DVIEWPORT vp;
490     IDirect3D *d3d;
491     D3DCOLOR color;
492     HWND window;
493     HRESULT hr;
494
495     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
496             0, 0, 640, 480, 0, 0, 0, 0);
497     if (!(ddraw = create_ddraw()))
498     {
499         skip("Failed to create ddraw object, skipping test.\n");
500         DestroyWindow(window);
501         return;
502     }
503     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
504     {
505         skip("Failed to create D3D device, skipping test.\n");
506         IDirectDraw_Release(ddraw);
507         DestroyWindow(window);
508         return;
509     }
510
511     hr = IDirect3DDevice_GetDirect3D(device, &d3d);
512     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
513     hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
514     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
515     hr = IDirect3D_CreateMaterial(d3d, &background, NULL);
516     ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
517     IDirect3D_Release(d3d);
518
519     hr = IDirect3DDevice_AddViewport(device, viewport);
520     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
521     memset(&vp, 0, sizeof(vp));
522     vp.dwSize = sizeof(vp);
523     vp.dwX = 0;
524     vp.dwY = 0;
525     vp.dwWidth = 640;
526     vp.dwHeight = 480;
527     vp.dvScaleX = 320.0f;
528     vp.dvScaleY = 240.0f;
529     vp.dvMaxX = 1.0f;
530     vp.dvMaxY = 1.0f;
531     vp.dvMinZ = 0.0f;
532     vp.dvMaxZ = 1.0f;
533     hr = IDirect3DViewport_SetViewport(viewport, &vp);
534     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
535
536     memset(&material, 0, sizeof(material));
537     material.dwSize = sizeof(material);
538     U1(U(material).diffuse).r = 1.0f;
539     U2(U(material).diffuse).g = 0.0f;
540     U3(U(material).diffuse).b = 0.0f;
541     U4(U(material).diffuse).a = 1.0f;
542     hr = IDirect3DMaterial_SetMaterial(background, &material);
543     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
544     hr = IDirect3DMaterial_GetHandle(background, device, &background_handle);
545     ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
546     hr = IDirect3DViewport_SetBackground(viewport, background_handle);
547     ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
548
549     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
550     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
551     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
552     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
553     color = get_surface_color(rt, 320, 240);
554     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
555
556     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
557     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
558     hr = IDirectDrawSurface_IsLost(rt);
559     ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
560     hr = restore_surfaces(ddraw);
561     ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
562
563     memset(&material, 0, sizeof(material));
564     material.dwSize = sizeof(material);
565     U1(U(material).diffuse).r = 0.0f;
566     U2(U(material).diffuse).g = 1.0f;
567     U3(U(material).diffuse).b = 0.0f;
568     U4(U(material).diffuse).a = 1.0f;
569     hr = IDirect3DMaterial_SetMaterial(background, &material);
570     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
571
572     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&surface);
573     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
574     ok(surface == rt, "Got unexpected surface %p.\n", surface);
575     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
576     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
577     color = get_surface_color(rt, 320, 240);
578     ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
579
580     hr = IDirect3DDevice_DeleteViewport(device, viewport);
581     ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
582     IDirect3DMaterial_Release(background);
583     IDirect3DViewport_Release(viewport);
584     IDirectDrawSurface_Release(surface);
585     IDirectDrawSurface_Release(rt);
586     IDirect3DDevice_Release(device);
587     IDirectDraw_Release(ddraw);
588     DestroyWindow(window);
589 }
590
591 static void test_surface_interface_mismatch(void)
592 {
593     IDirectDraw *ddraw = NULL;
594     IDirect3D *d3d = NULL;
595     IDirectDrawSurface *surface = NULL, *ds;
596     IDirectDrawSurface3 *surface3 = NULL;
597     IDirect3DDevice *device = NULL;
598     IDirect3DViewport *viewport = NULL;
599     IDirect3DMaterial *background = NULL;
600     DDSURFACEDESC surface_desc;
601     DWORD z_depth = 0;
602     HRESULT hr;
603     D3DCOLOR color;
604     HWND window;
605     D3DVIEWPORT vp;
606     D3DMATERIAL material;
607     D3DMATERIALHANDLE background_handle;
608     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
609
610     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
611             0, 0, 640, 480, 0, 0, 0, 0);
612
613     if (!(ddraw = create_ddraw()))
614     {
615         skip("Failed to create a ddraw object, skipping test.\n");
616         goto cleanup;
617     }
618
619     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
620     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
621
622     memset(&surface_desc, 0, sizeof(surface_desc));
623     surface_desc.dwSize = sizeof(surface_desc);
624     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
625     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
626     surface_desc.dwWidth = 640;
627     surface_desc.dwHeight = 480;
628
629     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
630     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
631
632     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
633     if (FAILED(hr))
634     {
635         skip("Failed to get the IDirectDrawSurface3 interface, skipping test.\n");
636         goto cleanup;
637     }
638
639     hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
640     if (FAILED(hr))
641     {
642         skip("Failed to get the IDirect3D interface, skipping test.\n");
643         goto cleanup;
644     }
645
646     hr = IDirect3D_EnumDevices(d3d, enum_z_fmt, &z_depth);
647     if (FAILED(hr) || !z_depth)
648     {
649         skip("No depth buffer formats available, skipping test.\n");
650         goto cleanup;
651     }
652
653     memset(&surface_desc, 0, sizeof(surface_desc));
654     surface_desc.dwSize = sizeof(surface_desc);
655     surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
656     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
657     surface_desc.dwZBufferBitDepth = z_depth;
658     surface_desc.dwWidth = 640;
659     surface_desc.dwHeight = 480;
660     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
661     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
662     if (FAILED(hr))
663         goto cleanup;
664
665     /* Using a different surface interface version still works */
666     hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
667     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
668     IDirectDrawSurface_Release(ds);
669     if (FAILED(hr))
670         goto cleanup;
671
672     /* Here too */
673     hr = IDirectDrawSurface3_QueryInterface(surface3, &IID_IDirect3DHALDevice, (void **)&device);
674     ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
675     if (FAILED(hr))
676         goto cleanup;
677
678     hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
679     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
680     hr = IDirect3D_CreateMaterial(d3d, &background, NULL);
681     ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
682
683     hr = IDirect3DDevice_AddViewport(device, viewport);
684     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
685     memset(&vp, 0, sizeof(vp));
686     vp.dwSize = sizeof(vp);
687     vp.dwX = 0;
688     vp.dwY = 0;
689     vp.dwWidth = 640;
690     vp.dwHeight = 480;
691     vp.dvScaleX = 320.0f;
692     vp.dvScaleY = 240.0f;
693     vp.dvMaxX = 1.0f;
694     vp.dvMaxY = 1.0f;
695     vp.dvMinZ = 0.0f;
696     vp.dvMaxZ = 1.0f;
697     hr = IDirect3DViewport_SetViewport(viewport, &vp);
698     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
699
700     memset(&material, 0, sizeof(material));
701     material.dwSize = sizeof(material);
702     U1(U(material).diffuse).r = 1.0f;
703     U2(U(material).diffuse).g = 0.0f;
704     U3(U(material).diffuse).b = 0.0f;
705     U4(U(material).diffuse).a = 1.0f;
706     hr = IDirect3DMaterial_SetMaterial(background, &material);
707     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
708     hr = IDirect3DMaterial_GetHandle(background, device, &background_handle);
709     ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
710     hr = IDirect3DViewport_SetBackground(viewport, background_handle);
711     ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
712
713     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
714     ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
715     color = get_surface_color(surface, 320, 240);
716     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
717
718 cleanup:
719     if (viewport)
720     {
721         IDirect3DDevice2_DeleteViewport(device, viewport);
722         IDirect3DViewport2_Release(viewport);
723     }
724     if (background) IDirect3DMaterial2_Release(background);
725     if (surface3) IDirectDrawSurface3_Release(surface3);
726     if (surface) IDirectDrawSurface7_Release(surface);
727     if (device) IDirect3DDevice2_Release(device);
728     if (d3d) IDirect3D7_Release(d3d);
729     if (ddraw) IDirectDraw7_Release(ddraw);
730     DestroyWindow(window);
731 }
732
733 START_TEST(ddraw1)
734 {
735     test_coop_level_create_device_window();
736     test_clipper_blt();
737     test_coop_level_d3d_state();
738     test_surface_interface_mismatch();
739 }