mshtml: Moved conversion to unicode of buffers from binding to navigate.c.
[wine] / dlls / ddraw / tests / ddraw1.c
1 /*
2  * Copyright 2011-2012 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 #define COBJMACROS
19
20 #include "wine/test.h"
21 #include "d3d.h"
22
23 struct create_window_thread_param
24 {
25     HWND window;
26     HANDLE window_created;
27     HANDLE destroy_window;
28     HANDLE thread;
29 };
30
31 static BOOL compare_color(D3DCOLOR c1, D3DCOLOR c2, BYTE max_diff)
32 {
33     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
34     c1 >>= 8; c2 >>= 8;
35     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
36     c1 >>= 8; c2 >>= 8;
37     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
38     c1 >>= 8; c2 >>= 8;
39     if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
40     return TRUE;
41 }
42
43 static DWORD WINAPI create_window_thread_proc(void *param)
44 {
45     struct create_window_thread_param *p = param;
46     DWORD res;
47     BOOL ret;
48
49     p->window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
50             0, 0, 640, 480, 0, 0, 0, 0);
51     ret = SetEvent(p->window_created);
52     ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
53
54     for (;;)
55     {
56         MSG msg;
57
58         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
59             DispatchMessage(&msg);
60         res = WaitForSingleObject(p->destroy_window, 100);
61         if (res == WAIT_OBJECT_0)
62             break;
63         if (res != WAIT_TIMEOUT)
64         {
65             ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
66             break;
67         }
68     }
69
70     DestroyWindow(p->window);
71
72     return 0;
73 }
74
75 static void create_window_thread(struct create_window_thread_param *p)
76 {
77     DWORD res, tid;
78
79     p->window_created = CreateEvent(NULL, FALSE, FALSE, NULL);
80     ok(!!p->window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
81     p->destroy_window = CreateEvent(NULL, FALSE, FALSE, NULL);
82     ok(!!p->destroy_window, "CreateEvent failed, last error %#x.\n", GetLastError());
83     p->thread = CreateThread(NULL, 0, create_window_thread_proc, p, 0, &tid);
84     ok(!!p->thread, "Failed to create thread, last error %#x.\n", GetLastError());
85     res = WaitForSingleObject(p->window_created, INFINITE);
86     ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
87 }
88
89 static void destroy_window_thread(struct create_window_thread_param *p)
90 {
91     SetEvent(p->destroy_window);
92     WaitForSingleObject(p->thread, INFINITE);
93     CloseHandle(p->destroy_window);
94     CloseHandle(p->window_created);
95     CloseHandle(p->thread);
96 }
97
98 static D3DCOLOR get_surface_color(IDirectDrawSurface *surface, UINT x, UINT y)
99 {
100     RECT rect = {x, y, x + 1, y + 1};
101     DDSURFACEDESC surface_desc;
102     D3DCOLOR color;
103     HRESULT hr;
104
105     memset(&surface_desc, 0, sizeof(surface_desc));
106     surface_desc.dwSize = sizeof(surface_desc);
107
108     hr = IDirectDrawSurface_Lock(surface, &rect, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
109     ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
110     if (FAILED(hr))
111         return 0xdeadbeef;
112
113     color = *((DWORD *)surface_desc.lpSurface) & 0x00ffffff;
114
115     hr = IDirectDrawSurface_Unlock(surface, NULL);
116     ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
117
118     return color;
119 }
120
121 static void emit_process_vertices(void **ptr, WORD base_idx, DWORD vertex_count)
122 {
123     D3DINSTRUCTION *inst = *ptr;
124     D3DPROCESSVERTICES *pv = (D3DPROCESSVERTICES *)(inst + 1);
125
126     inst->bOpcode = D3DOP_PROCESSVERTICES;
127     inst->bSize = sizeof(*pv);
128     inst->wCount = 1;
129
130     pv->dwFlags = D3DPROCESSVERTICES_COPY;
131     pv->wStart = base_idx;
132     pv->wDest = 0;
133     pv->dwCount = vertex_count;
134     pv->dwReserved = 0;
135
136     *ptr = pv + 1;
137 }
138
139 static void emit_set_rs(void **ptr, D3DRENDERSTATETYPE state, DWORD value)
140 {
141     D3DINSTRUCTION *inst = *ptr;
142     D3DSTATE *rs = (D3DSTATE *)(inst + 1);
143
144     inst->bOpcode = D3DOP_STATERENDER;
145     inst->bSize = sizeof(*rs);
146     inst->wCount = 1;
147
148     U1(*rs).drstRenderStateType = state;
149     U2(*rs).dwArg[0] = value;
150
151     *ptr = rs + 1;
152 }
153
154 static void emit_tquad(void **ptr, WORD base_idx)
155 {
156     D3DINSTRUCTION *inst = *ptr;
157     D3DTRIANGLE *tri = (D3DTRIANGLE *)(inst + 1);
158
159     inst->bOpcode = D3DOP_TRIANGLE;
160     inst->bSize = sizeof(*tri);
161     inst->wCount = 2;
162
163     U1(*tri).v1 = base_idx;
164     U2(*tri).v2 = base_idx + 1;
165     U3(*tri).v3 = base_idx + 2;
166     tri->wFlags = D3DTRIFLAG_START;
167     ++tri;
168
169     U1(*tri).v1 = base_idx + 2;
170     U2(*tri).v2 = base_idx + 1;
171     U3(*tri).v3 = base_idx + 3;
172     tri->wFlags = D3DTRIFLAG_ODD;
173     ++tri;
174
175     *ptr = tri;
176 }
177
178 static void emit_end(void **ptr)
179 {
180     D3DINSTRUCTION *inst = *ptr;
181
182     inst->bOpcode = D3DOP_EXIT;
183     inst->bSize = 0;
184     inst->wCount = 0;
185
186     *ptr = inst + 1;
187 }
188
189 static void set_execute_data(IDirect3DExecuteBuffer *execute_buffer, UINT vertex_count, UINT offset, UINT len)
190 {
191     D3DEXECUTEDATA exec_data;
192     HRESULT hr;
193
194     memset(&exec_data, 0, sizeof(exec_data));
195     exec_data.dwSize = sizeof(exec_data);
196     exec_data.dwVertexCount = vertex_count;
197     exec_data.dwInstructionOffset = offset;
198     exec_data.dwInstructionLength = len;
199     hr = IDirect3DExecuteBuffer_SetExecuteData(execute_buffer, &exec_data);
200     ok(SUCCEEDED(hr), "Failed to set execute data, hr %#x.\n", hr);
201 }
202
203 static HRESULT CALLBACK enum_z_fmt(GUID *guid, char *description, char *name,
204         D3DDEVICEDESC *hal_desc, D3DDEVICEDESC *hel_desc, void *ctx)
205 {
206     DWORD *z_depth = ctx;
207
208     if (!IsEqualGUID(&IID_IDirect3DHALDevice, guid))
209         return D3DENUMRET_OK;
210
211     if (hal_desc->dwDeviceZBufferBitDepth & DDBD_32)
212         *z_depth = 32;
213     else if (hal_desc->dwDeviceZBufferBitDepth & DDBD_24)
214         *z_depth = 24;
215     else if (hal_desc->dwDeviceZBufferBitDepth & DDBD_16)
216         *z_depth = 16;
217
218     return DDENUMRET_OK;
219 }
220
221 static IDirectDraw *create_ddraw(void)
222 {
223     IDirectDraw *ddraw;
224
225     if (FAILED(DirectDrawCreate(NULL, &ddraw, NULL)))
226         return NULL;
227
228     return ddraw;
229 }
230
231 static IDirect3DDevice *create_device(IDirectDraw *ddraw, HWND window, DWORD coop_level)
232 {
233     IDirectDrawSurface *surface, *ds;
234     IDirect3DDevice *device = NULL;
235     DDSURFACEDESC surface_desc;
236     DWORD z_depth = 0;
237     IDirect3D *d3d;
238     HRESULT hr;
239
240     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, coop_level);
241     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
242
243     memset(&surface_desc, 0, sizeof(surface_desc));
244     surface_desc.dwSize = sizeof(surface_desc);
245     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
246     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
247     surface_desc.dwWidth = 640;
248     surface_desc.dwHeight = 480;
249
250     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
251     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
252
253     if (coop_level & DDSCL_NORMAL)
254     {
255         IDirectDrawClipper *clipper;
256
257         hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
258         ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
259         hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
260         ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
261         hr = IDirectDrawSurface_SetClipper(surface, clipper);
262         ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
263         IDirectDrawClipper_Release(clipper);
264     }
265
266     hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
267     if (FAILED(hr))
268     {
269         IDirectDrawSurface_Release(surface);
270         return NULL;
271     }
272
273     hr = IDirect3D_EnumDevices(d3d, enum_z_fmt, &z_depth);
274     ok(SUCCEEDED(hr), "Failed to enumerate z-formats, hr %#x.\n", hr);
275     IDirect3D_Release(d3d);
276     if (FAILED(hr) || !z_depth)
277     {
278         IDirectDrawSurface_Release(surface);
279         return NULL;
280     }
281
282     memset(&surface_desc, 0, sizeof(surface_desc));
283     surface_desc.dwSize = sizeof(surface_desc);
284     surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
285     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
286     U2(surface_desc).dwZBufferBitDepth = z_depth;
287     surface_desc.dwWidth = 640;
288     surface_desc.dwHeight = 480;
289     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
290     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
291     if (FAILED(hr))
292     {
293         IDirectDrawSurface_Release(surface);
294         return NULL;
295     }
296
297     hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
298     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
299     IDirectDrawSurface_Release(ds);
300     if (FAILED(hr))
301     {
302         IDirectDrawSurface_Release(surface);
303         return NULL;
304     }
305
306     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DHALDevice, (void **)&device);
307     IDirectDrawSurface_Release(surface);
308     if (FAILED(hr))
309         return NULL;
310
311     return device;
312 }
313
314 static IDirect3DViewport *create_viewport(IDirect3DDevice *device, UINT x, UINT y, UINT w, UINT h)
315 {
316     IDirect3DViewport *viewport;
317     D3DVIEWPORT vp;
318     IDirect3D *d3d;
319     HRESULT hr;
320
321     hr = IDirect3DDevice_GetDirect3D(device, &d3d);
322     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
323     hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
324     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
325     hr = IDirect3DDevice_AddViewport(device, viewport);
326     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
327     memset(&vp, 0, sizeof(vp));
328     vp.dwSize = sizeof(vp);
329     vp.dwX = x;
330     vp.dwY = y;
331     vp.dwWidth = w;
332     vp.dwHeight = h;
333     vp.dvScaleX = (float)w / 2.0f;
334     vp.dvScaleY = (float)h / 2.0f;
335     vp.dvMaxX = 1.0f;
336     vp.dvMaxY = 1.0f;
337     vp.dvMinZ = 0.0f;
338     vp.dvMaxZ = 1.0f;
339     hr = IDirect3DViewport_SetViewport(viewport, &vp);
340     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
341     IDirect3D_Release(d3d);
342
343     return viewport;
344 }
345
346 static void viewport_set_background(IDirect3DDevice *device, IDirect3DViewport *viewport,
347         IDirect3DMaterial *material)
348 {
349     D3DMATERIALHANDLE material_handle;
350     HRESULT hr;
351
352     hr = IDirect3DMaterial2_GetHandle(material, device, &material_handle);
353     ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
354     hr = IDirect3DViewport2_SetBackground(viewport, material_handle);
355     ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
356 }
357
358 static void destroy_viewport(IDirect3DDevice *device, IDirect3DViewport *viewport)
359 {
360     HRESULT hr;
361
362     hr = IDirect3DDevice_DeleteViewport(device, viewport);
363     ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
364     IDirect3DViewport_Release(viewport);
365 }
366
367 static IDirect3DMaterial *create_diffuse_material(IDirect3DDevice *device, float r, float g, float b, float a)
368 {
369     IDirect3DMaterial *material;
370     D3DMATERIAL mat;
371     IDirect3D *d3d;
372     HRESULT hr;
373
374     hr = IDirect3DDevice_GetDirect3D(device, &d3d);
375     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
376     hr = IDirect3D_CreateMaterial(d3d, &material, NULL);
377     ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
378     memset(&mat, 0, sizeof(mat));
379     mat.dwSize = sizeof(mat);
380     U1(U(mat).diffuse).r = r;
381     U2(U(mat).diffuse).g = g;
382     U3(U(mat).diffuse).b = b;
383     U4(U(mat).diffuse).a = a;
384     hr = IDirect3DMaterial_SetMaterial(material, &mat);
385     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
386     IDirect3D_Release(d3d);
387
388     return material;
389 }
390
391 static void destroy_material(IDirect3DMaterial *material)
392 {
393     IDirect3DMaterial_Release(material);
394 }
395
396 static const UINT *expect_messages;
397
398 static LRESULT CALLBACK test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
399 {
400     if (expect_messages && message == *expect_messages)
401         ++expect_messages;
402
403     return DefWindowProcA(hwnd, message, wparam, lparam);
404 }
405
406 /* Set the wndproc back to what ddraw expects it to be, and release the ddraw
407  * interface. This prevents subsequent SetCooperativeLevel() calls on a
408  * different window from failing with DDERR_HWNDALREADYSET. */
409 static void fix_wndproc(HWND window, LONG_PTR proc)
410 {
411     IDirectDraw *ddraw;
412     HRESULT hr;
413
414     if (!(ddraw = create_ddraw()))
415         return;
416
417     SetWindowLongPtrA(window, GWLP_WNDPROC, proc);
418     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
419     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
420     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
421     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
422
423     IDirectDraw_Release(ddraw);
424 }
425
426 static HRESULT CALLBACK restore_callback(IDirectDrawSurface *surface, DDSURFACEDESC *desc, void *context)
427 {
428     HRESULT hr = IDirectDrawSurface_Restore(surface);
429     ok(SUCCEEDED(hr), "Failed to restore surface, hr %#x.\n", hr);
430     IDirectDrawSurface_Release(surface);
431
432     return DDENUMRET_OK;
433 }
434
435 static HRESULT restore_surfaces(IDirectDraw *ddraw)
436 {
437     return IDirectDraw_EnumSurfaces(ddraw, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
438             NULL, NULL, restore_callback);
439 }
440
441 static void test_coop_level_create_device_window(void)
442 {
443     HWND focus_window, device_window;
444     IDirectDraw *ddraw;
445     HRESULT hr;
446
447     focus_window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
448             0, 0, 640, 480, 0, 0, 0, 0);
449     if (!(ddraw = create_ddraw()))
450     {
451         skip("Failed to create a ddraw object, skipping test.\n");
452         DestroyWindow(focus_window);
453         return;
454     }
455
456     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
457     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
458     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
459     ok(!device_window, "Unexpected device window found.\n");
460     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
461     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
462     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
463     ok(!device_window, "Unexpected device window found.\n");
464     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
465     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
466     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
467     ok(!device_window, "Unexpected device window found.\n");
468     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
469     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
470     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
471     ok(!device_window, "Unexpected device window found.\n");
472     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
473     ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
474     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
475     ok(!device_window, "Unexpected device window found.\n");
476
477     /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
478     if (broken(hr == DDERR_INVALIDPARAMS))
479     {
480         win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
481         IDirectDraw_Release(ddraw);
482         DestroyWindow(focus_window);
483         return;
484     }
485
486     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
487     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
488     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
489     ok(!device_window, "Unexpected device window found.\n");
490     hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
491     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
492     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
493     ok(!device_window, "Unexpected device window found.\n");
494
495     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
496     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
497     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
498     ok(!device_window, "Unexpected device window found.\n");
499     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
500             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
501     ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
502     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
503     ok(!!device_window, "Device window not found.\n");
504
505     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
506     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
507     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
508     ok(!device_window, "Unexpected device window found.\n");
509     hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
510             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
511     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
512     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
513     ok(!!device_window, "Device window not found.\n");
514
515     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
516     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
517     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
518     ok(!device_window, "Unexpected device window found.\n");
519     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
520     ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
521     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
522     ok(!device_window, "Unexpected device window found.\n");
523     hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
524     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
525     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
526     ok(!device_window, "Unexpected device window found.\n");
527     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
528     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
529     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
530     ok(!!device_window, "Device window not found.\n");
531
532     IDirectDraw_Release(ddraw);
533     DestroyWindow(focus_window);
534 }
535
536 static void test_clipper_blt(void)
537 {
538     IDirectDrawSurface *src_surface, *dst_surface;
539     RECT client_rect, src_rect, *rect;
540     IDirectDrawClipper *clipper;
541     DDSURFACEDESC surface_desc;
542     unsigned int i, j, x, y;
543     IDirectDraw *ddraw;
544     RGNDATA *rgn_data;
545     D3DCOLOR color;
546     HRGN r1, r2;
547     HWND window;
548     DDBLTFX fx;
549     HRESULT hr;
550     DWORD *ptr;
551     DWORD ret;
552
553     static const DWORD src_data[] =
554     {
555         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
556         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
557         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
558     };
559     static const D3DCOLOR expected1[] =
560     {
561         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
562         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
563         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
564         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
565     };
566     static const D3DCOLOR expected2[] =
567     {
568         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
569         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
570         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
571         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
572     };
573
574     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
575             10, 10, 640, 480, 0, 0, 0, 0);
576     ShowWindow(window, SW_SHOW);
577     if (!(ddraw = create_ddraw()))
578     {
579         skip("Failed to create a ddraw object, skipping test.\n");
580         DestroyWindow(window);
581         return;
582     }
583
584     ret = GetClientRect(window, &client_rect);
585     ok(ret, "Failed to get client rect.\n");
586     ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
587     ok(ret, "Failed to map client rect.\n");
588
589     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
590     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
591
592     hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
593     ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
594     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
595     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
596     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
597     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
598     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
599     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
600     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
601     hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
602     ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
603     ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
604     ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
605     ok(rgn_data->rdh.nCount == 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
606     ok(rgn_data->rdh.nRgnSize == 16 || broken(rgn_data->rdh.nRgnSize == 168 /* NT4 */),
607             "Got unexpected region size %u.\n", rgn_data->rdh.nRgnSize);
608     ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
609             "Got unexpected bounding rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
610             rgn_data->rdh.rcBound.left, rgn_data->rdh.rcBound.top,
611             rgn_data->rdh.rcBound.right, rgn_data->rdh.rcBound.bottom,
612             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
613     rect = (RECT *)&rgn_data->Buffer[0];
614     ok(EqualRect(rect, &client_rect),
615             "Got unexpected clip rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
616             rect->left, rect->top, rect->right, rect->bottom,
617             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
618     HeapFree(GetProcessHeap(), 0, rgn_data);
619
620     r1 = CreateRectRgn(0, 0, 320, 240);
621     ok(!!r1, "Failed to create region.\n");
622     r2 = CreateRectRgn(320, 240, 640, 480);
623     ok(!!r2, "Failed to create region.\n");
624     CombineRgn(r1, r1, r2, RGN_OR);
625     ret = GetRegionData(r1, 0, NULL);
626     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
627     ret = GetRegionData(r1, ret, rgn_data);
628     ok(!!ret, "Failed to get region data.\n");
629
630     DeleteObject(r2);
631     DeleteObject(r1);
632
633     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
634     ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
635     hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
636     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
637     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
638     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
639
640     HeapFree(GetProcessHeap(), 0, rgn_data);
641
642     memset(&surface_desc, 0, sizeof(surface_desc));
643     surface_desc.dwSize = sizeof(surface_desc);
644     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
645     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
646     surface_desc.dwWidth = 640;
647     surface_desc.dwHeight = 480;
648     surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
649     surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
650     U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
651     U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
652     U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
653     U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
654
655     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
656     ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
657     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
658     ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
659
660     memset(&fx, 0, sizeof(fx));
661     fx.dwSize = sizeof(fx);
662     hr = IDirectDrawSurface_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
663     ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
664     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
665     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
666
667     hr = IDirectDrawSurface_Lock(src_surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
668     ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
669     ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
670     ptr = surface_desc.lpSurface;
671     memcpy(&ptr[   0], &src_data[ 0], 6 * sizeof(DWORD));
672     memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
673     memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
674     hr = IDirectDrawSurface_Unlock(src_surface, NULL);
675     ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
676
677     hr = IDirectDrawSurface_SetClipper(dst_surface, clipper);
678     ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
679
680     SetRect(&src_rect, 1, 1, 5, 2);
681     hr = IDirectDrawSurface_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
682     ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
683     for (i = 0; i < 4; ++i)
684     {
685         for (j = 0; j < 4; ++j)
686         {
687             x = 80 * ((2 * j) + 1);
688             y = 60 * ((2 * i) + 1);
689             color = get_surface_color(dst_surface, x, y);
690             ok(compare_color(color, expected1[i * 4 + j], 1),
691                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
692         }
693     }
694
695     U5(fx).dwFillColor = 0xff0000ff;
696     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
697     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
698     for (i = 0; i < 4; ++i)
699     {
700         for (j = 0; j < 4; ++j)
701         {
702             x = 80 * ((2 * j) + 1);
703             y = 60 * ((2 * i) + 1);
704             color = get_surface_color(dst_surface, x, y);
705             ok(compare_color(color, expected2[i * 4 + j], 1),
706                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
707         }
708     }
709
710     hr = IDirectDrawSurface_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
711     ok(hr == DDERR_BLTFASTCANTCLIP || broken(hr == E_NOTIMPL /* NT4 */), "Got unexpected hr %#x.\n", hr);
712
713     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
714     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
715     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
716     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
717     DestroyWindow(window);
718     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
719     ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
720     hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
721     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
722     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
723     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
724     hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
725     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
726     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
727     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
728     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
729     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
730
731     IDirectDrawSurface_Release(dst_surface);
732     IDirectDrawSurface_Release(src_surface);
733     IDirectDrawClipper_Release(clipper);
734     IDirectDraw_Release(ddraw);
735 }
736
737 static void test_coop_level_d3d_state(void)
738 {
739     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
740     IDirectDrawSurface *rt, *surface;
741     IDirect3DMaterial *background;
742     IDirect3DViewport *viewport;
743     IDirect3DDevice *device;
744     D3DMATERIAL material;
745     IDirectDraw *ddraw;
746     D3DCOLOR color;
747     HWND window;
748     HRESULT hr;
749
750     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
751             0, 0, 640, 480, 0, 0, 0, 0);
752     if (!(ddraw = create_ddraw()))
753     {
754         skip("Failed to create ddraw object, skipping test.\n");
755         DestroyWindow(window);
756         return;
757     }
758     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
759     {
760         skip("Failed to create D3D device, skipping test.\n");
761         IDirectDraw_Release(ddraw);
762         DestroyWindow(window);
763         return;
764     }
765
766     background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
767     viewport = create_viewport(device, 0, 0, 640, 480);
768     viewport_set_background(device, viewport, background);
769
770     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
771     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
772     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
773     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
774     color = get_surface_color(rt, 320, 240);
775     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
776
777     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
778     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
779     hr = IDirectDrawSurface_IsLost(rt);
780     ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
781     hr = restore_surfaces(ddraw);
782     ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
783
784     memset(&material, 0, sizeof(material));
785     material.dwSize = sizeof(material);
786     U1(U(material).diffuse).r = 0.0f;
787     U2(U(material).diffuse).g = 1.0f;
788     U3(U(material).diffuse).b = 0.0f;
789     U4(U(material).diffuse).a = 1.0f;
790     hr = IDirect3DMaterial_SetMaterial(background, &material);
791     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
792
793     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&surface);
794     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
795     ok(surface == rt, "Got unexpected surface %p.\n", surface);
796     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
797     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
798     color = get_surface_color(rt, 320, 240);
799     ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
800
801     destroy_viewport(device, viewport);
802     destroy_material(background);
803     IDirectDrawSurface_Release(surface);
804     IDirectDrawSurface_Release(rt);
805     IDirect3DDevice_Release(device);
806     IDirectDraw_Release(ddraw);
807     DestroyWindow(window);
808 }
809
810 static void test_surface_interface_mismatch(void)
811 {
812     IDirectDraw *ddraw = NULL;
813     IDirect3D *d3d = NULL;
814     IDirectDrawSurface *surface = NULL, *ds;
815     IDirectDrawSurface3 *surface3 = NULL;
816     IDirect3DDevice *device = NULL;
817     IDirect3DViewport *viewport = NULL;
818     IDirect3DMaterial *background = NULL;
819     DDSURFACEDESC surface_desc;
820     DWORD z_depth = 0;
821     ULONG refcount;
822     HRESULT hr;
823     D3DCOLOR color;
824     HWND window;
825     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
826
827     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
828             0, 0, 640, 480, 0, 0, 0, 0);
829
830     if (!(ddraw = create_ddraw()))
831     {
832         skip("Failed to create a ddraw object, skipping test.\n");
833         goto cleanup;
834     }
835
836     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
837     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
838
839     memset(&surface_desc, 0, sizeof(surface_desc));
840     surface_desc.dwSize = sizeof(surface_desc);
841     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
842     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
843     surface_desc.dwWidth = 640;
844     surface_desc.dwHeight = 480;
845
846     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
847     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
848
849     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
850     if (FAILED(hr))
851     {
852         skip("Failed to get the IDirectDrawSurface3 interface, skipping test.\n");
853         goto cleanup;
854     }
855
856     hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
857     if (FAILED(hr))
858     {
859         skip("Failed to get the IDirect3D interface, skipping test.\n");
860         goto cleanup;
861     }
862
863     hr = IDirect3D_EnumDevices(d3d, enum_z_fmt, &z_depth);
864     if (FAILED(hr) || !z_depth)
865     {
866         skip("No depth buffer formats available, skipping test.\n");
867         goto cleanup;
868     }
869
870     memset(&surface_desc, 0, sizeof(surface_desc));
871     surface_desc.dwSize = sizeof(surface_desc);
872     surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
873     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
874     U2(surface_desc).dwZBufferBitDepth = z_depth;
875     surface_desc.dwWidth = 640;
876     surface_desc.dwHeight = 480;
877     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
878     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
879     if (FAILED(hr))
880         goto cleanup;
881
882     /* Using a different surface interface version still works */
883     hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
884     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
885     refcount = IDirectDrawSurface_Release(ds);
886     ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
887     if (FAILED(hr))
888         goto cleanup;
889
890     /* Here too */
891     hr = IDirectDrawSurface3_QueryInterface(surface3, &IID_IDirect3DHALDevice, (void **)&device);
892     ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
893     if (FAILED(hr))
894         goto cleanup;
895
896     background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
897     viewport = create_viewport(device, 0, 0, 640, 480);
898     viewport_set_background(device, viewport, background);
899
900     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
901     ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
902     color = get_surface_color(surface, 320, 240);
903     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
904
905 cleanup:
906     if (viewport)
907         destroy_viewport(device, viewport);
908     if (background)
909         destroy_material(background);
910     if (surface3) IDirectDrawSurface3_Release(surface3);
911     if (surface) IDirectDrawSurface_Release(surface);
912     if (device) IDirect3DDevice_Release(device);
913     if (d3d) IDirect3D_Release(d3d);
914     if (ddraw) IDirectDraw_Release(ddraw);
915     DestroyWindow(window);
916 }
917
918 static void test_coop_level_threaded(void)
919 {
920     struct create_window_thread_param p;
921     IDirectDraw *ddraw;
922     HRESULT hr;
923
924     if (!(ddraw = create_ddraw()))
925     {
926         skip("Failed to create a ddraw object, skipping test.\n");
927         return;
928     }
929     create_window_thread(&p);
930
931     hr = IDirectDraw_SetCooperativeLevel(ddraw, p.window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
932     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
933
934     IDirectDraw_Release(ddraw);
935     destroy_window_thread(&p);
936 }
937
938 static ULONG get_refcount(IUnknown *test_iface)
939 {
940     IUnknown_AddRef(test_iface);
941     return IUnknown_Release(test_iface);
942 }
943
944 static void test_viewport_interfaces(void)
945 {
946     IDirectDraw *ddraw;
947     IDirect3D *d3d;
948     HRESULT hr;
949     ULONG ref;
950     IDirect3DViewport *viewport;
951     IDirect3DViewport2 *viewport2;
952     IDirect3DViewport3 *viewport3;
953     IDirectDrawGammaControl *gamma;
954     IUnknown *unknown;
955
956     if (!(ddraw = create_ddraw()))
957     {
958         skip("Failed to create ddraw object, skipping test.\n");
959         return;
960     }
961     hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
962     ok(SUCCEEDED(hr) || hr == E_NOINTERFACE, "Failed to get d3d interface, hr %#x.\n", hr);
963     if (FAILED(hr))
964     {
965         skip("Direct3D not available, skipping tests\n");
966         IDirectDraw_Release(ddraw);
967         return;
968     }
969     ref = get_refcount((IUnknown *)d3d);
970     ok(ref == 2, "IDirect3D refcount is %d\n", ref);
971
972     hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
973     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
974     ref = get_refcount((IUnknown *)viewport);
975     ok(ref == 1, "Initial IDirect3DViewport refcount is %u\n", ref);
976     ref = get_refcount((IUnknown *)d3d);
977     ok(ref == 2, "IDirect3D refcount is %u\n", ref);
978
979     /* E_FAIL return values are returned by Winetestbot Windows NT machines. While not supporting
980      * newer interfaces is legitimate for old ddraw versions, E_FAIL violates Microsoft's rules
981      * for QueryInterface, hence the broken() */
982     gamma = (IDirectDrawGammaControl *)0xdeadbeef;
983     hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirectDrawGammaControl, (void **)&gamma);
984     ok(hr == E_NOINTERFACE || broken(hr == E_FAIL), "Got unexpected hr %#x.\n", hr);
985     ok(gamma == NULL, "Interface not set to NULL by failed QI call: %p\n", gamma);
986     if (SUCCEEDED(hr)) IDirectDrawGammaControl_Release(gamma);
987     /* NULL iid: Segfaults */
988
989     hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirect3DViewport2, (void **)&viewport2);
990     ok(SUCCEEDED(hr) || hr == E_NOINTERFACE || broken(hr == E_FAIL),
991             "Failed to QI IDirect3DViewport2, hr %#x.\n", hr);
992     if (viewport2)
993     {
994         ref = get_refcount((IUnknown *)viewport);
995         ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
996         ref = get_refcount((IUnknown *)viewport2);
997         ok(ref == 2, "IDirect3DViewport2 refcount is %u\n", ref);
998         IDirect3DViewport2_Release(viewport2);
999         viewport2 = NULL;
1000     }
1001
1002     hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirect3DViewport3, (void **)&viewport3);
1003     ok(SUCCEEDED(hr) || hr == E_NOINTERFACE || broken(hr == E_FAIL),
1004             "Failed to QI IDirect3DViewport3, hr %#x.\n", hr);
1005     if (viewport3)
1006     {
1007         ref = get_refcount((IUnknown *)viewport);
1008         ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
1009         ref = get_refcount((IUnknown *)viewport3);
1010         ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1011         IDirect3DViewport3_Release(viewport3);
1012     }
1013
1014     hr = IDirect3DViewport_QueryInterface(viewport, &IID_IUnknown, (void **)&unknown);
1015     ok(SUCCEEDED(hr), "Failed to QI IUnknown, hr %#x.\n", hr);
1016     if (unknown)
1017     {
1018         ref = get_refcount((IUnknown *)viewport);
1019         ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
1020         ref = get_refcount(unknown);
1021         ok(ref == 2, "IUnknown refcount is %u\n", ref);
1022         IUnknown_Release(unknown);
1023     }
1024
1025     IDirect3DViewport_Release(viewport);
1026     IDirect3D_Release(d3d);
1027     IDirectDraw_Release(ddraw);
1028 }
1029
1030 static void test_zenable(void)
1031 {
1032     static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1033     static D3DTLVERTEX tquad[] =
1034     {
1035         {{  0.0f}, {480.0f}, {-0.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1036         {{  0.0f}, {  0.0f}, {-0.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1037         {{640.0f}, {480.0f}, { 1.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1038         {{640.0f}, {  0.0f}, { 1.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1039     };
1040     IDirect3DExecuteBuffer *execute_buffer;
1041     D3DEXECUTEBUFFERDESC exec_desc;
1042     IDirect3DMaterial *background;
1043     IDirect3DViewport *viewport;
1044     IDirect3DDevice *device;
1045     IDirectDrawSurface *rt;
1046     IDirectDraw *ddraw;
1047     UINT inst_length;
1048     D3DCOLOR color;
1049     HWND window;
1050     HRESULT hr;
1051     UINT x, y;
1052     UINT i, j;
1053     void *ptr;
1054
1055     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1056             0, 0, 640, 480, 0, 0, 0, 0);
1057     if (!(ddraw = create_ddraw()))
1058     {
1059         skip("Failed to create ddraw object, skipping test.\n");
1060         DestroyWindow(window);
1061         return;
1062     }
1063     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1064     {
1065         skip("Failed to create D3D device, skipping test.\n");
1066         IDirectDraw_Release(ddraw);
1067         DestroyWindow(window);
1068         return;
1069     }
1070
1071     background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
1072     viewport = create_viewport(device, 0, 0, 640, 480);
1073     viewport_set_background(device, viewport, background);
1074
1075     memset(&exec_desc, 0, sizeof(exec_desc));
1076     exec_desc.dwSize = sizeof(exec_desc);
1077     exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
1078     exec_desc.dwBufferSize = 1024;
1079     exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
1080
1081     hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
1082     ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
1083     hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
1084     ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
1085     memcpy(exec_desc.lpData, tquad, sizeof(tquad));
1086     ptr = ((BYTE *)exec_desc.lpData) + sizeof(tquad);
1087     emit_process_vertices(&ptr, 0, 4);
1088     emit_set_rs(&ptr, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
1089     emit_tquad(&ptr, 0);
1090     emit_end(&ptr);
1091     inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
1092     inst_length -= sizeof(tquad);
1093     hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
1094     ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
1095
1096     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1097     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1098     hr = IDirect3DDevice_BeginScene(device);
1099     ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1100     set_execute_data(execute_buffer, 4, sizeof(tquad), inst_length);
1101     hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1102     ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1103     hr = IDirect3DDevice_EndScene(device);
1104     ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1105
1106     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
1107     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1108     for (i = 0; i < 4; ++i)
1109     {
1110         for (j = 0; j < 4; ++j)
1111         {
1112             x = 80 * ((2 * j) + 1);
1113             y = 60 * ((2 * i) + 1);
1114             color = get_surface_color(rt, x, y);
1115             ok(compare_color(color, 0x0000ff00, 1),
1116                     "Expected color 0x0000ff00 at %u, %u, got 0x%08x.\n", x, y, color);
1117         }
1118     }
1119     IDirectDrawSurface_Release(rt);
1120
1121     destroy_viewport(device, viewport);
1122     IDirect3DExecuteBuffer_Release(execute_buffer);
1123     destroy_material(background);
1124     IDirect3DDevice_Release(device);
1125     IDirectDraw_Release(ddraw);
1126     DestroyWindow(window);
1127 }
1128
1129 static void test_ck_rgba(void)
1130 {
1131     static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1132     static D3DTLVERTEX tquad[] =
1133     {
1134         {{  0.0f}, {480.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1135         {{  0.0f}, {  0.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1136         {{640.0f}, {480.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1137         {{640.0f}, {  0.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1138         {{  0.0f}, {480.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1139         {{  0.0f}, {  0.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1140         {{640.0f}, {480.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1141         {{640.0f}, {  0.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1142     };
1143     static const struct
1144     {
1145         D3DCOLOR fill_color;
1146         BOOL color_key;
1147         BOOL blend;
1148         D3DCOLOR result1;
1149         D3DCOLOR result2;
1150     }
1151     tests[] =
1152     {
1153         {0xff00ff00, TRUE,  TRUE,  0x00ff0000, 0x000000ff},
1154         {0xff00ff00, TRUE,  FALSE, 0x00ff0000, 0x000000ff},
1155         {0xff00ff00, FALSE, TRUE,  0x0000ff00, 0x0000ff00},
1156         {0xff00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00},
1157         {0x7f00ff00, TRUE,  TRUE,  0x00807f00, 0x00807f00},
1158         {0x7f00ff00, TRUE,  FALSE, 0x0000ff00, 0x0000ff00},
1159         {0x7f00ff00, FALSE, TRUE,  0x00807f00, 0x00807f00},
1160         {0x7f00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00},
1161     };
1162
1163     IDirect3DExecuteBuffer *execute_buffer;
1164     D3DTEXTUREHANDLE texture_handle;
1165     D3DEXECUTEBUFFERDESC exec_desc;
1166     IDirect3DMaterial *background;
1167     IDirectDrawSurface *surface;
1168     IDirect3DViewport *viewport;
1169     DDSURFACEDESC surface_desc;
1170     IDirect3DTexture *texture;
1171     IDirect3DDevice *device;
1172     IDirectDrawSurface *rt;
1173     IDirectDraw *ddraw;
1174     D3DCOLOR color;
1175     HWND window;
1176     DDBLTFX fx;
1177     HRESULT hr;
1178     UINT i;
1179
1180     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1181             0, 0, 640, 480, 0, 0, 0, 0);
1182     if (!(ddraw = create_ddraw()))
1183     {
1184         skip("Failed to create ddraw object, skipping test.\n");
1185         DestroyWindow(window);
1186         return;
1187     }
1188     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1189     {
1190         skip("Failed to create D3D device, skipping test.\n");
1191         IDirectDraw_Release(ddraw);
1192         DestroyWindow(window);
1193         return;
1194     }
1195
1196     background = create_diffuse_material(device, 1.0, 0.0f, 0.0f, 1.0f);
1197     viewport = create_viewport(device, 0, 0, 640, 480);
1198     viewport_set_background(device, viewport, background);
1199
1200     memset(&surface_desc, 0, sizeof(surface_desc));
1201     surface_desc.dwSize = sizeof(surface_desc);
1202     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1203     surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1204     surface_desc.dwWidth = 256;
1205     surface_desc.dwHeight = 256;
1206     surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
1207     surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
1208     U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
1209     U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1210     U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1211     U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
1212     U5(surface_desc.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
1213     surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0xff00ff00;
1214     surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0xff00ff00;
1215     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1216     ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
1217     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
1218     ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1219     hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
1220     ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
1221     IDirect3DTexture_Release(texture);
1222
1223     memset(&exec_desc, 0, sizeof(exec_desc));
1224     exec_desc.dwSize = sizeof(exec_desc);
1225     exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
1226     exec_desc.dwBufferSize = 1024;
1227     exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
1228     hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
1229     ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
1230
1231     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
1232     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1233
1234     for (i = 0; i < sizeof(tests) / sizeof(*tests); ++i)
1235     {
1236         UINT draw1_len, draw2_len;
1237         void *ptr;
1238
1239         hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
1240         ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
1241         memcpy(exec_desc.lpData, tquad, sizeof(tquad));
1242         ptr = ((BYTE *)exec_desc.lpData) + sizeof(tquad);
1243         emit_process_vertices(&ptr, 0, 4);
1244         emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
1245         emit_set_rs(&ptr, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
1246         emit_set_rs(&ptr, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
1247         emit_set_rs(&ptr, D3DRENDERSTATE_COLORKEYENABLE, tests[i].color_key);
1248         emit_set_rs(&ptr, D3DRENDERSTATE_ALPHABLENDENABLE, tests[i].blend);
1249         emit_tquad(&ptr, 0);
1250         emit_end(&ptr);
1251         draw1_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - sizeof(tquad);
1252         emit_process_vertices(&ptr, 4, 4);
1253         emit_tquad(&ptr, 0);
1254         emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, 0);
1255         emit_end(&ptr);
1256         draw2_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw1_len;
1257         hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
1258         ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
1259
1260         memset(&fx, 0, sizeof(fx));
1261         fx.dwSize = sizeof(fx);
1262         U5(fx).dwFillColor = tests[i].fill_color;
1263         hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1264         ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1265
1266         hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER);
1267         ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1268         hr = IDirect3DDevice_BeginScene(device);
1269         ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1270         set_execute_data(execute_buffer, 8, sizeof(tquad), draw1_len);
1271         hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1272         ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1273         hr = IDirect3DDevice_EndScene(device);
1274         ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1275
1276         color = get_surface_color(rt, 320, 240);
1277         if (i == 2)
1278             todo_wine ok(compare_color(color, tests[i].result1, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1279                     tests[i].result1, i, color);
1280         else
1281             ok(compare_color(color, tests[i].result1, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1282                     tests[i].result1, i, color);
1283
1284         U5(fx).dwFillColor = 0xff0000ff;
1285         hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1286         ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1287
1288         hr = IDirect3DDevice_BeginScene(device);
1289         ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1290         set_execute_data(execute_buffer, 8, sizeof(tquad) + draw1_len, draw2_len);
1291         hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1292         ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1293         hr = IDirect3DDevice_EndScene(device);
1294         ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1295
1296         /* This tests that fragments that are masked out by the color key are
1297          * discarded, instead of just fully transparent. */
1298         color = get_surface_color(rt, 320, 240);
1299         if (i == 2)
1300             todo_wine ok(compare_color(color, tests[i].result2, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1301                     tests[i].result2, i, color);
1302         else
1303             ok(compare_color(color, tests[i].result2, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1304                     tests[i].result2, i, color);
1305     }
1306
1307     IDirectDrawSurface_Release(rt);
1308     IDirect3DExecuteBuffer_Release(execute_buffer);
1309     IDirectDrawSurface_Release(surface);
1310     destroy_viewport(device, viewport);
1311     destroy_material(background);
1312     IDirect3DDevice_Release(device);
1313     IDirectDraw_Release(ddraw);
1314     DestroyWindow(window);
1315 }
1316
1317 static void test_ck_default(void)
1318 {
1319     static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1320     static D3DTLVERTEX tquad[] =
1321     {
1322         {{  0.0f}, {480.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1323         {{  0.0f}, {  0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1324         {{640.0f}, {480.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1325         {{640.0f}, {  0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1326     };
1327     IDirect3DExecuteBuffer *execute_buffer;
1328     IDirectDrawSurface *surface, *rt;
1329     D3DTEXTUREHANDLE texture_handle;
1330     D3DEXECUTEBUFFERDESC exec_desc;
1331     IDirect3DMaterial *background;
1332     UINT draw1_offset, draw1_len;
1333     UINT draw2_offset, draw2_len;
1334     UINT draw3_offset, draw3_len;
1335     UINT draw4_offset, draw4_len;
1336     IDirect3DViewport *viewport;
1337     DDSURFACEDESC surface_desc;
1338     IDirect3DTexture *texture;
1339     IDirect3DDevice *device;
1340     IDirectDraw *ddraw;
1341     D3DCOLOR color;
1342     HWND window;
1343     DDBLTFX fx;
1344     HRESULT hr;
1345     void *ptr;
1346
1347     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1348             0, 0, 640, 480, 0, 0, 0, 0);
1349
1350     if (!(ddraw = create_ddraw()))
1351     {
1352         skip("Failed to create ddraw object, skipping test.\n");
1353         DestroyWindow(window);
1354         return;
1355     }
1356     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1357     {
1358         skip("Failed to create D3D device, skipping test.\n");
1359         IDirectDraw_Release(ddraw);
1360         DestroyWindow(window);
1361         return;
1362     }
1363
1364     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
1365     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1366
1367     background = create_diffuse_material(device, 0.0, 1.0f, 0.0f, 1.0f);
1368     viewport = create_viewport(device, 0, 0, 640, 480);
1369     viewport_set_background(device, viewport, background);
1370
1371     memset(&surface_desc, 0, sizeof(surface_desc));
1372     surface_desc.dwSize = sizeof(surface_desc);
1373     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1374     surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1375     surface_desc.dwWidth = 256;
1376     surface_desc.dwHeight = 256;
1377     surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
1378     surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
1379     U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
1380     U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1381     U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1382     U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
1383     surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x000000ff;
1384     surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x000000ff;
1385     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1386     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1387     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
1388     ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1389     hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
1390     ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
1391     IDirect3DTexture_Release(texture);
1392
1393     memset(&fx, 0, sizeof(fx));
1394     fx.dwSize = sizeof(fx);
1395     U5(fx).dwFillColor = 0x000000ff;
1396     hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1397     ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
1398
1399     memset(&exec_desc, 0, sizeof(exec_desc));
1400     exec_desc.dwSize = sizeof(exec_desc);
1401     exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
1402     exec_desc.dwBufferSize = 1024;
1403     exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
1404     hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
1405     ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
1406
1407     hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
1408     ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
1409     memcpy(exec_desc.lpData, tquad, sizeof(tquad));
1410     ptr = (BYTE *)exec_desc.lpData + sizeof(tquad);
1411     emit_process_vertices(&ptr, 0, 4);
1412     emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
1413     emit_tquad(&ptr, 0);
1414     emit_end(&ptr);
1415     draw1_offset = sizeof(tquad);
1416     draw1_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw1_offset;
1417     emit_process_vertices(&ptr, 0, 4);
1418     emit_set_rs(&ptr, D3DRENDERSTATE_COLORKEYENABLE, FALSE);
1419     emit_tquad(&ptr, 0);
1420     emit_end(&ptr);
1421     draw2_offset = draw1_offset + draw1_len;
1422     draw2_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw2_offset;
1423     emit_process_vertices(&ptr, 0, 4);
1424     emit_tquad(&ptr, 0);
1425     emit_end(&ptr);
1426     draw3_offset = draw2_offset + draw2_len;
1427     draw3_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw3_offset;
1428     emit_process_vertices(&ptr, 0, 4);
1429     emit_set_rs(&ptr, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
1430     emit_tquad(&ptr, 0);
1431     emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, 0);
1432     emit_end(&ptr);
1433     draw4_offset = draw3_offset + draw3_len;
1434     draw4_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw4_offset;
1435     hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
1436     ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
1437
1438     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1439     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1440     hr = IDirect3DDevice_BeginScene(device);
1441     ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1442     set_execute_data(execute_buffer, 4, draw1_offset, draw1_len);
1443     hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1444     ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1445     hr = IDirect3DDevice_EndScene(device);
1446     ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1447     color = get_surface_color(rt, 320, 240);
1448     ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1449
1450     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1451     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1452     hr = IDirect3DDevice_BeginScene(device);
1453     ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1454     set_execute_data(execute_buffer, 4, draw2_offset, draw2_len);
1455     hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1456     ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1457     hr = IDirect3DDevice_EndScene(device);
1458     ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1459     color = get_surface_color(rt, 320, 240);
1460     ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
1461
1462     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1463     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1464     hr = IDirect3DDevice_BeginScene(device);
1465     ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1466     set_execute_data(execute_buffer, 4, draw3_offset, draw3_len);
1467     hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1468     ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1469     hr = IDirect3DDevice_EndScene(device);
1470     ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1471     color = get_surface_color(rt, 320, 240);
1472     ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
1473
1474     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1475     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1476     hr = IDirect3DDevice_BeginScene(device);
1477     ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1478     set_execute_data(execute_buffer, 4, draw4_offset, draw4_len);
1479     hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1480     ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1481     hr = IDirect3DDevice_EndScene(device);
1482     ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1483     color = get_surface_color(rt, 320, 240);
1484     ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1485
1486     IDirect3DExecuteBuffer_Release(execute_buffer);
1487     IDirectDrawSurface_Release(surface);
1488     destroy_viewport(device, viewport);
1489     destroy_material(background);
1490     IDirectDrawSurface_Release(rt);
1491     IDirect3DDevice_Release(device);
1492     IDirectDraw_Release(ddraw);
1493     DestroyWindow(window);
1494 }
1495
1496 struct qi_test
1497 {
1498     REFIID iid;
1499     REFIID refcount_iid;
1500     HRESULT hr;
1501 };
1502
1503 static void test_qi(const char *test_name, IUnknown *base_iface,
1504         REFIID refcount_iid, const struct qi_test *tests, UINT entry_count)
1505 {
1506     ULONG refcount, expected_refcount;
1507     IUnknown *iface1, *iface2;
1508     HRESULT hr;
1509     UINT i, j;
1510
1511     for (i = 0; i < entry_count; ++i)
1512     {
1513         hr = IUnknown_QueryInterface(base_iface, tests[i].iid, (void **)&iface1);
1514         ok(hr == tests[i].hr, "Got hr %#x for test \"%s\" %u.\n", hr, test_name, i);
1515         if (SUCCEEDED(hr))
1516         {
1517             for (j = 0; j < entry_count; ++j)
1518             {
1519                 hr = IUnknown_QueryInterface(iface1, tests[j].iid, (void **)&iface2);
1520                 ok(hr == tests[j].hr, "Got hr %#x for test \"%s\" %u, %u.\n", hr, test_name, i, j);
1521                 if (SUCCEEDED(hr))
1522                 {
1523                     expected_refcount = 0;
1524                     if (IsEqualGUID(refcount_iid, tests[j].refcount_iid))
1525                         ++expected_refcount;
1526                     if (IsEqualGUID(tests[i].refcount_iid, tests[j].refcount_iid))
1527                         ++expected_refcount;
1528                     refcount = IUnknown_Release(iface2);
1529                     ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, %u, expected %u.\n",
1530                             refcount, test_name, i, j, expected_refcount);
1531                 }
1532             }
1533
1534             expected_refcount = 0;
1535             if (IsEqualGUID(refcount_iid, tests[i].refcount_iid))
1536                 ++expected_refcount;
1537             refcount = IUnknown_Release(iface1);
1538             ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, expected %u.\n",
1539                     refcount, test_name, i, expected_refcount);
1540         }
1541     }
1542 }
1543
1544 static void test_surface_qi(void)
1545 {
1546     static const struct qi_test tests[] =
1547     {
1548         {&IID_IDirect3DTexture2,        &IID_IDirectDrawSurface,        S_OK         },
1549         {&IID_IDirect3DTexture,         &IID_IDirectDrawSurface,        S_OK         },
1550         {&IID_IDirectDrawGammaControl,  &IID_IDirectDrawGammaControl,   S_OK         },
1551         {&IID_IDirectDrawColorControl,  NULL,                           E_NOINTERFACE},
1552         {&IID_IDirectDrawSurface7,      &IID_IDirectDrawSurface7,       S_OK         },
1553         {&IID_IDirectDrawSurface4,      &IID_IDirectDrawSurface4,       S_OK         },
1554         {&IID_IDirectDrawSurface3,      &IID_IDirectDrawSurface3,       S_OK         },
1555         {&IID_IDirectDrawSurface2,      &IID_IDirectDrawSurface2,       S_OK         },
1556         {&IID_IDirectDrawSurface,       &IID_IDirectDrawSurface,        S_OK         },
1557         {&IID_IDirect3DDevice7,         NULL,                           E_INVALIDARG },
1558         {&IID_IDirect3DDevice3,         NULL,                           E_INVALIDARG },
1559         {&IID_IDirect3DDevice2,         NULL,                           E_INVALIDARG },
1560         {&IID_IDirect3DDevice,          NULL,                           E_INVALIDARG },
1561         {&IID_IDirect3D7,               NULL,                           E_INVALIDARG },
1562         {&IID_IDirect3D3,               NULL,                           E_INVALIDARG },
1563         {&IID_IDirect3D2,               NULL,                           E_INVALIDARG },
1564         {&IID_IDirect3D,                NULL,                           E_INVALIDARG },
1565         {&IID_IDirectDraw7,             NULL,                           E_INVALIDARG },
1566         {&IID_IDirectDraw4,             NULL,                           E_INVALIDARG },
1567         {&IID_IDirectDraw3,             NULL,                           E_INVALIDARG },
1568         {&IID_IDirectDraw2,             NULL,                           E_INVALIDARG },
1569         {&IID_IDirectDraw,              NULL,                           E_INVALIDARG },
1570         {&IID_IDirect3DLight,           NULL,                           E_INVALIDARG },
1571         {&IID_IDirect3DMaterial,        NULL,                           E_INVALIDARG },
1572         {&IID_IDirect3DMaterial2,       NULL,                           E_INVALIDARG },
1573         {&IID_IDirect3DMaterial3,       NULL,                           E_INVALIDARG },
1574         {&IID_IDirect3DExecuteBuffer,   NULL,                           E_INVALIDARG },
1575         {&IID_IDirect3DViewport,        NULL,                           E_INVALIDARG },
1576         {&IID_IDirect3DViewport2,       NULL,                           E_INVALIDARG },
1577         {&IID_IDirect3DViewport3,       NULL,                           E_INVALIDARG },
1578         {&IID_IDirect3DVertexBuffer,    NULL,                           E_INVALIDARG },
1579         {&IID_IDirect3DVertexBuffer7,   NULL,                           E_INVALIDARG },
1580         {&IID_IDirectDrawPalette,       NULL,                           E_INVALIDARG },
1581         {&IID_IDirectDrawClipper,       NULL,                           E_INVALIDARG },
1582         {&IID_IUnknown,                 &IID_IDirectDrawSurface,        S_OK         },
1583     };
1584
1585     IDirectDrawSurface *surface;
1586     DDSURFACEDESC surface_desc;
1587     IDirect3DDevice *device;
1588     IDirectDraw *ddraw;
1589     HWND window;
1590     HRESULT hr;
1591
1592     if (!GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx"))
1593     {
1594         win_skip("DirectDrawCreateEx not available, skipping test.\n");
1595         return;
1596     }
1597
1598     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1599             0, 0, 640, 480, 0, 0, 0, 0);
1600     if (!(ddraw = create_ddraw()))
1601     {
1602         skip("Failed to create a ddraw object, skipping test.\n");
1603         DestroyWindow(window);
1604         return;
1605     }
1606     /* Try to create a D3D device to see if the ddraw implementation supports
1607      * D3D. 64-bit ddraw in particular doesn't seem to support D3D, and
1608      * doesn't support e.g. the IDirect3DTexture interfaces. */
1609     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1610     {
1611         skip("Failed to create D3D device, skipping test.\n");
1612         IDirectDraw_Release(ddraw);
1613         DestroyWindow(window);
1614         return;
1615     }
1616     IDirect3DDevice_Release(device);
1617
1618     memset(&surface_desc, 0, sizeof(surface_desc));
1619     surface_desc.dwSize = sizeof(surface_desc);
1620     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1621     surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1622     surface_desc.dwWidth = 512;
1623     surface_desc.dwHeight = 512;
1624     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1625     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1626
1627     test_qi("surface_qi", (IUnknown *)surface, &IID_IDirectDrawSurface, tests, sizeof(tests) / sizeof(*tests));
1628
1629     IDirectDrawSurface_Release(surface);
1630     IDirectDraw_Release(ddraw);
1631     DestroyWindow(window);
1632 }
1633
1634 static void test_device_qi(void)
1635 {
1636     static const struct qi_test tests[] =
1637     {
1638         {&IID_IDirect3DTexture2,        &IID_IDirectDrawSurface,        S_OK         },
1639         {&IID_IDirect3DTexture,         &IID_IDirectDrawSurface,        S_OK         },
1640         {&IID_IDirectDrawGammaControl,  &IID_IDirectDrawGammaControl,   S_OK         },
1641         {&IID_IDirectDrawColorControl,  NULL,                           E_NOINTERFACE},
1642         {&IID_IDirectDrawSurface7,      &IID_IDirectDrawSurface7,       S_OK         },
1643         {&IID_IDirectDrawSurface4,      &IID_IDirectDrawSurface4,       S_OK         },
1644         {&IID_IDirectDrawSurface3,      &IID_IDirectDrawSurface3,       S_OK         },
1645         {&IID_IDirectDrawSurface2,      &IID_IDirectDrawSurface2,       S_OK         },
1646         {&IID_IDirectDrawSurface,       &IID_IDirectDrawSurface,        S_OK         },
1647         {&IID_IDirect3DDevice7,         NULL,                           E_INVALIDARG },
1648         {&IID_IDirect3DDevice3,         NULL,                           E_INVALIDARG },
1649         {&IID_IDirect3DDevice2,         NULL,                           E_INVALIDARG },
1650         {&IID_IDirect3DDevice,          NULL,                           E_INVALIDARG },
1651         {&IID_IDirect3DHALDevice,       &IID_IDirectDrawSurface,        S_OK         },
1652         {&IID_IDirect3D7,               NULL,                           E_INVALIDARG },
1653         {&IID_IDirect3D3,               NULL,                           E_INVALIDARG },
1654         {&IID_IDirect3D2,               NULL,                           E_INVALIDARG },
1655         {&IID_IDirect3D,                NULL,                           E_INVALIDARG },
1656         {&IID_IDirectDraw7,             NULL,                           E_INVALIDARG },
1657         {&IID_IDirectDraw4,             NULL,                           E_INVALIDARG },
1658         {&IID_IDirectDraw3,             NULL,                           E_INVALIDARG },
1659         {&IID_IDirectDraw2,             NULL,                           E_INVALIDARG },
1660         {&IID_IDirectDraw,              NULL,                           E_INVALIDARG },
1661         {&IID_IDirect3DLight,           NULL,                           E_INVALIDARG },
1662         {&IID_IDirect3DMaterial,        NULL,                           E_INVALIDARG },
1663         {&IID_IDirect3DMaterial2,       NULL,                           E_INVALIDARG },
1664         {&IID_IDirect3DMaterial3,       NULL,                           E_INVALIDARG },
1665         {&IID_IDirect3DExecuteBuffer,   NULL,                           E_INVALIDARG },
1666         {&IID_IDirect3DViewport,        NULL,                           E_INVALIDARG },
1667         {&IID_IDirect3DViewport2,       NULL,                           E_INVALIDARG },
1668         {&IID_IDirect3DViewport3,       NULL,                           E_INVALIDARG },
1669         {&IID_IDirect3DVertexBuffer,    NULL,                           E_INVALIDARG },
1670         {&IID_IDirect3DVertexBuffer7,   NULL,                           E_INVALIDARG },
1671         {&IID_IDirectDrawPalette,       NULL,                           E_INVALIDARG },
1672         {&IID_IDirectDrawClipper,       NULL,                           E_INVALIDARG },
1673         {&IID_IUnknown,                 &IID_IDirectDrawSurface,        S_OK         },
1674     };
1675
1676
1677     IDirect3DDevice *device;
1678     IDirectDraw *ddraw;
1679     HWND window;
1680
1681     if (!GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx"))
1682     {
1683         win_skip("DirectDrawCreateEx not available, skipping test.\n");
1684         return;
1685     }
1686
1687     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1688             0, 0, 640, 480, 0, 0, 0, 0);
1689     if (!(ddraw = create_ddraw()))
1690     {
1691         skip("Failed to create ddraw object, skipping test.\n");
1692         DestroyWindow(window);
1693         return;
1694     }
1695     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1696     {
1697         skip("Failed to create D3D device, skipping test.\n");
1698         IDirectDraw_Release(ddraw);
1699         DestroyWindow(window);
1700         return;
1701     }
1702
1703     test_qi("device_qi", (IUnknown *)device, &IID_IDirectDrawSurface, tests, sizeof(tests) / sizeof(*tests));
1704
1705     IDirect3DDevice_Release(device);
1706     IDirectDraw_Release(ddraw);
1707     DestroyWindow(window);
1708 }
1709
1710 static void test_wndproc(void)
1711 {
1712     LONG_PTR proc, ddraw_proc;
1713     IDirectDraw *ddraw;
1714     WNDCLASSA wc = {0};
1715     HWND window;
1716     HRESULT hr;
1717     ULONG ref;
1718
1719     static const UINT messages[] =
1720     {
1721         WM_WINDOWPOSCHANGING,
1722         WM_MOVE,
1723         WM_SIZE,
1724         WM_WINDOWPOSCHANGING,
1725         WM_ACTIVATE,
1726         WM_SETFOCUS,
1727         0,
1728     };
1729
1730     /* DDSCL_EXCLUSIVE replaces the window's window proc. */
1731     if (!(ddraw = create_ddraw()))
1732     {
1733         skip("Failed to create IDirectDraw object, skipping tests.\n");
1734         return;
1735     }
1736
1737     wc.lpfnWndProc = test_proc;
1738     wc.lpszClassName = "ddraw_test_wndproc_wc";
1739     ok(RegisterClassA(&wc), "Failed to register window class.\n");
1740
1741     window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
1742             WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
1743
1744     proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1745     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1746             (LONG_PTR)test_proc, proc);
1747     expect_messages = messages;
1748     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1749     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1750     ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
1751     expect_messages = NULL;
1752     proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1753     ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
1754             (LONG_PTR)test_proc, proc);
1755     ref = IDirectDraw_Release(ddraw);
1756     ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
1757     proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1758     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1759             (LONG_PTR)test_proc, proc);
1760
1761     /* DDSCL_NORMAL doesn't. */
1762     ddraw = create_ddraw();
1763     proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1764     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1765             (LONG_PTR)test_proc, proc);
1766     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
1767     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1768     proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1769     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1770             (LONG_PTR)test_proc, proc);
1771     ref = IDirectDraw_Release(ddraw);
1772     ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
1773     proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1774     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1775             (LONG_PTR)test_proc, proc);
1776
1777     /* The original window proc is only restored by ddraw if the current
1778      * window proc matches the one ddraw set. This also affects switching
1779      * from DDSCL_NORMAL to DDSCL_EXCLUSIVE. */
1780     ddraw = create_ddraw();
1781     proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1782     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1783             (LONG_PTR)test_proc, proc);
1784     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1785     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1786     proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1787     ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
1788             (LONG_PTR)test_proc, proc);
1789     ddraw_proc = proc;
1790     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
1791     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1792     proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1793     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1794             (LONG_PTR)test_proc, proc);
1795     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1796     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1797     proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
1798     ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
1799             (LONG_PTR)test_proc, proc);
1800     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
1801     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1802     proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1803     ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
1804             (LONG_PTR)DefWindowProcA, proc);
1805     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1806     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1807     proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)ddraw_proc);
1808     ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
1809             (LONG_PTR)DefWindowProcA, proc);
1810     ref = IDirectDraw_Release(ddraw);
1811     ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
1812     proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1813     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1814             (LONG_PTR)test_proc, proc);
1815
1816     ddraw = create_ddraw();
1817     proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1818     ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1819             (LONG_PTR)test_proc, proc);
1820     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1821     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1822     proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
1823     ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
1824             (LONG_PTR)test_proc, proc);
1825     ref = IDirectDraw_Release(ddraw);
1826     ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
1827     proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1828     ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
1829             (LONG_PTR)DefWindowProcA, proc);
1830
1831     fix_wndproc(window, (LONG_PTR)test_proc);
1832     expect_messages = NULL;
1833     DestroyWindow(window);
1834     UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
1835 }
1836
1837 static void test_window_style(void)
1838 {
1839     LONG style, exstyle, tmp;
1840     RECT fullscreen_rect, r;
1841     IDirectDraw *ddraw;
1842     HWND window;
1843     HRESULT hr;
1844     ULONG ref;
1845
1846     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1847             0, 0, 100, 100, 0, 0, 0, 0);
1848     if (!(ddraw = create_ddraw()))
1849     {
1850         skip("Failed to create a ddraw object, skipping test.\n");
1851         DestroyWindow(window);
1852         return;
1853     }
1854
1855     style = GetWindowLongA(window, GWL_STYLE);
1856     exstyle = GetWindowLongA(window, GWL_EXSTYLE);
1857     SetRect(&fullscreen_rect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1858
1859     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1860     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1861
1862     tmp = GetWindowLongA(window, GWL_STYLE);
1863     todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
1864     tmp = GetWindowLongA(window, GWL_EXSTYLE);
1865     todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
1866
1867     GetWindowRect(window, &r);
1868     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
1869             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
1870             r.left, r.top, r.right, r.bottom);
1871     GetClientRect(window, &r);
1872     todo_wine ok(!EqualRect(&r, &fullscreen_rect), "Client rect and window rect are equal.\n");
1873
1874     ref = IDirectDraw_Release(ddraw);
1875     ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
1876
1877     DestroyWindow(window);
1878 }
1879
1880 static void test_redundant_mode_set(void)
1881 {
1882     DDSURFACEDESC surface_desc = {0};
1883     IDirectDraw *ddraw;
1884     HWND window;
1885     HRESULT hr;
1886     RECT r, s;
1887     ULONG ref;
1888
1889     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1890             0, 0, 100, 100, 0, 0, 0, 0);
1891     if (!(ddraw = create_ddraw()))
1892     {
1893         skip("Failed to create a ddraw object, skipping test.\n");
1894         DestroyWindow(window);
1895         return;
1896     }
1897
1898     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1899     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1900
1901     surface_desc.dwSize = sizeof(surface_desc);
1902     hr = IDirectDraw_GetDisplayMode(ddraw, &surface_desc);
1903     ok(SUCCEEDED(hr), "GetDipslayMode failed, hr %#x.\n", hr);
1904
1905     hr = IDirectDraw_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
1906             U1(surface_desc.ddpfPixelFormat).dwRGBBitCount);
1907     ok(SUCCEEDED(hr), "SetDipslayMode failed, hr %#x.\n", hr);
1908
1909     GetWindowRect(window, &r);
1910     r.right /= 2;
1911     r.bottom /= 2;
1912     SetWindowPos(window, HWND_TOP, r.left, r.top, r.right, r.bottom, 0);
1913     GetWindowRect(window, &s);
1914     ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
1915             r.left, r.top, r.right, r.bottom,
1916             s.left, s.top, s.right, s.bottom);
1917
1918     hr = IDirectDraw_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
1919             U1(surface_desc.ddpfPixelFormat).dwRGBBitCount);
1920     ok(SUCCEEDED(hr), "SetDipslayMode failed, hr %#x.\n", hr);
1921
1922     GetWindowRect(window, &s);
1923     ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
1924             r.left, r.top, r.right, r.bottom,
1925             s.left, s.top, s.right, s.bottom);
1926
1927     ref = IDirectDraw_Release(ddraw);
1928     ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
1929
1930     DestroyWindow(window);
1931 }
1932
1933 static SIZE screen_size;
1934
1935 static LRESULT CALLBACK mode_set_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
1936 {
1937     if (message == WM_SIZE)
1938     {
1939         screen_size.cx = GetSystemMetrics(SM_CXSCREEN);
1940         screen_size.cy = GetSystemMetrics(SM_CYSCREEN);
1941     }
1942
1943     return test_proc(hwnd, message, wparam, lparam);
1944 }
1945
1946 static void test_coop_level_mode_set(void)
1947 {
1948     IDirectDrawSurface *primary;
1949     RECT fullscreen_rect, r, s;
1950     IDirectDraw *ddraw;
1951     DDSURFACEDESC ddsd;
1952     WNDCLASSA wc = {0};
1953     HWND window;
1954     HRESULT hr;
1955     ULONG ref;
1956
1957     static const UINT exclusive_messages[] =
1958     {
1959         WM_WINDOWPOSCHANGING,
1960         WM_WINDOWPOSCHANGED,
1961         WM_SIZE,
1962         WM_DISPLAYCHANGE,
1963         0,
1964     };
1965
1966     static const UINT normal_messages[] =
1967     {
1968         WM_DISPLAYCHANGE,
1969         0,
1970     };
1971
1972     if (!(ddraw = create_ddraw()))
1973     {
1974         skip("Failed to create a ddraw object, skipping test.\n");
1975         return;
1976     }
1977
1978     wc.lpfnWndProc = mode_set_proc;
1979     wc.lpszClassName = "ddraw_test_wndproc_wc";
1980     ok(RegisterClassA(&wc), "Failed to register window class.\n");
1981
1982     window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test", WS_OVERLAPPEDWINDOW,
1983             0, 0, 100, 100, 0, 0, 0, 0);
1984
1985     SetRect(&fullscreen_rect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1986     SetRect(&s, 0, 0, 640, 480);
1987
1988     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1989     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1990
1991     GetWindowRect(window, &r);
1992     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
1993             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
1994             r.left, r.top, r.right, r.bottom);
1995
1996     memset(&ddsd, 0, sizeof(ddsd));
1997     ddsd.dwSize = sizeof(ddsd);
1998     ddsd.dwFlags = DDSD_CAPS;
1999     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2000
2001     hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2002     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2003     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2004     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2005     ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2006             fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2007     ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2008             fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2009
2010     GetWindowRect(window, &r);
2011     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2012             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2013             r.left, r.top, r.right, r.bottom);
2014
2015     expect_messages = exclusive_messages;
2016     screen_size.cx = 0;
2017     screen_size.cy = 0;
2018
2019     hr = IDirectDraw_SetDisplayMode(ddraw, 640, 480, 32);
2020     ok(SUCCEEDED(hr), "SetDipslayMode failed, hr %#x.\n", hr);
2021
2022     ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2023     expect_messages = NULL;
2024     ok(screen_size.cx == s.right && screen_size.cy == s.bottom,
2025             "Expected screen size %ux%u, got %ux%u.\n",
2026             s.right, s.bottom, screen_size.cx, screen_size.cy);
2027
2028     GetWindowRect(window, &r);
2029     ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2030             s.left, s.top, s.right, s.bottom,
2031             r.left, r.top, r.right, r.bottom);
2032
2033     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2034     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2035     ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2036             fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2037     ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2038             fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2039     IDirectDrawSurface_Release(primary);
2040
2041     memset(&ddsd, 0, sizeof(ddsd));
2042     ddsd.dwSize = sizeof(ddsd);
2043     ddsd.dwFlags = DDSD_CAPS;
2044     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2045
2046     hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2047     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2048     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2049     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2050     ok(ddsd.dwWidth == s.right - s.left, "Expected surface width %u, got %u.\n",
2051             s.right - s.left, ddsd.dwWidth);
2052     ok(ddsd.dwHeight == s.bottom - s.top, "Expected surface height %u, got %u.\n",
2053             s.bottom - s.top, ddsd.dwHeight);
2054
2055     GetWindowRect(window, &r);
2056     ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2057             s.left, s.top, s.right, s.bottom,
2058             r.left, r.top, r.right, r.bottom);
2059
2060     expect_messages = exclusive_messages;
2061     screen_size.cx = 0;
2062     screen_size.cy = 0;
2063
2064     hr = IDirectDraw_RestoreDisplayMode(ddraw);
2065     ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2066
2067     ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2068     expect_messages = NULL;
2069     ok(screen_size.cx == fullscreen_rect.right && screen_size.cy == fullscreen_rect.bottom,
2070             "Expected screen size %ux%u, got %ux%u.\n",
2071             fullscreen_rect.right, fullscreen_rect.bottom, screen_size.cx, screen_size.cy);
2072
2073     GetWindowRect(window, &r);
2074     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2075             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2076             r.left, r.top, r.right, r.bottom);
2077
2078     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2079     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2080     ok(ddsd.dwWidth == s.right - s.left, "Expected surface width %u, got %u.\n",
2081             s.right - s.left, ddsd.dwWidth);
2082     ok(ddsd.dwHeight == s.bottom - s.top, "Expected surface height %u, got %u.\n",
2083             s.bottom - s.top, ddsd.dwHeight);
2084     IDirectDrawSurface_Release(primary);
2085
2086     memset(&ddsd, 0, sizeof(ddsd));
2087     ddsd.dwSize = sizeof(ddsd);
2088     ddsd.dwFlags = DDSD_CAPS;
2089     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2090
2091     hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2092     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2093     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2094     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2095     ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2096             fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2097     ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2098             fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2099
2100     GetWindowRect(window, &r);
2101     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2102             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2103             r.left, r.top, r.right, r.bottom);
2104
2105     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2106     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2107
2108     GetWindowRect(window, &r);
2109     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2110             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2111             r.left, r.top, r.right, r.bottom);
2112
2113     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2114     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2115     ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2116             fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2117     ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2118             fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2119     IDirectDrawSurface_Release(primary);
2120
2121     memset(&ddsd, 0, sizeof(ddsd));
2122     ddsd.dwSize = sizeof(ddsd);
2123     ddsd.dwFlags = DDSD_CAPS;
2124     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2125
2126     hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2127     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2128     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2129     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2130     ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2131             fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2132     ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2133             fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2134
2135     GetWindowRect(window, &r);
2136     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2137             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2138             r.left, r.top, r.right, r.bottom);
2139
2140     expect_messages = normal_messages;
2141     screen_size.cx = 0;
2142     screen_size.cy = 0;
2143
2144     hr = IDirectDraw_SetDisplayMode(ddraw, 640, 480, 32);
2145     ok(SUCCEEDED(hr) || broken(hr == DDERR_NOEXCLUSIVEMODE) /* NT4 testbot */,
2146             "SetDipslayMode failed, hr %#x.\n", hr);
2147     if (hr == DDERR_NOEXCLUSIVEMODE)
2148     {
2149         win_skip("Broken SetDisplayMode(), skipping remaining tests.\n");
2150         IDirectDrawSurface_Release(primary);
2151         IDirectDraw_Release(ddraw);
2152         goto done;
2153     }
2154
2155     ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2156     expect_messages = NULL;
2157     ok(!screen_size.cx && !screen_size.cy, "Got unxpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2158
2159     GetWindowRect(window, &r);
2160     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2161             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2162             r.left, r.top, r.right, r.bottom);
2163
2164     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2165     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2166     ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2167             fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2168     ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2169             fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2170     IDirectDrawSurface_Release(primary);
2171
2172     memset(&ddsd, 0, sizeof(ddsd));
2173     ddsd.dwSize = sizeof(ddsd);
2174     ddsd.dwFlags = DDSD_CAPS;
2175     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2176
2177     hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2178     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2179     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2180     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2181     ok(ddsd.dwWidth == s.right - s.left, "Expected surface width %u, got %u.\n",
2182             s.right - s.left, ddsd.dwWidth);
2183     ok(ddsd.dwHeight == s.bottom - s.top, "Expected surface height %u, got %u.\n",
2184             s.bottom - s.top, ddsd.dwHeight);
2185
2186     GetWindowRect(window, &r);
2187     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2188             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2189             r.left, r.top, r.right, r.bottom);
2190
2191     expect_messages = normal_messages;
2192     screen_size.cx = 0;
2193     screen_size.cy = 0;
2194
2195     hr = IDirectDraw_RestoreDisplayMode(ddraw);
2196     ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2197
2198     ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2199     expect_messages = NULL;
2200     ok(!screen_size.cx && !screen_size.cy, "Got unxpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2201
2202     GetWindowRect(window, &r);
2203     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2204             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2205             r.left, r.top, r.right, r.bottom);
2206
2207     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2208     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2209     ok(ddsd.dwWidth == s.right - s.left, "Expected surface width %u, got %u.\n",
2210             s.right - s.left, ddsd.dwWidth);
2211     ok(ddsd.dwHeight == s.bottom - s.top, "Expected surface height %u, got %u.\n",
2212             s.bottom - s.top, ddsd.dwHeight);
2213     IDirectDrawSurface_Release(primary);
2214
2215     memset(&ddsd, 0, sizeof(ddsd));
2216     ddsd.dwSize = sizeof(ddsd);
2217     ddsd.dwFlags = DDSD_CAPS;
2218     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2219
2220     hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2221     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2222     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2223     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2224     ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2225             fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2226     ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2227             fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2228
2229     GetWindowRect(window, &r);
2230     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2231             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2232             r.left, r.top, r.right, r.bottom);
2233
2234     /* DDSCL_NORMAL | DDSCL_FULLSCREEN behaves the same as just DDSCL_NORMAL.
2235      * Resizing the window on mode changes is a property of DDSCL_EXCLUSIVE,
2236      * not DDSCL_FULLSCREEN. */
2237     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
2238     ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2239
2240     GetWindowRect(window, &r);
2241     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2242             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2243             r.left, r.top, r.right, r.bottom);
2244
2245     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2246     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2247     ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2248             fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2249     ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2250             fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2251     IDirectDrawSurface_Release(primary);
2252
2253     memset(&ddsd, 0, sizeof(ddsd));
2254     ddsd.dwSize = sizeof(ddsd);
2255     ddsd.dwFlags = DDSD_CAPS;
2256     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2257
2258     hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2259     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2260     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2261     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2262     ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2263             fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2264     ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2265             fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2266
2267     GetWindowRect(window, &r);
2268     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2269             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2270             r.left, r.top, r.right, r.bottom);
2271
2272     expect_messages = normal_messages;
2273     screen_size.cx = 0;
2274     screen_size.cy = 0;
2275
2276     hr = IDirectDraw_SetDisplayMode(ddraw, 640, 480, 32);
2277     ok(SUCCEEDED(hr), "SetDipslayMode failed, hr %#x.\n", hr);
2278
2279     ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2280     expect_messages = NULL;
2281     ok(!screen_size.cx && !screen_size.cy, "Got unxpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2282
2283     GetWindowRect(window, &r);
2284     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2285             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2286             r.left, r.top, r.right, r.bottom);
2287
2288     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2289     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2290     ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2291             fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2292     ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2293             fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2294     IDirectDrawSurface_Release(primary);
2295
2296     memset(&ddsd, 0, sizeof(ddsd));
2297     ddsd.dwSize = sizeof(ddsd);
2298     ddsd.dwFlags = DDSD_CAPS;
2299     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2300
2301     hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2302     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2303     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2304     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2305     ok(ddsd.dwWidth == s.right - s.left, "Expected surface width %u, got %u.\n",
2306             s.right - s.left, ddsd.dwWidth);
2307     ok(ddsd.dwHeight == s.bottom - s.top, "Expected surface height %u, got %u.\n",
2308             s.bottom - s.top, ddsd.dwHeight);
2309
2310     GetWindowRect(window, &r);
2311     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2312             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2313             r.left, r.top, r.right, r.bottom);
2314
2315     expect_messages = normal_messages;
2316     screen_size.cx = 0;
2317     screen_size.cy = 0;
2318
2319     hr = IDirectDraw_RestoreDisplayMode(ddraw);
2320     ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2321
2322     ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2323     expect_messages = NULL;
2324     ok(!screen_size.cx && !screen_size.cy, "Got unxpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2325
2326     GetWindowRect(window, &r);
2327     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2328             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2329             r.left, r.top, r.right, r.bottom);
2330
2331     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2332     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2333     ok(ddsd.dwWidth == s.right - s.left, "Expected surface width %u, got %u.\n",
2334             s.right - s.left, ddsd.dwWidth);
2335     ok(ddsd.dwHeight == s.bottom - s.top, "Expected surface height %u, got %u.\n",
2336             s.bottom - s.top, ddsd.dwHeight);
2337     IDirectDrawSurface_Release(primary);
2338
2339     memset(&ddsd, 0, sizeof(ddsd));
2340     ddsd.dwSize = sizeof(ddsd);
2341     ddsd.dwFlags = DDSD_CAPS;
2342     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2343
2344     hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2345     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2346     hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2347     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2348     ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2349             fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2350     ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2351             fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2352     IDirectDrawSurface_Release(primary);
2353
2354     GetWindowRect(window, &r);
2355     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2356             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2357             r.left, r.top, r.right, r.bottom);
2358
2359     ref = IDirectDraw_Release(ddraw);
2360     ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2361
2362     GetWindowRect(window, &r);
2363     ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2364             fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2365             r.left, r.top, r.right, r.bottom);
2366
2367 done:
2368     expect_messages = NULL;
2369     DestroyWindow(window);
2370     UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
2371 }
2372
2373 static void test_initialize(void)
2374 {
2375     IDirectDraw *ddraw;
2376     IDirect3D *d3d;
2377     HRESULT hr;
2378
2379     if (!(ddraw = create_ddraw()))
2380     {
2381         skip("Failed to create a ddraw object, skipping test.\n");
2382         return;
2383     }
2384
2385     hr = IDirectDraw_Initialize(ddraw, NULL);
2386     ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x.\n", hr);
2387     IDirectDraw_Release(ddraw);
2388
2389     CoInitialize(NULL);
2390     hr = CoCreateInstance(&CLSID_DirectDraw, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectDraw, (void **)&ddraw);
2391     ok(SUCCEEDED(hr), "Failed to create IDirectDraw instance, hr %#x.\n", hr);
2392     hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
2393     if (SUCCEEDED(hr))
2394     {
2395         /* IDirect3D_Initialize() just returns DDERR_ALREADYINITIALIZED. */
2396         hr = IDirect3D_Initialize(d3d, NULL);
2397         ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x, expected DDERR_ALREADYINITIALIZED.\n", hr);
2398         IDirect3D_Release(d3d);
2399     }
2400     else skip("Failed to query IDirect3D interface, skipping tests.\n");
2401     hr = IDirectDraw_Initialize(ddraw, NULL);
2402     ok(hr == DD_OK, "Initialize returned hr %#x, expected DD_OK.\n", hr);
2403     hr = IDirectDraw_Initialize(ddraw, NULL);
2404     ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x, expected DDERR_ALREADYINITIALIZED.\n", hr);
2405     IDirectDraw_Release(ddraw);
2406     CoUninitialize();
2407
2408     if (0) /* This crashes on the W2KPROSP4 testbot. */
2409     {
2410         CoInitialize(NULL);
2411         hr = CoCreateInstance(&CLSID_DirectDraw, NULL, CLSCTX_INPROC_SERVER, &IID_IDirect3D, (void **)&d3d);
2412         ok(hr == E_NOINTERFACE, "CoCreateInstance returned hr %#x, expected E_NOINTERFACE.\n", hr);
2413         CoUninitialize();
2414     }
2415 }
2416
2417 static void test_coop_level_surf_create(void)
2418 {
2419     IDirectDrawSurface *surface;
2420     IDirectDraw *ddraw;
2421     DDSURFACEDESC ddsd;
2422     HRESULT hr;
2423
2424     if (!(ddraw = create_ddraw()))
2425     {
2426         skip("Failed to create a ddraw object, skipping test.\n");
2427         return;
2428     }
2429
2430     memset(&ddsd, 0, sizeof(ddsd));
2431     ddsd.dwSize = sizeof(ddsd);
2432     ddsd.dwFlags = DDSD_CAPS;
2433     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2434     hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
2435     ok(hr == DDERR_NOCOOPERATIVELEVELSET, "Surface creation returned hr %#x.\n", hr);
2436
2437     IDirectDraw_Release(ddraw);
2438 }
2439
2440 START_TEST(ddraw1)
2441 {
2442     test_coop_level_create_device_window();
2443     test_clipper_blt();
2444     test_coop_level_d3d_state();
2445     test_surface_interface_mismatch();
2446     test_coop_level_threaded();
2447     test_viewport_interfaces();
2448     test_zenable();
2449     test_ck_rgba();
2450     test_ck_default();
2451     test_surface_qi();
2452     test_device_qi();
2453     test_wndproc();
2454     test_window_style();
2455     test_redundant_mode_set();
2456     test_coop_level_mode_set();
2457     test_initialize();
2458     test_coop_level_surf_create();
2459 }