ddraw/tests: Move some helper functions up with the rest of the helper functions.
[wine] / dlls / ddraw / tests / ddraw2.c
1 /*
2  * Copyright 2011 Henri Verbeet for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 #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 IDirectDrawSurface *get_depth_stencil(IDirect3DDevice2 *device)
99 {
100     IDirectDrawSurface *rt, *ret;
101     DDSCAPS caps = {DDSCAPS_ZBUFFER};
102     HRESULT hr;
103
104     hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
105     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
106     hr = IDirectDrawSurface_GetAttachedSurface(rt, &caps, &ret);
107     ok(SUCCEEDED(hr) || hr == DDERR_NOTFOUND, "Failed to get the z buffer, hr %#x.\n", hr);
108     IDirectDrawSurface_Release(rt);
109     return ret;
110 }
111
112 static D3DCOLOR get_surface_color(IDirectDrawSurface *surface, UINT x, UINT y)
113 {
114     RECT rect = {x, y, x + 1, y + 1};
115     DDSURFACEDESC surface_desc;
116     D3DCOLOR color;
117     HRESULT hr;
118
119     memset(&surface_desc, 0, sizeof(surface_desc));
120     surface_desc.dwSize = sizeof(surface_desc);
121
122     hr = IDirectDrawSurface_Lock(surface, &rect, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
123     ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
124     if (FAILED(hr))
125         return 0xdeadbeef;
126
127     color = *((DWORD *)surface_desc.lpSurface) & 0x00ffffff;
128
129     hr = IDirectDrawSurface_Unlock(surface, NULL);
130     ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
131
132     return color;
133 }
134
135 static HRESULT CALLBACK enum_z_fmt(GUID *guid, char *description, char *name,
136         D3DDEVICEDESC *hal_desc, D3DDEVICEDESC *hel_desc, void *ctx)
137 {
138     DWORD *z_depth = ctx;
139
140     if (!IsEqualGUID(&IID_IDirect3DHALDevice, guid))
141         return D3DENUMRET_OK;
142
143     if (hal_desc->dwDeviceZBufferBitDepth & DDBD_32)
144         *z_depth = 32;
145     else if (hal_desc->dwDeviceZBufferBitDepth & DDBD_24)
146         *z_depth = 24;
147     else if (hal_desc->dwDeviceZBufferBitDepth & DDBD_16)
148         *z_depth = 16;
149
150     return DDENUMRET_OK;
151 }
152
153 static IDirectDraw2 *create_ddraw(void)
154 {
155     IDirectDraw2 *ddraw2;
156     IDirectDraw *ddraw1;
157     HRESULT hr;
158
159     if (FAILED(DirectDrawCreate(NULL, &ddraw1, NULL)))
160         return NULL;
161
162     hr = IDirectDraw_QueryInterface(ddraw1, &IID_IDirectDraw2, (void **)&ddraw2);
163     IDirectDraw_Release(ddraw1);
164     if (FAILED(hr))
165         return NULL;
166
167     return ddraw2;
168 }
169
170 static IDirect3DDevice2 *create_device(IDirectDraw2 *ddraw, HWND window, DWORD coop_level)
171 {
172     IDirectDrawSurface *surface, *ds;
173     IDirect3DDevice2 *device = NULL;
174     DDSURFACEDESC surface_desc;
175     DWORD z_depth = 0;
176     IDirect3D2 *d3d;
177     HRESULT hr;
178
179     hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, coop_level);
180     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
181
182     memset(&surface_desc, 0, sizeof(surface_desc));
183     surface_desc.dwSize = sizeof(surface_desc);
184     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
185     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
186     surface_desc.dwWidth = 640;
187     surface_desc.dwHeight = 480;
188
189     hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
190     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
191
192     if (coop_level & DDSCL_NORMAL)
193     {
194         IDirectDrawClipper *clipper;
195
196         hr = IDirectDraw2_CreateClipper(ddraw, 0, &clipper, NULL);
197         ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
198         hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
199         ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
200         hr = IDirectDrawSurface_SetClipper(surface, clipper);
201         ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
202         IDirectDrawClipper_Release(clipper);
203     }
204
205     hr = IDirectDraw2_QueryInterface(ddraw, &IID_IDirect3D2, (void **)&d3d);
206     if (FAILED(hr))
207     {
208         IDirectDrawSurface_Release(surface);
209         return NULL;
210     }
211
212     hr = IDirect3D2_EnumDevices(d3d, enum_z_fmt, &z_depth);
213     ok(SUCCEEDED(hr), "Failed to enumerate z-formats, hr %#x.\n", hr);
214     if (FAILED(hr) || !z_depth)
215     {
216         IDirect3D2_Release(d3d);
217         IDirectDrawSurface_Release(surface);
218         return NULL;
219     }
220
221     memset(&surface_desc, 0, sizeof(surface_desc));
222     surface_desc.dwSize = sizeof(surface_desc);
223     surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
224     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
225     U2(surface_desc).dwZBufferBitDepth = z_depth;
226     surface_desc.dwWidth = 640;
227     surface_desc.dwHeight = 480;
228     hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &ds, NULL);
229     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
230     if (FAILED(hr))
231     {
232         IDirect3D2_Release(d3d);
233         IDirectDrawSurface_Release(surface);
234         return NULL;
235     }
236
237     hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
238     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
239     IDirectDrawSurface_Release(ds);
240     if (FAILED(hr))
241     {
242         IDirect3D2_Release(d3d);
243         IDirectDrawSurface_Release(surface);
244         return NULL;
245     }
246
247     hr = IDirect3D2_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device);
248     IDirect3D2_Release(d3d);
249     IDirectDrawSurface_Release(surface);
250     if (FAILED(hr))
251         return NULL;
252
253     return device;
254 }
255
256 static HRESULT CALLBACK restore_callback(IDirectDrawSurface *surface, DDSURFACEDESC *desc, void *context)
257 {
258     HRESULT hr = IDirectDrawSurface_Restore(surface);
259     ok(SUCCEEDED(hr), "Failed to restore surface, hr %#x.\n", hr);
260     IDirectDrawSurface_Release(surface);
261
262     return DDENUMRET_OK;
263 }
264
265 static HRESULT restore_surfaces(IDirectDraw2 *ddraw)
266 {
267     return IDirectDraw2_EnumSurfaces(ddraw, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
268             NULL, NULL, restore_callback);
269 }
270
271 static void test_coop_level_create_device_window(void)
272 {
273     HWND focus_window, device_window;
274     IDirectDraw2 *ddraw;
275     HRESULT hr;
276
277     focus_window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
278             0, 0, 640, 480, 0, 0, 0, 0);
279     if (!(ddraw = create_ddraw()))
280     {
281         skip("Failed to create a ddraw object, skipping test.\n");
282         DestroyWindow(focus_window);
283         return;
284     }
285
286     hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
287     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
288     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
289     ok(!device_window, "Unexpected device window found.\n");
290     hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
291     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
292     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
293     ok(!device_window, "Unexpected device window found.\n");
294     hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
295     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
296     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
297     ok(!device_window, "Unexpected device window found.\n");
298     hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
299     ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
300     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
301     ok(!device_window, "Unexpected device window found.\n");
302     hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
303     ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
304     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
305     ok(!device_window, "Unexpected device window found.\n");
306
307     /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
308     if (broken(hr == DDERR_INVALIDPARAMS))
309     {
310         win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
311         IDirectDraw2_Release(ddraw);
312         DestroyWindow(focus_window);
313         return;
314     }
315
316     hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
317     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
318     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
319     ok(!device_window, "Unexpected device window found.\n");
320     hr = IDirectDraw2_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
321     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
322     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
323     ok(!device_window, "Unexpected device window found.\n");
324
325     hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
326     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
327     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
328     ok(!device_window, "Unexpected device window found.\n");
329     hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
330             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
331     ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
332     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
333     ok(!!device_window, "Device window not found.\n");
334
335     hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
336     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
337     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
338     ok(!device_window, "Unexpected device window found.\n");
339     hr = IDirectDraw2_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
340             | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
341     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
342     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
343     ok(!!device_window, "Device window not found.\n");
344
345     hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
346     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
347     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
348     ok(!device_window, "Unexpected device window found.\n");
349     hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
350     ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
351     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
352     ok(!device_window, "Unexpected device window found.\n");
353     hr = IDirectDraw2_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
354     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
355     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
356     ok(!device_window, "Unexpected device window found.\n");
357     hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
358     ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
359     device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
360     ok(!!device_window, "Device window not found.\n");
361
362     IDirectDraw2_Release(ddraw);
363     DestroyWindow(focus_window);
364 }
365
366 static void test_clipper_blt(void)
367 {
368     IDirectDrawSurface *src_surface, *dst_surface;
369     RECT client_rect, src_rect, *rect;
370     IDirectDrawClipper *clipper;
371     DDSURFACEDESC surface_desc;
372     unsigned int i, j, x, y;
373     IDirectDraw2 *ddraw;
374     RGNDATA *rgn_data;
375     D3DCOLOR color;
376     HRGN r1, r2;
377     HWND window;
378     DDBLTFX fx;
379     HRESULT hr;
380     DWORD *ptr;
381     DWORD ret;
382
383     static const DWORD src_data[] =
384     {
385         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
386         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
387         0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
388     };
389     static const D3DCOLOR expected1[] =
390     {
391         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
392         0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
393         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
394         0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
395     };
396     static const D3DCOLOR expected2[] =
397     {
398         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
399         0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
400         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
401         0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
402     };
403
404     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
405             10, 10, 640, 480, 0, 0, 0, 0);
406     ShowWindow(window, SW_SHOW);
407     if (!(ddraw = create_ddraw()))
408     {
409         skip("Failed to create a ddraw object, skipping test.\n");
410         DestroyWindow(window);
411         return;
412     }
413
414     ret = GetClientRect(window, &client_rect);
415     ok(ret, "Failed to get client rect.\n");
416     ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
417     ok(ret, "Failed to map client rect.\n");
418
419     hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
420     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
421
422     hr = IDirectDraw2_CreateClipper(ddraw, 0, &clipper, NULL);
423     ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
424     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
425     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
426     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
427     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
428     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
429     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
430     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
431     hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
432     ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
433     ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
434     ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
435     ok(rgn_data->rdh.nCount == 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
436     ok(rgn_data->rdh.nRgnSize == 16 || broken(rgn_data->rdh.nRgnSize == 168 /* NT4 */),
437             "Got unexpected region size %u.\n", rgn_data->rdh.nRgnSize);
438     ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
439             "Got unexpected bounding rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
440             rgn_data->rdh.rcBound.left, rgn_data->rdh.rcBound.top,
441             rgn_data->rdh.rcBound.right, rgn_data->rdh.rcBound.bottom,
442             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
443     rect = (RECT *)&rgn_data->Buffer[0];
444     ok(EqualRect(rect, &client_rect),
445             "Got unexpected clip rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
446             rect->left, rect->top, rect->right, rect->bottom,
447             client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
448     HeapFree(GetProcessHeap(), 0, rgn_data);
449
450     r1 = CreateRectRgn(0, 0, 320, 240);
451     ok(!!r1, "Failed to create region.\n");
452     r2 = CreateRectRgn(320, 240, 640, 480);
453     ok(!!r2, "Failed to create region.\n");
454     CombineRgn(r1, r1, r2, RGN_OR);
455     ret = GetRegionData(r1, 0, NULL);
456     rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
457     ret = GetRegionData(r1, ret, rgn_data);
458     ok(!!ret, "Failed to get region data.\n");
459
460     DeleteObject(r2);
461     DeleteObject(r1);
462
463     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
464     ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
465     hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
466     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
467     hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
468     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
469
470     HeapFree(GetProcessHeap(), 0, rgn_data);
471
472     memset(&surface_desc, 0, sizeof(surface_desc));
473     surface_desc.dwSize = sizeof(surface_desc);
474     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
475     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
476     surface_desc.dwWidth = 640;
477     surface_desc.dwHeight = 480;
478     surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
479     surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
480     U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
481     U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
482     U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
483     U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
484
485     hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
486     ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
487     hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
488     ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
489
490     memset(&fx, 0, sizeof(fx));
491     fx.dwSize = sizeof(fx);
492     hr = IDirectDrawSurface_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
493     ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
494     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
495     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
496
497     hr = IDirectDrawSurface_Lock(src_surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
498     ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
499     ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
500     ptr = surface_desc.lpSurface;
501     memcpy(&ptr[   0], &src_data[ 0], 6 * sizeof(DWORD));
502     memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
503     memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
504     hr = IDirectDrawSurface_Unlock(src_surface, NULL);
505     ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
506
507     hr = IDirectDrawSurface_SetClipper(dst_surface, clipper);
508     ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
509
510     SetRect(&src_rect, 1, 1, 5, 2);
511     hr = IDirectDrawSurface_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
512     ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
513     for (i = 0; i < 4; ++i)
514     {
515         for (j = 0; j < 4; ++j)
516         {
517             x = 80 * ((2 * j) + 1);
518             y = 60 * ((2 * i) + 1);
519             color = get_surface_color(dst_surface, x, y);
520             ok(compare_color(color, expected1[i * 4 + j], 1),
521                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
522         }
523     }
524
525     U5(fx).dwFillColor = 0xff0000ff;
526     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
527     ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
528     for (i = 0; i < 4; ++i)
529     {
530         for (j = 0; j < 4; ++j)
531         {
532             x = 80 * ((2 * j) + 1);
533             y = 60 * ((2 * i) + 1);
534             color = get_surface_color(dst_surface, x, y);
535             ok(compare_color(color, expected2[i * 4 + j], 1),
536                     "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
537         }
538     }
539
540     hr = IDirectDrawSurface_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
541     ok(hr == DDERR_BLTFASTCANTCLIP || broken(hr == E_NOTIMPL /* NT4 */), "Got unexpected hr %#x.\n", hr);
542
543     hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
544     ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
545     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
546     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
547     DestroyWindow(window);
548     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
549     ok(hr == E_FAIL, "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_GetClipList(clipper, NULL, NULL, &ret);
553     ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
554     hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
555     ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
556     hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
557     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
558     hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
559     ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
560
561     IDirectDrawSurface_Release(dst_surface);
562     IDirectDrawSurface_Release(src_surface);
563     IDirectDrawClipper_Release(clipper);
564     IDirectDraw2_Release(ddraw);
565 }
566
567 static void test_coop_level_d3d_state(void)
568 {
569     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
570     D3DMATERIALHANDLE background_handle;
571     IDirectDrawSurface *rt, *surface;
572     IDirect3DMaterial2 *background;
573     IDirect3DViewport2 *viewport;
574     IDirect3DDevice2 *device;
575     D3DMATERIAL material;
576     IDirectDraw2 *ddraw;
577     D3DVIEWPORT2 vp;
578     IDirect3D2 *d3d;
579     D3DCOLOR color;
580     DWORD value;
581     HWND window;
582     HRESULT hr;
583
584     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
585             0, 0, 640, 480, 0, 0, 0, 0);
586     if (!(ddraw = create_ddraw()))
587     {
588         skip("Failed to create ddraw object, skipping test.\n");
589         DestroyWindow(window);
590         return;
591     }
592     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
593     {
594         skip("Failed to create D3D device, skipping test.\n");
595         IDirectDraw2_Release(ddraw);
596         DestroyWindow(window);
597         return;
598     }
599
600     hr = IDirect3DDevice2_GetDirect3D(device, &d3d);
601     ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
602     hr = IDirect3D2_CreateViewport(d3d, &viewport, NULL);
603     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
604     hr = IDirect3D2_CreateMaterial(d3d, &background, NULL);
605     ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
606     IDirect3D2_Release(d3d);
607
608     hr = IDirect3DDevice2_AddViewport(device, viewport);
609     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
610     memset(&vp, 0, sizeof(vp));
611     vp.dwSize = sizeof(vp);
612     vp.dwX = 0;
613     vp.dwY = 0;
614     vp.dwWidth = 640;
615     vp.dwHeight = 480;
616     vp.dvClipX = -1.0f;
617     vp.dvClipY =  1.0f;
618     vp.dvClipWidth = 2.0f;
619     vp.dvClipHeight = 2.0f;
620     vp.dvMinZ = 0.0f;
621     vp.dvMaxZ = 1.0f;
622     hr = IDirect3DViewport2_SetViewport2(viewport, &vp);
623     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
624
625     memset(&material, 0, sizeof(material));
626     material.dwSize = sizeof(material);
627     U1(U(material).diffuse).r = 1.0f;
628     U2(U(material).diffuse).g = 0.0f;
629     U3(U(material).diffuse).b = 0.0f;
630     U4(U(material).diffuse).a = 1.0f;
631     hr = IDirect3DMaterial2_SetMaterial(background, &material);
632     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
633     hr = IDirect3DMaterial2_GetHandle(background, device, &background_handle);
634     ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
635     hr = IDirect3DViewport2_SetBackground(viewport, background_handle);
636     ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
637
638     hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
639     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
640     hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
641     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
642     ok(!!value, "Got unexpected z-enable state %#x.\n", value);
643     hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
644     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
645     ok(!value, "Got unexpected alpha blend enable state %#x.\n", value);
646     hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
647     ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
648     hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
649     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
650     color = get_surface_color(rt, 320, 240);
651     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
652
653     hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
654     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
655     hr = IDirectDrawSurface_IsLost(rt);
656     ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
657     hr = restore_surfaces(ddraw);
658     ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
659
660     memset(&material, 0, sizeof(material));
661     material.dwSize = sizeof(material);
662     U1(U(material).diffuse).r = 0.0f;
663     U2(U(material).diffuse).g = 1.0f;
664     U3(U(material).diffuse).b = 0.0f;
665     U4(U(material).diffuse).a = 1.0f;
666     hr = IDirect3DMaterial2_SetMaterial(background, &material);
667     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
668
669     hr = IDirect3DDevice2_GetRenderTarget(device, &surface);
670     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
671     ok(surface == rt, "Got unexpected surface %p.\n", surface);
672     hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
673     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
674     ok(!!value, "Got unexpected z-enable state %#x.\n", value);
675     hr = IDirect3DDevice2_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
676     ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
677     ok(!!value, "Got unexpected alpha blend enable state %#x.\n", value);
678     hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
679     ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
680     color = get_surface_color(rt, 320, 240);
681     ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
682
683     hr = IDirect3DDevice2_DeleteViewport(device, viewport);
684     ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
685     IDirect3DMaterial2_Release(background);
686     IDirect3DViewport2_Release(viewport);
687     IDirectDrawSurface_Release(surface);
688     IDirectDrawSurface_Release(rt);
689     IDirect3DDevice2_Release(device);
690     IDirectDraw2_Release(ddraw);
691     DestroyWindow(window);
692 }
693
694 static void test_surface_interface_mismatch(void)
695 {
696     IDirectDraw2 *ddraw = NULL;
697     IDirect3D2 *d3d = NULL;
698     IDirectDrawSurface *surface = NULL, *ds;
699     IDirectDrawSurface3 *surface3 = NULL;
700     IDirect3DDevice2 *device = NULL;
701     IDirect3DViewport2 *viewport = NULL;
702     IDirect3DMaterial2 *background = NULL;
703     DDSURFACEDESC surface_desc;
704     DWORD z_depth = 0;
705     ULONG refcount;
706     HRESULT hr;
707     D3DCOLOR color;
708     HWND window;
709     D3DVIEWPORT2 vp;
710     D3DMATERIAL material;
711     D3DMATERIALHANDLE background_handle;
712     D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
713
714     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
715             0, 0, 640, 480, 0, 0, 0, 0);
716
717     if (!(ddraw = create_ddraw()))
718     {
719         skip("Failed to create a ddraw object, skipping test.\n");
720         goto cleanup;
721     }
722
723     hr = IDirectDraw2_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
724     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
725
726     memset(&surface_desc, 0, sizeof(surface_desc));
727     surface_desc.dwSize = sizeof(surface_desc);
728     surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
729     surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
730     surface_desc.dwWidth = 640;
731     surface_desc.dwHeight = 480;
732
733     hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &surface, NULL);
734     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
735
736     hr = IDirectDrawSurface2_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
737     if (FAILED(hr))
738     {
739         skip("Failed to get the IDirectDrawSurface3 interface, skipping test.\n");
740         goto cleanup;
741     }
742
743     hr = IDirectDraw2_QueryInterface(ddraw, &IID_IDirect3D2, (void **)&d3d);
744     if (FAILED(hr))
745     {
746         skip("Failed to get the IDirect3D2 interface, skipping test.\n");
747         goto cleanup;
748     }
749
750     hr = IDirect3D2_EnumDevices(d3d, enum_z_fmt, &z_depth);
751     if (FAILED(hr) || !z_depth)
752     {
753         skip("No depth buffer formats available, skipping test.\n");
754         goto cleanup;
755     }
756
757     memset(&surface_desc, 0, sizeof(surface_desc));
758     surface_desc.dwSize = sizeof(surface_desc);
759     surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
760     surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
761     surface_desc.dwZBufferBitDepth = z_depth;
762     surface_desc.dwWidth = 640;
763     surface_desc.dwHeight = 480;
764     hr = IDirectDraw2_CreateSurface(ddraw, &surface_desc, &ds, NULL);
765     ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
766     if (FAILED(hr))
767         goto cleanup;
768
769     /* Using a different surface interface version still works */
770     hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
771     ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
772     refcount = IDirectDrawSurface_Release(ds);
773     ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
774     if (FAILED(hr))
775         goto cleanup;
776
777     /* Here too */
778     hr = IDirect3D2_CreateDevice(d3d, &IID_IDirect3DHALDevice, (IDirectDrawSurface *)surface3, &device);
779     ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
780     if (FAILED(hr))
781         goto cleanup;
782
783     hr = IDirect3D2_CreateViewport(d3d, &viewport, NULL);
784     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
785     hr = IDirect3D2_CreateMaterial(d3d, &background, NULL);
786     ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
787
788     hr = IDirect3DDevice2_AddViewport(device, viewport);
789     ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
790     memset(&vp, 0, sizeof(vp));
791     vp.dwSize = sizeof(vp);
792     vp.dwX = 0;
793     vp.dwY = 0;
794     vp.dwWidth = 640;
795     vp.dwHeight = 480;
796     vp.dvClipX = -1.0f;
797     vp.dvClipY =  1.0f;
798     vp.dvClipWidth = 2.0f;
799     vp.dvClipHeight = 2.0f;
800     vp.dvMinZ = 0.0f;
801     vp.dvMaxZ = 1.0f;
802     hr = IDirect3DViewport2_SetViewport2(viewport, &vp);
803     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
804
805     memset(&material, 0, sizeof(material));
806     material.dwSize = sizeof(material);
807     U1(U(material).diffuse).r = 1.0f;
808     U2(U(material).diffuse).g = 0.0f;
809     U3(U(material).diffuse).b = 0.0f;
810     U4(U(material).diffuse).a = 1.0f;
811     hr = IDirect3DMaterial2_SetMaterial(background, &material);
812     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
813     hr = IDirect3DMaterial2_GetHandle(background, device, &background_handle);
814     ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
815     hr = IDirect3DViewport2_SetBackground(viewport, background_handle);
816     ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
817
818     hr = IDirect3DViewport2_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
819     ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
820     color = get_surface_color(surface, 320, 240);
821     ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
822
823 cleanup:
824     if (viewport)
825     {
826         IDirect3DDevice2_DeleteViewport(device, viewport);
827         IDirect3DViewport2_Release(viewport);
828     }
829     if (background) IDirect3DMaterial2_Release(background);
830     if (surface3) IDirectDrawSurface3_Release(surface3);
831     if (surface) IDirectDrawSurface_Release(surface);
832     if (device) IDirect3DDevice2_Release(device);
833     if (d3d) IDirect3D2_Release(d3d);
834     if (ddraw) IDirectDraw2_Release(ddraw);
835     DestroyWindow(window);
836 }
837
838 static void test_coop_level_threaded(void)
839 {
840     struct create_window_thread_param p;
841     IDirectDraw2 *ddraw;
842     HRESULT hr;
843
844     if (!(ddraw = create_ddraw()))
845     {
846         skip("Failed to create a ddraw object, skipping test.\n");
847         return;
848     }
849     create_window_thread(&p);
850
851     hr = IDirectDraw2_SetCooperativeLevel(ddraw, p.window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
852     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
853
854     IDirectDraw2_Release(ddraw);
855     destroy_window_thread(&p);
856 }
857
858 static void test_depth_blit(void)
859 {
860     static D3DLVERTEX quad1[] =
861     {
862         {{-1.0}, { 1.0}, {0.50f}, 0, {0xff00ff00}},
863         {{ 1.0}, { 1.0}, {0.50f}, 0, {0xff00ff00}},
864         {{-1.0}, {-1.0}, {0.50f}, 0, {0xff00ff00}},
865         {{ 1.0}, {-1.0}, {0.50f}, 0, {0xff00ff00}},
866     };
867     static const D3DCOLOR expected_colors[4][4] =
868     {
869         {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
870         {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
871         {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
872         {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
873     };
874     static const BOOL todo[4][4] =
875     {
876         {FALSE, FALSE, TRUE, TRUE},
877         {FALSE, FALSE, TRUE, TRUE},
878         {TRUE,  TRUE,  TRUE, TRUE},
879         {TRUE,  TRUE,  TRUE, TRUE},
880     };
881     DDSURFACEDESC ddsd_new, ddsd_existing;
882
883     IDirect3DDevice2 *device;
884     IDirectDrawSurface *ds1, *ds2, *ds3, *rt;
885     IDirect3DViewport2 *viewport;
886     D3DVIEWPORT2 vp_data;
887     RECT src_rect, dst_rect;
888     unsigned int i, j;
889     D3DCOLOR color;
890     HRESULT hr;
891     IDirect3D2 *d3d;
892     IDirectDraw2 *ddraw;
893     DDBLTFX fx;
894     HWND window;
895     D3DRECT d3drect;
896     IDirect3DMaterial2 *background;
897     D3DMATERIALHANDLE background_handle;
898     D3DMATERIAL material;
899
900     window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
901             0, 0, 640, 480, 0, 0, 0, 0);
902     if (!(ddraw = create_ddraw()))
903     {
904         skip("Failed to create ddraw object, skipping test.\n");
905         DestroyWindow(window);
906         return;
907     }
908     if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
909     {
910         skip("Failed to create D3D device, skipping test.\n");
911         IDirectDraw2_Release(ddraw);
912         DestroyWindow(window);
913         return;
914     }
915
916     hr = IDirectDraw2_QueryInterface(ddraw, &IID_IDirect3D2, (void **)&d3d);
917     ok(SUCCEEDED(hr), "Failed to get Direct3D2 interface, hr %#x.\n", hr);
918     hr = IDirect3D2_CreateViewport(d3d, &viewport, NULL);
919     ok(SUCCEEDED(hr), "Failed to create a viewport, hr %#x.\n", hr);
920     hr = IDirect3D2_CreateMaterial(d3d, &background, NULL);
921     ok(SUCCEEDED(hr), "Failed to create a material, hr %#x.\n", hr);
922
923     ds1 = get_depth_stencil(device);
924
925     memset(&ddsd_new, 0, sizeof(ddsd_new));
926     ddsd_new.dwSize = sizeof(ddsd_new);
927     memset(&ddsd_existing, 0, sizeof(ddsd_existing));
928     ddsd_existing.dwSize = sizeof(ddsd_existing);
929     hr = IDirectDrawSurface_GetSurfaceDesc(ds1, &ddsd_existing);
930     ddsd_new.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
931     ddsd_new.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
932     ddsd_new.dwWidth = ddsd_existing.dwWidth;
933     ddsd_new.dwHeight = ddsd_existing.dwHeight;
934     U4(ddsd_new).ddpfPixelFormat = U4(ddsd_existing).ddpfPixelFormat;
935     hr = IDirectDraw2_CreateSurface(ddraw, &ddsd_new, &ds2, NULL);
936     ok(SUCCEEDED(hr), "Failed to create a surface, hr %#x.\n", hr);
937     hr = IDirectDraw2_CreateSurface(ddraw, &ddsd_new, &ds3, NULL);
938     ok(SUCCEEDED(hr), "Failed to create a surface, hr %#x.\n", hr);
939
940     hr = IDirect3DDevice2_AddViewport(device, viewport);
941     ok(SUCCEEDED(hr), "Failed to add viewport to device, hr %#x.\n", hr);
942     memset(&vp_data, 0, sizeof(vp_data));
943     vp_data.dwSize = sizeof(vp_data);
944     vp_data.dwWidth = ddsd_existing.dwWidth;
945     vp_data.dwHeight = ddsd_existing.dwHeight;
946     vp_data.dvMaxZ = 1.0;
947     vp_data.dvClipX = -1.0f;
948     vp_data.dvClipWidth = 2.0f;
949     vp_data.dvClipY = 1.0f;
950     vp_data.dvClipHeight = 2.0f;
951     hr = IDirect3DViewport2_SetViewport2(viewport, &vp_data);
952     ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
953     hr = IDirect3DDevice2_SetCurrentViewport(device, viewport);
954     ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
955
956     memset(&material, 0, sizeof(material));
957     material.dwSize = sizeof(material);
958     U1(U(material).diffuse).r = 1.0f;
959     U2(U(material).diffuse).g = 0.0f;
960     U3(U(material).diffuse).b = 0.0f;
961     U4(U(material).diffuse).a = 1.0f;
962     hr = IDirect3DMaterial2_SetMaterial(background, &material);
963     ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
964     hr = IDirect3DMaterial2_GetHandle(background, device, &background_handle);
965     ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
966     hr = IDirect3DViewport2_SetBackground(viewport, background_handle);
967     ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
968
969     hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
970     ok(SUCCEEDED(hr), "Failed to enable z testing, hr %#x.\n", hr);
971     hr = IDirect3DDevice2_SetRenderState(device, D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL);
972     ok(SUCCEEDED(hr), "Failed to set the z function, hr %#x.\n", hr);
973
974     U1(d3drect).x1 = U2(d3drect).y1 = 0;
975     U3(d3drect).x2 = vp_data.dwWidth; U4(d3drect).y2 = vp_data.dwHeight;
976     hr = IDirect3DViewport2_Clear(viewport, 1, &d3drect, D3DCLEAR_ZBUFFER);
977     ok(SUCCEEDED(hr), "Failed to clear the z buffer, hr %#x.\n", hr);
978
979     /* Partial blit. */
980     SetRect(&src_rect, 0, 0, 320, 240);
981     SetRect(&dst_rect, 0, 0, 320, 240);
982     hr = IDirectDrawSurface_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
983     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
984     /* Different locations. */
985     SetRect(&src_rect, 0, 0, 320, 240);
986     SetRect(&dst_rect, 320, 240, 640, 480);
987     hr = IDirectDrawSurface_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
988     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
989     /* Streched. */
990     SetRect(&src_rect, 0, 0, 320, 240);
991     SetRect(&dst_rect, 0, 0, 640, 480);
992     hr = IDirectDrawSurface_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
993     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
994     /* Flipped. */
995     SetRect(&src_rect, 0, 480, 640, 0);
996     SetRect(&dst_rect, 0, 0, 640, 480);
997     hr = IDirectDrawSurface_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
998     ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
999     SetRect(&src_rect, 0, 0, 640, 480);
1000     SetRect(&dst_rect, 0, 480, 640, 0);
1001     hr = IDirectDrawSurface_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1002     ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
1003     /* Full, explicit. */
1004     SetRect(&src_rect, 0, 0, 640, 480);
1005     SetRect(&dst_rect, 0, 0, 640, 480);
1006     hr = IDirectDrawSurface_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1007     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1008     /* Depth -> color blit: Succeeds on Win7 + Radeon HD 5700, fails on WinXP + Radeon X1600 */
1009
1010     /* Depth blit inside a BeginScene / EndScene pair */
1011     hr = IDirect3DDevice2_BeginScene(device);
1012     ok(SUCCEEDED(hr), "Failed to start a scene, hr %#x.\n", hr);
1013     /* From the current depth stencil */
1014     hr = IDirectDrawSurface_Blt(ds2, NULL, ds1, NULL, DDBLT_WAIT, NULL);
1015     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1016     /* To the current depth stencil */
1017     hr = IDirectDrawSurface_Blt(ds1, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1018     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1019     /* Between unbound surfaces */
1020     hr = IDirectDrawSurface_Blt(ds3, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1021     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1022     hr = IDirect3DDevice2_EndScene(device);
1023     ok(SUCCEEDED(hr), "Failed to end a scene, hr %#x.\n", hr);
1024
1025     /* Avoid changing the depth stencil, it doesn't work properly on Windows.
1026      * Instead use DDBLT_DEPTHFILL to clear the depth stencil. Unfortunately
1027      * drivers disagree on the meaning of dwFillDepth. Only 0 seems to produce
1028      * a reliable result(z = 0.0) */
1029     memset(&fx, 0, sizeof(fx));
1030     fx.dwSize = sizeof(fx);
1031     fx.dwFillDepth = 0;
1032     hr = IDirectDrawSurface_Blt(ds2, NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
1033     ok(SUCCEEDED(hr), "Failed to clear the source z buffer, hr %#x.\n", hr);
1034
1035     /* This clears the Z buffer with 1.0 */
1036     hr = IDirect3DViewport2_Clear(viewport, 1, &d3drect, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET);
1037     ok(SUCCEEDED(hr), "Failed to clear the color and z buffers, hr %#x.\n", hr);
1038
1039     SetRect(&dst_rect, 0, 0, 320, 240);
1040     hr = IDirectDrawSurface_Blt(ds1, &dst_rect, ds2, NULL, DDBLT_WAIT, NULL);
1041     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1042     IDirectDrawSurface_Release(ds3);
1043     IDirectDrawSurface_Release(ds2);
1044     IDirectDrawSurface_Release(ds1);
1045
1046     hr = IDirect3DDevice2_BeginScene(device);
1047     ok(SUCCEEDED(hr), "Failed to start a scene, hr %#x.\n", hr);
1048     hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DVT_LVERTEX, quad1, 4, 0);
1049     ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1050     hr = IDirect3DDevice2_EndScene(device);
1051     ok(SUCCEEDED(hr), "Failed to end a scene, hr %#x.\n", hr);
1052
1053     hr = IDirect3DDevice2_GetRenderTarget(device, &rt);
1054     ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1055     for (i = 0; i < 4; ++i)
1056     {
1057         for (j = 0; j < 4; ++j)
1058         {
1059             unsigned int x = 80 * ((2 * j) + 1);
1060             unsigned int y = 60 * ((2 * i) + 1);
1061             color = get_surface_color(rt, x, y);
1062             if (todo[i][j])
1063                 todo_wine ok(compare_color(color, expected_colors[i][j], 1),
1064                         "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected_colors[i][j], x, y, color);
1065             else
1066                 ok(compare_color(color, expected_colors[i][j], 1),
1067                         "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected_colors[i][j], x, y, color);
1068         }
1069     }
1070     IDirectDrawSurface_Release(rt);
1071
1072     hr = IDirect3DDevice2_DeleteViewport(device, viewport);
1073     ok(SUCCEEDED(hr), "Failed to delete viewport from device, hr %#x.\n", hr);
1074     IDirect3DMaterial2_Release(background);
1075     IDirect3DViewport2_Release(viewport);
1076     IDirect3D2_Release(d3d);
1077     IDirect3DDevice2_Release(device);
1078     IDirectDraw2_Release(ddraw);
1079     DestroyWindow(window);
1080 }
1081
1082 static void test_texture_load_ckey(void)
1083 {
1084     IDirectDraw2 *ddraw = NULL;
1085     IDirectDrawSurface *src = NULL;
1086     IDirectDrawSurface *dst = NULL;
1087     IDirect3DTexture *src_tex = NULL;
1088     IDirect3DTexture *dst_tex = NULL;
1089     DDSURFACEDESC ddsd;
1090     HRESULT hr;
1091     DDCOLORKEY ckey;
1092
1093     if (!(ddraw = create_ddraw()))
1094     {
1095         skip("Failed to create ddraw object, skipping test.\n");
1096         return;
1097     }
1098     hr = IDirectDraw2_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
1099     ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1100
1101     memset(&ddsd, 0, sizeof(ddsd));
1102     ddsd.dwSize = sizeof(ddsd);
1103     ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
1104     ddsd.dwHeight = 128;
1105     ddsd.dwWidth = 128;
1106     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY;
1107     hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &src, NULL);
1108     ok(SUCCEEDED(hr), "Failed to create source texture, hr %#x.\n", hr);
1109     ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1110     hr = IDirectDraw2_CreateSurface(ddraw, &ddsd, &dst, NULL);
1111     ok(SUCCEEDED(hr), "Failed to create destination texture, hr %#x.\n", hr);
1112
1113     hr = IDirectDrawSurface_QueryInterface(src, &IID_IDirect3DTexture, (void **)&src_tex);
1114     ok(SUCCEEDED(hr) || hr == E_NOINTERFACE, "Failed to get Direct3DTexture interface, hr %#x.\n", hr);
1115     if (FAILED(hr))
1116     {
1117         /* 64 bit ddraw does not support d3d */
1118         skip("Could not get Direct3DTexture interface, skipping texture::Load color keying tests.\n");
1119         goto done;
1120     }
1121     hr = IDirectDrawSurface_QueryInterface(dst, &IID_IDirect3DTexture, (void **)&dst_tex);
1122     ok(SUCCEEDED(hr), "Failed to get Direct3DTexture interface, hr %#x.\n", hr);
1123
1124     /* No surface has a color key */
1125     hr = IDirect3DTexture_Load(dst_tex, src_tex);
1126     ok(SUCCEEDED(hr) || broken(hr == DDERR_INVALIDCAPS), "Got unexpected hr %#x.\n", hr);
1127     if (FAILED(hr))
1128     {
1129         /* Testbot Windows NT VMs */
1130         skip("IDirect3DTexture::Load does not work, skipping color keying tests.\n");
1131         goto done;
1132     }
1133
1134     ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0xdeadbeef;
1135     hr = IDirectDrawSurface_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1136     ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1137     ok(ckey.dwColorSpaceLowValue == 0xdeadbeef, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1138     ok(ckey.dwColorSpaceHighValue == 0xdeadbeef, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1139
1140     /* Source surface has a color key */
1141     ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x0000ff00;
1142     hr = IDirectDrawSurface_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
1143     ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1144     hr = IDirect3DTexture_Load(dst_tex, src_tex);
1145     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1146     hr = IDirectDrawSurface_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1147     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1148     ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1149     ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1150
1151     /* Both surfaces have a color key: Dest ckey is overwritten */
1152     ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x000000ff;
1153     hr = IDirectDrawSurface_SetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1154     ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1155     hr = IDirect3DTexture_Load(dst_tex, src_tex);
1156     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1157     hr = IDirectDrawSurface_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1158     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1159     ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1160     ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1161
1162     /* Only the destination has a color key: It is not deleted */
1163     hr = IDirectDrawSurface_SetColorKey(src, DDCKEY_SRCBLT, NULL);
1164     ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1165     hr = IDirectDrawSurface_GetColorKey(src, DDCKEY_SRCBLT, &ckey);
1166     ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1167     hr = IDirect3DTexture_Load(dst_tex, src_tex);
1168     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1169     hr = IDirectDrawSurface_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1170     ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1171     ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1172     ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1173
1174 done:
1175     if (dst_tex) IDirect3DTexture_Release(dst_tex);
1176     if (src_tex) IDirect3DTexture_Release(src_tex);
1177     if (dst) IDirectDrawSurface_Release(dst);
1178     if (src) IDirectDrawSurface_Release(src);
1179     if (ddraw) IDirectDraw2_Release(ddraw);
1180 }
1181
1182 static LONG get_refcount(IUnknown *test_iface)
1183 {
1184     IUnknown_AddRef(test_iface);
1185     return IUnknown_Release(test_iface);
1186 }
1187
1188 static void test_viewport_interfaces(void)
1189 {
1190     IDirectDraw2 *ddraw;
1191     IDirect3D2 *d3d;
1192     HRESULT hr;
1193     LONG ref, old_d3d_ref;
1194     IDirect3DViewport *viewport;
1195     IDirect3DViewport2 *viewport2;
1196     IDirect3DViewport3 *viewport3;
1197     IDirectDrawGammaControl *gamma;
1198     IUnknown *unknown;
1199
1200     if (!(ddraw = create_ddraw()))
1201     {
1202         skip("Failed to create ddraw object, skipping test.\n");
1203         return;
1204     }
1205
1206     hr = IDirectDraw2_QueryInterface(ddraw, &IID_IDirect3D2, (void **)&d3d);
1207     ok(SUCCEEDED(hr) || hr == E_NOINTERFACE, "Failed to get d3d interface, hr %#x.\n", hr);
1208     if (FAILED(hr))
1209     {
1210         skip("Direct3D not available, skipping tests\n");
1211         IDirectDraw2_Release(ddraw);
1212         return;
1213     }
1214     old_d3d_ref = get_refcount((IUnknown *)d3d);
1215
1216     hr = IDirect3D2_CreateViewport(d3d, &viewport2, NULL);
1217     ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1218     ref = get_refcount((IUnknown *)viewport2);
1219     ok(ref == 1, "Initial IDirect3DViewport2 refcount is %d\n", ref);
1220     ref = get_refcount((IUnknown *)d3d);
1221     ok(ref == old_d3d_ref, "IDirect3D2 refcount is %d\n", ref);
1222
1223     gamma = (IDirectDrawGammaControl *)0xdeadbeef;
1224     hr = IDirect3DViewport2_QueryInterface(viewport2, &IID_IDirectDrawGammaControl, (void **)&gamma);
1225     ok(hr == E_NOINTERFACE, "Got unexpected hr %#x.\n", hr);
1226     ok(gamma == NULL, "Interface not set to NULL by failed QI call: %p\n", gamma);
1227     if (SUCCEEDED(hr)) IDirectDrawGammaControl_Release(gamma);
1228     /* NULL iid: Segfaults */
1229
1230     hr = IDirect3DViewport2_QueryInterface(viewport2, &IID_IDirect3DViewport, (void **)&viewport);
1231     ok(SUCCEEDED(hr), "Failed to QI IDirect3DViewport, hr %#x.\n", hr);
1232     if (viewport)
1233     {
1234         ref = get_refcount((IUnknown *)viewport);
1235         ok(ref == 2, "IDirect3DViewport refcount is %d\n", ref);
1236         ref = get_refcount((IUnknown *)viewport2);
1237         ok(ref == 2, "IDirect3DViewport2 refcount is %d\n", ref);
1238         IDirect3DViewport_Release(viewport);
1239         viewport = NULL;
1240     }
1241
1242     hr = IDirect3DViewport2_QueryInterface(viewport2, &IID_IDirect3DViewport3, (void **)&viewport3);
1243     ok(SUCCEEDED(hr), "Failed to QI IDirect3DViewport3, hr %#x.\n", hr);
1244     if (viewport3)
1245     {
1246         ref = get_refcount((IUnknown *)viewport2);
1247         ok(ref == 2, "IDirect3DViewport2 refcount is %d\n", ref);
1248         ref = get_refcount((IUnknown *)viewport3);
1249         ok(ref == 2, "IDirect3DViewport3 refcount is %d\n", ref);
1250         IDirect3DViewport3_Release(viewport3);
1251     }
1252
1253     hr = IDirect3DViewport2_QueryInterface(viewport2, &IID_IUnknown, (void **)&unknown);
1254     ok(SUCCEEDED(hr), "Failed to QI IUnknown, hr %#x.\n", hr);
1255     if (unknown)
1256     {
1257         ref = get_refcount((IUnknown *)viewport2);
1258         ok(ref == 2, "IDirect3DViewport2 refcount is %d\n", ref);
1259         ref = get_refcount(unknown);
1260         ok(ref == 2, "IUnknown refcount is %d\n", ref);
1261         IUnknown_Release(unknown);
1262     }
1263
1264     IDirect3DViewport2_Release(viewport2);
1265     IDirect3D2_Release(d3d);
1266     IDirectDraw2_Release(ddraw);
1267 }
1268
1269 START_TEST(ddraw2)
1270 {
1271     test_coop_level_create_device_window();
1272     test_clipper_blt();
1273     test_coop_level_d3d_state();
1274     test_surface_interface_mismatch();
1275     test_coop_level_threaded();
1276     test_depth_blit();
1277     test_texture_load_ckey();
1278     test_viewport_interfaces();
1279 }