kernel32: GetModuleHandleEx already clears the module handle on failure.
[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 HRESULT CALLBACK enum_z_fmt(GUID *guid, char *description, char *name,
190         D3DDEVICEDESC *hal_desc, D3DDEVICEDESC *hel_desc, void *ctx)
191 {
192     DWORD *z_depth = ctx;
193
194     if (!IsEqualGUID(&IID_IDirect3DHALDevice, guid))
195         return D3DENUMRET_OK;
196
197     if (hal_desc->dwDeviceZBufferBitDepth & DDBD_32)
198         *z_depth = 32;
199     else if (hal_desc->dwDeviceZBufferBitDepth & DDBD_24)
200         *z_depth = 24;
201     else if (hal_desc->dwDeviceZBufferBitDepth & DDBD_16)
202         *z_depth = 16;
203
204     return DDENUMRET_OK;
205 }
206
207 static IDirectDraw *create_ddraw(void)
208 {
209     IDirectDraw *ddraw;
210
211     if (FAILED(DirectDrawCreate(NULL, &ddraw, NULL)))
212         return NULL;
213
214     return ddraw;
215 }
216
217 static IDirect3DDevice *create_device(IDirectDraw *ddraw, HWND window, DWORD coop_level)
218 {
219     IDirectDrawSurface *surface, *ds;
220     IDirect3DDevice *device = NULL;
221     DDSURFACEDESC surface_desc;
222     DWORD z_depth = 0;
223     IDirect3D *d3d;
224     HRESULT hr;
225
226     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, coop_level);
227     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
228
229     memset(&surface_desc, 0, sizeof(surface_desc));
230     surface_desc.dwSize = sizeof(surface_desc);
231     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
232     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
233     surface_desc.dwWidth = 640;
234     surface_desc.dwHeight = 480;
235
236     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
237     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
238
239     if (coop_level & DDSCL_NORMAL)
240     {
241         IDirectDrawClipper *clipper;
242
243         hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
244         ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
245         hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
246         ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
247         hr = IDirectDrawSurface_SetClipper(surface, clipper);
248         ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
249         IDirectDrawClipper_Release(clipper);
250     }
251
252     hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
253     if (FAILED(hr))
254     {
255         IDirectDrawSurface_Release(surface);
256         return NULL;
257     }
258
259     hr = IDirect3D_EnumDevices(d3d, enum_z_fmt, &z_depth);
260     ok(SUCCEEDED(hr), "Failed to enumerate z-formats, hr %#x.\n", hr);
261     IDirect3D_Release(d3d);
262     if (FAILED(hr) || !z_depth)
263     {
264         IDirectDrawSurface_Release(surface);
265         return NULL;
266     }
267
268     memset(&surface_desc, 0, sizeof(surface_desc));
269     surface_desc.dwSize = sizeof(surface_desc);
270     surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
271     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
272     U2(surface_desc).dwZBufferBitDepth = z_depth;
273     surface_desc.dwWidth = 640;
274     surface_desc.dwHeight = 480;
275     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
276     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
277     if (FAILED(hr))
278     {
279         IDirectDrawSurface_Release(surface);
280         return NULL;
281     }
282
283     hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
284     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
285     IDirectDrawSurface_Release(ds);
286     if (FAILED(hr))
287     {
288         IDirectDrawSurface_Release(surface);
289         return NULL;
290     }
291
292     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DHALDevice, (void **)&device);
293     IDirectDrawSurface_Release(surface);
294     if (FAILED(hr))
295         return NULL;
296
297     return device;
298 }
299
300 static IDirect3DViewport *create_viewport(IDirect3DDevice *device, UINT x, UINT y, UINT w, UINT h)
301 {
302     IDirect3DViewport *viewport;
303     D3DVIEWPORT vp;
304     IDirect3D *d3d;
305     HRESULT hr;
306
307     hr = IDirect3DDevice_GetDirect3D(device, &d3d);
308     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
309     hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
310     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
311     hr = IDirect3DDevice_AddViewport(device, viewport);
312     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
313     memset(&vp, 0, sizeof(vp));
314     vp.dwSize = sizeof(vp);
315     vp.dwX = x;
316     vp.dwY = y;
317     vp.dwWidth = w;
318     vp.dwHeight = h;
319     vp.dvScaleX = (float)w / 2.0f;
320     vp.dvScaleY = (float)h / 2.0f;
321     vp.dvMaxX = 1.0f;
322     vp.dvMaxY = 1.0f;
323     vp.dvMinZ = 0.0f;
324     vp.dvMaxZ = 1.0f;
325     hr = IDirect3DViewport_SetViewport(viewport, &vp);
326     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
327     IDirect3D_Release(d3d);
328
329     return viewport;
330 }
331
332 static void destroy_viewport(IDirect3DDevice *device, IDirect3DViewport *viewport)
333 {
334     HRESULT hr;
335
336     hr = IDirect3DDevice_DeleteViewport(device, viewport);
337     ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
338     IDirect3DViewport_Release(viewport);
339 }
340
341 static HRESULT CALLBACK restore_callback(IDirectDrawSurface *surface, DDSURFACEDESC *desc, void *context)
342 {
343     HRESULT hr = IDirectDrawSurface_Restore(surface);
344     ok(SUCCEEDED(hr), "Failed to restore surface, hr %#x.\n", hr);
345     IDirectDrawSurface_Release(surface);
346
347     return DDENUMRET_OK;
348 }
349
350 static HRESULT restore_surfaces(IDirectDraw *ddraw)
351 {
352     return IDirectDraw_EnumSurfaces(ddraw, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
353             NULL, NULL, restore_callback);
354 }
355
356 static void test_coop_level_create_device_window(void)
357 {
358     HWND focus_window, device_window;
359     IDirectDraw *ddraw;
360     HRESULT hr;
361
362     focus_window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
363             0, 0, 640, 480, 0, 0, 0, 0);
364     if (!(ddraw = create_ddraw()))
365     {
366         skip("Failed to create a ddraw object, skipping test.\n");
367         DestroyWindow(focus_window);
368         return;
369     }
370
371     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
372     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
373     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
374     ok(!device_window, "Unexpected device window found.\n");
375     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
376     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
377     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
378     ok(!device_window, "Unexpected device window found.\n");
379     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
380     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
381     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
382     ok(!device_window, "Unexpected device window found.\n");
383     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
384     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
385     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
386     ok(!device_window, "Unexpected device window found.\n");
387     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
388     ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
389     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
390     ok(!device_window, "Unexpected device window found.\n");
391
392     /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
393     if (broken(hr == DDERR_INVALIDPARAMS))
394     {
395         win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
396         IDirectDraw_Release(ddraw);
397         DestroyWindow(focus_window);
398         return;
399     }
400
401     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
402     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
403     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
404     ok(!device_window, "Unexpected device window found.\n");
405     hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
406     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
407     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
408     ok(!device_window, "Unexpected device window found.\n");
409
410     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
411     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
412     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
413     ok(!device_window, "Unexpected device window found.\n");
414     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
415             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
416     ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
417     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
418     ok(!!device_window, "Device window not found.\n");
419
420     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
421     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
422     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
423     ok(!device_window, "Unexpected device window found.\n");
424     hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
425             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
426     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
427     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
428     ok(!!device_window, "Device window not found.\n");
429
430     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
431     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
432     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
433     ok(!device_window, "Unexpected device window found.\n");
434     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
435     ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
436     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
437     ok(!device_window, "Unexpected device window found.\n");
438     hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
439     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
440     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
441     ok(!device_window, "Unexpected device window found.\n");
442     hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
443     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
444     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
445     ok(!!device_window, "Device window not found.\n");
446
447     IDirectDraw_Release(ddraw);
448     DestroyWindow(focus_window);
449 }
450
451 static void test_clipper_blt(void)
452 {
453     IDirectDrawSurface *src_surface, *dst_surface;
454     RECT client_rect, src_rect, *rect;
455     IDirectDrawClipper *clipper;
456     DDSURFACEDESC surface_desc;
457     unsigned int i, j, x, y;
458     IDirectDraw *ddraw;
459     RGNDATA *rgn_data;
460     D3DCOLOR color;
461     HRGN r1, r2;
462     HWND window;
463     DDBLTFX fx;
464     HRESULT hr;
465     DWORD *ptr;
466     DWORD ret;
467
468     static const DWORD src_data[] =
469     {
470         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
471         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
472         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
473     };
474     static const D3DCOLOR expected1[] =
475     {
476         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
477         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
478         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
479         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
480     };
481     static const D3DCOLOR expected2[] =
482     {
483         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
484         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
485         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
486         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
487     };
488
489     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
490             10, 10, 640, 480, 0, 0, 0, 0);
491     ShowWindow(window, SW_SHOW);
492     if (!(ddraw = create_ddraw()))
493     {
494         skip("Failed to create a ddraw object, skipping test.\n");
495         DestroyWindow(window);
496         return;
497     }
498
499     ret = GetClientRect(window, &client_rect);
500     ok(ret, "Failed to get client rect.\n");
501     ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
502     ok(ret, "Failed to map client rect.\n");
503
504     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
505     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
506
507     hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
508     ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
509     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
510     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
511     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
512     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
513     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
514     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
515     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
516     hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
517     ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
518     ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
519     ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
520     ok(rgn_data->rdh.nCount == 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
521     ok(rgn_data->rdh.nRgnSize == 16 || broken(rgn_data->rdh.nRgnSize == 168 /* NT4 */),
522             "Got unexpected region size %u.\n", rgn_data->rdh.nRgnSize);
523     ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
524             "Got unexpected bounding rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
525             rgn_data->rdh.rcBound.left, rgn_data->rdh.rcBound.top,
526             rgn_data->rdh.rcBound.right, rgn_data->rdh.rcBound.bottom,
527             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
528     rect = (RECT *)&rgn_data->Buffer[0];
529     ok(EqualRect(rect, &client_rect),
530             "Got unexpected clip rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
531             rect->left, rect->top, rect->right, rect->bottom,
532             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
533     HeapFree(GetProcessHeap(), 0, rgn_data);
534
535     r1 = CreateRectRgn(0, 0, 320, 240);
536     ok(!!r1, "Failed to create region.\n");
537     r2 = CreateRectRgn(320, 240, 640, 480);
538     ok(!!r2, "Failed to create region.\n");
539     CombineRgn(r1, r1, r2, RGN_OR);
540     ret = GetRegionData(r1, 0, NULL);
541     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
542     ret = GetRegionData(r1, ret, rgn_data);
543     ok(!!ret, "Failed to get region data.\n");
544
545     DeleteObject(r2);
546     DeleteObject(r1);
547
548     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
549     ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
550     hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
551     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
552     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
553     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
554
555     HeapFree(GetProcessHeap(), 0, rgn_data);
556
557     memset(&surface_desc, 0, sizeof(surface_desc));
558     surface_desc.dwSize = sizeof(surface_desc);
559     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
560     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
561     surface_desc.dwWidth = 640;
562     surface_desc.dwHeight = 480;
563     surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
564     surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
565     U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
566     U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
567     U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
568     U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
569
570     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
571     ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
572     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
573     ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
574
575     memset(&fx, 0, sizeof(fx));
576     fx.dwSize = sizeof(fx);
577     hr = IDirectDrawSurface_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
578     ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
579     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
580     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
581
582     hr = IDirectDrawSurface_Lock(src_surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
583     ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
584     ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
585     ptr = surface_desc.lpSurface;
586     memcpy(&ptr[   0], &src_data[ 0], 6 * sizeof(DWORD));
587     memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
588     memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
589     hr = IDirectDrawSurface_Unlock(src_surface, NULL);
590     ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
591
592     hr = IDirectDrawSurface_SetClipper(dst_surface, clipper);
593     ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
594
595     SetRect(&src_rect, 1, 1, 5, 2);
596     hr = IDirectDrawSurface_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
597     ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
598     for (i = 0; i < 4; ++i)
599     {
600         for (j = 0; j < 4; ++j)
601         {
602             x = 80 * ((2 * j) + 1);
603             y = 60 * ((2 * i) + 1);
604             color = get_surface_color(dst_surface, x, y);
605             ok(compare_color(color, expected1[i * 4 + j], 1),
606                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
607         }
608     }
609
610     U5(fx).dwFillColor = 0xff0000ff;
611     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
612     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
613     for (i = 0; i < 4; ++i)
614     {
615         for (j = 0; j < 4; ++j)
616         {
617             x = 80 * ((2 * j) + 1);
618             y = 60 * ((2 * i) + 1);
619             color = get_surface_color(dst_surface, x, y);
620             ok(compare_color(color, expected2[i * 4 + j], 1),
621                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
622         }
623     }
624
625     hr = IDirectDrawSurface_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
626     ok(hr == DDERR_BLTFASTCANTCLIP || broken(hr == E_NOTIMPL /* NT4 */), "Got unexpected hr %#x.\n", hr);
627
628     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
629     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
630     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
631     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
632     DestroyWindow(window);
633     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
634     ok(hr == E_FAIL, "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_GetClipList(clipper, NULL, NULL, &ret);
638     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
639     hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
640     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
641     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
642     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
643     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
644     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
645
646     IDirectDrawSurface_Release(dst_surface);
647     IDirectDrawSurface_Release(src_surface);
648     IDirectDrawClipper_Release(clipper);
649     IDirectDraw_Release(ddraw);
650 }
651
652 static void test_coop_level_d3d_state(void)
653 {
654     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
655     D3DMATERIALHANDLE background_handle;
656     IDirectDrawSurface *rt, *surface;
657     IDirect3DMaterial *background;
658     IDirect3DViewport *viewport;
659     IDirect3DDevice *device;
660     D3DMATERIAL material;
661     IDirectDraw *ddraw;
662     IDirect3D *d3d;
663     D3DCOLOR color;
664     HWND window;
665     HRESULT hr;
666
667     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
668             0, 0, 640, 480, 0, 0, 0, 0);
669     if (!(ddraw = create_ddraw()))
670     {
671         skip("Failed to create ddraw object, skipping test.\n");
672         DestroyWindow(window);
673         return;
674     }
675     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
676     {
677         skip("Failed to create D3D device, skipping test.\n");
678         IDirectDraw_Release(ddraw);
679         DestroyWindow(window);
680         return;
681     }
682
683     hr = IDirect3DDevice_GetDirect3D(device, &d3d);
684     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
685     hr = IDirect3D_CreateMaterial(d3d, &background, NULL);
686     ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
687     IDirect3D_Release(d3d);
688
689     viewport = create_viewport(device, 0, 0, 640, 480);
690
691     memset(&material, 0, sizeof(material));
692     material.dwSize = sizeof(material);
693     U1(U(material).diffuse).r = 1.0f;
694     U2(U(material).diffuse).g = 0.0f;
695     U3(U(material).diffuse).b = 0.0f;
696     U4(U(material).diffuse).a = 1.0f;
697     hr = IDirect3DMaterial_SetMaterial(background, &material);
698     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
699     hr = IDirect3DMaterial_GetHandle(background, device, &background_handle);
700     ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
701     hr = IDirect3DViewport_SetBackground(viewport, background_handle);
702     ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
703
704     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
705     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
706     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
707     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
708     color = get_surface_color(rt, 320, 240);
709     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
710
711     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
712     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
713     hr = IDirectDrawSurface_IsLost(rt);
714     ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
715     hr = restore_surfaces(ddraw);
716     ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
717
718     memset(&material, 0, sizeof(material));
719     material.dwSize = sizeof(material);
720     U1(U(material).diffuse).r = 0.0f;
721     U2(U(material).diffuse).g = 1.0f;
722     U3(U(material).diffuse).b = 0.0f;
723     U4(U(material).diffuse).a = 1.0f;
724     hr = IDirect3DMaterial_SetMaterial(background, &material);
725     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
726
727     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&surface);
728     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
729     ok(surface == rt, "Got unexpected surface %p.\n", surface);
730     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
731     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
732     color = get_surface_color(rt, 320, 240);
733     ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
734
735     destroy_viewport(device, viewport);
736     IDirect3DMaterial_Release(background);
737     IDirectDrawSurface_Release(surface);
738     IDirectDrawSurface_Release(rt);
739     IDirect3DDevice_Release(device);
740     IDirectDraw_Release(ddraw);
741     DestroyWindow(window);
742 }
743
744 static void test_surface_interface_mismatch(void)
745 {
746     IDirectDraw *ddraw = NULL;
747     IDirect3D *d3d = NULL;
748     IDirectDrawSurface *surface = NULL, *ds;
749     IDirectDrawSurface3 *surface3 = NULL;
750     IDirect3DDevice *device = NULL;
751     IDirect3DViewport *viewport = NULL;
752     IDirect3DMaterial *background = NULL;
753     DDSURFACEDESC surface_desc;
754     DWORD z_depth = 0;
755     ULONG refcount;
756     HRESULT hr;
757     D3DCOLOR color;
758     HWND window;
759     D3DMATERIAL material;
760     D3DMATERIALHANDLE background_handle;
761     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
762
763     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
764             0, 0, 640, 480, 0, 0, 0, 0);
765
766     if (!(ddraw = create_ddraw()))
767     {
768         skip("Failed to create a ddraw object, skipping test.\n");
769         goto cleanup;
770     }
771
772     hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
773     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
774
775     memset(&surface_desc, 0, sizeof(surface_desc));
776     surface_desc.dwSize = sizeof(surface_desc);
777     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
778     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
779     surface_desc.dwWidth = 640;
780     surface_desc.dwHeight = 480;
781
782     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
783     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
784
785     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
786     if (FAILED(hr))
787     {
788         skip("Failed to get the IDirectDrawSurface3 interface, skipping test.\n");
789         goto cleanup;
790     }
791
792     hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
793     if (FAILED(hr))
794     {
795         skip("Failed to get the IDirect3D interface, skipping test.\n");
796         goto cleanup;
797     }
798
799     hr = IDirect3D_EnumDevices(d3d, enum_z_fmt, &z_depth);
800     if (FAILED(hr) || !z_depth)
801     {
802         skip("No depth buffer formats available, skipping test.\n");
803         goto cleanup;
804     }
805
806     memset(&surface_desc, 0, sizeof(surface_desc));
807     surface_desc.dwSize = sizeof(surface_desc);
808     surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
809     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
810     U2(surface_desc).dwZBufferBitDepth = z_depth;
811     surface_desc.dwWidth = 640;
812     surface_desc.dwHeight = 480;
813     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
814     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
815     if (FAILED(hr))
816         goto cleanup;
817
818     /* Using a different surface interface version still works */
819     hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
820     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
821     refcount = IDirectDrawSurface_Release(ds);
822     ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
823     if (FAILED(hr))
824         goto cleanup;
825
826     /* Here too */
827     hr = IDirectDrawSurface3_QueryInterface(surface3, &IID_IDirect3DHALDevice, (void **)&device);
828     ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
829     if (FAILED(hr))
830         goto cleanup;
831
832     hr = IDirect3D_CreateMaterial(d3d, &background, NULL);
833     ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
834
835     viewport = create_viewport(device, 0, 0, 640, 480);
836
837     memset(&material, 0, sizeof(material));
838     material.dwSize = sizeof(material);
839     U1(U(material).diffuse).r = 1.0f;
840     U2(U(material).diffuse).g = 0.0f;
841     U3(U(material).diffuse).b = 0.0f;
842     U4(U(material).diffuse).a = 1.0f;
843     hr = IDirect3DMaterial_SetMaterial(background, &material);
844     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
845     hr = IDirect3DMaterial_GetHandle(background, device, &background_handle);
846     ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
847     hr = IDirect3DViewport_SetBackground(viewport, background_handle);
848     ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
849
850     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
851     ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
852     color = get_surface_color(surface, 320, 240);
853     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
854
855 cleanup:
856     if (viewport)
857         destroy_viewport(device, viewport);
858     if (background) IDirect3DMaterial_Release(background);
859     if (surface3) IDirectDrawSurface3_Release(surface3);
860     if (surface) IDirectDrawSurface_Release(surface);
861     if (device) IDirect3DDevice_Release(device);
862     if (d3d) IDirect3D_Release(d3d);
863     if (ddraw) IDirectDraw_Release(ddraw);
864     DestroyWindow(window);
865 }
866
867 static void test_coop_level_threaded(void)
868 {
869     struct create_window_thread_param p;
870     IDirectDraw *ddraw;
871     HRESULT hr;
872
873     if (!(ddraw = create_ddraw()))
874     {
875         skip("Failed to create a ddraw object, skipping test.\n");
876         return;
877     }
878     create_window_thread(&p);
879
880     hr = IDirectDraw_SetCooperativeLevel(ddraw, p.window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
881     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
882
883     IDirectDraw_Release(ddraw);
884     destroy_window_thread(&p);
885 }
886
887 static ULONG get_refcount(IUnknown *test_iface)
888 {
889     IUnknown_AddRef(test_iface);
890     return IUnknown_Release(test_iface);
891 }
892
893 static void test_viewport_interfaces(void)
894 {
895     IDirectDraw *ddraw;
896     IDirect3D *d3d;
897     HRESULT hr;
898     ULONG ref;
899     IDirect3DViewport *viewport;
900     IDirect3DViewport2 *viewport2;
901     IDirect3DViewport3 *viewport3;
902     IDirectDrawGammaControl *gamma;
903     IUnknown *unknown;
904
905     if (!(ddraw = create_ddraw()))
906     {
907         skip("Failed to create ddraw object, skipping test.\n");
908         return;
909     }
910     hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
911     ok(SUCCEEDED(hr) || hr == E_NOINTERFACE, "Failed to get d3d interface, hr %#x.\n", hr);
912     if (FAILED(hr))
913     {
914         skip("Direct3D not available, skipping tests\n");
915         IDirectDraw_Release(ddraw);
916         return;
917     }
918     ref = get_refcount((IUnknown *)d3d);
919     ok(ref == 2, "IDirect3D refcount is %d\n", ref);
920
921     hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
922     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
923     ref = get_refcount((IUnknown *)viewport);
924     ok(ref == 1, "Initial IDirect3DViewport refcount is %u\n", ref);
925     ref = get_refcount((IUnknown *)d3d);
926     ok(ref == 2, "IDirect3D refcount is %u\n", ref);
927
928     /* E_FAIL return values are returned by Winetestbot Windows NT machines. While not supporting
929      * newer interfaces is legitimate for old ddraw versions, E_FAIL violates Microsoft's rules
930      * for QueryInterface, hence the broken() */
931     gamma = (IDirectDrawGammaControl *)0xdeadbeef;
932     hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirectDrawGammaControl, (void **)&gamma);
933     ok(hr == E_NOINTERFACE || broken(hr == E_FAIL), "Got unexpected hr %#x.\n", hr);
934     ok(gamma == NULL, "Interface not set to NULL by failed QI call: %p\n", gamma);
935     if (SUCCEEDED(hr)) IDirectDrawGammaControl_Release(gamma);
936     /* NULL iid: Segfaults */
937
938     hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirect3DViewport2, (void **)&viewport2);
939     ok(SUCCEEDED(hr) || hr == E_NOINTERFACE || broken(hr == E_FAIL),
940             "Failed to QI IDirect3DViewport2, hr %#x.\n", hr);
941     if (viewport2)
942     {
943         ref = get_refcount((IUnknown *)viewport);
944         ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
945         ref = get_refcount((IUnknown *)viewport2);
946         ok(ref == 2, "IDirect3DViewport2 refcount is %u\n", ref);
947         IDirect3DViewport2_Release(viewport2);
948         viewport2 = NULL;
949     }
950
951     hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirect3DViewport3, (void **)&viewport3);
952     ok(SUCCEEDED(hr) || hr == E_NOINTERFACE || broken(hr == E_FAIL),
953             "Failed to QI IDirect3DViewport3, hr %#x.\n", hr);
954     if (viewport3)
955     {
956         ref = get_refcount((IUnknown *)viewport);
957         ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
958         ref = get_refcount((IUnknown *)viewport3);
959         ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
960         IDirect3DViewport3_Release(viewport3);
961     }
962
963     hr = IDirect3DViewport_QueryInterface(viewport, &IID_IUnknown, (void **)&unknown);
964     ok(SUCCEEDED(hr), "Failed to QI IUnknown, hr %#x.\n", hr);
965     if (unknown)
966     {
967         ref = get_refcount((IUnknown *)viewport);
968         ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
969         ref = get_refcount(unknown);
970         ok(ref == 2, "IUnknown refcount is %u\n", ref);
971         IUnknown_Release(unknown);
972     }
973
974     IDirect3DViewport_Release(viewport);
975     IDirect3D_Release(d3d);
976     IDirectDraw_Release(ddraw);
977 }
978
979 static void test_zenable(void)
980 {
981     static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
982     static D3DTLVERTEX tquad[] =
983     {
984         {{  0.0f}, {480.0f}, {-0.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
985         {{  0.0f}, {  0.0f}, {-0.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
986         {{640.0f}, {480.0f}, { 1.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
987         {{640.0f}, {  0.0f}, { 1.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
988     };
989     IDirect3DExecuteBuffer *execute_buffer;
990     D3DMATERIALHANDLE background_handle;
991     D3DEXECUTEBUFFERDESC exec_desc;
992     IDirect3DMaterial *background;
993     IDirect3DViewport *viewport;
994     D3DEXECUTEDATA exec_data;
995     IDirect3DDevice *device;
996     IDirectDrawSurface *rt;
997     D3DMATERIAL material;
998     IDirectDraw *ddraw;
999     UINT inst_length;
1000     IDirect3D *d3d;
1001     D3DCOLOR color;
1002     HWND window;
1003     HRESULT hr;
1004     UINT x, y;
1005     UINT i, j;
1006     void *ptr;
1007
1008     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1009             0, 0, 640, 480, 0, 0, 0, 0);
1010     if (!(ddraw = create_ddraw()))
1011     {
1012         skip("Failed to create ddraw object, skipping test.\n");
1013         DestroyWindow(window);
1014         return;
1015     }
1016     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1017     {
1018         skip("Failed to create D3D device, skipping test.\n");
1019         IDirectDraw_Release(ddraw);
1020         DestroyWindow(window);
1021         return;
1022     }
1023
1024     hr = IDirect3DDevice_GetDirect3D(device, &d3d);
1025     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1026     hr = IDirect3D_CreateMaterial(d3d, &background, NULL);
1027     ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
1028
1029     viewport = create_viewport(device, 0, 0, 640, 480);
1030
1031     memset(&material, 0, sizeof(material));
1032     material.dwSize = sizeof(material);
1033     U1(U(material).diffuse).r = 1.0f;
1034     U2(U(material).diffuse).g = 0.0f;
1035     U3(U(material).diffuse).b = 0.0f;
1036     U4(U(material).diffuse).a = 1.0f;
1037     hr = IDirect3DMaterial_SetMaterial(background, &material);
1038     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
1039     hr = IDirect3DMaterial_GetHandle(background, device, &background_handle);
1040     ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
1041     hr = IDirect3DViewport_SetBackground(viewport, background_handle);
1042     ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
1043
1044     memset(&exec_desc, 0, sizeof(exec_desc));
1045     exec_desc.dwSize = sizeof(exec_desc);
1046     exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
1047     exec_desc.dwBufferSize = 1024;
1048     exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
1049
1050     hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
1051     ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
1052     hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
1053     ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
1054     memcpy(exec_desc.lpData, tquad, sizeof(tquad));
1055     ptr = ((BYTE *)exec_desc.lpData) + sizeof(tquad);
1056     emit_process_vertices(&ptr, 0, 4);
1057     emit_set_rs(&ptr, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
1058     emit_tquad(&ptr, 0);
1059     emit_end(&ptr);
1060     inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
1061     inst_length -= sizeof(tquad);
1062     hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
1063     ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
1064
1065     memset(&exec_data, 0, sizeof(exec_data));
1066     exec_data.dwSize = sizeof(exec_data);
1067     exec_data.dwVertexCount = 4;
1068     exec_data.dwInstructionOffset = sizeof(tquad);
1069     exec_data.dwInstructionLength = inst_length;
1070     hr = IDirect3DExecuteBuffer_SetExecuteData(execute_buffer, &exec_data);
1071     ok(SUCCEEDED(hr), "Failed to set execute data, hr %#x.\n", hr);
1072
1073     hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1074     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1075     hr = IDirect3DDevice_BeginScene(device);
1076     ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1077     hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1078     ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1079     hr = IDirect3DDevice_EndScene(device);
1080     ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1081
1082     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
1083     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1084     for (i = 0; i < 4; ++i)
1085     {
1086         for (j = 0; j < 4; ++j)
1087         {
1088             x = 80 * ((2 * j) + 1);
1089             y = 60 * ((2 * i) + 1);
1090             color = get_surface_color(rt, x, y);
1091             ok(compare_color(color, 0x0000ff00, 1),
1092                     "Expected color 0x0000ff00 at %u, %u, got 0x%08x.\n", x, y, color);
1093         }
1094     }
1095     IDirectDrawSurface_Release(rt);
1096
1097     destroy_viewport(device, viewport);
1098     IDirect3DExecuteBuffer_Release(execute_buffer);
1099     IDirect3DMaterial_Release(background);
1100     IDirect3D_Release(d3d);
1101     IDirect3DDevice_Release(device);
1102     IDirectDraw_Release(ddraw);
1103     DestroyWindow(window);
1104 }
1105
1106 static void test_ck_rgba(void)
1107 {
1108     static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1109     static D3DTLVERTEX tquad[] =
1110     {
1111         {{  0.0f}, {480.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1112         {{  0.0f}, {  0.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1113         {{640.0f}, {480.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1114         {{640.0f}, {  0.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1115         {{  0.0f}, {480.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1116         {{  0.0f}, {  0.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1117         {{640.0f}, {480.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1118         {{640.0f}, {  0.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1119     };
1120     static const struct
1121     {
1122         D3DCOLOR fill_color;
1123         BOOL color_key;
1124         BOOL blend;
1125         D3DCOLOR result1;
1126         D3DCOLOR result2;
1127     }
1128     tests[] =
1129     {
1130         {0xff00ff00, TRUE,  TRUE,  0x00ff0000, 0x000000ff},
1131         {0xff00ff00, TRUE,  FALSE, 0x00ff0000, 0x000000ff},
1132         {0xff00ff00, FALSE, TRUE,  0x0000ff00, 0x0000ff00},
1133         {0xff00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00},
1134         {0x7f00ff00, TRUE,  TRUE,  0x00807f00, 0x00807f00},
1135         {0x7f00ff00, TRUE,  FALSE, 0x0000ff00, 0x0000ff00},
1136         {0x7f00ff00, FALSE, TRUE,  0x00807f00, 0x00807f00},
1137         {0x7f00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00},
1138     };
1139
1140     IDirect3DExecuteBuffer *execute_buffer;
1141     D3DMATERIALHANDLE background_handle;
1142     D3DTEXTUREHANDLE texture_handle;
1143     D3DEXECUTEBUFFERDESC exec_desc;
1144     IDirect3DMaterial *background;
1145     IDirectDrawSurface *surface;
1146     IDirect3DViewport *viewport;
1147     DDSURFACEDESC surface_desc;
1148     IDirect3DTexture *texture;
1149     IDirect3DDevice *device;
1150     IDirectDrawSurface *rt;
1151     D3DMATERIAL material;
1152     IDirectDraw *ddraw;
1153     IDirect3D *d3d;
1154     D3DCOLOR color;
1155     HWND window;
1156     DDBLTFX fx;
1157     HRESULT hr;
1158     UINT i;
1159
1160     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1161             0, 0, 640, 480, 0, 0, 0, 0);
1162     if (!(ddraw = create_ddraw()))
1163     {
1164         skip("Failed to create ddraw object, skipping test.\n");
1165         DestroyWindow(window);
1166         return;
1167     }
1168     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1169     {
1170         skip("Failed to create D3D device, skipping test.\n");
1171         DestroyWindow(window);
1172         return;
1173     }
1174
1175     hr = IDirect3DDevice_GetDirect3D(device, &d3d);
1176     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1177
1178     viewport = create_viewport(device, 0, 0, 640, 480);
1179
1180     hr = IDirect3D_CreateMaterial(d3d, &background, NULL);
1181     ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
1182     memset(&material, 0, sizeof(material));
1183     material.dwSize = sizeof(material);
1184     U1(U(material).diffuse).r = 1.0f;
1185     U2(U(material).diffuse).g = 0.0f;
1186     U3(U(material).diffuse).b = 0.0f;
1187     U4(U(material).diffuse).a = 1.0f;
1188     hr = IDirect3DMaterial_SetMaterial(background, &material);
1189     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
1190     hr = IDirect3DMaterial_GetHandle(background, device, &background_handle);
1191     ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
1192     hr = IDirect3DViewport_SetBackground(viewport, background_handle);
1193     ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
1194
1195     IDirect3D_Release(d3d);
1196
1197     memset(&surface_desc, 0, sizeof(surface_desc));
1198     surface_desc.dwSize = sizeof(surface_desc);
1199     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1200     surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1201     surface_desc.dwWidth = 256;
1202     surface_desc.dwHeight = 256;
1203     surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
1204     surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
1205     U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
1206     U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1207     U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1208     U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
1209     U5(surface_desc.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
1210     surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0xff00ff00;
1211     surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0xff00ff00;
1212     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1213     ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
1214     hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
1215     ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1216     hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
1217     ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
1218     IDirect3DTexture_Release(texture);
1219
1220     memset(&exec_desc, 0, sizeof(exec_desc));
1221     exec_desc.dwSize = sizeof(exec_desc);
1222     exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
1223     exec_desc.dwBufferSize = 1024;
1224     exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
1225     hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
1226     ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
1227
1228     hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
1229     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1230
1231     for (i = 0; i < sizeof(tests) / sizeof(*tests); ++i)
1232     {
1233         UINT draw1_len, draw2_len;
1234         D3DEXECUTEDATA exec_data;
1235         void *ptr;
1236
1237         hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
1238         ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
1239         memcpy(exec_desc.lpData, tquad, sizeof(tquad));
1240         ptr = ((BYTE *)exec_desc.lpData) + sizeof(tquad);
1241         emit_process_vertices(&ptr, 0, 4);
1242         emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
1243         emit_set_rs(&ptr, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
1244         emit_set_rs(&ptr, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
1245         emit_set_rs(&ptr, D3DRENDERSTATE_COLORKEYENABLE, tests[i].color_key);
1246         emit_set_rs(&ptr, D3DRENDERSTATE_ALPHABLENDENABLE, tests[i].blend);
1247         emit_tquad(&ptr, 0);
1248         emit_end(&ptr);
1249         draw1_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - sizeof(tquad);
1250         emit_process_vertices(&ptr, 4, 4);
1251         emit_tquad(&ptr, 0);
1252         emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, 0);
1253         emit_end(&ptr);
1254         draw2_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw1_len;
1255         hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
1256         ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
1257
1258         memset(&fx, 0, sizeof(fx));
1259         fx.dwSize = sizeof(fx);
1260         U5(fx).dwFillColor = tests[i].fill_color;
1261         hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1262         ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1263
1264         memset(&exec_data, 0, sizeof(exec_data));
1265         exec_data.dwSize = sizeof(exec_data);
1266         exec_data.dwVertexCount = 8;
1267         exec_data.dwInstructionOffset = sizeof(tquad);
1268         exec_data.dwInstructionLength = draw1_len;
1269         hr = IDirect3DExecuteBuffer_SetExecuteData(execute_buffer, &exec_data);
1270         ok(SUCCEEDED(hr), "Failed to set execute data, hr %#x.\n", hr);
1271
1272         hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER);
1273         ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1274         hr = IDirect3DDevice_BeginScene(device);
1275         ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1276         hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1277         ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1278         hr = IDirect3DDevice_EndScene(device);
1279         ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1280
1281         color = get_surface_color(rt, 320, 240);
1282         if (i == 2 || i == 3)
1283             todo_wine ok(compare_color(color, tests[i].result1, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1284                     tests[i].result1, i, color);
1285         else
1286             ok(compare_color(color, tests[i].result1, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1287                     tests[i].result1, i, color);
1288
1289         U5(fx).dwFillColor = 0xff0000ff;
1290         hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1291         ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1292
1293         exec_data.dwInstructionOffset = sizeof(tquad) + draw1_len;
1294         exec_data.dwInstructionLength = draw2_len;
1295         hr = IDirect3DExecuteBuffer_SetExecuteData(execute_buffer, &exec_data);
1296         ok(SUCCEEDED(hr), "Failed to set execute data, hr %#x.\n", hr);
1297
1298         hr = IDirect3DDevice_BeginScene(device);
1299         ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1300         hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1301         ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1302         hr = IDirect3DDevice_EndScene(device);
1303         ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1304
1305         /* This tests that fragments that are masked out by the color key are
1306          * discarded, instead of just fully transparent. */
1307         color = get_surface_color(rt, 320, 240);
1308         if (i == 2 || i == 3)
1309             todo_wine ok(compare_color(color, tests[i].result2, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1310                     tests[i].result2, i, color);
1311         else
1312             ok(compare_color(color, tests[i].result2, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1313                     tests[i].result2, i, color);
1314     }
1315
1316     IDirectDrawSurface_Release(rt);
1317     IDirect3DExecuteBuffer_Release(execute_buffer);
1318     IDirectDrawSurface_Release(surface);
1319     destroy_viewport(device, viewport);
1320     IDirect3DMaterial_Release(background);
1321     IDirect3DDevice_Release(device);
1322     IDirectDraw_Release(ddraw);
1323     DestroyWindow(window);
1324 }
1325
1326 struct qi_test
1327 {
1328     REFIID iid;
1329     REFIID refcount_iid;
1330     HRESULT hr;
1331 };
1332
1333 static void test_qi(const char *test_name, IUnknown *base_iface,
1334         REFIID refcount_iid, const struct qi_test *tests, UINT entry_count)
1335 {
1336     ULONG refcount, expected_refcount;
1337     IUnknown *iface1, *iface2;
1338     HRESULT hr;
1339     UINT i, j;
1340
1341     for (i = 0; i < entry_count; ++i)
1342     {
1343         hr = IUnknown_QueryInterface(base_iface, tests[i].iid, (void **)&iface1);
1344         ok(hr == tests[i].hr, "Got hr %#x for test \"%s\" %u.\n", hr, test_name, i);
1345         if (SUCCEEDED(hr))
1346         {
1347             for (j = 0; j < entry_count; ++j)
1348             {
1349                 hr = IUnknown_QueryInterface(iface1, tests[j].iid, (void **)&iface2);
1350                 ok(hr == tests[j].hr, "Got hr %#x for test \"%s\" %u, %u.\n", hr, test_name, i, j);
1351                 if (SUCCEEDED(hr))
1352                 {
1353                     expected_refcount = 0;
1354                     if (IsEqualGUID(refcount_iid, tests[j].refcount_iid))
1355                         ++expected_refcount;
1356                     if (IsEqualGUID(tests[i].refcount_iid, tests[j].refcount_iid))
1357                         ++expected_refcount;
1358                     refcount = IUnknown_Release(iface2);
1359                     ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, %u, expected %u.\n",
1360                             refcount, test_name, i, j, expected_refcount);
1361                 }
1362             }
1363
1364             expected_refcount = 0;
1365             if (IsEqualGUID(refcount_iid, tests[i].refcount_iid))
1366                 ++expected_refcount;
1367             refcount = IUnknown_Release(iface1);
1368             ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, expected %u.\n",
1369                     refcount, test_name, i, expected_refcount);
1370         }
1371     }
1372 }
1373
1374 static void test_surface_qi(void)
1375 {
1376     static const struct qi_test tests[] =
1377     {
1378         {&IID_IDirect3DTexture2,        &IID_IDirectDrawSurface,        S_OK         },
1379         {&IID_IDirect3DTexture,         &IID_IDirectDrawSurface,        S_OK         },
1380         {&IID_IDirectDrawGammaControl,  &IID_IDirectDrawGammaControl,   S_OK         },
1381         {&IID_IDirectDrawColorControl,  NULL,                           E_NOINTERFACE},
1382         {&IID_IDirectDrawSurface7,      &IID_IDirectDrawSurface7,       S_OK         },
1383         {&IID_IDirectDrawSurface4,      &IID_IDirectDrawSurface4,       S_OK         },
1384         {&IID_IDirectDrawSurface3,      &IID_IDirectDrawSurface3,       S_OK         },
1385         {&IID_IDirectDrawSurface2,      &IID_IDirectDrawSurface2,       S_OK         },
1386         {&IID_IDirectDrawSurface,       &IID_IDirectDrawSurface,        S_OK         },
1387         {&IID_IDirect3DDevice7,         NULL,                           E_INVALIDARG },
1388         {&IID_IDirect3DDevice3,         NULL,                           E_INVALIDARG },
1389         {&IID_IDirect3DDevice2,         NULL,                           E_INVALIDARG },
1390         {&IID_IDirect3DDevice,          NULL,                           E_INVALIDARG },
1391         {&IID_IDirect3D7,               NULL,                           E_INVALIDARG },
1392         {&IID_IDirect3D3,               NULL,                           E_INVALIDARG },
1393         {&IID_IDirect3D2,               NULL,                           E_INVALIDARG },
1394         {&IID_IDirect3D,                NULL,                           E_INVALIDARG },
1395         {&IID_IDirectDraw7,             NULL,                           E_INVALIDARG },
1396         {&IID_IDirectDraw4,             NULL,                           E_INVALIDARG },
1397         {&IID_IDirectDraw3,             NULL,                           E_INVALIDARG },
1398         {&IID_IDirectDraw2,             NULL,                           E_INVALIDARG },
1399         {&IID_IDirectDraw,              NULL,                           E_INVALIDARG },
1400         {&IID_IDirect3DLight,           NULL,                           E_INVALIDARG },
1401         {&IID_IDirect3DMaterial,        NULL,                           E_INVALIDARG },
1402         {&IID_IDirect3DMaterial2,       NULL,                           E_INVALIDARG },
1403         {&IID_IDirect3DMaterial3,       NULL,                           E_INVALIDARG },
1404         {&IID_IDirect3DExecuteBuffer,   NULL,                           E_INVALIDARG },
1405         {&IID_IDirect3DViewport,        NULL,                           E_INVALIDARG },
1406         {&IID_IDirect3DViewport2,       NULL,                           E_INVALIDARG },
1407         {&IID_IDirect3DViewport3,       NULL,                           E_INVALIDARG },
1408         {&IID_IDirect3DVertexBuffer,    NULL,                           E_INVALIDARG },
1409         {&IID_IDirect3DVertexBuffer7,   NULL,                           E_INVALIDARG },
1410         {&IID_IDirectDrawPalette,       NULL,                           E_INVALIDARG },
1411         {&IID_IDirectDrawClipper,       NULL,                           E_INVALIDARG },
1412         {&IID_IUnknown,                 &IID_IDirectDrawSurface,        S_OK         },
1413     };
1414
1415     IDirectDrawSurface *surface;
1416     DDSURFACEDESC surface_desc;
1417     IDirect3DDevice *device;
1418     IDirectDraw *ddraw;
1419     HWND window;
1420     HRESULT hr;
1421
1422     if (!GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx"))
1423     {
1424         win_skip("DirectDrawCreateEx not available, skipping test.\n");
1425         return;
1426     }
1427
1428     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1429             0, 0, 640, 480, 0, 0, 0, 0);
1430     if (!(ddraw = create_ddraw()))
1431     {
1432         skip("Failed to create a ddraw object, skipping test.\n");
1433         return;
1434     }
1435     /* Try to create a D3D device to see if the ddraw implementation supports
1436      * D3D. 64-bit ddraw in particular doesn't seem to support D3D, and
1437      * doesn't support e.g. the IDirect3DTexture interfaces. */
1438     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1439     {
1440         skip("Failed to create D3D device, skipping test.\n");
1441         DestroyWindow(window);
1442         return;
1443     }
1444     IDirect3DDevice_Release(device);
1445
1446     memset(&surface_desc, 0, sizeof(surface_desc));
1447     surface_desc.dwSize = sizeof(surface_desc);
1448     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1449     surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1450     surface_desc.dwWidth = 512;
1451     surface_desc.dwHeight = 512;
1452     hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1453     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1454
1455     test_qi("surface_qi", (IUnknown *)surface, &IID_IDirectDrawSurface, tests, sizeof(tests) / sizeof(*tests));
1456
1457     IDirectDrawSurface_Release(surface);
1458     IDirectDraw_Release(ddraw);
1459     DestroyWindow(window);
1460 }
1461
1462 START_TEST(ddraw1)
1463 {
1464     test_coop_level_create_device_window();
1465     test_clipper_blt();
1466     test_coop_level_d3d_state();
1467     test_surface_interface_mismatch();
1468     test_coop_level_threaded();
1469     test_viewport_interfaces();
1470     test_zenable();
1471     test_ck_rgba();
1472     test_surface_qi();
1473 }